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. /* Keep the raw UTF-8 bytes; ngui's UTF-8 renderer handles them and the
  123. * connection path needs the exact SSID. */
  124. for (i = 0; i < n; i++)
  125. out[i] = (char)b->ssid[i];
  126. out[n] = '\0';
  127. if (!n) {
  128. out[0] = '(';
  129. out[1] = 'h'; out[2] = 'i'; out[3] = 'd';
  130. out[4] = 'd'; out[5] = 'e'; out[6] = 'n';
  131. out[7] = ')';
  132. out[8] = '\0';
  133. }
  134. }
  135. static const char *security_name(unsigned int s)
  136. {
  137. return s == SYS_WIFI_SEC_OPEN ? "open" :
  138. s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
  139. }
  140. static int rssi_bars(int rssi)
  141. {
  142. if (rssi >= -55)
  143. return 4;
  144. if (rssi >= -65)
  145. return 3;
  146. if (rssi >= -75)
  147. return 2;
  148. return 1;
  149. }
  150. static int bss_is_active(const struct sys_wifi_bss *b)
  151. {
  152. unsigned int i;
  153. if (wst.state < SYS_WIFI_ST_ASSOC)
  154. return 0;
  155. for (i = 0; i < 6; i++)
  156. if (wst.bssid[i] != b->bssid[i])
  157. return 0;
  158. return 1;
  159. }
  160. static void set_status(const char *msg, int err)
  161. {
  162. unsigned int i;
  163. for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
  164. status[i] = msg[i];
  165. status[i] = '\0';
  166. status_error = err;
  167. status_until = msg && msg[0] ? u_ticks_ms() + 6000 : 0;
  168. ngui_invalidate(&ui);
  169. }
  170. static int dev_active(const struct sys_netdev *d)
  171. {
  172. return net_present && seq(net.name, d->name);
  173. }
  174. static void load_status(void)
  175. {
  176. int i;
  177. net_present = (u_net_info(&net) == 1 && net.present);
  178. if (!wifi_checked || wifi_present) {
  179. if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
  180. wifi_checked = 1;
  181. wifi_present = wst.present != 0;
  182. } else {
  183. /* Do not repeatedly probe a machine without a card. */
  184. wifi_checked = 1;
  185. wifi_present = 0;
  186. }
  187. }
  188. for (i = 0; i < ndev; i++) {
  189. devs[i].active = dev_active(&devs[i]) ? 1u : 0u;
  190. if (devs[i].active)
  191. devs[i].link = net.link;
  192. }
  193. }
  194. static void load_devices(void)
  195. {
  196. int n = u_net_devlist(devs, MAX_DEVS);
  197. ndev = n > 0 ? n : 0;
  198. load_status();
  199. }
  200. static void drain_tty(void)
  201. {
  202. char buf[256];
  203. int n;
  204. if (connect_tty <= 0)
  205. return;
  206. while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
  207. int i;
  208. for (i = 0; i < n; i++) {
  209. if (conn_log_len + 1 >= sizeof(conn_log)) {
  210. unsigned int half = sizeof(conn_log) / 2;
  211. unsigned int j;
  212. for (j = 0; j < half; j++)
  213. conn_log[j] = conn_log[conn_log_len -
  214. half + j];
  215. conn_log_len = half;
  216. }
  217. conn_log[conn_log_len++] = buf[i];
  218. }
  219. conn_log[conn_log_len] = '\0';
  220. ngui_invalidate(&ui);
  221. }
  222. }
  223. static void sort_bss(void)
  224. {
  225. int i, j;
  226. for (i = 1; i < nbss; i++)
  227. for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
  228. struct sys_wifi_bss t = bss[j];
  229. bss[j] = bss[j - 1];
  230. bss[j - 1] = t;
  231. }
  232. }
  233. static int same_ssid(const struct sys_wifi_bss *a,
  234. const struct sys_wifi_bss *b)
  235. {
  236. unsigned int i;
  237. if (a->ssid_len == 0 || a->ssid_len != b->ssid_len)
  238. return 0;
  239. for (i = 0; i < a->ssid_len; i++)
  240. if (a->ssid[i] != b->ssid[i])
  241. return 0;
  242. return 1;
  243. }
  244. /* Mesh/repeater setups often advertise one SSID from several BSSIDs. The
  245. * list is RSSI-sorted, so keep the strongest entry of each SSID/security
  246. * pair (or the active BSSID when one of them is the current connection).
  247. * Hidden-SSID entries are dropped: the menu cannot join them anyway. */
  248. static void filter_bss(void)
  249. {
  250. int i, j, out = 0;
  251. for (i = 0; i < nbss; i++) {
  252. int dup = -1;
  253. if (bss[i].ssid_len == 0)
  254. continue;
  255. for (j = 0; j < out; j++)
  256. if (bss[i].security == bss[j].security &&
  257. same_ssid(&bss[i], &bss[j])) {
  258. dup = j;
  259. break;
  260. }
  261. if (dup < 0) {
  262. bss[out++] = bss[i];
  263. } else if (bss_is_active(&bss[i]) &&
  264. !bss_is_active(&bss[dup])) {
  265. bss[dup] = bss[i];
  266. }
  267. }
  268. nbss = out;
  269. }
  270. static void start_scan(void)
  271. {
  272. int r;
  273. if (scanning || !wifi_present)
  274. return;
  275. set_status("Scanning for WiFi networks...", 0);
  276. paint_frame();
  277. /* SYS_WIFI_SCAN loads the firmware itself if it is still down. */
  278. r = u_wifi(SYS_WIFI_SCAN, 0, 0);
  279. if (r < 0) {
  280. char m[48], *p;
  281. u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
  282. p = sput(m, "WiFi: ");
  283. p = sput(p, wst.detail[0] ? wst.detail : "scan failed");
  284. *p = '\0';
  285. set_status(m, 1);
  286. return;
  287. }
  288. scanning = 1;
  289. scan_start = u_ticks_ms();
  290. nbss = 0;
  291. wifi_scroll.offset = 0;
  292. ngui_invalidate(&ui);
  293. }
  294. static void poll_scan(void)
  295. {
  296. int n;
  297. if (!scanning)
  298. return;
  299. n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
  300. if (n == -2) {
  301. if (u_ticks_ms() - scan_start > 15000) {
  302. scanning = 0;
  303. set_status("WiFi scan timed out", 1);
  304. }
  305. return;
  306. }
  307. scanning = 0;
  308. if (n < 0) {
  309. set_status("WiFi scan failed", 1);
  310. return;
  311. }
  312. nbss = n;
  313. sort_bss();
  314. filter_bss();
  315. if (nbss == 0)
  316. set_status("No WiFi networks found", 1);
  317. else
  318. set_status("WiFi scan complete", 0);
  319. ngui_invalidate(&ui);
  320. }
  321. static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
  322. {
  323. struct wifi_cfg cfg;
  324. unsigned int i, n = slen(pass);
  325. int r;
  326. for (i = 0; i < sizeof(cfg); i++)
  327. ((unsigned char *)&cfg)[i] = 0;
  328. cfg.magic = WIFI_CFG_MAGIC;
  329. cfg.ssid_len = b->ssid_len;
  330. cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
  331. for (i = 0; i < 6; i++)
  332. cfg.bssid[i] = b->bssid[i];
  333. for (i = 0; i < b->ssid_len && i < 32; i++)
  334. cfg.ssid[i] = b->ssid[i];
  335. for (i = 0; i < cfg.pass_len; i++)
  336. cfg.pass[i] = pass[i];
  337. r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
  338. (int)&cfg, (int)sizeof(cfg));
  339. for (i = 0; i < sizeof(cfg); i++)
  340. ((unsigned char *)&cfg)[i] = 0;
  341. return r;
  342. }
  343. static void start_connect(const char *pass)
  344. {
  345. struct sys_wifi_bss *b;
  346. char msg[80], *p;
  347. int pid;
  348. if (sel_bss < 0 || sel_bss >= nbss)
  349. return;
  350. if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
  351. set_status("A WiFi connection is already in progress", 1);
  352. return;
  353. }
  354. b = &bss[sel_bss];
  355. ssid_name(b, sel_ssid);
  356. if (write_wifi_cfg(b, pass) != 0) {
  357. set_status("Cannot stage the WiFi request in /share", 1);
  358. return;
  359. }
  360. if (connect_tty <= 0) {
  361. connect_tty = u_tty_create();
  362. if (connect_tty < 0)
  363. connect_tty = 0;
  364. }
  365. conn_log_len = 0;
  366. conn_log[0] = '\0';
  367. connect_start = u_ticks_ms();
  368. pid = u_spawn("/ramfs/bin/wifi", "connectfile /share/netmenu.wifi",
  369. connect_tty, "/");
  370. if (pid < 0) {
  371. u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
  372. set_status("Cannot start the WiFi helper", 1);
  373. return;
  374. }
  375. connect_pid = pid;
  376. view = VIEW_CONNECT;
  377. p = sput(msg, "Connecting to \"");
  378. p = sput(p, sel_ssid);
  379. p = sput(p, "\"...");
  380. *p = '\0';
  381. set_status(msg, 0);
  382. }
  383. static void cancel_connect(void)
  384. {
  385. if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
  386. u_kill((unsigned int)connect_pid);
  387. connect_pid = -1;
  388. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  389. view = VIEW_MAIN;
  390. set_status("WiFi connection cancelled", 0);
  391. load_status();
  392. }
  393. static void poll_connect(void)
  394. {
  395. int i;
  396. if (connect_pid <= 0)
  397. return;
  398. if (!u_alive((unsigned int)connect_pid)) {
  399. connect_pid = -1;
  400. load_status();
  401. if (wst.state >= SYS_WIFI_ST_LINK) {
  402. int have_ip = 0;
  403. for (i = 0; i < 4; i++)
  404. if (net.ip[i])
  405. have_ip = 1;
  406. view = VIEW_MAIN;
  407. if (net_present && seq(net.name, "wifi")) {
  408. if (have_ip)
  409. set_status("WiFi connected and DHCP is up", 0);
  410. else
  411. set_status("WiFi connected, but DHCP got no lease", 1);
  412. } else {
  413. set_status("WiFi associated, but the netdev is busy", 1);
  414. }
  415. } else {
  416. /* Keep the log on screen so the failure reason is visible;
  417. * the Cancel button becomes Back. */
  418. set_status("WiFi connection failed", 1);
  419. }
  420. ngui_invalidate(&ui);
  421. return;
  422. }
  423. if (u_ticks_ms() - connect_start > 45000) {
  424. u_kill((unsigned int)connect_pid);
  425. connect_pid = -1;
  426. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  427. set_status("WiFi connection timed out", 1);
  428. }
  429. }
  430. static void select_device(int i)
  431. {
  432. char msg[64], *p;
  433. if (i < 0 || i >= ndev)
  434. return;
  435. p = sput(msg, "Selecting ");
  436. p = sput(p, devs[i].name);
  437. *p = '\0';
  438. set_status(msg, 0);
  439. paint_frame();
  440. if (u_net_select(devs[i].name) != 0) {
  441. set_status("Cannot select that netdev (busy or unavailable)", 1);
  442. return;
  443. }
  444. set_status("Running DHCP...", 0);
  445. paint_frame();
  446. if (u_dhcp_run(6000) == 0)
  447. set_status("Device selected; DHCP lease acquired", 0);
  448. else
  449. set_status("Device selected, but DHCP got no lease", 1);
  450. load_devices();
  451. ngui_invalidate(&ui);
  452. }
  453. static void select_bss(int i)
  454. {
  455. if (i < 0 || i >= nbss)
  456. return;
  457. if (bss[i].ssid_len == 0) {
  458. set_status("Hidden network: use `wifi connect` from a terminal", 1);
  459. return;
  460. }
  461. sel_bss = i;
  462. ssid_name(&bss[i], sel_ssid);
  463. if (bss[i].security == SYS_WIFI_SEC_OTHER) {
  464. set_status("Unsupported security (only open and WPA2-PSK)", 1);
  465. return;
  466. }
  467. if (bss[i].security == SYS_WIFI_SEC_PSK) {
  468. ngui_input_set(&pass_in, "");
  469. pass_in.secret = 1;
  470. pass_in.placeholder = "WPA2 passphrase";
  471. view = VIEW_PASS;
  472. set_status("", 0);
  473. ngui_set_focus(&ui, ID_PASS);
  474. ngui_invalidate(&ui);
  475. return;
  476. }
  477. start_connect("");
  478. }
  479. static void handle_actions(void)
  480. {
  481. unsigned int a = actions;
  482. int di = action_dev, bi = action_bss;
  483. actions = 0;
  484. action_dev = -1;
  485. action_bss = -1;
  486. if (a & ACT_BACK) {
  487. if (view == VIEW_CONNECT)
  488. cancel_connect();
  489. else {
  490. view = VIEW_MAIN;
  491. set_status("", 0);
  492. }
  493. return;
  494. }
  495. if (a & ACT_CONNECT) {
  496. char pass[65];
  497. unsigned int i, n;
  498. ngui_input_value(&pass_in, pass, sizeof(pass));
  499. n = slen(pass);
  500. if (n == 0) {
  501. set_status("Enter the WPA2 passphrase", 1);
  502. } else if (n < 8 || n > 63) {
  503. /* A 64-character hex string is a raw PSK. */
  504. int hex = n == 64;
  505. if (hex) {
  506. unsigned int j;
  507. for (j = 0; j < n; j++) {
  508. char c = pass[j];
  509. if (!((c >= '0' && c <= '9') ||
  510. (c >= 'a' && c <= 'f') ||
  511. (c >= 'A' && c <= 'F'))) {
  512. hex = 0;
  513. break;
  514. }
  515. }
  516. }
  517. if (hex)
  518. start_connect(pass);
  519. else
  520. set_status("Use 8-63 characters or 64 hex digits", 1);
  521. } else {
  522. start_connect(pass);
  523. }
  524. for (i = 0; i < sizeof(pass); i++)
  525. ((volatile char *)pass)[i] = 0;
  526. return;
  527. }
  528. if (a & ACT_REFRESH) {
  529. set_status("Refreshing network devices...", 0);
  530. paint_frame();
  531. load_devices();
  532. set_status("Device list refreshed", 0);
  533. ngui_invalidate(&ui);
  534. } else if (a & ACT_SCAN) {
  535. start_scan();
  536. } else if (a & ACT_DISCONNECT) {
  537. u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
  538. load_status();
  539. set_status("WiFi disconnected", 0);
  540. ngui_invalidate(&ui);
  541. } else if (di >= 0) {
  542. select_device(di);
  543. } else if (bi >= 0) {
  544. select_bss(bi);
  545. }
  546. }
  547. static void draw_bars(int x, int y, int bars, unsigned short color)
  548. {
  549. int i;
  550. for (i = 0; i < 4; i++) {
  551. int h = 3 + i * 3;
  552. struct ngui_rect r;
  553. r.x = x + i * 5;
  554. r.y = y + 12 - h;
  555. r.w = 3;
  556. r.h = h;
  557. ngui_fill_rect(&ui, r, i < bars ? color : ui.theme.border);
  558. }
  559. }
  560. static void draw_status(void)
  561. {
  562. if (status[0] && status_until &&
  563. (int)(u_ticks_ms() - status_until) < 0)
  564. ngui_text_utf8(&ui, 10, ui.h - 22, status,
  565. status_error ? ui.theme.error : ui.theme.accent);
  566. }
  567. static void draw_wifi_list(struct ngui_rect area)
  568. {
  569. char label[40], shortcut[20];
  570. int i;
  571. if (!wifi_present) {
  572. ngui_text(&ui, area.x + 2, area.y + 4,
  573. "No WiFi card detected", ui.theme.text_dim);
  574. return;
  575. }
  576. if (scanning) {
  577. ngui_text(&ui, area.x + 2, area.y + 4,
  578. "Scanning...", ui.theme.text_dim);
  579. return;
  580. }
  581. if (nbss == 0) {
  582. ngui_text(&ui, area.x + 2, area.y + 4,
  583. wst.state >= SYS_WIFI_ST_ASSOC ?
  584. "Connected - Disconnect to scan for other networks" :
  585. "Press Scan to list WiFi networks",
  586. ui.theme.text_dim);
  587. return;
  588. }
  589. wifi_scroll.content_h = nbss * ROW_H;
  590. ngui_scroll_begin(&ui, area, &wifi_scroll);
  591. ngui_scroll_wheel(&ui, area, &wifi_scroll);
  592. ngui_scroll_keys(&ui, &wifi_scroll);
  593. for (i = 0; i < nbss; i++) {
  594. struct ngui_rect rr;
  595. struct sys_wifi_bss *b = &bss[i];
  596. char ssid[33];
  597. int sw, bx, clicked;
  598. unsigned short signal;
  599. rr.x = area.x;
  600. rr.y = area.y - wifi_scroll.offset + i * ROW_H;
  601. rr.w = area.w - 16;
  602. rr.h = ROW_H - 2;
  603. if (rr.y + rr.h <= area.y || rr.y >= area.y + area.h)
  604. continue;
  605. ssid_name(b, ssid);
  606. sput(label, ssid);
  607. sput(shortcut, security_name(b->security));
  608. clicked = ngui_menu_item_utf8(&ui, ID_BSS_BASE + i, rr, label,
  609. shortcut, bss_is_active(b));
  610. sw = (int)ngui_text_width_ui(&ui, shortcut);
  611. bx = rr.x + rr.w - 8 - sw - 30;
  612. if (b->security == SYS_WIFI_SEC_OTHER)
  613. signal = ui.theme.error;
  614. else
  615. signal = rssi_bars(b->rssi) >= 3 ? ui.theme.accent :
  616. ui.theme.text_dim;
  617. draw_bars(bx, rr.y + 3, rssi_bars(b->rssi), signal);
  618. if (clicked) {
  619. action_bss = i;
  620. }
  621. }
  622. ngui_scroll_end(&ui, area, &wifi_scroll);
  623. }
  624. static void draw_main(void)
  625. {
  626. struct ngui_rect body = { 10, 8, ui.w - 20, ui.h - 34 };
  627. struct ngui_rect r, rb;
  628. char line[128], *p;
  629. int i, shown;
  630. /* Active connection summary. */
  631. r = ngui_cut_top(&body, 20);
  632. p = line;
  633. if (!net_present) {
  634. p = sput(p, "No active network device");
  635. } else {
  636. p = sput(p, "Active: ");
  637. p = sput(p, net.name);
  638. p = sput(p, " (");
  639. p = sput(p, net.chip);
  640. p = sput(p, ")");
  641. if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
  642. p = sput(p, " ");
  643. for (i = 0; i < wst.ssid_len && i < 32; i++)
  644. *p++ = (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_utf8(&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_utf8(&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_utf8(&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_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
  787. r = ngui_cut_top(&body, 22);
  788. if (status[0])
  789. ngui_text_utf8(&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_utf8(&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;
  815. }
  816. if (line_y) {
  817. line[line_y] = '\0';
  818. ngui_text_utf8(&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. }