improvWifi bug fixes resulting adding network interface component

Signed-off-by: Karl Osterseher <karli_o@gmx.at>
This commit is contained in:
Karl Osterseher
2025-02-21 23:58:23 +01:00
Unverified
parent fa9852ac31
commit 4cafdcaf37
5 changed files with 80 additions and 25 deletions

View File

@@ -6,6 +6,8 @@
*/
#include "improvWifi.h"
#include <stdio.h>
#include <string>
void ImprovWiFi::handleSerial(const uint8_t *data, size_t length) {
@@ -274,7 +276,9 @@ bool ImprovWiFi::parseImprovSerial(size_t position, uint8_t byte,
if (position == 8 + data_len + 1) {
uint8_t checksum = 0x00;
for (size_t i = 0; i < position; i++) checksum += buffer[i];
for (size_t i = 0; i < position; i++) {
checksum += buffer[i];
}
if (checksum != byte) {
_position = 0;

View File

@@ -5,6 +5,7 @@
* Author: karl
*/
#include <stdlib.h>
#include <string.h>
#include "driver/uart.h"
@@ -31,13 +32,25 @@ static QueueHandle_t uart0_queue;
void uart_event_handler(void) {
uart_event_t event;
uint8_t dtmp[RD_BUF_SIZE];
size_t buffered_size;
uint8_t *dtmp;
// Waiting for UART event.
if (xQueueReceive(uart0_queue, (void *)&event, (TickType_t)portMAX_DELAY)) {
bzero(dtmp, RD_BUF_SIZE);
// ESP_LOGI(TAG, "uart[%d] event:", UART_NUM_0);
dtmp = (uint8_t *)calloc(1, event.size);
if (!dtmp) {
ESP_LOGE(TAG, "no free memory for uart receive. Dropping data...");
uint8_t drop;
while (event.size--) {
uart_read_bytes(UART_NUM_0, &drop, event.size, portMAX_DELAY);
}
return;
}
// ESP_LOGI(TAG, "uart[%d] event: %d, size %d", UART_NUM_0, event.type,
// event.size);
switch (event.type) {
// Event of UART receving data
/*We'd better handler data event fast, there would be much more data
@@ -53,7 +66,7 @@ void uart_event_handler(void) {
break;
// Event of HW FIFO overflow detected
case UART_FIFO_OVF:
// ESP_LOGI(TAG, "hw fifo overflow");
// ESP_LOGI(TAG, "hw fifo overflow");
// If fifo overflow happened, you should consider adding flow control
// for your application. The ISR has already reset the rx FIFO, As an
@@ -76,6 +89,8 @@ void uart_event_handler(void) {
// ESP_LOGI(TAG, "uart event type: %d", event.type);
break;
}
free(dtmp);
}
}
@@ -151,17 +166,17 @@ bool improv_wifi_connect(const char *ssid, const char *password) {
uint8_t count = 0;
esp_netif_ip_info_t ip;
esp_wifi_disconnect();
while (wifi_get_ip(&ip) == true) {
vTaskDelay(pdMS_TO_TICKS(100));
}
wifi_config_t wifi_config;
ESP_ERROR_CHECK(esp_wifi_get_config(WIFI_IF_STA, &wifi_config));
strcpy((char *)wifi_config.sta.ssid, ssid);
strcpy((char *)wifi_config.sta.password, password);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
esp_wifi_disconnect();
while (wifi_get_ip(&ip) == true) {
vTaskDelay(pdMS_TO_TICKS(100));
}
esp_wifi_connect();
while (wifi_get_ip(&ip) == false) {
vTaskDelay(pdMS_TO_TICKS(500));

View File

@@ -29,6 +29,7 @@ static uint8_t eth_port_cnt = 0;
static esp_netif_ip_info_t ip_info = {{0}, {0}, {0}};
static bool connected = false;
static SemaphoreHandle_t connIpSemaphoreHandle = NULL;
#if CONFIG_SNAPCLIENT_SPI_ETHERNETS_NUM
#define SPI_ETHERNETS_NUM CONFIG_SNAPCLIENT_SPI_ETHERNETS_NUM
@@ -364,6 +365,10 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
break;
case ETHERNET_EVENT_DISCONNECTED:
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
connected = false;
xSemaphoreGive(connIpSemaphoreHandle);
ESP_LOGI(TAG, "Ethernet Link Down");
break;
case ETHERNET_EVENT_START:
@@ -392,12 +397,13 @@ static void lost_ip_event_handler(void *arg, esp_event_base_t event_base,
if (network_is_our_netif(if_desc_str, event->esp_netif)) {
// const esp_netif_ip_info_t *ip_info = &event->ip_info;
memcpy((void *)&ip_info, (const void *)&event->ip_info,
sizeof(esp_netif_ip_info_t));
ESP_LOGI(TAG, "Ethernet Lost IP Address");
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
memcpy((void *)&ip_info, (const void *)&event->ip_info,
sizeof(esp_netif_ip_info_t));
connected = false;
xSemaphoreGive(connIpSemaphoreHandle);
break;
}
@@ -417,10 +423,13 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
strcat(strcpy(if_desc_str, NETWORK_INTERFACE_DESC_ETH), num_str);
if (network_is_our_netif(if_desc_str, event->esp_netif)) {
// const esp_netif_ip_info_t *ip_info = &event->ip_info;
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
memcpy((void *)&ip_info, (const void *)&event->ip_info,
sizeof(esp_netif_ip_info_t));
connected = true;
xSemaphoreGive(connIpSemaphoreHandle);
ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
@@ -429,8 +438,6 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info.gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
connected = true;
break;
}
}
@@ -439,11 +446,16 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
/**
*/
bool eth_get_ip(esp_netif_ip_info_t *ip) {
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
if (ip) {
memcpy((void *)ip, (const void *)&ip_info, sizeof(esp_netif_ip_info_t));
}
bool _connected = connected;
return connected;
xSemaphoreGive(connIpSemaphoreHandle);
return _connected;
}
static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base,
@@ -466,6 +478,10 @@ void eth_start(void) {
esp_eth_handle_t *eth_handles;
esp_netif_t *eth_netif;
if (!connIpSemaphoreHandle) {
connIpSemaphoreHandle = xSemaphoreCreateMutex();
}
ESP_ERROR_CHECK(eth_init(&eth_handles, &eth_port_cnt));
#if CONFIG_SNAPCLIENT_USE_INTERNAL_ETHERNET || \

View File

@@ -13,6 +13,9 @@
#include "esp_netif_types.h"
#include "esp_timer.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/semphr.h"
#include "network_interface.h"
#include "nvs_flash.h"
@@ -34,6 +37,7 @@ static esp_netif_t *esp_wifi_netif = NULL;
static esp_netif_ip_info_t ip_info = {{0}, {0}, {0}};
static bool connected = false;
static SemaphoreHandle_t connIpSemaphoreHandle = NULL;
#if ENABLE_WIFI_PROVISIONING
static esp_timer_handle_t resetReasonTimerHandle = NULL;
@@ -79,6 +83,10 @@ static void event_handler(void *arg, esp_event_base_t event_base, int event_id,
} else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_STA_DISCONNECTED) {
if ((s_retry_num < WIFI_MAXIMUM_RETRY) || (WIFI_MAXIMUM_RETRY == 0)) {
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
connected = false;
xSemaphoreGive(connIpSemaphoreHandle);
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
@@ -96,10 +104,13 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
return;
}
// const esp_netif_ip_info_t *ip_info = &event->ip_info;
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
memcpy((void *)&ip_info, (const void *)&event->ip_info,
sizeof(esp_netif_ip_info_t));
connected = true;
xSemaphoreGive(connIpSemaphoreHandle);
ESP_LOGI(TAG, "Wifi Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
@@ -108,8 +119,6 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "WIFIGW:" IPSTR, IP2STR(&ip_info.gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
connected = true;
s_retry_num = 0;
}
@@ -122,27 +131,39 @@ static void lost_ip_event_handler(void *arg, esp_event_base_t event_base,
// const esp_netif_ip_info_t *ip_info = &event->ip_info;
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
memcpy((void *)&ip_info, (const void *)&event->ip_info,
sizeof(esp_netif_ip_info_t));
connected = false;
xSemaphoreGive(connIpSemaphoreHandle);
ESP_LOGI(TAG, "Wifi Lost IP Address");
}
/**
*/
bool wifi_get_ip(esp_netif_ip_info_t *ip) {
xSemaphoreTake(connIpSemaphoreHandle, portMAX_DELAY);
if (ip) {
memcpy((void *)ip, (const void *)&ip_info, sizeof(esp_netif_ip_info_t));
}
bool _connected = connected;
return connected;
xSemaphoreGive(connIpSemaphoreHandle);
return _connected;
}
/**
*/
void wifi_start(void) {
if (!connIpSemaphoreHandle) {
connIpSemaphoreHandle = xSemaphoreCreateMutex();
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

View File

@@ -30,8 +30,6 @@
#include "eth_interface.h"
#endif
#include "nvs_flash.h"
// #include "wifi_interface.h"
#include "board.h"
#include "es8388.h"
#include "esp_netif.h"
@@ -44,6 +42,7 @@
#include "mdns.h"
#include "net_functions.h"
#include "network_interface.h"
#include "nvs_flash.h"
// Web socket server
// #include "websocket_if.h"