diff options
| author | Your Name <you@example.com> | 2026-05-18 19:07:08 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 19:07:08 +0530 |
| commit | 859f9b14e033de9a0690ccbbb975b4b472b688ce (patch) | |
| tree | 678b248420886b1a17fbd32fc796cb2029863bf6 | |
| parent | 6e6d24169fe7ac8960236293bd39a4532f08f65b (diff) | |
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
| -rw-r--r-- | main/captive_portal.c | 21 | ||||
| -rw-r--r-- | main/tollgate_api.c | 23 | ||||
| -rw-r--r-- | main/tollgate_main.c | 10 |
3 files changed, 25 insertions, 29 deletions
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) | |||
| 236 | return ESP_OK; | 236 | return ESP_OK; |
| 237 | } | 237 | } |
| 238 | 238 | ||
| 239 | static esp_err_t mints_handler(httpd_req_t *req) | ||
| 240 | { | ||
| 241 | int mint_count = 0; | ||
| 242 | const mint_status_t *mints = mint_health_get_all(&mint_count); | ||
| 243 | cJSON *arr = cJSON_CreateArray(); | ||
| 244 | for (int i = 0; i < mint_count; i++) { | ||
| 245 | cJSON *obj = cJSON_CreateObject(); | ||
| 246 | cJSON_AddStringToObject(obj, "url", mints[i].url); | ||
| 247 | cJSON_AddBoolToObject(obj, "reachable", mints[i].reachable); | ||
| 248 | cJSON_AddItemToArray(arr, obj); | ||
| 249 | } | ||
| 250 | char *json = cJSON_PrintUnformatted(arr); | ||
| 251 | httpd_resp_set_type(req, "application/json"); | ||
| 252 | httpd_resp_send(req, json, strlen(json)); | ||
| 253 | cJSON_free(json); | ||
| 254 | cJSON_Delete(arr); | ||
| 255 | return ESP_OK; | ||
| 256 | } | ||
| 257 | |||
| 258 | static esp_err_t status_handler(httpd_req_t *req) | 239 | static esp_err_t status_handler(httpd_req_t *req) |
| 259 | { | 240 | { |
| 260 | const tollgate_config_t *cfg = tollgate_config_get(); | 241 | const tollgate_config_t *cfg = tollgate_config_get(); |
| @@ -355,7 +336,6 @@ static esp_err_t catchall_handler(httpd_req_t *req) | |||
| 355 | 336 | ||
| 356 | static const httpd_uri_t uri_portal = { .uri = "/", .method = HTTP_GET, .handler = portal_handler }; | 337 | static const httpd_uri_t uri_portal = { .uri = "/", .method = HTTP_GET, .handler = portal_handler }; |
| 357 | static const httpd_uri_t uri_grant = { .uri = "/grant_access", .method = HTTP_GET, .handler = grant_access_handler }; | 338 | static const httpd_uri_t uri_grant = { .uri = "/grant_access", .method = HTTP_GET, .handler = grant_access_handler }; |
| 358 | static const httpd_uri_t uri_mints = { .uri = "/mints", .method = HTTP_GET, .handler = mints_handler }; | ||
| 359 | static const httpd_uri_t uri_status = { .uri = "/api/status", .method = HTTP_GET, .handler = status_handler }; | 339 | static const httpd_uri_t uri_status = { .uri = "/api/status", .method = HTTP_GET, .handler = status_handler }; |
| 360 | static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .handler = whoami_handler }; | 340 | static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .handler = whoami_handler }; |
| 361 | static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = usage_handler }; | 341 | 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) | |||
| 386 | 366 | ||
| 387 | httpd_register_uri_handler(s_server, &uri_portal); | 367 | httpd_register_uri_handler(s_server, &uri_portal); |
| 388 | httpd_register_uri_handler(s_server, &uri_grant); | 368 | httpd_register_uri_handler(s_server, &uri_grant); |
| 389 | httpd_register_uri_handler(s_server, &uri_mints); | ||
| 390 | httpd_register_uri_handler(s_server, &uri_status); | 369 | httpd_register_uri_handler(s_server, &uri_status); |
| 391 | httpd_register_uri_handler(s_server, &uri_whoami); | 370 | httpd_register_uri_handler(s_server, &uri_whoami); |
| 392 | httpd_register_uri_handler(s_server, &uri_usage); | 371 | 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) | |||
| 484 | return ESP_OK; | 484 | return ESP_OK; |
| 485 | } | 485 | } |
| 486 | 486 | ||
| 487 | static esp_err_t api_get_mints(httpd_req_t *req) | ||
| 488 | { | ||
| 489 | int mint_count = 0; | ||
| 490 | const mint_status_t *mints = mint_health_get_all(&mint_count); | ||
| 491 | cJSON *arr = cJSON_CreateArray(); | ||
| 492 | for (int i = 0; i < mint_count; i++) { | ||
| 493 | cJSON *obj = cJSON_CreateObject(); | ||
| 494 | cJSON_AddStringToObject(obj, "url", mints[i].url); | ||
| 495 | cJSON_AddBoolToObject(obj, "reachable", mints[i].reachable); | ||
| 496 | cJSON_AddItemToArray(arr, obj); | ||
| 497 | } | ||
| 498 | char *json = cJSON_PrintUnformatted(arr); | ||
| 499 | httpd_resp_set_type(req, "application/json"); | ||
| 500 | httpd_resp_send(req, json, strlen(json)); | ||
| 501 | cJSON_free(json); | ||
| 502 | cJSON_Delete(arr); | ||
| 503 | return ESP_OK; | ||
| 504 | } | ||
| 505 | |||
| 487 | static const httpd_uri_t uri_discovery = { .uri = "/", .method = HTTP_GET, .handler = api_get_discovery }; | 506 | static const httpd_uri_t uri_discovery = { .uri = "/", .method = HTTP_GET, .handler = api_get_discovery }; |
| 488 | static const httpd_uri_t uri_payment = { .uri = "/", .method = HTTP_POST, .handler = api_post_payment }; | 507 | static const httpd_uri_t uri_payment = { .uri = "/", .method = HTTP_POST, .handler = api_post_payment }; |
| 508 | static const httpd_uri_t uri_mints = { .uri = "/mints", .method = HTTP_GET, .handler = api_get_mints }; | ||
| 489 | static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = api_get_usage }; | 509 | static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = api_get_usage }; |
| 490 | static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .handler = api_get_whoami }; | 510 | static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .handler = api_get_whoami }; |
| 491 | static const httpd_uri_t uri_wallet = { .uri = "/wallet", .method = HTTP_GET, .handler = api_get_wallet }; | 511 | 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) | |||
| 499 | httpd_config_t config = HTTPD_DEFAULT_CONFIG(); | 519 | httpd_config_t config = HTTPD_DEFAULT_CONFIG(); |
| 500 | config.server_port = 2121; | 520 | config.server_port = 2121; |
| 501 | config.ctrl_port = 32769; | 521 | config.ctrl_port = 32769; |
| 502 | config.max_uri_handlers = 10; | 522 | config.max_uri_handlers = 12; |
| 503 | config.stack_size = 16384; | 523 | config.stack_size = 16384; |
| 504 | 524 | ||
| 505 | esp_err_t ret = httpd_start(&s_api_server, &config); | 525 | esp_err_t ret = httpd_start(&s_api_server, &config); |
| @@ -510,6 +530,7 @@ esp_err_t tollgate_api_start(void) | |||
| 510 | 530 | ||
| 511 | httpd_register_uri_handler(s_api_server, &uri_discovery); | 531 | httpd_register_uri_handler(s_api_server, &uri_discovery); |
| 512 | httpd_register_uri_handler(s_api_server, &uri_payment); | 532 | httpd_register_uri_handler(s_api_server, &uri_payment); |
| 533 | httpd_register_uri_handler(s_api_server, &uri_mints); | ||
| 513 | httpd_register_uri_handler(s_api_server, &uri_usage); | 534 | httpd_register_uri_handler(s_api_server, &uri_usage); |
| 514 | httpd_register_uri_handler(s_api_server, &uri_whoami); | 535 | httpd_register_uri_handler(s_api_server, &uri_whoami); |
| 515 | httpd_register_uri_handler(s_api_server, &uri_wallet); | 536 | 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 @@ | |||
| 26 | #include "cvm_server.h" | 26 | #include "cvm_server.h" |
| 27 | #include "display.h" | 27 | #include "display.h" |
| 28 | 28 | ||
| 29 | #define MAX_STA_RETRY 5 | 29 | #define MAX_STA_RETRY 3 |
| 30 | static const char *TAG = "tollgate_main"; | 30 | static const char *TAG = "tollgate_main"; |
| 31 | 31 | ||
| 32 | static EventGroupHandle_t s_wifi_event_group; | 32 | static EventGroupHandle_t s_wifi_event_group; |
| @@ -55,7 +55,6 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, | |||
| 55 | s_retry_count++; | 55 | s_retry_count++; |
| 56 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); | 56 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); |
| 57 | tollgate_client_on_sta_disconnected(); | 57 | tollgate_client_on_sta_disconnected(); |
| 58 | if (s_services_running) stop_services(); | ||
| 59 | if (s_retry_count < MAX_STA_RETRY) { | 58 | if (s_retry_count < MAX_STA_RETRY) { |
| 60 | esp_wifi_connect(); | 59 | esp_wifi_connect(); |
| 61 | } else { | 60 | } else { |
| @@ -112,7 +111,6 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, | |||
| 112 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { | 111 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { |
| 113 | ESP_LOGW(TAG, "Lost IP address"); | 112 | ESP_LOGW(TAG, "Lost IP address"); |
| 114 | xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | 113 | xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| 115 | stop_services(); | ||
| 116 | } | 114 | } |
| 117 | } | 115 | } |
| 118 | 116 | ||
| @@ -319,10 +317,8 @@ void app_main(void) | |||
| 319 | 317 | ||
| 320 | ESP_LOGI(TAG, "WiFi AP+STA started, waiting for connection..."); | 318 | ESP_LOGI(TAG, "WiFi AP+STA started, waiting for connection..."); |
| 321 | 319 | ||
| 322 | if (tollgate_config_get_wifi(&(wifi_config_t){0}) != ESP_OK) { | 320 | xTaskCreate(services_start_task, "svc_start", 32768, NULL, 5, NULL); |
| 323 | ESP_LOGI(TAG, "No STA network configured, starting services immediately"); | 321 | ESP_LOGI(TAG, "Services starting immediately (AP-only mode, STA will connect when available)"); |
| 324 | xTaskCreate(services_start_task, "svc_start", 32768, NULL, 5, NULL); | ||
| 325 | } | ||
| 326 | 322 | ||
| 327 | while (1) { | 323 | while (1) { |
| 328 | vTaskDelay(pdMS_TO_TICKS(1000)); | 324 | vTaskDelay(pdMS_TO_TICKS(1000)); |