summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main/main.cpp (renamed from main/main.c)211
1 files changed, 158 insertions, 53 deletions
diff --git a/main/main.c b/main/main.cpp
index f1d9594..648c415 100644
--- a/main/main.c
+++ b/main/main.cpp
@@ -1,15 +1,43 @@
+#include "esp_misc.h"
#include "esp_sta.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_spiffs.h"
#include "spiffs.h"
+#include "spiffs.h"
#include "sdkconfig.h"
#include <assert.h>
+/*
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+*/
+
#include "main-config.h"
-// static bool show_ip = false;
+extern "C"
+uint32_t user_rf_cal_sector_set();
+
+extern "C"
+void user_init();
+
+enum events {
+ EVENTS_GOT_IP = (1 << 0)
+};
+
+struct config {
+ int loaded;
+ char mqtt_host[20];
+ int mqtt_port;
+};
+
+struct _reent fs_reent;
+
+xTaskHandle main_task_handle;
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
@@ -23,7 +51,7 @@
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
-uint32_t user_rf_cal_sector_set(void)
+uint32_t user_rf_cal_sector_set()
{
flash_size_map size_map = system_get_flash_size_map();
uint32_t rf_cal_sec = 0;
@@ -60,22 +88,77 @@ uint32_t user_rf_cal_sector_set(void)
return rf_cal_sec;
}
-__attribute__((used))
-static int32_t fs_init(void)
+static int config_load(spiffs *fs, struct config &config)
{
- struct esp_spiffs_config config;
+ int ret = 0;
- uint32_t log_page_size = CONFIG_MAIN_FLASH_LOG_PAGE_SIZE;
+ bzero(&config, sizeof(config));
- config.phys_size = CONFIG_MAIN_FLASH_SIZE_KB * 1024;
- config.phys_addr = CONFIG_MAIN_FLASH_FLASH_ADDR_KB * 1024;
- config.phys_erase_block = CONFIG_MAIN_FLASH_SECTOR_SIZE_KB * 1024;
- config.log_block_size = CONFIG_MAIN_FLASH_LOG_BLOCK_SIZE_KB * 1024;
- config.log_page_size = log_page_size;
- config.fd_buf_size = CONFIG_MAIN_FLASH_FD_BUF_SIZE * 2;
- config.cache_buf_size = (log_page_size + 32) * 8;
+ int fd = _spiffs_open_r(&fs_reent, "config", O_RDONLY, 0);
- return esp_spiffs_init(&config);
+ printf("fd=%d\n", fd);
+ if (fd < 0) {
+ ret = -1;
+ goto fail;
+ }
+
+ 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");
+ break;
+ } else if (ret < 0) {
+ printf("FAIL\n");
+ break;
+ }
+ }
+
+ config.loaded = 1;
+
+fail:
+ if (fd != -1) {
+ _spiffs_close_r(&fs_reent, fd);
+ }
+
+ printf("%s: ret=%d\n", __FUNCTION__, ret);
+
+ return ret;
+}
+
+static int config_write_string(int fd, const char *str) {
+ int ret;
+ size_t sz = strlen(str);
+ printf("Writing %s, sz=%d\n", str, sz);
+
+ ret = _spiffs_write_r(&fs_reent, fd, (char *)str, sz);
+ if (ret != sz) {
+ return ret;
+ }
+
+ return _spiffs_write_r(&fs_reent, fd, (char *)"\0", 1) != 1;
+}
+
+static int config_write(struct config &config) {
+ int ret = 0;
+
+ int fd = _spiffs_open_r(&fs_reent, "config", O_WRONLY | O_CREAT, 0);
+
+ if (fd == -1) {
+ ret = -1;
+ goto fail;
+ }
+
+ config_write_string(fd, config.mqtt_host);
+
+fail:
+ if (fd != -1) {
+ _spiffs_close_r(&fs_reent, fd);
+ }
+
+ printf("%s: ret=%d\n", __FUNCTION__, ret);
+ return ret;
}
void wifi_event_handler_cb(System_Event_t *event)
@@ -86,8 +169,9 @@ void wifi_event_handler_cb(System_Event_t *event)
}
if (event->event_id == EVENT_STAMODE_GOT_IP) {
- printf("STA GOT IP");
- // show_ip = true;
+ printf("STA GOT IP\n");
+ 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) {
@@ -98,6 +182,23 @@ void wifi_event_handler_cb(System_Event_t *event)
}
}
+static int fs_init()
+{
+ struct esp_spiffs_config config;
+
+ uint32_t log_page_size = CONFIG_MAIN_FLASH_LOG_PAGE_SIZE;
+
+ config.phys_size = CONFIG_MAIN_FLASH_SIZE_KB * 1024;
+ config.phys_addr = CONFIG_MAIN_FLASH_FLASH_ADDR_KB * 1024;
+ config.phys_erase_block = CONFIG_MAIN_FLASH_SECTOR_SIZE_KB * 1024;
+ config.log_block_size = CONFIG_MAIN_FLASH_LOG_BLOCK_SIZE_KB * 1024;
+ config.log_page_size = log_page_size;
+ config.fd_buf_size = CONFIG_MAIN_FLASH_FD_BUF_SIZE * 2;
+ config.cache_buf_size = (log_page_size + 32) * 8;
+
+ return esp_spiffs_init(&config);
+}
+
static void set_station_mode() {
printf("STATION_MODE\n");
if (wifi_get_opmode_default() != NULL_MODE) {
@@ -118,67 +219,71 @@ static void set_station_mode() {
wifi_station_connect();
}
-void ets_wdt_disable();
-void my_thread(void* ctx) {
+void main_task(void* ctx) {
(void) ctx;
- // wifi_set_opmode(NULL_MODE);
+ printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle());
int count = 0;
+ uint32_t notification_value;
while (1) {
- printf("Hello World! %d\n", count);
- if (count == 2) {
- // pvShowMalloc();
- }
+ int timeout = xTaskNotifyWait(0, UINT32_MAX, &notification_value, pdMS_TO_TICKS(500)) == pdFALSE;
- if (count == 5) {
- // set_station_mode();
+ if (notification_value & EVENTS_GOT_IP) {
+ struct ip_info info;
+ wifi_get_ip_info(STATION_IF, &info);
+ printf("ip=" IPSTR ", nm=" IPSTR ", gw=" IPSTR "\n", IP2STR(&info.ip), IP2STR(&info.netmask), IP2STR(&info.gw));
}
- /*
- */
- /*
- show_ip = false;
- if (show_ip) {
- show_ip = false;
-
- struct ip_info info;
- bool ok = wifi_get_ip_info(STATION_IF, &info);
+ if (timeout) {
+ printf("Hello World! %d\n", count);
+ if (count == 2) {
+ // pvShowMalloc();
+ }
- if (ok) {
- printf("ip=" IPSTR ", nm=" IPSTR ", gw=" IPSTR "\n", IP2STR(&info.ip), IP2STR(&info.netmask), IP2STR(&info.gw));
- } else {
- printf("show ip failed\n");
+ if (count == 5) {
+ // set_station_mode();
}
+
+ count++;
}
- */
- count++;
- vTaskDelay(500 / portTICK_PERIOD_MS);
+ // vTaskDelay(pdMS_TO_TICKS(500));
}
}
-#define THREAD_NAME "my_thread"
+#define THREAD_NAME "main"
#define THREAD_STACK_WORDS 2048
#define THREAD_PRIO 8
void user_init() {
+ int ret;
+
os_printf("SDK version: %s, free: %d\n", system_get_sdk_version(), system_get_free_heap_size());
- set_station_mode();
- // pvShowMalloc();
+ fs_init();
- // assert(fs_init() != -1);
+ struct config config;
+ ret = config_load(nullptr, config);
+ if (ret) {
+ printf("Loading file failed, errno=%d\n", fs_reent._errno);
+ }
- xTaskHandle thread_handle;
+ if (!config.loaded) {
+ printf("No config loaded, writing new\n");
+ strncpy(config.mqtt_host, "trygvis.io", sizeof(config.mqtt_host));
+ config.mqtt_port = 1883;
+ ret = config_write(config);
+ if (ret) {
+ printf("Writing file failed, errno=%d\n", fs_reent._errno);
+ }
+ }
- // set_station_mode();
+ return;
+
+ set_station_mode();
- int ret = xTaskCreate(my_thread,
- THREAD_NAME,
- THREAD_STACK_WORDS,
- NULL,
- THREAD_PRIO,
- &thread_handle);
+ printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle());
+ ret = xTaskCreate(main_task, THREAD_NAME, THREAD_STACK_WORDS, NULL, THREAD_PRIO, &main_task_handle);
assert(ret == pdPASS);
}