ucmd_netmenu.c 29 KB

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