• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

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>

変更サマリ

差分

--- a/nexus/CommandListener.cpp
+++ b/nexus/CommandListener.cpp
@@ -47,11 +47,11 @@ CommandListener::WifiEnableCmd::WifiEnableCmd() :
4747
4848 int CommandListener::WifiEnableCmd::runCommand(SocketClient *cli, char *data) {
4949 Controller *c = NetworkManager::Instance()->findController("WIFI");
50- char buffer[32];
5150
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);
5555 return 0;
5656 }
5757
@@ -61,10 +61,11 @@ CommandListener::WifiDisableCmd::WifiDisableCmd() :
6161
6262 int CommandListener::WifiDisableCmd::runCommand(SocketClient *cli, char *data) {
6363 Controller *c = NetworkManager::Instance()->findController("WIFI");
64- char buffer[32];
6564
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);
6869 return 0;
6970 }
7071
@@ -77,7 +78,6 @@ int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) {
7778
7879 WifiController *wc = (WifiController *) NetworkManager::Instance()->findController("WIFI");
7980
80- char buffer[32];
8181 int mode = 0;
8282 char *bword, *last;
8383
@@ -93,8 +93,11 @@ int CommandListener::WifiScanCmd::runCommand(SocketClient *cli, char *data) {
9393
9494 mode = atoi(bword);
9595
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+
98101 return 0;
99102 }
100103
@@ -112,16 +115,16 @@ int CommandListener::WifiScanResultsCmd::runCommand(SocketClient *cli, char *dat
112115 char buffer[256];
113116
114117 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",
116119 (*it)->getBssid(), (*it)->getFreq(), (*it)->getLevel(),
117120 (*it)->getFlags(), (*it)->getSsid());
118- cli->sendMsg(buffer);
121+ cli->sendMsg(125, buffer, false);
119122 delete (*it);
120123 it = src->erase(it);
121124 }
122125
123126 delete src;
124- cli->sendMsg("WIFI_SCAN_RESULT:0");
127+ cli->sendMsg(200, "Scan results complete", false);
125128 return 0;
126129 }
127130
@@ -134,10 +137,11 @@ CommandListener::VpnEnableCmd::VpnEnableCmd() :
134137
135138 int CommandListener::VpnEnableCmd::runCommand(SocketClient *cli, char *data) {
136139 Controller *c = NetworkManager::Instance()->findController("VPN");
137- char buffer[32];
138140
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);
141145 return 0;
142146 }
143147
@@ -147,9 +151,10 @@ CommandListener::VpnDisableCmd::VpnDisableCmd() :
147151
148152 int CommandListener::VpnDisableCmd::runCommand(SocketClient *cli, char *data) {
149153 Controller *c = NetworkManager::Instance()->findController("VPN");
150- char buffer[32];
151154
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);
154159 return 0;
155160 }
--- a/nexus/ScanResult.cpp
+++ b/nexus/ScanResult.cpp
@@ -14,6 +14,7 @@
1414 * limitations under the License.
1515 */
1616 #include <stdlib.h>
17+#include <ctype.h>
1718
1819 #define LOG_TAG "ScanResult"
1920 #include <cutils/log.h>
@@ -24,30 +25,53 @@ ScanResult::ScanResult() {
2425 }
2526
2627 ScanResult::ScanResult(char *rawResult) {
27- char *tok, *next = NULL;
28+ char *p = rawResult, *q = rawResult;
29+ char tmp[255];
2830
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;
3237
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;
3644
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;
4051
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;
4458
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+ }
4868
49- return;
69+ strncpy(tmp, p, (q - p));
70+ tmp[q-p] = '\0';
71+ mSsid = strdup(tmp);
72+ ++q;
5073
74+ return;
5175 out_bad:
5276 LOGW("Malformatted scan result (%s)", rawResult);
5377 }
--- a/nexus/Supplicant.cpp
+++ b/nexus/Supplicant.cpp
@@ -15,12 +15,16 @@
1515 */
1616
1717 #include <stdlib.h>
18+#include <sys/types.h>
19+#include <fcntl.h>
1820 #include <errno.h>
1921
2022 #define LOG_TAG "Supplicant"
2123 #include <cutils/log.h>
2224 #include <cutils/properties.h>
2325
26+#include "private/android_filesystem_config.h"
27+
2428 #undef HAVE_LIBC_SYSTEM_PROPERTIES
2529
2630 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
@@ -41,6 +45,9 @@
4145 #define DRIVER_PROP_NAME "wlan.driver.status"
4246 #define SUPPLICANT_NAME "wpa_supplicant"
4347 #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+
4451
4552 Supplicant::Supplicant() {
4653 mCtrl = NULL;
@@ -55,7 +62,10 @@ Supplicant::Supplicant() {
5562 }
5663
5764 int Supplicant::start() {
58- // XXX: Validate supplicant config file
65+
66+ if (setupConfig()) {
67+ LOGW("Unable to setup supplicant.conf");
68+ }
5969
6070 char status[PROPERTY_VALUE_MAX] = {'\0'};
6171 int count = 200;
@@ -66,7 +76,6 @@ int Supplicant::start() {
6676
6777 if (property_get(SUPP_PROP_NAME, status, NULL) &&
6878 !strcmp(status, "running")) {
69- LOGD("Supplicant already started");
7079 } else {
7180 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
7281 pi = __system_property_find(SUPP_PROP_NAME);
@@ -93,7 +102,7 @@ int Supplicant::start() {
93102 }
94103 #else
95104 if (property_get(SUPP_PROP_NAME, status, NULL)) {
96- if (strcmp(status, "running") == 0)
105+ if (!strcmp(status, "running"))
97106 break;
98107 }
99108 #endif
@@ -348,9 +357,9 @@ int Supplicant::onScanResultsEvent(SupplicantEvent *evt) {
348357 while((linep = strtok_r(NULL, "\n", &linep_next)))
349358 mLatestScanResults->push_back(new ScanResult(linep));
350359
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);
354363 pthread_mutex_unlock(&mLatestScanResultsLock);
355364 free(reply);
356365 } else {
@@ -403,4 +412,52 @@ ScanResultCollection *Supplicant::createLatestScanResults() {
403412
404413 pthread_mutex_unlock(&mLatestScanResultsLock);
405414 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+}
--- a/nexus/Supplicant.h
+++ b/nexus/Supplicant.h
@@ -67,6 +67,7 @@ public:
6767 private:
6868 int connectToSupplicant();
6969 int sendCommand(const char *cmd, char *reply, size_t *reply_len);
70+ int setupConfig();
7071 };
7172
7273 #endif
--- a/nexus/TiwlanWifiController.cpp
+++ b/nexus/TiwlanWifiController.cpp
@@ -67,15 +67,6 @@ int TiwlanWifiController::loadFirmware() {
6767 }
6868
6969 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
8071 return false;
8172 }
--- a/nexus/WifiController.cpp
+++ b/nexus/WifiController.cpp
@@ -93,11 +93,8 @@ out_powerdown:
9393 return -1;
9494 }
9595
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);
10198 }
10299
103100 int WifiController::disable() {
--- a/nexus/WifiController.h
+++ b/nexus/WifiController.h
@@ -77,7 +77,7 @@ protected:
7777 virtual bool isFirmwareLoaded() = 0;
7878 virtual bool isPoweredUp() = 0;
7979
80- void sendStatusBroadcast(const char *msg);
80+ void sendStatusBroadcast(char *msg);
8181 };
8282
8383 #endif
--- a/nexus/main.cpp
+++ b/nexus/main.cpp
@@ -54,6 +54,8 @@ int main() {
5454 exit (1);
5555 }
5656
57+ // XXX: we'll use the main thread for the NetworkManager eventuall
58+
5759 while(1) {
5860 sleep(1000);
5961 }
--- a/nexus/nexctl.c
+++ b/nexus/nexctl.c
@@ -31,10 +31,6 @@
3131
3232 #include <private/android_filesystem_config.h>
3333
34-static void signal_handler(int sig) {
35- fprintf(stdout, "{ interrupt! }\n");
36-}
37-
3834 int main(int argc, char **argv) {
3935 int sock;
4036
@@ -47,56 +43,81 @@ int main(int argc, char **argv) {
4743
4844 printf("Connected to nexus\n");
4945
46+ char line[255];
47+ char *buffer = malloc(4096);
48+ int cursor = 0;
49+ int col = 0;
50+
5051 while(1) {
5152 fd_set read_fds;
5253 struct timeval to;
5354 int rc = 0;
5455
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:
7456 to.tv_sec = 10;
7557 to.tv_usec = 0;
58+
7659 FD_ZERO(&read_fds);
7760 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+ }
7868
79- signal(SIGINT, signal_handler);
80-
8169 if ((rc = select(sock +1, &read_fds, NULL, NULL, &to)) < 0) {
82- if (errno == EINTR)
83- continue;
8470 fprintf(stderr, "Error in select (%s)\n", strerror(errno));
8571 exit(2);
8672 } else if (!rc) {
87- printf("{response timeout}\n");
8873 continue;
8974 } 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) {
9177 fprintf(stderr, "Error reading response (%s)\n", strerror(errno));
9278 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+ }
96119 }
97120 }
98121
99-
100122 exit(0);
101-
102123 }