| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- /* 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 DEV_ROW_H 42
- #define WIFI_ROW_H 42
- #define SAVED_ROW_H 30
- #define SAVED_LIST_ROW_H 42
- #define SECTION_H 28
- #define MENU_GAP 10
- #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 ID_INFO_BASE 400
- #define ID_SAVED_ROW_BASE 500
- #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_popover pop;
- static struct ngui_input pass_in;
- static struct ngui_scroll main_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 char *put_mac(char *p, const unsigned char mac[6])
- {
- static const char hex[] = "0123456789abcdef";
- int i;
- for (i = 0; i < 6; i++) {
- *p++ = hex[mac[i] >> 4];
- *p++ = hex[mac[i] & 15];
- if (i != 5)
- *p++ = ':';
- }
- return p;
- }
- static int ip_any(const unsigned char ip[4])
- {
- return ip[0] || ip[1] || ip[2] || ip[3];
- }
- 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;
- main_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_status(void)
- {
- if (status[0] && status_until &&
- (int)(u_ticks_ms() - status_until) < 0)
- ngui_popover_footer(&ui, &pop, status,
- status_error ? ui.theme.error : ui.theme.success);
- }
- /* The active connection at the top of the panel: a status dot, the link
- * state and the device address (plus the SSID and RSSI on Wi-Fi). */
- static void draw_status_card(struct ngui_rect r)
- {
- struct ngui_rect line_r = { r.x + 34, r.y + 34, r.w - 44, 16 };
- struct ngui_rect dot = { r.x + 14, r.y + 25, 10, 10 };
- char line[160], *p;
- unsigned short dot_color;
- int i, have_ip = 0;
- for (i = 0; i < 4; i++)
- if (net.ip[i])
- have_ip = 1;
- if (!net_present)
- dot_color = ui.theme.error;
- else if (!have_ip ||
- (seq(net.name, "wifi") && wst.state < SYS_WIFI_ST_LINK))
- dot_color = ui.theme.warning;
- else
- dot_color = ui.theme.success;
- ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel);
- ngui_frame_round(&ui, r, 8, 8, 8, 8, 1, ui.theme.panel_alt);
- ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color);
- p = line;
- if (!net_present)
- p = sput(p, "Offline");
- else if (seq(net.name, "wifi"))
- p = sput(p, wst.state >= SYS_WIFI_ST_LINK ?
- "Wi-Fi connected" : "Wi-Fi");
- else if (seq(net.name, "rndis"))
- p = sput(p, "USB tethering");
- else
- p = sput(p, "Wired network");
- *p = '\0';
- ngui_text_px(&ui, r.x + 34, r.y + 10, line, slen(line),
- ui.theme.text, 14, 1, 0, 0);
- p = line;
- if (net_present) {
- p = sput(p, net.name);
- p = sput(p, " \xC2\xB7 ");
- if (have_ip)
- p = put_ip(p, net.ip);
- else
- p = sput(p, "no IP");
- if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
- p = sput(p, " \xC2\xB7 ");
- 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");
- }
- } else {
- p = sput(p, "No network device active");
- }
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, line_r, line_r.x, line_r.y, line,
- ui.theme.text_dim);
- }
- static void draw_wifi_list(struct ngui_rect area, int *y)
- {
- struct ngui_row row;
- char ssid[33], sub[72], *p;
- int i;
- if (!wifi_present) {
- ngui_text(&ui, area.x + 12, *y + 12,
- "No Wi-Fi card detected", ui.theme.text_dim);
- *y += WIFI_ROW_H;
- return;
- }
- if (scanning) {
- ngui_text(&ui, area.x + 12, *y + 12,
- "Scanning for networks...", ui.theme.accent);
- *y += WIFI_ROW_H;
- return;
- }
- if (nbss == 0) {
- const char *msg = wst.state >= SYS_WIFI_ST_ASSOC ?
- "Connected - disconnect to scan for other networks" :
- "Press Scan to list Wi-Fi networks";
- ngui_text(&ui, area.x + 12, *y + 12, msg,
- ui.theme.text_dim);
- *y += WIFI_ROW_H;
- return;
- }
- for (i = 0; i < nbss; i++) {
- struct sys_wifi_bss *b = &bss[i];
- struct ngui_rect r = { area.x, *y, area.w - 18, WIFI_ROW_H };
- if (r.y + r.h > area.y && r.y < area.y + area.h) {
- ssid_name(b, ssid);
- p = sub;
- p = sput(p, security_name(b->security));
- if (bss_saved(b) >= 0)
- p = sput(p, " \xC2\xB7 saved");
- if (bss_is_active(b))
- p = sput(p, " \xC2\xB7 connected");
- *p = '\0';
- row.title = ssid;
- row.subtitle = sub;
- row.trailing = 0;
- row.bars = b->security == SYS_WIFI_SEC_OTHER ? -1 :
- rssi_bars(b->rssi);
- row.selected = bss_is_active(b);
- if (ngui_list_row_utf8(&ui, ID_BSS_BASE + i, r,
- &row))
- action_bss = i;
- }
- *y += WIFI_ROW_H;
- }
- }
- static void draw_main(void)
- {
- struct ngui_rect body = pop.body;
- struct ngui_rect card, area, r;
- struct ngui_row row;
- char line[48], *p;
- int i, y, content_h, shown, more;
- card = ngui_cut_top(&body, 64);
- draw_status_card(card);
- body.y += MENU_GAP;
- body.h -= MENU_GAP;
- shown = 0;
- for (i = 0; i < ndev; i++)
- if (devs[i].kind != SYS_NETDEV_WIFI)
- shown++;
- more = shown > 4 ? shown - 4 : 0;
- if (shown > 4)
- shown = 4;
- content_h = SECTION_H + (shown ? shown * DEV_ROW_H : DEV_ROW_H) +
- (more ? 20 : 0) + MENU_GAP + SECTION_H;
- if (!wifi_present || scanning || nbss == 0)
- content_h += WIFI_ROW_H;
- else
- content_h += nbss * WIFI_ROW_H;
- if (net_present)
- content_h += MENU_GAP + SECTION_H + 4 * 30;
- if (nsaved)
- content_h += SAVED_ROW_H + MENU_GAP;
- main_scroll.content_h = content_h;
- area = body;
- ngui_scroll_begin(&ui, area, &main_scroll);
- ngui_scroll_wheel(&ui, area, &main_scroll);
- ngui_scroll_keys(&ui, &main_scroll);
- y = area.y - main_scroll.offset;
- r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
- if (ngui_section(&ui, r, "Devices", ID_REFRESH, "Refresh"))
- actions |= ACT_REFRESH;
- y += SECTION_H;
- {
- int n = 0;
- for (i = 0; i < ndev && n < shown; i++) {
- if (devs[i].kind == SYS_NETDEV_WIFI)
- continue;
- r = (struct ngui_rect){ area.x, y, area.w - 18,
- DEV_ROW_H };
- if (r.y + r.h > area.y && r.y < area.y + area.h) {
- row.title = devs[i].name;
- row.subtitle = devs[i].chip;
- row.trailing = devs[i].active ? "Connected" :
- (devs[i].link ? "link" : 0);
- row.bars = -1;
- row.selected = devs[i].active;
- if (ngui_list_row(&ui, ID_DEV_BASE + i, r,
- &row))
- action_dev = i;
- }
- y += DEV_ROW_H;
- n++;
- }
- }
- if (!shown) {
- const char *msg = wifi_present ?
- "No wired or USB device" :
- "No device found - plug one in and Refresh";
- ngui_text(&ui, area.x + 12, y + 12, msg,
- ui.theme.text_dim);
- y += DEV_ROW_H;
- } else if (more) {
- p = line;
- p = sput(p, "+");
- p = put_dec(p, (unsigned int)more);
- p = sput(p, " more devices");
- *p = '\0';
- ngui_text(&ui, area.x + 12, y + 2, line,
- ui.theme.text_dim);
- y += 20;
- }
- y += MENU_GAP;
- r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
- if (wifi_present && wst.state >= SYS_WIFI_ST_ASSOC) {
- if (ngui_section(&ui, r, "Wi-Fi", ID_DISCONNECT,
- "Disconnect"))
- actions |= ACT_DISCONNECT;
- } else if (wifi_present && !scanning) {
- if (ngui_section(&ui, r, "Wi-Fi", ID_SCAN, "Scan"))
- actions |= ACT_SCAN;
- } else {
- ngui_section(&ui, r, "Wi-Fi", 0, 0);
- }
- y += SECTION_H;
- draw_wifi_list(area, &y);
- if (net_present) {
- static const char *labels[4] = {
- "IP address", "Gateway", "DNS", "MAC address"
- };
- char value[40];
- int di;
- y += MENU_GAP;
- r = (struct ngui_rect){ area.x, y, area.w - 18, SECTION_H };
- ngui_section(&ui, r, "Connection", 0, 0);
- y += SECTION_H;
- for (di = 0; di < 4; di++) {
- char *vp = value;
- if (di == 0)
- vp = ip_any(net.ip) ? put_ip(vp, net.ip) :
- sput(vp, "not configured");
- else if (di == 1)
- vp = ip_any(net.gw) ? put_ip(vp, net.gw) :
- sput(vp, "-");
- else if (di == 2)
- vp = ip_any(net.dns) ? put_ip(vp, net.dns) :
- sput(vp, "-");
- else
- vp = put_mac(vp, net.mac);
- *vp = '\0';
- r = (struct ngui_rect){ area.x, y, area.w - 18, 30 };
- if (r.y + r.h > area.y && r.y < area.y + area.h) {
- row.title = labels[di];
- row.subtitle = 0;
- row.trailing = value;
- row.bars = -1;
- row.selected = 0;
- ngui_list_row(&ui, ID_INFO_BASE + di, r, &row);
- }
- y += 30;
- }
- }
- if (nsaved) {
- char nbuf[12], *np = nbuf;
- y += MENU_GAP;
- r = (struct ngui_rect){ area.x, y, area.w - 18, SAVED_ROW_H };
- if (r.y + r.h > area.y && r.y < area.y + area.h) {
- np = put_dec(np, (unsigned int)nsaved);
- *np = '\0';
- row.title = "Saved networks";
- row.subtitle = 0;
- row.trailing = nbuf;
- row.bars = -1;
- row.selected = 0;
- if (ngui_list_row(&ui, ID_SAVED, r, &row))
- actions |= ACT_SAVED;
- }
- }
- ngui_scroll_end(&ui, area, &main_scroll);
- }
- static void draw_pass(void)
- {
- struct ngui_rect body = pop.body;
- struct ngui_rect r, b1, b2;
- char line[80], *p;
- int ev;
- r = ngui_cut_top(&body, 24);
- p = sput(line, "Join \"");
- 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_px(&ui, r.x, r.y, "WPA2-PSK passphrase", 19,
- ui.theme.text_dim, 14, 1, 0, 0);
- r = ngui_cut_top(&body, 34);
- 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 += MENU_GAP;
- body.h -= MENU_GAP;
- 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);
- body.y += MENU_GAP;
- body.h -= MENU_GAP;
- r = ngui_cut_top(&body, 28);
- b1 = r;
- b1.w = 110;
- b2 = r;
- b2.x = r.x + 120;
- b2.w = 110;
- if (ngui_button(&ui, ID_PASS_OK, b1, "Connect"))
- actions |= ACT_CONNECT;
- if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel"))
- actions |= ACT_BACK;
- }
- static void draw_connect(void)
- {
- struct ngui_rect body = pop.body;
- struct ngui_rect log, b, r;
- char line[96], *p;
- unsigned int start = 0, lines = 0, i;
- int y, line_y = 0;
- r = ngui_cut_top(&body, 22);
- p = sput(line, connect_pid > 0 ? "Connecting to \"" :
- "Connection to \"");
- p = sput(p, sel_ssid);
- p = sput(p, "\"");
- *p = '\0';
- ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
- /* The supplicant trail in a recessed card; the connection state is in
- * the popover footer. */
- log = body;
- log.h -= 38;
- ngui_fill_round_rect(&ui, log, 6, 6, 6, 6, ui.theme.panel);
- ngui_push_clip(&ui, (struct ngui_rect){ log.x + 8, log.y + 8,
- log.w - 16, log.h - 16 });
- for (i = conn_log_len; i > 0; i--) {
- if (conn_log[i - 1] == '\n' && ++lines > 12) {
- start = i;
- break;
- }
- }
- y = log.y + 10;
- for (i = start; i < conn_log_len && y + 17 <= log.y + log.h - 8; i++) {
- char c = conn_log[i];
- if (c == '\r')
- continue;
- if (c == '\n') {
- line[line_y] = '\0';
- if (line_y)
- ngui_text_px(&ui, log.x + 10, y, line,
- (unsigned int)line_y,
- ui.theme.text_dim, 14, 0, 0, 1);
- y += 17;
- 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_px(&ui, log.x + 10, y, line, (unsigned int)line_y,
- ui.theme.text_dim, 14, 0, 0, 1);
- }
- ngui_pop_clip(&ui);
- b.x = body.x;
- b.y = body.y + body.h - 28;
- b.w = 110;
- b.h = 28;
- 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 = pop.body;
- struct ngui_rect back, row, bt, list, r;
- struct ngui_row desc;
- char ssid[33];
- int i;
- r = ngui_cut_top(&body, 22);
- ngui_text_px(&ui, r.x, r.y, "Saved Wi-Fi networks", 20,
- ui.theme.text, 14, 1, 0, 0);
- if (!nsaved) {
- ngui_text(&ui, body.x + 2, body.y + 4,
- "No saved networks", ui.theme.text_dim);
- } else {
- list = body;
- list.h -= 38;
- saved_scroll.content_h = nsaved * SAVED_LIST_ROW_H;
- ngui_scroll_begin(&ui, list, &saved_scroll);
- ngui_scroll_wheel(&ui, list, &saved_scroll);
- ngui_scroll_keys(&ui, &saved_scroll);
- for (i = 0; i < nsaved; i++) {
- row.x = list.x;
- row.y = list.y - saved_scroll.offset +
- i * SAVED_LIST_ROW_H;
- row.w = list.w - 16;
- row.h = SAVED_LIST_ROW_H;
- if (row.y + row.h <= list.y ||
- row.y >= list.y + list.h)
- continue;
- cfg_ssid_name(&saved[i], ssid);
- bt = row;
- bt.x = row.x + row.w - 64;
- bt.w = 60;
- bt.h = 28;
- bt.y = row.y + 7;
- row.w -= 76;
- desc.title = ssid;
- desc.subtitle = saved[i].pass_len ? "WPA2-PSK" : "Open";
- desc.trailing = 0;
- desc.bars = -1;
- desc.selected = 0;
- ngui_list_row_utf8(&ui, ID_SAVED_ROW_BASE + i, row,
- &desc);
- if (ngui_button(&ui, ID_SAVED_BASE + i, bt,
- "Forget")) {
- action_saved = i;
- actions |= ACT_FORGET;
- }
- }
- ngui_scroll_end(&ui, list, &saved_scroll);
- }
- back.x = body.x;
- back.y = body.y + body.h - 28;
- back.w = 100;
- back.h = 28;
- if (ngui_button(&ui, ID_SAVED_BACK, back, "Back"))
- actions |= ACT_BACK;
- }
- static void draw(void)
- {
- ngui_popover_begin(&ui, &pop);
- if (view == VIEW_PASS)
- draw_pass();
- else if (view == VIEW_CONNECT)
- draw_connect();
- else if (view == VIEW_SAVED)
- draw_saved();
- else
- draw_main();
- 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_popover(404, 520, "Network", &wi) != 0 &&
- u_win_create_popover(380, 440, "Network", &wi) != 0 &&
- u_win_create_popover(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;
- main_scroll.offset = 0;
- 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 (;;) {
- static int first_frame = 1;
- 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);
- if (first_frame) {
- /* The first present can race the WM's initial
- * mapping of the popover canvas; paint a second
- * frame once so the panel never shows a black
- * tail. */
- first_frame = 0;
- ngui_invalidate(&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);
- }
|