blob: 7450b951b6da95ef7d037a72a25a8bdd8fa571f4 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
San Mehat3c5a6f02009-05-22 15:36:13 -070016
San Mehatdc266072009-05-06 11:16:52 -070017#include <stdio.h>
18#include <errno.h>
19
20#define LOG_TAG "Nexus"
21
22#include <cutils/log.h>
23
24#include "NetworkManager.h"
San Mehat192331d2009-05-22 13:58:06 -070025#include "InterfaceConfig.h"
San Mehatd6c67962009-06-22 10:39:36 -070026#include "DhcpClient.h"
San Mehatdc266072009-05-06 11:16:52 -070027
San Mehat1441e762009-05-07 11:37:10 -070028NetworkManager *NetworkManager::sInstance = NULL;
29
30NetworkManager *NetworkManager::Instance() {
31 if (!sInstance)
San Mehat3c5a6f02009-05-22 15:36:13 -070032 sInstance = new NetworkManager(new PropertyManager());
San Mehat1441e762009-05-07 11:37:10 -070033 return sInstance;
34}
San Mehatdc266072009-05-06 11:16:52 -070035
San Mehat3c5a6f02009-05-22 15:36:13 -070036NetworkManager::NetworkManager(PropertyManager *propMngr) {
San Mehat1441e762009-05-07 11:37:10 -070037 mBroadcaster = NULL;
San Mehatdc266072009-05-06 11:16:52 -070038 mControllers = new ControllerCollection();
San Mehat3c5a6f02009-05-22 15:36:13 -070039 mPropMngr = propMngr;
San Mehatd6c67962009-06-22 10:39:36 -070040 mDhcp = new DhcpClient(this);
San Mehat3c5a6f02009-05-22 15:36:13 -070041}
42
43NetworkManager::~NetworkManager() {
San Mehatdc266072009-05-06 11:16:52 -070044}
45
46int NetworkManager::run() {
San Mehatdc266072009-05-06 11:16:52 -070047 if (startControllers()) {
48 LOGW("Unable to start all controllers (%s)", strerror(errno));
49 }
San Mehatdc266072009-05-06 11:16:52 -070050 return 0;
51}
52
San Mehat1441e762009-05-07 11:37:10 -070053int NetworkManager::attachController(Controller *c) {
San Mehatdc266072009-05-06 11:16:52 -070054 mControllers->push_back(c);
San Mehat1441e762009-05-07 11:37:10 -070055 return 0;
San Mehatdc266072009-05-06 11:16:52 -070056}
57
58int NetworkManager::startControllers() {
59 int rc = 0;
60 ControllerCollection::iterator i;
61
62 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
63 int irc = (*i)->start();
64 LOGD("Controller '%s' start rc = %d", (*i)->getName(), irc);
San Mehat3c5a6f02009-05-22 15:36:13 -070065 if (irc && !rc)
San Mehatdc266072009-05-06 11:16:52 -070066 rc = irc;
67 }
68 return rc;
69}
70
71int NetworkManager::stopControllers() {
72 int rc = 0;
73 ControllerCollection::iterator i;
74
75 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
76 int irc = (*i)->stop();
77 LOGD("Controller '%s' stop rc = %d", (*i)->getName(), irc);
San Mehat3c5a6f02009-05-22 15:36:13 -070078 if (irc && !rc)
San Mehatdc266072009-05-06 11:16:52 -070079 rc = irc;
80 }
81 return rc;
82}
83
84Controller *NetworkManager::findController(const char *name) {
85 ControllerCollection::iterator i;
86 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
87 if (!strcmp((*i)->getName(), name))
88 return *i;
89 }
90 LOGW("Controller '%s' not found", name);
91 return NULL;
92}
93
San Mehatd6c67962009-06-22 10:39:36 -070094void NetworkManager::onInterfaceConnected(Controller *c, const InterfaceConfig *cfg) {
95 LOGD("Controller %s interface %s connected", c->getName(), c->getBoundInterface());
San Mehat192331d2009-05-22 13:58:06 -070096
97 // Look up the interface
98
99 if (0) { // already started?
San Mehat192331d2009-05-22 13:58:06 -0700100 }
101
San Mehat3aff2d12009-06-15 14:10:44 -0700102 if (cfg) {
San Mehatd6c67962009-06-22 10:39:36 -0700103 if (cfg->getUseDhcp() && mDhcp->start(c->getBoundInterface())) {
104 LOGE("DHCP start failed");
105 } else if (!cfg->getUseDhcp()) {
San Mehat3aff2d12009-06-15 14:10:44 -0700106 // Static configuration
107 }
San Mehat192331d2009-05-22 13:58:06 -0700108 } else {
San Mehat3aff2d12009-06-15 14:10:44 -0700109 LOGD("No InterfaceConfig for %s:%s - assuming self-managed",
110 c->getName(), c->getBoundInterface());
San Mehat192331d2009-05-22 13:58:06 -0700111 }
San Mehatdc266072009-05-06 11:16:52 -0700112}
113
San Mehatd6c67962009-06-22 10:39:36 -0700114void NetworkManager::onInterfaceDisconnected(Controller *c, const char *name) {
115 LOGD("Controller %s interface %s disconnected", c->getName(), name);
116
117 // If we have a DHCP request out on this interface then stop it
118 if (1) {
119 mDhcp->stop();
120 }
San Mehatdc266072009-05-06 11:16:52 -0700121}