ucmd_netmenu.c 29 KB

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