upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/tollgate_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/tollgate_main.c')
-rw-r--r--main/tollgate_main.c54
1 files changed, 45 insertions, 9 deletions
diff --git a/main/tollgate_main.c b/main/tollgate_main.c
index 4741765..f062cb6 100644
--- a/main/tollgate_main.c
+++ b/main/tollgate_main.c
@@ -27,6 +27,8 @@
27#include "local_relay.h" 27#include "local_relay.h"
28#include "relay_selector.h" 28#include "relay_selector.h"
29#include "sync_manager.h" 29#include "sync_manager.h"
30#include "beacon_price.h"
31#include "market.h"
30 32
31#define MAX_STA_RETRY 5 33#define MAX_STA_RETRY 5
32static const char *TAG = "tollgate_main"; 34static const char *TAG = "tollgate_main";
@@ -38,6 +40,8 @@ static esp_netif_t *s_sta_netif = NULL;
38static esp_netif_t *s_ap_netif = NULL; 40static esp_netif_t *s_ap_netif = NULL;
39static int s_retry_count = 0; 41static int s_retry_count = 0;
40static bool s_services_running = false; 42static bool s_services_running = false;
43static bool s_ap_services_running = false;
44static bool s_sta_connecting = false;
41static SemaphoreHandle_t s_services_mutex = NULL; 45static SemaphoreHandle_t s_services_mutex = NULL;
42static char s_ap_ip_str[16] = "10.0.0.1"; 46static char s_ap_ip_str[16] = "10.0.0.1";
43 47
@@ -46,23 +50,42 @@ static sync_manager_t s_sync_manager;
46 50
47static void start_services(void); 51static void start_services(void);
48static void stop_services(void); 52static void stop_services(void);
53static void start_ap_services(void);
54
55static void start_ap_services(void)
56{
57 if (s_ap_services_running) return;
58
59 tollgate_api_start();
60 beacon_price_start();
61 market_init();
62
63 s_ap_services_running = true;
64 ESP_LOGI(TAG, "=== AP-only services started (no STA) ===");
65}
49 66
50static void wifi_event_handler(void *arg, esp_event_base_t event_base, 67static void wifi_event_handler(void *arg, esp_event_base_t event_base,
51 int32_t event_id, void *event_data) 68 int32_t event_id, void *event_data)
52{ 69{
53 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { 70 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
54 wifi_config_t wifi_cfg; 71 if (!s_sta_connecting) {
55 if (tollgate_config_get_wifi(&wifi_cfg) == ESP_OK) { 72 wifi_config_t wifi_cfg;
56 esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); 73 if (tollgate_config_get_wifi(&wifi_cfg) == ESP_OK) {
74 esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
75 }
76 s_sta_connecting = true;
77 esp_wifi_connect();
57 } 78 }
58 esp_wifi_connect();
59 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { 79 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
80 wifi_event_sta_disconnected_t *disc = (wifi_event_sta_disconnected_t *)event_data;
60 s_retry_count++; 81 s_retry_count++;
61 ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); 82 s_sta_connecting = false;
83 ESP_LOGW(TAG, "WiFi disconnected, reason=%d, retry %d/%d", disc->reason, s_retry_count, MAX_STA_RETRY);
62 tollgate_client_on_sta_disconnected(); 84 tollgate_client_on_sta_disconnected();
63 if (s_services_running) stop_services(); 85 if (s_services_running) stop_services();
64 if (s_retry_count < MAX_STA_RETRY) { 86 if (s_retry_count < MAX_STA_RETRY) {
65 vTaskDelay(pdMS_TO_TICKS(2000)); 87 vTaskDelay(pdMS_TO_TICKS(2000));
88 s_sta_connecting = true;
66 esp_wifi_connect(); 89 esp_wifi_connect();
67 } else { 90 } else {
68 wifi_config_t wifi_cfg; 91 wifi_config_t wifi_cfg;
@@ -72,7 +95,11 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base,
72 int idx = cfg->current_network; 95 int idx = cfg->current_network;
73 ESP_LOGI(TAG, "Trying WiFi network %d: %s", idx, cfg->networks[idx].ssid); 96 ESP_LOGI(TAG, "Trying WiFi network %d: %s", idx, cfg->networks[idx].ssid);
74 s_retry_count = 0; 97 s_retry_count = 0;
98 vTaskDelay(pdMS_TO_TICKS(2000));
99 s_sta_connecting = true;
75 esp_wifi_connect(); 100 esp_wifi_connect();
101 } else {
102 ESP_LOGI(TAG, "All WiFi networks exhausted, STA stopped (market scans active)");
76 } 103 }
77 } 104 }
78 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) { 105 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
@@ -85,6 +112,8 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base,
85 ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", 112 ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x",
86 event->mac[0], event->mac[1], event->mac[2], 113 event->mac[0], event->mac[1], event->mac[2],
87 event->mac[3], event->mac[4], event->mac[5]); 114 event->mac[3], event->mac[4], event->mac[5]);
115 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) {
116 start_ap_services();
88 } 117 }
89} 118}
90 119
@@ -163,7 +192,11 @@ static void start_services(void)
163 192
164 dns_server_start(ap_ip_info.ip, upstream_dns); 193 dns_server_start(ap_ip_info.ip, upstream_dns);
165 captive_portal_start(cfg->ap_ip_str); 194 captive_portal_start(cfg->ap_ip_str);
166 tollgate_api_start(); 195 if (!s_ap_services_running) {
196 tollgate_api_start();
197 beacon_price_start();
198 market_init();
199 }
167 200
168 relay_selector_init(&s_relay_selector); 201 relay_selector_init(&s_relay_selector);
169 relay_selector_seed_from_config(&s_relay_selector); 202 relay_selector_seed_from_config(&s_relay_selector);
@@ -198,7 +231,10 @@ static void stop_services(void)
198 } 231 }
199 232
200 captive_portal_stop(); 233 captive_portal_stop();
201 tollgate_api_stop(); 234 if (!s_ap_services_running) {
235 tollgate_api_stop();
236 beacon_price_stop();
237 }
202 dns_server_stop(); 238 dns_server_stop();
203 cvm_server_stop(); 239 cvm_server_stop();
204 sync_manager_stop(&s_sync_manager); 240 sync_manager_stop(&s_sync_manager);
@@ -321,8 +357,7 @@ void app_main(void)
321 ESP_LOGI(TAG, "STA configured for SSID: %s", tcfg2->networks[tcfg2->current_network].ssid); 357 ESP_LOGI(TAG, "STA configured for SSID: %s", tcfg2->networks[tcfg2->current_network].ssid);
322 } 358 }
323 359
324 ESP_ERROR_CHECK(esp_wifi_set_country_code("DE", false)); 360 ESP_ERROR_CHECK(esp_wifi_set_country_code("DE", true));
325 ESP_LOGI(TAG, "WiFi country code set to DE (EU regulatory domain)");
326 361
327 ESP_ERROR_CHECK(esp_wifi_start()); 362 ESP_ERROR_CHECK(esp_wifi_start());
328 363
@@ -341,5 +376,6 @@ void app_main(void)
341 session_tick(); 376 session_tick();
342 tollgate_client_tick(); 377 tollgate_client_tick();
343 lightning_payout_tick(); 378 lightning_payout_tick();
379 market_tick();
344 } 380 }
345} 381}