ucmd_netmenu.c 22 KB

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