Commit f126b6fe authored by Johny Mattsson's avatar Johny Mattsson Committed by J Mattsson
Browse files

Initial migration to IDFv5.0

Plenty of dependency adjustments, printf format specificier updates,
FreeRTOS type and macro name modernisation, not to mention API changes.

Still plenty of legacy/deprecated drivers in use which will need updating.

The following features have been removed due to no longer being available
from the IDF:
  - ADC hall effect sensor reading
  - Configuration of SD SPI host via sdmmc module (now must be done first
    via the spimaster module)
  - FAT partition selection on external SD cards; only the first FAT
    partition is supported by the IDF now

On the other hand, the eth module now supports the following new chipsets:
  - KSZ8001
  - KSZ8021
  - KSZ8031
  - KSZ8051
  - KSZ8061
  - KSZ8091
  - Possibly additional models in the LAN87xx series (the IDF docs aren't
    clear on precisely which models are handled)

Further, the sdmmc module is now available on the ESP32-S3 as well.
parent 2b8e3271
......@@ -10,6 +10,9 @@ components/*/.output/
tools/toolchains
.ccache
bin
version.txt
managed_components/
dependencies.lock
#ignore Eclipse project files
.cproject
......
......@@ -2,6 +2,6 @@ idf_component_register(
SRCS "ip_fmt.c" "user_main.c"
INCLUDE_DIRS "include"
REQUIRES "lua"
PRIV_REQUIRES "nvs_flash" "spiffs"
PRIV_REQUIRES "nvs_flash" "spiffs" "esp_netif" "driver"
LDFRAGMENTS "nodemcu.lf"
)
......@@ -17,8 +17,8 @@
#include "esp_system.h"
#include "esp_event.h"
#include "esp_spiffs.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "flash_api.h"
#include "task/task.h"
#include "sections.h"
......@@ -64,7 +64,7 @@ static void relay_default_loop_events(
if (task_post_medium(relayed_event_task, (intptr_t)&event))
xSemaphoreTake(relayed_event_handled, portMAX_DELAY);
else
printf("ERROR: failed to forward esp event %s/%d", base, id);
printf("ERROR: failed to forward esp event %s/%ld", base, id);
}
......
......@@ -32,6 +32,9 @@
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "soc/gpio_sig_map.h"
#include "rom/gpio.h"
#include "esp_intr_alloc.h"
#include "soc/dport_reg.h"
#include <math.h>
......
......@@ -3,6 +3,7 @@ if(IDF_TARGET STREQUAL "esp32")
idf_component_register(
SRCS "CAN.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES "driver" "esp_rom"
)
endif()
idf_component_register(
SRCS "i2c_sw_master.c"
INCLUDE_DIRS "include"
REQUIRES "esp32"
REQUIRES "esp_rom"
PRIV_REQUIRES "driver"
)
#ifndef __I2C_SW_MASTER_H__
#define __I2C_SW_MASTER_H__
#include "rom/ets_sys.h"
#include "esp_rom_sys.h"
#define I2C_NUM_MAX 1
void i2c_sw_master_gpio_init(uint8_t sda, uint8_t scl);
void i2c_sw_master_init(void);
#define i2c_sw_master_wait ets_delay_us
#define i2c_sw_master_wait esp_rom_delay_us
void i2c_sw_master_stop(void);
void i2c_sw_master_start(void);
void i2c_sw_master_setAck(uint8_t level);
......
......@@ -2,7 +2,7 @@ menu "Lua configuration"
choice LUA_VERSION
prompt "Lua version"
default LUA_VERSION_51
default LUA_VERSION_53
help
The version of Lua interpreter to use.
......
......@@ -20,9 +20,7 @@
#include "lstate.h"
#include "lstring.h"
#include "lvm.h"
#ifndef LUA_CROSS_COMPILER
#include "flash_api.h"
#else
#ifdef LUA_CROSS_COMPILER
#include <limits.h>
#endif
const TValue luaO_nilobject_ = {LUA_TVALUE_NIL};
......
......@@ -177,7 +177,7 @@ static void platform_flash_erase_sector(lu_int32 i) {
lockFlashWrite();
}
static void platform_s_flash_write(const void *from, lu_int32 to, lu_int32 len) {
static void platform_flash_write(const void *from, lu_int32 to, lu_int32 len) {
lua_assert(to >= LFSbase && to + len < LFSbase + LFS_SIZE); /* DEBUG */
unlockFlashWrite();
memcpy(byteptr(LFSregion) + (to-LFSbase), from, len);
......@@ -449,7 +449,7 @@ static const char *readF (lua_State *L, void *ud, size_t *size) {
static void eraseLFS(LFSflashState *F) {
lu_int32 i;
#ifdef LUA_USE_ESP
printf("\nErasing LFS from flash addr 0x%06x", F->addrPhys);
printf("\nErasing LFS from flash addr 0x%06lx", F->addrPhys);
#endif
unlockFlashWrite();
for (i = 0; i < F->size; i += FLASH_PAGE_SIZE) {
......@@ -464,7 +464,7 @@ static void eraseLFS(LFSflashState *F) {
platform_flash_erase_sector(s);
}
#ifdef LUA_USE_ESP
printf(" to 0x%06x\n", F->addrPhys + F->size-1);
printf(" to 0x%06lx\n", F->addrPhys + F->size-1);
#endif
flush_icache(F);
lockFlashWrite();
......@@ -483,7 +483,7 @@ LUAI_FUNC void luaN_flushFlash(void *vF) {
lu_int32 size = F->oNdx * WORDSIZE;
lua_assert(start + size < F->addrPhys + F->size); /* is write in bounds? */
//printf("Flush Buf: %6x (%u)\n", F->oNdx, size); //DEBUG
platform_s_flash_write(F->oBuff, start, size);
platform_flash_write(F->oBuff, start, size);
F->oChunkNdx += F->oNdx;
F->oNdx = 0;
}
......
......@@ -59,13 +59,14 @@ if(IDF_TARGET STREQUAL "esp32")
elseif(IDF_TARGET STREQUAL "esp32s2")
list(APPEND module_srcs
"dac.c"
"rmt.c"
"pulsecnt.c"
"rmt.c"
)
elseif(IDF_TARGET STREQUAL "esp32s3")
list(APPEND module_srcs
"rmt.c"
"pulsecnt.c"
"rmt.c"
"sdmmc.c"
)
elseif(IDF_TARGET STREQUAL "esp32c3")
list(APPEND module_srcs
......@@ -81,9 +82,11 @@ idf_component_register(
"base_nodemcu"
"bt"
"driver_can"
"esp_eth"
"esp_http_client"
"fatfs"
"esp_http_server"
"esp_hw_support"
"fatfs"
"libsodium"
"lua"
"mbedtls"
......@@ -91,10 +94,12 @@ idf_component_register(
"platform"
"qrcodegen"
"sdmmc"
"spi_flash"
"sjson"
"soc"
"u8g2"
"ucg"
"vfs"
)
# Match up all the module source files with their corresponding Kconfig
......
......@@ -54,9 +54,9 @@ static int adc_read( lua_State *L )
// Lua: read_hall_sensor( )
static int read_hall_sensor( lua_State *L )
{
int sample = platform_adc_read_hall_sensor( );
lua_pushinteger( L, ( lua_Integer ) sample );
return 1;
const char *msg = "ADC hall sensor no longer supported in IDF, sorry";
platform_print_deprecation_note("msg", "IDFv5");
return luaL_error(L, msg);
}
// Module function map
......
......@@ -29,7 +29,7 @@ CAN_device_t CAN_cfg = {
static task_handle_t can_data_task_id;
static int can_on_received = LUA_NOREF;
static xTaskHandle xCanTaskHandle = NULL;
static TaskHandle_t xCanTaskHandle = NULL;
// LUA
static void can_data_task( task_param_t param, task_prio_t prio ) {
......
......@@ -14,6 +14,7 @@
#include "esp_eth.h"
#include "esp_eth_phy.h"
#include "driver/gpio.h"
#include "esp_mac.h"
typedef esp_eth_phy_t *(*new_eth_phy_fn)(const eth_phy_config_t *config);
......@@ -21,9 +22,8 @@ typedef esp_eth_phy_t *(*new_eth_phy_fn)(const eth_phy_config_t *config);
typedef enum {
ETH_PHY_DP83848,
ETH_PHY_IP101,
ETH_PHY_KSZ8041,
ETH_PHY_KSZ8081,
ETH_PHY_LAN8720,
ETH_PHY_KSZ80XX,
ETH_PHY_LAN87XX,
ETH_PHY_RTL8201,
ETH_PHY_MAX
} eth_phy_t;
......@@ -31,9 +31,8 @@ typedef enum {
static const new_eth_phy_fn new_eth_phy[] = {
[ETH_PHY_DP83848] = esp_eth_phy_new_dp83848,
[ETH_PHY_IP101] = esp_eth_phy_new_ip101,
[ETH_PHY_KSZ8041] = esp_eth_phy_new_ksz8041,
[ETH_PHY_KSZ8081] = esp_eth_phy_new_ksz8081,
[ETH_PHY_LAN8720] = esp_eth_phy_new_lan8720,
[ETH_PHY_KSZ80XX] = esp_eth_phy_new_ksz80xx,
[ETH_PHY_LAN87XX] = esp_eth_phy_new_lan87xx,
[ETH_PHY_RTL8201] = esp_eth_phy_new_rtl8201,
};
_Static_assert(sizeof(new_eth_phy) == (sizeof(new_eth_phy[0]) * ETH_PHY_MAX),
......@@ -273,13 +272,15 @@ static int leth_init( lua_State *L )
// temporarily copy option table to top of stack for opt_ functions
lua_pushvalue( L, stack );
eth_mac_config_t mac_cfg = ETH_MAC_DEFAULT_CONFIG();
eth_esp32_emac_config_t emac_cfg = ETH_ESP32_EMAC_DEFAULT_CONFIG();
mac_cfg.smi_mdc_gpio_num =
emac_cfg.smi_mdc_gpio_num =
opt_checkint_range( L, "mdc", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 );
mac_cfg.smi_mdio_gpio_num =
emac_cfg.smi_mdio_gpio_num =
opt_checkint_range( L, "mdio", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 );
eth_mac_config_t mac_cfg = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_cfg = ETH_PHY_DEFAULT_CONFIG();
phy_cfg.phy_addr = opt_checkint_range( L, "addr", -1, -1, 31 );
......@@ -290,7 +291,7 @@ static int leth_init( lua_State *L )
lua_settop( L, top );
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_cfg);
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&emac_cfg, &mac_cfg);
esp_eth_phy_t *phy = new_eth_phy[phy_type](&phy_cfg);
esp_eth_config_t eth_cfg = ETH_DEFAULT_CONFIG(mac, phy);
......@@ -302,10 +303,6 @@ static int leth_init( lua_State *L )
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *new_eth = esp_netif_new(&netif_cfg);
err = esp_eth_set_default_handlers(new_eth);
if (err != ESP_OK)
goto cleanup_netif;
void *glue = esp_eth_new_netif_glue(eth_handle);
err = esp_netif_attach(new_eth, glue);
if (err != ESP_OK)
......@@ -320,8 +317,6 @@ static int leth_init( lua_State *L )
cleanup_glue:
esp_eth_del_netif_glue(glue);
cleanup_netif:
esp_netif_destroy(new_eth);
cleanup_mac_phy:
......@@ -354,9 +349,11 @@ LROT_BEGIN(eth, NULL, 0)
LROT_NUMENTRY( PHY_DP83848, ETH_PHY_DP83848 )
LROT_NUMENTRY( PHY_IP101, ETH_PHY_IP101 )
LROT_NUMENTRY( PHY_KSZ8041, ETH_PHY_KSZ8041 )
LROT_NUMENTRY( PHY_KSZ8081, ETH_PHY_KSZ8081 )
LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN8720 )
LROT_NUMENTRY( PHY_KSZ80XX, ETH_PHY_KSZ80XX )
LROT_NUMENTRY( PHY_KSZ8041, ETH_PHY_KSZ80XX ) // deprecated in favour of 80XX
LROT_NUMENTRY( PHY_KSZ8081, ETH_PHY_KSZ80XX ) // deprecated in favour of 80XX
LROT_NUMENTRY( PHY_LAN87XX, ETH_PHY_LAN87XX )
LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN87XX ) // deprecated in favour of 87XX
LROT_NUMENTRY( PHY_RTL8201, ETH_PHY_RTL8201 )
LROT_END(eth, NULL, 0)
......
#include "module.h"
#include "lauxlib.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcpp"
#include "esp_heap_trace.h"
#pragma GCC diagnostic pop
static heap_trace_record_t *buffer = NULL;
......
#include "module.h"
#include "lauxlib.h"
#include "lmem.h"
#include "platform.h"
#include "driver/i2c.h"
#include "soc/i2c_reg.h"
#include "hal/i2c_ll.h"
#include "rom/gpio.h"
#include "i2c_common.h"
......@@ -73,7 +74,7 @@ static void vTransferTask( void *pvParameters )
xQueueReceive( xQueue, job, portMAX_DELAY );
job->err = i2c_master_cmd_begin( job->port, job->cmd,
job->to_ms > 0 ? job->to_ms / portTICK_RATE_MS : portMAX_DELAY );
job->to_ms > 0 ? job->to_ms / portTICK_PERIOD_MS : portMAX_DELAY );
task_post_medium( i2c_transfer_task_id, (task_param_t)job );
}
......@@ -202,11 +203,11 @@ int li2c_hw_master_setup( lua_State *L, unsigned id, unsigned sda, unsigned scl,
memset( &cfg, 0, sizeof( cfg ) );
cfg.mode = I2C_MODE_MASTER;
luaL_argcheck( L, GPIO_IS_VALID_OUTPUT_GPIO(sda), 2, "invalid sda pin" );
luaL_argcheck( L, platform_gpio_output_exists(sda), 2, "invalid sda pin" );
cfg.sda_io_num = sda;
cfg.sda_pullup_en = GPIO_PULLUP_ENABLE;
luaL_argcheck( L, GPIO_IS_VALID_OUTPUT_GPIO(scl), 3, "invalid scl pin" );
luaL_argcheck( L, platform_gpio_output_exists(scl), 3, "invalid scl pin" );
cfg.scl_io_num = scl;
cfg.scl_pullup_en = GPIO_PULLUP_ENABLE;
......@@ -357,7 +358,7 @@ int li2c_hw_master_transfer( lua_State *L )
// note that i2c_master_cmd_begin() implements mutual exclusive access
// if it is currently in progress from the transfer task, it will block here until
esp_err_t err = i2c_master_cmd_begin( job->port, job->cmd,
job->to_ms > 0 ? job->to_ms / portTICK_RATE_MS : portMAX_DELAY );
job->to_ms > 0 ? job->to_ms / portTICK_PERIOD_MS : portMAX_DELAY );
switch (err) {
case ESP_OK:
......
......@@ -30,11 +30,11 @@ typedef struct {
typedef struct {
struct {
xTaskHandle taskHandle;
TaskHandle_t taskHandle;
QueueHandle_t queue;
} tx;
struct {
xTaskHandle taskHandle;
TaskHandle_t taskHandle;
QueueHandle_t queue;
} rx;
int cb;
......@@ -152,8 +152,8 @@ static int node_i2s_start( lua_State *L )
//
i2s_config.channel_format = opt_checkint(L, "channel", I2S_CHANNEL_FMT_RIGHT_LEFT);
i2s_config.communication_format = opt_checkint(L, "format", I2S_COMM_FORMAT_STAND_I2S);
i2s_config.dma_buf_count = opt_checkint_range(L, "buffer_count", 2, 2, 128);
i2s_config.dma_buf_len = opt_checkint_range(L, "buffer_len", i2s_config.sample_rate / 100, 8, 1024);
i2s_config.dma_desc_num = opt_checkint_range(L, "buffer_count", 2, 2, 128);
i2s_config.dma_frame_num = opt_checkint_range(L, "buffer_len", i2s_config.sample_rate / 100, 8, 1024);
i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
//
pin_config.bck_io_num = opt_checkint(L, "bck_pin", I2S_PIN_NO_CHANGE);
......@@ -176,9 +176,9 @@ static int node_i2s_start( lua_State *L )
esp_err_t err;
if (i2s_config.mode & I2S_MODE_RX) {
err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_buf_count, &is->rx.queue);
err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_desc_num, &is->rx.queue);
} else {
err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_buf_count, NULL);
err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_desc_num, NULL);
}
if (err != ESP_OK)
luaL_error( L, "i2s can not start" );
......@@ -265,7 +265,7 @@ static int node_i2s_read( lua_State *L )
int wait_ms = luaL_optint(L, 3, 0);
char * data = luaM_malloc( L, bytes );
size_t read;
if (i2s_read(i2s_id, data, bytes, &read, wait_ms / portTICK_RATE_MS) != ESP_OK)
if (i2s_read(i2s_id, data, bytes, &read, wait_ms / portTICK_PERIOD_MS) != ESP_OK)
return luaL_error( L, "I2S driver error" );
lua_pushlstring(L, data, read);
......
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: ">=5.0.0"
# Component dependencies
libsodium: "~1.0.20"
......@@ -50,6 +50,13 @@ typedef struct {
};
} mqtt_context_t;
typedef struct {
mqtt_context_t *ctx;
esp_mqtt_event_t event;
} event_info_t;
// event_handler_t is the function signature for all events
typedef void (*event_handler_t)(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt_event_handle_t event);
......@@ -64,10 +71,12 @@ task_handle_t event_handler_task_id = 0;
// event_clone makes a copy of the mqtt event received so we can pass it on
// and the mqtt library can discard it.
static esp_mqtt_event_handle_t event_clone(esp_mqtt_event_handle_t ev) {
static event_info_t *event_clone(esp_mqtt_event_handle_t ev, mqtt_context_t *ctx) {
// allocate memory for the copy
esp_mqtt_event_handle_t ev1 = (esp_mqtt_event_handle_t)malloc(sizeof(esp_mqtt_event_t));
event_info_t *clone = (event_info_t *)malloc(sizeof(event_info_t));
ESP_LOGD(TAG, "event_clone(): event %p, event id %d, msg %d", ev, ev->event_id, ev->msg_id);
clone->ctx = ctx;
esp_mqtt_event_handle_t ev1 = &clone->event;
// make a shallow copy:
*ev1 = *ev;
......@@ -95,11 +104,12 @@ static esp_mqtt_event_handle_t event_clone(esp_mqtt_event_handle_t ev) {
ev1->topic = NULL;
}
}
return ev1;
return clone;
}
// event_free deallocates all the memory associated with a cloned event
static void event_free(esp_mqtt_event_handle_t ev) {
static void event_free(event_info_t *clone) {
esp_mqtt_event_handle_t ev = &clone->event;
if (ev->data != NULL) {
ESP_LOGD(TAG, "event_free():free: event %p, msg %d, data %p", ev, ev->msg_id, ev->data);
free(ev->data);
......@@ -108,7 +118,7 @@ static void event_free(esp_mqtt_event_handle_t ev) {
ESP_LOGD(TAG, "event_free():free: event %p, msg %d, topic %p", ev, ev->msg_id, ev->topic);
free(ev->topic);
}
free(ev);
free(clone);
}
// event_connected is run when the mqtt client connected
......@@ -235,10 +245,9 @@ static void event_data_received(lua_State* L, mqtt_context_t* mqtt_context, esp_
// event_task_handler takes a nodemcu task message and dispatches it to the appropriate event_xxx callback above.
static void event_task_handler(task_param_t param, task_prio_t prio) {
// extract the event data out of the task param
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)param;
// recover the mqtt context from the event user_context field:
mqtt_context_t* mqtt_context = (mqtt_context_t*)event->user_context;
event_info_t *info = (event_info_t *)param;
esp_mqtt_event_handle_t event = &info->event;
mqtt_context_t* mqtt_context = info->ctx;
// Check if this event is about an object that is in the process of garbage collection:
if (!luaX_valid_ref(mqtt_context->self)) {
......@@ -290,14 +299,17 @@ static void event_task_handler(task_param_t param, task_prio_t prio) {
lua_settop(L, top); // leave the stack as it was
task_handler_end:
event_free(event); // free the event copy memory
event_free(info); // free the cloned event info
}
// mqtt_event_handler receives all events from the esp mqtt library and converts them
// to a task message
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) {
task_post_medium(event_handler_task_id, (task_param_t)event_clone(event));
return ESP_OK;
static void mqtt_event_handler(void *handler_arg, esp_event_base_t base, int32_t event_id, void *event_data) {
(void)base;
(void)event_id;
event_info_t *clone = event_clone(event_data, handler_arg);
if (!task_post_medium(event_handler_task_id, (task_param_t)clone))
event_free(clone);
}
// Lua: on()
......@@ -330,7 +342,15 @@ static int mqtt_connect(lua_State* L) {
memset(&config, 0, sizeof(esp_mqtt_client_config_t));
// process function parameters populating the mqtt config structure
config.host = luaL_checkstring(L, 2);
const char *host = luaL_checkstring(L, 2);
bool is_mqtt_uri = strncmp(host, "mqtt://", 7) == 0;
bool is_mqtts_uri = strncmp(host, "mqtts://", 8) == 0;
bool is_ws_uri = strncmp(host, "ws://", 5) == 0;
bool is_wss_uri = strncmp(host, "wss://", 6) == 0;
if (is_mqtt_uri || is_mqtts_uri || is_ws_uri || is_wss_uri)
config.broker.address.uri = host;
else
config.broker.address.hostname = host;
// set defaults:
int secure = 0;
......@@ -340,14 +360,20 @@ static int mqtt_connect(lua_State* L) {
const char * cert_pem = NULL;
const char * client_cert_pem = NULL;
const char * client_key_pem = NULL;
size_t cert_pem_len = 0;
size_t client_cert_pem_len = 0;
size_t client_key_pem_len = 0;
if (lua_isnumber(L, n)) {
if (is_mqtt_uri || is_mqtts_uri)
return luaL_error(L, "port arg must be nil if giving full uri");
port = luaL_checknumber(L, n);
n++;
}
if (lua_isnumber(L, n)) {
if (is_mqtt_uri || is_mqtts_uri)
return luaL_error(L, "secure on/off determined by uri");
secure = !!luaL_checkinteger(L, n);
n++;
......@@ -355,19 +381,19 @@ static int mqtt_connect(lua_State* L) {
if (lua_istable(L, n)) {
secure = true;
lua_getfield(L, n, "ca_cert");
if ((cert_pem = luaL_optstring(L, -1, NULL)) != NULL) {
if ((cert_pem = luaL_optlstring(L, -1, NULL, &cert_pem_len)) != NULL) {
luaX_set_ref(L, -1, &mqtt_context->cert_pem);
}
lua_pop(L, 1);
//
lua_getfield(L, n, "client_cert");
if ((client_cert_pem = luaL_optstring(L, -1, NULL)) != NULL) {
if ((client_cert_pem = luaL_optlstring(L, -1, NULL, &client_cert_pem_len)) != NULL) {
luaX_set_ref(L, -1, &mqtt_context->client_cert_pem);
}
lua_pop(L, 1);
//
lua_getfield(L, n, "client_key");
if ((client_key_pem = luaL_optstring(L, -1, NULL)) != NULL) {
if ((client_key_pem = luaL_optlstring(L, -1, NULL, &client_key_pem_len)) != NULL) {
luaX_set_ref(L, -1, &mqtt_context->client_key_pem);
}
lua_pop(L, 1);
......@@ -393,24 +419,26 @@ static int mqtt_connect(lua_State* L) {
ESP_LOGD(TAG, "connect: mqtt_context*: %p", mqtt_context);
config.user_context = mqtt_context; // store a pointer to our context in the mqtt client user context field
// this will be useful to identify to which instance events belong to
config.event_handle = mqtt_event_handler; // set the function that will be called by the mqtt client everytime something
// happens
config.client_id = mqtt_context->client_id;
config.lwt_msg = mqtt_context->lwt_msg;
config.lwt_topic = mqtt_context->lwt_topic;
config.username = mqtt_context->username;
config.password = mqtt_context->password;
config.keepalive = mqtt_context->keepalive;
config.disable_clean_session = mqtt_context->disable_clean_session;
config.port = port;
config.disable_auto_reconnect = (reconnect == 0);
config.transport = secure ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP;
config.cert_pem = cert_pem;
config.client_cert_pem = client_cert_pem;
config.client_key_pem = client_key_pem;
if (config.broker.address.uri != NULL)
{
config.broker.address.port = port;
config.broker.address.transport =
secure ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP;
}
config.broker.verification.certificate = cert_pem;
config.broker.verification.certificate_len = cert_pem_len;
config.credentials.authentication.certificate = client_cert_pem;
config.credentials.authentication.certificate_len = client_cert_pem_len;
config.credentials.authentication.key = client_key_pem;
config.credentials.authentication.key_len = client_key_pem_len;
config.credentials.authentication.password = mqtt_context->password;
config.credentials.client_id = mqtt_context->client_id;
config.credentials.username = mqtt_context->username;
config.network.disable_auto_reconnect = (reconnect == 0);
config.session.disable_clean_session = mqtt_context->disable_clean_session;
config.session.keepalive = mqtt_context->keepalive;
config.session.last_will.msg = mqtt_context->lwt_msg;
config.session.last_will.topic = mqtt_context->lwt_topic;
// create a mqtt client instance
mqtt_context->client = esp_mqtt_client_init(&config);
......@@ -419,6 +447,10 @@ static int mqtt_connect(lua_State* L) {
return 0;
}
// register the event handler with mqtt_context as the handler arg
esp_mqtt_client_register_event(
mqtt_context->client, ESP_EVENT_ANY_ID, mqtt_event_handler, mqtt_context);
// actually start the mqtt client and connect
esp_err_t err = esp_mqtt_client_start(mqtt_context->client);
if (err != ESP_OK) {
......
......@@ -5,6 +5,7 @@
#include "platform.h"
#include "task/task.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "esp_log.h"
#include "esp_sleep.h"
#include "driver/rtc_io.h"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment