ucmd_netmenu.c 21 KB

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