main/config.c

Tue, 03 Oct 2023 17:24:06 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 03 Oct 2023 17:24:06 +0200
changeset 77
15dc572a7fcb
parent 74
34da2d2b12d5
child 80
715785443a95
permissions
-rw-r--r--

Version 0.3.0. Backported network code from experimental roaming project. Will now connect after reset to the strongest AP. Id the signal level drops below -67, extra scans are done to see for a better AP. Nothing is done yet. Removed config.conf file, all info is taken from the project menu and live tests. Better log the board type and send it via json mqtt. Send bssid and current channel too.

/**
 * @file config.c
 * @brief The 'co2meter' configuration data. These are stored in the 
 *        spiffs filesystem in a flash partition.
 */

#include "config.h"

static const char *TAG = "config";

unit_t				units[3];                       ///< Pressure test units
SemaphoreHandle_t		xSemaphoreUnits = NULL;		///< Semaphore Units records
wifiStation_t			wifiStation;



void write_units() {
    uint8_t     *dst = (uint8_t *)&units;
    FILE        *f = fopen("/spiffs/units.conf", "r+");

    if (f == NULL) {
        ESP_LOGE(TAG, "write /spiffs/units.conf failed");
        return;
    }
    size_t bytes = fwrite(dst, 1, sizeof(units), f);
    fclose(f);
    if (bytes != sizeof(units)) {
        ESP_LOGE(TAG, "/spiffs/units.conf written %d/%d bytes", bytes, sizeof(units));
    } else {
        ESP_LOGD(TAG, "/spiffs/units.conf written %d bytes", bytes);
    }
}



void read_units() {
    uint8_t     *dst = (uint8_t *)&units;
    uint8_t     mac_addr[8] = {0};
    size_t	bytes;
    FILE        *f = fopen("/spiffs/units.conf", "r");

    if (f == NULL) {
        // No units yet, create them.
	ESP_LOGE(TAG, "/spiffs/units.conf not found, create new");
	goto u_error;
    } else {
        bytes = fread(dst, 1, sizeof(units), f);
        fclose(f);
	if (bytes != sizeof(units)) {
	    ESP_LOGE(TAG, "/spiffs/units.conf read %d of %d bytes", bytes, sizeof(units));
	    goto u_error;
	}
        ESP_LOGI(TAG, "/spiffs/units.conf read %d bytes", bytes);
	for (int i = 0; i < 3; i++)
	   ESP_LOGI(TAG, "%d %s %d %4lu %3lu", i, units[i].alias, units[i].pressure_channel, units[i].pressure_voltage, units[i].pressure_zero);
    }
    return;

u_error:
    esp_efuse_mac_get_default(mac_addr);
    for (int i = 0; i < 3; i++) {
        memset(&units[i], 0, sizeof(unit_t));
        sprintf(units[i].uuid, "c0ffeeee-dead-beef-caf%d-%02x%02x%02x%02x%02x%02x", i & 3,
                        mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
        sprintf(units[i].alias, "unit%d", (i + 1) & 3);
        units[i].pressure_zero = 110;
    }
    f = fopen("/spiffs/units.conf", "w+");
    bytes = fwrite(dst, 1, sizeof(units), f);
    fclose(f);
    if (bytes != sizeof(units)) {
        ESP_LOGE(TAG, "/spiffs/units.conf written %d/%d bytes", bytes, sizeof(units));
    } else {
        ESP_LOGI(TAG, "/spiffs/units.conf written %d bytes", bytes);
    }
}



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");
}

mercurial