]> git.meshlink.io Git - meshlink-tiny/blobdiff - src/mlt_main.c
Merge support for ESP8266.
[meshlink-tiny] / src / mlt_main.c
index 977b9c3f0fc457f167db5935ebfd5028a76c413e..e2a6cdfbee4761525748d21d045eb68bd68c58dd 100644 (file)
@@ -1,22 +1,26 @@
 /* MeshLink-tiny Example */
 
-#include <string.h>
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "freertos/event_groups.h"
-#include "esp_system.h"
-#include "esp_wifi.h"
-#include "esp_event.h"
-#include "esp_log.h"
-#include "esp_console.h"
-#include "esp_vfs_dev.h"
-#include "driver/uart.h"
-#include "linenoise/linenoise.h"
-#include "argtable3/argtable3.h"
-#include "nvs_flash.h"
-
-#include "lwip/err.h"
-#include "lwip/sys.h"
+#include <freertos/FreeRTOS.h>
+#ifdef CONFIG_IDF_TARGET_ESP8266
+#include <freertos/private/list.h>
+#endif
+#include <freertos/task.h>
+#include <freertos/event_groups.h>
+#include <esp_system.h>
+#include <esp_wifi.h>
+#include <esp_event.h>
+#include <esp_log.h>
+#include <esp_console.h>
+#include <esp_vfs_dev.h>
+#include <driver/uart.h>
+#include <linenoise/linenoise.h>
+#include <argtable3/argtable3.h>
+#include <nvs.h>
+#include <nvs_flash.h>
+
+#include <lwip/err.h>
+#include <lwip/sys.h>
+
 
 #include "meshlink-tiny.h"
 
@@ -67,44 +71,33 @@ static void event_handler(void *arg, esp_event_base_t event_base,
 void wifi_init_sta(void) {
        s_wifi_event_group = xEventGroupCreate();
 
-       ESP_ERROR_CHECK(esp_netif_init());
+       tcpip_adapter_init();
 
        ESP_ERROR_CHECK(esp_event_loop_create_default());
-       esp_netif_create_default_wifi_sta();
 
        wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
        ESP_ERROR_CHECK(esp_wifi_init(&cfg));
 
-       esp_event_handler_instance_t instance_any_id;
-       esp_event_handler_instance_t instance_got_ip;
-       ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
-                       ESP_EVENT_ANY_ID,
-                       &event_handler,
-                       NULL,
-                       &instance_any_id));
-       ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
-                       IP_EVENT_STA_GOT_IP,
-                       &event_handler,
-                       NULL,
-                       &instance_got_ip));
+       ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
+       ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
 
        wifi_config_t wifi_config = {
                .sta = {
                        .ssid = EXAMPLE_ESP_WIFI_SSID,
-                       .password = EXAMPLE_ESP_WIFI_PASS,
-                       /* Setting a password implies station will connect to all security modes including WEP/WPA.
-                        * However these modes are deprecated and not advisable to be used. Incase your Access point
-                        * doesn't support WPA2, these mode can be enabled by commenting below line */
-                       .threshold.authmode = WIFI_AUTH_WPA2_PSK,
-
-                       .pmf_cfg = {
-                               .capable = true,
-                               .required = false
-                       },
+                       .password = EXAMPLE_ESP_WIFI_PASS
                },
        };
+
+       /* Setting a password implies station will connect to all security modes including WEP/WPA.
+           * However these modes are deprecated and not advisable to be used. Incase your Access point
+           * doesn't support WPA2, these mode can be enabled by commenting below line */
+
+       if(strlen((char *)wifi_config.sta.password)) {
+               wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
+       }
+
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
-       ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
+       ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
        ESP_ERROR_CHECK(esp_wifi_start());
 
        ESP_LOGI(TAG, "wifi_init_sta finished.");
@@ -129,12 +122,12 @@ void wifi_init_sta(void) {
                ESP_LOGE(TAG, "UNEXPECTED EVENT");
        }
 
-       /* The event will not be processed after unregister */
-       ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
-       ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
+       ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
+       ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
        vEventGroupDelete(s_wifi_event_group);
 }
 
+
 static void mlt_log(meshlink_handle_t *mesh, meshlink_log_level_t leve, const char *text) {
        ESP_LOGI(TAG, "Log: %s", text);
 }
@@ -181,7 +174,7 @@ static int say_func(int argc, char **argv) {
                return 1;
        }
 
-       if(!meshlink_send(mesh, peer, argv[2], strlen(argv[2]))) {
+       if(!meshlink_send(mesh, peer, argv[2], strlen(argv[2]) + 1)) {
                ESP_LOGE(TAG, "Send failed");
                return 1;
        }
@@ -207,43 +200,6 @@ static void mlt_main(void) {
                return;
        }
 
-       fflush(stdout);
-       fsync(fileno(stdout));
-       setvbuf(stdin, NULL, _IONBF, 0);
-
-       esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
-       esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
-
-       const uart_config_t uart_config = {
-               .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
-               .data_bits = UART_DATA_8_BITS,
-               .parity = UART_PARITY_DISABLE,
-               .stop_bits = UART_STOP_BITS_1,
-#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
-               .source_clk = UART_SCLK_REF_TICK,
-#else
-               .source_clk = UART_SCLK_XTAL,
-#endif
-       };
-       /* Install UART driver for interrupt-driven reads and writes */
-       ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
-                                           256, 0, 0, NULL, 0));
-       ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
-
-       /* Tell VFS to use UART driver */
-       esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
-
-       esp_console_config_t console_config = {
-               .max_cmdline_args = 8,
-               .max_cmdline_length = 256,
-       };
-
-       ESP_ERROR_CHECK(esp_console_init(&console_config));
-
-       linenoiseSetMultiLine(1);
-       linenoiseHistorySetMaxLen(1);
-       linenoiseAllowEmpty(false);
-
        esp_console_cmd_t join_cmd = {
                .command = "join",
                .help = "Join a mesh",
@@ -284,16 +240,72 @@ static void mlt_main(void) {
        esp_console_deinit();
 }
 
-void app_main(void) {
-       //Initialize NVS
-       esp_err_t ret = nvs_flash_init();
+static void initialize_nvs() {
+       esp_err_t err = nvs_flash_init();
 
-       if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
+       if(err == ESP_ERR_NVS_NO_FREE_PAGES) {
                ESP_ERROR_CHECK(nvs_flash_erase());
-               ret = nvs_flash_init();
+               err = nvs_flash_init();
        }
 
-       ESP_ERROR_CHECK(ret);
+       ESP_ERROR_CHECK(err);
+}
+
+static void initialize_console() {
+       /* Disable buffering on stdin */
+       setvbuf(stdin, NULL, _IONBF, 0);
+
+       /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
+       esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
+       /* Move the caret to the beginning of the next line on '\n' */
+       esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
+
+       /* Configure UART. Note that REF_TICK is used so that the baud rate remains
+        * correct while APB frequency is changing in light sleep mode.
+        */
+       uart_config_t uart_config = {
+               .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
+               .data_bits = UART_DATA_8_BITS,
+               .parity = UART_PARITY_DISABLE,
+               .stop_bits = UART_STOP_BITS_1,
+       };
+       ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
+
+       /* Install UART driver for interrupt-driven reads and writes */
+       ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
+                                           256, 0, 0, NULL, 0));
+
+       /* Tell VFS to use UART driver */
+       esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
+
+       /* Initialize the console */
+       esp_console_config_t console_config = {
+               .max_cmdline_args = 8,
+               .max_cmdline_length = 256,
+#if CONFIG_LOG_COLORS
+               .hint_color = atoi(LOG_COLOR_CYAN)
+#endif
+       };
+       ESP_ERROR_CHECK(esp_console_init(&console_config));
+}
+
+
+void app_main(void) {
+       /* Print chip information */
+       esp_chip_info_t chip_info;
+       esp_chip_info(&chip_info);
+       printf("This is ESP8266 chip with %d CPU cores, WiFi, ",
+              chip_info.cores);
+
+       printf("silicon revision %d, ", chip_info.revision);
+
+       printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
+              (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
+
+       initialize_nvs();
+       initialize_console();
+
+       esp_console_register_help_command();
 
        ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
        wifi_init_sta();