2016-09-16 10:10:18 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright 2013-2014 Espressif Systems (Wuxi)
|
|
|
|
*
|
|
|
|
* FileName: user_main.c
|
|
|
|
*
|
|
|
|
* Description: entry file of user application
|
|
|
|
*
|
|
|
|
* Modification history:
|
|
|
|
* 2014/1/1, v1.0 create this file.
|
|
|
|
*******************************************************************************/
|
|
|
|
#include "lua.h"
|
2021-08-21 17:39:54 +02:00
|
|
|
#include "linput.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
#include "platform.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "sdkconfig.h"
|
2016-09-21 09:51:50 +02:00
|
|
|
#include "esp_system.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "esp_event.h"
|
2021-10-20 12:24:09 +02:00
|
|
|
#include "esp_spiffs.h"
|
2023-01-31 07:07:54 +01:00
|
|
|
#include "esp_netif.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "nvs_flash.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
|
|
|
#include "task/task.h"
|
|
|
|
#include "sections.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "nodemcu_esp_event.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-20 05:35:56 +02:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "freertos/queue.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
|
|
|
#define SIG_LUA 0
|
|
|
|
#define SIG_UARTINPUT 1
|
|
|
|
|
2021-07-19 09:23:38 +02:00
|
|
|
// We don't get argument size data from the esp_event dispatch, so it's
|
|
|
|
// not possible to copy and forward events from the default event queue
|
|
|
|
// to one running within our task context. To cope with this, we instead
|
|
|
|
// have to effectively make a blocking inter-task call, by having our
|
|
|
|
// default loop handler post a nodemcu task event with a pointer to the
|
|
|
|
// event data, and then *block* until that task event has been processed.
|
|
|
|
// This is less elegant than I would like, but trying to run the entire
|
|
|
|
// LVM in the context of the system default event loop RTOS task is an
|
|
|
|
// even worse idea, so here we are.
|
|
|
|
typedef struct {
|
|
|
|
esp_event_base_t event_base;
|
|
|
|
int32_t event_id;
|
|
|
|
void *event_data;
|
|
|
|
} relayed_event_t;
|
|
|
|
static task_handle_t relayed_event_task;
|
|
|
|
static SemaphoreHandle_t relayed_event_handled;
|
|
|
|
|
|
|
|
|
|
|
|
// This function runs in the context of the system default event loop RTOS task
|
|
|
|
static void relay_default_loop_events(
|
|
|
|
void *arg, esp_event_base_t base, int32_t id, void *data)
|
2016-09-27 10:42:08 +02:00
|
|
|
{
|
2021-07-19 09:23:38 +02:00
|
|
|
(void)arg;
|
|
|
|
relayed_event_t event = {
|
|
|
|
.event_base = base,
|
|
|
|
.event_id = id,
|
|
|
|
.event_data = data,
|
|
|
|
};
|
|
|
|
_Static_assert(sizeof(&event) >= sizeof(task_param_t), "pointer-vs-int");
|
|
|
|
// Only block if we actually posted the request, otherwise we'll deadlock!
|
|
|
|
if (task_post_medium(relayed_event_task, (intptr_t)&event))
|
|
|
|
xSemaphoreTake(relayed_event_handled, portMAX_DELAY);
|
|
|
|
else
|
2023-01-31 07:07:54 +01:00
|
|
|
printf("ERROR: failed to forward esp event %s/%ld", base, id);
|
2016-09-27 10:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-19 09:23:38 +02:00
|
|
|
static void handle_default_loop_event(task_param_t param, task_prio_t prio)
|
2016-09-27 10:42:08 +02:00
|
|
|
{
|
|
|
|
(void)prio;
|
2021-07-19 09:23:38 +02:00
|
|
|
const relayed_event_t *event = (const relayed_event_t *)param;
|
2016-09-27 10:42:08 +02:00
|
|
|
|
2021-07-19 09:23:38 +02:00
|
|
|
nodemcu_esp_event_reg_t *evregs = &_esp_event_cb_table_start;
|
|
|
|
for (; evregs < &_esp_event_cb_table_end; ++evregs)
|
2016-09-27 10:42:08 +02:00
|
|
|
{
|
2021-07-19 09:23:38 +02:00
|
|
|
bool event_base_match =
|
|
|
|
(evregs->event_base_ptr == NULL) || // ESP_EVENT_ANY_BASE marker
|
|
|
|
(*evregs->event_base_ptr == event->event_base);
|
|
|
|
bool event_id_match =
|
|
|
|
(evregs->event_id == event->event_id) ||
|
|
|
|
(evregs->event_id == ESP_EVENT_ANY_ID);
|
|
|
|
|
|
|
|
if (event_base_match && event_id_match)
|
|
|
|
evregs->callback(event->event_base, event->event_id, event->event_data);
|
2016-09-27 10:42:08 +02:00
|
|
|
}
|
2021-07-19 09:23:38 +02:00
|
|
|
xSemaphoreGive(relayed_event_handled);
|
2016-09-27 10:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-16 10:10:18 +02:00
|
|
|
// +================== New task interface ==================+
|
2016-10-03 04:05:17 +02:00
|
|
|
static void start_lua ()
|
|
|
|
{
|
2016-09-16 10:10:18 +02:00
|
|
|
NODE_DBG("Task task_lua started.\n");
|
2021-08-23 15:00:54 +02:00
|
|
|
if (lua_main()) // If it returns true then LFS restart is needed
|
|
|
|
lua_main();
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void nodemcu_init(void)
|
|
|
|
{
|
|
|
|
NODE_ERR("\n");
|
|
|
|
// Initialize platform first for lua modules.
|
|
|
|
if( platform_init() != PLATFORM_OK )
|
|
|
|
{
|
|
|
|
// This should never happen
|
|
|
|
NODE_DBG("Can not init platform for modules.\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-10-20 12:24:09 +02:00
|
|
|
const char *label = CONFIG_NODEMCU_DEFAULT_SPIFFS_LABEL;
|
|
|
|
|
|
|
|
esp_vfs_spiffs_conf_t spiffs_cfg = {
|
|
|
|
.base_path = "",
|
|
|
|
.partition_label = (label && label[0]) ? label : NULL,
|
|
|
|
.max_files = CONFIG_NODEMCU_MAX_OPEN_FILES,
|
|
|
|
.format_if_mount_failed = true,
|
|
|
|
};
|
|
|
|
const char *reason = NULL;
|
|
|
|
switch(esp_vfs_spiffs_register(&spiffs_cfg))
|
|
|
|
{
|
|
|
|
case ESP_OK: break;
|
|
|
|
case ESP_ERR_NO_MEM:
|
|
|
|
reason = "out of memory";
|
|
|
|
break;
|
|
|
|
case ESP_ERR_INVALID_STATE:
|
|
|
|
reason = "already mounted, or encrypted";
|
|
|
|
break;
|
|
|
|
case ESP_ERR_NOT_FOUND:
|
|
|
|
reason = "no SPIFFS partition found";
|
|
|
|
break;
|
|
|
|
case ESP_FAIL:
|
|
|
|
reason = "failed to mount or format partition";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reason = "unknown";
|
|
|
|
break;
|
2016-09-21 09:51:50 +02:00
|
|
|
}
|
2021-10-20 12:24:09 +02:00
|
|
|
if (reason)
|
|
|
|
printf("Failed to mount SPIFFS partition: %s\n", reason);
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-19 09:23:38 +02:00
|
|
|
void __attribute__((noreturn)) app_main(void)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
Fix net module data loss & RTOS task unsafety (#2829)
To avoid races between the lwIP callbacks (lwIP RTOS task) and the Lua
handlers (LVM RTOS task), the data flow and ownership has been simplified
and cleaned up.
lwIP callbacks now have no visibility of the userdata struct. They are
limited to creating small event objects and task_post()ing them over
to the LVM "thread", passing ownership in doing so. The shared identifier
then becomes the struct netconn*.
On the LVM side, we keep a linked list of active userdata objects. This
allows us to retrieve the correct userdata when we get an event with
a netconn pointer. Because this list is only ever used within the LVM
task, no locking is necessary.
The old approach of stashing a userdata pointer into the 'socket' field
on the netconn has been removed entirely, as this was both not
thread/RTOS-task safe, and also interfered with the IDFs internal use
of the socket field (even when using only the netconn layer). As an
added benefit, this removed the need for all the SYS_ARCH_PROTECT()
locking stuff.
The need to track receive events before the corresponding userdata object
has been established has been removed by virtue of not reordering the
"accept" and the "recv" events any more (previously accepts were posted
with medium priority, while the receives where high priority, leading
to the observed reordering and associated headaches).
The workaround for IDF issue 784 has been removed as it is now not needed
and is in fact directly harmful as it results in a double-free. Yay for
getting rid of old workarounds!
DNS resolution code paths were merged for the two instances of "socket"
initiated resolves (connect/dns functions).
Also fixed an instance of using a stack variable for receiving the resolved
IP address, with said variable going out of scope before the DNS resolution
necessarily completed (hello, memory corruption!).
Where possible, moved to use the Lua allocator rather than plain malloc.
Finally, the NodeMCU task posting mechanism got a polish and an adjustment.
Given all the Bad(tm) that tends to happen if something fails task posting,
I went through a couple of iterations on how to avoid that. Alas, the
preferred solution of blocking non-LVM RTOS tasks until a slot is free
turned out to not be viable, as this easily resulted in deadlocks with the
lwIP stack. After much deliberation I settled on increasing the number of
available queue slots for the task_post() mechanism, but in the interest
of user control also now made it user configurable via Kconfig.
2019-07-14 23:20:20 +02:00
|
|
|
task_init();
|
|
|
|
|
2021-07-19 09:23:38 +02:00
|
|
|
relayed_event_handled = xSemaphoreCreateBinary();
|
|
|
|
relayed_event_task = task_get_id(handle_default_loop_event);
|
|
|
|
|
|
|
|
esp_event_loop_create_default();
|
|
|
|
esp_event_handler_register(
|
|
|
|
ESP_EVENT_ANY_BASE,
|
|
|
|
ESP_EVENT_ANY_ID,
|
|
|
|
relay_default_loop_events,
|
|
|
|
NULL);
|
2020-08-03 13:39:42 +02:00
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
platform_uart_start(CONFIG_ESP_CONSOLE_UART_NUM);
|
2018-10-07 21:40:49 +02:00
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
nodemcu_init ();
|
2016-09-20 05:35:56 +02:00
|
|
|
|
2016-09-28 09:57:00 +02:00
|
|
|
nvs_flash_init ();
|
2021-07-15 08:25:25 +02:00
|
|
|
esp_netif_init ();
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-10-03 04:05:17 +02:00
|
|
|
start_lua ();
|
2016-09-27 10:42:08 +02:00
|
|
|
task_pump_messages ();
|
|
|
|
__builtin_unreachable ();
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|