ucmd_netmenu.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /* netmenu: the top-bar network menu.
  2. *
  3. * Lists the NICs the kernel reports as present (Ethernet, USB Ethernet,
  4. * RNDIS tethering) and the WiFi networks from a scan. Selecting a wired or
  5. * USB device makes it the active netdev and runs DHCP; selecting a WiFi
  6. * network joins it with the passphrase saved in /data/WIFI.CFG when one is
  7. * known, otherwise it asks for the WPA2 passphrase, then starts the existing
  8. * `wifi` supplicant with a staged /share/netmenu.wifi request (the SYS_SPAWN
  9. * argument limit is too small for an SSID plus a long passphrase). A Saved
  10. * view lists the known networks and forgets individual entries. The
  11. * supplicant itself runs DHCP once the data port opens.
  12. *
  13. * A small ngui canvas window; the WM places it under the network icon and
  14. * opens only one instance at a time. */
  15. #include "ucmd_lib.h"
  16. #include "ngui.h"
  17. #include "wifi_store.h"
  18. #define MAX_DEVS 8
  19. #define MAX_BSS 32
  20. #define ROW_H 24
  21. #define LOG_MAX 2048
  22. #define ID_REFRESH 1
  23. #define ID_SCAN 2
  24. #define ID_DISCONNECT 3
  25. #define ID_PASS_OK 4
  26. #define ID_PASS_CANCEL 5
  27. #define ID_CONN_CANCEL 6
  28. #define ID_PASS 7
  29. #define ID_SAVED 8
  30. #define ID_SAVED_BACK 9
  31. #define ID_DEV_BASE 100
  32. #define ID_BSS_BASE 200
  33. #define ID_SAVED_BASE 300
  34. #define ACT_REFRESH (1u << 0)
  35. #define ACT_SCAN (1u << 1)
  36. #define ACT_DISCONNECT (1u << 2)
  37. #define ACT_BACK (1u << 3)
  38. #define ACT_CONNECT (1u << 4)
  39. #define ACT_SAVED (1u << 5)
  40. #define ACT_FORGET (1u << 6)
  41. enum { VIEW_MAIN, VIEW_PASS, VIEW_CONNECT, VIEW_SAVED };
  42. static struct ngui ui;
  43. static struct ngui_input pass_in;
  44. static struct ngui_scroll wifi_scroll;
  45. static struct ngui_scroll saved_scroll;
  46. static struct sys_netdev devs[MAX_DEVS];
  47. static int ndev;
  48. static struct sys_wifi_bss bss[MAX_BSS];
  49. static int nbss;
  50. static struct wifi_cfg saved[WIFI_CFG_MAX];
  51. static int nsaved;
  52. static struct sys_wifi_status wst;
  53. static struct sys_netinfo net;
  54. static int net_present;
  55. static int wifi_checked;
  56. static int wifi_present;
  57. static int view;
  58. static int sel_bss = -1;
  59. static char sel_ssid[33];
  60. static int scanning;
  61. static unsigned int scan_start;
  62. static int connect_pid = -1;
  63. static int connect_tty;
  64. static unsigned int connect_start;
  65. static char conn_log[LOG_MAX];
  66. static unsigned int conn_log_len;
  67. static char status[160];
  68. static int status_error;
  69. static unsigned int status_until;
  70. static unsigned int actions;
  71. static int action_dev = -1;
  72. static int action_bss = -1;
  73. static int action_saved = -1;
  74. static void draw(void);
  75. static void paint_frame(void);
  76. static unsigned int slen(const char *s)
  77. {
  78. unsigned int n = 0;
  79. while (s && s[n])
  80. n++;
  81. return n;
  82. }
  83. static int seq(const char *a, const char *b)
  84. {
  85. while (*a && *b) {
  86. if (*a != *b)
  87. return 0;
  88. a++;
  89. b++;
  90. }
  91. return *a == *b;
  92. }
  93. static char *sput(char *p, const char *s)
  94. {
  95. while (s && *s)
  96. *p++ = *s++;
  97. *p = '\0';
  98. return p;
  99. }
  100. static char *put_dec(char *p, unsigned int v)
  101. {
  102. char b[10];
  103. unsigned int n = 0;
  104. do {
  105. b[n++] = (char)('0' + v % 10);
  106. v /= 10;
  107. } while (v);
  108. while (n)
  109. *p++ = b[--n];
  110. return p;
  111. }
  112. static char *put_signed(char *p, int v)
  113. {
  114. if (v < 0) {
  115. *p++ = '-';
  116. v = -v;
  117. }
  118. return put_dec(p, (unsigned int)v);
  119. }
  120. static char *put_ip(char *p, const unsigned char ip[4])
  121. {
  122. p = put_dec(p, ip[0]); *p++ = '.';
  123. p = put_dec(p, ip[1]); *p++ = '.';
  124. p = put_dec(p, ip[2]); *p++ = '.';
  125. p = put_dec(p, ip[3]);
  126. return p;
  127. }
  128. static void ssid_name(const struct sys_wifi_bss *b, char out[33])
  129. {
  130. unsigned int i, n = b->ssid_len;
  131. if (n > 32)
  132. n = 32;
  133. /* Keep the raw UTF-8 bytes; ngui's UTF-8 renderer handles them and the
  134. * connection path needs the exact SSID. */
  135. for (i = 0; i < n; i++)
  136. out[i] = (char)b->ssid[i];
  137. out[n] = '\0';
  138. if (!n) {
  139. out[0] = '(';
  140. out[1] = 'h'; out[2] = 'i'; out[3] = 'd';
  141. out[4] = 'd'; out[5] = 'e'; out[6] = 'n';
  142. out[7] = ')';
  143. out[8] = '\0';
  144. }
  145. }
  146. static void cfg_ssid_name(const struct wifi_cfg *c, char out[33])
  147. {
  148. unsigned int i, n = c->ssid_len;
  149. if (n > 32)
  150. n = 32;
  151. for (i = 0; i < n; i++)
  152. out[i] = (char)c->ssid[i];
  153. out[n] = '\0';
  154. }
  155. static void load_saved(void)
  156. {
  157. int n = wifi_store_load(saved, WIFI_CFG_MAX);
  158. nsaved = n > 0 ? n : 0;
  159. }
  160. static int bss_saved(const struct sys_wifi_bss *b)
  161. {
  162. return wifi_store_find(saved, nsaved, b->ssid, b->ssid_len);
  163. }
  164. static const char *security_name(unsigned int s)
  165. {
  166. return s == SYS_WIFI_SEC_OPEN ? "open" :
  167. s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
  168. }
  169. static int rssi_bars(int rssi)
  170. {
  171. if (rssi >= -55)
  172. return 4;
  173. if (rssi >= -65)
  174. return 3;
  175. if (rssi >= -75)
  176. return 2;
  177. return 1;
  178. }
  179. static int bss_is_active(const struct sys_wifi_bss *b)
  180. {
  181. unsigned int i;
  182. if (wst.state < SYS_WIFI_ST_ASSOC)
  183. return 0;
  184. for (i = 0; i < 6; i++)
  185. if (wst.bssid[i] != b->bssid[i])
  186. return 0;
  187. return 1;
  188. }
  189. static void set_status(const char *msg, int err)
  190. {
  191. unsigned int i;
  192. for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
  193. status[i] = msg[i];
  194. status[i] = '\0';
  195. status_error = err;
  196. status_until = msg && msg[0] ? u_ticks_ms() + 6000 : 0;
  197. ngui_invalidate(&ui);
  198. }
  199. static int dev_active(const struct sys_netdev *d)
  200. {
  201. return net_present && seq(net.name, d->name);
  202. }
  203. static void load_status(void)
  204. {
  205. int i;
  206. net_present = (u_net_info(&net) == 1 && net.present);
  207. if (!wifi_checked || wifi_present) {
  208. if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
  209. wifi_checked = 1;
  210. wifi_present = wst.present != 0;
  211. } else {
  212. /* Do not repeatedly probe a machine without a card. */
  213. wifi_checked = 1;
  214. wifi_present = 0;
  215. }
  216. }
  217. for (i = 0; i < ndev; i++) {
  218. devs[i].active = dev_active(&devs[i]) ? 1u : 0u;
  219. if (devs[i].active)
  220. devs[i].link = net.link;
  221. }
  222. }
  223. static void load_devices(void)
  224. {
  225. int n = u_net_devlist(devs, MAX_DEVS);
  226. ndev = n > 0 ? n : 0;
  227. load_status();
  228. }
  229. static void drain_tty(void)
  230. {
  231. char buf[256];
  232. int n;
  233. if (connect_tty <= 0)
  234. return;
  235. while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
  236. int i;
  237. for (i = 0; i < n; i++) {
  238. if (conn_log_len + 1 >= sizeof(conn_log)) {
  239. unsigned int half = sizeof(conn_log) / 2;
  240. unsigned int j;
  241. for (j = 0; j < half; j++)
  242. conn_log[j] = conn_log[conn_log_len -
  243. half + j];
  244. conn_log_len = half;
  245. }
  246. conn_log[conn_log_len++] = buf[i];
  247. }
  248. conn_log[conn_log_len] = '\0';
  249. ngui_invalidate(&ui);
  250. }
  251. }
  252. static void sort_bss(void)
  253. {
  254. int i, j;
  255. for (i = 1; i < nbss; i++)
  256. for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
  257. struct sys_wifi_bss t = bss[j];
  258. bss[j] = bss[j - 1];
  259. bss[j - 1] = t;
  260. }
  261. }
  262. static int same_ssid(const struct sys_wifi_bss *a,
  263. const struct sys_wifi_bss *b)
  264. {
  265. unsigned int i;
  266. if (a->ssid_len == 0 || a->ssid_len != b->ssid_len)
  267. return 0;
  268. for (i = 0; i < a->ssid_len; i++)
  269. if (a->ssid[i] != b->ssid[i])
  270. return 0;
  271. return 1;
  272. }
  273. /* Mesh/repeater setups often advertise one SSID from several BSSIDs. The
  274. * list is RSSI-sorted, so keep the strongest entry of each SSID/security
  275. * pair (or the active BSSID when one of them is the current connection).
  276. * Hidden-SSID entries are dropped: the menu cannot join them anyway. */
  277. static void filter_bss(void)
  278. {
  279. int i, j, out = 0;
  280. for (i = 0; i < nbss; i++) {
  281. int dup = -1;
  282. if (bss[i].ssid_len == 0)
  283. continue;
  284. for (j = 0; j < out; j++)
  285. if (bss[i].security == bss[j].security &&
  286. same_ssid(&bss[i], &bss[j])) {
  287. dup = j;
  288. break;
  289. }
  290. if (dup < 0) {
  291. bss[out++] = bss[i];
  292. } else if (bss_is_active(&bss[i]) &&
  293. !bss_is_active(&bss[dup])) {
  294. bss[dup] = bss[i];
  295. }
  296. }
  297. nbss = out;
  298. }
  299. static void start_scan(void)
  300. {
  301. int r;
  302. if (scanning || !wifi_present)
  303. return;
  304. set_status("Scanning for WiFi networks...", 0);
  305. paint_frame();
  306. /* SYS_WIFI_SCAN loads the firmware itself if it is still down. */
  307. r = u_wifi(SYS_WIFI_SCAN, 0, 0);
  308. if (r < 0) {
  309. char m[48], *p;
  310. u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
  311. p = sput(m, "WiFi: ");
  312. p = sput(p, wst.detail[0] ? wst.detail : "scan failed");
  313. *p = '\0';
  314. set_status(m, 1);
  315. return;
  316. }
  317. scanning = 1;
  318. scan_start = u_ticks_ms();
  319. nbss = 0;
  320. wifi_scroll.offset = 0;
  321. ngui_invalidate(&ui);
  322. }
  323. static void poll_scan(void)
  324. {
  325. int n;
  326. if (!scanning)
  327. return;
  328. n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
  329. if (n == -2) {
  330. if (u_ticks_ms() - scan_start > 15000) {
  331. scanning = 0;
  332. set_status("WiFi scan timed out", 1);
  333. }
  334. return;
  335. }
  336. scanning = 0;
  337. if (n < 0) {
  338. set_status("WiFi scan failed", 1);
  339. return;
  340. }
  341. nbss = n;
  342. sort_bss();
  343. filter_bss();
  344. if (nbss == 0)
  345. set_status("No WiFi networks found", 1);
  346. else
  347. set_status("WiFi scan complete", 0);
  348. ngui_invalidate(&ui);
  349. }
  350. static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
  351. {
  352. struct wifi_cfg cfg;
  353. unsigned int i, n = slen(pass);
  354. int r;
  355. for (i = 0; i < sizeof(cfg); i++)
  356. ((unsigned char *)&cfg)[i] = 0;
  357. cfg.magic = WIFI_CFG_MAGIC;
  358. cfg.ssid_len = b->ssid_len;
  359. cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
  360. for (i = 0; i < 6; i++)
  361. cfg.bssid[i] = b->bssid[i];
  362. for (i = 0; i < b->ssid_len && i < 32; i++)
  363. cfg.ssid[i] = b->ssid[i];
  364. for (i = 0; i < cfg.pass_len; i++)
  365. cfg.pass[i] = pass[i];
  366. r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
  367. (int)&cfg, (int)sizeof(cfg));
  368. for (i = 0; i < sizeof(cfg); i++)
  369. ((unsigned char *)&cfg)[i] = 0;
  370. return r;
  371. }
  372. static void start_connect(const char *pass)
  373. {
  374. struct sys_wifi_bss *b;
  375. char msg[80], *p;
  376. int pid;
  377. if (sel_bss < 0 || sel_bss >= nbss)
  378. return;
  379. if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
  380. set_status("A WiFi connection is already in progress", 1);
  381. return;
  382. }
  383. b = &bss[sel_bss];
  384. ssid_name(b, sel_ssid);
  385. if (write_wifi_cfg(b, pass) != 0) {
  386. set_status("Cannot stage the WiFi request in /share", 1);
  387. return;
  388. }
  389. if (connect_tty <= 0) {
  390. connect_tty = u_tty_create();
  391. if (connect_tty < 0)
  392. connect_tty = 0;
  393. }
  394. conn_log_len = 0;
  395. conn_log[0] = '\0';
  396. connect_start = u_ticks_ms();
  397. pid = u_spawn("/ramfs/bin/wifi", "connectfile /share/netmenu.wifi",
  398. connect_tty, "/");
  399. if (pid < 0) {
  400. u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
  401. set_status("Cannot start the WiFi helper", 1);
  402. return;
  403. }
  404. connect_pid = pid;
  405. view = VIEW_CONNECT;
  406. p = sput(msg, "Connecting to \"");
  407. p = sput(p, sel_ssid);
  408. p = sput(p, "\"...");
  409. *p = '\0';
  410. set_status(msg, 0);
  411. }
  412. static void cancel_connect(void)
  413. {
  414. if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
  415. u_kill((unsigned int)connect_pid);
  416. connect_pid = -1;
  417. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  418. view = VIEW_MAIN;
  419. set_status("WiFi connection cancelled", 0);
  420. load_status();
  421. }
  422. static void poll_connect(void)
  423. {
  424. int i;
  425. if (connect_pid <= 0)
  426. return;
  427. if (!u_alive((unsigned int)connect_pid)) {
  428. connect_pid = -1;
  429. load_status();
  430. load_saved();
  431. if (wst.state >= SYS_WIFI_ST_LINK) {
  432. int have_ip = 0;
  433. for (i = 0; i < 4; i++)
  434. if (net.ip[i])
  435. have_ip = 1;
  436. view = VIEW_MAIN;
  437. if (net_present && seq(net.name, "wifi")) {
  438. if (have_ip)
  439. set_status("WiFi connected and DHCP is up", 0);
  440. else
  441. set_status("WiFi connected, but DHCP got no lease", 1);
  442. } else {
  443. set_status("WiFi associated, but the netdev is busy", 1);
  444. }
  445. } else {
  446. /* Keep the log on screen so the failure reason is visible;
  447. * the Cancel button becomes Back. */
  448. set_status("WiFi connection failed", 1);
  449. }
  450. ngui_invalidate(&ui);
  451. return;
  452. }
  453. if (u_ticks_ms() - connect_start > 45000) {
  454. u_kill((unsigned int)connect_pid);
  455. connect_pid = -1;
  456. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  457. set_status("WiFi connection timed out", 1);
  458. }
  459. }
  460. static void select_device(int i)
  461. {
  462. char msg[64], *p;
  463. if (i < 0 || i >= ndev)
  464. return;
  465. p = sput(msg, "Selecting ");
  466. p = sput(p, devs[i].name);
  467. *p = '\0';
  468. set_status(msg, 0);
  469. paint_frame();
  470. if (u_net_select(devs[i].name) != 0) {
  471. set_status("Cannot select that netdev (busy or unavailable)", 1);
  472. return;
  473. }
  474. set_status("Running DHCP...", 0);
  475. paint_frame();
  476. if (u_dhcp_run(6000) == 0)
  477. set_status("Device selected; DHCP lease acquired", 0);
  478. else
  479. set_status("Device selected, but DHCP got no lease", 1);
  480. load_devices();
  481. ngui_invalidate(&ui);
  482. }
  483. static void select_bss(int i)
  484. {
  485. if (i < 0 || i >= nbss)
  486. return;
  487. if (bss[i].ssid_len == 0) {
  488. set_status("Hidden network: use `wifi connect` from a terminal", 1);
  489. return;
  490. }
  491. sel_bss = i;
  492. ssid_name(&bss[i], sel_ssid);
  493. if (bss[i].security == SYS_WIFI_SEC_OTHER) {
  494. set_status("Unsupported security (only open and WPA2-PSK)", 1);
  495. return;
  496. }
  497. if (bss[i].security == SYS_WIFI_SEC_PSK) {
  498. int si = bss_saved(&bss[i]);
  499. if (si >= 0 && saved[si].pass_len > 0) {
  500. char pass[65];
  501. unsigned int j;
  502. for (j = 0; j < saved[si].pass_len; j++)
  503. pass[j] = saved[si].pass[j];
  504. pass[saved[si].pass_len] = '\0';
  505. start_connect(pass);
  506. for (j = 0; j < sizeof(pass); j++)
  507. ((volatile char *)pass)[j] = 0;
  508. return;
  509. }
  510. ngui_input_set(&pass_in, "");
  511. pass_in.secret = 1;
  512. pass_in.placeholder = "WPA2 passphrase";
  513. view = VIEW_PASS;
  514. set_status("", 0);
  515. ngui_set_focus(&ui, ID_PASS);
  516. ngui_invalidate(&ui);
  517. return;
  518. }
  519. start_connect("");
  520. }
  521. static void handle_actions(void)
  522. {
  523. unsigned int a = actions;
  524. int di = action_dev, bi = action_bss, fi = action_saved;
  525. actions = 0;
  526. action_dev = -1;
  527. action_bss = -1;
  528. action_saved = -1;
  529. if (a & ACT_BACK) {
  530. if (view == VIEW_CONNECT)
  531. cancel_connect();
  532. else {
  533. view = VIEW_MAIN;
  534. set_status("", 0);
  535. }
  536. return;
  537. }
  538. if (a & ACT_SAVED) {
  539. load_saved();
  540. saved_scroll.offset = 0;
  541. view = VIEW_SAVED;
  542. set_status("", 0);
  543. ngui_invalidate(&ui);
  544. return;
  545. }
  546. if (a & ACT_FORGET) {
  547. if (fi >= 0 && fi < nsaved) {
  548. if (wifi_store_forget(saved[fi].ssid,
  549. saved[fi].ssid_len) > 0) {
  550. load_saved();
  551. saved_scroll.offset = 0;
  552. set_status("Saved network removed", 0);
  553. } else {
  554. set_status("Could not remove the saved network", 1);
  555. }
  556. }
  557. ngui_invalidate(&ui);
  558. return;
  559. }
  560. if (a & ACT_CONNECT) {
  561. char pass[65];
  562. unsigned int i, n;
  563. ngui_input_value(&pass_in, pass, sizeof(pass));
  564. n = slen(pass);
  565. if (n == 0) {
  566. set_status("Enter the WPA2 passphrase", 1);
  567. } else if (n < 8 || n > 63) {
  568. /* A 64-character hex string is a raw PSK. */
  569. int hex = n == 64;
  570. if (hex) {
  571. unsigned int j;
  572. for (j = 0; j < n; j++) {
  573. char c = pass[j];
  574. if (!((c >= '0' && c <= '9') ||
  575. (c >= 'a' && c <= 'f') ||
  576. (c >= 'A' && c <= 'F'))) {
  577. hex = 0;
  578. break;
  579. }
  580. }
  581. }
  582. if (hex)
  583. start_connect(pass);
  584. else
  585. set_status("Use 8-63 characters or 64 hex digits", 1);
  586. } else {
  587. start_connect(pass);
  588. }
  589. for (i = 0; i < sizeof(pass); i++)
  590. ((volatile char *)pass)[i] = 0;
  591. return;
  592. }
  593. if (a & ACT_REFRESH) {
  594. set_status("Refreshing network devices...", 0);
  595. paint_frame();
  596. load_devices();
  597. set_status("Device list refreshed", 0);
  598. ngui_invalidate(&ui);
  599. } else if (a & ACT_SCAN) {
  600. start_scan();
  601. } else if (a & ACT_DISCONNECT) {
  602. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  603. load_status();
  604. set_status("WiFi disconnected", 0);
  605. ngui_invalidate(&ui);
  606. } else if (di >= 0) {
  607. select_device(di);
  608. } else if (bi >= 0) {
  609. select_bss(bi);
  610. }
  611. }
  612. static void draw_bars(int x, int y, int bars, unsigned short color)
  613. {
  614. int i;
  615. for (i = 0; i < 4; i++) {
  616. int h = 3 + i * 3;
  617. struct ngui_rect r;
  618. r.x = x + i * 5;
  619. r.y = y + 12 - h;
  620. r.w = 3;
  621. r.h = h;
  622. ngui_fill_rect(&ui, r, i < bars ? color : ui.theme.border);
  623. }
  624. }
  625. static void draw_status(void)
  626. {
  627. if (status[0] && status_until &&
  628. (int)(u_ticks_ms() - status_until) < 0)
  629. ngui_text_utf8(&ui, 10, ui.h - 22, status,
  630. status_error ? ui.theme.error : ui.theme.accent);
  631. }
  632. static void draw_wifi_list(struct ngui_rect area)
  633. {
  634. char label[40], shortcut[20];
  635. int i;
  636. if (!wifi_present) {
  637. ngui_text(&ui, area.x + 2, area.y + 4,
  638. "No WiFi card detected", ui.theme.text_dim);
  639. return;
  640. }
  641. if (scanning) {
  642. ngui_text(&ui, area.x + 2, area.y + 4,
  643. "Scanning...", ui.theme.text_dim);
  644. return;
  645. }
  646. if (nbss == 0) {
  647. ngui_text(&ui, area.x + 2, area.y + 4,
  648. wst.state >= SYS_WIFI_ST_ASSOC ?
  649. "Connected - Disconnect to scan for other networks" :
  650. "Press Scan to list WiFi networks",
  651. ui.theme.text_dim);
  652. return;
  653. }
  654. wifi_scroll.content_h = nbss * ROW_H;
  655. ngui_scroll_begin(&ui, area, &wifi_scroll);
  656. ngui_scroll_wheel(&ui, area, &wifi_scroll);
  657. ngui_scroll_keys(&ui, &wifi_scroll);
  658. for (i = 0; i < nbss; i++) {
  659. struct ngui_rect rr;
  660. struct sys_wifi_bss *b = &bss[i];
  661. char ssid[33];
  662. int sw, bx, clicked;
  663. unsigned short signal;
  664. rr.x = area.x;
  665. rr.y = area.y - wifi_scroll.offset + i * ROW_H;
  666. rr.w = area.w - 16;
  667. rr.h = ROW_H - 2;
  668. if (rr.y + rr.h <= area.y || rr.y >= area.y + area.h)
  669. continue;
  670. ssid_name(b, ssid);
  671. sput(label, ssid);
  672. sput(shortcut, security_name(b->security));
  673. if (bss_saved(b) >= 0)
  674. sput(shortcut + slen(shortcut), " saved");
  675. clicked = ngui_menu_item_utf8(&ui, ID_BSS_BASE + i, rr, label,
  676. shortcut, bss_is_active(b));
  677. sw = (int)ngui_text_width_ui(&ui, shortcut);
  678. bx = rr.x + rr.w - 8 - sw - 30;
  679. if (b->security == SYS_WIFI_SEC_OTHER)
  680. signal = ui.theme.error;
  681. else
  682. signal = rssi_bars(b->rssi) >= 3 ? ui.theme.accent :
  683. ui.theme.text_dim;
  684. draw_bars(bx, rr.y + 3, rssi_bars(b->rssi), signal);
  685. if (clicked) {
  686. action_bss = i;
  687. }
  688. }
  689. ngui_scroll_end(&ui, area, &wifi_scroll);
  690. }
  691. static void draw_main(void)
  692. {
  693. struct ngui_rect body = { 10, 8, ui.w - 20, ui.h - 34 };
  694. struct ngui_rect r, rb;
  695. char line[128], *p;
  696. int i, shown;
  697. /* Active connection summary. */
  698. r = ngui_cut_top(&body, 20);
  699. p = line;
  700. if (!net_present) {
  701. p = sput(p, "No active network device");
  702. } else {
  703. p = sput(p, "Active: ");
  704. p = sput(p, net.name);
  705. p = sput(p, " (");
  706. p = sput(p, net.chip);
  707. p = sput(p, ")");
  708. if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
  709. p = sput(p, " ");
  710. for (i = 0; i < wst.ssid_len && i < 32; i++)
  711. *p++ = (char)wst.ssid[i];
  712. p = sput(p, " ");
  713. p = put_signed(p, wst.rssi);
  714. p = sput(p, " dBm");
  715. }
  716. }
  717. *p = '\0';
  718. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  719. r = ngui_cut_top(&body, 20);
  720. p = line;
  721. if (net_present) {
  722. p = sput(p, "IP ");
  723. if (net.ip[0] || net.ip[1] || net.ip[2] || net.ip[3])
  724. p = put_ip(p, net.ip);
  725. else
  726. p = sput(p, "not configured");
  727. if (seq(net.name, "wifi"))
  728. p = sput(p, wst.state >= SYS_WIFI_ST_LINK ?
  729. " link up" : " link down");
  730. } else if (wifi_present) {
  731. p = sput(p, "WiFi card present - scan to join a network");
  732. } else {
  733. p = sput(p, "Plug in Ethernet or USB tethering, then Refresh");
  734. }
  735. *p = '\0';
  736. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text_dim);
  737. body.y += 4;
  738. body.h -= 4;
  739. /* Devices. */
  740. r = ngui_cut_top(&body, 26);
  741. ngui_text(&ui, r.x, r.y + 3, "Network devices", ui.theme.text_dim);
  742. rb = r;
  743. rb.x = r.x + r.w - 78;
  744. rb.w = 78;
  745. rb.h = 22;
  746. if (ngui_button(&ui, ID_REFRESH, rb, "Refresh"))
  747. actions |= ACT_REFRESH;
  748. shown = 0;
  749. for (i = 0; i < ndev && shown < 6; i++) {
  750. if (devs[i].kind == SYS_NETDEV_WIFI)
  751. continue;
  752. p = line;
  753. p = sput(p, devs[i].name);
  754. p = sput(p, " ");
  755. p = sput(p, devs[i].chip);
  756. *p = '\0';
  757. r = ngui_cut_top(&body, ROW_H);
  758. if (ngui_menu_item(&ui, ID_DEV_BASE + i, r, line,
  759. devs[i].active ? "active" :
  760. (devs[i].link ? "link" : ""),
  761. devs[i].active))
  762. action_dev = i;
  763. shown++;
  764. }
  765. if (!shown) {
  766. r = ngui_cut_top(&body, ROW_H);
  767. ngui_text(&ui, r.x + 2, r.y + 3,
  768. wifi_present ?
  769. "No wired or USB device - use the WiFi list" :
  770. "None found - Refresh after plugging a device in",
  771. ui.theme.text_dim);
  772. }
  773. body.y += 6;
  774. body.h -= 6;
  775. /* WiFi networks. */
  776. r = ngui_cut_top(&body, 26);
  777. ngui_text(&ui, r.x, r.y + 3, "WiFi networks", ui.theme.text_dim);
  778. rb = r;
  779. rb.x = r.x + r.w - 96;
  780. rb.w = 96;
  781. rb.h = 22;
  782. if (nsaved > 0) {
  783. struct ngui_rect sb = rb;
  784. sb.x = rb.x - 68;
  785. sb.w = 64;
  786. if (ngui_button(&ui, ID_SAVED, sb, "Saved"))
  787. actions |= ACT_SAVED;
  788. }
  789. if (wifi_present) {
  790. if (wst.state >= SYS_WIFI_ST_ASSOC) {
  791. if (ngui_button(&ui, ID_DISCONNECT, rb, "Disconnect"))
  792. actions |= ACT_DISCONNECT;
  793. } else if (scanning) {
  794. ngui_text(&ui, rb.x + 10, rb.y + 3, "Scanning...",
  795. ui.theme.text_dim);
  796. } else {
  797. if (ngui_button(&ui, ID_SCAN, rb, "Scan"))
  798. actions |= ACT_SCAN;
  799. }
  800. }
  801. draw_wifi_list(body);
  802. }
  803. static void draw_pass(void)
  804. {
  805. struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
  806. struct ngui_rect r, b1, b2;
  807. char line[80], *p;
  808. int ev;
  809. r = ngui_cut_top(&body, 24);
  810. p = sput(line, "Join WiFi network \"");
  811. p = sput(p, sel_ssid);
  812. p = sput(p, "\"");
  813. *p = '\0';
  814. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  815. r = ngui_cut_top(&body, 22);
  816. ngui_text(&ui, r.x, r.y, "WPA2-PSK passphrase", ui.theme.text_dim);
  817. r = ngui_cut_top(&body, 30);
  818. ev = ngui_text_input(&ui, ID_PASS, r, &pass_in);
  819. if (ev & NGUI_INPUT_SUBMIT)
  820. actions |= ACT_CONNECT;
  821. if (ev & NGUI_INPUT_CANCEL)
  822. actions |= ACT_BACK;
  823. body.y += 6;
  824. body.h -= 6;
  825. r = ngui_cut_top(&body, 30);
  826. b1 = r;
  827. b1.w = 100;
  828. b2 = r;
  829. b2.x = r.x + 110;
  830. b2.w = 100;
  831. if (ngui_button(&ui, ID_PASS_OK, b1, "Connect"))
  832. actions |= ACT_CONNECT;
  833. if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel"))
  834. actions |= ACT_BACK;
  835. r = ngui_cut_top(&body, 22);
  836. ngui_text(&ui, r.x, r.y,
  837. "8-63 characters, or 64 hex digits for a raw PSK.",
  838. ui.theme.text_dim);
  839. draw_status();
  840. }
  841. static void draw_connect(void)
  842. {
  843. struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
  844. struct ngui_rect r, b;
  845. char line[96], *p;
  846. unsigned int start = 0, lines = 0, i;
  847. int y, line_y;
  848. r = ngui_cut_top(&body, 24);
  849. p = line;
  850. if (connect_pid > 0) {
  851. p = sput(p, "Connecting to \"");
  852. p = sput(p, sel_ssid);
  853. p = sput(p, "\"...");
  854. } else {
  855. p = sput(p, "WiFi connection \"");
  856. p = sput(p, sel_ssid);
  857. p = sput(p, "\"");
  858. }
  859. *p = '\0';
  860. ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  861. r = ngui_cut_top(&body, 22);
  862. if (status[0])
  863. ngui_text_utf8(&ui, r.x, r.y, status,
  864. status_error ? ui.theme.error : ui.theme.accent);
  865. body.y += 4;
  866. body.h -= 4;
  867. /* Last ~12 lines of the helper's terminal output. */
  868. for (i = conn_log_len; i > 0; i--) {
  869. if (conn_log[i - 1] == '\n' && ++lines > 12) {
  870. start = i;
  871. break;
  872. }
  873. }
  874. y = body.y;
  875. line_y = 0;
  876. for (i = start; i < conn_log_len && y + 16 <= body.y + body.h - 34; i++) {
  877. char c = conn_log[i];
  878. if (c == '\r')
  879. continue;
  880. if (c == '\n') {
  881. line[line_y] = '\0';
  882. ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
  883. y += 16;
  884. line_y = 0;
  885. continue;
  886. }
  887. if ((unsigned int)line_y + 1 < sizeof(line))
  888. line[line_y++] = c;
  889. }
  890. if (line_y) {
  891. line[line_y] = '\0';
  892. ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
  893. }
  894. /* Cancel at the bottom. */
  895. b.x = body.x;
  896. b.y = body.y + body.h - 28;
  897. b.w = 100;
  898. b.h = 24;
  899. if (ngui_button(&ui, ID_CONN_CANCEL, b,
  900. connect_pid > 0 ? "Cancel" : "Back"))
  901. actions |= ACT_BACK;
  902. }
  903. static void draw_saved(void)
  904. {
  905. struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
  906. struct ngui_rect r, b, row, bt;
  907. char line[96], *p;
  908. int i;
  909. r = ngui_cut_top(&body, 24);
  910. ngui_text(&ui, r.x, r.y, "Saved WiFi networks", ui.theme.text);
  911. body.y += 4;
  912. body.h -= 4;
  913. body.h -= 34; /* room for the Back button */
  914. if (!nsaved) {
  915. ngui_text(&ui, body.x + 2, body.y + 4,
  916. "No saved networks", ui.theme.text_dim);
  917. } else {
  918. saved_scroll.content_h = nsaved * 28;
  919. ngui_scroll_begin(&ui, body, &saved_scroll);
  920. ngui_scroll_wheel(&ui, body, &saved_scroll);
  921. ngui_scroll_keys(&ui, &saved_scroll);
  922. for (i = 0; i < nsaved; i++) {
  923. char ssid[33];
  924. row.x = body.x;
  925. row.y = body.y - saved_scroll.offset + i * 28;
  926. row.w = body.w - 16;
  927. row.h = 26;
  928. if (row.y + row.h <= body.y ||
  929. row.y >= body.y + body.h)
  930. continue;
  931. cfg_ssid_name(&saved[i], ssid);
  932. bt = row;
  933. bt.x = row.x + row.w - 64;
  934. bt.w = 60;
  935. bt.h = 22;
  936. row.w -= 70;
  937. p = sput(line, ssid);
  938. p = sput(p, saved[i].pass_len ? " WPA2" : " open");
  939. *p = '\0';
  940. ngui_text_ellipsis_utf8(&ui, row, row.x, row.y + 3,
  941. line, ui.theme.text);
  942. if (ngui_button(&ui, ID_SAVED_BASE + i, bt,
  943. "Forget")) {
  944. action_saved = i;
  945. actions |= ACT_FORGET;
  946. }
  947. }
  948. ngui_scroll_end(&ui, body, &saved_scroll);
  949. }
  950. b.x = body.x;
  951. b.y = body.y + body.h + 6;
  952. b.w = 100;
  953. b.h = 24;
  954. if (ngui_button(&ui, ID_SAVED_BACK, b, "Back"))
  955. actions |= ACT_BACK;
  956. }
  957. static void draw(void)
  958. {
  959. ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
  960. ui.theme.bg);
  961. if (view == VIEW_PASS)
  962. draw_pass();
  963. else if (view == VIEW_CONNECT)
  964. draw_connect();
  965. else if (view == VIEW_SAVED)
  966. draw_saved();
  967. else
  968. draw_main();
  969. if (view != VIEW_PASS && view != VIEW_CONNECT)
  970. draw_status();
  971. }
  972. static void paint_frame(void)
  973. {
  974. if (ngui_begin(&ui)) {
  975. draw();
  976. ngui_end(&ui);
  977. }
  978. actions = 0;
  979. action_dev = -1;
  980. action_bss = -1;
  981. action_saved = -1;
  982. }
  983. void _start(const char *arg)
  984. {
  985. struct win_info wi;
  986. unsigned int last_status = 0;
  987. (void)arg;
  988. if (u_win_create(460, 500, "Network", &wi) != 0 &&
  989. u_win_create(400, 420, "Network", &wi) != 0 &&
  990. u_win_create(340, 360, "Network", &wi) != 0) {
  991. u_exit(1);
  992. }
  993. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  994. ngui_font_attach(&ui);
  995. pass_in.secret = 1;
  996. pass_in.placeholder = "WPA2 passphrase";
  997. view = VIEW_MAIN;
  998. load_devices();
  999. load_status();
  1000. load_saved();
  1001. if (wifi_present && wst.state != SYS_WIFI_ST_DOWN &&
  1002. wst.state < SYS_WIFI_ST_ASSOC) {
  1003. start_scan();
  1004. paint_frame();
  1005. }
  1006. for (;;) {
  1007. unsigned int now = u_ticks_ms();
  1008. drain_tty();
  1009. poll_scan();
  1010. poll_connect();
  1011. if (now - last_status >= 1000) {
  1012. last_status = now;
  1013. load_status();
  1014. ngui_invalidate(&ui);
  1015. }
  1016. if (status_until && (int)(now - status_until) >= 0) {
  1017. status[0] = '\0';
  1018. status_until = 0;
  1019. ngui_invalidate(&ui);
  1020. }
  1021. if (ngui_begin(&ui)) {
  1022. draw();
  1023. ngui_end(&ui);
  1024. handle_actions();
  1025. }
  1026. /* The password field owns Esc while focused. */
  1027. if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
  1028. ngui_key(&ui);
  1029. if (view == VIEW_PASS) {
  1030. view = VIEW_MAIN;
  1031. set_status("", 0);
  1032. } else if (view == VIEW_CONNECT) {
  1033. cancel_connect();
  1034. } else if (view == VIEW_SAVED) {
  1035. view = VIEW_MAIN;
  1036. set_status("", 0);
  1037. } else if (ui.focus == 0) {
  1038. break;
  1039. }
  1040. }
  1041. u_sleep_ms(25);
  1042. }
  1043. ngui_font_detach(&ui);
  1044. u_win_close();
  1045. u_exit(0);
  1046. }