Skip to content

Commit

Permalink
UDP target can manually be set via API, MAC is reported via API
Browse files Browse the repository at this point in the history
#70 Added MAC response & custom UDP target
Updated sdkconfig to match new dependencies
  • Loading branch information
seeul8er committed Apr 23, 2024
1 parent 0d293ec commit 25fb68e
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 40 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ http://dronebridge.local/api/system/reboot
to
```http request
http://dronebridge.local/api/settings/change
```

**Manually add a UDP connection target:** Send a valid JSON
```json
{
"ip": "XXX.XXX.XXX.XXX",
"port": 452
}
```
to
```http request
http://dronebridge.local/api/system/addudp
```

### Testing
Expand Down
1 change: 1 addition & 0 deletions main/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern int DB_UART_BAUD_RATE;
extern uint16_t DB_TRANS_BUF_SIZE;
extern uint8_t DB_LTM_FRAME_NUM_BUFFER; // Number of LTM frames per UDP packet (min = 1; max = 5)
extern int station_rssi; // updated when ESP32 is in station mode and connected to an access point
extern uint8_t LOCAL_MAC_ADDRESS[6]; // filled with the mac address during init of Wifi interface

extern uint32_t uart_byte_count; // Total bytes read from UART
extern int8_t num_connected_tcp_clients;
Expand Down
13 changes: 9 additions & 4 deletions main/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ static esp_err_t settings_change_post_handler(httpd_req_t *req) {
}

/**
* Process incoming UDP connection add request. Only one IPv4 connection can be added
* Process incoming UDP connection add request. Only one IPv4 connection can be added at a time
* Expecting JSON in the form of:
* {
* "ip": "XXX.XXX.XXX.XXX",
* "port": 452
* }
* }
* @param req
* @return
*/
Expand Down Expand Up @@ -261,7 +261,8 @@ static esp_err_t udp_conn_add_post_handler(httpd_req_t *req) {
int new_udp_port = 0;
char new_ip[IP4ADDR_STRLEN_MAX];
cJSON *json = cJSON_GetObjectItem(root, "ip");
if (json) strncpy(new_ip, json->valuestring, IP4ADDR_STRLEN_MAX);
if (json) strncpy(new_ip, json->valuestring, sizeof(new_ip));
new_ip[IP4ADDR_STRLEN_MAX-1] = '\0'; // to remove warning and to be sure
json = cJSON_GetObjectItem(root, "port");
if (json) new_udp_port = json->valueint;

Expand All @@ -270,11 +271,11 @@ static esp_err_t udp_conn_add_post_handler(httpd_req_t *req) {
memset(&new_sockaddr, 0, sizeof(new_sockaddr));
new_sockaddr.sin_family = AF_INET;
inet_pton(AF_INET, new_ip, &new_sockaddr.sin_addr);
new_sockaddr.sin_port = htons(new_udp_port);
struct db_udp_client_t new_udp_client = {
.udp_client = new_sockaddr,
.mac = {0, 0, 0, 0, 0, 0} // dummy MAC
};
new_sockaddr.sin_port = htons(new_udp_port);
// udp_conn_list is initialized as the very first thing during startup - we expect it to be there
bool success = add_to_known_udp_clients(udp_conn_list, new_udp_client);

Expand Down Expand Up @@ -309,6 +310,10 @@ static esp_err_t system_info_get_handler(httpd_req_t *req) {
cJSON_AddNumberToObject(root, "db_build_version", BUILDVERSION);
cJSON_AddNumberToObject(root, "major_version", MAJOR_VERSION);
cJSON_AddNumberToObject(root, "minor_version", MINOR_VERSION);
char mac_str[18];
sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X",
LOCAL_MAC_ADDRESS[0], LOCAL_MAC_ADDRESS[1], LOCAL_MAC_ADDRESS[2], LOCAL_MAC_ADDRESS[3], LOCAL_MAC_ADDRESS[4], LOCAL_MAC_ADDRESS[5]);
cJSON_AddStringToObject(root, "mac", mac_str);
const char *sys_info = cJSON_Print(root);
httpd_resp_sendstr(req, sys_info);
free((void *) sys_info);
Expand Down
4 changes: 4 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ int32_t DB_UART_BAUD_RATE = 57600;
uint16_t DB_TRANS_BUF_SIZE = 64;
uint8_t DB_LTM_FRAME_NUM_BUFFER = 2;
int station_rssi = 0;
uint8_t LOCAL_MAC_ADDRESS[6];

struct udp_conn_list_t *udp_conn_list;

Expand Down Expand Up @@ -275,6 +276,7 @@ void init_wifi_apmode(int wifi_mode) {

ESP_ERROR_CHECK(esp_netif_set_hostname(esp_default_netif, "DBESP32"));
strncpy(CURRENT_CLIENT_IP, DEFAULT_AP_IP, sizeof(CURRENT_CLIENT_IP));
ESP_ERROR_CHECK(esp_read_mac(LOCAL_MAC_ADDRESS, ESP_MAC_WIFI_SOFTAP));
}

/**
Expand Down Expand Up @@ -342,6 +344,7 @@ int init_wifi_clientmode() {
return -1;
}
ESP_LOGI(TAG, "WiFi client mode enabled and connected!");
ESP_ERROR_CHECK(esp_read_mac(LOCAL_MAC_ADDRESS, ESP_MAC_WIFI_STA));
return 0;
}

Expand All @@ -364,6 +367,7 @@ void init_wifi_espnow() {
ESP_ERROR_CHECK(esp_wifi_set_channel(DB_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE));
ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_LR) );
ESP_LOGI(TAG, "Enabled ESP-NOW WiFi Mode! LR Mode is set. This device will be invisible to non-ESP32 devices!");
ESP_ERROR_CHECK(esp_read_mac(LOCAL_MAC_ADDRESS, ESP_MAC_WIFI_STA));
}

/**
Expand Down
46 changes: 19 additions & 27 deletions sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ CONFIG_WEB_MOUNT_POINT="/www"
#
# Compiler options
#
CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y
# CONFIG_COMPILER_OPTIMIZATION_DEFAULT is not set
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
# CONFIG_COMPILER_OPTIMIZATION_PERF is not set
CONFIG_COMPILER_OPTIMIZATION_PERF=y
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
Expand Down Expand Up @@ -579,13 +579,8 @@ CONFIG_ESP_ERR_TO_NAME_LOOKUP=y
#
# Ethernet
#
CONFIG_ETH_ENABLED=y
CONFIG_ETH_USE_SPI_ETHERNET=y
# CONFIG_ETH_SPI_ETHERNET_DM9051 is not set
# CONFIG_ETH_SPI_ETHERNET_W5500 is not set
# CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL is not set
# CONFIG_ETH_USE_SPI_ETHERNET is not set
# CONFIG_ETH_USE_OPENETH is not set
# CONFIG_ETH_TRANSMIT_MUTEX is not set
# end of Ethernet

#
Expand Down Expand Up @@ -1011,7 +1006,6 @@ CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1
#
# Port
#
CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set
CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y
# CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set
Expand Down Expand Up @@ -1368,11 +1362,10 @@ CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI=y
#
# ESP-MQTT Configurations
#
CONFIG_MQTT_PROTOCOL_311=y
# CONFIG_MQTT_PROTOCOL_311 is not set
# CONFIG_MQTT_PROTOCOL_5 is not set
CONFIG_MQTT_TRANSPORT_SSL=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y
# CONFIG_MQTT_TRANSPORT_SSL is not set
# CONFIG_MQTT_TRANSPORT_WEBSOCKET is not set
# CONFIG_MQTT_MSG_ID_INCREMENTAL is not set
# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set
# CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set
Expand Down Expand Up @@ -1675,6 +1668,17 @@ CONFIG_ESPNOW_DEBUG_CONSOLE_UART_NUM=0
# end of ESP-NOW Debug Configuration
# end of ESP-NOW Configuration

#
# CMake Utilities
#
# CONFIG_CU_RELINKER_ENABLE is not set
# CONFIG_CU_DIAGNOSTICS_COLOR_NEVER is not set
CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS=y
# CONFIG_CU_DIAGNOSTICS_COLOR_AUTO is not set
# CONFIG_CU_GCC_LTO_ENABLE is not set
# CONFIG_CU_GCC_STRING_1BYTE_ALIGN is not set
# end of CMake Utilities

#
# IoT Button
#
Expand All @@ -1690,17 +1694,6 @@ CONFIG_ADC_BUTTON_MAX_BUTTON_PER_CHANNEL=8
CONFIG_ADC_BUTTON_SAMPLE_TIMES=1
# end of IoT Button

#
# CMake Utilities
#
# CONFIG_CU_RELINKER_ENABLE is not set
# CONFIG_CU_DIAGNOSTICS_COLOR_NEVER is not set
CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS=y
# CONFIG_CU_DIAGNOSTICS_COLOR_AUTO is not set
# CONFIG_CU_GCC_LTO_ENABLE is not set
# CONFIG_CU_GCC_STRING_1BYTE_ALIGN is not set
# end of CMake Utilities

#
# mDNS
#
Expand All @@ -1726,7 +1719,6 @@ CONFIG_MDNS_MULTIPLE_INSTANCE=y
#
CONFIG_MDNS_PREDEF_NETIF_STA=y
CONFIG_MDNS_PREDEF_NETIF_AP=y
CONFIG_MDNS_PREDEF_NETIF_ETH=y
# end of MDNS Predefined interfaces
# end of mDNS
# end of Component config
Expand All @@ -1750,8 +1742,8 @@ CONFIG_LOG_BOOTLOADER_LEVEL=3
CONFIG_FLASHMODE_DIO=y
# CONFIG_FLASHMODE_DOUT is not set
CONFIG_MONITOR_BAUD=115200
CONFIG_OPTIMIZATION_LEVEL_DEBUG=y
CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y
# CONFIG_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
Expand Down
113 changes: 108 additions & 5 deletions sdkconfig_c3
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT=y
CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA=y
# CONFIG_ESP_WIFI_SLP_IRAM_OPT is not set
# CONFIG_ESP_WIFI_FTM_ENABLE is not set
CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE=y
# CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is not set
# CONFIG_ESP_WIFI_GCMP_SUPPORT is not set
# CONFIG_ESP_WIFI_GMAC_SUPPORT is not set
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y
Expand Down Expand Up @@ -1358,11 +1358,10 @@ CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI=y
#
# ESP-MQTT Configurations
#
CONFIG_MQTT_PROTOCOL_311=y
# CONFIG_MQTT_PROTOCOL_311 is not set
# CONFIG_MQTT_PROTOCOL_5 is not set
CONFIG_MQTT_TRANSPORT_SSL=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y
# CONFIG_MQTT_TRANSPORT_SSL is not set
# CONFIG_MQTT_TRANSPORT_WEBSOCKET is not set
# CONFIG_MQTT_MSG_ID_INCREMENTAL is not set
# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set
# CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set
Expand Down Expand Up @@ -1588,6 +1587,109 @@ CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y
# CONFIG_WIFI_PROV_STA_FAST_SCAN is not set
# end of Wi-Fi Provisioning Manager

#
# CMake Utilities
#
# CONFIG_CU_RELINKER_ENABLE is not set
# CONFIG_CU_DIAGNOSTICS_COLOR_NEVER is not set
CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS=y
# CONFIG_CU_DIAGNOSTICS_COLOR_AUTO is not set
# CONFIG_CU_GCC_LTO_ENABLE is not set
# CONFIG_CU_GCC_STRING_1BYTE_ALIGN is not set
# end of CMake Utilities

#
# ESP-NOW Configuration
#

#
# ESP-NOW Security Configuration
#
CONFIG_ESPNOW_APP_SECURITY=y
# CONFIG_ESPNOW_ALL_SECURITY is not set
# CONFIG_ESPNOW_CONTROL_SECURITY is not set
# CONFIG_ESPNOW_DEBUG_SECURITY is not set
# CONFIG_ESPNOW_OTA_SECURITY is not set
# CONFIG_ESPNOW_PROV_SECURITY is not set
# end of ESP-NOW Security Configuration

#
# ESP-NOW Light Sleep Configuration
#
# CONFIG_ESPNOW_LIGHT_SLEEP is not set
# end of ESP-NOW Light Sleep Configuration

#
# ESP-NOW Control Configuration
#
# CONFIG_ESPNOW_CONTROL_AUTO_CHANNEL_SENDING is not set
# end of ESP-NOW Control Configuration

#
# ESP-NOW OTA configuration
#
CONFIG_ESPNOW_OTA_RETRANSMISSION_TIMES=10
CONFIG_ESPNOW_OTA_RETRY_COUNT=50
CONFIG_ESPNOW_OTA_SEND_FORWARD_TTL=1
CONFIG_ESPNOW_OTA_SEND_FORWARD_RSSI=-65
CONFIG_ESPNOW_OTA_WAIT_RESPONSE_TIMEOUT=10000
# end of ESP-NOW OTA configuration

# CONFIG_ESPNOW_AUTO_RESTORE_CHANNEL is not set
# CONFIG_ESPNOW_DATA_FAST_ACK is not set

#
# ESP-NOW Utils Configuration
#
CONFIG_ESPNOW_MEM_ALLOCATION_DEFAULT=y
CONFIG_ESPNOW_MEM_DEBUG=y
CONFIG_ESPNOW_MEM_DBG_INFO_MAX=128
CONFIG_ESPNOW_NVS_NAMESPACE="espnow"
CONFIG_ESPNOW_REBOOT_UNBROKEN_INTERVAL_TIMEOUT=5000
CONFIG_ESPNOW_REBOOT_UNBROKEN_FALLBACK_COUNT=30
# end of ESP-NOW Utils Configuration

#
# ESP-NOW Debug Configuration
#

#
# Debug console Configuration
#
CONFIG_ESPNOW_STORE_HISTORY=y
# end of Debug console Configuration

#
# Debug Log Configuration
#
CONFIG_ESPNOW_DEBUG_LOG_PARTITION_LABEL_DATA="log_info"
CONFIG_ESPNOW_DEBUG_LOG_PARTITION_LABEL_NVS="log_status"
CONFIG_ESPNOW_DEBUG_LOG_FILE_MAX_SIZE=65536
CONFIG_ESPNOW_DEBUG_LOG_PARTITION_OFFSET=0
# CONFIG_ESPNOW_DEBUG_LOG_PRINTF_ENABLE is not set
# end of Debug Log Configuration

CONFIG_ESPNOW_DEBUG_CONSOLE_UART_NUM_0=y
# CONFIG_ESPNOW_DEBUG_CONSOLE_UART_NUM_1 is not set
CONFIG_ESPNOW_DEBUG_CONSOLE_UART_NUM=0
# end of ESP-NOW Debug Configuration
# end of ESP-NOW Configuration

#
# IoT Button
#
CONFIG_BUTTON_PERIOD_TIME_MS=5
CONFIG_BUTTON_DEBOUNCE_TICKS=2
CONFIG_BUTTON_SHORT_PRESS_TIME_MS=180
CONFIG_BUTTON_LONG_PRESS_TIME_MS=1500
CONFIG_BUTTON_LONG_PRESS_TOLERANCE_MS=20
CONFIG_BUTTON_SERIAL_TIME_MS=20
# CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE is not set
CONFIG_ADC_BUTTON_MAX_CHANNEL=3
CONFIG_ADC_BUTTON_MAX_BUTTON_PER_CHANNEL=8
CONFIG_ADC_BUTTON_SAMPLE_TIMES=1
# end of IoT Button

#
# mDNS
#
Expand All @@ -1604,6 +1706,7 @@ CONFIG_MDNS_TIMER_PERIOD_MS=100
# CONFIG_MDNS_NETWORKING_SOCKET is not set
# CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES is not set
# CONFIG_MDNS_ENABLE_DEBUG_PRINTS is not set
CONFIG_MDNS_ENABLE_CONSOLE_CLI=y
# CONFIG_MDNS_RESPOND_REVERSE_QUERIES is not set
CONFIG_MDNS_MULTIPLE_INSTANCE=y

Expand Down
Loading

0 comments on commit 25fb68e

Please sign in to comment.