blob: 022d7f88a9f4a2d3966f1511d5f109282630a167 [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 Mehatdc266072009-05-06 11:16:52 -070024
San Mehat1441e762009-05-07 11:37:10 -070025NetworkManager *NetworkManager::sInstance = NULL;
26
27NetworkManager *NetworkManager::Instance() {
28 if (!sInstance)
29 sInstance = new NetworkManager();
30 return sInstance;
31}
San Mehatdc266072009-05-06 11:16:52 -070032
33NetworkManager::NetworkManager() {
San Mehat1441e762009-05-07 11:37:10 -070034 mBroadcaster = NULL;
San Mehatdc266072009-05-06 11:16:52 -070035 mControllers = new ControllerCollection();
36}
37
38int NetworkManager::run() {
San Mehatdc266072009-05-06 11:16:52 -070039 if (startControllers()) {
40 LOGW("Unable to start all controllers (%s)", strerror(errno));
41 }
San Mehatdc266072009-05-06 11:16:52 -070042 return 0;
43}
44
San Mehat1441e762009-05-07 11:37:10 -070045int NetworkManager::attachController(Controller *c) {
San Mehatdc266072009-05-06 11:16:52 -070046 mControllers->push_back(c);
San Mehat1441e762009-05-07 11:37:10 -070047 return 0;
San Mehatdc266072009-05-06 11:16:52 -070048}
49
50int NetworkManager::startControllers() {
51 int rc = 0;
52 ControllerCollection::iterator i;
53
54 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
55 int irc = (*i)->start();
56 LOGD("Controller '%s' start rc = %d", (*i)->getName(), irc);
57 if (irc && !rc)
58 rc = irc;
59 }
60 return rc;
61}
62
63int NetworkManager::stopControllers() {
64 int rc = 0;
65 ControllerCollection::iterator i;
66
67 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
68 int irc = (*i)->stop();
69 LOGD("Controller '%s' stop rc = %d", (*i)->getName(), irc);
70 if (irc && !rc)
71 rc = irc;
72 }
73 return rc;
74}
75
76Controller *NetworkManager::findController(const char *name) {
77 ControllerCollection::iterator i;
78 for (i = mControllers->begin(); i != mControllers->end(); ++i) {
79 if (!strcmp((*i)->getName(), name))
80 return *i;
81 }
82 LOGW("Controller '%s' not found", name);
83 return NULL;
84}
85
San Mehat48765672009-05-20 15:28:43 -070086int NetworkManager::setProperty(const char *name, char *value) {
87 char *tmp = strdup(name);
88 char *next = tmp;
89 char *prefix;
90 char *rest;
91 ControllerCollection::iterator it;
92
93 if (!(prefix = strsep(&next, ".")))
94 goto out_inval;
95
96 rest = next;
97
98 if (!strncasecmp(prefix, "netman", 6)) {
99 errno = ENOSYS;
100 return -1;
101 }
102
103 for (it = mControllers->begin(); it != mControllers->end(); ++it) {
104 if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
105 return (*it)->setProperty(rest, value);
106 }
107 }
108
109 errno = ENOENT;
110 return -1;
111
112out_inval:
113 errno = EINVAL;
114 return -1;
115}
116
117const char *NetworkManager::getProperty(const char *name, char *buffer,
118 size_t maxsize) {
119 char *tmp = strdup(name);
120 char *next = tmp;
121 char *prefix;
122 char *rest;
123 ControllerCollection::iterator it;
124
125 if (!(prefix = strsep(&next, ".")))
126 goto out_inval;
127
128 rest = next;
129
130 if (!strncasecmp(prefix, "netman", 6)) {
131 errno = ENOSYS;
132 return NULL;
133 }
134
135 for (it = mControllers->begin(); it != mControllers->end(); ++it) {
136 if (!strcasecmp(prefix, (*it)->getPropertyPrefix())) {
137 return (*it)->getProperty(rest, buffer, maxsize);
138 }
139 }
140
141 errno = ENOENT;
142 return NULL;
143
144out_inval:
145 errno = EINVAL;
146 return NULL;
147}
148
149const PropertyCollection &NetworkManager::getProperties() {
150 return *mProperties;
151}
152
San Mehatdc266072009-05-06 11:16:52 -0700153int NetworkManager::onInterfaceCreated(Controller *c, char *name) {
154 LOGD("Interface %s created by controller %s", name, c->getName());
155 return 0;
156}
157
158int NetworkManager::onInterfaceDestroyed(Controller *c, char *name) {
159 LOGD("Interface %s destroyed by controller %s", name, c->getName());
160 return 0;
161}