blob: 7c934a75ab995dbb0ad0b897139782745dcf4d03 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
San Mehat3c5a6f02009-05-22 15:36:13 -07002 * Copyright (C) 2008 The Android Open Source Project
San Mehatdc266072009-05-06 11:16:52 -07003 *
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 <stdlib.h>
San Mehat5d6d4172009-05-14 15:00:06 -070018#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
San Mehatdc266072009-05-06 11:16:52 -070021#include <errno.h>
22
23#define LOG_TAG "CommandListener"
24#include <cutils/log.h>
25
San Mehat1441e762009-05-07 11:37:10 -070026#include <sysutils/SocketClient.h>
27
San Mehatdc266072009-05-06 11:16:52 -070028#include "CommandListener.h"
29#include "Controller.h"
San Mehat3c5a6f02009-05-22 15:36:13 -070030#include "Property.h"
San Mehatdc266072009-05-06 11:16:52 -070031#include "NetworkManager.h"
32#include "WifiController.h"
San Mehat5d6d4172009-05-14 15:00:06 -070033#include "VpnController.h"
San Mehatc4a895b2009-06-23 21:10:57 -070034#include "ResponseCode.h"
San Mehatdc266072009-05-06 11:16:52 -070035
San Mehat1441e762009-05-07 11:37:10 -070036CommandListener::CommandListener() :
San Mehatdc266072009-05-06 11:16:52 -070037 FrameworkListener("nexus") {
San Mehat1441e762009-05-07 11:37:10 -070038 registerCmd(new WifiScanResultsCmd());
San Mehat82a21162009-05-12 17:26:28 -070039 registerCmd(new WifiListNetworksCmd());
San Mehat3c5a6f02009-05-22 15:36:13 -070040 registerCmd(new WifiCreateNetworkCmd());
San Mehat82a21162009-05-12 17:26:28 -070041 registerCmd(new WifiRemoveNetworkCmd());
San Mehatdc266072009-05-06 11:16:52 -070042
San Mehat48765672009-05-20 15:28:43 -070043 registerCmd(new GetCmd());
44 registerCmd(new SetCmd());
San Mehat3c5a6f02009-05-22 15:36:13 -070045 registerCmd(new ListCmd());
San Mehatdc266072009-05-06 11:16:52 -070046}
San Mehat3c5a6f02009-05-22 15:36:13 -070047
San Mehatdc266072009-05-06 11:16:52 -070048/* -------------
49 * Wifi Commands
50 * ------------ */
51
San Mehat3c5a6f02009-05-22 15:36:13 -070052CommandListener::WifiCreateNetworkCmd::WifiCreateNetworkCmd() :
53 NexusCommand("wifi_create_network") {
54}
55
San Mehat3aff2d12009-06-15 14:10:44 -070056int CommandListener::WifiCreateNetworkCmd::runCommand(SocketClient *cli,
57 int argc, char **argv) {
San Mehat82a21162009-05-12 17:26:28 -070058 NetworkManager *nm = NetworkManager::Instance();
59 WifiController *wc = (WifiController *) nm->findController("WIFI");
San Mehat3c5a6f02009-05-22 15:36:13 -070060 WifiNetwork *wn;
San Mehat82a21162009-05-12 17:26:28 -070061
San Mehat3c5a6f02009-05-22 15:36:13 -070062 if (!(wn = wc->createNetwork()))
San Mehatc4a895b2009-06-23 21:10:57 -070063 cli->sendMsg(ResponseCode::OperationFailed, "Failed to create network", true);
San Mehat82a21162009-05-12 17:26:28 -070064 else {
65 char tmp[128];
San Mehat3c5a6f02009-05-22 15:36:13 -070066 sprintf(tmp, "Created network id %d.", wn->getNetworkId());
San Mehatc4a895b2009-06-23 21:10:57 -070067 cli->sendMsg(ResponseCode::CommandOkay, tmp, false);
San Mehat82a21162009-05-12 17:26:28 -070068 }
69 return 0;
70}
71
72CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() :
73 NexusCommand("wifi_remove_network") {
San Mehat3c5a6f02009-05-22 15:36:13 -070074}
75
San Mehat3aff2d12009-06-15 14:10:44 -070076int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli,
77 int argc, char **argv) {
San Mehat82a21162009-05-12 17:26:28 -070078 NetworkManager *nm = NetworkManager::Instance();
79 WifiController *wc = (WifiController *) nm->findController("WIFI");
80
San Mehat3aff2d12009-06-15 14:10:44 -070081 if (wc->removeNetwork(atoi(argv[1])))
San Mehatc4a895b2009-06-23 21:10:57 -070082 cli->sendMsg(ResponseCode::OperationFailed, "Failed to remove network", true);
San Mehat82a21162009-05-12 17:26:28 -070083 else {
San Mehatc4a895b2009-06-23 21:10:57 -070084 cli->sendMsg(ResponseCode::CommandOkay, "Network removed.", false);
San Mehat82a21162009-05-12 17:26:28 -070085 }
86 return 0;
87}
88
San Mehat1441e762009-05-07 11:37:10 -070089CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() :
90 NexusCommand("wifi_scan_results") {
San Mehat3c5a6f02009-05-22 15:36:13 -070091}
San Mehat1441e762009-05-07 11:37:10 -070092
San Mehat3aff2d12009-06-15 14:10:44 -070093int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli,
94 int argc, char **argv) {
San Mehat1441e762009-05-07 11:37:10 -070095 NetworkManager *nm = NetworkManager::Instance();
San Mehat1441e762009-05-07 11:37:10 -070096 WifiController *wc = (WifiController *) nm->findController("WIFI");
97
98 ScanResultCollection *src = wc->createScanResults();
99 ScanResultCollection::iterator it;
100 char buffer[256];
San Mehat3c5a6f02009-05-22 15:36:13 -0700101
San Mehat1441e762009-05-07 11:37:10 -0700102 for(it = src->begin(); it != src->end(); ++it) {
San Mehatc4a895b2009-06-23 21:10:57 -0700103 sprintf(buffer, "%s %u %d %s %s",
San Mehat1441e762009-05-07 11:37:10 -0700104 (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(),
105 (*it)->getFlags(), (*it)->getSsid());
San Mehatc4a895b2009-06-23 21:10:57 -0700106 cli->sendMsg(ResponseCode::WifiScanResult, buffer, false);
San Mehat1441e762009-05-07 11:37:10 -0700107 delete (*it);
108 it = src->erase(it);
109 }
110
111 delete src;
San Mehatc4a895b2009-06-23 21:10:57 -0700112 cli->sendMsg(ResponseCode::CommandOkay, "Scan results complete.", false);
San Mehatdc266072009-05-06 11:16:52 -0700113 return 0;
114}
115
San Mehat82a21162009-05-12 17:26:28 -0700116CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() :
117 NexusCommand("wifi_list_networks") {
San Mehat3c5a6f02009-05-22 15:36:13 -0700118}
San Mehat82a21162009-05-12 17:26:28 -0700119
San Mehat3aff2d12009-06-15 14:10:44 -0700120int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli,
121 int argc, char **argv) {
San Mehat82a21162009-05-12 17:26:28 -0700122 NetworkManager *nm = NetworkManager::Instance();
123 WifiController *wc = (WifiController *) nm->findController("WIFI");
124
125 WifiNetworkCollection *src = wc->createNetworkList();
126 WifiNetworkCollection::iterator it;
127 char buffer[256];
San Mehat3c5a6f02009-05-22 15:36:13 -0700128
San Mehat82a21162009-05-12 17:26:28 -0700129 for(it = src->begin(); it != src->end(); ++it) {
130 sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid());
San Mehatc4a895b2009-06-23 21:10:57 -0700131 cli->sendMsg(ResponseCode::WifiNetworkList, buffer, false);
San Mehat82a21162009-05-12 17:26:28 -0700132 delete (*it);
San Mehat82a21162009-05-12 17:26:28 -0700133 }
134
135 delete src;
San Mehatc4a895b2009-06-23 21:10:57 -0700136 cli->sendMsg(ResponseCode::CommandOkay, "Network listing complete.", false);
San Mehat82a21162009-05-12 17:26:28 -0700137 return 0;
138}
139
San Mehatdc266072009-05-06 11:16:52 -0700140/* ------------
141 * Vpn Commands
142 * ------------ */
San Mehatdc266072009-05-06 11:16:52 -0700143
San Mehat48765672009-05-20 15:28:43 -0700144/* ----------------
145 * Generic Commands
146 * ---------------- */
147CommandListener::GetCmd::GetCmd() :
148 NexusCommand("get") {
San Mehat3c5a6f02009-05-22 15:36:13 -0700149}
San Mehat5d6d4172009-05-14 15:00:06 -0700150
San Mehat3aff2d12009-06-15 14:10:44 -0700151int CommandListener::GetCmd::runCommand(SocketClient *cli, int argc, char **argv) {
152 char val[Property::ValueMaxSize];
San Mehat5d6d4172009-05-14 15:00:06 -0700153
San Mehat3aff2d12009-06-15 14:10:44 -0700154 if (!NetworkManager::Instance()->getPropMngr()->get(argv[1],
155 val,
156 sizeof(val))) {
San Mehat48765672009-05-20 15:28:43 -0700157 goto out_inval;
San Mehat5d6d4172009-05-14 15:00:06 -0700158 }
159
San Mehat3aff2d12009-06-15 14:10:44 -0700160 char *tmp;
161 asprintf(&tmp, "%s %s", argv[1], val);
San Mehatc4a895b2009-06-23 21:10:57 -0700162 cli->sendMsg(ResponseCode::PropertyRead, tmp, false);
San Mehat3aff2d12009-06-15 14:10:44 -0700163 free(tmp);
San Mehat48765672009-05-20 15:28:43 -0700164
San Mehatc4a895b2009-06-23 21:10:57 -0700165 cli->sendMsg(ResponseCode::CommandOkay, "Property read.", false);
San Mehat5d6d4172009-05-14 15:00:06 -0700166 return 0;
167out_inval:
168 errno = EINVAL;
San Mehatc4a895b2009-06-23 21:10:57 -0700169 cli->sendMsg(ResponseCode::CommandParameterError, "Failed to read property.", true);
San Mehat5d6d4172009-05-14 15:00:06 -0700170 return 0;
171}
172
San Mehat48765672009-05-20 15:28:43 -0700173CommandListener::SetCmd::SetCmd() :
174 NexusCommand("set") {
175}
San Mehatdc266072009-05-06 11:16:52 -0700176
San Mehat3aff2d12009-06-15 14:10:44 -0700177int CommandListener::SetCmd::runCommand(SocketClient *cli, int argc,
178 char **argv) {
179 if (NetworkManager::Instance()->getPropMngr()->set(argv[1], argv[2]))
San Mehat48765672009-05-20 15:28:43 -0700180 goto out_inval;
181
San Mehatc4a895b2009-06-23 21:10:57 -0700182 cli->sendMsg(ResponseCode::CommandOkay, "Property set.", false);
San Mehat48765672009-05-20 15:28:43 -0700183 return 0;
184
185out_inval:
186 errno = EINVAL;
San Mehatc4a895b2009-06-23 21:10:57 -0700187 cli->sendMsg(ResponseCode::CommandParameterError, "Failed to set property.", true);
San Mehatdc266072009-05-06 11:16:52 -0700188 return 0;
189}
San Mehat192331d2009-05-22 13:58:06 -0700190
191CommandListener::ListCmd::ListCmd() :
192 NexusCommand("list") {
193}
194
San Mehat3aff2d12009-06-15 14:10:44 -0700195int CommandListener::ListCmd::runCommand(SocketClient *cli, int argc, char **argv) {
San Mehat3c5a6f02009-05-22 15:36:13 -0700196 android::List<char *> *pc;
San Mehatc4a895b2009-06-23 21:10:57 -0700197 char *prefix = NULL;
San Mehat3c5a6f02009-05-22 15:36:13 -0700198
San Mehatc4a895b2009-06-23 21:10:57 -0700199 if (argc > 1)
200 prefix = argv[1];
201
202 if (!(pc = NetworkManager::Instance()->getPropMngr()->createPropertyList(prefix))) {
San Mehat3c5a6f02009-05-22 15:36:13 -0700203 errno = ENODATA;
San Mehatc4a895b2009-06-23 21:10:57 -0700204 cli->sendMsg(ResponseCode::CommandParameterError, "Failed to list properties.", true);
San Mehat3c5a6f02009-05-22 15:36:13 -0700205 return 0;
206 }
207
208 android::List<char *>::iterator it;
209
210 for (it = pc->begin(); it != pc->end(); ++it) {
211 char p_v[Property::ValueMaxSize];
212
213 if (!NetworkManager::Instance()->getPropMngr()->get((*it),
214 p_v,
215 sizeof(p_v))) {
Steve Blockae8b56c2012-01-05 22:25:38 +0000216 ALOGW("Failed to get %s (%s)", (*it), strerror(errno));
San Mehat3c5a6f02009-05-22 15:36:13 -0700217 }
218
219 char *buf;
San Mehat3aff2d12009-06-15 14:10:44 -0700220 if (asprintf(&buf, "%s %s", (*it), p_v) < 0) {
Steve Block01dda202012-01-06 14:13:42 +0000221 ALOGE("Failed to allocate memory");
San Mehat3c5a6f02009-05-22 15:36:13 -0700222 free((*it));
223 continue;
224 }
San Mehatc4a895b2009-06-23 21:10:57 -0700225 cli->sendMsg(ResponseCode::PropertyList, buf, false);
San Mehat3c5a6f02009-05-22 15:36:13 -0700226 free(buf);
227
228 free((*it));
229 }
230
231 delete pc;
232
San Mehatc4a895b2009-06-23 21:10:57 -0700233 cli->sendMsg(ResponseCode::CommandOkay, "Properties list complete.", false);
San Mehat192331d2009-05-22 13:58:06 -0700234 return 0;
235}