blob: 49b0210a8bb8741796feeb494a5df3788c153e15 [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 */
16#include <stdio.h>
17#include <errno.h>
18
19#define LOG_TAG "Nexus"
20
21#include <cutils/log.h>
22
23#include "NetworkManager.h"
San Mehat192331d2009-05-22 13:58:06 -070024#include "InterfaceConfig.h"
San Mehatdc266072009-05-06 11:16:52 -070025
San Mehat1441e762009-05-07 11:37:10 -070026NetworkManager *NetworkManager::sInstance = NULL;
27
28NetworkManager *NetworkManager::Instance() {
29 if (!sInstance)
30 sInstance = new NetworkManager();
31 return sInstance;
32}
San Mehatdc266072009-05-06 11:16:52 -070033
34NetworkManager::NetworkManager() {
San Mehat1441e762009-05-07 11:37:10 -070035 mBroadcaster = NULL;
San Mehatdc266072009-05-06 11:16:52 -070036 mControllers = new ControllerCollection();
37}
38
39int NetworkManager::run() {
San Mehatdc266072009-05-06 11:16:52 -070040 if (startControllers()) {
41 LOGW("Unable to start all controllers (%s)", strerror(errno));
42 }
San Mehatdc266072009-05-06 11:16:52 -070043 return 0;
44}
45
San Mehat1441e762009-05-07 11:37:10 -070046int NetworkManager::attachController(Controller *c) {
San Mehatdc266072009-05-06 11:16:52 -070047 mControllers->push_back(c);
San Mehat1441e762009-05-07 11:37:10 -070048 return 0;
San Mehatdc266072009-05-06 11:16:52 -070049}
50
51int NetworkManager::startControllers() {
52 int rc = 0;
53 ControllerCollection::iterator i;
54
55 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
56 int irc = (*i)->start();
57 LOGD("Controller '%s' start rc = %d", (*i)->getName(), irc);
58 if (irc && !rc)
59 rc = irc;
60 }
61 return rc;
62}
63
64int NetworkManager::stopControllers() {
65 int rc = 0;
66 ControllerCollection::iterator i;
67
68 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
69 int irc = (*i)->stop();
70 LOGD("Controller '%s' stop rc = %d", (*i)->getName(), irc);
71 if (irc && !rc)
72 rc = irc;
73 }
74 return rc;
75}
76
77Controller *NetworkManager::findController(const char *name) {
78 ControllerCollection::iterator i;
79 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
80 if (!strcmp((*i)->getName(), name))
81 return *i;
82 }
83 LOGW("Controller '%s' not found", name);
84 return NULL;
85}
86
San Mehat48765672009-05-20 15:28:43 -070087int NetworkManager::setProperty(const char *name, char *value) {
88 char *tmp = strdup(name);
89 char *next = tmp;
90 char *prefix;
91 char *rest;
92 ControllerCollection::iterator it;
93
94 if (!(prefix = strsep(&next, ".")))
95 goto out_inval;
96
97 rest = next;
98
99 if (!strncasecmp(prefix, "netman", 6)) {
100 errno = ENOSYS;
101 return -1;
102 }
103
104 for (it = mControllers->begin(); it != mControllers->end(); ++it) {
105 if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
106 return (*it)->setProperty(rest, value);
107 }
108 }
109
110 errno = ENOENT;
111 return -1;
112
113out_inval:
114 errno = EINVAL;
115 return -1;
116}
117
118const char *NetworkManager::getProperty(const char *name, char *buffer,
119 size_t maxsize) {
120 char *tmp = strdup(name);
121 char *next = tmp;
122 char *prefix;
123 char *rest;
124 ControllerCollection::iterator it;
125
126 if (!(prefix = strsep(&next, ".")))
127 goto out_inval;
128
129 rest = next;
130
131 if (!strncasecmp(prefix, "netman", 6)) {
132 errno = ENOSYS;
133 return NULL;
134 }
135
136 for (it = mControllers->begin(); it != mControllers->end(); ++it) {
137 if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
138 return (*it)->getProperty(rest, buffer, maxsize);
139 }
140 }
141
142 errno = ENOENT;
143 return NULL;
144
145out_inval:
146 errno = EINVAL;
147 return NULL;
148}
149
150const PropertyCollection &NetworkManager::getProperties() {
151 return *mProperties;
152}
153
San Mehat192331d2009-05-22 13:58:06 -0700154int NetworkManager::onInterfaceStart(Controller *c, const InterfaceConfig *cfg) {
155 LOGD("Interface %s started by controller %s", cfg->getName(), c->getName());
156
157 // Look up the interface
158
159 if (0) { // already started?
160 errno = EADDRINUSE;
161 return -1;
162 }
163
164 if (cfg->getUseDhcp()) {
165 } else {
166 }
San Mehatdc266072009-05-06 11:16:52 -0700167 return 0;
168}
169
San Mehat192331d2009-05-22 13:58:06 -0700170int NetworkManager::onInterfaceStop(Controller *c, const char *name) {
171 LOGD("Interface %s stopped by controller %s", name, c->getName());
San Mehatdc266072009-05-06 11:16:52 -0700172 return 0;
173}