San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 1 | /* |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008 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 | */ |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 16 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 18 | #include <sys/socket.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <arpa/inet.h> |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | |
| 23 | #define LOG_TAG "CommandListener" |
| 24 | #include <cutils/log.h> |
| 25 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 26 | #include <sysutils/SocketClient.h> |
| 27 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 28 | #include "CommandListener.h" |
| 29 | #include "Controller.h" |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 30 | #include "Property.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 31 | #include "NetworkManager.h" |
| 32 | #include "WifiController.h" |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 33 | #include "VpnController.h" |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 34 | #include "ResponseCode.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 35 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 36 | CommandListener::CommandListener() : |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 37 | FrameworkListener("nexus") { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 38 | registerCmd(new WifiScanResultsCmd()); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 39 | registerCmd(new WifiListNetworksCmd()); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 40 | registerCmd(new WifiCreateNetworkCmd()); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 41 | registerCmd(new WifiRemoveNetworkCmd()); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 42 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 43 | registerCmd(new GetCmd()); |
| 44 | registerCmd(new SetCmd()); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 45 | registerCmd(new ListCmd()); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 46 | } |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 47 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 48 | /* ------------- |
| 49 | * Wifi Commands |
| 50 | * ------------ */ |
| 51 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 52 | CommandListener::WifiCreateNetworkCmd::WifiCreateNetworkCmd() : |
| 53 | NexusCommand("wifi_create_network") { |
| 54 | } |
| 55 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 56 | int CommandListener::WifiCreateNetworkCmd::runCommand(SocketClient *cli, |
| 57 | int argc, char **argv) { |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 58 | NetworkManager *nm = NetworkManager::Instance(); |
| 59 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 60 | WifiNetwork *wn; |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 61 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 62 | if (!(wn = wc->createNetwork())) |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 63 | cli->sendMsg(ResponseCode::OperationFailed, "Failed to create network", true); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 64 | else { |
| 65 | char tmp[128]; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 66 | sprintf(tmp, "Created network id %d.", wn->getNetworkId()); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 67 | cli->sendMsg(ResponseCode::CommandOkay, tmp, false); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | CommandListener::WifiRemoveNetworkCmd::WifiRemoveNetworkCmd() : |
| 73 | NexusCommand("wifi_remove_network") { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 74 | } |
| 75 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 76 | int CommandListener::WifiRemoveNetworkCmd::runCommand(SocketClient *cli, |
| 77 | int argc, char **argv) { |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 78 | NetworkManager *nm = NetworkManager::Instance(); |
| 79 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
| 80 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 81 | if (wc->removeNetwork(atoi(argv[1]))) |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 82 | cli->sendMsg(ResponseCode::OperationFailed, "Failed to remove network", true); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 83 | else { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 84 | cli->sendMsg(ResponseCode::CommandOkay, "Network removed.", false); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 89 | CommandListener::WifiScanResultsCmd::WifiScanResultsCmd() : |
| 90 | NexusCommand("wifi_scan_results") { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 91 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 92 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 93 | int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, |
| 94 | int argc, char **argv) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 95 | NetworkManager *nm = NetworkManager::Instance(); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 96 | WifiController *wc = (WifiController *) nm->findController("WIFI"); |
| 97 | |
| 98 | ScanResultCollection *src = wc->createScanResults(); |
| 99 | ScanResultCollection::iterator it; |
| 100 | char buffer[256]; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 101 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 102 | for(it = src->begin(); it != src->end(); ++it) { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 103 | sprintf(buffer, "%s %u %d %s %s", |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 104 | (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(), |
| 105 | (*it)->getFlags(), (*it)->getSsid()); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 106 | cli->sendMsg(ResponseCode::WifiScanResult, buffer, false); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 107 | delete (*it); |
| 108 | it = src->erase(it); |
| 109 | } |
| 110 | |
| 111 | delete src; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 112 | cli->sendMsg(ResponseCode::CommandOkay, "Scan results complete.", false); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 116 | CommandListener::WifiListNetworksCmd::WifiListNetworksCmd() : |
| 117 | NexusCommand("wifi_list_networks") { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 118 | } |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 119 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 120 | int CommandListener::WifiListNetworksCmd::runCommand(SocketClient *cli, |
| 121 | int argc, char **argv) { |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 122 | 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 Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 128 | |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 129 | for(it = src->begin(); it != src->end(); ++it) { |
| 130 | sprintf(buffer, "%d:%s", (*it)->getNetworkId(), (*it)->getSsid()); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 131 | cli->sendMsg(ResponseCode::WifiNetworkList, buffer, false); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 132 | delete (*it); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | delete src; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 136 | cli->sendMsg(ResponseCode::CommandOkay, "Network listing complete.", false); |
San Mehat | 82a2116 | 2009-05-12 17:26:28 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 140 | /* ------------ |
| 141 | * Vpn Commands |
| 142 | * ------------ */ |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 143 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 144 | /* ---------------- |
| 145 | * Generic Commands |
| 146 | * ---------------- */ |
| 147 | CommandListener::GetCmd::GetCmd() : |
| 148 | NexusCommand("get") { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 149 | } |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 150 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 151 | int CommandListener::GetCmd::runCommand(SocketClient *cli, int argc, char **argv) { |
| 152 | char val[Property::ValueMaxSize]; |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 153 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 154 | if (!NetworkManager::Instance()->getPropMngr()->get(argv[1], |
| 155 | val, |
| 156 | sizeof(val))) { |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 157 | goto out_inval; |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 158 | } |
| 159 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 160 | char *tmp; |
| 161 | asprintf(&tmp, "%s %s", argv[1], val); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 162 | cli->sendMsg(ResponseCode::PropertyRead, tmp, false); |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 163 | free(tmp); |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 164 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 165 | cli->sendMsg(ResponseCode::CommandOkay, "Property read.", false); |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | out_inval: |
| 168 | errno = EINVAL; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 169 | cli->sendMsg(ResponseCode::CommandParameterError, "Failed to read property.", true); |
San Mehat | 5d6d417 | 2009-05-14 15:00:06 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 173 | CommandListener::SetCmd::SetCmd() : |
| 174 | NexusCommand("set") { |
| 175 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 176 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 177 | int CommandListener::SetCmd::runCommand(SocketClient *cli, int argc, |
| 178 | char **argv) { |
| 179 | if (NetworkManager::Instance()->getPropMngr()->set(argv[1], argv[2])) |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 180 | goto out_inval; |
| 181 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 182 | cli->sendMsg(ResponseCode::CommandOkay, "Property set.", false); |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | |
| 185 | out_inval: |
| 186 | errno = EINVAL; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 187 | cli->sendMsg(ResponseCode::CommandParameterError, "Failed to set property.", true); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 188 | return 0; |
| 189 | } |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 190 | |
| 191 | CommandListener::ListCmd::ListCmd() : |
| 192 | NexusCommand("list") { |
| 193 | } |
| 194 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 195 | int CommandListener::ListCmd::runCommand(SocketClient *cli, int argc, char **argv) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 196 | android::List<char *> *pc; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 197 | char *prefix = NULL; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 198 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 199 | if (argc > 1) |
| 200 | prefix = argv[1]; |
| 201 | |
| 202 | if (!(pc = NetworkManager::Instance()->getPropMngr()->createPropertyList(prefix))) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 203 | errno = ENODATA; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 204 | cli->sendMsg(ResponseCode::CommandParameterError, "Failed to list properties.", true); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 205 | 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))) { |
| 216 | LOGW("Failed to get %s (%s)", (*it), strerror(errno)); |
| 217 | } |
| 218 | |
| 219 | char *buf; |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 220 | if (asprintf(&buf, "%s %s", (*it), p_v) < 0) { |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 221 | LOGE("Failed to allocate memory"); |
| 222 | free((*it)); |
| 223 | continue; |
| 224 | } |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 225 | cli->sendMsg(ResponseCode::PropertyList, buf, false); |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 226 | free(buf); |
| 227 | |
| 228 | free((*it)); |
| 229 | } |
| 230 | |
| 231 | delete pc; |
| 232 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 233 | cli->sendMsg(ResponseCode::CommandOkay, "Properties list complete.", false); |
San Mehat | 192331d | 2009-05-22 13:58:06 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |