diff options
| author | Your Name <you@example.com> | 2026-05-17 04:21:39 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 04:21:39 +0530 |
| commit | 78dd599277b8e8b2ddc39a4ae710ec91d737272e (patch) | |
| tree | 9fbd89695cede00b8ff3b12ce428e96a2aa70e9b /main/tollgate_main.c | |
| parent | b0d7394e089f00a9ffa67a2b33a502e47b778a93 (diff) | |
Phase 4: TollGate client detection + auto-payment
- New tollgate_client.c/h: detect upstream TollGate (kind=10021),
auto-pay via nucula wallet, session monitoring with 20% renewal
- State machine: IDLE→DETECTING→NEEDS_PAY→PAYING→PAID→RENEWING
- Blocking: upstream payment before local services start
- Synchronous wallet init (was async task)
- Client config: enabled, steps_to_buy, renewal_threshold_pct
- Updated PLAN.md with Phases 4-7 (client, payout, bytes, CVM)
- Updated CHECKLIST.md with all new phase items
- 30 new unit tests (all passing), 116 total
Diffstat (limited to 'main/tollgate_main.c')
| -rw-r--r-- | main/tollgate_main.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/main/tollgate_main.c b/main/tollgate_main.c index 7fa1be1..d4dcf0d 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c | |||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include "tollgate_api.h" | 19 | #include "tollgate_api.h" |
| 20 | #include "nucula_wallet.h" | 20 | #include "nucula_wallet.h" |
| 21 | #include "wifistr.h" | 21 | #include "wifistr.h" |
| 22 | #include "tollgate_client.h" | ||
| 22 | 23 | ||
| 23 | #define MAX_STA_RETRY 5 | 24 | #define MAX_STA_RETRY 5 |
| 24 | static const char *TAG = "tollgate_main"; | 25 | static const char *TAG = "tollgate_main"; |
| @@ -48,6 +49,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, | |||
| 48 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { | 49 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { |
| 49 | s_retry_count++; | 50 | s_retry_count++; |
| 50 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); | 51 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); |
| 52 | tollgate_client_on_sta_disconnected(); | ||
| 51 | if (s_services_running) stop_services(); | 53 | if (s_services_running) stop_services(); |
| 52 | if (s_retry_count < MAX_STA_RETRY) { | 54 | if (s_retry_count < MAX_STA_RETRY) { |
| 53 | esp_wifi_connect(); | 55 | esp_wifi_connect(); |
| @@ -80,9 +82,17 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, | |||
| 80 | { | 82 | { |
| 81 | if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { | 83 | if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { |
| 82 | ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; | 84 | ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; |
| 83 | ESP_LOGI(TAG, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip)); | 85 | ESP_LOGI(TAG, "Got IP:" IPSTR ", GW:" IPSTR, IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw)); |
| 84 | s_retry_count = 0; | 86 | s_retry_count = 0; |
| 85 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | 87 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| 88 | |||
| 89 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 90 | nucula_wallet_init(cfg->mint_url); | ||
| 91 | |||
| 92 | char gw_ip_str[16]; | ||
| 93 | snprintf(gw_ip_str, sizeof(gw_ip_str), IPSTR, IP2STR(&event->ip_info.gw)); | ||
| 94 | tollgate_client_on_sta_connected(gw_ip_str); | ||
| 95 | |||
| 86 | start_services(); | 96 | start_services(); |
| 87 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { | 97 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { |
| 88 | ESP_LOGW(TAG, "Lost IP address"); | 98 | ESP_LOGW(TAG, "Lost IP address"); |
| @@ -126,8 +136,6 @@ static void start_services(void) | |||
| 126 | firewall_init(ap_ip_info.ip); | 136 | firewall_init(ap_ip_info.ip); |
| 127 | session_manager_init(); | 137 | session_manager_init(); |
| 128 | 138 | ||
| 129 | xTaskCreate(wallet_init_task, "wallet_init", 32768, NULL, 5, NULL); | ||
| 130 | |||
| 131 | const tollgate_config_t *cfg = tollgate_config_get(); | 139 | const tollgate_config_t *cfg = tollgate_config_get(); |
| 132 | dns_server_start(ap_ip_info.ip, upstream_dns); | 140 | dns_server_start(ap_ip_info.ip, upstream_dns); |
| 133 | captive_portal_start(cfg->ap_ip_str); | 141 | captive_portal_start(cfg->ap_ip_str); |
| @@ -273,5 +281,6 @@ void app_main(void) | |||
| 273 | while (1) { | 281 | while (1) { |
| 274 | vTaskDelay(pdMS_TO_TICKS(1000)); | 282 | vTaskDelay(pdMS_TO_TICKS(1000)); |
| 275 | session_tick(); | 283 | session_tick(); |
| 284 | tollgate_client_tick(); | ||
| 276 | } | 285 | } |
| 277 | } | 286 | } |