From 859f9b14e033de9a0690ccbbb975b4b472b688ce Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 18 May 2026 19:07:08 +0530 Subject: Move /mints to API server (port 2121), fix services persistence - /mints handler moved from captive portal (port 80) to API server (port 2121) where it belongs (JSON endpoint, matches portal JS fetch URL) - Remove stop_services() from STA disconnect and IP loss handlers - Services now persist across WiFi STA reconnections - Reduce MAX_STA_RETRY from 5 to 3 --- main/captive_portal.c | 21 --------------------- main/tollgate_api.c | 23 ++++++++++++++++++++++- main/tollgate_main.c | 10 +++------- 3 files changed, 25 insertions(+), 29 deletions(-) (limited to 'main') diff --git a/main/captive_portal.c b/main/captive_portal.c index 58a3560..c9bcf19 100644 --- a/main/captive_portal.c +++ b/main/captive_portal.c @@ -236,25 +236,6 @@ static esp_err_t grant_access_handler(httpd_req_t *req) return ESP_OK; } -static esp_err_t mints_handler(httpd_req_t *req) -{ - int mint_count = 0; - const mint_status_t *mints = mint_health_get_all(&mint_count); - cJSON *arr = cJSON_CreateArray(); - for (int i = 0; i < mint_count; i++) { - cJSON *obj = cJSON_CreateObject(); - cJSON_AddStringToObject(obj, "url", mints[i].url); - cJSON_AddBoolToObject(obj, "reachable", mints[i].reachable); - cJSON_AddItemToArray(arr, obj); - } - char *json = cJSON_PrintUnformatted(arr); - httpd_resp_set_type(req, "application/json"); - httpd_resp_send(req, json, strlen(json)); - cJSON_free(json); - cJSON_Delete(arr); - return ESP_OK; -} - static esp_err_t status_handler(httpd_req_t *req) { const tollgate_config_t *cfg = tollgate_config_get(); @@ -355,7 +336,6 @@ static esp_err_t catchall_handler(httpd_req_t *req) static const httpd_uri_t uri_portal = { .uri = "/", .method = HTTP_GET, .handler = portal_handler }; static const httpd_uri_t uri_grant = { .uri = "/grant_access", .method = HTTP_GET, .handler = grant_access_handler }; -static const httpd_uri_t uri_mints = { .uri = "/mints", .method = HTTP_GET, .handler = mints_handler }; static const httpd_uri_t uri_status = { .uri = "/api/status", .method = HTTP_GET, .handler = status_handler }; static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .handler = whoami_handler }; static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = usage_handler }; @@ -386,7 +366,6 @@ esp_err_t captive_portal_start(const char *ap_ip_str) httpd_register_uri_handler(s_server, &uri_portal); httpd_register_uri_handler(s_server, &uri_grant); - httpd_register_uri_handler(s_server, &uri_mints); httpd_register_uri_handler(s_server, &uri_status); httpd_register_uri_handler(s_server, &uri_whoami); httpd_register_uri_handler(s_server, &uri_usage); diff --git a/main/tollgate_api.c b/main/tollgate_api.c index b694729..22b30ee 100644 --- a/main/tollgate_api.c +++ b/main/tollgate_api.c @@ -484,8 +484,28 @@ static esp_err_t api_post_wallet_send(httpd_req_t *req) return ESP_OK; } +static esp_err_t api_get_mints(httpd_req_t *req) +{ + int mint_count = 0; + const mint_status_t *mints = mint_health_get_all(&mint_count); + cJSON *arr = cJSON_CreateArray(); + for (int i = 0; i < mint_count; i++) { + cJSON *obj = cJSON_CreateObject(); + cJSON_AddStringToObject(obj, "url", mints[i].url); + cJSON_AddBoolToObject(obj, "reachable", mints[i].reachable); + cJSON_AddItemToArray(arr, obj); + } + char *json = cJSON_PrintUnformatted(arr); + httpd_resp_set_type(req, "application/json"); + httpd_resp_send(req, json, strlen(json)); + cJSON_free(json); + cJSON_Delete(arr); + return ESP_OK; +} + static const httpd_uri_t uri_discovery = { .uri = "/", .method = HTTP_GET, .handler = api_get_discovery }; static const httpd_uri_t uri_payment = { .uri = "/", .method = HTTP_POST, .handler = api_post_payment }; +static const httpd_uri_t uri_mints = { .uri = "/mints", .method = HTTP_GET, .handler = api_get_mints }; static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = api_get_usage }; static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .handler = api_get_whoami }; static const httpd_uri_t uri_wallet = { .uri = "/wallet", .method = HTTP_GET, .handler = api_get_wallet }; @@ -499,7 +519,7 @@ esp_err_t tollgate_api_start(void) httpd_config_t config = HTTPD_DEFAULT_CONFIG(); config.server_port = 2121; config.ctrl_port = 32769; - config.max_uri_handlers = 10; + config.max_uri_handlers = 12; config.stack_size = 16384; esp_err_t ret = httpd_start(&s_api_server, &config); @@ -510,6 +530,7 @@ esp_err_t tollgate_api_start(void) httpd_register_uri_handler(s_api_server, &uri_discovery); httpd_register_uri_handler(s_api_server, &uri_payment); + httpd_register_uri_handler(s_api_server, &uri_mints); httpd_register_uri_handler(s_api_server, &uri_usage); httpd_register_uri_handler(s_api_server, &uri_whoami); httpd_register_uri_handler(s_api_server, &uri_wallet); diff --git a/main/tollgate_main.c b/main/tollgate_main.c index 5f3e0e1..018d934 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c @@ -26,7 +26,7 @@ #include "cvm_server.h" #include "display.h" -#define MAX_STA_RETRY 5 +#define MAX_STA_RETRY 3 static const char *TAG = "tollgate_main"; static EventGroupHandle_t s_wifi_event_group; @@ -55,7 +55,6 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, s_retry_count++; ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); tollgate_client_on_sta_disconnected(); - if (s_services_running) stop_services(); if (s_retry_count < MAX_STA_RETRY) { esp_wifi_connect(); } else { @@ -112,7 +111,6 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { ESP_LOGW(TAG, "Lost IP address"); xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - stop_services(); } } @@ -319,10 +317,8 @@ void app_main(void) ESP_LOGI(TAG, "WiFi AP+STA started, waiting for connection..."); - if (tollgate_config_get_wifi(&(wifi_config_t){0}) != ESP_OK) { - ESP_LOGI(TAG, "No STA network configured, starting services immediately"); - xTaskCreate(services_start_task, "svc_start", 32768, NULL, 5, NULL); - } + xTaskCreate(services_start_task, "svc_start", 32768, NULL, 5, NULL); + ESP_LOGI(TAG, "Services starting immediately (AP-only mode, STA will connect when available)"); while (1) { vTaskDelay(pdMS_TO_TICKS(1000)); -- cgit v1.2.3