| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- /* netmenu: the top-bar network menu.
- *
- * Lists the NICs the kernel reports as present (Ethernet, USB Ethernet,
- * RNDIS tethering) and the WiFi networks from a scan. Selecting a wired or
- * USB device makes it the active netdev and runs DHCP; selecting a WiFi
- * network joins it with the passphrase saved in /data/WIFI.CFG when one is
- * known, otherwise it asks for the WPA2 passphrase, then starts the existing
- * `wifi` supplicant with a staged /share/netmenu.wifi request (the SYS_SPAWN
- * argument limit is too small for an SSID plus a long passphrase). A Saved
- * view lists the known networks and forgets individual entries. The
- * supplicant itself runs DHCP once the data port opens.
- *
- * A small ngui canvas window; the WM places it under the network icon and
- * opens only one instance at a time. */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #include "wifi_store.h"
- #define MAX_DEVS 8
- #define MAX_BSS 32
- #define ROW_H 24
- #define LOG_MAX 2048
- #define ID_REFRESH 1
- #define ID_SCAN 2
- #define ID_DISCONNECT 3
- #define ID_PASS_OK 4
- #define ID_PASS_CANCEL 5
- #define ID_CONN_CANCEL 6
- #define ID_PASS 7
- #define ID_SAVED 8
- #define ID_SAVED_BACK 9
- #define ID_DEV_BASE 100
- #define ID_BSS_BASE 200
- #define ID_SAVED_BASE 300
- #define ACT_REFRESH (1u << 0)
- #define ACT_SCAN (1u << 1)
- #define ACT_DISCONNECT (1u << 2)
- #define ACT_BACK (1u << 3)
- #define ACT_CONNECT (1u << 4)
- #define ACT_SAVED (1u << 5)
- #define ACT_FORGET (1u << 6)
- enum { VIEW_MAIN, VIEW_PASS, VIEW_CONNECT, VIEW_SAVED };
- static struct ngui ui;
- static struct ngui_input pass_in;
- static struct ngui_scroll wifi_scroll;
- static struct ngui_scroll saved_scroll;
- static struct sys_netdev devs[MAX_DEVS];
- static int ndev;
- static struct sys_wifi_bss bss[MAX_BSS];
- static int nbss;
- static struct wifi_cfg saved[WIFI_CFG_MAX];
- static int nsaved;
- static struct sys_wifi_status wst;
- static struct sys_netinfo net;
- static int net_present;
- static int wifi_checked;
- static int wifi_present;
- static int view;
- static int sel_bss = -1;
- static char sel_ssid[33];
- static int scanning;
- static unsigned int scan_start;
- static int connect_pid = -1;
- static int connect_tty;
- static unsigned int connect_start;
- static char conn_log[LOG_MAX];
- static unsigned int conn_log_len;
- static char status[160];
- static int status_error;
- static unsigned int status_until;
- static unsigned int actions;
- static int action_dev = -1;
- static int action_bss = -1;
- static int action_saved = -1;
- static void draw(void);
- static void paint_frame(void);
- static unsigned int slen(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static int seq(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static char *sput(char *p, const char *s)
- {
- while (s && *s)
- *p++ = *s++;
- *p = '\0';
- return p;
- }
- static char *put_dec(char *p, unsigned int v)
- {
- char b[10];
- unsigned int n = 0;
- do {
- b[n++] = (char)('0' + v % 10);
- v /= 10;
- } while (v);
- while (n)
- *p++ = b[--n];
- return p;
- }
- static char *put_signed(char *p, int v)
- {
- if (v < 0) {
- *p++ = '-';
- v = -v;
- }
- return put_dec(p, (unsigned int)v);
- }
- static char *put_ip(char *p, const unsigned char ip[4])
- {
- p = put_dec(p, ip[0]); *p++ = '.';
- p = put_dec(p, ip[1]); *p++ = '.';
- p = put_dec(p, ip[2]); *p++ = '.';
- p = put_dec(p, ip[3]);
- return p;
- }
- static void ssid_name(const struct sys_wifi_bss *b, char out[33])
- {
- unsigned int i, n = b->ssid_len;
- if (n > 32)
- n = 32;
- /* Keep the raw UTF-8 bytes; ngui's UTF-8 renderer handles them and the
- * connection path needs the exact SSID. */
- for (i = 0; i < n; i++)
- out[i] = (char)b->ssid[i];
- out[n] = '\0';
- if (!n) {
- out[0] = '(';
- out[1] = 'h'; out[2] = 'i'; out[3] = 'd';
- out[4] = 'd'; out[5] = 'e'; out[6] = 'n';
- out[7] = ')';
- out[8] = '\0';
- }
- }
- static void cfg_ssid_name(const struct wifi_cfg *c, char out[33])
- {
- unsigned int i, n = c->ssid_len;
- if (n > 32)
- n = 32;
- for (i = 0; i < n; i++)
- out[i] = (char)c->ssid[i];
- out[n] = '\0';
- }
- static void load_saved(void)
- {
- int n = wifi_store_load(saved, WIFI_CFG_MAX);
- nsaved = n > 0 ? n : 0;
- }
- static int bss_saved(const struct sys_wifi_bss *b)
- {
- return wifi_store_find(saved, nsaved, b->ssid, b->ssid_len);
- }
- static const char *security_name(unsigned int s)
- {
- return s == SYS_WIFI_SEC_OPEN ? "open" :
- s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
- }
- static int rssi_bars(int rssi)
- {
- if (rssi >= -55)
- return 4;
- if (rssi >= -65)
- return 3;
- if (rssi >= -75)
- return 2;
- return 1;
- }
- static int bss_is_active(const struct sys_wifi_bss *b)
- {
- unsigned int i;
- if (wst.state < SYS_WIFI_ST_ASSOC)
- return 0;
- for (i = 0; i < 6; i++)
- if (wst.bssid[i] != b->bssid[i])
- return 0;
- return 1;
- }
- static void set_status(const char *msg, int err)
- {
- unsigned int i;
- for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
- status[i] = msg[i];
- status[i] = '\0';
- status_error = err;
- status_until = msg && msg[0] ? u_ticks_ms() + 6000 : 0;
- ngui_invalidate(&ui);
- }
- static int dev_active(const struct sys_netdev *d)
- {
- return net_present && seq(net.name, d->name);
- }
- static void load_status(void)
- {
- int i;
- net_present = (u_net_info(&net) == 1 && net.present);
- if (!wifi_checked || wifi_present) {
- if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
- wifi_checked = 1;
- wifi_present = wst.present != 0;
- } else {
- /* Do not repeatedly probe a machine without a card. */
- wifi_checked = 1;
- wifi_present = 0;
- }
- }
- for (i = 0; i < ndev; i++) {
- devs[i].active = dev_active(&devs[i]) ? 1u : 0u;
- if (devs[i].active)
- devs[i].link = net.link;
- }
- }
- static void load_devices(void)
- {
- int n = u_net_devlist(devs, MAX_DEVS);
- ndev = n > 0 ? n : 0;
- load_status();
- }
- static void drain_tty(void)
- {
- char buf[256];
- int n;
- if (connect_tty <= 0)
- return;
- while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
- int i;
- for (i = 0; i < n; i++) {
- if (conn_log_len + 1 >= sizeof(conn_log)) {
- unsigned int half = sizeof(conn_log) / 2;
- unsigned int j;
- for (j = 0; j < half; j++)
- conn_log[j] = conn_log[conn_log_len -
- half + j];
- conn_log_len = half;
- }
- conn_log[conn_log_len++] = buf[i];
- }
- conn_log[conn_log_len] = '\0';
- ngui_invalidate(&ui);
- }
- }
- static void sort_bss(void)
- {
- int i, j;
- for (i = 1; i < nbss; i++)
- for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
- struct sys_wifi_bss t = bss[j];
- bss[j] = bss[j - 1];
- bss[j - 1] = t;
- }
- }
- static int same_ssid(const struct sys_wifi_bss *a,
- const struct sys_wifi_bss *b)
- {
- unsigned int i;
- if (a->ssid_len == 0 || a->ssid_len != b->ssid_len)
- return 0;
- for (i = 0; i < a->ssid_len; i++)
- if (a->ssid[i] != b->ssid[i])
- return 0;
- return 1;
- }
- /* Mesh/repeater setups often advertise one SSID from several BSSIDs. The
- * list is RSSI-sorted, so keep the strongest entry of each SSID/security
- * pair (or the active BSSID when one of them is the current connection).
- * Hidden-SSID entries are dropped: the menu cannot join them anyway. */
- static void filter_bss(void)
- {
- int i, j, out = 0;
- for (i = 0; i < nbss; i++) {
- int dup = -1;
- if (bss[i].ssid_len == 0)
- continue;
- for (j = 0; j < out; j++)
- if (bss[i].security == bss[j].security &&
- same_ssid(&bss[i], &bss[j])) {
- dup = j;
- break;
- }
- if (dup < 0) {
- bss[out++] = bss[i];
- } else if (bss_is_active(&bss[i]) &&
- !bss_is_active(&bss[dup])) {
- bss[dup] = bss[i];
- }
- }
- nbss = out;
- }
- static void start_scan(void)
- {
- int r;
- if (scanning || !wifi_present)
- return;
- set_status("Scanning for WiFi networks...", 0);
- paint_frame();
- /* SYS_WIFI_SCAN loads the firmware itself if it is still down. */
- r = u_wifi(SYS_WIFI_SCAN, 0, 0);
- if (r < 0) {
- char m[48], *p;
- u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
- p = sput(m, "WiFi: ");
- p = sput(p, wst.detail[0] ? wst.detail : "scan failed");
- *p = '\0';
- set_status(m, 1);
- return;
- }
- scanning = 1;
- scan_start = u_ticks_ms();
- nbss = 0;
- wifi_scroll.offset = 0;
- ngui_invalidate(&ui);
- }
- static void poll_scan(void)
- {
- int n;
- if (!scanning)
- return;
- n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
- if (n == -2) {
- if (u_ticks_ms() - scan_start > 15000) {
- scanning = 0;
- set_status("WiFi scan timed out", 1);
- }
- return;
- }
- scanning = 0;
- if (n < 0) {
- set_status("WiFi scan failed", 1);
- return;
- }
- nbss = n;
- sort_bss();
- filter_bss();
- if (nbss == 0)
- set_status("No WiFi networks found", 1);
- else
- set_status("WiFi scan complete", 0);
- ngui_invalidate(&ui);
- }
- static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
- {
- struct wifi_cfg cfg;
- unsigned int i, n = slen(pass);
- int r;
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- cfg.magic = WIFI_CFG_MAGIC;
- cfg.ssid_len = b->ssid_len;
- cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
- for (i = 0; i < 6; i++)
- cfg.bssid[i] = b->bssid[i];
- for (i = 0; i < b->ssid_len && i < 32; i++)
- cfg.ssid[i] = b->ssid[i];
- for (i = 0; i < cfg.pass_len; i++)
- cfg.pass[i] = pass[i];
- r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
- (int)&cfg, (int)sizeof(cfg));
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- return r;
- }
- static void start_connect(const char *pass)
- {
- struct sys_wifi_bss *b;
- char msg[80], *p;
- int pid;
- if (sel_bss < 0 || sel_bss >= nbss)
- return;
- if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
- set_status("A WiFi connection is already in progress", 1);
- return;
- }
- b = &bss[sel_bss];
- ssid_name(b, sel_ssid);
- if (write_wifi_cfg(b, pass) != 0) {
- set_status("Cannot stage the WiFi request in /share", 1);
- return;
- }
- if (connect_tty <= 0) {
- connect_tty = u_tty_create();
- if (connect_tty < 0)
- connect_tty = 0;
- }
- conn_log_len = 0;
- conn_log[0] = '\0';
- connect_start = u_ticks_ms();
- pid = u_spawn("/ramfs/bin/wifi", "connectfile /share/netmenu.wifi",
- connect_tty, "/");
- if (pid < 0) {
- u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
- set_status("Cannot start the WiFi helper", 1);
- return;
- }
- connect_pid = pid;
- view = VIEW_CONNECT;
- p = sput(msg, "Connecting to \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"...");
- *p = '\0';
- set_status(msg, 0);
- }
- static void cancel_connect(void)
- {
- if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
- u_kill((unsigned int)connect_pid);
- connect_pid = -1;
- u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
- view = VIEW_MAIN;
- set_status("WiFi connection cancelled", 0);
- load_status();
- }
- static void poll_connect(void)
- {
- int i;
- if (connect_pid <= 0)
- return;
- if (!u_alive((unsigned int)connect_pid)) {
- connect_pid = -1;
- load_status();
- load_saved();
- if (wst.state >= SYS_WIFI_ST_LINK) {
- int have_ip = 0;
- for (i = 0; i < 4; i++)
- if (net.ip[i])
- have_ip = 1;
- view = VIEW_MAIN;
- if (net_present && seq(net.name, "wifi")) {
- if (have_ip)
- set_status("WiFi connected and DHCP is up", 0);
- else
- set_status("WiFi connected, but DHCP got no lease", 1);
- } else {
- set_status("WiFi associated, but the netdev is busy", 1);
- }
- } else {
- /* Keep the log on screen so the failure reason is visible;
- * the Cancel button becomes Back. */
- set_status("WiFi connection failed", 1);
- }
- ngui_invalidate(&ui);
- return;
- }
- if (u_ticks_ms() - connect_start > 45000) {
- u_kill((unsigned int)connect_pid);
- connect_pid = -1;
- u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
- set_status("WiFi connection timed out", 1);
- }
- }
- static void select_device(int i)
- {
- char msg[64], *p;
- if (i < 0 || i >= ndev)
- return;
- p = sput(msg, "Selecting ");
- p = sput(p, devs[i].name);
- *p = '\0';
- set_status(msg, 0);
- paint_frame();
- if (u_net_select(devs[i].name) != 0) {
- set_status("Cannot select that netdev (busy or unavailable)", 1);
- return;
- }
- set_status("Running DHCP...", 0);
- paint_frame();
- if (u_dhcp_run(6000) == 0)
- set_status("Device selected; DHCP lease acquired", 0);
- else
- set_status("Device selected, but DHCP got no lease", 1);
- load_devices();
- ngui_invalidate(&ui);
- }
- static void select_bss(int i)
- {
- if (i < 0 || i >= nbss)
- return;
- if (bss[i].ssid_len == 0) {
- set_status("Hidden network: use `wifi connect` from a terminal", 1);
- return;
- }
- sel_bss = i;
- ssid_name(&bss[i], sel_ssid);
- if (bss[i].security == SYS_WIFI_SEC_OTHER) {
- set_status("Unsupported security (only open and WPA2-PSK)", 1);
- return;
- }
- if (bss[i].security == SYS_WIFI_SEC_PSK) {
- int si = bss_saved(&bss[i]);
- if (si >= 0 && saved[si].pass_len > 0) {
- char pass[65];
- unsigned int j;
- for (j = 0; j < saved[si].pass_len; j++)
- pass[j] = saved[si].pass[j];
- pass[saved[si].pass_len] = '\0';
- start_connect(pass);
- for (j = 0; j < sizeof(pass); j++)
- ((volatile char *)pass)[j] = 0;
- return;
- }
- ngui_input_set(&pass_in, "");
- pass_in.secret = 1;
- pass_in.placeholder = "WPA2 passphrase";
- view = VIEW_PASS;
- set_status("", 0);
- ngui_set_focus(&ui, ID_PASS);
- ngui_invalidate(&ui);
- return;
- }
- start_connect("");
- }
- static void handle_actions(void)
- {
- unsigned int a = actions;
- int di = action_dev, bi = action_bss, fi = action_saved;
- actions = 0;
- action_dev = -1;
- action_bss = -1;
- action_saved = -1;
- if (a & ACT_BACK) {
- if (view == VIEW_CONNECT)
- cancel_connect();
- else {
- view = VIEW_MAIN;
- set_status("", 0);
- }
- return;
- }
- if (a & ACT_SAVED) {
- load_saved();
- saved_scroll.offset = 0;
- view = VIEW_SAVED;
- set_status("", 0);
- ngui_invalidate(&ui);
- return;
- }
- if (a & ACT_FORGET) {
- if (fi >= 0 && fi < nsaved) {
- if (wifi_store_forget(saved[fi].ssid,
- saved[fi].ssid_len) > 0) {
- load_saved();
- saved_scroll.offset = 0;
- set_status("Saved network removed", 0);
- } else {
- set_status("Could not remove the saved network", 1);
- }
- }
- ngui_invalidate(&ui);
- return;
- }
- if (a & ACT_CONNECT) {
- char pass[65];
- unsigned int i, n;
- ngui_input_value(&pass_in, pass, sizeof(pass));
- n = slen(pass);
- if (n == 0) {
- set_status("Enter the WPA2 passphrase", 1);
- } else if (n < 8 || n > 63) {
- /* A 64-character hex string is a raw PSK. */
- int hex = n == 64;
- if (hex) {
- unsigned int j;
- for (j = 0; j < n; j++) {
- char c = pass[j];
- if (!((c >= '0' && c <= '9') ||
- (c >= 'a' && c <= 'f') ||
- (c >= 'A' && c <= 'F'))) {
- hex = 0;
- break;
- }
- }
- }
- if (hex)
- start_connect(pass);
- else
- set_status("Use 8-63 characters or 64 hex digits", 1);
- } else {
- start_connect(pass);
- }
- for (i = 0; i < sizeof(pass); i++)
- ((volatile char *)pass)[i] = 0;
- return;
- }
- if (a & ACT_REFRESH) {
- set_status("Refreshing network devices...", 0);
- paint_frame();
- load_devices();
- set_status("Device list refreshed", 0);
- ngui_invalidate(&ui);
- } else if (a & ACT_SCAN) {
- start_scan();
- } else if (a & ACT_DISCONNECT) {
- u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
- load_status();
- set_status("WiFi disconnected", 0);
- ngui_invalidate(&ui);
- } else if (di >= 0) {
- select_device(di);
- } else if (bi >= 0) {
- select_bss(bi);
- }
- }
- static void draw_bars(int x, int y, int bars, unsigned short color)
- {
- int i;
- for (i = 0; i < 4; i++) {
- int h = 3 + i * 3;
- struct ngui_rect r;
- r.x = x + i * 5;
- r.y = y + 12 - h;
- r.w = 3;
- r.h = h;
- ngui_fill_rect(&ui, r, i < bars ? color : ui.theme.border);
- }
- }
- static void draw_status(void)
- {
- if (status[0] && status_until &&
- (int)(u_ticks_ms() - status_until) < 0)
- ngui_text_utf8(&ui, 10, ui.h - 22, status,
- status_error ? ui.theme.error : ui.theme.accent);
- }
- static void draw_wifi_list(struct ngui_rect area)
- {
- char label[40], shortcut[20];
- int i;
- if (!wifi_present) {
- ngui_text(&ui, area.x + 2, area.y + 4,
- "No WiFi card detected", ui.theme.text_dim);
- return;
- }
- if (scanning) {
- ngui_text(&ui, area.x + 2, area.y + 4,
- "Scanning...", ui.theme.text_dim);
- return;
- }
- if (nbss == 0) {
- ngui_text(&ui, area.x + 2, area.y + 4,
- wst.state >= SYS_WIFI_ST_ASSOC ?
- "Connected - Disconnect to scan for other networks" :
- "Press Scan to list WiFi networks",
- ui.theme.text_dim);
- return;
- }
- wifi_scroll.content_h = nbss * ROW_H;
- ngui_scroll_begin(&ui, area, &wifi_scroll);
- ngui_scroll_wheel(&ui, area, &wifi_scroll);
- ngui_scroll_keys(&ui, &wifi_scroll);
- for (i = 0; i < nbss; i++) {
- struct ngui_rect rr;
- struct sys_wifi_bss *b = &bss[i];
- char ssid[33];
- int sw, bx, clicked;
- unsigned short signal;
- rr.x = area.x;
- rr.y = area.y - wifi_scroll.offset + i * ROW_H;
- rr.w = area.w - 16;
- rr.h = ROW_H - 2;
- if (rr.y + rr.h <= area.y || rr.y >= area.y + area.h)
- continue;
- ssid_name(b, ssid);
- sput(label, ssid);
- sput(shortcut, security_name(b->security));
- if (bss_saved(b) >= 0)
- sput(shortcut + slen(shortcut), " saved");
- clicked = ngui_menu_item_utf8(&ui, ID_BSS_BASE + i, rr, label,
- shortcut, bss_is_active(b));
- sw = (int)ngui_text_width_ui(&ui, shortcut);
- bx = rr.x + rr.w - 8 - sw - 30;
- if (b->security == SYS_WIFI_SEC_OTHER)
- signal = ui.theme.error;
- else
- signal = rssi_bars(b->rssi) >= 3 ? ui.theme.accent :
- ui.theme.text_dim;
- draw_bars(bx, rr.y + 3, rssi_bars(b->rssi), signal);
- if (clicked) {
- action_bss = i;
- }
- }
- ngui_scroll_end(&ui, area, &wifi_scroll);
- }
- static void draw_main(void)
- {
- struct ngui_rect body = { 10, 8, ui.w - 20, ui.h - 34 };
- struct ngui_rect r, rb;
- char line[128], *p;
- int i, shown;
- /* Active connection summary. */
- r = ngui_cut_top(&body, 20);
- p = line;
- if (!net_present) {
- p = sput(p, "No active network device");
- } else {
- p = sput(p, "Active: ");
- p = sput(p, net.name);
- p = sput(p, " (");
- p = sput(p, net.chip);
- p = sput(p, ")");
- if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
- p = sput(p, " ");
- for (i = 0; i < wst.ssid_len && i < 32; i++)
- *p++ = (char)wst.ssid[i];
- p = sput(p, " ");
- p = put_signed(p, wst.rssi);
- p = sput(p, " dBm");
- }
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 20);
- p = line;
- if (net_present) {
- p = sput(p, "IP ");
- if (net.ip[0] || net.ip[1] || net.ip[2] || net.ip[3])
- p = put_ip(p, net.ip);
- else
- p = sput(p, "not configured");
- if (seq(net.name, "wifi"))
- p = sput(p, wst.state >= SYS_WIFI_ST_LINK ?
- " link up" : " link down");
- } else if (wifi_present) {
- p = sput(p, "WiFi card present - scan to join a network");
- } else {
- p = sput(p, "Plug in Ethernet or USB tethering, then Refresh");
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text_dim);
- body.y += 4;
- body.h -= 4;
- /* Devices. */
- r = ngui_cut_top(&body, 26);
- ngui_text(&ui, r.x, r.y + 3, "Network devices", ui.theme.text_dim);
- rb = r;
- rb.x = r.x + r.w - 78;
- rb.w = 78;
- rb.h = 22;
- if (ngui_button(&ui, ID_REFRESH, rb, "Refresh"))
- actions |= ACT_REFRESH;
- shown = 0;
- for (i = 0; i < ndev && shown < 6; i++) {
- if (devs[i].kind == SYS_NETDEV_WIFI)
- continue;
- p = line;
- p = sput(p, devs[i].name);
- p = sput(p, " ");
- p = sput(p, devs[i].chip);
- *p = '\0';
- r = ngui_cut_top(&body, ROW_H);
- if (ngui_menu_item(&ui, ID_DEV_BASE + i, r, line,
- devs[i].active ? "active" :
- (devs[i].link ? "link" : ""),
- devs[i].active))
- action_dev = i;
- shown++;
- }
- if (!shown) {
- r = ngui_cut_top(&body, ROW_H);
- ngui_text(&ui, r.x + 2, r.y + 3,
- wifi_present ?
- "No wired or USB device - use the WiFi list" :
- "None found - Refresh after plugging a device in",
- ui.theme.text_dim);
- }
- body.y += 6;
- body.h -= 6;
- /* WiFi networks. */
- r = ngui_cut_top(&body, 26);
- ngui_text(&ui, r.x, r.y + 3, "WiFi networks", ui.theme.text_dim);
- rb = r;
- rb.x = r.x + r.w - 96;
- rb.w = 96;
- rb.h = 22;
- if (nsaved > 0) {
- struct ngui_rect sb = rb;
- sb.x = rb.x - 68;
- sb.w = 64;
- if (ngui_button(&ui, ID_SAVED, sb, "Saved"))
- actions |= ACT_SAVED;
- }
- if (wifi_present) {
- if (wst.state >= SYS_WIFI_ST_ASSOC) {
- if (ngui_button(&ui, ID_DISCONNECT, rb, "Disconnect"))
- actions |= ACT_DISCONNECT;
- } else if (scanning) {
- ngui_text(&ui, rb.x + 10, rb.y + 3, "Scanning...",
- ui.theme.text_dim);
- } else {
- if (ngui_button(&ui, ID_SCAN, rb, "Scan"))
- actions |= ACT_SCAN;
- }
- }
- draw_wifi_list(body);
- }
- static void draw_pass(void)
- {
- struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
- struct ngui_rect r, b1, b2;
- char line[80], *p;
- int ev;
- r = ngui_cut_top(&body, 24);
- p = sput(line, "Join WiFi network \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"");
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 22);
- ngui_text(&ui, r.x, r.y, "WPA2-PSK passphrase", ui.theme.text_dim);
- r = ngui_cut_top(&body, 30);
- ev = ngui_text_input(&ui, ID_PASS, r, &pass_in);
- if (ev & NGUI_INPUT_SUBMIT)
- actions |= ACT_CONNECT;
- if (ev & NGUI_INPUT_CANCEL)
- actions |= ACT_BACK;
- body.y += 6;
- body.h -= 6;
- r = ngui_cut_top(&body, 30);
- b1 = r;
- b1.w = 100;
- b2 = r;
- b2.x = r.x + 110;
- b2.w = 100;
- if (ngui_button(&ui, ID_PASS_OK, b1, "Connect"))
- actions |= ACT_CONNECT;
- if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel"))
- actions |= ACT_BACK;
- r = ngui_cut_top(&body, 22);
- ngui_text(&ui, r.x, r.y,
- "8-63 characters, or 64 hex digits for a raw PSK.",
- ui.theme.text_dim);
- draw_status();
- }
- static void draw_connect(void)
- {
- struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
- struct ngui_rect r, b;
- char line[96], *p;
- unsigned int start = 0, lines = 0, i;
- int y, line_y;
- r = ngui_cut_top(&body, 24);
- p = line;
- if (connect_pid > 0) {
- p = sput(p, "Connecting to \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"...");
- } else {
- p = sput(p, "WiFi connection \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"");
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- r = ngui_cut_top(&body, 22);
- if (status[0])
- ngui_text_utf8(&ui, r.x, r.y, status,
- status_error ? ui.theme.error : ui.theme.accent);
- body.y += 4;
- body.h -= 4;
- /* Last ~12 lines of the helper's terminal output. */
- for (i = conn_log_len; i > 0; i--) {
- if (conn_log[i - 1] == '\n' && ++lines > 12) {
- start = i;
- break;
- }
- }
- y = body.y;
- line_y = 0;
- for (i = start; i < conn_log_len && y + 16 <= body.y + body.h - 34; i++) {
- char c = conn_log[i];
- if (c == '\r')
- continue;
- if (c == '\n') {
- line[line_y] = '\0';
- ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
- y += 16;
- line_y = 0;
- continue;
- }
- if ((unsigned int)line_y + 1 < sizeof(line))
- line[line_y++] = c;
- }
- if (line_y) {
- line[line_y] = '\0';
- ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
- }
- /* Cancel at the bottom. */
- b.x = body.x;
- b.y = body.y + body.h - 28;
- b.w = 100;
- b.h = 24;
- if (ngui_button(&ui, ID_CONN_CANCEL, b,
- connect_pid > 0 ? "Cancel" : "Back"))
- actions |= ACT_BACK;
- }
- static void draw_saved(void)
- {
- struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
- struct ngui_rect r, b, row, bt;
- char line[96], *p;
- int i;
- r = ngui_cut_top(&body, 24);
- ngui_text(&ui, r.x, r.y, "Saved WiFi networks", ui.theme.text);
- body.y += 4;
- body.h -= 4;
- body.h -= 34; /* room for the Back button */
- if (!nsaved) {
- ngui_text(&ui, body.x + 2, body.y + 4,
- "No saved networks", ui.theme.text_dim);
- } else {
- saved_scroll.content_h = nsaved * 28;
- ngui_scroll_begin(&ui, body, &saved_scroll);
- ngui_scroll_wheel(&ui, body, &saved_scroll);
- ngui_scroll_keys(&ui, &saved_scroll);
- for (i = 0; i < nsaved; i++) {
- char ssid[33];
- row.x = body.x;
- row.y = body.y - saved_scroll.offset + i * 28;
- row.w = body.w - 16;
- row.h = 26;
- if (row.y + row.h <= body.y ||
- row.y >= body.y + body.h)
- continue;
- cfg_ssid_name(&saved[i], ssid);
- bt = row;
- bt.x = row.x + row.w - 64;
- bt.w = 60;
- bt.h = 22;
- row.w -= 70;
- p = sput(line, ssid);
- p = sput(p, saved[i].pass_len ? " WPA2" : " open");
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, row, row.x, row.y + 3,
- line, ui.theme.text);
- if (ngui_button(&ui, ID_SAVED_BASE + i, bt,
- "Forget")) {
- action_saved = i;
- actions |= ACT_FORGET;
- }
- }
- ngui_scroll_end(&ui, body, &saved_scroll);
- }
- b.x = body.x;
- b.y = body.y + body.h + 6;
- b.w = 100;
- b.h = 24;
- if (ngui_button(&ui, ID_SAVED_BACK, b, "Back"))
- actions |= ACT_BACK;
- }
- static void draw(void)
- {
- ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
- ui.theme.bg);
- if (view == VIEW_PASS)
- draw_pass();
- else if (view == VIEW_CONNECT)
- draw_connect();
- else if (view == VIEW_SAVED)
- draw_saved();
- else
- draw_main();
- if (view != VIEW_PASS && view != VIEW_CONNECT)
- draw_status();
- }
- static void paint_frame(void)
- {
- if (ngui_begin(&ui)) {
- draw();
- ngui_end(&ui);
- }
- actions = 0;
- action_dev = -1;
- action_bss = -1;
- action_saved = -1;
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- unsigned int last_status = 0;
- (void)arg;
- if (u_win_create(460, 500, "Network", &wi) != 0 &&
- u_win_create(400, 420, "Network", &wi) != 0 &&
- u_win_create(340, 360, "Network", &wi) != 0) {
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- ngui_font_attach(&ui);
- pass_in.secret = 1;
- pass_in.placeholder = "WPA2 passphrase";
- view = VIEW_MAIN;
- load_devices();
- load_status();
- load_saved();
- if (wifi_present && wst.state != SYS_WIFI_ST_DOWN &&
- wst.state < SYS_WIFI_ST_ASSOC) {
- start_scan();
- paint_frame();
- }
- for (;;) {
- unsigned int now = u_ticks_ms();
- drain_tty();
- poll_scan();
- poll_connect();
- if (now - last_status >= 1000) {
- last_status = now;
- load_status();
- ngui_invalidate(&ui);
- }
- if (status_until && (int)(now - status_until) >= 0) {
- status[0] = '\0';
- status_until = 0;
- ngui_invalidate(&ui);
- }
- if (ngui_begin(&ui)) {
- draw();
- ngui_end(&ui);
- handle_actions();
- }
- /* The password field owns Esc while focused. */
- if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
- ngui_key(&ui);
- if (view == VIEW_PASS) {
- view = VIEW_MAIN;
- set_status("", 0);
- } else if (view == VIEW_CONNECT) {
- cancel_connect();
- } else if (view == VIEW_SAVED) {
- view = VIEW_MAIN;
- set_status("", 0);
- } else if (ui.focus == 0) {
- break;
- }
- }
- u_sleep_ms(25);
- }
- ngui_font_detach(&ui);
- u_win_close();
- u_exit(0);
- }
|