Compare commits

...

17 Commits
main2 ... main

Author SHA1 Message Date
370f791151 fixed minor incompatibilities with idf 5.4 version 2025-05-11 12:49:02 +02:00
9585559ab7 shift register added to the component 2025-04-18 17:51:42 +02:00
45284cb4db disable error fields in protocol response message if no errors 2025-04-13 11:23:21 +02:00
65c790ea8e fixed baud rate setting affected on not own uart 2025-04-09 15:23:53 +02:00
19a9a46b2c fixed serial port access while port is disabled 2025-04-01 11:53:45 +02:00
27e87e4083 rollback expected max data size to fix file transfer operations 2025-03-26 18:55:03 +02:00
9b807179f9 fixed disable serial port function 2025-03-19 10:03:24 +02:00
2a6f6233af fixed uart tx disabled 2025-03-19 09:39:02 +02:00
b52dcdc5d8 pppos control 2025-03-14 19:21:58 +02:00
d254ff5c46 MQTT test message memory optimisation 2025-02-25 18:59:18 +02:00
a162ef8091 added modem reset on many mqtt retry, added reboot and modem reset to
command interface, pppos control from config
2025-02-22 16:34:15 +02:00
ea5bad2c1d fixed memory allocation for ISO8601 time record 2025-02-18 14:21:16 +02:00
915e6aa7c5 added experemental PPPoS 2025-02-17 18:47:04 +02:00
490fd3bfa4 extended log time format changed to ISO8601 2025-02-05 10:38:59 +02:00
0687a6a981 payload name added to the transport protocol 2025-01-24 13:26:31 +02:00
94af947d1f serial port settings fixed 2025-01-23 20:10:39 +02:00
e8e8876686 wifi reconnect policy improved, default uart settings restored 2025-01-23 14:00:39 +02:00
19 changed files with 633 additions and 283 deletions

View File

@ -25,6 +25,8 @@ idf_component_register(
"src/MQTT.c"
"src/CronTimers.c"
"src/SerialPort.c"
src/PPPoS.c
src/ShiftRegisterSPI.c
src/sdcard.c
src/FileBlockHandler.c
src/OTA.c

42
Kconfig
View File

@ -812,6 +812,48 @@ menu "WebGUIApp"
endif
endmenu
menu "PPPoS configuration"
config WEBGUIAPP_PPPOS_ENABLE
bool "Enabled PPPoS transport"
default n
endmenu
menu "SR IO extender configuration"
config WEBGUIAPP_SR_ENABLE
bool "Enabled SR IO extender"
default n
config WEBGUIAPP_SR_CLOCK_MHZ
int "SR extender clock speed (MHz)"
range 1 20
default 10 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C3
default 30 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
help
Set the clock speed (MHz) of SPI interface.
config WEBGUIAPP_SR_CS
int "SR SC signal GPIO"
range GPIO_RANGE_MIN GPIO_RANGE_MAX
default 12
help
Set GPIO for chip select signal.
config WEBGUIAPP_SR_OUTPUTS
int "SR outputs number"
range 0 64
default 8
help
Set the number of outputs (must be a multiple of 8).
config WEBGUIAPP_SR_INPUTS
int "SR inputs number"
range 0 64
default 8
help
Set the number of inputs (must be a multiple of 8).
endmenu
menu "Serial port configuration"
config WEBGUIAPP_UART_TRANSPORT_ENABLE

View File

@ -39,8 +39,8 @@ int ExecCommand(char *cmd);
void GetObjectsInfo(char *data);
void SetCustomObjects(obj_struct_t *obj);
obj_struct_t* GetSystemObjects();
obj_struct_t* GetCustomObjects();
const obj_struct_t* GetSystemObjects();
const obj_struct_t* GetCustomObjects();

View File

@ -54,6 +54,9 @@ typedef int mqtt_app_err_t;
#define MQTT1 0
#define MQTT2 1
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
typedef struct
{
char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH];
@ -86,4 +89,6 @@ void SystemDataHandler(char *data, uint32_t len, int idx);
mqtt_app_err_t PublicTestMQTT(int idx);
esp_err_t ExternalServiceMQTTSend(char *servname, char *data, int len, int idx);
#endif
#endif /* MAIN_INCLUDE_MQTT_H_ */

View File

@ -73,6 +73,7 @@ void EthStart(void);
void WiFiTransportTask(void *prm);
void PPPConnReset (void);
void PPPModemColdStart(void);
void PPPModemSoftRestart(void);
@ -96,6 +97,7 @@ esp_netif_t* GetSTANetifAdapter(void);
esp_netif_t* GetAPNetifAdapter(void);
esp_netif_t* GetETHNetifAdapter(void);
bool isWIFIConnected(void);
bool isETHConnected(void);
bool isPPPConnected(void);

View File

@ -0,0 +1,51 @@
/*
* ShiftRegisterSPI.h
*
* Created on: Apr 18, 2025
* Author: bogd
*/
#ifndef COMPONENTS_WEBGUIAPP_INCLUDE_SHIFTREGISTERSPI_H_
#define COMPONENTS_WEBGUIAPP_INCLUDE_SHIFTREGISTERSPI_H_
#include "esp_err.h"
typedef enum {
VGPIO_NUM_NC = -1,
VGPIO_NUM_0 = 0,
VGPIO_NUM_1 = 1,
VGPIO_NUM_2 = 2,
VGPIO_NUM_3 = 3,
VGPIO_NUM_4 = 4,
VGPIO_NUM_5 = 5,
VGPIO_NUM_6 = 6,
VGPIO_NUM_7 = 7,
VGPIO_NUM_MAX,
/** @endcond */
} virtual_gpio_num_t;
typedef enum {
VGPIO_MOTOR_IN1 = 0,
VGPIO_MOTOR_IN2,
VGPIO_PILOT_RELAY,
VGPIO_RELAY1,
VGPIO_RELAY2,
VGPIO_TRIAC1,
VGPIO_TRIAC2,
VGPIO_TRIAC3
} virtual_gpio_funct_t;
esp_err_t ShiftRegInit(void);
esp_err_t vgpio_set_level(virtual_gpio_num_t gpio_num, uint8_t *gpio_level);
esp_err_t vgpio_get_level(virtual_gpio_num_t gpio_num, uint8_t *gpio_level);
esp_err_t vgpi_get_level(int gpio_num, uint8_t *gpio_level);
esp_err_t vgpio_set_reg(uint8_t reg);
void GPIOExtenderTxRx(uint8_t *tx, uint8_t *rx, int bt);
void GPIOInputRead(int num, int *val);
void GPIOInputWrite(int num, int *val);
#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_SHIFTREGISTERSPI_H_ */

View File

@ -98,6 +98,7 @@ typedef struct
time_t time;
int msgType;
int payloadType;
char payloadName[32];
void *payload;
unsigned char sha256[32];
} parsedData;
@ -111,6 +112,7 @@ typedef struct
}UART_DATA_SEND_STRUCT;
void InitSerialPort(void);
void InitPPPSerial();
void InitSysSDCard();
esp_err_t TransmitSerialPort(char *data, int ln);

View File

@ -35,6 +35,7 @@
#include "SystemApplication.h"
#include "UserCallbacks.h"
#include "CommandProcSys.h"
#include "ShiftRegisterSPI.h"
esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle, spi_transaction_t *trans_desc);

View File

@ -20,8 +20,10 @@
*/
#include "CommandProcSys.h"
#include "NetTransport.h"
#include "webguiapp.h"
#define TAG "COMMAND_PROC_SYS"
//#define MAX_OBJECTS_NUMBER CONFIG_WEBGUIAPP_MAX_OBJECTS_NUM
@ -48,6 +50,14 @@ static void SYSTEM_TEST_handle(char *obj, char *com, char *arg)
static void SYSTEM_REBOOT_handle(char *obj, char *com, char *arg)
{
ESP_LOGI(TAG, "Object:%s, Command:%s, Argument %s", obj, com, arg);
esp_restart();
}
static void MODEM_REBOOT_handle(char *obj, char *com, char *arg)
{
ESP_LOGI(TAG, "Object:%s, Command:%s, Argument %s", obj, com, arg);
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
PPPConnReset();
#endif
}
obj_struct_t *custom_com_obj_arr = NULL;
@ -65,9 +75,9 @@ const obj_struct_t com_obj_arr[] = {
},
{
.index = 0,
.object_name = "SYSTEM1",
.object_name = "MODEM",
.allowed_actions = { "TEST", "REBOOT", "TEST2", "TEST3", "TEST4" },
.command_handlers = { &SYSTEM_TEST_handle, &SYSTEM_REBOOT_handle }
.command_handlers = { &SYSTEM_TEST_handle, &MODEM_REBOOT_handle }
},
{
.index = 0,
@ -84,12 +94,12 @@ const obj_struct_t com_obj_arr[] = {
{ 0 }
};
obj_struct_t* GetSystemObjects()
const obj_struct_t* GetSystemObjects()
{
return &com_obj_arr;
return (obj_struct_t*)&com_obj_arr;
}
obj_struct_t* GetCustomObjects()
const obj_struct_t* GetCustomObjects()
{
return custom_com_obj_arr;
}

View File

@ -24,6 +24,7 @@
#include "../include/SysConfiguration.h"
#include "NetTransport.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_modem_api.h"
@ -32,6 +33,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
static EventGroupHandle_t event_group = NULL;
@ -62,13 +64,14 @@ void RegGSMReset(void (*gsm_rst)(uint8_t level)) { gsm_reset = gsm_rst; }
esp_netif_t *GetPPPNetifAdapter(void) {
if (isPPPConn)
return ppp_netif;
else
return NULL;
else
return NULL;
}
MODEM_INFO *GetPPPModemInfo(void) { return &mod_info; }
bool isPPPConnected(void) { return isPPPConn; }
void PPPConnReset(void) { isPPPConn = false; }
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
static void on_ppp_changed(void *arg, esp_event_base_t event_base,
@ -196,8 +199,9 @@ static void GSMInitTask(void *pvParameter) {
ppp_netif);
assert(dce);
if (esp_modem_set_baud(dce, CUSTOM_MODEM_BAUDRATE) == ESP_OK)
uart_set_baudrate(0, CUSTOM_MODEM_BAUDRATE);
uart_set_baudrate(CONFIG_MODEM_UART_PORT_NUM, CUSTOM_MODEM_BAUDRATE);
mod_info.model[0] = 0x00;
@ -266,7 +270,8 @@ static void GSMInitTask(void *pvParameter) {
}
ESP_LOGI(TAG, "PPP data mode OK");
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, pdMS_TO_TICKS(WAIT_FOR_GET_IP * 1000));
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE,
pdMS_TO_TICKS(WAIT_FOR_GET_IP * 1000));
isPPPinitializing = false;
vTaskDelete(NULL);
@ -281,7 +286,7 @@ modem_init_fail:
void PPPModemColdStart(void) {
ResetType = 0;
xTaskCreatePinnedToCore(GSMInitTask, "GSMInitTask", 1024 * 6, &ResetType, 3,
&initTaskhandle, 1);
&initTaskhandle, 1);
ESP_LOGI(TAG, "Start GSM cold initialization task");
}
@ -303,7 +308,8 @@ static void GSMRunTask(void *pvParameter) {
}
void PPPModemStart(void) {
xTaskCreatePinnedToCore(GSMRunTask, "GSMRunTask", 1024 * 4, &ResetType, 3, NULL, 1);
xTaskCreatePinnedToCore(GSMRunTask, "GSMRunTask", 1024 * 4, &ResetType, 3,
NULL, 0);
}
int PPPModemGetRSSI(void) {

View File

@ -20,11 +20,16 @@
*/
#include <SysConfiguration.h>
#include <SystemApplication.h>
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"
#include "Helpers.h"
#include "NetTransport.h"
#include "MQTT.h"
#include "UserCallbacks.h"
#if (ESP_IDF_VERSION_MAJOR == 5 && ESP_IDF_VERSION_MINOR > 1)
#include "esp_log_level.h"
#endif
#define TAG "MQTT"
#define SERVICE_NAME "SYSTEM" // Dedicated service name
@ -161,44 +166,16 @@ 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;
}
*/
#define MAX_MQTT_PUBLICTEST_MESSAGE 1024
mqtt_app_err_t PublicTestMQTT(int idx)
{
char tmp[10];
char resp[256];
char JSONMess[1024];
char *data = (char*) malloc(MAX_MQTT_PUBLICTEST_MESSAGE);
if (data == NULL)
return ESP_ERR_NO_MEM;
static char resp[256];
struct jWriteControl jwc;
jwOpen(&jwc, JSONMess, 1024 - 64, JW_OBJECT, JW_COMPACT);
jwOpen(&jwc, data, 1024 - 64, JW_OBJECT, JW_COMPACT);
jwObj_object(&jwc, "data");
time_t now;
time(&now);
@ -277,7 +254,7 @@ mqtt_app_err_t PublicTestMQTT(int idx)
jwEnd(&jwc); //close payload
jwEnd(&jwc); //close data
//calculate sha from 'data' object
char *datap = strstr(JSONMess, "\"data\":");
char *datap = strstr(data, "\"data\":");
if (datap)
{
datap += sizeof("\"data\":") - 1;
@ -291,13 +268,15 @@ mqtt_app_err_t PublicTestMQTT(int idx)
#endif
jwObj_string(&jwc, "signature", (char*) sha_print);
}
else
return ESP_ERR_NOT_FOUND;
else{
free(data);
return ESP_ERR_NOT_FOUND;}
jwClose(&jwc);
mqtt_app_err_t merr = API_OK;
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
if (SysServiceMQTTSend(data, strlen(data), idx) != ESP_OK)
merr = API_INTERNAL_ERR;
free(data);
return merr;
}
@ -349,8 +328,14 @@ msg_id = esp_mqtt_client_subscribe(client, (char*) topic, 0);
#endif
if (++MQTTReconnectCounter > MQTT_RECONNECT_CHANGE_ADAPTER)
{
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
char interface[3];
GetDefaultNetIFName(interface);
if(!strcmp((const char*)interface , "pp")) //Cold reboot modem on can't connect to ppp
PPPConnReset();
#endif
MQTTReconnectCounter = 0;
NextDefaultNetIF();
NextDefaultNetIF();
}
mqtt[idx].is_connected = false;
break;
@ -648,15 +633,18 @@ esp_err_t ExtendedLog(esp_log_level_t level, char *format, ...)
case ESP_LOG_ERROR:
ESP_LOGE(SPIRAL_LOG_TAG, "%s", data);
break;
#if (ESP_IDF_VERSION_MAJOR == 5 && ESP_IDF_VERSION_MINOR > 1)
case ESP_LOG_MAX:
#endif
}
for (int idx = 0; idx < 2; idx++)
{
if (GetMQTTHandlesPool(idx)->mqtt_queue == NULL)
continue;
char time[RFC3339_TIMESTAMP_LENGTH];
GetRFC3339Time(time);
char *buf = (char*) malloc(strlen(data) + RFC3339_TIMESTAMP_LENGTH + 2);
char time[ISO8601_TIMESTAMP_LENGTH];
GetISO8601Time(time);
char *buf = (char*) malloc(strlen(data) + ISO8601_TIMESTAMP_LENGTH + 2);
if (buf)
{
strcpy(buf, time);

View File

@ -30,7 +30,7 @@ extern struct netif *netif_default;
esp_netif_t* GetNetifCurrentDefault()
{
return netif_default;
return (esp_netif_t*)netif_default;
}
esp_netif_t* GetNetifByName(char *name)

103
src/PPPoS.c Normal file
View File

@ -0,0 +1,103 @@
/*
* PPPoS.c
*
* Created on: Feb 10, 2025
* Author: bogd
*/
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include "freertos/projdefs.h"
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/netif.h"
#include "netif/ppp/pppapi.h"
#include "netif/ppp/pppos.h"
#include "driver/uart.h"
#include "esp_netif.h"
#include <esp_log.h>
#define TAG "pppos"
// UART configuration
#define UART_PORT_NUM UART_NUM_2
#define UART_BAUD_RATE 115200
#define UART_RX_PIN 2
#define UART_TX_PIN 1
#define UART_BUF_SIZE 4096
#ifdef CONFIG_WEBGUIAPP_PPPOS_ENABLE
// PPP configuration
static ppp_pcb *ppp;
static struct netif ppp_netif;
// Function to handle PPP link status changes
static void ppp_link_status_cb(ppp_pcb *pcb, int err_code, void *ctx)
{
struct netif *pppif = (struct netif *)ctx;
if (err_code == PPPERR_NONE)
{
ESP_LOGI(TAG, "PPP connection established");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "IP address: %s", ip4addr_ntoa(netif_ip4_addr(pppif)));
ESP_LOGI(TAG, "Gateway: %s", ip4addr_ntoa(netif_ip4_gw(pppif)));
ESP_LOGI(TAG, "Netmask: %s", ip4addr_ntoa(netif_ip4_netmask(pppif)));
ESP_LOGI(TAG, "~~~~~~~~~~~");
return;
}
ESP_LOGI(TAG, "PPP connection lost");
}
// Function to output PPP data over UART
static u32_t pppos_output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
{
uart_write_bytes(UART_PORT_NUM, (const char *)data, len);
return len;
}
static void uart_init()
{
uart_config_t uart_config = { .baud_rate = UART_BAUD_RATE, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE };
uart_param_config(UART_PORT_NUM, &uart_config);
uart_set_pin(UART_PORT_NUM, UART_TX_PIN, UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_PORT_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, 0);
}
static uint8_t data[UART_BUF_SIZE];
static void pppos_task(void *arg)
{
while (1)
{
int len = uart_read_bytes(UART_PORT_NUM, data, UART_BUF_SIZE, pdMS_TO_TICKS(10));
if (len > 0)
{
pppos_input(ppp, data, len);
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void InitPPPSerial()
{
uart_init();
// Create PPPoS interface
ppp = pppapi_pppos_create(&ppp_netif, pppos_output_cb, ppp_link_status_cb, &ppp_netif);
if (ppp == NULL)
{
ESP_LOGE(TAG, "Failed to create PPPoS interface");
return;
}
// Set PPP as the default interface
pppapi_set_default(ppp);
// Connect PPP
pppapi_connect(ppp, 0);
xTaskCreate(pppos_task, "pppos_task", 4096 , (void *)0, 7, NULL);
}
#endif

View File

@ -552,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 },

View File

@ -16,8 +16,8 @@
* \version 1.0
* \date 2023-10-15
* \author Bogdan Pilyugin
* \brief
* \details
* \brief
* \details
* \copyright Apache License, Version 2.0
*/
@ -34,18 +34,26 @@
#include <sdkconfig.h>
#include <stdlib.h>
#include <string.h>
#include "SysConfiguration.h"
#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*/
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/netif.h"
#include "netif/ppp/pppapi.h"
#include "netif/ppp/pppos.h"
#include "esp_netif.h"
#define UART_TX_QUEUE_SIZE (5)
#define UART_RX_QUEUE_SIZE (5)
#define UART_DEBUG_MODE 0
#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*/
#ifdef CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE
#define UART_TX_QUEUE_SIZE (5)
#define UART_RX_QUEUE_SIZE (5)
#define UART_DEBUG_MODE 0
#if CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE
QueueHandle_t UARTtxQueueHandle;
static StaticQueue_t xStaticUARTtxQueue;
@ -54,8 +62,11 @@ uint8_t UARTtxQueueStorageArea[UART_TX_QUEUE_SIZE * sizeof(UART_DATA_SEND_STRUCT
static QueueHandle_t uart_event_queue;
static char rxbuf[CONFIG_WEBGUIAPP_UART_BUF_SIZE];
esp_err_t TransmitSerialPort(char *data, int ln)
{
if(!GetSysConf()->serialSettings.Flags.IsSerialEnabled)
return ESP_ERR_NOT_ALLOWED;
UART_DATA_SEND_STRUCT DSS;
char *buf = malloc(ln);
if (!buf)
@ -84,7 +95,6 @@ static void ReceiveHandlerAPI()
M.outputDataBuffer = respbuf;
M.outputDataLength = EXPECTED_MAX_DATA_SIZE;
ServiceDataHandler(&M);
if (M.outputDataBuffer[0] != 0x00 && M.outputDataLength > 0)
{
TransmitSerialPort(M.outputDataBuffer, MIN(EXPECTED_MAX_DATA_SIZE, strlen(M.outputDataBuffer)));
@ -104,31 +114,30 @@ 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);
ESP_LOGI(TAG, "uart[%d] event:%d", CONFIG_WEBGUIAPP_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);
#if UART_DEBUG_MODE == 1
ESP_LOGI(TAG, "read of %d bytes: %s", buffered_size, rxbuf);
#endif
if (GetSysConf()->serialSettings.Flags.IsBridgeEnabled)
{
ExternalServiceMQTTSend(EXTERNAL_SERVICE_NAME, rxbuf, buffered_size, 0);
@ -139,8 +148,8 @@ void serial_RX_task(void *arg)
}
}
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 +160,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 +210,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 +227,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 +238,89 @@ 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", 4096 * 2, (void *)0, 7, NULL);
xTaskCreate(serial_RX_task, "serial_rx", 4096 * 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

123
src/ShiftRegisterSPI.c Normal file
View File

@ -0,0 +1,123 @@
/*
* ShiftRegisterSPI.c
*
* Created on: Apr 18, 2025
* Author: bogd
*/
#include "SysConfiguration.h"
#include "webguiapp.h"
#include "sdkconfig.h"
#include "esp_timer.h"
#ifdef CONFIG_WEBGUIAPP_SR_ENABLE
#define TAG "ShiftRegDriver"
#define CONFIG_SHIFTREG_SPI_HOST (1)
#define CONFIG_SHIFTREG_SPI_CLOCK_HZ (10000000)
#define CONFIG_SHIFTREG_SPI_CS_GPIO (12)
#define CONFIG_SHIFTREG_SAMPLES_MS (10)
static spi_device_handle_t spi_shift_handle;
static spi_transaction_t spi_shift_transaction;
static uint8_t inputs[] = { 0, 0 }, outputs[] = { 0, 0 };
static const int SHIFTREG_SAMPLE_START_BIT = BIT0;
static EventGroupHandle_t digio_event_group = NULL;
static SemaphoreHandle_t xSemaphoreShiftRegHandle = NULL;
static StaticSemaphore_t xSemaphoreShiftRegBuf;
esp_timer_handle_t shiftreg_timer, inputs_periodical_timer;
void ShiftRegSampleStart(void *arg);
const esp_timer_create_args_t shiftreg_timer_args = { .callback = &ShiftRegSampleStart, .name = "shiftregTimer" };
void ShiftRegSampleStart(void *arg)
{
xEventGroupSetBits(digio_event_group, SHIFTREG_SAMPLE_START_BIT);
}
static esp_err_t shiftreg_txrx_transaction(uint8_t *tx, uint8_t *rx, int bits)
{
memset(&spi_shift_transaction, 0, sizeof(spi_shift_transaction));
spi_shift_transaction.cmd = 0;
spi_shift_transaction.addr = 0;
spi_shift_transaction.length = bits;
spi_shift_transaction.tx_buffer = tx;
spi_shift_transaction.rx_buffer = rx;
spi_shift_transaction.rxlength = 0;
esp_err_t err = spi_device_polling_transmit(spi_shift_handle, &spi_shift_transaction);
ESP_ERROR_CHECK(err);
return err;
}
static void shift_reg_task(void *pvParameter)
{
while (1)
{
xEventGroupWaitBits(digio_event_group, SHIFTREG_SAMPLE_START_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
xSemaphoreTake(xSemaphoreShiftRegHandle, portMAX_DELAY);
shiftreg_txrx_transaction(outputs, inputs, 8);
xSemaphoreGive(xSemaphoreShiftRegHandle);
}
}
esp_err_t ShiftRegInit(void)
{
spi_device_interface_config_t devcfg = { .command_bits = 0, .address_bits = 0, .mode = 0, .clock_speed_hz = CONFIG_SHIFTREG_SPI_CLOCK_HZ, .queue_size = 10, .spics_io_num = 12 };
esp_err_t ret = spi_bus_add_device(CONFIG_SHIFTREG_SPI_HOST, &devcfg, &spi_shift_handle);
ESP_ERROR_CHECK(ret);
outputs[0] = 0b00110000;
digio_event_group = xEventGroupCreate();
xSemaphoreShiftRegHandle = xSemaphoreCreateBinaryStatic(&xSemaphoreShiftRegBuf);
xSemaphoreGive(xSemaphoreShiftRegHandle);
ESP_ERROR_CHECK(esp_timer_create(&shiftreg_timer_args, &shiftreg_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(shiftreg_timer, CONFIG_SHIFTREG_SAMPLES_MS * 1000));
ESP_LOGI(TAG, "HC595 SPI device init OK");
return ESP_OK;
}
esp_err_t vgpio_set_level(virtual_gpio_num_t gpio_num, uint8_t *gpio_level)
{
if (gpio_num < 0 || gpio_num >= VGPIO_NUM_MAX)
return ESP_ERR_INVALID_ARG;
uint8_t lv = *gpio_level & 1;
xSemaphoreTake(xSemaphoreShiftRegHandle, portMAX_DELAY);
outputs[0] = (outputs[0] & ~(1 << gpio_num)) | (lv << gpio_num);
shiftreg_txrx_transaction(outputs, inputs, 8);
xSemaphoreGive(xSemaphoreShiftRegHandle);
return ESP_OK;
}
esp_err_t vgpio_get_level(virtual_gpio_num_t gpio_num, uint8_t *gpio_level)
{
if (gpio_num < 0 || gpio_num >= VGPIO_NUM_MAX)
return ESP_ERR_INVALID_ARG;
*gpio_level = (outputs[0] & (1 << gpio_num)) ? 1 : 0;
return ESP_OK;
}
esp_err_t vgpio_set_reg(uint8_t reg)
{
if (xSemaphoreTake(xSemaphoreShiftRegHandle, pdMS_TO_TICKS(50)) == pdTRUE)
{
outputs[0] = (outputs[0] & 0b11110001) | (reg << 1);
shiftreg_txrx_transaction(outputs, inputs, 8);
xSemaphoreGive(xSemaphoreShiftRegHandle);
return ESP_OK;
}
else
{
ESP_LOGW(TAG, "Can't obtain SPI bus");
return ESP_ERR_NOT_FINISHED;
}
}
#endif

View File

@ -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);
}

View File

@ -23,6 +23,7 @@
#include "../include/SysConfiguration.h"
#include "ShiftRegisterSPI.h"
#include "SystemApplication.h"
#include <webguiapp.h>
#include "stdlib.h"
@ -73,6 +74,7 @@ static void InitSysIO(void);
static void InitSysSPI(void);
static void InitSysI2C(void);
esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle,
spi_transaction_t *trans_desc)
{
@ -97,6 +99,11 @@ esp_err_t WebGuiAppInit(void)
#if CONFIG_WEBGUIAPP_SPI_ENABLE
InitSysSPI();
#endif
#ifdef CONFIG_WEBGUIAPP_SR_ENABLE
ShiftRegInit();
#endif
#if CONFIG_WEBGUIAPP_I2C_ENABLE
InitSysI2C();
#endif
@ -176,9 +183,14 @@ esp_err_t WebGuiAppInit(void)
#endif
#if CONFIG_WEBGUIAPP_UART_TRANSPORT_ENABLE
InitSerialPort();
if(GetSysConf()->serialSettings.Flags.IsSerialEnabled)
InitSerialPort();
#endif
#ifdef CONFIG_WEBGUIAPP_PPPOS_ENABLE
InitPPPSerial();
#endif
return ESP_OK;
}

View File

@ -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,7 @@ 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);
@ -323,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);
@ -372,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 = {
@ -395,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);
@ -412,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);
@ -423,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 = {
@ -456,14 +412,13 @@ 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_netif_get_hostname(sta_netif, (const char**)(&name));
ESP_LOGW(TAG, "Net bios name set to %s", name);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
@ -479,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);
@ -496,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)
{
@ -540,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)
@ -563,7 +522,6 @@ static void WiFiControlTask(void *arg)
}
}
}
}
if (isWiFiConnected)
@ -578,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()
@ -614,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)
{
@ -631,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()
@ -647,4 +605,3 @@ int GetAPClientsNumber()
}
return -1;
}