Skip to content

Commit

Permalink
Fixed client mode reconnection bug
Browse files Browse the repository at this point in the history
Removed fd for serial socket since no use case
Added more logs
  • Loading branch information
seeul8er committed Jan 30, 2024
1 parent f2ed465 commit 51ceae8
Show file tree
Hide file tree
Showing 27 changed files with 106 additions and 75 deletions.
4 changes: 2 additions & 2 deletions dependencies.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
dependencies:
espressif/mdns:
component_hash: 810ec139689ae93bf42520d05de4855fbb68f7140ef67797d91d8d61829589cb
component_hash: 090826401d30f756037f91d91068f470ba8d0ef04fe507d3aa8761305cecd7ca
source:
service_url: https://api.components.espressif.com/
type: service
version: 1.2.2
version: 1.2.3
idf:
component_hash: null
source:
Expand Down
24 changes: 12 additions & 12 deletions main/db_esp32_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ uint32_t uart_byte_count = 0;
int8_t num_connected_tcp_clients = 0;
int8_t num_connected_udp_clients = 0;

int open_serial_socket() {
int serial_socket;
esp_err_t open_serial_socket() {
uart_config_t uart_config = {
.baud_rate = DB_UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
Expand All @@ -57,15 +56,14 @@ int open_serial_socket() {
};
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, DB_UART_PIN_TX, DB_UART_PIN_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, 1024, 0, 0, NULL, 0));
if ((serial_socket = open("/dev/uart/2", O_RDWR)) == -1) {
ESP_LOGE(TAG, "Cannot open UART2");
close(serial_socket);
uart_driver_delete(UART_NUM);
return ESP_FAIL;
}
esp_vfs_dev_uart_use_driver(2);
return serial_socket;
// if ((serial_socket = open("/dev/uart/1", O_RDWR)) == -1) {
// ESP_LOGE(TAG, "Cannot open UART1");
// close(serial_socket);
// uart_driver_delete(UART_NUM);
// return ESP_FAIL;
// }
// esp_vfs_dev_uart_use_driver(UART_NUM);
return uart_driver_install(UART_NUM, 1024, 0, 0, NULL, 0);
}

int open_udp_socket() {
Expand Down Expand Up @@ -164,7 +162,7 @@ void parse_msp_ltm(int tcp_clients[], struct db_udp_connection_t *udp_conn, uint


/**
* Reads one byte from UART and checks if we already got enough bytes to send them out
* Reads TRANS_RD_BYTES_NUM bytes from UART and checks if we already got enough bytes to send them out
*
* @param tcp_clients Array of connected TCP clients
* @param serial_read_bytes Number of bytes already read for the current packet
Expand Down Expand Up @@ -267,13 +265,15 @@ void remove_udp_from_known_clients(struct db_udp_connection_t *connections, stru
* Thread that manages all incoming and outgoing TCP, UDP and serial (UART) connections
*/
void control_module_udp_tcp() {
ESP_LOGI(TAG, "Starting control module");
// only open serial socket/UART if PINs are not matching - matching PIN nums mean they still need to be defined by
// the user no pre-defined pins as of this release since ESP32 boards have wildly different pin configurations
int uart_socket = ESP_FAIL;
if (DB_UART_PIN_RX != DB_UART_PIN_TX) {
uart_socket = open_serial_socket();
} else {
// do no long continue setting up the system and kill task
ESP_LOGW(TAG, "Init of control module aborted. TX GPIO == RX GPIO - Configure first!");
vTaskDelete(NULL);
}
int tcp_master_socket = open_tcp_server(app_port_proxy);
Expand Down
44 changes: 25 additions & 19 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e
// Wifi access point mode events
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
ESP_LOGI(TAG, "Client connected - station:"MACSTR", AID=%d", MAC2STR(event->mac), event->aid);
ESP_LOGI(TAG, "WIFI_EVENT - Client connected - station:"MACSTR", 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, "Client disconnected - station:"MACSTR", AID=%d", MAC2STR(event->mac), event->aid);
ESP_LOGI(TAG, "WIFI_EVENT - Client disconnected - station:"MACSTR", AID=%d", MAC2STR(event->mac), event->aid);
struct db_udp_client_t db_udp_client;
memcpy(db_udp_client.mac, event->mac, sizeof(db_udp_client.mac));
remove_udp_from_known_clients(&udp_conn, db_udp_client);
} else if (event_id == WIFI_EVENT_AP_START) {
ESP_LOGI(TAG, "AP started! (SSID: %s PASS: %s)", DEFAULT_SSID, DEFAULT_PWD);
ESP_LOGI(TAG, "WIFI_EVENT - AP started! (SSID: %s PASS: %s)", DEFAULT_SSID, DEFAULT_PWD);
} else if (event_id == WIFI_EVENT_AP_STOP) {
ESP_LOGI(TAG, "AP stopped!");
ESP_LOGI(TAG, "WIFI_EVENT - AP stopped!");
} else if(event_base == IP_EVENT && event_id == IP_EVENT_AP_STAIPASSIGNED){
ip_event_ap_staipassigned_t* event = (ip_event_ap_staipassigned_t*) event_data;
ESP_LOGI(TAG, "New station IP:" IPSTR, IP2STR(&event->ip));
ESP_LOGI(TAG, "MAC: " MACSTR, MAC2STR(event->mac));
ESP_LOGI(TAG, "WIFI_EVENT - New station IP:" IPSTR, IP2STR(&event->ip));
ESP_LOGI(TAG, "WIFI_EVENT - MAC: " MACSTR, MAC2STR(event->mac));
struct db_udp_client_t db_udp_client;
db_udp_client.udp_client.sin_family = PF_INET;
db_udp_client.udp_client.sin_port = htons(APP_PORT_PROXY_UDP);
Expand All @@ -109,19 +109,21 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e
}
// Wifi client mode events
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
ESP_LOGI(TAG, "WIFI_EVENT - Wifi Started");
ESP_ERROR_CHECK(esp_wifi_connect());
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_LOGI(TAG, "WIFI_EVENT - Lost connection to access point");
if (s_retry_num < WIFI_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
ESP_ERROR_CHECK(esp_wifi_connect());
s_retry_num++;
ESP_LOGI(TAG, "Retry to connect to the AP (%i/%i)", s_retry_num, WIFI_ESP_MAXIMUM_RETRY);
} else {
ESP_LOGI(TAG,"Connecting to the AP failed");
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"Connecting to the AP failed");
} 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;
ESP_LOGI(TAG, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip));
ESP_LOGI(TAG, "WIFI_EVENT - Got IP:" IPSTR, IP2STR(&event->ip_info.ip));
sprintf(CURRENT_CLIENT_IP, IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
Expand All @@ -142,6 +144,7 @@ void start_mdns_service() {
ESP_ERROR_CHECK(mdns_service_add(NULL, "_db_proxy", "_tcp", APP_PORT_PROXY, NULL, 0));
ESP_ERROR_CHECK(mdns_service_add(NULL, "_db_comm", "_tcp", APP_PORT_COMM, NULL, 0));
ESP_ERROR_CHECK(mdns_service_instance_name_set("_http", "_tcp", "DroneBridge for ESP32"));
ESP_LOGI(TAG, "MDNS Service started!");
}

#if CONFIG_WEB_DEPLOY_SEMIHOST
Expand Down Expand Up @@ -183,9 +186,9 @@ esp_err_t init_fs(void) {
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
} else {
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
ESP_LOGI(TAG, "Filesystem init finished! Partition size: total: %d bytes, used: %d bytes (%i%%)", total, used, (used*100)/total);
}
return ESP_OK;
return ret;
}

#endif
Expand Down Expand Up @@ -218,7 +221,7 @@ void init_wifi_apmode(int wifi_mode) {
.ap = {
.ssid = "DroneBridge_ESP32_Init",
.ssid_len = 0,
.authmode = WIFI_AUTH_WPA_PSK,
.authmode = WIFI_AUTH_WPA2_PSK,
.channel = DEFAULT_CHANNEL,
.ssid_hidden = 0,
.beacon_interval = 100,
Expand Down Expand Up @@ -254,7 +257,7 @@ void init_wifi_apmode(int wifi_mode) {
}

/**
* Initializes the ESP Wifi client mode where we connect to a known access point.
* Initializes the ESP Wifi client mode where we connect to a known access point.
*/
int init_wifi_clientmode() {
s_wifi_event_group = xEventGroupCreate();
Expand All @@ -279,7 +282,8 @@ int init_wifi_clientmode() {
wifi_config_t wifi_config = {
.sta = {
.ssid = "DroneBridge_ESP32_Init",
.password = "dronebridge"
.password = "dronebridge",
.threshold.authmode = WIFI_AUTH_WPA_PSK
},
};
strncpy((char *)wifi_config.sta.ssid, (char *)DEFAULT_SSID, 32);
Expand Down Expand Up @@ -311,14 +315,16 @@ int init_wifi_clientmode() {
} else {
ESP_LOGE(TAG, "UNEXPECTED WIFI EVENT");
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
// /* The event will not be processed after unregister */
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
// vEventGroupDelete(s_wifi_event_group);

if (enable_temp_ap_mode) {
ESP_LOGW(TAG, "WiFi client mode was not able to connect to the specified access point");
return -1;
}
ESP_LOGI(TAG, "WiFi client mode enabled and connected!");
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion managed_components/espressif__mdns/.component_hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
810ec139689ae93bf42520d05de4855fbb68f7140ef67797d91d8d61829589cb
090826401d30f756037f91d91068f470ba8d0ef04fe507d3aa8761305cecd7ca
2 changes: 1 addition & 1 deletion managed_components/espressif__mdns/.cz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ commitizen:
bump_message: 'bump(mdns): $current_version -> $new_version'
pre_bump_hooks: python ../../ci/changelog.py mdns
tag_format: mdns-v$version
version: 1.2.2
version: 1.2.3
version_files:
- idf_component.yml
11 changes: 11 additions & 0 deletions managed_components/espressif__mdns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [1.2.3](https://github.com/espressif/esp-protocols/commits/mdns-v1.2.3)

### Bug Fixes

- fixed CI issues for host and afl tests ([4be5efc84e](https://github.com/espressif/esp-protocols/commit/4be5efc84e))
- fix copy delegated host addr ([19fb36000c](https://github.com/espressif/esp-protocols/commit/19fb36000c))
- enable CONFIG_ESP_WIFI_ENABLED if CONFIG_SOC_WIFI_SUPPORTED is also enabled (for ESP-IDF <= 5.1) ([d20a718320](https://github.com/espressif/esp-protocols/commit/d20a718320))
- remove protocol_examples_common ([1ee9dae6bf](https://github.com/espressif/esp-protocols/commit/1ee9dae6bf))
- move the example into a subdirectory ([d28232b9f8](https://github.com/espressif/esp-protocols/commit/d28232b9f8))
- reference protocol_examples_common from IDF ([c83b76ea8f](https://github.com/espressif/esp-protocols/commit/c83b76ea8f))

## [1.2.2](https://github.com/espressif/esp-protocols/commits/mdns-v1.2.2)

### Bug Fixes
Expand Down
10 changes: 0 additions & 10 deletions managed_components/espressif__mdns/examples/CMakeLists.txt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(query_advertise)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mDNS example

Shows how to use mDNS to advertise lookup services and hosts
Shows how to use mDNS to advertise and query services and hosts

## Example workflow

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies:
## Required IDF version
idf: ">=5.0"
espressif/mdns:
version: "^1.0.0"
override_path: "../../../"
protocol_examples_common:
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
2 changes: 1 addition & 1 deletion managed_components/espressif__mdns/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ dependencies:
version: '>=5.0'
description: mDNS
url: https://github.com/espressif/esp-protocols/tree/master/components/mdns
version: 1.2.2
version: 1.2.3
4 changes: 2 additions & 2 deletions managed_components/espressif__mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
#endif

#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 1, 0)
// IDF <= v5.1 does not support enabling/disabling esp-wifi
#define MDNS_ESP_WIFI_ENABLED 1
#define MDNS_ESP_WIFI_ENABLED CONFIG_SOC_WIFI_SUPPORTED
#else
#define MDNS_ESP_WIFI_ENABLED CONFIG_ESP_WIFI_ENABLED
#endif
Expand Down Expand Up @@ -5883,6 +5882,7 @@ static mdns_ip_addr_t *_copy_delegated_host_address_list(char *hostname)
if (strcasecmp(host->hostname, hostname) == 0) {
return copy_address_list(host->address_list);
}
host = host->next;
}
return NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ CFLAGS=-g -Wno-unused-value -Wno-missing-declarations -Wno-pointer-bool-conversi
-I$(COMPONENTS_DIR)/heap/include \
-I$(COMPONENTS_DIR)/log/include \
-I$(COMPONENTS_DIR)/lwip/lwip/src/include \
-I$(COMPONENTS_DIR)/linux/include \
-I$(COMPONENTS_DIR)/lwip/port/esp32/include \
-I$(COMPONENTS_DIR)/lwip/lwip/src/include/lwip/apps \
-I$(COMPONENTS_DIR)/newlib/platform_include \
-I$(COMPONENTS_DIR)/soc/include \
-I$(COMPONENTS_DIR)/soc/include \
-I$(COMPONENTS_DIR)/soc/esp32/include \
-I$(COMPONENTS_DIR)/soc/src/esp32/include \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set(EXTRA_COMPONENT_DIRS ../../ ../../../../common_components/protocol_examples_common)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

project(mdns_test_app)
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,17 @@ menu "Example Configuration"
help
If enabled, a portion of MAC address is added to the hostname, this is used
for evaluation of tests in CI
config MDNS_ADD_MAC_TO_HOSTNAME
bool "Add mac suffix to hostname"
default n
help
If enabled, a portion of MAC address is added to the hostname, this is used
for evaluation of tests in CI
config MDNS_PUBLISH_DELEGATE_HOST
bool "Publish a delegated host"
help
Enable publishing a delegated host other than ESP32.
The example will also add a mock service for this host.


endmenu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies:
## Required IDF version
idf: ">=5.0"
espressif/mdns:
version: "^1.0.0"
override_path: "../../../"
protocol_examples_common:
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_MDNS_RESOLVE_TEST_SERVICES=y
CONFIG_MDNS_ADD_MAC_TO_HOSTNAME=y
CONFIG_MDNS_PUBLISH_DELEGATE_HOST=y
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
Expand All @@ -12,4 +11,3 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
CONFIG_EXAMPLE_ETH_PHY_ADDR=1
CONFIG_EXAMPLE_CONNECT_IPV6=y
CONFIG_MDNS_BUTTON_GPIO=32
Loading

0 comments on commit 51ceae8

Please sign in to comment.