From 1e644a5593e50a49284a050e4ec3f97de29a1e99 Mon Sep 17 00:00:00 2001 From: yenon Date: Sun, 7 Apr 2024 00:39:40 +0200 Subject: [PATCH 1/6] bugged initial testing for random mac setting --- src/core/settings.c | 3 ++ src/core/settings.h | 1 + src/ui/page_wifi.c | 74 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/core/settings.c b/src/core/settings.c index 1af64c74..76b763a2 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -1,5 +1,6 @@ #include "settings.h" +#include #include #include #include @@ -208,6 +209,7 @@ const setting_t g_setting_defaults = { .rf_channel = 6, .root_pw = "divimath", .ssh = false, + .macRandom = true, }, .storage = { .logging = false, @@ -448,6 +450,7 @@ void settings_load(void) { g_setting.wifi.rf_channel = ini_getl("wifi", "rf_channel", g_setting_defaults.wifi.rf_channel, SETTING_INI); ini_gets("wifi", "root_pw", g_setting_defaults.wifi.root_pw, g_setting.wifi.root_pw, WIFI_PASSWD_MAX, SETTING_INI); g_setting.wifi.ssh = settings_get_bool("wifi", "ssh", g_setting_defaults.wifi.ssh); + g_setting.wifi.macRandom = settings_get_bool("wifi", "macRandom", g_setting_defaults.wifi.macRandom); // no dial under video mode g_setting.ease.no_dial = fs_file_exists(NO_DIAL_FILE); diff --git a/src/core/settings.h b/src/core/settings.h index d978de1c..e59a0536 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -220,6 +220,7 @@ typedef struct { uint8_t rf_channel; char root_pw[WIFI_SSID_MAX]; bool ssh; + bool macRandom; } wifi_t; typedef struct { diff --git a/src/ui/page_wifi.c b/src/ui/page_wifi.c index fbd48aa4..98b0b9f2 100644 --- a/src/ui/page_wifi.c +++ b/src/ui/page_wifi.c @@ -92,6 +92,7 @@ typedef struct { root_pw_t root_pw; button_t ssh; lv_obj_t *note; + button_t macRandom; int row_count; } page_3_t; @@ -105,6 +106,11 @@ typedef struct { bool dirty; } page_options_t; +typedef struct { + char ipAddress[16]; + char macAddress[18]; +} wifi_info; + /** * Constants */ @@ -145,6 +151,10 @@ static void page_wifi_update_services() { fprintf(fp, "insmod /mnt/app/ko/xradio_mac.ko\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_core.ko\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_wlan.ko\n"); + if (!g_setting.wifi.macRandom) { + // fprintf(fp, "ifconfig wlan0 hw ether 00:28:C7:0A:42:A2"); + } + fprintf(fp, "ifconfig wlan0 up\n"); if (g_setting.wifi.dhcp) { @@ -267,6 +277,7 @@ static void page_wifi_update_settings() { g_setting.wifi.mode = btn_group_get_sel(&page_wifi.page_1.mode.button); g_setting.wifi.dhcp = btn_group_get_sel(&page_wifi.page_2.dhcp.button) == 0; g_setting.wifi.ssh = btn_group_get_sel(&page_wifi.page_3.ssh.button) == 0; + g_setting.wifi.macRandom = btn_group_get_sel(&page_wifi.page_3.macRandom.button) == 0; snprintf(g_setting.wifi.ssid[g_setting.wifi.mode], WIFI_SSID_MAX, "%s", page_wifi.page_1.ssid.text[g_setting.wifi.mode]); snprintf(g_setting.wifi.passwd[g_setting.wifi.mode], WIFI_PASSWD_MAX, "%s", page_wifi.page_1.passwd.text[g_setting.wifi.mode]); @@ -297,6 +308,7 @@ static void page_wifi_update_settings() { ini_putl("wifi", "rf_channel", g_setting.wifi.rf_channel, SETTING_INI); ini_puts("wifi", "root_pw", g_setting.wifi.root_pw, SETTING_INI); settings_put_bool("wifi", "ssh", g_setting.wifi.ssh); + settings_put_bool("wifi", "macRandom", g_setting.wifi.macRandom); // Prepare WiFi interfaces system_script(WIFI_OFF); @@ -316,35 +328,48 @@ static void page_wifi_update_settings() { /** * Acquire the actual address in use. */ -static const char *page_wifi_get_real_address() { - const char *address = NULL; +static wifi_info *page_wifi_get_real_address() { + wifi_info *info; int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + strcpy(info->ipAddress, ""); + strcpy(info->macAddress, ""); + if (fd >= 0) { struct ifreq ifr; strcpy(ifr.ifr_name, "wlan0"); // Try to derive the real ip address if (0 == ioctl(fd, SIOCGIFADDR, &ifr)) { - address = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); + strcpy(info->ipAddress, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); } + /* + if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) { + sprintf(info->macAddress, "%02x:%02x:%02x:%02x:%02x:%02x", + (unsigned char)ifr.ifr_addr.sa_data[0], + (unsigned char)ifr.ifr_addr.sa_data[1], + (unsigned char)ifr.ifr_addr.sa_data[2], + (unsigned char)ifr.ifr_addr.sa_data[3], + (unsigned char)ifr.ifr_addr.sa_data[4], + (unsigned char)ifr.ifr_addr.sa_data[5]); + }*/ close(fd); } - return address; + return info; } /** * Updates the all notes on every page. */ static void page_wifi_update_page_1_notes() { - const char *address = page_wifi_get_real_address(); + wifi_info *info = page_wifi_get_real_address(); if (btn_group_get_sel(&page_wifi.page_1.mode.button) == WIFI_MODE_STA && btn_group_get_sel(&page_wifi.page_2.dhcp.button) == 0 && - address == NULL) { - address = "x.x.x.x"; + strcmp(info->ipAddress, "")) { + strcpy(info->ipAddress, "x.x.x.x"); } static char buffer[1024]; @@ -356,17 +381,22 @@ static void page_wifi_update_page_1_notes() { " 1. Connect to the WiFi network identified above.\n" " 2. Use VLC Player to open a Network Stream:\n\n" " rtsp://%s:8554/hdzero\n\n", - address ? address : page_wifi.page_2.ip_addr.text); + strcmp(info->ipAddress, "") ? info->ipAddress : page_wifi.page_2.ip_addr.text); lv_label_set_text(page_wifi.page_1.note, buffer); } static void page_wifi_update_page_3_notes() { + wifi_info *info = page_wifi_get_real_address(); + static char buffer[1024]; snprintf(buffer, sizeof(buffer), "Password Requirements:\n" - " Minimum 8 characters, maximum 64 characters.\n\n"); + " Minimum 8 characters, maximum 64 characters.\n\n" + "MAC-Address:\n" + " %s\n\n", + info->macAddress); lv_label_set_text(page_wifi.page_3.note, buffer); } @@ -440,6 +470,7 @@ static void page_wifi_update_current_page(int which) { lv_obj_add_flag(page_wifi.page_3.root_pw.input, LV_OBJ_FLAG_HIDDEN); lv_obj_add_flag(page_wifi.page_3.root_pw.status, LV_OBJ_FLAG_HIDDEN); btn_group_show(&page_wifi.page_3.ssh.button, false); + btn_group_show(&page_wifi.page_3.macRandom.button, false); lv_obj_add_flag(page_wifi.page_3.note, LV_OBJ_FLAG_HIDDEN); switch (which) { @@ -473,8 +504,8 @@ static void page_wifi_update_current_page(int which) { lv_obj_clear_flag(page_wifi.page_2.gateway.input, LV_OBJ_FLAG_HIDDEN); if (page_wifi.page_1.mode.button.current == WIFI_MODE_AP || - (page_wifi.page_1.mode.button.current == WIFI_MODE_STA && - page_wifi.page_2.dhcp.button.current == 1)) { + (page_wifi.page_1.mode.button.current == WIFI_MODE_STA && + page_wifi.page_2.dhcp.button.current == 1)) { lv_obj_clear_state(page_wifi.page_2.netmask.label, STATE_DISABLED); lv_obj_clear_state(page_wifi.page_2.netmask.input, STATE_DISABLED); lv_obj_clear_state(page_wifi.page_2.gateway.label, STATE_DISABLED); @@ -513,6 +544,7 @@ static void page_wifi_update_current_page(int which) { lv_obj_clear_flag(page_wifi.page_3.root_pw.input, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(page_wifi.page_3.root_pw.status, LV_OBJ_FLAG_HIDDEN); btn_group_show(&page_wifi.page_3.ssh.button, true); + btn_group_show(&page_wifi.page_3.macRandom.button, true); lv_obj_clear_flag(page_wifi.page_3.note, LV_OBJ_FLAG_HIDDEN); page_wifi_update_page_3_notes(); break; @@ -543,7 +575,8 @@ static void page_wifi_dirty_flag_reset() { page_wifi.page_2.dns.dirty = page_wifi.page_2.rf_channel.dirty = page_wifi.page_3.root_pw.dirty = - page_wifi.page_3.ssh.dirty = false; + page_wifi.page_3.ssh.dirty = + page_wifi.page_3.macRandom.dirty = false; } /** @@ -600,7 +633,8 @@ static void page_wifi_update_dirty_flag() { page_wifi.page_2.dns.dirty || page_wifi.page_2.rf_channel.dirty || page_wifi.page_3.root_pw.dirty || - page_wifi.page_3.ssh.dirty; + page_wifi.page_3.ssh.dirty || + page_wifi.page_3.macRandom.dirty; if (page_wifi.dirty) { if (!page_wifi_apply_settings_pending_timer) { @@ -681,6 +715,9 @@ static void page_wifi_create_page_3(lv_obj_t *parent) { create_btn_group_item(&page_wifi.page_3.ssh.button, parent, 2, "SSH", "On", "Off", "", "", 2); btn_group_set_sel(&page_wifi.page_3.ssh.button, !g_setting.wifi.ssh); + create_btn_group_item(&page_wifi.page_3.macRandom.button, parent, 2, "MAC", "Random", "Fixed", "", "", 3); + btn_group_set_sel(&page_wifi.page_3.macRandom.button, !g_setting.wifi.macRandom); + page_wifi.page_3.note = lv_label_create(parent); lv_obj_set_style_text_font(page_wifi.page_3.note, &lv_font_montserrat_16, 0); lv_obj_set_style_text_align(page_wifi.page_3.note, LV_TEXT_ALIGN_LEFT, 0); @@ -690,7 +727,7 @@ static void page_wifi_create_page_3(lv_obj_t *parent) { lv_obj_set_grid_cell(page_wifi.page_3.note, LV_GRID_ALIGN_START, 1, 4, LV_GRID_ALIGN_START, 7, 2); page_wifi_update_page_3_notes(); - page_wifi.page_3.row_count = 3; + page_wifi.page_3.row_count = 4; } /** @@ -784,7 +821,7 @@ static void page_wifi_on_update(uint32_t delta_ms) { if (g_setting.wifi.enable && (elapsed == -1 || (elapsed += delta_ms) > 300000)) { switch (g_setting.wifi.mode) { case WIFI_MODE_STA: - if (page_wifi_get_real_address()) { + if (!strcmp(page_wifi_get_real_address()->ipAddress, "")) { if (!fs_file_exists("/tmp/hdz_goggle_fw.latest") && !fs_file_exists("/tmp/hdz_vtx_fw.latest")) { system_script(WIFI_DOWNLOAD); @@ -912,6 +949,11 @@ static void page_wifi_on_click(uint8_t key, int sel) { keyboard_press(); } break; + case 2: + btn_group_toggle_sel(&page_wifi.page_3.macRandom.button); + page_wifi.page_3.macRandom.dirty = + (btn_group_get_sel(&page_wifi.page_3.macRandom.button) != !g_setting.wifi.macRandom); + break; } break; case 4: @@ -1135,7 +1177,7 @@ void page_wifi_get_statusbar_text(char *buffer, int size) { snprintf(buffer, size, "WiFi: %s", g_setting.wifi.ssid[WIFI_MODE_AP]); break; case WIFI_MODE_STA: - if (page_wifi_get_real_address()) { + if (!strcmp(page_wifi_get_real_address()->ipAddress, "")) { snprintf(buffer, size, "WiFi: %s", g_setting.wifi.ssid[WIFI_MODE_STA]); } else { snprintf(buffer, size, "WiFi: Searching"); From 2e8d0954692e4bfe166dfbc82fab2f272ee00e1f Mon Sep 17 00:00:00 2001 From: yenon Date: Sun, 7 Apr 2024 15:33:23 +0200 Subject: [PATCH 2/6] Fully working version of fixed mac setting --- .gitignore | 1 + mkapp/app/script/wlan_stop.sh | 1 + src/core/defines.h | 15 ++++--- src/core/settings.c | 52 ++++++++++++++++++++++ src/core/settings.h | 3 ++ src/ui/page_wifi.c | 83 +++++++++++++++++++++++------------ 6 files changed, 120 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 793bb303..09614836 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.cache /build /build_emu linux-4.9 diff --git a/mkapp/app/script/wlan_stop.sh b/mkapp/app/script/wlan_stop.sh index fa8622f7..515ed325 100755 --- a/mkapp/app/script/wlan_stop.sh +++ b/mkapp/app/script/wlan_stop.sh @@ -2,6 +2,7 @@ /mnt/app/app/record/gogglecmd -live quit sleep 1 killall rtspLive +killall wpa_supplicant killall hostapd killall udhcpd killall dropbear diff --git a/src/core/defines.h b/src/core/defines.h index d9dacbf0..d6295708 100644 --- a/src/core/defines.h +++ b/src/core/defines.h @@ -1,8 +1,8 @@ #pragma once -#define DIAL_SENSITIVITY 1 // number of clicks before dial event is triggered +#define DIAL_SENSITIVITY 1 // number of clicks before dial event is triggered #define DIAL_SENSITIVTY_TIMEOUT_MS 1000 // ms -#define CHANNEL_SHOWTIME 30 // must <= 127 +#define CHANNEL_SHOWTIME 30 // must <= 127 #define GPIO_BEEP 131 @@ -20,8 +20,9 @@ #define DEV_SPI_VRX_L "/dev/mtd9" #define DEV_SPI_VA "/dev/mtd10" -#define SELF_TEST_FILE "/mnt/extsd/self_test.txt" -#define NO_DIAL_FILE "/mnt/extsd/no_dial.txt" -#define APP_LOG_FILE "/mnt/extsd/HDZGOGGLE.log" -#define APP_BIN_FILE "/mnt/extsd/HDZGOGGLE" -#define DEVELOP_SCRIPT "/mnt/extsd/develop.sh" +#define SELF_TEST_FILE "/mnt/extsd/self_test.txt" +#define NO_DIAL_FILE "/mnt/extsd/no_dial.txt" +#define APP_LOG_FILE "/mnt/extsd/HDZGOGGLE.log" +#define APP_BIN_FILE "/mnt/extsd/HDZGOGGLE" +#define DEVELOP_SCRIPT "/mnt/extsd/develop.sh" +#define MAC_OVERRIDE_FILE "/mnt/extsd/macaddr.txt" \ No newline at end of file diff --git a/src/core/settings.c b/src/core/settings.c index 76b763a2..a3d292fa 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -1,8 +1,10 @@ #include "settings.h" #include +#include #include #include +#include #include #include @@ -452,6 +454,34 @@ void settings_load(void) { g_setting.wifi.ssh = settings_get_bool("wifi", "ssh", g_setting_defaults.wifi.ssh); g_setting.wifi.macRandom = settings_get_bool("wifi", "macRandom", g_setting_defaults.wifi.macRandom); + ini_gets("wifi", "mac", "", g_setting.wifi.mac, 18, SETTING_INI); + + // Check for override file and apply if it exists. + if (fs_file_exists(MAC_OVERRIDE_FILE)) { + FILE *file = fopen(MAC_OVERRIDE_FILE, "r"); + char new_mac[18]; + if (fgets(new_mac, 18, file) != NULL) { + if (is_valid_mac_address(new_mac)) { + strcpy(g_setting.wifi.mac, new_mac); + ini_puts("wifi", "mac", g_setting.wifi.mac, SETTING_INI); + } + } + fclose(file); + } + + // Generate one random MAC for fixed MAC mode if no one is found in either settings or override file + if (!is_valid_mac_address(g_setting.wifi.mac)) { + srandom(time(NULL)); + sprintf(g_setting.wifi.mac, "%02lx:%02lx:%02lx:%02lx:%02lx:%02lx", + random() & 254, // First byte must be even. + random() & 255, + random() & 255, + random() & 255, + random() & 255, + random() & 255); + ini_puts("wifi", "mac", g_setting.wifi.mac, SETTING_INI); + } + // no dial under video mode g_setting.ease.no_dial = fs_file_exists(NO_DIAL_FILE); @@ -470,3 +500,25 @@ void settings_load(void) { g_setting.storage.logging = log_file_open(APP_LOG_FILE); } } + +static bool is_valid_mac_address(const char mac_address[]) { + for (int i = 0; i < 17; i++) { + // first byte has to be even to be a valid broadcast address + if (i == 1 && (mac_address[i] != '0' && mac_address[i] != '2' && mac_address[i] != '4' && mac_address[i] != '6' && mac_address[i] != '8' && mac_address[i] != 'a' && mac_address[i] != 'A' && mac_address[i] != 'c' && mac_address[i] != 'C' && mac_address[i] != 'e' && mac_address[i] != 'E')) { + return false; + } + if (i % 3 == 2) { + if (mac_address[i] != ':') { + return false; + } + } else { + if (!( + mac_address[i] >= '0' && mac_address[i] <= '9' || + mac_address[i] >= 'A' && mac_address[i] <= 'F' || + mac_address[i] >= 'a' && mac_address[i] <= 'f')) { + return false; + } + } + } + return true; +} \ No newline at end of file diff --git a/src/core/settings.h b/src/core/settings.h index e59a0536..875e5fb2 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -221,6 +221,7 @@ typedef struct { char root_pw[WIFI_SSID_MAX]; bool ssh; bool macRandom; + char mac[18]; } wifi_t; typedef struct { @@ -292,6 +293,8 @@ int settings_put_osd_element_pos_y(const setting_osd_goggle_element_positions_t int settings_put_osd_element_pos_x(const setting_osd_goggle_element_positions_t *pos, char *config_name); int settings_put_osd_element_shown(bool show, char *config_name); +static bool is_valid_mac_address(const char mac_address[]); + #ifdef __cplusplus } #endif diff --git a/src/ui/page_wifi.c b/src/ui/page_wifi.c index 98b0b9f2..c1e5e263 100644 --- a/src/ui/page_wifi.c +++ b/src/ui/page_wifi.c @@ -15,6 +15,7 @@ #include "core/common.hh" #include "core/dvr.h" #include "core/settings.h" +#include "lvgl/src/core/lv_obj.h" #include "ui/page_common.h" #include "ui/ui_attribute.h" #include "ui/ui_keyboard.h" @@ -106,11 +107,6 @@ typedef struct { bool dirty; } page_options_t; -typedef struct { - char ipAddress[16]; - char macAddress[18]; -} wifi_info; - /** * Constants */ @@ -149,10 +145,11 @@ static void page_wifi_update_services() { if ((fp = fopen(WIFI_STA_ON, "w"))) { fprintf(fp, "#!/bin/sh\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_mac.ko\n"); + fprintf(fp, "insmod /mnt/app/ko/xradio_mac.ko\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_core.ko\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_wlan.ko\n"); if (!g_setting.wifi.macRandom) { - // fprintf(fp, "ifconfig wlan0 hw ether 00:28:C7:0A:42:A2"); + fprintf(fp, "ifconfig wlan0 hw ether %s\n", g_setting.wifi.mac); } fprintf(fp, "ifconfig wlan0 up\n"); @@ -326,50 +323,67 @@ static void page_wifi_update_settings() { } /** - * Acquire the actual address in use. + * Acquire the actual IP address in use. */ -static wifi_info *page_wifi_get_real_address() { - wifi_info *info; +static const char *page_wifi_get_real_address() { + const char *address = NULL; int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); - strcpy(info->ipAddress, ""); - strcpy(info->macAddress, ""); - if (fd >= 0) { struct ifreq ifr; strcpy(ifr.ifr_name, "wlan0"); // Try to derive the real ip address if (0 == ioctl(fd, SIOCGIFADDR, &ifr)) { - strcpy(info->ipAddress, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + address = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); } - /* + + close(fd); + } + + return address; +} + +/** + * Acquire the MAC address in use. + */ +static const char *page_wifi_get_real_mac_address() { + const char *address = NULL; + int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + + if (fd >= 0) { + struct ifreq ifr; + strcpy(ifr.ifr_name, "wlan0"); + + char mac[18]; + if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) { - sprintf(info->macAddress, "%02x:%02x:%02x:%02x:%02x:%02x", + sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)ifr.ifr_addr.sa_data[0], (unsigned char)ifr.ifr_addr.sa_data[1], (unsigned char)ifr.ifr_addr.sa_data[2], (unsigned char)ifr.ifr_addr.sa_data[3], (unsigned char)ifr.ifr_addr.sa_data[4], (unsigned char)ifr.ifr_addr.sa_data[5]); - }*/ + } + address = mac; close(fd); } - return info; + return address; } /** - * Updates the all notes on every page. + * Updates the notes on page 1. */ static void page_wifi_update_page_1_notes() { - wifi_info *info = page_wifi_get_real_address(); + const char *address = page_wifi_get_real_address(); if (btn_group_get_sel(&page_wifi.page_1.mode.button) == WIFI_MODE_STA && btn_group_get_sel(&page_wifi.page_2.dhcp.button) == 0 && - strcmp(info->ipAddress, "")) { - strcpy(info->ipAddress, "x.x.x.x"); + address == NULL) { + address = "x.x.x.x"; } static char buffer[1024]; @@ -381,22 +395,28 @@ static void page_wifi_update_page_1_notes() { " 1. Connect to the WiFi network identified above.\n" " 2. Use VLC Player to open a Network Stream:\n\n" " rtsp://%s:8554/hdzero\n\n", - strcmp(info->ipAddress, "") ? info->ipAddress : page_wifi.page_2.ip_addr.text); + address ? address : page_wifi.page_2.ip_addr.text); lv_label_set_text(page_wifi.page_1.note, buffer); } +/** + * Updates the notes on page 3. + */ static void page_wifi_update_page_3_notes() { - wifi_info *info = page_wifi_get_real_address(); + const char *mac = page_wifi_get_real_mac_address(); static char buffer[1024]; snprintf(buffer, sizeof(buffer), "Password Requirements:\n" " Minimum 8 characters, maximum 64 characters.\n\n" - "MAC-Address:\n" - " %s\n\n", - info->macAddress); + "MAC-address:\n" + " %s\n\n" + "You can override the fixed MAC in client mode by creating the file macaddr.txt\n" + "with the new MAC inside on the SD card. Make sure there are no spaces!\n" + "Valid mac addresses start with an even byte.\n\n", + mac); lv_label_set_text(page_wifi.page_3.note, buffer); } @@ -543,6 +563,12 @@ static void page_wifi_update_current_page(int which) { lv_obj_clear_flag(page_wifi.page_3.root_pw.label, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(page_wifi.page_3.root_pw.input, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(page_wifi.page_3.root_pw.status, LV_OBJ_FLAG_HIDDEN); + if (page_wifi.page_1.mode.button.current == WIFI_MODE_STA) { + lv_obj_clear_state(page_wifi.page_3.macRandom.button.label, STATE_DISABLED); + } else { + lv_obj_add_state(page_wifi.page_3.macRandom.button.label, STATE_DISABLED); + lv_obj_clear_flag(pp_wifi.p_arr.panel[3], FLAG_SELECTABLE); + } btn_group_show(&page_wifi.page_3.ssh.button, true); btn_group_show(&page_wifi.page_3.macRandom.button, true); lv_obj_clear_flag(page_wifi.page_3.note, LV_OBJ_FLAG_HIDDEN); @@ -821,7 +847,7 @@ static void page_wifi_on_update(uint32_t delta_ms) { if (g_setting.wifi.enable && (elapsed == -1 || (elapsed += delta_ms) > 300000)) { switch (g_setting.wifi.mode) { case WIFI_MODE_STA: - if (!strcmp(page_wifi_get_real_address()->ipAddress, "")) { + if (page_wifi_get_real_address()) { if (!fs_file_exists("/tmp/hdz_goggle_fw.latest") && !fs_file_exists("/tmp/hdz_vtx_fw.latest")) { system_script(WIFI_DOWNLOAD); @@ -950,6 +976,7 @@ static void page_wifi_on_click(uint8_t key, int sel) { } break; case 2: + btn_group_toggle_sel(&page_wifi.page_3.macRandom.button); page_wifi.page_3.macRandom.dirty = (btn_group_get_sel(&page_wifi.page_3.macRandom.button) != !g_setting.wifi.macRandom); @@ -1177,7 +1204,7 @@ void page_wifi_get_statusbar_text(char *buffer, int size) { snprintf(buffer, size, "WiFi: %s", g_setting.wifi.ssid[WIFI_MODE_AP]); break; case WIFI_MODE_STA: - if (!strcmp(page_wifi_get_real_address()->ipAddress, "")) { + if (page_wifi_get_real_address()) { snprintf(buffer, size, "WiFi: %s", g_setting.wifi.ssid[WIFI_MODE_STA]); } else { snprintf(buffer, size, "WiFi: Searching"); From 61e168c841ab8a6c5144465ad56d8d7943d262eb Mon Sep 17 00:00:00 2001 From: yenon Date: Sun, 7 Apr 2024 15:52:17 +0200 Subject: [PATCH 3/6] Update settings.c Cleaned up error in comment, we want to avoid broadcast and multicast addresses. --- src/core/settings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/settings.c b/src/core/settings.c index a3d292fa..1897ea2a 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -503,7 +503,7 @@ void settings_load(void) { static bool is_valid_mac_address(const char mac_address[]) { for (int i = 0; i < 17; i++) { - // first byte has to be even to be a valid broadcast address + // first byte has to be even to be a valid MAC address for our usecase if (i == 1 && (mac_address[i] != '0' && mac_address[i] != '2' && mac_address[i] != '4' && mac_address[i] != '6' && mac_address[i] != '8' && mac_address[i] != 'a' && mac_address[i] != 'A' && mac_address[i] != 'c' && mac_address[i] != 'C' && mac_address[i] != 'e' && mac_address[i] != 'E')) { return false; } @@ -521,4 +521,4 @@ static bool is_valid_mac_address(const char mac_address[]) { } } return true; -} \ No newline at end of file +} From 318ff2f763eb6a7c218d38e1f183424097033f76 Mon Sep 17 00:00:00 2001 From: yenon Date: Fri, 26 Apr 2024 17:42:03 +0200 Subject: [PATCH 4/6] Removed random mac setting, mac will be used for clientid aswell, refactored code into page_wifi.c --- src/core/settings.c | 55 +-------------------------- src/core/settings.h | 2 - src/ui/page_wifi.c | 91 +++++++++++++++++++++++++++++---------------- 3 files changed, 60 insertions(+), 88 deletions(-) diff --git a/src/core/settings.c b/src/core/settings.c index 1897ea2a..09063213 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -200,7 +200,6 @@ const setting_t g_setting_defaults = { .wifi = { .enable = false, .mode = 0, - .clientid = {""}, .ssid = {"HDZero", "MySSID"}, .passwd = {"divimath", "MyPassword"}, .dhcp = true, @@ -211,7 +210,6 @@ const setting_t g_setting_defaults = { .rf_channel = 6, .root_pw = "divimath", .ssh = false, - .macRandom = true, }, .storage = { .logging = false, @@ -439,7 +437,6 @@ void settings_load(void) { // wifi g_setting.wifi.enable = settings_get_bool("wifi", "enable", g_setting_defaults.wifi.enable); g_setting.wifi.mode = ini_getl("wifi", "mode", g_setting_defaults.wifi.mode, SETTING_INI); - ini_gets("wifi", "clientid", g_setting_defaults.wifi.clientid, g_setting.wifi.clientid, WIFI_CLIENTID_MAX, SETTING_INI); ini_gets("wifi", "ap_ssid", g_setting_defaults.wifi.ssid[0], g_setting.wifi.ssid[0], WIFI_SSID_MAX, SETTING_INI); ini_gets("wifi", "ap_passwd", g_setting_defaults.wifi.passwd[0], g_setting.wifi.passwd[0], WIFI_PASSWD_MAX, SETTING_INI); ini_gets("wifi", "sta_ssid", g_setting_defaults.wifi.ssid[1], g_setting.wifi.ssid[1], WIFI_SSID_MAX, SETTING_INI); @@ -452,36 +449,8 @@ void settings_load(void) { g_setting.wifi.rf_channel = ini_getl("wifi", "rf_channel", g_setting_defaults.wifi.rf_channel, SETTING_INI); ini_gets("wifi", "root_pw", g_setting_defaults.wifi.root_pw, g_setting.wifi.root_pw, WIFI_PASSWD_MAX, SETTING_INI); g_setting.wifi.ssh = settings_get_bool("wifi", "ssh", g_setting_defaults.wifi.ssh); - g_setting.wifi.macRandom = settings_get_bool("wifi", "macRandom", g_setting_defaults.wifi.macRandom); - ini_gets("wifi", "mac", "", g_setting.wifi.mac, 18, SETTING_INI); - // Check for override file and apply if it exists. - if (fs_file_exists(MAC_OVERRIDE_FILE)) { - FILE *file = fopen(MAC_OVERRIDE_FILE, "r"); - char new_mac[18]; - if (fgets(new_mac, 18, file) != NULL) { - if (is_valid_mac_address(new_mac)) { - strcpy(g_setting.wifi.mac, new_mac); - ini_puts("wifi", "mac", g_setting.wifi.mac, SETTING_INI); - } - } - fclose(file); - } - - // Generate one random MAC for fixed MAC mode if no one is found in either settings or override file - if (!is_valid_mac_address(g_setting.wifi.mac)) { - srandom(time(NULL)); - sprintf(g_setting.wifi.mac, "%02lx:%02lx:%02lx:%02lx:%02lx:%02lx", - random() & 254, // First byte must be even. - random() & 255, - random() & 255, - random() & 255, - random() & 255, - random() & 255); - ini_puts("wifi", "mac", g_setting.wifi.mac, SETTING_INI); - } - // no dial under video mode g_setting.ease.no_dial = fs_file_exists(NO_DIAL_FILE); @@ -499,26 +468,4 @@ void settings_load(void) { unlink(APP_LOG_FILE); g_setting.storage.logging = log_file_open(APP_LOG_FILE); } -} - -static bool is_valid_mac_address(const char mac_address[]) { - for (int i = 0; i < 17; i++) { - // first byte has to be even to be a valid MAC address for our usecase - if (i == 1 && (mac_address[i] != '0' && mac_address[i] != '2' && mac_address[i] != '4' && mac_address[i] != '6' && mac_address[i] != '8' && mac_address[i] != 'a' && mac_address[i] != 'A' && mac_address[i] != 'c' && mac_address[i] != 'C' && mac_address[i] != 'e' && mac_address[i] != 'E')) { - return false; - } - if (i % 3 == 2) { - if (mac_address[i] != ':') { - return false; - } - } else { - if (!( - mac_address[i] >= '0' && mac_address[i] <= '9' || - mac_address[i] >= 'A' && mac_address[i] <= 'F' || - mac_address[i] >= 'a' && mac_address[i] <= 'f')) { - return false; - } - } - } - return true; -} +} \ No newline at end of file diff --git a/src/core/settings.h b/src/core/settings.h index 875e5fb2..e52cd919 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -209,7 +209,6 @@ enum { typedef struct { bool enable; uint8_t mode; // 0 == WIFI_MODE_AP, 1 == WIFI_MODE_STA - char clientid[WIFI_CLIENTID_MAX]; char ssid[WIFI_MODE_COUNT][WIFI_SSID_MAX]; char passwd[WIFI_MODE_COUNT][WIFI_PASSWD_MAX]; bool dhcp; @@ -220,7 +219,6 @@ typedef struct { uint8_t rf_channel; char root_pw[WIFI_SSID_MAX]; bool ssh; - bool macRandom; char mac[18]; } wifi_t; diff --git a/src/ui/page_wifi.c b/src/ui/page_wifi.c index c1e5e263..1b1f69d9 100644 --- a/src/ui/page_wifi.c +++ b/src/ui/page_wifi.c @@ -93,7 +93,6 @@ typedef struct { root_pw_t root_pw; button_t ssh; lv_obj_t *note; - button_t macRandom; int row_count; } page_3_t; @@ -148,9 +147,7 @@ static void page_wifi_update_services() { fprintf(fp, "insmod /mnt/app/ko/xradio_mac.ko\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_core.ko\n"); fprintf(fp, "insmod /mnt/app/ko/xradio_wlan.ko\n"); - if (!g_setting.wifi.macRandom) { - fprintf(fp, "ifconfig wlan0 hw ether %s\n", g_setting.wifi.mac); - } + fprintf(fp, "ifconfig wlan0 hw ether %s\n", g_setting.wifi.mac); fprintf(fp, "ifconfig wlan0 up\n"); @@ -158,7 +155,7 @@ static void page_wifi_update_services() { fprintf(fp, "udhcpc -x hostname:%s -x 0x3d:%s -r %s -i wlan0 -b&\n", g_setting.wifi.ssid[WIFI_MODE_AP], - g_setting.wifi.clientid, + g_setting.wifi.mac, g_setting.wifi.ip_addr); } @@ -266,6 +263,50 @@ static void page_wifi_mask_password(lv_obj_t *obj, int size) { lv_label_set_text(obj, buffer); } +static bool page_wifi_is_valid_mac_address(const char mac_address[]) { + for (int i = 0; i < 17; i++) { + // first byte has to be even to be a valid broadcast address + if (i == 1 && (mac_address[i] != '0' && mac_address[i] != '2' && mac_address[i] != '4' && mac_address[i] != '6' && mac_address[i] != '8' && mac_address[i] != 'a' && mac_address[i] != 'A' && mac_address[i] != 'c' && mac_address[i] != 'C' && mac_address[i] != 'e' && mac_address[i] != 'E')) { + return false; + } + if (i % 3 == 2) { + if (mac_address[i] != ':') { + return false; + } + } else { + if (!( + mac_address[i] >= '0' && mac_address[i] <= '9' || + mac_address[i] >= 'A' && mac_address[i] <= 'F' || + mac_address[i] >= 'a' && mac_address[i] <= 'f')) { + return false; + } + } + } + return true; +} + +static void page_wifi_generate_mac_address(char *mac_address) { + srandom(time(NULL)); + sprintf(mac_address, "%02lx:%02lx:%02lx:%02lx:%02lx:%02lx", + random() & 254, // First byte must be even. + random() & 255, + random() & 255, + random() & 255, + random() & 255, + random() & 255); +} + +static void page_wifi_read_mac_file(char *mac_address) { + FILE *file = fopen(MAC_OVERRIDE_FILE, "r"); + char read_mac[18]; + if (fgets(read_mac, 18, file) != NULL) { + if (page_wifi_is_valid_mac_address(read_mac)) { + strcpy(mac_address, read_mac); + } + } + fclose(file); +} + /** * Update settings and apply them. */ @@ -274,7 +315,6 @@ static void page_wifi_update_settings() { g_setting.wifi.mode = btn_group_get_sel(&page_wifi.page_1.mode.button); g_setting.wifi.dhcp = btn_group_get_sel(&page_wifi.page_2.dhcp.button) == 0; g_setting.wifi.ssh = btn_group_get_sel(&page_wifi.page_3.ssh.button) == 0; - g_setting.wifi.macRandom = btn_group_get_sel(&page_wifi.page_3.macRandom.button) == 0; snprintf(g_setting.wifi.ssid[g_setting.wifi.mode], WIFI_SSID_MAX, "%s", page_wifi.page_1.ssid.text[g_setting.wifi.mode]); snprintf(g_setting.wifi.passwd[g_setting.wifi.mode], WIFI_PASSWD_MAX, "%s", page_wifi.page_1.passwd.text[g_setting.wifi.mode]); @@ -286,9 +326,16 @@ static void page_wifi_update_settings() { snprintf(g_setting.wifi.root_pw, WIFI_PASSWD_MAX, "%s", page_wifi.page_3.root_pw.text); - if (0 == strlen(g_setting.wifi.clientid)) { - page_wifi_generate_clientid(g_setting.wifi.clientid, WIFI_CLIENTID_MAX); - ini_puts("wifi", "clientid", g_setting.wifi.clientid, SETTING_INI); + // Generate one random MAC if none is set or if it is invalid. + if (0 == strlen(g_setting.wifi.mac) || !page_wifi_is_valid_mac_address(g_setting.wifi.mac)) { + page_wifi_generate_mac_address(g_setting.wifi.mac); + ini_puts("wifi", "mac", g_setting.wifi.mac, SETTING_INI); + } + + // Check for override file and apply if it exists. + if (fs_file_exists(MAC_OVERRIDE_FILE)) { + page_wifi_read_mac_file(g_setting.wifi.mac); + ini_puts("wifi", "mac", g_setting.wifi.mac, SETTING_INI); } settings_put_bool("wifi", "enable", g_setting.wifi.enable); @@ -305,7 +352,6 @@ static void page_wifi_update_settings() { ini_putl("wifi", "rf_channel", g_setting.wifi.rf_channel, SETTING_INI); ini_puts("wifi", "root_pw", g_setting.wifi.root_pw, SETTING_INI); settings_put_bool("wifi", "ssh", g_setting.wifi.ssh); - settings_put_bool("wifi", "macRandom", g_setting.wifi.macRandom); // Prepare WiFi interfaces system_script(WIFI_OFF); @@ -490,7 +536,6 @@ static void page_wifi_update_current_page(int which) { lv_obj_add_flag(page_wifi.page_3.root_pw.input, LV_OBJ_FLAG_HIDDEN); lv_obj_add_flag(page_wifi.page_3.root_pw.status, LV_OBJ_FLAG_HIDDEN); btn_group_show(&page_wifi.page_3.ssh.button, false); - btn_group_show(&page_wifi.page_3.macRandom.button, false); lv_obj_add_flag(page_wifi.page_3.note, LV_OBJ_FLAG_HIDDEN); switch (which) { @@ -563,14 +608,7 @@ static void page_wifi_update_current_page(int which) { lv_obj_clear_flag(page_wifi.page_3.root_pw.label, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(page_wifi.page_3.root_pw.input, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(page_wifi.page_3.root_pw.status, LV_OBJ_FLAG_HIDDEN); - if (page_wifi.page_1.mode.button.current == WIFI_MODE_STA) { - lv_obj_clear_state(page_wifi.page_3.macRandom.button.label, STATE_DISABLED); - } else { - lv_obj_add_state(page_wifi.page_3.macRandom.button.label, STATE_DISABLED); - lv_obj_clear_flag(pp_wifi.p_arr.panel[3], FLAG_SELECTABLE); - } btn_group_show(&page_wifi.page_3.ssh.button, true); - btn_group_show(&page_wifi.page_3.macRandom.button, true); lv_obj_clear_flag(page_wifi.page_3.note, LV_OBJ_FLAG_HIDDEN); page_wifi_update_page_3_notes(); break; @@ -601,8 +639,7 @@ static void page_wifi_dirty_flag_reset() { page_wifi.page_2.dns.dirty = page_wifi.page_2.rf_channel.dirty = page_wifi.page_3.root_pw.dirty = - page_wifi.page_3.ssh.dirty = - page_wifi.page_3.macRandom.dirty = false; + page_wifi.page_3.ssh.dirty = false; } /** @@ -659,8 +696,7 @@ static void page_wifi_update_dirty_flag() { page_wifi.page_2.dns.dirty || page_wifi.page_2.rf_channel.dirty || page_wifi.page_3.root_pw.dirty || - page_wifi.page_3.ssh.dirty || - page_wifi.page_3.macRandom.dirty; + page_wifi.page_3.ssh.dirty; if (page_wifi.dirty) { if (!page_wifi_apply_settings_pending_timer) { @@ -741,9 +777,6 @@ static void page_wifi_create_page_3(lv_obj_t *parent) { create_btn_group_item(&page_wifi.page_3.ssh.button, parent, 2, "SSH", "On", "Off", "", "", 2); btn_group_set_sel(&page_wifi.page_3.ssh.button, !g_setting.wifi.ssh); - create_btn_group_item(&page_wifi.page_3.macRandom.button, parent, 2, "MAC", "Random", "Fixed", "", "", 3); - btn_group_set_sel(&page_wifi.page_3.macRandom.button, !g_setting.wifi.macRandom); - page_wifi.page_3.note = lv_label_create(parent); lv_obj_set_style_text_font(page_wifi.page_3.note, &lv_font_montserrat_16, 0); lv_obj_set_style_text_align(page_wifi.page_3.note, LV_TEXT_ALIGN_LEFT, 0); @@ -753,7 +786,7 @@ static void page_wifi_create_page_3(lv_obj_t *parent) { lv_obj_set_grid_cell(page_wifi.page_3.note, LV_GRID_ALIGN_START, 1, 4, LV_GRID_ALIGN_START, 7, 2); page_wifi_update_page_3_notes(); - page_wifi.page_3.row_count = 4; + page_wifi.page_3.row_count = 3; } /** @@ -975,12 +1008,6 @@ static void page_wifi_on_click(uint8_t key, int sel) { keyboard_press(); } break; - case 2: - - btn_group_toggle_sel(&page_wifi.page_3.macRandom.button); - page_wifi.page_3.macRandom.dirty = - (btn_group_get_sel(&page_wifi.page_3.macRandom.button) != !g_setting.wifi.macRandom); - break; } break; case 4: From c0211949cdf4737afa07a5e0c6a2de4337cfa807 Mon Sep 17 00:00:00 2001 From: yenon Date: Fri, 26 Apr 2024 17:53:07 +0200 Subject: [PATCH 5/6] Merged minor comment difference --- src/ui/page_wifi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/page_wifi.c b/src/ui/page_wifi.c index 1b1f69d9..88445e4d 100644 --- a/src/ui/page_wifi.c +++ b/src/ui/page_wifi.c @@ -265,7 +265,7 @@ static void page_wifi_mask_password(lv_obj_t *obj, int size) { static bool page_wifi_is_valid_mac_address(const char mac_address[]) { for (int i = 0; i < 17; i++) { - // first byte has to be even to be a valid broadcast address + // first byte has to be even to be a valid MAC address for our usecase if (i == 1 && (mac_address[i] != '0' && mac_address[i] != '2' && mac_address[i] != '4' && mac_address[i] != '6' && mac_address[i] != '8' && mac_address[i] != 'a' && mac_address[i] != 'A' && mac_address[i] != 'c' && mac_address[i] != 'C' && mac_address[i] != 'e' && mac_address[i] != 'E')) { return false; } From 856e4ed0e980508a09f502fd0703ab0bf1a06021 Mon Sep 17 00:00:00 2001 From: yenon Date: Fri, 26 Apr 2024 18:04:03 +0200 Subject: [PATCH 6/6] Cleanup imports, removed \n --- src/core/settings.c | 5 ----- src/ui/page_wifi.c | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/core/settings.c b/src/core/settings.c index 09063213..03118b8e 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -1,16 +1,11 @@ #include "settings.h" -#include -#include -#include #include -#include #include #include #include -#include "core/self_test.h" #include "ui/page_common.h" #include "util/filesystem.h" #include "util/system.h" diff --git a/src/ui/page_wifi.c b/src/ui/page_wifi.c index 88445e4d..dc181d7a 100644 --- a/src/ui/page_wifi.c +++ b/src/ui/page_wifi.c @@ -1,7 +1,6 @@ #include "page_wifi.h" #include -#include #include #include #include @@ -17,7 +16,6 @@ #include "core/settings.h" #include "lvgl/src/core/lv_obj.h" #include "ui/page_common.h" -#include "ui/ui_attribute.h" #include "ui/ui_keyboard.h" #include "ui/ui_style.h" #include "util/filesystem.h" @@ -1241,4 +1239,4 @@ void page_wifi_get_statusbar_text(char *buffer, int size) { } else { snprintf(buffer, size, "WiFi: Off"); } -} +} \ No newline at end of file