Compare commits
5 Commits
60f8299f59
...
0687a6a981
| Author | SHA1 | Date | |
|---|---|---|---|
| 0687a6a981 | |||
| 94af947d1f | |||
| e8e8876686 | |||
| 2547be508c | |||
| ca082595d9 |
|
|
@ -238,8 +238,9 @@ typedef struct
|
|||
|
||||
} modbusSettings;
|
||||
|
||||
|
||||
|
||||
cron_timer_t Timers[CONFIG_WEBGUIAPP_CRON_NUMBER];
|
||||
bool bIsCRONEnabled;
|
||||
|
||||
} SYS_CONFIG;
|
||||
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ typedef struct
|
|||
time_t time;
|
||||
int msgType;
|
||||
int payloadType;
|
||||
char payloadName[32];
|
||||
void *payload;
|
||||
unsigned char sha256[32];
|
||||
} parsedData;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ const char *exec_errors[] = {
|
|||
"param COMMAND not found",
|
||||
"object not exists",
|
||||
"command not exists",
|
||||
"handler not set"
|
||||
"handler not set",
|
||||
"param ARGUMENT not found",
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -158,6 +159,8 @@ static int ExecCommandParse(char *cmd)
|
|||
return 2; //ERROR_OBJECT_NOT_PARSED
|
||||
if (!com)
|
||||
return 3; //ERROR_ACTION_NOT_PARSED
|
||||
if (!arg)
|
||||
return 7; //ERROR_ACTION_NOT_PARSED
|
||||
|
||||
for (int idx = 0; idx < CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM; idx++)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@
|
|||
*/
|
||||
|
||||
#include <CronTimers.h>
|
||||
#include "SysConfiguration.h"
|
||||
#include "esp_log.h"
|
||||
#include "webguiapp.h"
|
||||
#include "string.h"
|
||||
|
|
@ -42,7 +43,8 @@ char* GetCronError()
|
|||
|
||||
void custom_cron_job_callback(cron_job *job)
|
||||
{
|
||||
ExecCommand(((cron_timer_t*) job->data)->exec);
|
||||
if (GetSysConf()->bIsCRONEnabled)
|
||||
ExecCommand(((cron_timer_t*) job->data)->exec);
|
||||
}
|
||||
|
||||
const char* check_expr(const char *expr)
|
||||
|
|
@ -96,7 +98,8 @@ static void ExecuteLastAction(obj_struct_t *objarr)
|
|||
if (minimal != -1)
|
||||
{
|
||||
ESP_LOGI(TAG, "Run previous CRON \"%s\" with delta %d", GetSysConf()->Timers[minimal].exec, (int )delta);
|
||||
ExecCommand(GetSysConf()->Timers[minimal].exec);
|
||||
if (GetSysConf()->bIsCRONEnabled)
|
||||
ExecCommand(GetSysConf()->Timers[minimal].exec);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
40
src/MQTT.c
40
src/MQTT.c
|
|
@ -161,42 +161,12 @@ esp_err_t ExternalServiceMQTTSend(char *servname, char *data, int len, int idx)
|
|||
}
|
||||
|
||||
#define MAX_ERROR_JSON 256
|
||||
/*
|
||||
mqtt_app_err_t PublicTestMQTT(int idx)
|
||||
{
|
||||
char tmp[10];
|
||||
char resp[256];
|
||||
char JSONMess[512];
|
||||
struct jWriteControl jwc;
|
||||
jwOpen(&jwc, JSONMess, MAX_ERROR_JSON, JW_OBJECT, JW_PRETTY);
|
||||
time_t now;
|
||||
time(&now);
|
||||
jwObj_int(&jwc, "time", (unsigned int) now);
|
||||
jwObj_string(&jwc, "event", "MQTT_TEST_MESSAGE)");
|
||||
strcpy(resp, "mqtt://");
|
||||
strcat(resp, GetSysConf()->mqttStation[idx].ServerAddr);
|
||||
itoa(GetSysConf()->mqttStation[idx].ServerPort, tmp, 10);
|
||||
strcat(resp, ":");
|
||||
strcat(resp, tmp);
|
||||
jwObj_string(&jwc, "url", resp);
|
||||
ComposeTopic(resp, idx, SERVICE_NAME, UPLINK_SUBTOPIC);
|
||||
jwObj_string(&jwc, "tx_topic", resp);
|
||||
ComposeTopic(resp, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
|
||||
jwObj_string(&jwc, "rx_topic", resp);
|
||||
jwEnd(&jwc);
|
||||
jwClose(&jwc);
|
||||
mqtt_app_err_t merr = API_OK;
|
||||
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
|
||||
merr = API_INTERNAL_ERR;
|
||||
return merr;
|
||||
}
|
||||
*/
|
||||
|
||||
static char resp[256];
|
||||
static char JSONMess[1024];
|
||||
mqtt_app_err_t PublicTestMQTT(int idx)
|
||||
{
|
||||
char tmp[10];
|
||||
char resp[256];
|
||||
char* JSONMess = malloc(1024);
|
||||
|
||||
struct jWriteControl jwc;
|
||||
jwOpen(&jwc, JSONMess, 1024 - 64, JW_OBJECT, JW_COMPACT);
|
||||
jwObj_object(&jwc, "data");
|
||||
|
|
@ -292,16 +262,12 @@ mqtt_app_err_t PublicTestMQTT(int idx)
|
|||
jwObj_string(&jwc, "signature", (char*) sha_print);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(JSONMess);
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
jwClose(&jwc);
|
||||
mqtt_app_err_t merr = API_OK;
|
||||
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
|
||||
merr = API_INTERNAL_ERR;
|
||||
free(JSONMess);
|
||||
return merr;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -449,7 +449,9 @@ const rest_var_t SystemVariables[] =
|
|||
|
||||
{ 0, "lat", &funct_lat, VAR_FUNCT, RW, 0, 0 },
|
||||
{ 0, "lon", &funct_lon, VAR_FUNCT, RW, 0, 0 },
|
||||
|
||||
|
||||
{ 0, "cronrecs_enab", &SysConfig.bIsCRONEnabled, VAR_BOOL, RW, 0, 1 },
|
||||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
{ 0, "mqtt_1_enab", &SysConfig.mqttStation[0].Flags1.bIsGlobalEnabled, VAR_BOOL, RW, 0, 1 },
|
||||
{ 0, "mqtt_1_serv", &SysConfig.mqttStation[0].ServerAddr, VAR_STRING, RW, 3, 63 },
|
||||
|
|
@ -550,7 +552,7 @@ const rest_var_t SystemVariables[] =
|
|||
{ 0, "serial_enab", &SysConfig.serialSettings.Flags.IsSerialEnabled, VAR_BOOL, RW, 0, 1 },
|
||||
{ 0, "serial_bridge", &SysConfig.serialSettings.Flags.IsBridgeEnabled, VAR_BOOL, RW, 0, 1 },
|
||||
{ 0, "serial_mode", &funct_serial_mode, VAR_FUNCT, R, 1, 2 },
|
||||
{ 0, "serial_baud", &SysConfig.serialSettings.BaudRate, VAR_INT, RW, 1200, 4096000 },
|
||||
{ 0, "serial_baud", &SysConfig.serialSettings.BaudRate, VAR_INT, RW, 1200, 8192000 },
|
||||
|
||||
{ 0, "serial_bits", &SysConfig.serialSettings.DataBits, VAR_INT, RW, 0, 3 },
|
||||
{ 0, "serial_parity", &SysConfig.serialSettings.Parity, VAR_INT, RW, 0, 3 },
|
||||
|
|
|
|||
166
src/SerialPort.c
166
src/SerialPort.c
|
|
@ -16,8 +16,8 @@
|
|||
* \version 1.0
|
||||
* \date 2023-10-15
|
||||
* \author Bogdan Pilyugin
|
||||
* \brief
|
||||
* \details
|
||||
* \brief
|
||||
* \details
|
||||
* \copyright Apache License, Version 2.0
|
||||
*/
|
||||
|
||||
|
|
@ -37,13 +37,13 @@
|
|||
#include "webguiapp.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#define TAG "serial_port"
|
||||
#define UART_READ_TOUT (100) // 3.5T * 8 = 28 ticks, TOUT=3 -> ~24..33 ticks
|
||||
#define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/
|
||||
#define TAG "serial_port"
|
||||
#define UART_READ_TOUT (80) // 3.5T * 8 = 28 ticks, TOUT=3 -> ~24..33 ticks
|
||||
#define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/
|
||||
|
||||
#define UART_TX_QUEUE_SIZE (5)
|
||||
#define UART_RX_QUEUE_SIZE (5)
|
||||
#define UART_DEBUG_MODE 0
|
||||
#define UART_TX_QUEUE_SIZE (5)
|
||||
#define UART_RX_QUEUE_SIZE (5)
|
||||
#define UART_DEBUG_MODE 0
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE
|
||||
|
||||
|
|
@ -104,24 +104,23 @@ void serial_RX_task(void *arg)
|
|||
|
||||
for (;;)
|
||||
{
|
||||
//Waiting for UART event.
|
||||
if (xQueueReceive(uart_event_queue, (void*) &event, portMAX_DELAY))
|
||||
// Waiting for UART event.
|
||||
if (xQueueReceive(uart_event_queue, (void *)&event, portMAX_DELAY))
|
||||
{
|
||||
#if UART_DEBUG_MODE == 1
|
||||
ESP_LOGI(TAG, "uart[%d] event:%d", CONFIG_UART_PORT_NUM, event.type);
|
||||
#endif
|
||||
switch (event.type)
|
||||
{
|
||||
//Event of UART receving data
|
||||
// Event of UART receving data
|
||||
/*We'd better handler data event fast, there would be much more data events than
|
||||
other types of events. If we take too much time on data event, the queue might
|
||||
be full.*/
|
||||
case UART_DATA:
|
||||
if (event.timeout_flag)
|
||||
{
|
||||
|
||||
bzero(rxbuf, CONFIG_WEBGUIAPP_UART_BUF_SIZE);
|
||||
uart_get_buffered_data_len(CONFIG_WEBGUIAPP_UART_PORT_NUM, (size_t*) &buffered_size);
|
||||
uart_get_buffered_data_len(CONFIG_WEBGUIAPP_UART_PORT_NUM, (size_t *)&buffered_size);
|
||||
if (buffered_size)
|
||||
{
|
||||
uart_read_bytes(CONFIG_WEBGUIAPP_UART_PORT_NUM, rxbuf, buffered_size, 100);
|
||||
|
|
@ -136,11 +135,10 @@ void serial_RX_task(void *arg)
|
|||
}
|
||||
else
|
||||
ReceiveHandlerAPI();
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
break;
|
||||
// Event of HW FIFO overflow detected
|
||||
case UART_FIFO_OVF:
|
||||
|
||||
#if UART_DEBUG_MODE == 1
|
||||
|
|
@ -151,36 +149,36 @@ void serial_RX_task(void *arg)
|
|||
// As an example, we directly flush the rx buffer here in order to read more data.
|
||||
uart_flush_input(CONFIG_WEBGUIAPP_UART_PORT_NUM);
|
||||
xQueueReset(uart_event_queue);
|
||||
break;
|
||||
//Event of UART ring buffer full
|
||||
break;
|
||||
// Event of UART ring buffer full
|
||||
case UART_BUFFER_FULL:
|
||||
ESP_LOGE(TAG, "ring buffer full");
|
||||
// If buffer full happened, you should consider encreasing your buffer size
|
||||
// As an example, we directly flush the rx buffer here in order to read more data.
|
||||
uart_flush_input(CONFIG_WEBGUIAPP_UART_PORT_NUM);
|
||||
xQueueReset(uart_event_queue);
|
||||
break;
|
||||
//Event of UART RX break detected
|
||||
break;
|
||||
// Event of UART RX break detected
|
||||
case UART_BREAK:
|
||||
#if UART_DEBUG_MODE == 1
|
||||
#if UART_DEBUG_MODE == 1
|
||||
ESP_LOGI(TAG, "uart rx break");
|
||||
#endif
|
||||
break;
|
||||
//Event of UART parity check error
|
||||
break;
|
||||
// Event of UART parity check error
|
||||
case UART_PARITY_ERR:
|
||||
ESP_LOGE(TAG, "uart parity error");
|
||||
break;
|
||||
//Event of UART frame error
|
||||
break;
|
||||
// Event of UART frame error
|
||||
case UART_FRAME_ERR:
|
||||
ESP_LOGE(TAG, "uart frame error");
|
||||
break;
|
||||
//UART_PATTERN_DET
|
||||
break;
|
||||
// UART_PATTERN_DET
|
||||
case UART_PATTERN_DET:
|
||||
|
||||
uart_get_buffered_data_len(CONFIG_WEBGUIAPP_UART_PORT_NUM, (size_t*) &buffered_size);
|
||||
uart_get_buffered_data_len(CONFIG_WEBGUIAPP_UART_PORT_NUM, (size_t *)&buffered_size);
|
||||
int pos = uart_pattern_pop_pos(CONFIG_WEBGUIAPP_UART_PORT_NUM);
|
||||
#if UART_DEBUG_MODE == 1
|
||||
ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", (int )pos, (int )buffered_size);
|
||||
ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", (int)pos, (int)buffered_size);
|
||||
#endif
|
||||
if (pos == -1)
|
||||
{
|
||||
|
|
@ -201,11 +199,11 @@ void serial_RX_task(void *arg)
|
|||
#endif
|
||||
}
|
||||
|
||||
break;
|
||||
//Others
|
||||
break;
|
||||
// Others
|
||||
default:
|
||||
ESP_LOGW(TAG, "uart event type: %d", event.type);
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -218,8 +216,7 @@ void static serial_TX_task(void *arg)
|
|||
while (1)
|
||||
{
|
||||
xQueueReceive(UARTtxQueueHandle, &DSS, portMAX_DELAY);
|
||||
if (uart_write_bytes(CONFIG_WEBGUIAPP_UART_PORT_NUM, DSS.raw_data_ptr, DSS.data_length)
|
||||
!= DSS.data_length)
|
||||
if (uart_write_bytes(CONFIG_WEBGUIAPP_UART_PORT_NUM, DSS.raw_data_ptr, DSS.data_length) != DSS.data_length)
|
||||
ESP_LOGE(TAG, "RS485 write data failure");
|
||||
free(DSS.raw_data_ptr);
|
||||
if (uart_wait_tx_done(CONFIG_WEBGUIAPP_UART_PORT_NUM, pdMS_TO_TICKS(1000)) != ESP_OK)
|
||||
|
|
@ -230,51 +227,86 @@ void static serial_TX_task(void *arg)
|
|||
void InitSerialPort(void)
|
||||
{
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = GetSysConf()->serialSettings.BaudRate,
|
||||
.data_bits = UART_DATA_7_BITS,
|
||||
.parity = UART_PARITY_EVEN,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_APB,
|
||||
.baud_rate = GetSysConf()->serialSettings.BaudRate,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_APB,
|
||||
};
|
||||
|
||||
//uart_config.data_bits = (uint8_t) GetSysConf()->serialSettings.DataBits;
|
||||
//uart_config.parity = (uint8_t) GetSysConf()->serialSettings.Parity;
|
||||
//uart_config.stop_bits = (uint8_t) GetSysConf()->serialSettings.StopBits;
|
||||
//uart_config.data_bits = (uint8_t)GetSysConf()->serialSettings.DataBits;
|
||||
|
||||
ESP_LOGI(TAG, "UART data_bits:%d parity:%d stop_bits:%d",
|
||||
GetSysConf()->serialSettings.DataBits,
|
||||
GetSysConf()->serialSettings.Parity,
|
||||
GetSysConf()->serialSettings.StopBits);
|
||||
switch (GetSysConf()->serialSettings.DataBits)
|
||||
{
|
||||
case 0:
|
||||
uart_config.data_bits = UART_DATA_5_BITS;
|
||||
break;
|
||||
case 1:
|
||||
uart_config.data_bits = UART_DATA_6_BITS;
|
||||
break;
|
||||
case 2:
|
||||
uart_config.data_bits = UART_DATA_7_BITS;
|
||||
break;
|
||||
case 3:
|
||||
uart_config.data_bits = UART_DATA_8_BITS;
|
||||
break;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(
|
||||
uart_driver_install(CONFIG_WEBGUIAPP_UART_PORT_NUM, CONFIG_WEBGUIAPP_UART_BUF_SIZE * 2, 0, 20, &uart_event_queue, 0));
|
||||
ESP_ERROR_CHECK(uart_param_config(CONFIG_WEBGUIAPP_UART_PORT_NUM, &uart_config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(CONFIG_WEBGUIAPP_UART_PORT_NUM,
|
||||
CONFIG_WEBGUIAPP_UART_TXD, CONFIG_WEBGUIAPP_UART_RXD, CONFIG_WEBGUIAPP_UART_RTS, -1));
|
||||
|
||||
switch (GetSysConf()->serialSettings.Parity)
|
||||
{
|
||||
case 0:
|
||||
uart_config.parity = UART_PARITY_DISABLE;
|
||||
break;
|
||||
case 2:
|
||||
uart_config.parity = UART_PARITY_EVEN;
|
||||
break;
|
||||
case 3:
|
||||
uart_config.parity = UART_PARITY_ODD;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
switch (GetSysConf()->serialSettings.StopBits)
|
||||
{
|
||||
case 1:
|
||||
uart_config.stop_bits = UART_STOP_BITS_1;
|
||||
break;
|
||||
case 2:
|
||||
uart_config.stop_bits = UART_STOP_BITS_1_5;
|
||||
break;
|
||||
case 3:
|
||||
uart_config.stop_bits = UART_STOP_BITS_2;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// uart_config.stop_bits = (uint8_t) GetSysConf()->serialSettings.StopBits;
|
||||
|
||||
ESP_LOGI(TAG, "UART data_bits:%d parity:%d stop_bits:%d", GetSysConf()->serialSettings.DataBits, GetSysConf()->serialSettings.Parity, GetSysConf()->serialSettings.StopBits);
|
||||
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_driver_install(CONFIG_WEBGUIAPP_UART_PORT_NUM, CONFIG_WEBGUIAPP_UART_BUF_SIZE * 2, 0, 20, &uart_event_queue, 0));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_param_config(CONFIG_WEBGUIAPP_UART_PORT_NUM, &uart_config));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_set_pin(CONFIG_WEBGUIAPP_UART_PORT_NUM, CONFIG_WEBGUIAPP_UART_TXD, CONFIG_WEBGUIAPP_UART_RXD, CONFIG_WEBGUIAPP_UART_RTS, -1));
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_UART_MODE_UART
|
||||
ESP_ERROR_CHECK(uart_set_mode(CONFIG_WEBGUIAPP_UART_PORT_NUM, UART_MODE_UART));
|
||||
#elif CONFIG_WEBGUIAPP_UART_MODE_RS485
|
||||
ESP_ERROR_CHECK(uart_set_mode(CONFIG_WEBGUIAPP_UART_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_set_mode(CONFIG_WEBGUIAPP_UART_PORT_NUM, UART_MODE_UART));
|
||||
#elif CONFIG_WEBGUIAPP_UART_MODE_RS485
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_set_mode(CONFIG_WEBGUIAPP_UART_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));
|
||||
#endif
|
||||
|
||||
ESP_ERROR_CHECK(uart_enable_rx_intr(CONFIG_WEBGUIAPP_UART_PORT_NUM));
|
||||
ESP_ERROR_CHECK(uart_set_rx_timeout(CONFIG_WEBGUIAPP_UART_PORT_NUM, UART_READ_TOUT));
|
||||
//ESP_ERROR_CHECK(uart_enable_pattern_det_baud_intr(CONFIG_UART_PORT_NUM, '+', PATTERN_CHR_NUM, 9, 0, 0));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_enable_rx_intr(CONFIG_WEBGUIAPP_UART_PORT_NUM));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(uart_set_rx_timeout(CONFIG_WEBGUIAPP_UART_PORT_NUM, UART_READ_TOUT));
|
||||
// ESP_ERROR_CHECK_WITHOUT_ABORT(uart_enable_pattern_det_baud_intr(CONFIG_UART_PORT_NUM, '+', PATTERN_CHR_NUM, 9, 0, 0));
|
||||
uart_pattern_queue_reset(CONFIG_WEBGUIAPP_UART_PORT_NUM, 20);
|
||||
|
||||
UARTtxQueueHandle = NULL;
|
||||
UARTtxQueueHandle = xQueueCreateStatic(UART_TX_QUEUE_SIZE,
|
||||
sizeof(UART_DATA_SEND_STRUCT),
|
||||
UARTtxQueueStorageArea,
|
||||
&xStaticUARTtxQueue);
|
||||
UARTtxQueueHandle = xQueueCreateStatic(UART_TX_QUEUE_SIZE, sizeof(UART_DATA_SEND_STRUCT), UARTtxQueueStorageArea, &xStaticUARTtxQueue);
|
||||
|
||||
xTaskCreate(serial_TX_task, "serial_tx", 1024 * 2, (void*) 0, 7, NULL);
|
||||
xTaskCreate(serial_RX_task, "serial_rx", 1024 * 4, (void*) 0, 12, NULL);
|
||||
ESP_LOGI(TAG, "Serial port initialized on UART%d with baudrate %d", CONFIG_WEBGUIAPP_UART_PORT_NUM,
|
||||
GetSysConf()->serialSettings.BaudRate);
|
||||
xTaskCreate(serial_TX_task, "serial_tx", 1024 * 2, (void *)0, 7, NULL);
|
||||
xTaskCreate(serial_RX_task, "serial_rx", 1024 * 4, (void *)0, 12, NULL);
|
||||
ESP_LOGI(TAG, "Serial port initialized on UART%d with baudrate %d", CONFIG_WEBGUIAPP_UART_PORT_NUM, GetSysConf()->serialSettings.BaudRate);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -16,14 +16,15 @@
|
|||
* \version 1.0
|
||||
* \date 2023-07-26
|
||||
* \author Bogdan Pilyugin
|
||||
* \brief
|
||||
* \details
|
||||
* \brief
|
||||
* \details
|
||||
* \copyright Apache License, Version 2.0
|
||||
*/
|
||||
|
||||
#include "webguiapp.h"
|
||||
#include "SystemApplication.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include <string.h>
|
||||
|
||||
#define TAG "SysComm"
|
||||
|
||||
|
|
@ -59,13 +60,14 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
jwObj_string(&jwc, "time", time);
|
||||
jwObj_int(&jwc, "msgtype", DATA_MESSAGE_TYPE_RESPONSE);
|
||||
jwObj_int(&jwc, "payloadtype", MSG->parsedData.payloadType);
|
||||
jwObj_string(&jwc, "payloadname", MSG->parsedData.payloadName);
|
||||
jwObj_object(&jwc, "payload");
|
||||
jwObj_int(&jwc, "applytype", 0);
|
||||
jwObj_object(&jwc, "variables");
|
||||
|
||||
jRead(MSG->inputDataBuffer, "{'data'{'payload'{'variables'", &result);
|
||||
if (result.dataType == JREAD_OBJECT)
|
||||
{ //Write variables
|
||||
{ // Write variables
|
||||
char VarName[VAR_MAX_NAME_LENGTH];
|
||||
char *VarValue = malloc(VAR_MAX_VALUE_LENGTH);
|
||||
if (!VarValue)
|
||||
|
|
@ -73,9 +75,7 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
|
||||
for (int i = 0; i < result.elements; ++i)
|
||||
{
|
||||
jRead_string(MSG->inputDataBuffer, "{'data'{'payload'{'variables'{*", VarName,
|
||||
VAR_MAX_NAME_LENGTH,
|
||||
&i);
|
||||
jRead_string(MSG->inputDataBuffer, "{'data'{'payload'{'variables'{*", VarName, VAR_MAX_NAME_LENGTH, &i);
|
||||
const char parsevar[] = "{'data'{'payload'{'variables'{'";
|
||||
char expr[sizeof(parsevar) + VAR_MAX_NAME_LENGTH];
|
||||
strcpy(expr, parsevar);
|
||||
|
|
@ -89,7 +89,7 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
esp_err_t res = ESP_ERR_INVALID_ARG;
|
||||
rest_var_types tp = VAR_ERROR;
|
||||
if (MSG->parsedData.msgType == DATA_MESSAGE_TYPE_COMMAND)
|
||||
{ //Write variables
|
||||
{ // Write variables
|
||||
res = SetConfVar(VarName, VarValue, &tp);
|
||||
if (tp != VAR_FUNCT)
|
||||
{
|
||||
|
|
@ -101,20 +101,18 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
tp = VAR_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{ //Read variables
|
||||
{ // Read variables
|
||||
res = GetConfVar(VarName, VarValue, &tp);
|
||||
if (res != ESP_OK)
|
||||
strcpy(VarValue, esp_err_to_name(res));
|
||||
}
|
||||
//Response with actual data
|
||||
// Response with actual data
|
||||
if (tp == VAR_STRING || tp == VAR_IPADDR || tp == VAR_ERROR || tp == VAR_PASS)
|
||||
jwObj_string(&jwc, VarName, VarValue);
|
||||
else
|
||||
jwObj_raw(&jwc, VarName, VarValue);
|
||||
|
||||
}
|
||||
free(VarValue);
|
||||
}
|
||||
|
|
@ -123,24 +121,23 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
|
||||
jwEnd(&jwc);
|
||||
jwEnd(&jwc);
|
||||
GetSysErrorDetales((sys_error_code) MSG->err_code, &err_br, &err_desc);
|
||||
jwObj_string(&jwc, "error", (char*) err_br);
|
||||
jwObj_string(&jwc, "error_descr", (char*) err_desc);
|
||||
GetSysErrorDetales((sys_error_code)MSG->err_code, &err_br, &err_desc);
|
||||
jwObj_string(&jwc, "error", (char *)err_br);
|
||||
jwObj_string(&jwc, "error_descr", (char *)err_desc);
|
||||
jwEnd(&jwc);
|
||||
|
||||
char *datap = strstr(MSG->outputDataBuffer, "\"data\":");
|
||||
if (datap)
|
||||
{
|
||||
datap += sizeof("\"data\":") - 1;
|
||||
SHA256hmacHash((unsigned char*) datap, strlen(datap), (unsigned char*) "mykey", sizeof("mykey"),
|
||||
MSG->parsedData.sha256);
|
||||
SHA256hmacHash((unsigned char *)datap, strlen(datap), (unsigned char *)"mykey", sizeof("mykey"), MSG->parsedData.sha256);
|
||||
unsigned char sha_print[32 * 2 + 1];
|
||||
BytesToStr(MSG->parsedData.sha256, sha_print, 32);
|
||||
sha_print[32 * 2] = 0x00;
|
||||
#if REAST_API_DEBUG_MODE
|
||||
ESP_LOGI(TAG, "SHA256 of DATA object is %s", sha_print);
|
||||
#endif
|
||||
jwObj_string(&jwc, "signature", (char*) sha_print);
|
||||
jwObj_string(&jwc, "signature", (char *)sha_print);
|
||||
}
|
||||
else
|
||||
return SYS_ERROR_SHA256_DATA;
|
||||
|
|
@ -150,7 +147,7 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
jRead(MSG->inputDataBuffer, "{'data'{'payload'{'applytype'", &result);
|
||||
if (result.elements == 1)
|
||||
{
|
||||
int atype = atoi((char*) result.pValue);
|
||||
int atype = atoi((char *)result.pValue);
|
||||
switch (atype)
|
||||
{
|
||||
case 0:
|
||||
|
|
@ -159,17 +156,16 @@ static sys_error_code PayloadDefaultTypeHandler(data_message_t *MSG)
|
|||
WriteNVSSysConfig(GetSysConf());
|
||||
if (CustomSaveConf != NULL)
|
||||
CustomSaveConf();
|
||||
break;
|
||||
break;
|
||||
case 2:
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
if (CustomSaveConf != NULL)
|
||||
CustomSaveConf();
|
||||
DelayedRestart();
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
return SYS_ERROR_PARSE_APPLYTYPE;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -194,8 +190,7 @@ static sys_error_code DataHeaderParser(data_message_t *MSG)
|
|||
jRead_string(MSG->inputDataBuffer, "{'data'", hashbuf, MSG->inputDataLength, 0);
|
||||
if (strlen(hashbuf) > 0)
|
||||
{
|
||||
SHA256hmacHash((unsigned char*) hashbuf, strlen(hashbuf), (unsigned char*) "mykey", sizeof("mykey"),
|
||||
MSG->parsedData.sha256);
|
||||
SHA256hmacHash((unsigned char *)hashbuf, strlen(hashbuf), (unsigned char *)"mykey", sizeof("mykey"), MSG->parsedData.sha256);
|
||||
unsigned char sha_print[32 * 2 + 1];
|
||||
BytesToStr(MSG->parsedData.sha256, sha_print, 32);
|
||||
sha_print[32 * 2] = 0x00;
|
||||
|
|
@ -210,22 +205,19 @@ static sys_error_code DataHeaderParser(data_message_t *MSG)
|
|||
return SYS_ERROR_PARSE_DATA;
|
||||
}
|
||||
|
||||
|
||||
jRead(MSG->inputDataBuffer, "{'signature'", &result);
|
||||
if (result.elements == 1)
|
||||
{
|
||||
#if REAST_API_DEBUG_MODE
|
||||
ESP_LOGI(TAG, "Signature is %.*s", 64, (char* )result.pValue);
|
||||
ESP_LOGI(TAG, "Signature is %.*s", 64, (char *)result.pValue);
|
||||
#endif
|
||||
|
||||
//Here compare calculated and received signature;
|
||||
// Here compare calculated and received signature;
|
||||
}
|
||||
else
|
||||
return SYS_ERROR_PARSE_SIGNATURE;
|
||||
|
||||
|
||||
|
||||
//Extract 'messidx' or throw exception
|
||||
// Extract 'messidx' or throw exception
|
||||
jRead(MSG->inputDataBuffer, "{'data'{'msgid'", &result);
|
||||
if (result.elements == 1)
|
||||
{
|
||||
|
|
@ -252,11 +244,11 @@ static sys_error_code DataHeaderParser(data_message_t *MSG)
|
|||
else
|
||||
strcpy(MSG->parsedData.dstID, "FFFFFFFF");
|
||||
|
||||
//Extract 'msgtype' or throw exception
|
||||
// Extract 'msgtype' or throw exception
|
||||
jRead(MSG->inputDataBuffer, "{'data'{'msgtype'", &result);
|
||||
if (result.elements == 1)
|
||||
{
|
||||
MSG->parsedData.msgType = atoi((char*) result.pValue);
|
||||
MSG->parsedData.msgType = atoi((char *)result.pValue);
|
||||
if (MSG->parsedData.msgType > DATA_MESSAGE_TYPE_RESPONSE || MSG->parsedData.msgType < DATA_MESSAGE_TYPE_COMMAND)
|
||||
return SYS_ERROR_PARSE_MSGTYPE;
|
||||
if (MSG->parsedData.msgType == DATA_MESSAGE_TYPE_RESPONSE)
|
||||
|
|
@ -265,21 +257,29 @@ static sys_error_code DataHeaderParser(data_message_t *MSG)
|
|||
else
|
||||
return SYS_ERROR_PARSE_MSGTYPE;
|
||||
|
||||
//Extract 'payloadtype' or throw exception
|
||||
// Extract 'payloadtype' or throw exception
|
||||
jRead(MSG->inputDataBuffer, "{'data'{'payloadtype'", &result);
|
||||
if (result.elements == 1)
|
||||
{
|
||||
MSG->parsedData.payloadType = atoi((char*) result.pValue);
|
||||
MSG->parsedData.payloadType = atoi((char *)result.pValue);
|
||||
}
|
||||
else
|
||||
return SYS_ERROR_PARSE_PAYLOADTYPE;
|
||||
|
||||
jRead(MSG->inputDataBuffer, "{'data'{'payloadname'", &result);
|
||||
if (result.elements == 1)
|
||||
{
|
||||
jRead_string(MSG->inputDataBuffer, "{'data'{'payloadname'", MSG->parsedData.payloadName, 31, 0);
|
||||
}
|
||||
else
|
||||
strcpy(MSG->parsedData.payloadName, "notset");
|
||||
|
||||
sys_error_code err = SYS_ERROR_HANDLER_NOT_SET;
|
||||
switch (MSG->parsedData.payloadType)
|
||||
{
|
||||
case PAYLOAD_DEFAULT:
|
||||
err = PayloadDefaultTypeHandler(MSG);
|
||||
break;
|
||||
break;
|
||||
}
|
||||
if (err != SYS_ERROR_HANDLER_NOT_SET)
|
||||
return err;
|
||||
|
|
@ -314,7 +314,7 @@ esp_err_t ServiceDataHandler(data_message_t *MSG)
|
|||
|
||||
if (MSG->err_code == SYS_GOT_RESPONSE_MESSAGE)
|
||||
{
|
||||
//ToDo Here handler of received data
|
||||
// ToDo Here handler of received data
|
||||
#if REAST_API_DEBUG_MODE
|
||||
ESP_LOGI(TAG, "Got response message with msgid=%d", (int)MSG->parsedData.msgID);
|
||||
#endif
|
||||
|
|
@ -336,9 +336,9 @@ esp_err_t ServiceDataHandler(data_message_t *MSG)
|
|||
jwObj_int(&jwc, "messtype", DATA_MESSAGE_TYPE_RESPONSE);
|
||||
const char *err_br;
|
||||
const char *err_desc;
|
||||
GetSysErrorDetales((sys_error_code) MSG->err_code, &err_br, &err_desc);
|
||||
jwObj_string(&jwc, "error", (char*) err_br);
|
||||
jwObj_string(&jwc, "error_descr", (char*) err_desc);
|
||||
GetSysErrorDetales((sys_error_code)MSG->err_code, &err_br, &err_desc);
|
||||
jwObj_string(&jwc, "error", (char *)err_br);
|
||||
jwObj_string(&jwc, "error_descr", (char *)err_desc);
|
||||
jwEnd(&jwc);
|
||||
jwClose(&jwc);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -452,6 +452,7 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C
|
|||
strcpy(Conf->Timers[i].exec, "OBJECT,ACTION,ARGUMENTS");
|
||||
|
||||
}
|
||||
Conf->bIsCRONEnabled = true;
|
||||
|
||||
#ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
* Project: ChargePointMainboard
|
||||
* Created on: 2022-07-21
|
||||
* Author: Bogdan Pilyugin
|
||||
* Description:
|
||||
* Description:
|
||||
*/
|
||||
|
||||
#include <SysConfiguration.h>
|
||||
|
|
@ -39,12 +39,12 @@ esp_netif_t *ap_netif;
|
|||
|
||||
static const char *TAG = "WiFiTransport";
|
||||
|
||||
#define WIFI_CONNECT_AFTER_FAIL_DELAY 40
|
||||
#define WIFI_AP_ONBOOT_TIME 300
|
||||
#define WIFI_CONNECT_AFTER_FAIL_DELAY 40
|
||||
#define WIFI_AP_ONBOOT_TIME 300
|
||||
|
||||
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
|
||||
#define EXAMPLE_ESP_WIFI_CHANNEL 6
|
||||
#define EXAMPLE_MAX_STA_CONN 10
|
||||
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
|
||||
#define EXAMPLE_ESP_WIFI_CHANNEL 6
|
||||
#define EXAMPLE_MAX_STA_CONN 10
|
||||
|
||||
static bool isWiFiRunning = false;
|
||||
static bool isWiFiConnected = false;
|
||||
|
|
@ -56,7 +56,7 @@ static int TempAPCounter = 0;
|
|||
static wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
|
||||
static bool isScanExecuting = false;
|
||||
|
||||
wifi_ap_record_t* GetWiFiAPRecord(uint8_t n)
|
||||
wifi_ap_record_t *GetWiFiAPRecord(uint8_t n)
|
||||
{
|
||||
if (n < DEFAULT_SCAN_LIST_SIZE)
|
||||
{
|
||||
|
|
@ -65,12 +65,12 @@ wifi_ap_record_t* GetWiFiAPRecord(uint8_t n)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
esp_netif_t* GetSTANetifAdapter(void)
|
||||
esp_netif_t *GetSTANetifAdapter(void)
|
||||
{
|
||||
return sta_netif;
|
||||
}
|
||||
|
||||
esp_netif_t* GetAPNetifAdapter(void)
|
||||
esp_netif_t *GetAPNetifAdapter(void)
|
||||
{
|
||||
return ap_netif;
|
||||
}
|
||||
|
|
@ -87,9 +87,7 @@ void resonnectWithDelay(void *agr)
|
|||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id,
|
||||
void *event_data)
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
|
|
@ -116,12 +114,11 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
|||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_BEACON_TIMEOUT)
|
||||
{
|
||||
ESP_LOGW(TAG, "STA beacon timeout");
|
||||
|
||||
}
|
||||
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
const esp_netif_ip_info_t *ip_info = &event->ip_info;
|
||||
memcpy(&GetSysConf()->wifiSettings.InfIPAddr, &event->ip_info.ip, sizeof(event->ip_info.ip));
|
||||
memcpy(&GetSysConf()->wifiSettings.InfMask, &event->ip_info.netmask, sizeof(event->ip_info.netmask));
|
||||
|
|
@ -136,7 +133,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
|||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP)
|
||||
{
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
const esp_netif_ip_info_t *ip_info = &event->ip_info;
|
||||
memcpy(&GetSysConf()->wifiSettings.InfIPAddr, &event->ip_info.ip, sizeof(event->ip_info.ip));
|
||||
memcpy(&GetSysConf()->wifiSettings.InfMask, &event->ip_info.netmask, sizeof(event->ip_info.netmask));
|
||||
|
|
@ -152,30 +149,26 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
|||
|
||||
else if (event_id == WIFI_EVENT_AP_STACONNECTED)
|
||||
{
|
||||
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t*) event_data;
|
||||
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
|
||||
ESP_LOGI(TAG, "station " MACSTR " join, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
}
|
||||
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
|
||||
{
|
||||
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t*) event_data;
|
||||
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
|
||||
ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_init_softap(void *pvParameter)
|
||||
{
|
||||
char if_key_str[24];
|
||||
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
|
||||
;
|
||||
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
|
||||
|
||||
strcpy(if_key_str, "WIFI_AP_USER");
|
||||
esp_netif_conf.if_key = if_key_str;
|
||||
esp_netif_conf.route_prio = AP_PRIO;
|
||||
|
||||
esp_netif_config_t cfg_netif = {
|
||||
.base = &esp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
||||
};
|
||||
esp_netif_config_t cfg_netif = { .base = &esp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP };
|
||||
|
||||
ap_netif = esp_netif_new(&cfg_netif);
|
||||
assert(ap_netif);
|
||||
|
|
@ -196,15 +189,10 @@ static void wifi_init_softap(void *pvParameter)
|
|||
esp_netif_attach_wifi_ap(ap_netif);
|
||||
esp_wifi_set_default_wifi_ap_handlers();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
|
|
@ -220,8 +208,7 @@ static void wifi_init_softap(void *pvParameter)
|
|||
}
|
||||
|
||||
memcpy(wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
||||
memcpy(wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
memcpy(wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey, strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
|
|
@ -238,16 +225,13 @@ static void wifi_init_softap(void *pvParameter)
|
|||
|
||||
static void wifi_init_sta(void *pvParameter)
|
||||
{
|
||||
//sta_netif = esp_netif_create_default_wifi_sta();
|
||||
// sta_netif = esp_netif_create_default_wifi_sta();
|
||||
char if_key_str[24];
|
||||
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
|
||||
strcpy(if_key_str, "WIFI_STA_USER");
|
||||
esp_netif_conf.if_key = if_key_str;
|
||||
esp_netif_conf.route_prio = STA_PRIO;
|
||||
esp_netif_config_t cfg_netif = {
|
||||
.base = &esp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
|
||||
};
|
||||
esp_netif_config_t cfg_netif = { .base = &esp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
|
||||
sta_netif = esp_netif_new(&cfg_netif);
|
||||
assert(sta_netif);
|
||||
|
||||
|
|
@ -262,7 +246,7 @@ static void wifi_init_sta(void *pvParameter)
|
|||
esp_netif_set_ip_info(sta_netif, &ip_info);
|
||||
esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
||||
|
||||
//esp_netif_str_to_ip4(&GetSysConf()->wifiSettings.DNSAddr3, (esp_ip4_addr_t*)(&dns_info.ip));
|
||||
// esp_netif_str_to_ip4(&GetSysConf()->wifiSettings.DNSAddr3, (esp_ip4_addr_t*)(&dns_info.ip));
|
||||
memcpy(&dns_info.ip, &GetSysConf()->wifiSettings.DNSAddr3, sizeof(esp_ip4_addr_t));
|
||||
|
||||
esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_FALLBACK, &dns_info);
|
||||
|
|
@ -273,22 +257,13 @@ static void wifi_init_sta(void *pvParameter)
|
|||
esp_netif_attach_wifi_station(sta_netif);
|
||||
esp_wifi_set_default_wifi_sta_handlers();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
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_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));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
|
|
@ -304,8 +279,9 @@ static void wifi_init_sta(void *pvParameter)
|
|||
},
|
||||
};
|
||||
memcpy(wifi_config.sta.ssid, GetSysConf()->wifiSettings.InfSSID, strlen(GetSysConf()->wifiSettings.InfSSID));
|
||||
memcpy(wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
memcpy(wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey, strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
|
||||
esp_netif_set_hostname(sta_netif, GetSysConf()->NetBIOSName);
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
|
|
@ -321,39 +297,32 @@ static void wifi_init_sta(void *pvParameter)
|
|||
|
||||
static void wifi_init_apsta(void *pvParameter)
|
||||
{
|
||||
//BEGIN AP MODE IF
|
||||
// BEGIN AP MODE IF
|
||||
char ap_if_key_str[24];
|
||||
esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
|
||||
;
|
||||
esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
|
||||
|
||||
strcpy(ap_if_key_str, "WIFI_AP_USER");
|
||||
ap_esp_netif_conf.if_key = ap_if_key_str;
|
||||
ap_esp_netif_conf.route_prio = AP_PRIO;
|
||||
|
||||
esp_netif_config_t cfg_netif = {
|
||||
.base = &ap_esp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
||||
};
|
||||
esp_netif_config_t cfg_netif = { .base = &ap_esp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP };
|
||||
|
||||
ap_netif = esp_netif_new(&cfg_netif);
|
||||
assert(ap_netif);
|
||||
//END AP MODE IF
|
||||
// END AP MODE IF
|
||||
|
||||
//BEGIN STA MODE IF
|
||||
// BEGIN STA MODE IF
|
||||
char sta_if_key_str[24];
|
||||
esp_netif_inherent_config_t staesp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
|
||||
strcpy(sta_if_key_str, "WIFI_STA_USER");
|
||||
staesp_netif_conf.if_key = sta_if_key_str;
|
||||
staesp_netif_conf.route_prio = STA_PRIO;
|
||||
esp_netif_config_t sta_cfg_netif = {
|
||||
.base = &staesp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
|
||||
};
|
||||
esp_netif_config_t sta_cfg_netif = { .base = &staesp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
|
||||
sta_netif = esp_netif_new(&sta_cfg_netif);
|
||||
assert(sta_netif);
|
||||
//END STA MODE IF
|
||||
// END STA MODE IF
|
||||
|
||||
//BEGIN AP MODE CONFIGURATION
|
||||
// BEGIN AP MODE CONFIGURATION
|
||||
esp_netif_ip_info_t ip_info;
|
||||
memcpy(&ip_info.ip, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||
memcpy(&ip_info.gw, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||
|
|
@ -370,8 +339,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
esp_netif_attach_wifi_ap(ap_netif);
|
||||
esp_wifi_set_default_wifi_ap_handlers();
|
||||
|
||||
wifi_init_config_t ap_cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
wifi_init_config_t ap_cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&ap_cfg));
|
||||
wifi_config_t ap_wifi_config = {
|
||||
|
|
@ -393,13 +361,12 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
}
|
||||
|
||||
memcpy(ap_wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
||||
memcpy(ap_wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
memcpy(ap_wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey, strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
ap_wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
||||
//END AP MODE CONFIGURATION
|
||||
// END AP MODE CONFIGURATION
|
||||
|
||||
//BEGIN STA MODE CONFIGURATION
|
||||
//esp_netif_ip_info_t ip_info;
|
||||
// BEGIN STA MODE CONFIGURATION
|
||||
// esp_netif_ip_info_t ip_info;
|
||||
memcpy(&ip_info.ip, &GetSysConf()->wifiSettings.InfIPAddr, 4);
|
||||
memcpy(&ip_info.gw, &GetSysConf()->wifiSettings.InfGateway, 4);
|
||||
memcpy(&ip_info.netmask, &GetSysConf()->wifiSettings.InfMask, 4);
|
||||
|
|
@ -410,7 +377,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
esp_netif_set_ip_info(sta_netif, &ip_info);
|
||||
esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &sta_dns_info);
|
||||
|
||||
//esp_netif_str_to_ip4(&GetSysConf()->wifiSettings.DNSAddr3, (esp_ip4_addr_t*)(&dns_info.ip));
|
||||
// esp_netif_str_to_ip4(&GetSysConf()->wifiSettings.DNSAddr3, (esp_ip4_addr_t*)(&dns_info.ip));
|
||||
memcpy(&sta_dns_info.ip, &GetSysConf()->wifiSettings.DNSAddr3, sizeof(esp_ip4_addr_t));
|
||||
|
||||
esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_FALLBACK, &sta_dns_info);
|
||||
|
|
@ -421,22 +388,13 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
esp_netif_attach_wifi_station(sta_netif);
|
||||
esp_wifi_set_default_wifi_sta_handlers();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
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_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));
|
||||
|
||||
wifi_config_t sta_wifi_config = {
|
||||
.sta = {
|
||||
|
|
@ -454,9 +412,14 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
},
|
||||
};
|
||||
memcpy(sta_wifi_config.sta.ssid, GetSysConf()->wifiSettings.InfSSID, strlen(GetSysConf()->wifiSettings.InfSSID));
|
||||
memcpy(sta_wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
//END STA MODE CONFIGURATION
|
||||
memcpy(sta_wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey, strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
// END STA MODE CONFIGURATION
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "test_TEST"));
|
||||
ESP_ERROR_CHECK(esp_netif_set_hostname(ap_netif, GetSysConf()->NetBIOSName));
|
||||
char name[32];
|
||||
esp_netif_get_hostname(sta_netif, &name);
|
||||
ESP_LOGW(TAG, "Net bios name set to %s", name);
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_wifi_config));
|
||||
|
|
@ -471,8 +434,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
|
||||
wifi_country_t CC;
|
||||
esp_wifi_get_country(&CC);
|
||||
ESP_LOGI(TAG, "Country code %.*s, start_ch=%d, total_ch=%d, max power %d", 3, CC.cc, CC.schan, CC.nchan,
|
||||
CC.max_tx_power);
|
||||
ESP_LOGI(TAG, "Country code %.*s, start_ch=%d, total_ch=%d, max power %d", 3, CC.cc, CC.schan, CC.nchan, CC.max_tx_power);
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_softap_sta finished");
|
||||
vTaskDelete(NULL);
|
||||
|
|
@ -488,38 +450,40 @@ void WiFiConnect(void)
|
|||
esp_wifi_connect();
|
||||
}
|
||||
|
||||
#define RECONNECT_INTERVAL_AP 60
|
||||
#define RECONNECT_INTERVAL_STA 30
|
||||
#define WAITIP_INTERVAL 10
|
||||
#define RECONNECT_INTERVAL_AP 30
|
||||
#define RECONNECT_INTERVAL_STA 30
|
||||
#define RECONNECT_FAST_ATTEMPTS 5
|
||||
#define WAITIP_INTERVAL 10
|
||||
|
||||
static void WiFiControlTask(void *arg)
|
||||
{
|
||||
//WiFi init and start block
|
||||
// WiFi init and start block
|
||||
static int attempts = 0;
|
||||
static int reconnect_counter;
|
||||
reconnect_counter =
|
||||
(GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA) ? RECONNECT_INTERVAL_STA : RECONNECT_INTERVAL_AP;
|
||||
reconnect_counter = 5;
|
||||
static int waitip_counter = WAITIP_INTERVAL;
|
||||
//s_wifi_event_group = xEventGroupCreate();
|
||||
// s_wifi_event_group = xEventGroupCreate();
|
||||
switch (GetSysConf()->wifiSettings.WiFiMode)
|
||||
{
|
||||
case WIFI_MODE_STA:
|
||||
xTaskCreate(wifi_init_sta, "InitStationTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||
break;
|
||||
xTaskCreate(wifi_init_sta, "InitStationTask", 1024 * 4, (void *)0, 3, NULL);
|
||||
break;
|
||||
case WIFI_MODE_AP:
|
||||
xTaskCreate(wifi_init_softap, "InitSoftAPTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||
break;
|
||||
xTaskCreate(wifi_init_softap, "InitSoftAPTask", 1024 * 4, (void *)0, 3, NULL);
|
||||
break;
|
||||
case WIFI_MODE_APSTA:
|
||||
xTaskCreate(wifi_init_apsta, "InitSoftAPSTATask", 1024 * 4, (void*) 0, 3, NULL);
|
||||
break;
|
||||
xTaskCreate(wifi_init_apsta, "InitSoftAPSTATask", 1024 * 4, (void *)0, 3, NULL);
|
||||
break;
|
||||
}
|
||||
isWiFiRunning = true;
|
||||
//WiFi in work service
|
||||
// WiFi in work service
|
||||
TempAPCounter = GetSysConf()->wifiSettings.AP_disab_time * 60;
|
||||
while (isWiFiRunning)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
if (isWiFiConnected)
|
||||
{
|
||||
attempts = 0;
|
||||
reconnect_counter = RECONNECT_INTERVAL_STA;
|
||||
if (!isWiFiGotIp)
|
||||
{
|
||||
|
|
@ -532,16 +496,19 @@ static void WiFiControlTask(void *arg)
|
|||
}
|
||||
}
|
||||
if (isWiFiFail)
|
||||
|
||||
{
|
||||
if (--reconnect_counter <= 0 && !isScanExecuting)
|
||||
{
|
||||
ESP_LOGI(TAG, "WiFi STA started, reconnecting to AP...");
|
||||
esp_wifi_connect();
|
||||
reconnect_counter =
|
||||
(GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA) ?
|
||||
RECONNECT_INTERVAL_STA : RECONNECT_INTERVAL_AP;
|
||||
if (++attempts <= RECONNECT_FAST_ATTEMPTS)
|
||||
reconnect_counter = 5;
|
||||
else
|
||||
reconnect_counter = (GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA) ? RECONNECT_INTERVAL_STA : RECONNECT_INTERVAL_AP;
|
||||
}
|
||||
}
|
||||
|
||||
if (TempAPCounter > 0)
|
||||
{
|
||||
if (--TempAPCounter <= 0)
|
||||
|
|
@ -555,7 +522,6 @@ static void WiFiControlTask(void *arg)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (isWiFiConnected)
|
||||
|
|
@ -570,7 +536,7 @@ static void WiFiControlTask(void *arg)
|
|||
|
||||
void WiFiStart(void)
|
||||
{
|
||||
xTaskCreate(WiFiControlTask, "WiFiCtrlTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||
xTaskCreate(WiFiControlTask, "WiFiCtrlTask", 1024 * 4, (void *)0, 3, NULL);
|
||||
}
|
||||
|
||||
void WiFiStop()
|
||||
|
|
@ -606,7 +572,7 @@ static void wifi_scan(void *arg)
|
|||
{
|
||||
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
|
||||
uint16_t ap_count = 0;
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); //delay for command result get before network break
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); // delay for command result get before network break
|
||||
memset(ap_info, 0, sizeof(ap_info));
|
||||
while (esp_wifi_scan_start(NULL, true) == ESP_ERR_WIFI_STATE)
|
||||
{
|
||||
|
|
@ -623,7 +589,7 @@ static void wifi_scan(void *arg)
|
|||
void WiFiScan(void)
|
||||
{
|
||||
isScanExecuting = true;
|
||||
xTaskCreate(wifi_scan, "ScanWiFiTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||
xTaskCreate(wifi_scan, "ScanWiFiTask", 1024 * 4, (void *)0, 3, NULL);
|
||||
}
|
||||
|
||||
int GetAPClientsNumber()
|
||||
|
|
@ -639,4 +605,3 @@ int GetAPClientsNumber()
|
|||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user