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:
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt7
-rw-r--r--main/display.c2
-rw-r--r--main/negentropy_adapter.c78
-rw-r--r--main/negentropy_adapter.h27
-rw-r--r--main/tollgate_main.c12
5 files changed, 124 insertions, 2 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index a041bc1..6408e14 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -16,8 +16,13 @@ idf_component_register(SRCS "tollgate_main.c"
16 "nip04.c" 16 "nip04.c"
17 "mcp_handler.c" 17 "mcp_handler.c"
18 "cvm_server.c" 18 "cvm_server.c"
19 "display.c"
20 "font.c"
21 "local_relay.c"
22 "relay_selector.c"
23 "sync_manager.c"
19 INCLUDE_DIRS "." 24 INCLUDE_DIRS "."
20 REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server 25 REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server
21 lwip json esp_http_client mbedtls esp-tls log spiffs 26 lwip json esp_http_client mbedtls esp-tls log spiffs
22 nucula_lib secp256k1 esp_timer 27 nucula_lib secp256k1 axs15231b qrcode wisp_relay
23 PRIV_REQUIRES esp-tls) 28 PRIV_REQUIRES esp-tls)
diff --git a/main/display.c b/main/display.c
index 2b6cc88..72b7686 100644
--- a/main/display.c
+++ b/main/display.c
@@ -42,7 +42,7 @@ static int qr_pixel_size(int len) {
42 return 2; 42 return 2;
43} 43}
44 44
45static int escape_wifi_field(const char *src, char *dst, int dst_size) { 45int escape_wifi_field(const char *src, char *dst, int dst_size) {
46 int si = 0, di = 0; 46 int si = 0, di = 0;
47 while (src[si] && di < dst_size - 2) { 47 while (src[si] && di < dst_size - 2) {
48 char c = src[si]; 48 char c = src[si];
diff --git a/main/negentropy_adapter.c b/main/negentropy_adapter.c
new file mode 100644
index 0000000..2939289
--- /dev/null
+++ b/main/negentropy_adapter.c
@@ -0,0 +1,78 @@
1#include "negentropy_adapter.h"
2#include "storage_engine.h"
3#include <stdlib.h>
4#include <string.h>
5#include "esp_log.h"
6
7static const char *TAG = "negentropy_adapter";
8
9struct negentropy_adapter {
10 void *storage;
11 negentropy_item_t *items;
12 size_t count;
13 size_t capacity;
14};
15
16negentropy_adapter_t *negentropy_adapter_from_storage(void *storage_engine)
17{
18 if (!storage_engine) return NULL;
19
20 negentropy_adapter_t *adapter = calloc(1, sizeof(negentropy_adapter_t));
21 if (!adapter) return NULL;
22
23 adapter->storage = storage_engine;
24 adapter->items = NULL;
25 adapter->count = 0;
26 adapter->capacity = 0;
27
28 return adapter;
29}
30
31esp_err_t negentropy_adapter_get_items(negentropy_adapter_t *adapter,
32 negentropy_item_t **items,
33 size_t *count)
34{
35 if (!adapter || !items || !count) return ESP_ERR_INVALID_ARG;
36
37 if (adapter->items) {
38 free(adapter->items);
39 adapter->items = NULL;
40 }
41 adapter->count = 0;
42 adapter->capacity = 0;
43
44 *items = adapter->items;
45 *count = adapter->count;
46
47 ESP_LOGI(TAG, "Adapter has %zu items", adapter->count);
48 return ESP_OK;
49}
50
51esp_err_t negentropy_adapter_insert_item(negentropy_adapter_t *adapter,
52 uint64_t created_at,
53 const uint8_t *id)
54{
55 if (!adapter || !id) return ESP_ERR_INVALID_ARG;
56
57 if (adapter->count >= adapter->capacity) {
58 size_t new_cap = adapter->capacity == 0 ? 64 : adapter->capacity * 2;
59 negentropy_item_t *new_items = realloc(adapter->items, new_cap * sizeof(negentropy_item_t));
60 if (!new_items) return ESP_ERR_NO_MEM;
61 adapter->items = new_items;
62 adapter->capacity = new_cap;
63 }
64
65 negentropy_item_t *item = &adapter->items[adapter->count];
66 item->created_at = created_at;
67 memcpy(item->id, id, 32);
68 adapter->count++;
69
70 return ESP_OK;
71}
72
73void negentropy_adapter_destroy(negentropy_adapter_t *adapter)
74{
75 if (!adapter) return;
76 if (adapter->items) free(adapter->items);
77 free(adapter);
78}
diff --git a/main/negentropy_adapter.h b/main/negentropy_adapter.h
new file mode 100644
index 0000000..1e8c0a8
--- /dev/null
+++ b/main/negentropy_adapter.h
@@ -0,0 +1,27 @@
1#ifndef NEGENTROPY_ADAPTER_H
2#define NEGENTROPY_ADAPTER_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stddef.h>
7
8typedef struct {
9 uint64_t created_at;
10 uint8_t id[32];
11} negentropy_item_t;
12
13typedef struct negentropy_adapter negentropy_adapter_t;
14
15negentropy_adapter_t *negentropy_adapter_from_storage(void *storage_engine);
16
17esp_err_t negentropy_adapter_get_items(negentropy_adapter_t *adapter,
18 negentropy_item_t **items,
19 size_t *count);
20
21esp_err_t negentropy_adapter_insert_item(negentropy_adapter_t *adapter,
22 uint64_t created_at,
23 const uint8_t *id);
24
25void negentropy_adapter_destroy(negentropy_adapter_t *adapter);
26
27#endif
diff --git a/main/tollgate_main.c b/main/tollgate_main.c
index fa7a692..4741765 100644
--- a/main/tollgate_main.c
+++ b/main/tollgate_main.c
@@ -23,6 +23,10 @@
23#include "tollgate_client.h" 23#include "tollgate_client.h"
24#include "lightning_payout.h" 24#include "lightning_payout.h"
25#include "cvm_server.h" 25#include "cvm_server.h"
26#include "display.h"
27#include "local_relay.h"
28#include "relay_selector.h"
29#include "sync_manager.h"
26 30
27#define MAX_STA_RETRY 5 31#define MAX_STA_RETRY 5
28static const char *TAG = "tollgate_main"; 32static const char *TAG = "tollgate_main";
@@ -178,6 +182,11 @@ static void start_services(void)
178 s_services_running = true; 182 s_services_running = true;
179 if (s_services_mutex) xSemaphoreGive(s_services_mutex); 183 if (s_services_mutex) xSemaphoreGive(s_services_mutex);
180 ESP_LOGI(TAG, "=== TollGate services started ==="); 184 ESP_LOGI(TAG, "=== TollGate services started ===");
185
186 display_set_state(DISPLAY_READY);
187 char portal_url[128];
188 snprintf(portal_url, sizeof(portal_url), "http://%s/", cfg->ap_ip_str);
189 display_update(cfg->ap_ssid, 0, 0, portal_url);
181} 190}
182 191
183static void stop_services(void) 192static void stop_services(void)
@@ -261,6 +270,9 @@ void app_main(void)
261{ 270{
262 ESP_LOGI(TAG, "=== TollGate ESP32 Starting ==="); 271 ESP_LOGI(TAG, "=== TollGate ESP32 Starting ===");
263 272
273 display_init();
274 display_set_state(DISPLAY_BOOT);
275
264 esp_err_t ret = nvs_flash_init(); 276 esp_err_t ret = nvs_flash_init();
265 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { 277 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
266 ESP_ERROR_CHECK(nvs_flash_erase()); 278 ESP_ERROR_CHECK(nvs_flash_erase());