diff options
Diffstat (limited to 'main/tollgate_main.c')
| -rw-r--r-- | main/tollgate_main.c | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/main/tollgate_main.c b/main/tollgate_main.c new file mode 100644 index 0000000..9eba61f --- /dev/null +++ b/main/tollgate_main.c | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | #include <string.h> | ||
| 2 | #include "freertos/FreeRTOS.h" | ||
| 3 | #include "freertos/task.h" | ||
| 4 | #include "freertos/event_groups.h" | ||
| 5 | #include "esp_wifi.h" | ||
| 6 | #include "esp_event.h" | ||
| 7 | #include "esp_log.h" | ||
| 8 | #include "nvs_flash.h" | ||
| 9 | #include "esp_netif.h" | ||
| 10 | #include "lwip/netif.h" | ||
| 11 | #include "lwip/dns.h" | ||
| 12 | #include "dhcpserver/dhcpserver.h" | ||
| 13 | #include "config.h" | ||
| 14 | #include "dns_server.h" | ||
| 15 | #include "captive_portal.h" | ||
| 16 | #include "firewall.h" | ||
| 17 | |||
| 18 | #define MAX_STA_RETRY 5 | ||
| 19 | #define AP_IP_ADDR "192.168.4.1" | ||
| 20 | #define AP_SUBNET "255.255.255.0" | ||
| 21 | |||
| 22 | static const char *TAG = "tollgate_main"; | ||
| 23 | |||
| 24 | static EventGroupHandle_t s_wifi_event_group; | ||
| 25 | static const int WIFI_CONNECTED_BIT = BIT0; | ||
| 26 | |||
| 27 | static esp_netif_t *s_sta_netif = NULL; | ||
| 28 | static esp_netif_t *s_ap_netif = NULL; | ||
| 29 | static int s_retry_count = 0; | ||
| 30 | static bool s_services_running = false; | ||
| 31 | static SemaphoreHandle_t s_services_mutex = NULL; | ||
| 32 | |||
| 33 | static void start_services(void); | ||
| 34 | static void stop_services(void); | ||
| 35 | |||
| 36 | static void wifi_event_handler(void *arg, esp_event_base_t event_base, | ||
| 37 | int32_t event_id, void *event_data) | ||
| 38 | { | ||
| 39 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { | ||
| 40 | wifi_config_t wifi_cfg; | ||
| 41 | if (tollgate_config_get_wifi(&wifi_cfg) == ESP_OK) { | ||
| 42 | esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); | ||
| 43 | } | ||
| 44 | esp_wifi_connect(); | ||
| 45 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { | ||
| 46 | s_retry_count++; | ||
| 47 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); | ||
| 48 | if (s_services_running) stop_services(); | ||
| 49 | if (s_retry_count < MAX_STA_RETRY) { | ||
| 50 | esp_wifi_connect(); | ||
| 51 | } else { | ||
| 52 | wifi_config_t wifi_cfg; | ||
| 53 | if (tollgate_config_get_next_wifi(&wifi_cfg) == ESP_OK) { | ||
| 54 | esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); | ||
| 55 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 56 | int idx = cfg->current_network; | ||
| 57 | ESP_LOGI(TAG, "Trying WiFi network %d: %s", idx, cfg->networks[idx].ssid); | ||
| 58 | s_retry_count = 0; | ||
| 59 | esp_wifi_connect(); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) { | ||
| 63 | wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data; | ||
| 64 | ESP_LOGI(TAG, "Station connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 65 | event->mac[0], event->mac[1], event->mac[2], | ||
| 66 | event->mac[3], event->mac[4], event->mac[5]); | ||
| 67 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) { | ||
| 68 | wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; | ||
| 69 | ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 70 | event->mac[0], event->mac[1], event->mac[2], | ||
| 71 | event->mac[3], event->mac[4], event->mac[5]); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | static void ip_event_handler(void *arg, esp_event_base_t event_base, | ||
| 76 | int32_t event_id, void *event_data) | ||
| 77 | { | ||
| 78 | if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { | ||
| 79 | ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; | ||
| 80 | ESP_LOGI(TAG, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip)); | ||
| 81 | s_retry_count = 0; | ||
| 82 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | ||
| 83 | start_services(); | ||
| 84 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { | ||
| 85 | ESP_LOGW(TAG, "Lost IP address"); | ||
| 86 | xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | ||
| 87 | stop_services(); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | static void start_services(void) | ||
| 92 | { | ||
| 93 | if (s_services_mutex) xSemaphoreTake(s_services_mutex, portMAX_DELAY); | ||
| 94 | if (s_services_running) { | ||
| 95 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | esp_netif_get_ip_info(s_ap_netif, &(esp_netif_ip_info_t){0}); | ||
| 100 | esp_netif_ip_info_t ap_ip_info; | ||
| 101 | esp_netif_get_ip_info(s_ap_netif, &ap_ip_info); | ||
| 102 | |||
| 103 | esp_ip4_addr_t upstream_dns; | ||
| 104 | const ip_addr_t *dns_addr = dns_getserver(0); | ||
| 105 | upstream_dns.addr = dns_addr->addr; | ||
| 106 | |||
| 107 | firewall_init(ap_ip_info.ip); | ||
| 108 | |||
| 109 | dns_server_start(ap_ip_info.ip, upstream_dns); | ||
| 110 | captive_portal_start(); | ||
| 111 | |||
| 112 | s_services_running = true; | ||
| 113 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); | ||
| 114 | ESP_LOGI(TAG, "=== TollGate services started ==="); | ||
| 115 | } | ||
| 116 | |||
| 117 | static void stop_services(void) | ||
| 118 | { | ||
| 119 | if (s_services_mutex) xSemaphoreTake(s_services_mutex, portMAX_DELAY); | ||
| 120 | if (!s_services_running) { | ||
| 121 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | captive_portal_stop(); | ||
| 126 | dns_server_stop(); | ||
| 127 | firewall_disable_nat(); | ||
| 128 | firewall_revoke_all(); | ||
| 129 | s_services_running = false; | ||
| 130 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); | ||
| 131 | ESP_LOGI(TAG, "=== TollGate services stopped ==="); | ||
| 132 | } | ||
| 133 | |||
| 134 | static void wifi_create_ap_netif(void) | ||
| 135 | { | ||
| 136 | s_ap_netif = esp_netif_create_default_wifi_ap(); | ||
| 137 | |||
| 138 | esp_netif_ip_info_t ip_info = { | ||
| 139 | .ip.addr = esp_ip4addr_aton(AP_IP_ADDR), | ||
| 140 | .gw.addr = esp_ip4addr_aton(AP_IP_ADDR), | ||
| 141 | .netmask.addr = esp_ip4addr_aton(AP_SUBNET), | ||
| 142 | }; | ||
| 143 | ESP_ERROR_CHECK(esp_netif_dhcps_stop(s_ap_netif)); | ||
| 144 | ESP_ERROR_CHECK(esp_netif_set_ip_info(s_ap_netif, &ip_info)); | ||
| 145 | ESP_ERROR_CHECK(esp_netif_dhcps_start(s_ap_netif)); | ||
| 146 | |||
| 147 | dhcps_offer_t offer_dns = true; | ||
| 148 | esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, | ||
| 149 | &offer_dns, sizeof(offer_dns)); | ||
| 150 | } | ||
| 151 | |||
| 152 | static void wifi_configure_ap(void) | ||
| 153 | { | ||
| 154 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 155 | wifi_config_t ap_config = {0}; | ||
| 156 | strncpy((char *)ap_config.ap.ssid, cfg->ap_ssid, sizeof(ap_config.ap.ssid) - 1); | ||
| 157 | if (strlen(cfg->ap_password) > 0) { | ||
| 158 | strncpy((char *)ap_config.ap.password, cfg->ap_password, sizeof(ap_config.ap.password) - 1); | ||
| 159 | ap_config.ap.authmode = WIFI_AUTH_WPA2_PSK; | ||
| 160 | } else { | ||
| 161 | ap_config.ap.authmode = WIFI_AUTH_OPEN; | ||
| 162 | } | ||
| 163 | ap_config.ap.channel = cfg->ap_channel; | ||
| 164 | ap_config.ap.max_connection = cfg->ap_max_conn; | ||
| 165 | ap_config.ap.ssid_hidden = 0; | ||
| 166 | |||
| 167 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config)); | ||
| 168 | ESP_LOGI(TAG, "AP configured: SSID='%s', channel=%d", cfg->ap_ssid, cfg->ap_channel); | ||
| 169 | } | ||
| 170 | |||
| 171 | static void wifi_init_sta(void) | ||
| 172 | { | ||
| 173 | s_sta_netif = esp_netif_create_default_wifi_sta(); | ||
| 174 | } | ||
| 175 | |||
| 176 | void app_main(void) | ||
| 177 | { | ||
| 178 | ESP_LOGI(TAG, "=== TollGate ESP32 Starting ==="); | ||
| 179 | |||
| 180 | esp_err_t ret = nvs_flash_init(); | ||
| 181 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { | ||
| 182 | ESP_ERROR_CHECK(nvs_flash_erase()); | ||
| 183 | ret = nvs_flash_init(); | ||
| 184 | } | ||
| 185 | ESP_ERROR_CHECK(ret); | ||
| 186 | |||
| 187 | ESP_ERROR_CHECK(tollgate_config_init()); | ||
| 188 | ESP_ERROR_CHECK(esp_netif_init()); | ||
| 189 | ESP_ERROR_CHECK(esp_event_loop_create_default()); | ||
| 190 | |||
| 191 | s_wifi_event_group = xEventGroupCreate(); | ||
| 192 | s_services_mutex = xSemaphoreCreateMutex(); | ||
| 193 | |||
| 194 | wifi_init_sta(); | ||
| 195 | wifi_create_ap_netif(); | ||
| 196 | |||
| 197 | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
| 198 | ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | ||
| 199 | |||
| 200 | ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, | ||
| 201 | &wifi_event_handler, NULL, NULL)); | ||
| 202 | ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, | ||
| 203 | &ip_event_handler, NULL, NULL)); | ||
| 204 | ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_LOST_IP, | ||
| 205 | &ip_event_handler, NULL, NULL)); | ||
| 206 | |||
| 207 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); | ||
| 208 | |||
| 209 | wifi_configure_ap(); | ||
| 210 | |||
| 211 | wifi_config_t sta_config; | ||
| 212 | if (tollgate_config_get_wifi(&sta_config) == ESP_OK) { | ||
| 213 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config)); | ||
| 214 | const tollgate_config_t *tcfg = tollgate_config_get(); | ||
| 215 | ESP_LOGI(TAG, "STA configured for SSID: %s", tcfg->networks[tcfg->current_network].ssid); | ||
| 216 | } | ||
| 217 | |||
| 218 | ESP_ERROR_CHECK(esp_wifi_start()); | ||
| 219 | |||
| 220 | ESP_LOGI(TAG, "WiFi AP+STA started, waiting for connection..."); | ||
| 221 | } | ||