|
| 1 | +#include "about.h" |
| 2 | +#include "bsp/input.h" |
| 3 | +#include "common/display.h" |
| 4 | +#include "common/theme.h" |
| 5 | +#include "esp_netif_types.h" |
| 6 | +#include "esp_wifi.h" |
| 7 | +#include "esp_wifi_types_generic.h" |
| 8 | +#include "freertos/idf_additions.h" |
| 9 | +#include "gui_style.h" |
| 10 | +#include "icons.h" |
| 11 | +#include "lwipopts.h" |
| 12 | +#include "menu/message_dialog.h" |
| 13 | +#include "pax_fonts.h" |
| 14 | +#include "pax_gfx.h" |
| 15 | +#include "pax_matrix.h" |
| 16 | +#include "pax_text.h" |
| 17 | +#include "pax_types.h" |
| 18 | +#include "wifi_connection.h" |
| 19 | + |
| 20 | +#if defined(CONFIG_BSP_TARGET_TANMATSU) || defined(CONFIG_BSP_TARGET_KONSOOL) || \ |
| 21 | + defined(CONFIG_BSP_TARGET_HACKERHOTEL_2026) |
| 22 | +#define FOOTER_LEFT ((gui_element_icontext_t[]){{get_icon(ICON_ESC), "/"}, {get_icon(ICON_F1), "Back"}}), 2 |
| 23 | +#define FOOTER_RIGHT NULL, 0 |
| 24 | +#elif defined(CONFIG_BSP_TARGET_MCH2022) |
| 25 | +#define FOOTER_LEFT NULL, 0 |
| 26 | +#define FOOTER_RIGHT NULL, 0 |
| 27 | +#else |
| 28 | +#define FOOTER_LEFT NULL, 0 |
| 29 | +#define FOOTER_RIGHT NULL, 0 |
| 30 | +#endif |
| 31 | + |
| 32 | +extern bool wifi_stack_get_initialized(void); |
| 33 | + |
| 34 | +static wifi_ap_record_t connected_ap = {0}; |
| 35 | + |
| 36 | +static bool get_connection_info(void) { |
| 37 | + bool radio_initialized = wifi_stack_get_initialized(); |
| 38 | + wifi_mode_t mode = WIFI_MODE_NULL; |
| 39 | + if (radio_initialized && esp_wifi_get_mode(&mode) == ESP_OK) { |
| 40 | + if (mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) { |
| 41 | + if (wifi_connection_is_connected() && esp_wifi_sta_get_ap_info(&connected_ap) == ESP_OK) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +static const char* ip6_addr_type_to_string(esp_ip6_addr_type_t type) { |
| 50 | + switch (type) { |
| 51 | + case ESP_IP6_ADDR_IS_UNKNOWN: |
| 52 | + return "Unknown"; |
| 53 | + case ESP_IP6_ADDR_IS_GLOBAL: |
| 54 | + return "Global"; |
| 55 | + case ESP_IP6_ADDR_IS_LINK_LOCAL: |
| 56 | + return "Link Local"; |
| 57 | + case ESP_IP6_ADDR_IS_SITE_LOCAL: |
| 58 | + return "Site Local"; |
| 59 | + case ESP_IP6_ADDR_IS_UNIQUE_LOCAL: |
| 60 | + return "Unique Local"; |
| 61 | + case ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6: |
| 62 | + return "IPv4 Mapped IPv6"; |
| 63 | + default: |
| 64 | + return "Invalid Type"; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static void render(bool partial, bool icons) { |
| 69 | + gui_theme_t* theme = get_theme(); |
| 70 | + pax_buf_t* buffer = display_get_buffer(); |
| 71 | + |
| 72 | + char message_buffer[128] = {0}; |
| 73 | + |
| 74 | + int header_height = theme->header.height + (theme->header.vertical_margin * 2); |
| 75 | + int footer_height = theme->footer.height + (theme->footer.vertical_margin * 2); |
| 76 | + |
| 77 | + pax_vec2_t position = { |
| 78 | + .x0 = theme->menu.horizontal_margin + theme->menu.horizontal_padding, |
| 79 | + .y0 = header_height + theme->menu.vertical_margin + theme->menu.vertical_padding, |
| 80 | + .x1 = pax_buf_get_width(buffer) - theme->menu.horizontal_margin - theme->menu.horizontal_padding, |
| 81 | + .y1 = pax_buf_get_height(buffer) - footer_height - theme->menu.vertical_margin - theme->menu.vertical_padding, |
| 82 | + }; |
| 83 | + |
| 84 | + if (!partial || icons) { |
| 85 | + render_base_screen_statusbar(buffer, theme, !partial, !partial || icons, !partial, |
| 86 | + ((gui_element_icontext_t[]){{get_icon(ICON_INFO), "WiFi information"}}), 1, |
| 87 | + FOOTER_LEFT, FOOTER_RIGHT); |
| 88 | + } |
| 89 | + if (!partial) { |
| 90 | + bool connected = get_connection_info(); |
| 91 | + if (!connected) { |
| 92 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 93 | + position.y0 + 18 * 0, "Not connected to a network"); |
| 94 | + } else { |
| 95 | + esp_netif_ip_info_t ip_info; |
| 96 | + esp_netif_get_ip_info(wifi_get_netif(), &ip_info); |
| 97 | + |
| 98 | + esp_ip6_addr_t ip6[LWIP_IPV6_NUM_ADDRESSES]; |
| 99 | + int ip6_addrs = esp_netif_get_all_ip6(wifi_get_netif(), ip6); |
| 100 | + |
| 101 | + snprintf(message_buffer, sizeof(message_buffer), "SSID: %s", connected_ap.ssid); |
| 102 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 103 | + position.y0 + 18 * 0, message_buffer); |
| 104 | + snprintf(message_buffer, sizeof(message_buffer), "BSSID: %02x:%02x:%02x:%02x:%02x:%02x", |
| 105 | + connected_ap.bssid[0], connected_ap.bssid[1], connected_ap.bssid[2], connected_ap.bssid[3], |
| 106 | + connected_ap.bssid[4], connected_ap.bssid[5]); |
| 107 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 108 | + position.y0 + 18 * 1, message_buffer); |
| 109 | + snprintf(message_buffer, sizeof(message_buffer), "Channel: %d", connected_ap.primary); |
| 110 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 111 | + position.y0 + 18 * 2, message_buffer); |
| 112 | + snprintf(message_buffer, sizeof(message_buffer), "RSSI: %d dBm", connected_ap.rssi); |
| 113 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 114 | + position.y0 + 18 * 3, message_buffer); |
| 115 | + |
| 116 | + snprintf(message_buffer, sizeof(message_buffer), "IP address: " IPSTR, IP2STR(&ip_info.ip)); |
| 117 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 118 | + position.y0 + 18 * 5, message_buffer); |
| 119 | + snprintf(message_buffer, sizeof(message_buffer), "Gateway: " IPSTR, IP2STR(&ip_info.gw)); |
| 120 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 121 | + position.y0 + 18 * 6, message_buffer); |
| 122 | + snprintf(message_buffer, sizeof(message_buffer), "Netmask: " IPSTR, IP2STR(&ip_info.netmask)); |
| 123 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 124 | + position.y0 + 18 * 7, message_buffer); |
| 125 | + |
| 126 | + if (ip6_addrs > 0) { |
| 127 | + snprintf(message_buffer, sizeof(message_buffer), "IPv6 addresses:"); |
| 128 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 129 | + position.y0 + 18 * 8, message_buffer); |
| 130 | + |
| 131 | + for (int j = 0; j < ip6_addrs; ++j) { |
| 132 | + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j])); |
| 133 | + snprintf(message_buffer, sizeof(message_buffer), "- %s: " IPV6STR, |
| 134 | + ip6_addr_type_to_string(ipv6_type), IPV62STR(ip6[j])); |
| 135 | + pax_draw_text(buffer, theme->palette.color_foreground, theme->menu.text_font, 16, position.x0, |
| 136 | + position.y0 + 18 * (9 + j), message_buffer); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + display_blit_buffer(buffer); |
| 142 | +} |
| 143 | + |
| 144 | +void menu_wifi_info(void) { |
| 145 | + QueueHandle_t input_event_queue = NULL; |
| 146 | + ESP_ERROR_CHECK(bsp_input_get_queue(&input_event_queue)); |
| 147 | + |
| 148 | + render(false, true); |
| 149 | + while (1) { |
| 150 | + bsp_input_event_t event; |
| 151 | + if (xQueueReceive(input_event_queue, &event, pdMS_TO_TICKS(1000)) == pdTRUE) { |
| 152 | + switch (event.type) { |
| 153 | + case INPUT_EVENT_TYPE_NAVIGATION: { |
| 154 | + if (event.args_navigation.state) { |
| 155 | + switch (event.args_navigation.key) { |
| 156 | + case BSP_INPUT_NAVIGATION_KEY_ESC: |
| 157 | + case BSP_INPUT_NAVIGATION_KEY_F1: |
| 158 | + case BSP_INPUT_NAVIGATION_KEY_GAMEPAD_B: |
| 159 | + return; |
| 160 | + default: |
| 161 | + break; |
| 162 | + } |
| 163 | + } |
| 164 | + break; |
| 165 | + } |
| 166 | + default: |
| 167 | + break; |
| 168 | + } |
| 169 | + } else { |
| 170 | + render(true, true); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments