upleb.uk

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

summaryrefslogtreecommitdiff
path: root/main/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/config.c')
-rw-r--r--main/config.c88
1 files changed, 61 insertions, 27 deletions
diff --git a/main/config.c b/main/config.c
index 7e8a14c..47d631f 100644
--- a/main/config.c
+++ b/main/config.c
@@ -1,4 +1,5 @@
1#include "config.h" 1#include "config.h"
2#include "identity.h"
2#include "esp_log.h" 3#include "esp_log.h"
3#include "esp_spiffs.h" 4#include "esp_spiffs.h"
4#include "esp_system.h" 5#include "esp_system.h"
@@ -20,6 +21,7 @@ esp_err_t tollgate_config_init(void)
20 g_config.price_per_step = 21; 21 g_config.price_per_step = 21;
21 g_config.step_size_ms = 60000; 22 g_config.step_size_ms = 60000;
22 g_config.persist_threshold_sats = 1; 23 g_config.persist_threshold_sats = 1;
24 g_config.nostr_publish_interval_s = 21600;
23 25
24 esp_vfs_spiffs_conf_t conf = { 26 esp_vfs_spiffs_conf_t conf = {
25 .base_path = "/spiffs", 27 .base_path = "/spiffs",
@@ -37,16 +39,17 @@ esp_err_t tollgate_config_init(void)
37 if (!f) { 39 if (!f) {
38 ESP_LOGW(TAG, "No config.json found, generating default"); 40 ESP_LOGW(TAG, "No config.json found, generating default");
39 const char *default_json = "{" 41 const char *default_json = "{"
42 "\"nsec\":\"a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2\","
40 "\"wifi_networks\":[" 43 "\"wifi_networks\":["
41 "{\"ssid\":\"EnterSSID-2.4GHz\",\"password\":\"c03rad0r123!\"}" 44 "{\"ssid\":\"EnterSSID-2.4GHz\",\"password\":\"c03rad0r123!\"}"
42 "]," 45 "],"
43 "\"ap_ssid\":\"TollGate\","
44 "\"ap_password\":\"\"," 46 "\"ap_password\":\"\","
45 "\"ap_channel\":1,"
46 "\"mint_url\":\"https://testnut.cashu.space\"," 47 "\"mint_url\":\"https://testnut.cashu.space\","
47 "\"lnurl_url\":\"https://redeem.cashu.me/.well-known/lnurlp/tollgate\","
48 "\"price_per_step\":21," 48 "\"price_per_step\":21,"
49 "\"step_size_ms\":60000" 49 "\"step_size_ms\":60000,"
50 "\"nostr_geohash\":\"u281w0dfz\","
51 "\"nostr_relays\":[\"wss://relay.damus.io\",\"wss://nos.lol\"],"
52 "\"nostr_publish_interval_s\":21600"
50 "}"; 53 "}";
51 f = fopen("/spiffs/config.json", "w"); 54 f = fopen("/spiffs/config.json", "w");
52 if (f) { 55 if (f) {
@@ -80,6 +83,15 @@ esp_err_t tollgate_config_init(void)
80 return ESP_FAIL; 83 return ESP_FAIL;
81 } 84 }
82 85
86 cJSON *nsec = cJSON_GetObjectItem(root, "nsec");
87 if (nsec && cJSON_IsString(nsec)) {
88 strncpy(g_config.nsec, nsec->valuestring, sizeof(g_config.nsec) - 1);
89 } else {
90 ESP_LOGE(TAG, "Missing 'nsec' in config.json");
91 cJSON_Delete(root);
92 return ESP_FAIL;
93 }
94
83 cJSON *networks = cJSON_GetObjectItem(root, "wifi_networks"); 95 cJSON *networks = cJSON_GetObjectItem(root, "wifi_networks");
84 if (networks && cJSON_IsArray(networks)) { 96 if (networks && cJSON_IsArray(networks)) {
85 int count = cJSON_GetArraySize(networks); 97 int count = cJSON_GetArraySize(networks);
@@ -96,16 +108,9 @@ esp_err_t tollgate_config_init(void)
96 } 108 }
97 } 109 }
98 110
99 cJSON *ap_ssid = cJSON_GetObjectItem(root, "ap_ssid");
100 if (ap_ssid) strncpy(g_config.ap_ssid, ap_ssid->valuestring, sizeof(g_config.ap_ssid) - 1);
101 else strncpy(g_config.ap_ssid, "TollGate", sizeof(g_config.ap_ssid) - 1);
102
103 cJSON *ap_pass = cJSON_GetObjectItem(root, "ap_password"); 111 cJSON *ap_pass = cJSON_GetObjectItem(root, "ap_password");
104 if (ap_pass) strncpy(g_config.ap_password, ap_pass->valuestring, sizeof(g_config.ap_password) - 1); 112 if (ap_pass) strncpy(g_config.ap_password, ap_pass->valuestring, sizeof(g_config.ap_password) - 1);
105 113
106 cJSON *ap_ch = cJSON_GetObjectItem(root, "ap_channel");
107 if (ap_ch) g_config.ap_channel = ap_ch->valueint;
108
109 cJSON *mint = cJSON_GetObjectItem(root, "mint_url"); 114 cJSON *mint = cJSON_GetObjectItem(root, "mint_url");
110 if (mint) strncpy(g_config.mint_url, mint->valuestring, sizeof(g_config.mint_url) - 1); 115 if (mint) strncpy(g_config.mint_url, mint->valuestring, sizeof(g_config.mint_url) - 1);
111 116
@@ -121,9 +126,37 @@ esp_err_t tollgate_config_init(void)
121 cJSON *persist = cJSON_GetObjectItem(root, "persist_threshold_sats"); 126 cJSON *persist = cJSON_GetObjectItem(root, "persist_threshold_sats");
122 if (persist) g_config.persist_threshold_sats = (uint64_t)persist->valuedouble; 127 if (persist) g_config.persist_threshold_sats = (uint64_t)persist->valuedouble;
123 128
129 cJSON *geohash = cJSON_GetObjectItem(root, "nostr_geohash");
130 if (geohash) strncpy(g_config.nostr_geohash, geohash->valuestring, sizeof(g_config.nostr_geohash) - 1);
131 else strncpy(g_config.nostr_geohash, "u281w0dfz", sizeof(g_config.nostr_geohash) - 1);
132
133 cJSON *relays = cJSON_GetObjectItem(root, "nostr_relays");
134 if (relays && cJSON_IsArray(relays)) {
135 int rcount = cJSON_GetArraySize(relays);
136 if (rcount > TOLLGATE_MAX_RELAYS) rcount = TOLLGATE_MAX_RELAYS;
137 for (int i = 0; i < rcount; i++) {
138 cJSON *r = cJSON_GetArrayItem(relays, i);
139 if (r && cJSON_IsString(r)) {
140 strncpy(g_config.nostr_relays[i], r->valuestring, sizeof(g_config.nostr_relays[i]) - 1);
141 g_config.nostr_relay_count++;
142 }
143 }
144 }
145
146 cJSON *pub_interval = cJSON_GetObjectItem(root, "nostr_publish_interval_s");
147 if (pub_interval) g_config.nostr_publish_interval_s = pub_interval->valueint;
148
124 cJSON_Delete(root); 149 cJSON_Delete(root);
125 ESP_LOGI(TAG, "Config loaded: AP='%s', %d WiFi networks, price=%d sats/%dms", 150
126 g_config.ap_ssid, g_config.network_count, g_config.price_per_step, g_config.step_size_ms); 151 if (g_config.nostr_relay_count == 0) {
152 strncpy(g_config.nostr_relays[0], "wss://relay.damus.io", sizeof(g_config.nostr_relays[0]) - 1);
153 strncpy(g_config.nostr_relays[1], "wss://nos.lol", sizeof(g_config.nostr_relays[1]) - 1);
154 g_config.nostr_relay_count = 2;
155 }
156
157 ESP_LOGI(TAG, "Config loaded: nsec=%s...%s, %d WiFi networks, price=%d sats/%dms",
158 g_config.nsec, g_config.nsec + 60, g_config.network_count,
159 g_config.price_per_step, g_config.step_size_ms);
127 return ESP_OK; 160 return ESP_OK;
128} 161}
129 162
@@ -151,22 +184,23 @@ esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config)
151 184
152void tollgate_config_derive_unique(tollgate_config_t *cfg) 185void tollgate_config_derive_unique(tollgate_config_t *cfg)
153{ 186{
154 if (cfg->unique_derived) return; 187 if (cfg->identity_initialized) return;
155
156 uint8_t mac[6];
157 esp_read_mac(mac, ESP_MAC_WIFI_STA);
158 188
159 snprintf(cfg->ap_ssid + strlen(cfg->ap_ssid), 189 const tollgate_identity_t *id = identity_get();
160 TOLLGATE_MAX_AP_SSID_LEN - strlen(cfg->ap_ssid), 190 if (!id || !id->initialized) {
161 "-%02X%02X", mac[4], mac[5]); 191 ESP_LOGE(TAG, "Cannot derive unique config: identity not initialized");
192 return;
193 }
162 194
163 uint8_t b5 = mac[4]; 195 strncpy(cfg->ap_ssid, id->ap_ssid, sizeof(cfg->ap_ssid) - 1);
164 uint8_t b6 = mac[5]; 196 memcpy(cfg->sta_mac, id->sta_mac, 6);
165 uint8_t subnet = (b5 ^ b6) % 200 + 10; 197 memcpy(cfg->ap_mac, id->ap_mac, 6);
166 IP4_ADDR(&cfg->ap_ip, 10, b5, subnet, 1); 198 cfg->ap_ip = id->ap_ip;
167 snprintf(cfg->ap_ip_str, sizeof(cfg->ap_ip_str), IPSTR, IP2STR(&cfg->ap_ip)); 199 strncpy(cfg->ap_ip_str, id->ap_ip_str, sizeof(cfg->ap_ip_str) - 1);
200 strncpy(cfg->npub, id->npub_hex, sizeof(cfg->npub) - 1);
168 201
169 cfg->unique_derived = true; 202 cfg->identity_initialized = true;
170 203
171 ESP_LOGI(TAG, "Unique config: SSID='%s', AP_IP=%s", cfg->ap_ssid, cfg->ap_ip_str); 204 ESP_LOGI(TAG, "Unique config derived from nsec: SSID='%s', AP_IP=%s",
205 cfg->ap_ssid, cfg->ap_ip_str);
172} 206}