San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 1 | /* |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 2 | * Copyright (C) ErrorCode::CommandOkay8 The Android Open Source Project |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 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 <stdlib.h> |
| 17 | #include <errno.h> |
| 18 | |
| 19 | #define LOG_TAG "CommandListener" |
| 20 | #include <cutils/log.h> |
| 21 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 22 | #include <sysutils/SocketClient.h> |
| 23 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 24 | #include "CommandListener.h" |
| 25 | #include "Controller.h" |
| 26 | #include "NetworkManager.h" |
| 27 | #include "WifiController.h" |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 28 | #include "ErrorCode.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 29 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 30 | CommandListener::CommandListener() : |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 31 | FrameworkListener("nexus") { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 32 | registerCmd(new WifiEnableCmd()); |
| 33 | registerCmd(new WifiDisableCmd()); |
| 34 | registerCmd(new WifiScanCmd()); |
| 35 | registerCmd(new WifiScanResultsCmd()); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame^] | 36 | registerCmd(new WifiListNetworksCmd()); |
| 37 | registerCmd(new WifiAddNetworkCmd()); |
| 38 | registerCmd(new WifiRemoveNetworkCmd()); |
| 39 | registerCmd(new WifiSetVarCmd()); |
| 40 | registerCmd(new WifiGetVarCmd()); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 41 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 42 | registerCmd(new VpnEnableCmd()); |
| 43 | registerCmd(new VpnDisableCmd()); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* ------------- |
| 47 | * Wifi Commands |
| 48 | * ------------ */ |
| 49 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 50 | CommandListener::WifiEnableCmd::WifiEnableCmd() : |
| 51 | NexusCommand("wifi_enable") { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 52 | } |
| 53 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 54 | int CommandListener::WifiEnableCmd::runCommand(SocketClient *cli, char *data) { |
| 55 | Controller *c = NetworkManager::Instance()->findController("WIFI"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 56 | |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 57 | if (c->enable()) |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 58 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to enable wifi", true); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 59 | else |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 60 | cli->sendMsg(ErrorCode::CommandOkay, "Wifi Enabled", false); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 64 | CommandListener::WifiDisableCmd::WifiDisableCmd() : |
| 65 | NexusCommand("wifi_disable") { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 66 | } |
| 67 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 68 | int CommandListener::WifiDisableCmd::runCommand(SocketClient *cli, char *data) { |
| 69 | Controller *c = NetworkManager::Instance()->findController("WIFI"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 70 | |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 71 | if (c->disable()) |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 72 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to disable wifi", true); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 73 | else |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 74 | cli->sendMsg(ErrorCode::CommandOkay, "Wifi Disabled", false); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame^] | 78 | CommandListener::WifiAddNetworkCmd::WifiAddNetworkCmd() : |
| 79 | NexusCommand("wifi_add_network") { |
| 80 | } |
| 81 | |
| 82 | int CommandListener::WifiAddNetworkCmd::runCommand(SocketClient *cli, char *data) { |
| 83 | NetworkManager *nm = NetworkManager::Instance(); |
| 84 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
| 85 | int networkId; |
| 86 | |
| 87 | if ((networkId = wc->addNetwork()) < 0) |
| 88 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to add network", true); |
| 89 | else { |
| 90 | char tmp[128]; |
| 91 | sprintf(tmp, "Added network id %d.", networkId); |
| 92 | cli->sendMsg(ErrorCode::CommandOkay, tmp, false); |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() : |
| 98 | NexusCommand("wifi_remove_network") { |
| 99 | } |
| 100 | |
| 101 | int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, char *data) { |
| 102 | NetworkManager *nm = NetworkManager::Instance(); |
| 103 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
| 104 | |
| 105 | if (wc->removeNetwork(atoi(data))) |
| 106 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to remove network", true); |
| 107 | else { |
| 108 | cli->sendMsg(ErrorCode::CommandOkay, "Network removed.", false); |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 113 | CommandListener::WifiScanCmd::WifiScanCmd() : |
| 114 | NexusCommand("wifi_scan") { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 115 | } |
| 116 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 117 | int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 118 | WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI"); |
| 119 | |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 120 | if (wc->setScanMode(atoi(data))) |
| 121 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to set scan mode", true); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 122 | else |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 123 | cli->sendMsg(ErrorCode::CommandOkay, "Scan mode set", false); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 124 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() : |
| 129 | NexusCommand("wifi_scan_results") { |
| 130 | } |
| 131 | |
| 132 | int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *data) { |
| 133 | NetworkManager *nm = NetworkManager::Instance(); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 134 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
| 135 | |
| 136 | ScanResultCollection *src = wc->createScanResults(); |
| 137 | ScanResultCollection::iterator it; |
| 138 | char buffer[256]; |
| 139 | |
| 140 | for(it = src->begin(); it != src->end(); ++it) { |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 141 | sprintf(buffer, "%s:%u:%d:%s:%s", |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 142 | (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(), |
| 143 | (*it)->getFlags(), (*it)->getSsid()); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame^] | 144 | cli->sendMsg(ErrorCode::WifiScanResult, buffer, false); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 145 | delete (*it); |
| 146 | it = src->erase(it); |
| 147 | } |
| 148 | |
| 149 | delete src; |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 150 | cli->sendMsg(ErrorCode::CommandOkay, "Scan results complete", false); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame^] | 154 | CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() : |
| 155 | NexusCommand("wifi_list_networks") { |
| 156 | } |
| 157 | |
| 158 | int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, char *data) { |
| 159 | NetworkManager *nm = NetworkManager::Instance(); |
| 160 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
| 161 | |
| 162 | WifiNetworkCollection *src = wc->createNetworkList(); |
| 163 | WifiNetworkCollection::iterator it; |
| 164 | char buffer[256]; |
| 165 | |
| 166 | for(it = src->begin(); it != src->end(); ++it) { |
| 167 | sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid()); |
| 168 | cli->sendMsg(ErrorCode::WifiNetworkList, buffer, false); |
| 169 | delete (*it); |
| 170 | it = src->erase(it); |
| 171 | } |
| 172 | |
| 173 | delete src; |
| 174 | cli->sendMsg(ErrorCode::CommandOkay, "Network listing complete.", false); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | CommandListener::WifiSetVarCmd::WifiSetVarCmd() : |
| 179 | NexusCommand("wifi_setvar") { |
| 180 | } |
| 181 | |
| 182 | int CommandListener::WifiSetVarCmd::runCommand(SocketClient *cli, char *data) { |
| 183 | WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI"); |
| 184 | |
| 185 | char *bword; |
| 186 | char *last; |
| 187 | char varname[32]; |
| 188 | char val[250]; |
| 189 | int networkId; |
| 190 | |
| 191 | if (!(bword = strtok_r(data, ":", &last))) |
| 192 | goto out_inval; |
| 193 | |
| 194 | networkId = atoi(bword); |
| 195 | |
| 196 | if (!(bword = strtok_r(NULL, ":", &last))) |
| 197 | goto out_inval; |
| 198 | |
| 199 | strncpy(varname, bword, sizeof(varname)); |
| 200 | |
| 201 | if (!(bword = strtok_r(NULL, ":", &last))) |
| 202 | goto out_inval; |
| 203 | |
| 204 | strncpy(val, bword, sizeof(val)); |
| 205 | |
| 206 | LOGD("Network id %d, varname '%s', value '%s'", networkId, varname, val); |
| 207 | |
| 208 | return 0; |
| 209 | |
| 210 | out_inval: |
| 211 | errno = EINVAL; |
| 212 | cli->sendMsg(ErrorCode::CommandParameterError, "Failed to set variable.", true); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | CommandListener::WifiGetVarCmd::WifiGetVarCmd() : |
| 217 | NexusCommand("wifi_getvar") { |
| 218 | } |
| 219 | |
| 220 | int CommandListener::WifiGetVarCmd::runCommand(SocketClient *cli, char *data) { |
| 221 | WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI"); |
| 222 | |
| 223 | char *bword; |
| 224 | char *last; |
| 225 | char varname[32]; |
| 226 | int networkId; |
| 227 | |
| 228 | if (!(bword = strtok_r(data, ":", &last))) |
| 229 | goto out_inval; |
| 230 | |
| 231 | networkId = atoi(bword); |
| 232 | |
| 233 | if (!(bword = strtok_r(NULL, ":", &last))) |
| 234 | goto out_inval; |
| 235 | |
| 236 | strncpy(varname, bword, sizeof(varname)); |
| 237 | |
| 238 | LOGD("networkId = %d, varname '%s'", networkId, varname); |
| 239 | |
| 240 | return 0; |
| 241 | out_inval: |
| 242 | errno = EINVAL; |
| 243 | cli->sendMsg(ErrorCode::CommandParameterError, "Failed to get variable.", true); |
| 244 | return 0; |
| 245 | } |
| 246 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 247 | /* ------------ |
| 248 | * Vpn Commands |
| 249 | * ------------ */ |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 250 | CommandListener::VpnEnableCmd::VpnEnableCmd() : |
| 251 | NexusCommand("vpn_enable") { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 252 | } |
| 253 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 254 | int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) { |
| 255 | Controller *c = NetworkManager::Instance()->findController("VPN"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 256 | |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 257 | if (c->enable()) |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 258 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to enable VPN", true); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 259 | else |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 260 | cli->sendMsg(ErrorCode::CommandOkay, "VPN enabled", false); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 264 | CommandListener::VpnDisableCmd::VpnDisableCmd() : |
| 265 | NexusCommand("vpn_disable") { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 266 | } |
| 267 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 268 | int CommandListener::VpnDisableCmd::runCommand(SocketClient *cli, char *data) { |
| 269 | Controller *c = NetworkManager::Instance()->findController("VPN"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 270 | |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 271 | if (c->disable()) |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 272 | cli->sendMsg(ErrorCode::OperationFailed, "Failed to disable VPN", true); |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 273 | else |
San Mehat | 8d3fc3f | 2009-05-12 14:36:32 -0700 | [diff] [blame] | 274 | cli->sendMsg(ErrorCode::CommandOkay, "VPN disabled", false); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | } |