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