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.c111
1 files changed, 78 insertions, 33 deletions
diff --git a/main/config.c b/main/config.c
index b991991..5e3b247 100644
--- a/main/config.c
+++ b/main/config.c
@@ -16,7 +16,7 @@ esp_err_t tollgate_config_init(void)
16{ 16{
17 memset(&g_config, 0, sizeof(g_config)); 17 memset(&g_config, 0, sizeof(g_config));
18 g_config.max_retry = 5; 18 g_config.max_retry = 5;
19 g_config.ap_channel = 6; 19 g_config.ap_channel = 1;
20 g_config.ap_max_conn = 4; 20 g_config.ap_max_conn = 4;
21 g_config.price_per_step = 21; 21 g_config.price_per_step = 21;
22 g_config.step_size_ms = 60000; 22 g_config.step_size_ms = 60000;
@@ -24,6 +24,8 @@ esp_err_t tollgate_config_init(void)
24 strncpy(g_config.metric, "milliseconds", sizeof(g_config.metric) - 1); 24 strncpy(g_config.metric, "milliseconds", sizeof(g_config.metric) - 1);
25 g_config.persist_threshold_sats = 1; 25 g_config.persist_threshold_sats = 1;
26 g_config.nostr_publish_interval_s = 21600; 26 g_config.nostr_publish_interval_s = 21600;
27 g_config.nostr_sync_interval_s = 1800;
28 g_config.nostr_fallback_sync_interval_s = 21600;
27 g_config.client_enabled = false; 29 g_config.client_enabled = false;
28 g_config.client_steps_to_buy = 1; 30 g_config.client_steps_to_buy = 1;
29 g_config.client_renewal_threshold_pct = 20; 31 g_config.client_renewal_threshold_pct = 20;
@@ -35,8 +37,8 @@ esp_err_t tollgate_config_init(void)
35 g_config.payout.mint_count = 0; 37 g_config.payout.mint_count = 0;
36 g_config.cvm_enabled = true; 38 g_config.cvm_enabled = true;
37 strncpy(g_config.cvm_relays, "wss://relay.primal.net", sizeof(g_config.cvm_relays) - 1); 39 strncpy(g_config.cvm_relays, "wss://relay.primal.net", sizeof(g_config.cvm_relays) - 1);
38 g_config.nostr_sync_interval_s = 1800; 40 strncpy(g_config.wifi_auth_mode, "WPA2", sizeof(g_config.wifi_auth_mode) - 1);
39 g_config.nostr_fallback_sync_interval_s = 21600; 41 g_config.display_enabled = true;
40 42
41 esp_vfs_spiffs_conf_t conf = { 43 esp_vfs_spiffs_conf_t conf = {
42 .base_path = "/spiffs", 44 .base_path = "/spiffs",
@@ -56,17 +58,18 @@ esp_err_t tollgate_config_init(void)
56 const char *default_json = "{" 58 const char *default_json = "{"
57 "\"nsec\":\"a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2\"," 59 "\"nsec\":\"a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2\","
58 "\"wifi_networks\":[" 60 "\"wifi_networks\":["
59 "{\"ssid\":\"EnterSSID-2.4GHz\",\"password\":\"c03rad0r123!\"}," 61 "{\"ssid\":\"c03rad0r\",\"password\":\"c03rad0r123\"}"
60 "{\"ssid\":\"c03rad0r\",\"password\":\"c03rad0r123\"},"
61 "{\"ssid\":\"TK-GAESTE\",\"password\":\"\"}"
62 "]," 62 "],"
63 "\"ap_password\":\"\"," 63 "\"ap_password\":\"\","
64 "\"mint_url\":\"https://testnut.cashu.space\"," 64 "\"mint_url\":\"https://testnut.cashu.space\","
65 "\"accepted_mints\":[\"https://testnut.cashu.space\"],"
65 "\"price_per_step\":21," 66 "\"price_per_step\":21,"
66 "\"step_size_ms\":60000," 67 "\"step_size_ms\":60000,"
67 "\"nostr_geohash\":\"u281w0dfz\"," 68 "\"nostr_geohash\":\"u281w0dfz\","
68 "\"nostr_relays\":[\"wss://relay.damus.io\",\"wss://nos.lol\"]," 69 "\"nostr_relays\":[\"wss://relay.damus.io\",\"wss://nos.lol\"],"
69 "\"nostr_publish_interval_s\":21600," 70 "\"nostr_publish_interval_s\":21600,"
71 "\"nostr_sync_interval_s\":1800,"
72 "\"nostr_fallback_sync_interval_s\":21600,"
70 "\"client_enabled\":false," 73 "\"client_enabled\":false,"
71 "\"client_steps_to_buy\":1," 74 "\"client_steps_to_buy\":1,"
72 "\"client_renewal_threshold_pct\":20," 75 "\"client_renewal_threshold_pct\":20,"
@@ -129,12 +132,36 @@ esp_err_t tollgate_config_init(void)
129 } 132 }
130 } 133 }
131 134
135 if (g_config.network_count == 0) {
136 cJSON *ssid = cJSON_GetObjectItem(root, "wifi_ssid");
137 cJSON *pass = cJSON_GetObjectItem(root, "wifi_password");
138 if (ssid && cJSON_IsString(ssid) && pass && cJSON_IsString(pass)) {
139 strncpy(g_config.networks[0].ssid, ssid->valuestring, sizeof(g_config.networks[0].ssid) - 1);
140 strncpy(g_config.networks[0].password, pass->valuestring, sizeof(g_config.networks[0].password) - 1);
141 g_config.network_count = 1;
142 }
143 }
144
132 cJSON *ap_pass = cJSON_GetObjectItem(root, "ap_password"); 145 cJSON *ap_pass = cJSON_GetObjectItem(root, "ap_password");
133 if (ap_pass) strncpy(g_config.ap_password, ap_pass->valuestring, sizeof(g_config.ap_password) - 1); 146 if (ap_pass) strncpy(g_config.ap_password, ap_pass->valuestring, sizeof(g_config.ap_password) - 1);
134 147
135 cJSON *mint = cJSON_GetObjectItem(root, "mint_url"); 148 cJSON *mint = cJSON_GetObjectItem(root, "mint_url");
136 if (mint) strncpy(g_config.mint_url, mint->valuestring, sizeof(g_config.mint_url) - 1); 149 if (mint) strncpy(g_config.mint_url, mint->valuestring, sizeof(g_config.mint_url) - 1);
137 150
151 cJSON *acc_mints = cJSON_GetObjectItem(root, "accepted_mints");
152 if (acc_mints && cJSON_IsArray(acc_mints)) {
153 int mcount = cJSON_GetArraySize(acc_mints);
154 if (mcount > TOLLGATE_MAX_MINT_URLS) mcount = TOLLGATE_MAX_MINT_URLS;
155 for (int i = 0; i < mcount; i++) {
156 cJSON *m = cJSON_GetArrayItem(acc_mints, i);
157 if (m && cJSON_IsString(m)) {
158 strncpy(g_config.accepted_mints[i], m->valuestring,
159 sizeof(g_config.accepted_mints[i]) - 1);
160 g_config.accepted_mint_count++;
161 }
162 }
163 }
164
138 cJSON *lnurl = cJSON_GetObjectItem(root, "lnurl_url"); 165 cJSON *lnurl = cJSON_GetObjectItem(root, "lnurl_url");
139 if (lnurl) strncpy(g_config.lnurl_url, lnurl->valuestring, sizeof(g_config.lnurl_url) - 1); 166 if (lnurl) strncpy(g_config.lnurl_url, lnurl->valuestring, sizeof(g_config.lnurl_url) - 1);
140 167
@@ -175,6 +202,26 @@ esp_err_t tollgate_config_init(void)
175 cJSON *pub_interval = cJSON_GetObjectItem(root, "nostr_publish_interval_s"); 202 cJSON *pub_interval = cJSON_GetObjectItem(root, "nostr_publish_interval_s");
176 if (pub_interval) g_config.nostr_publish_interval_s = pub_interval->valueint; 203 if (pub_interval) g_config.nostr_publish_interval_s = pub_interval->valueint;
177 204
205 cJSON *sync_interval = cJSON_GetObjectItem(root, "nostr_sync_interval_s");
206 if (sync_interval) g_config.nostr_sync_interval_s = sync_interval->valueint;
207
208 cJSON *fallback_interval = cJSON_GetObjectItem(root, "nostr_fallback_sync_interval_s");
209 if (fallback_interval) g_config.nostr_fallback_sync_interval_s = fallback_interval->valueint;
210
211 cJSON *seed_relays = cJSON_GetObjectItem(root, "nostr_seed_relays");
212 if (seed_relays && cJSON_IsArray(seed_relays)) {
213 int srcount = cJSON_GetArraySize(seed_relays);
214 if (srcount > TOLLGATE_MAX_SEED_RELAYS) srcount = TOLLGATE_MAX_SEED_RELAYS;
215 for (int i = 0; i < srcount; i++) {
216 cJSON *r = cJSON_GetArrayItem(seed_relays, i);
217 if (r && cJSON_IsString(r)) {
218 strncpy(g_config.nostr_seed_relays[i], r->valuestring,
219 sizeof(g_config.nostr_seed_relays[i]) - 1);
220 g_config.nostr_seed_relay_count++;
221 }
222 }
223 }
224
178 cJSON *client_enabled = cJSON_GetObjectItem(root, "client_enabled"); 225 cJSON *client_enabled = cJSON_GetObjectItem(root, "client_enabled");
179 if (client_enabled && cJSON_IsBool(client_enabled)) g_config.client_enabled = cJSON_IsTrue(client_enabled); 226 if (client_enabled && cJSON_IsBool(client_enabled)) g_config.client_enabled = cJSON_IsTrue(client_enabled);
180 227
@@ -251,6 +298,14 @@ esp_err_t tollgate_config_init(void)
251 } 298 }
252 } 299 }
253 300
301 cJSON *auth_mode = cJSON_GetObjectItem(root, "wifi_auth_mode");
302 if (auth_mode && cJSON_IsString(auth_mode)) {
303 strncpy(g_config.wifi_auth_mode, auth_mode->valuestring, sizeof(g_config.wifi_auth_mode) - 1);
304 }
305
306 cJSON *disp_en = cJSON_GetObjectItem(root, "display_enabled");
307 if (disp_en && cJSON_IsBool(disp_en)) g_config.display_enabled = cJSON_IsTrue(disp_en);
308
254 if (g_config.payout.mint_count == 0 && g_config.mint_url[0] != '\0') { 309 if (g_config.payout.mint_count == 0 && g_config.mint_url[0] != '\0') {
255 strncpy(g_config.payout.mints[0].url, g_config.mint_url, 310 strncpy(g_config.payout.mints[0].url, g_config.mint_url,
256 sizeof(g_config.payout.mints[0].url) - 1); 311 sizeof(g_config.payout.mints[0].url) - 1);
@@ -259,28 +314,6 @@ esp_err_t tollgate_config_init(void)
259 g_config.payout.mint_count = 1; 314 g_config.payout.mint_count = 1;
260 } 315 }
261 316
262 cJSON *seed_relays = cJSON_GetObjectItem(root, "nostr_seed_relays");
263 if (seed_relays && cJSON_IsArray(seed_relays)) {
264 int srcount = cJSON_GetArraySize(seed_relays);
265 if (srcount > TOLLGATE_MAX_SEED_RELAYS) srcount = TOLLGATE_MAX_SEED_RELAYS;
266 for (int i = 0; i < srcount; i++) {
267 cJSON *r = cJSON_GetArrayItem(seed_relays, i);
268 if (r && cJSON_IsString(r)) {
269 strncpy(g_config.nostr_seed_relays[i], r->valuestring,
270 sizeof(g_config.nostr_seed_relays[i]) - 1);
271 g_config.nostr_seed_relay_count++;
272 }
273 }
274 }
275
276 cJSON *sync_interval = cJSON_GetObjectItem(root, "nostr_sync_interval_s");
277 if (sync_interval) g_config.nostr_sync_interval_s = sync_interval->valueint;
278
279 cJSON *fallback_interval = cJSON_GetObjectItem(root, "nostr_fallback_sync_interval_s");
280 if (fallback_interval) g_config.nostr_fallback_sync_interval_s = fallback_interval->valueint;
281
282 cJSON_Delete(root);
283
284 if (g_config.payout.recipient_count == 0) { 317 if (g_config.payout.recipient_count == 0) {
285 strncpy(g_config.payout.recipients[0].lightning_address, "TollGate@coinos.io", 318 strncpy(g_config.payout.recipients[0].lightning_address, "TollGate@coinos.io",
286 sizeof(g_config.payout.recipients[0].lightning_address) - 1); 319 sizeof(g_config.payout.recipients[0].lightning_address) - 1);
@@ -288,6 +321,14 @@ esp_err_t tollgate_config_init(void)
288 g_config.payout.recipient_count = 1; 321 g_config.payout.recipient_count = 1;
289 } 322 }
290 323
324 cJSON_Delete(root);
325
326 if (g_config.accepted_mint_count == 0 && g_config.mint_url[0] != '\0') {
327 strncpy(g_config.accepted_mints[0], g_config.mint_url,
328 sizeof(g_config.accepted_mints[0]) - 1);
329 g_config.accepted_mint_count = 1;
330 }
331
291 if (g_config.nostr_relay_count == 0) { 332 if (g_config.nostr_relay_count == 0) {
292 strncpy(g_config.nostr_relays[0], "wss://relay.damus.io", sizeof(g_config.nostr_relays[0]) - 1); 333 strncpy(g_config.nostr_relays[0], "wss://relay.damus.io", sizeof(g_config.nostr_relays[0]) - 1);
293 strncpy(g_config.nostr_relays[1], "wss://nos.lol", sizeof(g_config.nostr_relays[1]) - 1); 334 strncpy(g_config.nostr_relays[1], "wss://nos.lol", sizeof(g_config.nostr_relays[1]) - 1);
@@ -306,9 +347,9 @@ esp_err_t tollgate_config_init(void)
306 g_config.nostr_seed_relay_count = 4; 347 g_config.nostr_seed_relay_count = 4;
307 } 348 }
308 349
309 ESP_LOGI(TAG, "Config loaded: nsec=%s...%s, %d WiFi networks, price=%d sats/%dms", 350 ESP_LOGI(TAG, "Config loaded: nsec=%s...%s, %d WiFi networks, %d accepted mints, price=%d sats/%dms",
310 g_config.nsec, g_config.nsec + 60, g_config.network_count, 351 g_config.nsec, g_config.nsec + 60, g_config.network_count,
311 g_config.price_per_step, g_config.step_size_ms); 352 g_config.accepted_mint_count, g_config.price_per_step, g_config.step_size_ms);
312 return ESP_OK; 353 return ESP_OK;
313} 354}
314 355
@@ -325,14 +366,18 @@ esp_err_t tollgate_config_get_wifi(wifi_config_t *wifi_config)
325 strncpy((char *)wifi_config->sta.ssid, g_config.networks[idx].ssid, sizeof(wifi_config->sta.ssid) - 1); 366 strncpy((char *)wifi_config->sta.ssid, g_config.networks[idx].ssid, sizeof(wifi_config->sta.ssid) - 1);
326 strncpy((char *)wifi_config->sta.password, g_config.networks[idx].password, sizeof(wifi_config->sta.password) - 1); 367 strncpy((char *)wifi_config->sta.password, g_config.networks[idx].password, sizeof(wifi_config->sta.password) - 1);
327 wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; 368 wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
328 wifi_config->sta.pmf_cfg.capable = true; 369 if (strstr(g_config.wifi_auth_mode, "WPA3")) {
329 wifi_config->sta.pmf_cfg.required = false; 370 wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA3_PSK;
330 wifi_config->sta.scan_method = WIFI_ALL_CHANNEL_SCAN; 371 } else if (strstr(g_config.wifi_auth_mode, "WPA2")) {
372 wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
373 }
374 ESP_LOGI(TAG, "STA auth threshold: %s -> %d", g_config.wifi_auth_mode, wifi_config->sta.threshold.authmode);
331 return ESP_OK; 375 return ESP_OK;
332} 376}
333 377
334esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config) 378esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config)
335{ 379{
380 if (g_config.network_count == 0) return ESP_ERR_NOT_FOUND;
336 g_config.current_network = (g_config.current_network + 1) % g_config.network_count; 381 g_config.current_network = (g_config.current_network + 1) % g_config.network_count;
337 return tollgate_config_get_wifi(wifi_config); 382 return tollgate_config_get_wifi(wifi_config);
338} 383}