summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main/Kconfig16
-rw-r--r--main/component.mk9
-rw-r--r--main/main.cpp189
-rw-r--r--sdkconfig4
4 files changed, 179 insertions, 39 deletions
diff --git a/main/Kconfig b/main/Kconfig
index 6259a3f..11b89dc 100644
--- a/main/Kconfig
+++ b/main/Kconfig
@@ -27,4 +27,20 @@ config MAIN_FLASH_FD_BUF_SIZE
int "FD buf size. Default: 32*4"
default 128
+config MAIN_DEFAULT_WIFI_SSID
+ string "Default Wi-Fi SSID"
+ default "espressif"
+
+config MAIN_DEFAULT_WIFI_PASSWORD
+ string "Default Wi-Fi password"
+ default "12345678"
+
+config MAIN_DEFAULT_MQTT_HOST
+ string "Default MQTT host"
+ default "iot.eclipse.org"
+
+config MAIN_DEFAULT_MQTT_PORT
+ int "Default MQTT port"
+ default 1883
+
endmenu
diff --git a/main/component.mk b/main/component.mk
index e69de29..500a63f 100644
--- a/main/component.mk
+++ b/main/component.mk
@@ -0,0 +1,9 @@
+
+ifdef WIFI_SSID
+CFLAGS += '-DWIFI_SSID="$(WIFI_SSID)"'
+CPPFLAGS += '-DWIFI_SSID="$(WIFI_SSID)"'
+endif
+ifdef WIFI_PASSWORD
+CFLAGS += '-DWIFI_PASSWORD="$(WIFI_PASSWORD)"'
+CPPFLAGS += '-DWIFI_PASSWORD="$(WIFI_PASSWORD)"'
+endif
diff --git a/main/main.cpp b/main/main.cpp
index 648c415..3acf080 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -17,8 +17,6 @@
#include <errno.h>
*/
-#include "main-config.h"
-
extern "C"
uint32_t user_rf_cal_sector_set();
@@ -31,13 +29,22 @@ enum events {
struct config {
int loaded;
+ int ok_count;
+ char wifi_ssid[20];
+ char wifi_password[20];
+
char mqtt_host[20];
int mqtt_port;
};
struct _reent fs_reent;
+#define open(...) _spiffs_open_r(&fs_reent, __VA_ARGS__)
+#define close(...) _spiffs_close_r(&fs_reent, __VA_ARGS__)
+#define read(...) _spiffs_read_r(&fs_reent, __VA_ARGS__)
+#define write(...) _spiffs_write_r(&fs_reent, __VA_ARGS__)
+#define lseek(...) _spiffs_lseek_r(&fs_reent, __VA_ARGS__)
-xTaskHandle main_task_handle;
+xTaskHandle main_task_handle = 0;
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
@@ -88,14 +95,49 @@ uint32_t user_rf_cal_sector_set()
return rf_cal_sec;
}
-static int config_load(spiffs *fs, struct config &config)
-{
- int ret = 0;
+static int on_config_item(void *ctx, const char* key, const char* value) {
+ struct config *config = (struct config *)ctx;
+
+ if (strcmp(key, "wifi-ssid") == 0) {
+ strncpy(config->wifi_ssid, value, sizeof(config->wifi_ssid));
+ } else if (strcmp(key, "wifi-password") == 0) {
+ strncpy(config->wifi_password, value, sizeof(config->wifi_password));
+ } else if (strcmp(key, "mqtt-host") == 0) {
+ strncpy(config->mqtt_host, value, sizeof(config->mqtt_host));
+ } else if (strcmp(key, "mqtt-port") == 0) {
+ config->mqtt_port = atoi(value);
+ } else {
+ printf("Unknown key: %s\n", key);
+ return -1;
+ }
- bzero(&config, sizeof(config));
+ config->ok_count++;
+
+ return 0;
+}
+
+typedef int (kv_event_handler)(void *ctx, const char *key, const char *value);
+
+static int config_read_on_line(kv_event_handler *event_handler, void *ctx, const char* line, size_t sz) {
+ printf("Handling line: #%d, %s\n", sz, line);
+ char *ptr = strchr(line, '=');
+ if (ptr == NULL) {
+ printf("Could not find '='\n");
+ return -1;
+ }
- int fd = _spiffs_open_r(&fs_reent, "config", O_RDONLY, 0);
+ *ptr = '\0';
+ const char *key = line, *value = ++ptr;
+ return event_handler(ctx, key, value);
+}
+
+static int config_read(kv_event_handler *event_handler, void *ctx)
+{
+ int ret = 0;
+ int pos = 0;
+
+ int fd = open("config", O_RDONLY, 0);
printf("fd=%d\n", fd);
if (fd < 0) {
ret = -1;
@@ -103,23 +145,43 @@ static int config_load(spiffs *fs, struct config &config)
}
while (1) {
- char buf[6];
- ret = _spiffs_read_r(&fs_reent, fd, buf, sizeof(buf));
- printf("read: ret=%d\n", ret);
- if (ret == 0) {
- printf("EOF\n");
+ char buf[100];
+ ret = lseek(fd, pos, SEEK_SET);
+ if (ret == -1) {
+ printf("lseek failed: pos=%d, res=%d\n", pos, ret);
break;
- } else if (ret < 0) {
- printf("FAIL\n");
+ }
+ if (pos > 200) {
+ break; // fail safe for now
+ }
+
+ ret = read(fd, buf, sizeof(buf));
+ printf("read: pos=%d, ret=%d\n", (int) pos, ret);
+ if (ret > 0) {
+ char *end = (char *)memchr(buf, 0, ret);
+ if (end == NULL) {
+ printf("too line line, could not find newline\n");
+ ret = -1;
+ goto fail;
+ } else {
+ *end = '\0';
+ size_t line_sz = end - buf;
+ printf("line_sz=%d\n", line_sz);
+ pos += line_sz + 1;
+
+ ret = config_read_on_line(event_handler, ctx, buf, line_sz);
+ if (ret) {
+ goto fail;
+ }
+ }
+ } else {
break;
}
}
- config.loaded = 1;
-
fail:
if (fd != -1) {
- _spiffs_close_r(&fs_reent, fd);
+ close(fd);
}
printf("%s: ret=%d\n", __FUNCTION__, ret);
@@ -127,34 +189,63 @@ fail:
return ret;
}
-static int config_write_string(int fd, const char *str) {
+static int config_write_kv(int fd, const char *key, const char *value) {
int ret;
- size_t sz = strlen(str);
- printf("Writing %s, sz=%d\n", str, sz);
+ char c;
+ printf("Writing %s=%s\n", key, value);
+
+ size_t sz = strlen(key);
+ ret = write(fd, (char *)key, sz);
+ if (ret != sz) {
+ return ret;
+ }
+
+ c = '=';
+ ret = write(fd, &c, 1);
+ if (ret != 1) {
+ return ret;
+ }
- ret = _spiffs_write_r(&fs_reent, fd, (char *)str, sz);
+ sz = strlen(value);
+ ret = write(fd, (char *)value, sz);
if (ret != sz) {
return ret;
}
- return _spiffs_write_r(&fs_reent, fd, (char *)"\0", 1) != 1;
+ c = 0;
+ return write(fd, &c, 1) != 1;
+}
+
+static int config_write_kv(int fd, const char *key, int value) {
+ char buf[100];
+ itoa(value, buf, 10);
+
+ return config_write_kv(fd, key, buf);
}
static int config_write(struct config &config) {
int ret = 0;
- int fd = _spiffs_open_r(&fs_reent, "config", O_WRONLY | O_CREAT, 0);
+ int fd = open("config", O_WRONLY | O_CREAT, 0);
if (fd == -1) {
ret = -1;
goto fail;
}
- config_write_string(fd, config.mqtt_host);
+ ret = config_write_kv(fd, "mqtt-host", config.mqtt_host);
+ if (ret) {
+ goto fail;
+ }
+
+ ret = config_write_kv(fd, "mqtt-port", config.mqtt_port);
+ if (ret) {
+ goto fail;
+ }
fail:
if (fd != -1) {
- _spiffs_close_r(&fs_reent, fd);
+ close(fd);
}
printf("%s: ret=%d\n", __FUNCTION__, ret);
@@ -170,8 +261,10 @@ void wifi_event_handler_cb(System_Event_t *event)
if (event->event_id == EVENT_STAMODE_GOT_IP) {
printf("STA GOT IP\n");
- xTaskNotify(main_task_handle, EVENTS_GOT_IP, eSetBits);
- printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle());
+ if (main_task_handle) {
+ xTaskNotify(main_task_handle, EVENTS_GOT_IP, eSetBits);
+ printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle());
+ }
} else if (event->event_id == EVENT_STAMODE_CONNECTED) {
os_printf("STA CONNECTED\n");
} else if (event->event_id == EVENT_STAMODE_DISCONNECTED) {
@@ -199,7 +292,7 @@ static int fs_init()
return esp_spiffs_init(&config);
}
-static void set_station_mode() {
+static void set_station_mode(struct config &config) {
printf("STATION_MODE\n");
if (wifi_get_opmode_default() != NULL_MODE) {
printf("Setting default station mode");
@@ -208,11 +301,11 @@ static void set_station_mode() {
wifi_set_opmode_current(STATION_MODE);
- struct station_config config;
- bzero(&config, sizeof(struct station_config));
- sprintf((char *)config.ssid, WIFI_SSID);
- sprintf((char *)config.password, WIFI_PASSWORD);
- wifi_station_set_config(&config);
+ struct station_config sc;
+ bzero(&sc, sizeof(struct station_config));
+ sprintf((char *)sc.ssid, config.wifi_ssid);
+ sprintf((char *)sc.password, config.wifi_password);
+ wifi_station_set_config(&sc);
wifi_set_event_handler_cb(wifi_event_handler_cb);
@@ -264,24 +357,42 @@ void user_init() {
fs_init();
struct config config;
- ret = config_load(nullptr, config);
+ bzero(&config, sizeof(config));
+ ret = config_read(on_config_item, &config);
if (ret) {
printf("Loading file failed, errno=%d\n", fs_reent._errno);
}
+ if (config.ok_count != 5) {
+ printf("Loading file failed, loaded bad number of items=%d\n", config.ok_count);
+ } else {
+ config.loaded = 1;
+ }
if (!config.loaded) {
printf("No config loaded, writing new\n");
- strncpy(config.mqtt_host, "trygvis.io", sizeof(config.mqtt_host));
- config.mqtt_port = 1883;
+#ifndef WIFI_SSID
+#define WIFI_SSID CONFIG_MAIN_DEFAULT_WIFI_SSID
+#endif
+#ifndef WIFI_PASSWORD
+#define WIFI_PASSWORD CONFIG_MAIN_DEFAULT_WIFI_PASSWORD
+#endif
+ strncpy(config.wifi_ssid, WIFI_SSID, sizeof(config.wifi_ssid));
+ strncpy(config.wifi_password, WIFI_PASSWORD, sizeof(config.wifi_password));
+ strncpy(config.mqtt_host, CONFIG_MAIN_DEFAULT_MQTT_HOST, sizeof(config.mqtt_host));
+ config.mqtt_port = CONFIG_MAIN_DEFAULT_MQTT_PORT;
ret = config_write(config);
if (ret) {
printf("Writing file failed, errno=%d\n", fs_reent._errno);
}
}
- return;
+ printf("Configuration:\n");
+ printf(" wifi-ssid=%s\n wifi-password=%s\n", config.wifi_ssid, config.wifi_password);
+ printf("\n mqtt-host=%s\n mqtt-port=%d\n", config.mqtt_host, config.mqtt_port);
- set_station_mode();
+ set_station_mode(config);
+
+ return;
printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle());
ret = xTaskCreate(main_task, THREAD_NAME, THREAD_STACK_WORDS, NULL, THREAD_PRIO, &main_task_handle);
diff --git a/sdkconfig b/sdkconfig
index 15f4368..11a5634 100644
--- a/sdkconfig
+++ b/sdkconfig
@@ -171,6 +171,10 @@ CONFIG_MAIN_FLASH_SECTOR_SIZE_KB=4
CONFIG_MAIN_FLASH_LOG_BLOCK_SIZE_KB=4
CONFIG_MAIN_FLASH_LOG_PAGE_SIZE=128
CONFIG_MAIN_FLASH_FD_BUF_SIZE=128
+CONFIG_MAIN_DEFAULT_WIFI_SSID="espressif"
+CONFIG_MAIN_DEFAULT_WIFI_PASSWORD="12345678"
+CONFIG_MAIN_DEFAULT_MQTT_HOST="iot.eclipse.org"
+CONFIG_MAIN_DEFAULT_MQTT_PORT=1883
#
# Newlib