diff options
| author | Your Name <you@example.com> | 2026-05-18 18:50:20 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 18:50:20 +0530 |
| commit | f6369f5b87f5dab17a03315d82454fdcdd668ec9 (patch) | |
| tree | bc16c22f87253eafa680a30da370b1822d855ca4 /main/tollgate_main.c | |
| parent | d8c55d2a072433e16ae5c84b7b7c676587f6f2d9 (diff) | |
feat: extract tollgate_core ESP-IDF component
Extract shared TollGate business logic into a reusable ESP-IDF component
that can be consumed by esp-miner via the IDF Component Manager.
Component structure:
components/tollgate_core/
include/tollgate_core.h — public API
include/tollgate_platform.h — platform interface (config callbacks)
src/tollgate_core.c — orchestrator (init, payment, tick, owner)
src/tollgate_core_cashu.c/h — Cashu V3 token decode/verify
src/tollgate_core_dns.c/h — per-client DNS hijack/forward
src/tollgate_core_firewall.c/h — per-client NAT filter
src/tollgate_core_session.c/h — session lifecycle
Key design decisions:
- Platform interface pattern: consumers implement tollgate_platform_t
(config getters + optional spend_proofs wallet hook)
- Cross-module wiring (session→firewall→dns) stays internal
- No direct config.h dependency — all config via platform callbacks
- spend_proofs can be NULL (accepts payment without local wallet)
Standalone app updated:
- main/tollgate_platform.c implements platform via config singleton
- main/tollgate_main.c calls tollgate_core_init/tick/dns_start
- main/tollgate_api.c routes payment through tollgate_core_process_payment
- Removed cashu.c, dns_server.c, firewall.c, session.c from CMakeLists
Not yet build-verified — blocked on multi-mint, price-discovery,
cvm-integration, and display-fix branches merging to master.
Diffstat (limited to 'main/tollgate_main.c')
| -rw-r--r-- | main/tollgate_main.c | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/main/tollgate_main.c b/main/tollgate_main.c index c0ff65f..697dae3 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c | |||
| @@ -13,10 +13,8 @@ | |||
| 13 | #include "dhcpserver/dhcpserver.h" | 13 | #include "dhcpserver/dhcpserver.h" |
| 14 | #include "config.h" | 14 | #include "config.h" |
| 15 | #include "identity.h" | 15 | #include "identity.h" |
| 16 | #include "dns_server.h" | 16 | #include "tollgate_core.h" |
| 17 | #include "captive_portal.h" | 17 | #include "captive_portal.h" |
| 18 | #include "firewall.h" | ||
| 19 | #include "session.h" | ||
| 20 | #include "tollgate_api.h" | 18 | #include "tollgate_api.h" |
| 21 | #include "nucula_wallet.h" | 19 | #include "nucula_wallet.h" |
| 22 | #include "wifistr.h" | 20 | #include "wifistr.h" |
| @@ -73,11 +71,13 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, | |||
| 73 | ESP_LOGI(TAG, "Station connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", | 71 | ESP_LOGI(TAG, "Station connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
| 74 | event->mac[0], event->mac[1], event->mac[2], | 72 | event->mac[0], event->mac[1], event->mac[2], |
| 75 | event->mac[3], event->mac[4], event->mac[5]); | 73 | event->mac[3], event->mac[4], event->mac[5]); |
| 74 | tollgate_core_client_connected(event->mac, 0); | ||
| 76 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) { | 75 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) { |
| 77 | wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; | 76 | wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; |
| 78 | ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", | 77 | ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
| 79 | event->mac[0], event->mac[1], event->mac[2], | 78 | event->mac[0], event->mac[1], event->mac[2], |
| 80 | event->mac[3], event->mac[4], event->mac[5]); | 79 | event->mac[3], event->mac[4], event->mac[5]); |
| 80 | tollgate_core_client_disconnected(event->mac); | ||
| 81 | } | 81 | } |
| 82 | } | 82 | } |
| 83 | 83 | ||
| @@ -115,13 +115,6 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, | |||
| 115 | } | 115 | } |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | static void wallet_init_task(void *pvParameters) | ||
| 119 | { | ||
| 120 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 121 | nucula_wallet_init(cfg->mint_url); | ||
| 122 | vTaskDelete(NULL); | ||
| 123 | } | ||
| 124 | |||
| 125 | static void publish_wifistr_task(void *pvParameters) | 118 | static void publish_wifistr_task(void *pvParameters) |
| 126 | { | 119 | { |
| 127 | vTaskDelay(pdMS_TO_TICKS(5000)); | 120 | vTaskDelay(pdMS_TO_TICKS(5000)); |
| @@ -139,7 +132,6 @@ static void start_services(void) | |||
| 139 | return; | 132 | return; |
| 140 | } | 133 | } |
| 141 | 134 | ||
| 142 | esp_netif_get_ip_info(s_ap_netif, &(esp_netif_ip_info_t){0}); | ||
| 143 | esp_netif_ip_info_t ap_ip_info; | 135 | esp_netif_ip_info_t ap_ip_info; |
| 144 | esp_netif_get_ip_info(s_ap_netif, &ap_ip_info); | 136 | esp_netif_get_ip_info(s_ap_netif, &ap_ip_info); |
| 145 | 137 | ||
| @@ -147,21 +139,20 @@ static void start_services(void) | |||
| 147 | const ip_addr_t *dns_addr = dns_getserver(0); | 139 | const ip_addr_t *dns_addr = dns_getserver(0); |
| 148 | upstream_dns.addr = dns_addr->addr; | 140 | upstream_dns.addr = dns_addr->addr; |
| 149 | 141 | ||
| 150 | firewall_init(ap_ip_info.ip); | ||
| 151 | session_manager_init(); | ||
| 152 | |||
| 153 | const tollgate_config_t *cfg = tollgate_config_get(); | 142 | const tollgate_config_t *cfg = tollgate_config_get(); |
| 143 | const tollgate_platform_t *platform = tollgate_get_platform(); | ||
| 144 | tollgate_core_init(platform, ap_ip_info.ip); | ||
| 145 | |||
| 154 | nucula_wallet_init(cfg->mint_url); | 146 | nucula_wallet_init(cfg->mint_url); |
| 155 | lightning_payout_init(&cfg->payout); | 147 | lightning_payout_init(&cfg->payout); |
| 156 | 148 | ||
| 157 | dns_server_start(ap_ip_info.ip, upstream_dns); | 149 | tollgate_core_dns_start(upstream_dns); |
| 158 | captive_portal_start(cfg->ap_ip_str); | 150 | captive_portal_start(cfg->ap_ip_str); |
| 159 | tollgate_api_start(); | 151 | tollgate_api_start(); |
| 160 | 152 | ||
| 161 | xTaskCreate(publish_wifistr_task, "wifistr_init", 16384, NULL, 3, NULL); | 153 | xTaskCreate(publish_wifistr_task, "wifistr_init", 16384, NULL, 3, NULL); |
| 162 | 154 | ||
| 163 | const tollgate_config_t *cfg2 = tollgate_config_get(); | 155 | if (cfg->cvm_enabled) { |
| 164 | if (cfg2->cvm_enabled) { | ||
| 165 | cvm_server_init(); | 156 | cvm_server_init(); |
| 166 | cvm_server_start(); | 157 | cvm_server_start(); |
| 167 | } | 158 | } |
| @@ -186,9 +177,8 @@ static void stop_services(void) | |||
| 186 | 177 | ||
| 187 | captive_portal_stop(); | 178 | captive_portal_stop(); |
| 188 | tollgate_api_stop(); | 179 | tollgate_api_stop(); |
| 189 | dns_server_stop(); | 180 | tollgate_core_dns_stop(); |
| 190 | cvm_server_stop(); | 181 | cvm_server_stop(); |
| 191 | firewall_revoke_all(); | ||
| 192 | s_services_running = false; | 182 | s_services_running = false; |
| 193 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); | 183 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); |
| 194 | ESP_LOGI(TAG, "=== TollGate services stopped ==="); | 184 | ESP_LOGI(TAG, "=== TollGate services stopped ==="); |
| @@ -316,7 +306,7 @@ void app_main(void) | |||
| 316 | 306 | ||
| 317 | while (1) { | 307 | while (1) { |
| 318 | vTaskDelay(pdMS_TO_TICKS(1000)); | 308 | vTaskDelay(pdMS_TO_TICKS(1000)); |
| 319 | session_tick(); | 309 | tollgate_core_tick(); |
| 320 | tollgate_client_tick(); | 310 | tollgate_client_tick(); |
| 321 | lightning_payout_tick(); | 311 | lightning_payout_tick(); |
| 322 | } | 312 | } |