NetworkManager.cpp revision 3aff2d1de59972684bf2ab798351be5544158239
1/* 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 */ 16 17#include <stdio.h> 18#include <errno.h> 19 20#define LOG_TAG "Nexus" 21 22#include <cutils/log.h> 23 24#include "NetworkManager.h" 25#include "InterfaceConfig.h" 26 27NetworkManager *NetworkManager::sInstance = NULL; 28 29NetworkManager *NetworkManager::Instance() { 30 if (!sInstance) 31 sInstance = new NetworkManager(new PropertyManager()); 32 return sInstance; 33} 34 35NetworkManager::NetworkManager(PropertyManager *propMngr) { 36 mBroadcaster = NULL; 37 mControllers = new ControllerCollection(); 38 mPropMngr = propMngr; 39} 40 41NetworkManager::~NetworkManager() { 42} 43 44int NetworkManager::run() { 45 if (startControllers()) { 46 LOGW("Unable to start all controllers (%s)", strerror(errno)); 47 } 48 return 0; 49} 50 51int NetworkManager::attachController(Controller *c) { 52 mControllers->push_back(c); 53 return 0; 54} 55 56int NetworkManager::startControllers() { 57 int rc = 0; 58 ControllerCollection::iterator i; 59 60 for (i = mControllers->begin(); i != mControllers->end(); ++i) { 61 int irc = (*i)->start(); 62 LOGD("Controller '%s' start rc = %d", (*i)->getName(), irc); 63 if (irc && !rc) 64 rc = irc; 65 } 66 return rc; 67} 68 69int NetworkManager::stopControllers() { 70 int rc = 0; 71 ControllerCollection::iterator i; 72 73 for (i = mControllers->begin(); i != mControllers->end(); ++i) { 74 int irc = (*i)->stop(); 75 LOGD("Controller '%s' stop rc = %d", (*i)->getName(), irc); 76 if (irc && !rc) 77 rc = irc; 78 } 79 return rc; 80} 81 82Controller *NetworkManager::findController(const char *name) { 83 ControllerCollection::iterator i; 84 for (i = mControllers->begin(); i != mControllers->end(); ++i) { 85 if (!strcmp((*i)->getName(), name)) 86 return *i; 87 } 88 LOGW("Controller '%s' not found", name); 89 return NULL; 90} 91 92void NetworkManager::onInterfaceStarted(Controller *c, const InterfaceConfig *cfg) { 93 LOGD("Interface %s started by controller %s", c->getBoundInterface(), c->getName()); 94 95 // Look up the interface 96 97 if (0) { // already started? 98 } 99 100 if (cfg) { 101 if (cfg->getUseDhcp()) { 102 // Launch DHCP thread 103 } else { 104 // Static configuration 105 } 106 } else { 107 LOGD("No InterfaceConfig for %s:%s - assuming self-managed", 108 c->getName(), c->getBoundInterface()); 109 } 110} 111 112void NetworkManager::onInterfaceStopping(Controller *c, const char *name) { 113 LOGD("Interface %s stopped by controller %s", name, c->getName()); 114} 115