2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #define LOG_TAG "Nexus"
22 #include <cutils/log.h>
24 #include "NetworkManager.h"
25 #include "InterfaceConfig.h"
26 #include "DhcpClient.h"
27 #include "DhcpState.h"
28 #include "DhcpEvent.h"
29 #include "ResponseCode.h"
31 NetworkManager *NetworkManager::sInstance = NULL;
33 NetworkManager *NetworkManager::Instance() {
35 sInstance = new NetworkManager(new PropertyManager());
39 NetworkManager::NetworkManager(PropertyManager *propMngr) {
41 mControllerBindings = new ControllerBindingCollection();
43 mLastDhcpState = DhcpState::INIT;
44 mDhcp = new DhcpClient(this);
47 NetworkManager::~NetworkManager() {
50 int NetworkManager::run() {
51 if (startControllers()) {
52 LOGW("Unable to start all controllers (%s)", strerror(errno));
57 int NetworkManager::attachController(Controller *c) {
58 ControllerBinding *cb = new ControllerBinding(c);
59 mControllerBindings->push_back(cb);
63 int NetworkManager::startControllers() {
65 ControllerBindingCollection::iterator it;
67 for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) {
68 int irc = (*it)->getController()->start();
75 int NetworkManager::stopControllers() {
77 ControllerBindingCollection::iterator it;
79 for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) {
80 int irc = (*it)->getController()->stop();
87 NetworkManager::ControllerBinding *NetworkManager::lookupBinding(Controller *c) {
88 ControllerBindingCollection::iterator it;
90 for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) {
91 if ((*it)->getController() == c)
98 Controller *NetworkManager::findController(const char *name) {
99 ControllerBindingCollection::iterator it;
101 for (it = mControllerBindings->begin(); it != mControllerBindings->end(); ++it) {
102 if (!strcasecmp((*it)->getController()->getName(), name))
103 return (*it)->getController();
109 void NetworkManager::onInterfaceConnected(Controller *c) {
110 LOGD("Controller %s interface %s connected", c->getName(), c->getBoundInterface());
112 if (mDhcp->start(c)) {
113 LOGE("Failed to start DHCP (%s)", strerror(errno));
118 void NetworkManager::onInterfaceDisconnected(Controller *c) {
119 LOGD("Controller %s interface %s disconnected", c->getName(),
120 c->getBoundInterface());
125 void NetworkManager::onControllerSuspending(Controller *c) {
126 LOGD("Controller %s interface %s suspending", c->getName(),
127 c->getBoundInterface());
131 void NetworkManager::onControllerResumed(Controller *c) {
132 LOGD("Controller %s interface %s resumed", c->getName(),
133 c->getBoundInterface());
136 void NetworkManager::onDhcpStateChanged(Controller *c, int state) {
140 LOGD("onDhcpStateChanged(%s -> %s)",
141 DhcpState::toString(mLastDhcpState, tmp, sizeof(tmp)),
142 DhcpState::toString(state, tmp2, sizeof(tmp2)));
145 case DhcpState::BOUND:
146 // Refresh the 'net.xxx' for the controller
148 case DhcpState::RENEWING:
156 "DHCP state changed from %d (%s) -> %d (%s)",
158 DhcpState::toString(mLastDhcpState, tmp, sizeof(tmp)),
160 DhcpState::toString(state, tmp2, sizeof(tmp2)));
162 getBroadcaster()->sendBroadcast(ResponseCode::DhcpStateChange,
167 mLastDhcpState = state;
170 void NetworkManager::onDhcpEvent(Controller *c, int evt) {
172 LOGD("onDhcpEvent(%s)", DhcpEvent::toString(evt, tmp, sizeof(tmp)));
175 void NetworkManager::onDhcpLeaseUpdated(Controller *c, struct in_addr *addr,
179 struct in_addr *dns1,
180 struct in_addr *dns2) {
181 ControllerBinding *bind = lookupBinding(c);
183 if (!bind->getCurrentCfg())
184 bind->setCurrentCfg(new InterfaceConfig(true));
186 bind->getCurrentCfg()->setIp(addr);
187 bind->getCurrentCfg()->setNetmask(net);
188 bind->getCurrentCfg()->setGateway(gw);
189 bind->getCurrentCfg()->setBroadcast(brd);
190 bind->getCurrentCfg()->setDns(0, dns1);
191 bind->getCurrentCfg()->setDns(1, dns2);
194 NetworkManager::ControllerBinding::ControllerBinding(Controller *c) :
198 void NetworkManager::ControllerBinding::setCurrentCfg(InterfaceConfig *c) {
202 void NetworkManager::ControllerBinding::setBoundCfg(InterfaceConfig *c) {