upleb.uk

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

summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-21 16:46:51 +0530
committerYour Name <you@example.com>2026-05-21 16:46:51 +0530
commit19e175b1c1dea54efb61e8040ffdfa973fbac5d5 (patch)
treef41ca977a993cc24f8bf136e96370c8097ceeaf1 /main
parent243e4808246555f86d464d1c476681a902e644ff (diff)
Wallet receive via health task queue (no separate TLS worker)
- Removed standalone TLS worker task (couldn't allocate 16KB internal stack) - Added wallet receive queue to mint_health task (already has 16KB stack + working TLS) - health_task processes wallet tokens between probe intervals (1s poll) - tls_worker_set_queue() registers queue from mint_health_start() - Fallback to synchronous receive if queue not available - Added freertos/queue.h and tollgate_api.h stubs for unit tests - Added BaseType_t/UBaseType_t/TickType_t/pdFALSE to FreeRTOS stub - Full payment round-trip confirmed: 2 payments → 41 sat balance
Diffstat (limited to 'main')
-rw-r--r--main/mint_health.c50
-rw-r--r--main/tollgate_api.c29
-rw-r--r--main/tollgate_api.h1
3 files changed, 78 insertions, 2 deletions
diff --git a/main/mint_health.c b/main/mint_health.c
index a551295..4ed8a19 100644
--- a/main/mint_health.c
+++ b/main/mint_health.c
@@ -5,11 +5,17 @@
5#include "freertos/FreeRTOS.h" 5#include "freertos/FreeRTOS.h"
6#include "freertos/task.h" 6#include "freertos/task.h"
7#include "freertos/semphr.h" 7#include "freertos/semphr.h"
8#include "freertos/queue.h"
9#include "nucula_wallet.h"
10#include "tollgate_api.h"
8#include <string.h> 11#include <string.h>
9#include <stdlib.h> 12#include <stdlib.h>
10 13
11static const char *TAG = "mint_health"; 14static const char *TAG = "mint_health";
12 15
16#define WALLET_QUEUE_LEN 8
17static QueueHandle_t s_wallet_queue = NULL;
18
13static int s_last_probe_err = 0; 19static int s_last_probe_err = 0;
14 20
15static mint_status_t s_mints[MINT_HEALTH_MAX]; 21static mint_status_t s_mints[MINT_HEALTH_MAX];
@@ -163,16 +169,54 @@ static void run_initial_probes(void)
163 fire_callbacks(); 169 fire_callbacks();
164} 170}
165 171
172static void process_wallet_queue(void)
173{
174 char *token;
175 while (s_wallet_queue && xQueueReceive(s_wallet_queue, &token, 0) == pdTRUE) {
176 if (!token) continue;
177 ESP_LOGI(TAG, "Processing wallet receive (%zu bytes)", strlen(token));
178 esp_err_t err = nucula_wallet_receive(token);
179 if (err == ESP_OK) {
180 ESP_LOGI(TAG, "Wallet receive OK, balance=%llu",
181 (unsigned long long)nucula_wallet_balance());
182 } else {
183 ESP_LOGW(TAG, "Wallet receive failed");
184 }
185 free(token);
186 }
187}
188
166static void health_task(void *pvParameters) 189static void health_task(void *pvParameters)
167{ 190{
168 ESP_LOGI(TAG, "Health probe task started, waiting for DNS to stabilize..."); 191 ESP_LOGI(TAG, "Health probe task started, waiting for DNS to stabilize...");
169 vTaskDelay(pdMS_TO_TICKS(5000)); 192 vTaskDelay(pdMS_TO_TICKS(5000));
170 run_initial_probes(); 193 run_initial_probes();
194 process_wallet_queue();
171 195
172 while (s_running) { 196 while (s_running) {
173 vTaskDelay(pdMS_TO_TICKS(MINT_HEALTH_PROBE_INTERVAL_S * 1000)); 197 TickType_t start = xTaskGetTickCount();
198 while (s_running) {
199 TickType_t elapsed = (xTaskGetTickCount() - start) * portTICK_PERIOD_MS;
200 if (elapsed >= MINT_HEALTH_PROBE_INTERVAL_S * 1000) break;
201
202 char *token = NULL;
203 if (s_wallet_queue && xQueueReceive(s_wallet_queue, &token, pdMS_TO_TICKS(1000)) == pdTRUE) {
204 if (token) {
205 ESP_LOGI(TAG, "Processing wallet receive (%zu bytes)", strlen(token));
206 esp_err_t err = nucula_wallet_receive(token);
207 if (err == ESP_OK) {
208 ESP_LOGI(TAG, "Wallet receive OK, balance=%llu",
209 (unsigned long long)nucula_wallet_balance());
210 } else {
211 ESP_LOGW(TAG, "Wallet receive failed");
212 }
213 free(token);
214 }
215 }
216 }
174 if (!s_running) break; 217 if (!s_running) break;
175 run_probes(); 218 run_probes();
219 process_wallet_queue();
176 } 220 }
177 221
178 s_task_handle = NULL; 222 s_task_handle = NULL;
@@ -183,6 +227,10 @@ void mint_health_start(void)
183{ 227{
184 if (s_running) return; 228 if (s_running) return;
185 s_running = true; 229 s_running = true;
230
231 s_wallet_queue = xQueueCreate(WALLET_QUEUE_LEN, sizeof(char *));
232 tls_worker_set_queue(s_wallet_queue);
233
186 xTaskCreate(health_task, "mint_health", 16384, NULL, 3, &s_task_handle); 234 xTaskCreate(health_task, "mint_health", 16384, NULL, 3, &s_task_handle);
187} 235}
188 236
diff --git a/main/tollgate_api.c b/main/tollgate_api.c
index 7f2102e..be753ea 100644
--- a/main/tollgate_api.c
+++ b/main/tollgate_api.c
@@ -19,11 +19,37 @@
19#include "lwip/sockets.h" 19#include "lwip/sockets.h"
20#include "lwip/netdb.h" 20#include "lwip/netdb.h"
21#include "freertos/task.h" 21#include "freertos/task.h"
22#include "freertos/queue.h"
22#include <string.h> 23#include <string.h>
23 24
24static const char *TAG = "tollgate_api"; 25static const char *TAG = "tollgate_api";
25static httpd_handle_t s_api_server = NULL; 26static httpd_handle_t s_api_server = NULL;
26 27
28static QueueHandle_t s_wallet_queue = NULL;
29
30void tls_worker_set_queue(QueueHandle_t q)
31{
32 s_wallet_queue = q;
33}
34
35static void tls_worker_submit(const char *token)
36{
37 if (!s_wallet_queue) {
38 ESP_LOGW(TAG, "No wallet queue, receiving synchronously");
39 nucula_wallet_receive(token);
40 return;
41 }
42
43 char *copy = strdup(token);
44 if (!copy) return;
45
46 if (xQueueSend(s_wallet_queue, &copy, pdMS_TO_TICKS(1000)) != pdTRUE) {
47 ESP_LOGW(TAG, "Wallet queue full, receiving synchronously");
48 nucula_wallet_receive(copy);
49 free(copy);
50 }
51}
52
27static esp_err_t get_client_ip(httpd_req_t *req, uint32_t *ip_out) 53static esp_err_t get_client_ip(httpd_req_t *req, uint32_t *ip_out)
28{ 54{
29 int sockfd = httpd_req_to_sockfd(req); 55 int sockfd = httpd_req_to_sockfd(req);
@@ -343,7 +369,7 @@ static esp_err_t api_post_payment(httpd_req_t *req)
343 cJSON_free(json); 369 cJSON_free(json);
344 cJSON_Delete(session_event); 370 cJSON_Delete(session_event);
345 371
346 nucula_wallet_receive(body_copy); 372 tls_worker_submit(body_copy);
347 373
348 free(states); 374 free(states);
349 free(token); 375 free(token);
@@ -820,6 +846,7 @@ esp_err_t tollgate_api_start(void)
820 } 846 }
821 847
822 ESP_LOGI(TAG, "TollGate API started on port 2121"); 848 ESP_LOGI(TAG, "TollGate API started on port 2121");
849
823 return ESP_OK; 850 return ESP_OK;
824} 851}
825 852
diff --git a/main/tollgate_api.h b/main/tollgate_api.h
index 23e0d75..2af4b8c 100644
--- a/main/tollgate_api.h
+++ b/main/tollgate_api.h
@@ -6,5 +6,6 @@
6 6
7esp_err_t tollgate_api_start(void); 7esp_err_t tollgate_api_start(void);
8void tollgate_api_stop(void); 8void tollgate_api_stop(void);
9void tls_worker_set_queue(QueueHandle_t q);
9 10
10#endif 11#endif