system/corennnnn
リビジョン | 69772dc644e1ccc12b6394267f010100470f3c95 (tree) |
---|---|
日時 | 2009-05-13 06:02:32 |
作者 | San Mehat <san@goog...> |
コミッター | San Mehat |
nexus: Move to a line based protocol (similar to FTP) + fix bugs
Signed-off-by: San Mehat <san@google.com>
@@ -47,11 +47,11 @@ CommandListener::WifiEnableCmd::WifiEnableCmd() : | ||
47 | 47 | |
48 | 48 | int CommandListener::WifiEnableCmd::runCommand(SocketClient *cli, char *data) { |
49 | 49 | Controller *c = NetworkManager::Instance()->findController("WIFI"); |
50 | - char buffer[32]; | |
51 | 50 | |
52 | - sprintf(buffer, "WIFI_ENABLE:%d", (c->enable() ? errno : 0)); | |
53 | - | |
54 | - cli->sendMsg(buffer); | |
51 | + if (c->enable()) | |
52 | + cli->sendMsg(400, "Failed to enable wifi", true); | |
53 | + else | |
54 | + cli->sendMsg(200, "Wifi Enabled", false); | |
55 | 55 | return 0; |
56 | 56 | } |
57 | 57 |
@@ -61,10 +61,11 @@ CommandListener::WifiDisableCmd::WifiDisableCmd() : | ||
61 | 61 | |
62 | 62 | int CommandListener::WifiDisableCmd::runCommand(SocketClient *cli, char *data) { |
63 | 63 | Controller *c = NetworkManager::Instance()->findController("WIFI"); |
64 | - char buffer[32]; | |
65 | 64 | |
66 | - sprintf(buffer, "WIFI_DISABLE:%d", (c->disable() ? errno : 0)); | |
67 | - cli->sendMsg(buffer); | |
65 | + if (c->disable()) | |
66 | + cli->sendMsg(400, "Failed to disable wifi", true); | |
67 | + else | |
68 | + cli->sendMsg(200, "Wifi Disabled", false); | |
68 | 69 | return 0; |
69 | 70 | } |
70 | 71 |
@@ -77,7 +78,6 @@ int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) { | ||
77 | 78 | |
78 | 79 | WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI"); |
79 | 80 | |
80 | - char buffer[32]; | |
81 | 81 | int mode = 0; |
82 | 82 | char *bword, *last; |
83 | 83 |
@@ -93,8 +93,11 @@ int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) { | ||
93 | 93 | |
94 | 94 | mode = atoi(bword); |
95 | 95 | |
96 | - sprintf(buffer, "WIFI_SCAN:%d", (wc->setScanMode(mode) ? errno : 0)); | |
97 | - cli->sendMsg(buffer); | |
96 | + if (wc->setScanMode(mode)) | |
97 | + cli->sendMsg(400, "Failed to set scan mode", true); | |
98 | + else | |
99 | + cli->sendMsg(200, "Scan mode set", false); | |
100 | + | |
98 | 101 | return 0; |
99 | 102 | } |
100 | 103 |
@@ -112,16 +115,16 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat | ||
112 | 115 | char buffer[256]; |
113 | 116 | |
114 | 117 | for(it = src->begin(); it != src->end(); ++it) { |
115 | - sprintf(buffer, "WIFI_SCAN_RESULT:%s:%u:%d:%s:%s", | |
118 | + sprintf(buffer, "%s:%u:%d:%s:%s", | |
116 | 119 | (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(), |
117 | 120 | (*it)->getFlags(), (*it)->getSsid()); |
118 | - cli->sendMsg(buffer); | |
121 | + cli->sendMsg(125, buffer, false); | |
119 | 122 | delete (*it); |
120 | 123 | it = src->erase(it); |
121 | 124 | } |
122 | 125 | |
123 | 126 | delete src; |
124 | - cli->sendMsg("WIFI_SCAN_RESULT:0"); | |
127 | + cli->sendMsg(200, "Scan results complete", false); | |
125 | 128 | return 0; |
126 | 129 | } |
127 | 130 |
@@ -134,10 +137,11 @@ CommandListener::VpnEnableCmd::VpnEnableCmd() : | ||
134 | 137 | |
135 | 138 | int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) { |
136 | 139 | Controller *c = NetworkManager::Instance()->findController("VPN"); |
137 | - char buffer[32]; | |
138 | 140 | |
139 | - sprintf(buffer, "VPN_ENABLE:%d", (c->enable() ? errno : 0)); | |
140 | - cli->sendMsg(buffer); | |
141 | + if (c->enable()) | |
142 | + cli->sendMsg(400, "Failed to enable VPN", true); | |
143 | + else | |
144 | + cli->sendMsg(200, "VPN enabled", false); | |
141 | 145 | return 0; |
142 | 146 | } |
143 | 147 |
@@ -147,9 +151,10 @@ CommandListener::VpnDisableCmd::VpnDisableCmd() : | ||
147 | 151 | |
148 | 152 | int CommandListener::VpnDisableCmd::runCommand(SocketClient *cli, char *data) { |
149 | 153 | Controller *c = NetworkManager::Instance()->findController("VPN"); |
150 | - char buffer[32]; | |
151 | 154 | |
152 | - sprintf(buffer, "VPN_DISABLE:%d", (c->disable() ? errno : 0)); | |
153 | - cli->sendMsg(buffer); | |
155 | + if (c->disable()) | |
156 | + cli->sendMsg(400, "Failed to disable VPN", true); | |
157 | + else | |
158 | + cli->sendMsg(200, "VPN disabled", false); | |
154 | 159 | return 0; |
155 | 160 | } |
@@ -14,6 +14,7 @@ | ||
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | #include <stdlib.h> |
17 | +#include <ctype.h> | |
17 | 18 | |
18 | 19 | #define LOG_TAG "ScanResult" |
19 | 20 | #include <cutils/log.h> |
@@ -24,30 +25,53 @@ ScanResult::ScanResult() { | ||
24 | 25 | } |
25 | 26 | |
26 | 27 | ScanResult::ScanResult(char *rawResult) { |
27 | - char *tok, *next = NULL; | |
28 | + char *p = rawResult, *q = rawResult; | |
29 | + char tmp[255]; | |
28 | 30 | |
29 | - if (!(tok = strtok_r(rawResult, "\t", &next))) | |
30 | - goto out_bad; | |
31 | - mBssid = strdup(tok); | |
31 | + // BSSID | |
32 | + for (q = p; *q != '\t'; ++q); | |
33 | + strncpy(tmp, p, (q - p)); | |
34 | + tmp[q-p] = '\0'; | |
35 | + mBssid = strdup(tmp); | |
36 | + ++q; | |
32 | 37 | |
33 | - if (!(tok = strtok_r(NULL, "\t", &next))) | |
34 | - goto out_bad; | |
35 | - mFreq = atoi(tok); | |
38 | + // FREQ | |
39 | + for (p = q; *q != '\t'; ++q); | |
40 | + strncpy(tmp, p, (q - p)); | |
41 | + tmp[q-p] = '\0'; | |
42 | + mFreq = atoi(tmp); | |
43 | + ++q; | |
36 | 44 | |
37 | - if (!(tok = strtok_r(NULL, "\t", &next))) | |
38 | - goto out_bad; | |
39 | - mLevel = atoi(tok); | |
45 | + // LEVEL | |
46 | + for (p = q; *q != '\t'; ++q); | |
47 | + strncpy(tmp, p, (q - p)); | |
48 | + tmp[q-p] = '\0'; | |
49 | + mLevel = atoi(tmp); | |
50 | + ++q; | |
40 | 51 | |
41 | - if (!(tok = strtok_r(rawResult, "\t", &next))) | |
42 | - goto out_bad; | |
43 | - mFlags = strdup(tok); | |
52 | + // FLAGS | |
53 | + for (p = q; *q != '\t'; ++q); | |
54 | + strncpy(tmp, p, (q - p)); | |
55 | + tmp[q-p] = '\0'; | |
56 | + mFlags = strdup(tmp); | |
57 | + ++q; | |
44 | 58 | |
45 | - if (!(tok = strtok_r(rawResult, "\t", &next))) | |
46 | - goto out_bad; | |
47 | - mSsid = strdup(tok); | |
59 | + // XXX: For some reason Supplicant sometimes sends a double-tab here. | |
60 | + // haven't had time to dig into it ... | |
61 | + if (*q == '\t') | |
62 | + q++; | |
63 | + | |
64 | + for (p = q; *q != '\t'; ++q) { | |
65 | + if (*q == '\0') | |
66 | + break; | |
67 | + } | |
48 | 68 | |
49 | - return; | |
69 | + strncpy(tmp, p, (q - p)); | |
70 | + tmp[q-p] = '\0'; | |
71 | + mSsid = strdup(tmp); | |
72 | + ++q; | |
50 | 73 | |
74 | + return; | |
51 | 75 | out_bad: |
52 | 76 | LOGW("Malformatted scan result (%s)", rawResult); |
53 | 77 | } |
@@ -15,12 +15,16 @@ | ||
15 | 15 | */ |
16 | 16 | |
17 | 17 | #include <stdlib.h> |
18 | +#include <sys/types.h> | |
19 | +#include <fcntl.h> | |
18 | 20 | #include <errno.h> |
19 | 21 | |
20 | 22 | #define LOG_TAG "Supplicant" |
21 | 23 | #include <cutils/log.h> |
22 | 24 | #include <cutils/properties.h> |
23 | 25 | |
26 | +#include "private/android_filesystem_config.h" | |
27 | + | |
24 | 28 | #undef HAVE_LIBC_SYSTEM_PROPERTIES |
25 | 29 | |
26 | 30 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
@@ -41,6 +45,9 @@ | ||
41 | 45 | #define DRIVER_PROP_NAME "wlan.driver.status" |
42 | 46 | #define SUPPLICANT_NAME "wpa_supplicant" |
43 | 47 | #define SUPP_PROP_NAME "init.svc.wpa_supplicant" |
48 | +#define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf" | |
49 | +#define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf" | |
50 | + | |
44 | 51 | |
45 | 52 | Supplicant::Supplicant() { |
46 | 53 | mCtrl = NULL; |
@@ -55,7 +62,10 @@ Supplicant::Supplicant() { | ||
55 | 62 | } |
56 | 63 | |
57 | 64 | int Supplicant::start() { |
58 | - // XXX: Validate supplicant config file | |
65 | + | |
66 | + if (setupConfig()) { | |
67 | + LOGW("Unable to setup supplicant.conf"); | |
68 | + } | |
59 | 69 | |
60 | 70 | char status[PROPERTY_VALUE_MAX] = {'\0'}; |
61 | 71 | int count = 200; |
@@ -66,7 +76,6 @@ int Supplicant::start() { | ||
66 | 76 | |
67 | 77 | if (property_get(SUPP_PROP_NAME, status, NULL) && |
68 | 78 | !strcmp(status, "running")) { |
69 | - LOGD("Supplicant already started"); | |
70 | 79 | } else { |
71 | 80 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
72 | 81 | pi = __system_property_find(SUPP_PROP_NAME); |
@@ -93,7 +102,7 @@ int Supplicant::start() { | ||
93 | 102 | } |
94 | 103 | #else |
95 | 104 | if (property_get(SUPP_PROP_NAME, status, NULL)) { |
96 | - if (strcmp(status, "running") == 0) | |
105 | + if (!strcmp(status, "running")) | |
97 | 106 | break; |
98 | 107 | } |
99 | 108 | #endif |
@@ -348,9 +357,9 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) { | ||
348 | 357 | while((linep = strtok_r(NULL, "\n", &linep_next))) |
349 | 358 | mLatestScanResults->push_back(new ScanResult(linep)); |
350 | 359 | |
351 | - char tmp[32]; | |
352 | - sprintf(tmp, "WIFI_SCAN_RESULTS_READY:%d", mLatestScanResults->size()); | |
353 | - NetworkManager::Instance()->getBroadcaster()->sendBroadcast(tmp); | |
360 | + char tmp[128]; | |
361 | + sprintf(tmp, "%d scan results ready", mLatestScanResults->size()); | |
362 | + NetworkManager::Instance()->getBroadcaster()->sendBroadcast(600, tmp, false); | |
354 | 363 | pthread_mutex_unlock(&mLatestScanResultsLock); |
355 | 364 | free(reply); |
356 | 365 | } else { |
@@ -403,4 +412,52 @@ ScanResultCollection *Supplicant::createLatestScanResults() { | ||
403 | 412 | |
404 | 413 | pthread_mutex_unlock(&mLatestScanResultsLock); |
405 | 414 | return d; |
406 | -}; | |
415 | +} | |
416 | + | |
417 | +int Supplicant::setupConfig() { | |
418 | + char buf[2048]; | |
419 | + int srcfd, destfd; | |
420 | + int nread; | |
421 | + | |
422 | + if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) { | |
423 | + return 0; | |
424 | + } else if (errno != ENOENT) { | |
425 | + LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno)); | |
426 | + return -1; | |
427 | + } | |
428 | + | |
429 | + srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY); | |
430 | + if (srcfd < 0) { | |
431 | + LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno)); | |
432 | + return -1; | |
433 | + } | |
434 | + | |
435 | + destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660); | |
436 | + if (destfd < 0) { | |
437 | + close(srcfd); | |
438 | + LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno)); | |
439 | + return -1; | |
440 | + } | |
441 | + | |
442 | + while ((nread = read(srcfd, buf, sizeof(buf))) != 0) { | |
443 | + if (nread < 0) { | |
444 | + LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno)); | |
445 | + close(srcfd); | |
446 | + close(destfd); | |
447 | + unlink(SUPP_CONFIG_FILE); | |
448 | + return -1; | |
449 | + } | |
450 | + write(destfd, buf, nread); | |
451 | + } | |
452 | + | |
453 | + close(destfd); | |
454 | + close(srcfd); | |
455 | + | |
456 | + if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) { | |
457 | + LOGE("Error changing group ownership of %s to %d: %s", | |
458 | + SUPP_CONFIG_FILE, AID_WIFI, strerror(errno)); | |
459 | + unlink(SUPP_CONFIG_FILE); | |
460 | + return -1; | |
461 | + } | |
462 | + return 0; | |
463 | +} |
@@ -67,6 +67,7 @@ public: | ||
67 | 67 | private: |
68 | 68 | int connectToSupplicant(); |
69 | 69 | int sendCommand(const char *cmd, char *reply, size_t *reply_len); |
70 | + int setupConfig(); | |
70 | 71 | }; |
71 | 72 | |
72 | 73 | #endif |
@@ -67,15 +67,6 @@ int TiwlanWifiController::loadFirmware() { | ||
67 | 67 | } |
68 | 68 | |
69 | 69 | bool TiwlanWifiController::isFirmwareLoaded() { |
70 | - char driver_status[PROPERTY_VALUE_MAX]; | |
71 | - if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) { | |
72 | - if (!strcmp(driver_status, "ok")) | |
73 | - return true; | |
74 | - else { | |
75 | - LOGD("Driver status '%s'", driver_status); | |
76 | - return false; | |
77 | - } | |
78 | - } | |
79 | - LOGW("Unable to get property '%s'", DRIVER_PROP_NAME); | |
70 | + // Always load the firmware | |
80 | 71 | return false; |
81 | 72 | } |
@@ -93,11 +93,8 @@ out_powerdown: | ||
93 | 93 | return -1; |
94 | 94 | } |
95 | 95 | |
96 | -void WifiController::sendStatusBroadcast(const char *msg) { | |
97 | - char tmp[255]; | |
98 | - | |
99 | - sprintf(tmp, "WIFI_STATUS:%s", msg); | |
100 | - NetworkManager::Instance()->getBroadcaster()->sendBroadcast(tmp); | |
96 | +void WifiController::sendStatusBroadcast(char *msg) { | |
97 | + NetworkManager::Instance()->getBroadcaster()->sendBroadcast(600, msg, false); | |
101 | 98 | } |
102 | 99 | |
103 | 100 | int WifiController::disable() { |
@@ -77,7 +77,7 @@ protected: | ||
77 | 77 | virtual bool isFirmwareLoaded() = 0; |
78 | 78 | virtual bool isPoweredUp() = 0; |
79 | 79 | |
80 | - void sendStatusBroadcast(const char *msg); | |
80 | + void sendStatusBroadcast(char *msg); | |
81 | 81 | }; |
82 | 82 | |
83 | 83 | #endif |
@@ -54,6 +54,8 @@ int main() { | ||
54 | 54 | exit (1); |
55 | 55 | } |
56 | 56 | |
57 | + // XXX: we'll use the main thread for the NetworkManager eventuall | |
58 | + | |
57 | 59 | while(1) { |
58 | 60 | sleep(1000); |
59 | 61 | } |
@@ -31,10 +31,6 @@ | ||
31 | 31 | |
32 | 32 | #include <private/android_filesystem_config.h> |
33 | 33 | |
34 | -static void signal_handler(int sig) { | |
35 | - fprintf(stdout, "{ interrupt! }\n"); | |
36 | -} | |
37 | - | |
38 | 34 | int main(int argc, char **argv) { |
39 | 35 | int sock; |
40 | 36 |
@@ -47,56 +43,81 @@ int main(int argc, char **argv) { | ||
47 | 43 | |
48 | 44 | printf("Connected to nexus\n"); |
49 | 45 | |
46 | + char line[255]; | |
47 | + char *buffer = malloc(4096); | |
48 | + int cursor = 0; | |
49 | + int col = 0; | |
50 | + | |
50 | 51 | while(1) { |
51 | 52 | fd_set read_fds; |
52 | 53 | struct timeval to; |
53 | 54 | int rc = 0; |
54 | 55 | |
55 | - signal(SIGINT, SIG_DFL); | |
56 | - | |
57 | - printf("-> "); | |
58 | - fflush(stdout); | |
59 | - | |
60 | - char buffer[255]; | |
61 | - if (!fgets(buffer, sizeof(buffer) -1, stdin)) { | |
62 | - printf("Exiting...\n"); | |
63 | - exit(0); | |
64 | - } | |
65 | - | |
66 | - buffer[strlen(buffer) -1] = 0; | |
67 | - | |
68 | - if (write(sock, buffer, strlen(buffer) +1) < 0) { | |
69 | - fprintf(stderr, "Error writing data (%s)\n", strerror(errno)); | |
70 | - exit(2); | |
71 | - } | |
72 | - | |
73 | -wait: | |
74 | 56 | to.tv_sec = 10; |
75 | 57 | to.tv_usec = 0; |
58 | + | |
76 | 59 | FD_ZERO(&read_fds); |
77 | 60 | FD_SET(sock, &read_fds); |
61 | + FD_SET(0, &read_fds); | |
62 | + | |
63 | + if (col == 0) { | |
64 | + fprintf(stdout, "-> "); | |
65 | + fflush(stdout); | |
66 | + col = 3; | |
67 | + } | |
78 | 68 | |
79 | - signal(SIGINT, signal_handler); | |
80 | - | |
81 | 69 | if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) { |
82 | - if (errno == EINTR) | |
83 | - continue; | |
84 | 70 | fprintf(stderr, "Error in select (%s)\n", strerror(errno)); |
85 | 71 | exit(2); |
86 | 72 | } else if (!rc) { |
87 | - printf("{response timeout}\n"); | |
88 | 73 | continue; |
89 | 74 | } else if (FD_ISSET(sock, &read_fds)) { |
90 | - if ((rc = read(sock, buffer, sizeof(buffer)-1)) <= 0) { | |
75 | + memset(buffer, 0, 4096); | |
76 | + if ((rc = read(sock, buffer, 4096)) <= 0) { | |
91 | 77 | fprintf(stderr, "Error reading response (%s)\n", strerror(errno)); |
92 | 78 | exit(2); |
93 | - } | |
94 | - printf(" %s\n", buffer); | |
95 | - goto wait; | |
79 | + } | |
80 | + int i; | |
81 | + for (i = 0; i < col; i++) { | |
82 | + fprintf(stdout, "%c", 8); | |
83 | + } | |
84 | + | |
85 | + printf("%s", buffer); | |
86 | + printf("-> "); | |
87 | + for (i = 0; i < cursor; i++) { | |
88 | + fprintf(stdout, "%c", line[i]); | |
89 | + } | |
90 | + fflush(stdout); | |
91 | + } else if (FD_ISSET(0, &read_fds)) { | |
92 | + char c; | |
93 | + | |
94 | + if ((rc = read(0, &c, 1)) < 0) { | |
95 | + fprintf(stderr, "Error reading from terminal (%s)\n", strerror(errno)); | |
96 | + exit(2); | |
97 | + } else if (!rc) { | |
98 | + fprintf(stderr, "0 length read from terminal\n"); | |
99 | + exit(2); | |
100 | + } | |
101 | + | |
102 | + fprintf(stdout, "%c", c); | |
103 | + fflush(stdout); | |
104 | + | |
105 | + line[cursor] = c; | |
106 | + | |
107 | + if (c == '\n') { | |
108 | + if ((rc = write(sock, line, strlen(line))) < 0) { | |
109 | + fprintf(stderr, "Error writing to nexus (%s)\n", strerror(errno)); | |
110 | + exit(2); | |
111 | + } | |
112 | + memset(line, 0, sizeof(line)); | |
113 | + cursor = 0; | |
114 | + col = 0; | |
115 | + } else { | |
116 | + cursor++; | |
117 | + col++; | |
118 | + } | |
96 | 119 | } |
97 | 120 | } |
98 | 121 | |
99 | - | |
100 | 122 | exit(0); |
101 | - | |
102 | 123 | } |