AmendHub

Download:

jcs

/

wifi_da

/

amendments

/

38

*: Revamp scan->menu building, reduce data segment size

We were overflowing wifi_menu_networks in some cases.

jcs made amendment 38 14 days ago
--- main.c Tue Dec 3 22:23:52 2024 +++ main.c Tue Jan 7 15:00:05 2025 @@ -27,11 +27,10 @@ SysEnvRec world; #define ON_SYSTEM_7 (world.systemVersion >= 0x0700) struct wifi_da wifi_da = { 0 }; - short wifi_scsi_id = WIFI_SCSI_ID_FINDING; unsigned long wifi_scan_started = 0; -struct wifi_network_entry wifi_cur_info = { 0 }; -struct wifi_network_entry wifi_scan_networks[10] = { 0 }; +struct wifi_network_entry wifi_cur_info; +struct wifi_network_entry wifi_scan_networks[10]; MenuHandle menu, apple_menu; short menuID = 0; Cursor watch; @@ -112,6 +111,8 @@ da_open(void) } wifi_da.state = STATE_INIT; + memset(&wifi_cur_info, 0, sizeof(wifi_cur_info)); + memset(&wifi_scan_networks, 0, sizeof(wifi_scan_networks)); SetResLoad(false); h = GetCursor(watchCursor); --- wi-fi.h Tue Dec 3 22:10:25 2024 +++ wi-fi.h Tue Jan 7 16:52:45 2025 @@ -125,7 +125,8 @@ enum { }; extern unsigned long wifi_scan_started; extern struct wifi_network_entry wifi_cur_info; -extern struct wifi_network_entry wifi_scan_networks[10]; +#define MAX_WIFI_NETWORKS 10 +extern struct wifi_network_entry wifi_scan_networks[MAX_WIFI_NETWORKS]; extern short nwifi_scan_networks; extern Cursor watch; --- window.c Tue Dec 3 22:11:09 2024 +++ window.c Tue Jan 7 17:18:03 2025 @@ -19,7 +19,7 @@ #include "wi-fi.h" short nwifi_scan_networks = 0; -struct wifi_network_entry wifi_menu_networks[nitems(wifi_scan_networks)] = { 0 }; +struct wifi_network_entry wifi_menu_networks[MAX_WIFI_NETWORKS]; short nwifi_menu_networks = 0; void create_logger(void); @@ -32,6 +32,8 @@ create_window(short ctlrefnum) Str255 str; GrafPtr savePort, tport; + memset(&wifi_menu_networks, 0, sizeof(wifi_menu_networks)); + GetPort(&savePort); /* screenBits is not available from a DA */ @@ -225,7 +227,7 @@ void update_wifi_ssid_list(bool update_networks) { Str255 str; - char ssid[64] = { 0 }; + char ssid[64]; short n, m, mitem; if (wifi_scsi_id == WIFI_SCSI_ID_NONE) @@ -237,59 +239,75 @@ update_wifi_ssid_list(bool update_networks) logger_printf("scsi_wifi_scan_results: %d networks", nwifi_scan_networks); - if (nwifi_scan_networks == 0) { - memcpy(&wifi_scan_networks, &wifi_cur_info, - sizeof(wifi_cur_info)); - nwifi_scan_networks = 1; - } + if (nwifi_scan_networks == 0) + logger_printf("no networks found during scan"); } - logger_printf("current info: ssid:%s", wifi_cur_info.ssid); + if (nwifi_scan_networks == 0) { + /* use our current network (or none) as the only one found */ + memcpy(&wifi_scan_networks, &wifi_cur_info, sizeof(wifi_cur_info)); + nwifi_scan_networks = 1; + } + + logger_printf("current ssid: %s", wifi_cur_info.ssid[0] == '\0' ? + "(none)" : wifi_cur_info.ssid); - /* put the current network first, if any */ - memcpy(&wifi_menu_networks[0], &wifi_cur_info, - sizeof(wifi_menu_networks[0])); + /* + * now start building the ssid menu from wifi_scan_networks + */ - nwifi_menu_networks = nwifi_scan_networks; - if (nwifi_menu_networks > nitems(wifi_menu_networks)) { - nwifi_menu_networks = nitems(wifi_menu_networks); - logger_printf("capping nets to %d", nwifi_menu_networks); + /* leave room for current network and trailing "(other network)" */ + if (nwifi_scan_networks > MAX_WIFI_NETWORKS - 2) { + nwifi_scan_networks = MAX_WIFI_NETWORKS - 2; + logger_printf("capping wifi_scan_networks to %d", + nwifi_scan_networks); } - /* add each additional network from the scan, excluding current */ - for (n = 0, m = 1; n < nwifi_menu_networks; n++) { - if (strcmp(wifi_scan_networks[n].ssid, - wifi_cur_info.ssid) == 0) { - logger_printf("skipping %s, is current", - wifi_scan_networks[n].ssid); + /* put the current network (if any) first in the menu */ + logger_printf("menu[0]: %s", wifi_cur_info.ssid[0] ? + wifi_cur_info.ssid : "(no network)"); + memcpy(&wifi_menu_networks[0], &wifi_cur_info, + sizeof(wifi_menu_networks[0])); + nwifi_menu_networks = 1; + + /* add each additional network from the scan */ + for (n = 0; n < nwifi_scan_networks; n++) { + /* except current one, which we added first */ + if (strcmp(wifi_scan_networks[n].ssid, wifi_cur_info.ssid) == 0) continue; - } - logger_printf("net %d: %s", m, wifi_scan_networks[n].ssid); - - memcpy(&wifi_menu_networks[m++], &wifi_scan_networks[n], - sizeof(wifi_menu_networks[0])); + memcpy(&wifi_menu_networks[nwifi_menu_networks], + &wifi_scan_networks[n], sizeof(wifi_menu_networks[0])); + logger_printf("menu[%d]: %s", nwifi_menu_networks, + wifi_menu_networks[nwifi_menu_networks].ssid); + nwifi_menu_networks++; } - nwifi_menu_networks = m; - if (wifi_menu_networks[0].ssid[0] == '\0') { + /* replace blank string with "(no network)" */ GetIndString(str, wifi_da.str_res_id, STR_NO_NETWORK_ID); PtoCstr(str); strlcpy(wifi_menu_networks[0].ssid, (char *)str, sizeof(wifi_menu_networks[0].ssid)); - nwifi_menu_networks++; } - - /* keep "other network" as last entry */ + + /* add "other network" as last entry */ + nwifi_menu_networks++; + if (nwifi_menu_networks > MAX_WIFI_NETWORKS) + panic("nwifi_menu_networks overflow"); GetIndString(str, wifi_da.str_res_id, STR_OTHER_NETWORK_ID); PtoCstr(str); strlcpy(wifi_menu_networks[nwifi_menu_networks - 1].ssid, (char *)str, sizeof(wifi_menu_networks[0].ssid)); wifi_menu_networks[nwifi_menu_networks - 1].flags = WIFI_NETWORK_FLAG_AUTH | WIFI_NETWORK_FLAG_HIDDEN; + logger_printf("menu[%d]: %s", nwifi_menu_networks - 1, + "(other network)"); - /* leave the first item and we'll set its text later */ + /* + * leave the first item and we'll set its text later but delete all + * others + */ while (CountMItems(wifi_da.ssid_menu) > 1) DelMenuItem(wifi_da.ssid_menu, 2);