diff -r 332e85569339 -r 715785443a95 main/config.c --- a/main/config.c Tue Oct 03 19:55:34 2023 +0200 +++ b/main/config.c Wed Oct 04 11:28:49 2023 +0200 @@ -10,7 +10,7 @@ unit_t units[3]; ///< Pressure test units SemaphoreHandle_t xSemaphoreUnits = NULL; ///< Semaphore Units records -wifiStation_t wifiStation; +//wifiStation_t wifiStation; @@ -76,114 +76,3 @@ } - -int add_station(uint8_t *SSID, uint8_t *Password) -{ - FILE *f; - uint8_t *dst = (uint8_t *)&wifiStation; - - if (read_station(SSID) >= 0) { - ESP_LOGE(TAG, "add_station %s already exists", SSID); - return -1; - } - - f = fopen("/spiffs/stations.conf", "a+"); - if (f == NULL) { - ESP_LOGE(TAG, "write /spiffs/stations.conf failed"); - return -1; - } - memset(dst, 0, sizeof(wifiStation)); - sprintf(wifiStation.SSID, "%s", (char *)SSID); - sprintf(wifiStation.Password, "%s", (char *)Password); - fwrite(dst, 1, sizeof(wifiStation), f); - fclose(f); - - ESP_LOGI(TAG, "add_station %s record: %d", (char *)SSID, read_station(SSID)); - /* Return the record number */ - return read_station(SSID); -} - - - -int read_station(uint8_t *SSID) -{ - uint8_t *dst = (uint8_t *)&wifiStation; - static int rc; - FILE *f; - size_t bytes; - - if ((SSID == NULL) || (strlen((char *)SSID) == 0)) { - ESP_LOGI(TAG, "read_station(NULL)"); - return -1; - } - - memset(dst, 0, sizeof(wifiStation)); - f = fopen("/spiffs/stations.conf", "r+"); - if (f == NULL) { - f = fopen("/spiffs/stations.conf", "w+"); - fclose(f); - ESP_LOGI(TAG, "/spiffs/stations.conf created, return -1"); - return -1; - } - - rc = 0; - fseek(f, 0, SEEK_SET); - - while (1) { - bytes = fread(dst, 1, sizeof(wifiStation), f); - ESP_LOGI(TAG, " read_station bytes %d size %d", bytes, sizeof(wifiStation)); - if (bytes < sizeof(wifiStation)) { - fclose(f); - memset(dst, 0, sizeof(wifiStation)); - return -1; - } - if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { - // Fount it - fclose(f); - return rc; - } - rc++; - } - return -1; -} - - - -void remove_station(uint8_t *SSID) -{ - FILE *n, *o; - uint8_t *dst; - size_t bytes; - - n = fopen("/spiffs/stations.new", "a"); - if (n == NULL) { - ESP_LOGE(TAG, "cannot create /spiffs/stations.new"); - return; - } - o = fopen("/spiffs/stations.conf", "r"); - if (o == NULL) { - ESP_LOGE(TAG, "cannot open spiffs/stations.conf for reading"); - fclose(n); - return; - } - - dst = (uint8_t*)&wifiStation; - while (true) { - bytes = fread(dst, 1, sizeof(wifiStation), o); - if (bytes == 0) - break; - - if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) { - ESP_LOGI(TAG, "remove station %s", (char *)SSID); - } else { - fwrite(dst, 1, sizeof(wifiStation), n); - } - } - fclose(o); - fclose(n); - - rename("/spiffs/stations.conf", "/spiffs/stations.old"); - rename("/spiffs/stations.new", "/spiffs/stations.conf"); - unlink("/spiffs/stations.old"); -} -