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-18 22:49:34 +0530
committerYour Name <you@example.com>2026-05-18 22:49:34 +0530
commiteb2ae72d91a4b2ec0333a2e6d18eeeeee6b60464 (patch)
treeeff643bcd00ed44b5d7fd93084be1391970d6bd4 /main
parent910fe47b2ed1284d93ff4d274f5c2341cb0d2d0b (diff)
feat: add WiFi setup state machine + config_add_wifi with unit tests
Diffstat (limited to 'main')
-rw-r--r--main/config.c57
-rw-r--r--main/config.h1
-rw-r--r--main/wifi_setup.c89
-rw-r--r--main/wifi_setup.h51
4 files changed, 198 insertions, 0 deletions
diff --git a/main/config.c b/main/config.c
index 6753f40..32ae30e 100644
--- a/main/config.c
+++ b/main/config.c
@@ -320,3 +320,60 @@ void tollgate_config_derive_unique(tollgate_config_t *cfg)
320 ESP_LOGI(TAG, "Unique config derived from nsec: SSID='%s', AP_IP=%s", 320 ESP_LOGI(TAG, "Unique config derived from nsec: SSID='%s', AP_IP=%s",
321 cfg->ap_ssid, cfg->ap_ip_str); 321 cfg->ap_ssid, cfg->ap_ip_str);
322} 322}
323
324esp_err_t tollgate_config_add_wifi(const char *ssid, const char *password) {
325 if (!ssid || !password) return ESP_ERR_INVALID_ARG;
326 if (g_config.network_count >= TOLLGATE_MAX_WIFI_NETWORKS) return ESP_ERR_NO_MEM;
327
328 strncpy(g_config.networks[g_config.network_count].ssid, ssid,
329 sizeof(g_config.networks[g_config.network_count].ssid) - 1);
330 strncpy(g_config.networks[g_config.network_count].password, password,
331 sizeof(g_config.networks[g_config.network_count].password) - 1);
332 g_config.network_count++;
333
334 cJSON *root = cJSON_CreateObject();
335 cJSON *nsec = cJSON_CreateString(g_config.nsec);
336 cJSON_AddItemToObject(root, "nsec", nsec);
337
338 cJSON *networks = cJSON_CreateArray();
339 for (int i = 0; i < g_config.network_count; i++) {
340 cJSON *net = cJSON_CreateObject();
341 cJSON_AddStringToObject(net, "ssid", g_config.networks[i].ssid);
342 cJSON_AddStringToObject(net, "password", g_config.networks[i].password);
343 cJSON_AddItemToArray(networks, net);
344 }
345 cJSON_AddItemToObject(root, "wifi_networks", networks);
346
347 if (g_config.ap_password[0])
348 cJSON_AddStringToObject(root, "ap_password", g_config.ap_password);
349 cJSON_AddStringToObject(root, "mint_url", g_config.mint_url);
350 cJSON_AddNumberToObject(root, "price_per_step", g_config.price_per_step);
351 cJSON_AddNumberToObject(root, "step_size_ms", g_config.step_size_ms);
352
353 if (g_config.metric[0])
354 cJSON_AddStringToObject(root, "metric", g_config.metric);
355
356 cJSON_AddStringToObject(root, "nostr_geohash", g_config.nostr_geohash);
357
358 cJSON *relays = cJSON_CreateArray();
359 for (int i = 0; i < g_config.nostr_relay_count; i++) {
360 cJSON_AddItemToArray(relays, cJSON_CreateString(g_config.nostr_relays[i]));
361 }
362 cJSON_AddItemToObject(root, "nostr_relays", relays);
363 cJSON_AddNumberToObject(root, "nostr_publish_interval_s", g_config.nostr_publish_interval_s);
364
365 char *json_str = cJSON_Print(root);
366 cJSON_Delete(root);
367
368 FILE *f = fopen("/spiffs/config.json", "w");
369 if (!f) {
370 free(json_str);
371 return ESP_FAIL;
372 }
373 fputs(json_str, f);
374 fclose(f);
375 free(json_str);
376
377 ESP_LOGI(TAG, "Added WiFi network '%s' to config (total: %d)", ssid, g_config.network_count);
378 return ESP_OK;
379}
diff --git a/main/config.h b/main/config.h
index fa4d95c..dad0ef0 100644
--- a/main/config.h
+++ b/main/config.h
@@ -71,5 +71,6 @@ esp_err_t tollgate_config_init(void);
71const tollgate_config_t *tollgate_config_get(void); 71const tollgate_config_t *tollgate_config_get(void);
72esp_err_t tollgate_config_get_wifi(wifi_config_t *wifi_config); 72esp_err_t tollgate_config_get_wifi(wifi_config_t *wifi_config);
73esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config); 73esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config);
74esp_err_t tollgate_config_add_wifi(const char *ssid, const char *password);
74 75
75#endif 76#endif
diff --git a/main/wifi_setup.c b/main/wifi_setup.c
new file mode 100644
index 0000000..b2669e9
--- /dev/null
+++ b/main/wifi_setup.c
@@ -0,0 +1,89 @@
1#include "wifi_setup.h"
2#include <string.h>
3
4void wifi_setup_init(wifi_setup_t *setup) {
5 if (!setup) return;
6 memset(setup, 0, sizeof(*setup));
7 setup->state = SETUP_SCAN;
8 setup->selected_ap = -1;
9}
10
11void wifi_setup_set_aps(wifi_setup_t *setup, const wifi_ap_info_t *aps, int count) {
12 if (!setup || !aps) return;
13 if (count > WIFI_SETUP_MAX_APS) count = WIFI_SETUP_MAX_APS;
14 memcpy(setup->aps, aps, count * sizeof(wifi_ap_info_t));
15 setup->ap_count = count;
16 setup->list_scroll = 0;
17 setup->state = SETUP_LIST;
18}
19
20int wifi_setup_visible_count(const wifi_setup_t *setup) {
21 if (!setup) return 0;
22 int remaining = setup->ap_count - setup->list_scroll;
23 if (remaining > WIFI_SETUP_MAX_VISIBLE) remaining = WIFI_SETUP_MAX_VISIBLE;
24 return remaining < 0 ? 0 : remaining;
25}
26
27const wifi_ap_info_t *wifi_setup_get_visible(const wifi_setup_t *setup, int idx) {
28 if (!setup || idx < 0 || idx >= wifi_setup_visible_count(setup)) return NULL;
29 return &setup->aps[setup->list_scroll + idx];
30}
31
32setup_state_t wifi_setup_handle_select(wifi_setup_t *setup, int list_idx) {
33 if (!setup || setup->state != SETUP_LIST) return setup ? setup->state : SETUP_CANCELLED;
34 if (list_idx < 0 || list_idx >= wifi_setup_visible_count(setup)) return setup->state;
35
36 int real_idx = setup->list_scroll + list_idx;
37 setup->selected_ap = real_idx;
38 strncpy(setup->selected_ssid, setup->aps[real_idx].ssid, WIFI_SETUP_SSID_LEN - 1);
39 setup->selected_ssid[WIFI_SETUP_SSID_LEN - 1] = '\0';
40 setup->state = SETUP_PASSWORD;
41 return setup->state;
42}
43
44setup_state_t wifi_setup_handle_connect(wifi_setup_t *setup) {
45 if (!setup || setup->state != SETUP_PASSWORD) return setup ? setup->state : SETUP_CANCELLED;
46 setup->state = SETUP_CONNECTING;
47 return setup->state;
48}
49
50setup_state_t wifi_setup_handle_connect_result(wifi_setup_t *setup, bool success, const char *ip) {
51 if (!setup) return SETUP_CANCELLED;
52 if (setup->state != SETUP_CONNECTING) return setup->state;
53
54 if (success) {
55 setup->state = SETUP_SUCCESS;
56 if (ip) {
57 strncpy(setup->connect_ip, ip, sizeof(setup->connect_ip) - 1);
58 setup->connect_ip[sizeof(setup->connect_ip) - 1] = '\0';
59 }
60 setup->connect_failed_auth = false;
61 } else {
62 setup->state = SETUP_FAILED;
63 setup->connect_failed_auth = true;
64 setup->connect_ip[0] = '\0';
65 }
66 return setup->state;
67}
68
69setup_state_t wifi_setup_handle_cancel(wifi_setup_t *setup) {
70 if (!setup) return SETUP_CANCELLED;
71 setup->state = SETUP_CANCELLED;
72 return setup->state;
73}
74
75setup_state_t wifi_setup_handle_retry(wifi_setup_t *setup) {
76 if (!setup) return SETUP_CANCELLED;
77 if (setup->state != SETUP_FAILED) return setup->state;
78 setup->state = SETUP_PASSWORD;
79 setup->connect_failed_auth = false;
80 return setup->state;
81}
82
83setup_state_t wifi_setup_handle_change_network(wifi_setup_t *setup) {
84 if (!setup) return SETUP_CANCELLED;
85 if (setup->state != SETUP_FAILED) return setup->state;
86 setup->state = SETUP_LIST;
87 setup->connect_failed_auth = false;
88 return setup->state;
89}
diff --git a/main/wifi_setup.h b/main/wifi_setup.h
new file mode 100644
index 0000000..17712d5
--- /dev/null
+++ b/main/wifi_setup.h
@@ -0,0 +1,51 @@
1#ifndef WIFI_SETUP_H
2#define WIFI_SETUP_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stdbool.h>
7
8#define WIFI_SETUP_MAX_APS 20
9#define WIFI_SETUP_MAX_VISIBLE 8
10#define WIFI_SETUP_SSID_LEN 33
11#define WIFI_SETUP_PASS_LEN 64
12
13typedef enum {
14 SETUP_SCAN,
15 SETUP_LIST,
16 SETUP_PASSWORD,
17 SETUP_CONNECTING,
18 SETUP_SUCCESS,
19 SETUP_FAILED,
20 SETUP_CANCELLED
21} setup_state_t;
22
23typedef struct {
24 char ssid[WIFI_SETUP_SSID_LEN];
25 int rssi;
26 bool secured;
27} wifi_ap_info_t;
28
29typedef struct {
30 setup_state_t state;
31 wifi_ap_info_t aps[WIFI_SETUP_MAX_APS];
32 int ap_count;
33 int list_scroll;
34 int selected_ap;
35 char selected_ssid[WIFI_SETUP_SSID_LEN];
36 char connect_ip[16];
37 bool connect_failed_auth;
38} wifi_setup_t;
39
40void wifi_setup_init(wifi_setup_t *setup);
41void wifi_setup_set_aps(wifi_setup_t *setup, const wifi_ap_info_t *aps, int count);
42int wifi_setup_visible_count(const wifi_setup_t *setup);
43const wifi_ap_info_t *wifi_setup_get_visible(const wifi_setup_t *setup, int idx);
44setup_state_t wifi_setup_handle_select(wifi_setup_t *setup, int list_idx);
45setup_state_t wifi_setup_handle_connect(wifi_setup_t *setup);
46setup_state_t wifi_setup_handle_connect_result(wifi_setup_t *setup, bool success, const char *ip);
47setup_state_t wifi_setup_handle_cancel(wifi_setup_t *setup);
48setup_state_t wifi_setup_handle_retry(wifi_setup_t *setup);
49setup_state_t wifi_setup_handle_change_network(wifi_setup_t *setup);
50
51#endif