diff options
Diffstat (limited to 'main/config.c')
| -rw-r--r-- | main/config.c | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/main/config.c b/main/config.c new file mode 100644 index 0000000..f78bc8b --- /dev/null +++ b/main/config.c | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | #include "config.h" | ||
| 2 | #include "esp_log.h" | ||
| 3 | #include "esp_spiffs.h" | ||
| 4 | #include "cJSON.h" | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | static const char *TAG = "tollgate_config"; | ||
| 8 | static tollgate_config_t g_config; | ||
| 9 | |||
| 10 | esp_err_t tollgate_config_init(void) | ||
| 11 | { | ||
| 12 | memset(&g_config, 0, sizeof(g_config)); | ||
| 13 | g_config.max_retry = 5; | ||
| 14 | g_config.ap_channel = 1; | ||
| 15 | g_config.ap_max_conn = 4; | ||
| 16 | g_config.price_per_step = 21; | ||
| 17 | g_config.step_size_ms = 60000; | ||
| 18 | |||
| 19 | esp_vfs_spiffs_conf_t conf = { | ||
| 20 | .base_path = "/spiffs", | ||
| 21 | .partition_label = NULL, | ||
| 22 | .max_files = 5, | ||
| 23 | .format_if_mount_failed = true, | ||
| 24 | }; | ||
| 25 | esp_err_t ret = esp_vfs_spiffs_register(&conf); | ||
| 26 | if (ret != ESP_OK) { | ||
| 27 | ESP_LOGE(TAG, "Failed to mount SPIFFS: %s", esp_err_to_name(ret)); | ||
| 28 | return ret; | ||
| 29 | } | ||
| 30 | |||
| 31 | FILE *f = fopen("/spiffs/config.json", "r"); | ||
| 32 | if (!f) { | ||
| 33 | ESP_LOGW(TAG, "No config.json found, generating default"); | ||
| 34 | const char *default_json = "{" | ||
| 35 | "\"wifi_networks\":[" | ||
| 36 | "{\"ssid\":\"EnterSSID-2.4GHz\",\"password\":\"c03rad0r123!\"}" | ||
| 37 | "]," | ||
| 38 | "\"ap_ssid\":\"TollGate\"," | ||
| 39 | "\"ap_password\":\"\"," | ||
| 40 | "\"ap_channel\":1," | ||
| 41 | "\"mint_url\":\"https://nofee.testnut.cashu.space\"," | ||
| 42 | "\"lnurl_url\":\"https://redeem.cashu.me/.well-known/lnurlp/tollgate\"," | ||
| 43 | "\"price_per_step\":21," | ||
| 44 | "\"step_size_ms\":60000" | ||
| 45 | "}"; | ||
| 46 | f = fopen("/spiffs/config.json", "w"); | ||
| 47 | if (f) { | ||
| 48 | fputs(default_json, f); | ||
| 49 | fclose(f); | ||
| 50 | } | ||
| 51 | f = fopen("/spiffs/config.json", "r"); | ||
| 52 | } | ||
| 53 | |||
| 54 | if (!f) { | ||
| 55 | ESP_LOGE(TAG, "Failed to open config.json"); | ||
| 56 | return ESP_FAIL; | ||
| 57 | } | ||
| 58 | |||
| 59 | fseek(f, 0, SEEK_END); | ||
| 60 | long fsize = ftell(f); | ||
| 61 | fseek(f, 0, SEEK_SET); | ||
| 62 | char *buf = malloc(fsize + 1); | ||
| 63 | if (!buf) { | ||
| 64 | fclose(f); | ||
| 65 | return ESP_ERR_NO_MEM; | ||
| 66 | } | ||
| 67 | fread(buf, 1, fsize, f); | ||
| 68 | buf[fsize] = '\0'; | ||
| 69 | fclose(f); | ||
| 70 | |||
| 71 | cJSON *root = cJSON_Parse(buf); | ||
| 72 | free(buf); | ||
| 73 | if (!root) { | ||
| 74 | ESP_LOGE(TAG, "Failed to parse config.json"); | ||
| 75 | return ESP_FAIL; | ||
| 76 | } | ||
| 77 | |||
| 78 | cJSON *networks = cJSON_GetObjectItem(root, "wifi_networks"); | ||
| 79 | if (networks && cJSON_IsArray(networks)) { | ||
| 80 | int count = cJSON_GetArraySize(networks); | ||
| 81 | if (count > TOLLGATE_MAX_WIFI_NETWORKS) count = TOLLGATE_MAX_WIFI_NETWORKS; | ||
| 82 | for (int i = 0; i < count; i++) { | ||
| 83 | cJSON *net = cJSON_GetArrayItem(networks, i); | ||
| 84 | cJSON *ssid = cJSON_GetObjectItem(net, "ssid"); | ||
| 85 | cJSON *pass = cJSON_GetObjectItem(net, "password"); | ||
| 86 | if (ssid && pass) { | ||
| 87 | strncpy(g_config.networks[i].ssid, ssid->valuestring, sizeof(g_config.networks[i].ssid) - 1); | ||
| 88 | strncpy(g_config.networks[i].password, pass->valuestring, sizeof(g_config.networks[i].password) - 1); | ||
| 89 | g_config.network_count++; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | cJSON *ap_ssid = cJSON_GetObjectItem(root, "ap_ssid"); | ||
| 95 | if (ap_ssid) strncpy(g_config.ap_ssid, ap_ssid->valuestring, sizeof(g_config.ap_ssid) - 1); | ||
| 96 | else strncpy(g_config.ap_ssid, "TollGate", sizeof(g_config.ap_ssid) - 1); | ||
| 97 | |||
| 98 | cJSON *ap_pass = cJSON_GetObjectItem(root, "ap_password"); | ||
| 99 | if (ap_pass) strncpy(g_config.ap_password, ap_pass->valuestring, sizeof(g_config.ap_password) - 1); | ||
| 100 | |||
| 101 | cJSON *ap_ch = cJSON_GetObjectItem(root, "ap_channel"); | ||
| 102 | if (ap_ch) g_config.ap_channel = ap_ch->valueint; | ||
| 103 | |||
| 104 | cJSON *mint = cJSON_GetObjectItem(root, "mint_url"); | ||
| 105 | if (mint) strncpy(g_config.mint_url, mint->valuestring, sizeof(g_config.mint_url) - 1); | ||
| 106 | |||
| 107 | cJSON *lnurl = cJSON_GetObjectItem(root, "lnurl_url"); | ||
| 108 | if (lnurl) strncpy(g_config.lnurl_url, lnurl->valuestring, sizeof(g_config.lnurl_url) - 1); | ||
| 109 | |||
| 110 | cJSON *price = cJSON_GetObjectItem(root, "price_per_step"); | ||
| 111 | if (price) g_config.price_per_step = price->valueint; | ||
| 112 | |||
| 113 | cJSON *step = cJSON_GetObjectItem(root, "step_size_ms"); | ||
| 114 | if (step) g_config.step_size_ms = step->valueint; | ||
| 115 | |||
| 116 | cJSON_Delete(root); | ||
| 117 | ESP_LOGI(TAG, "Config loaded: AP='%s', %d WiFi networks, price=%d sats/%dms", | ||
| 118 | g_config.ap_ssid, g_config.network_count, g_config.price_per_step, g_config.step_size_ms); | ||
| 119 | return ESP_OK; | ||
| 120 | } | ||
| 121 | |||
| 122 | const tollgate_config_t *tollgate_config_get(void) | ||
| 123 | { | ||
| 124 | return &g_config; | ||
| 125 | } | ||
| 126 | |||
| 127 | esp_err_t tollgate_config_get_wifi(wifi_config_t *wifi_config) | ||
| 128 | { | ||
| 129 | if (g_config.network_count == 0) return ESP_ERR_NOT_FOUND; | ||
| 130 | int idx = g_config.current_network % g_config.network_count; | ||
| 131 | memset(wifi_config, 0, sizeof(wifi_config_t)); | ||
| 132 | strncpy((char *)wifi_config->sta.ssid, g_config.networks[idx].ssid, sizeof(wifi_config->sta.ssid) - 1); | ||
| 133 | strncpy((char *)wifi_config->sta.password, g_config.networks[idx].password, sizeof(wifi_config->sta.password) - 1); | ||
| 134 | wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; | ||
| 135 | return ESP_OK; | ||
| 136 | } | ||
| 137 | |||
| 138 | esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config) | ||
| 139 | { | ||
| 140 | g_config.current_network = (g_config.current_network + 1) % g_config.network_count; | ||
| 141 | return tollgate_config_get_wifi(wifi_config); | ||
| 142 | } | ||