Merge branch 'feature/simplify_ot_cli_config' into 'master'

openthread: simplify ot_cli example configuration

See merge request espressif/esp-idf!14410
This commit is contained in:
Shu Chen
2021-07-20 04:19:40 +00:00
11 changed files with 37 additions and 45 deletions

View File

@@ -7,4 +7,4 @@ cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(otbr_esp) project(esp_ot_br)

View File

@@ -3,7 +3,7 @@
# project subdirectory. # project subdirectory.
# #
PROJECT_NAME := otbr_esp PROJECT_NAME := esp_ot_br
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common

View File

@@ -3,6 +3,6 @@
# project subdirectory. # project subdirectory.
# #
PROJECT_NAME := ot_esp_cli PROJECT_NAME := esp_ot_cli
include $(IDF_PATH)/make/project.mk include $(IDF_PATH)/make/project.mk

View File

@@ -1,2 +1,8 @@
idf_component_register(SRCS "esp_ot_udp_socket.c" "esp_ot_tcp_socket.c" "esp_ot_cli_extension.c" "esp_ot_cli.c" set(srcs "esp_ot_cli.c")
if(CONFIG_OPENTHREAD_CLI_ESP_EXTENSION)
list(APPEND srcs "esp_ot_cli_extension.c" "esp_ot_tcp_socket.c" "esp_ot_udp_socket.c")
endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

View File

@@ -1,30 +1,22 @@
menu "Openthread" menu "OpenThread CLI Example"
menuconfig OPENTHREAD_CUSTOM_COMMAND config OPENTHREAD_CLI_ESP_EXTENSION
bool "Enable custom command in ot-cli" bool "Enable Espressif's extended features"
default n default y
help
Enable Espressif's extended features include TCP socket, UDP socket.
config OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE config OPENTHREAD_CLI_TCP_SERVER_PORT
bool "Enable openthread tcp socket"
depends on OPENTHREAD_CUSTOM_COMMAND
default n
config OPENTHEAD_EXAMPLE_TCP_SERVER_PORT
int "the port of TCP server" int "the port of TCP server"
default 12345 default 12345
depends on OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE depends on OPENTHREAD_CLI_ESP_EXTENSION
help help
Set the connect port of socket Set the connect port of socket
config OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE config OPENTHREAD_CLI_UDP_SERVER_PORT
bool "Enable openthread udp socket"
depends on OPENTHREAD_CUSTOM_COMMAND
default n
config OPENTHEAD_EXAMPLE_UDP_SERVER_PORT
int "the port of UDP server" int "the port of UDP server"
default 54321 default 54321
depends on OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE depends on OPENTHREAD_CLI_ESP_EXTENSION
help help
Set the connect port of socket Set the connect port of socket

View File

@@ -35,14 +35,15 @@
#include "openthread/instance.h" #include "openthread/instance.h"
#include "openthread/tasklet.h" #include "openthread/tasklet.h"
#if CONFIG_OPENTHREAD_CUSTOM_COMMAND #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
#include "esp_ot_cli_extension.h" #include "esp_ot_cli_extension.h"
#endif // CONFIG_OPENTHREAD_CUSTOM_COMMAND #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
#define TAG "ot_esp_cli" #define TAG "ot_esp_cli"
extern void otAppCliInit(otInstance *instance); extern void otAppCliInit(otInstance *aInstance);
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config) static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config)
{ {
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD(); esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
@@ -52,6 +53,7 @@ static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t
return netif; return netif;
} }
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
static void ot_task_worker(void *aContext) static void ot_task_worker(void *aContext)
{ {
@@ -60,7 +62,6 @@ static void ot_task_worker(void *aContext)
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
}; };
esp_netif_t *openthread_netif;
// Initialize the OpenThread stack // Initialize the OpenThread stack
ESP_ERROR_CHECK(esp_openthread_init(&config)); ESP_ERROR_CHECK(esp_openthread_init(&config));
@@ -68,19 +69,23 @@ static void ot_task_worker(void *aContext)
// Initialize the OpenThread cli // Initialize the OpenThread cli
otAppCliInit(esp_openthread_get_instance()); otAppCliInit(esp_openthread_get_instance());
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
esp_netif_t *openthread_netif;
// Initialize the esp_netif bindings // Initialize the esp_netif bindings
openthread_netif = init_openthread_netif(&config); openthread_netif = init_openthread_netif(&config);
#if CONFIG_OPENTHREAD_CUSTOM_COMMAND
esp_cli_custom_command_init(); esp_cli_custom_command_init();
#endif // CONFIG_OPENTHREAD_CUSTOM_COMMAND #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
// Run the main loop // Run the main loop
esp_openthread_launch_mainloop(); esp_openthread_launch_mainloop();
// Clean up // Clean up
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
esp_netif_destroy(openthread_netif); esp_netif_destroy(openthread_netif);
esp_openthread_netif_glue_deinit(); esp_openthread_netif_glue_deinit();
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
esp_vfs_eventfd_unregister(); esp_vfs_eventfd_unregister();
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@@ -96,7 +101,9 @@ void app_main(void)
}; };
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config)); ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL); xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
} }

View File

@@ -22,14 +22,10 @@
#include "openthread/cli.h" #include "openthread/cli.h"
static const otCliCommand kCommands[] = { static const otCliCommand kCommands[] = {
#if CONFIG_OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE
{"tcpsockserver", esp_ot_process_tcp_server}, {"tcpsockserver", esp_ot_process_tcp_server},
{"tcpsockclient", esp_ot_process_tcp_client}, {"tcpsockclient", esp_ot_process_tcp_client},
#endif // CONFIG_OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE
#if CONFIG_OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE
{"udpsockserver", esp_ot_process_udp_server}, {"udpsockserver", esp_ot_process_udp_server},
{"udpsockclient", esp_ot_process_udp_client}, {"udpsockclient", esp_ot_process_udp_client},
#endif // CONFIG_OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE
}; };
void esp_cli_custom_command_init() void esp_cli_custom_command_init()

View File

@@ -19,20 +19,18 @@
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#if CONFIG_OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE
#define TAG "ot_socket" #define TAG "ot_socket"
static void tcp_socket_server_task(void *pvParameters) static void tcp_socket_server_task(void *pvParameters)
{ {
char addr_str[128]; char addr_str[128];
char payload[] = "This message is from server\n";
char rx_buffer[128]; char rx_buffer[128];
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
int err = 0; int err = 0;
int len = 0; int len = 0;
int listen_sock; int listen_sock;
int opt = 1; int opt = 1;
int port = CONFIG_OPENTHEAD_EXAMPLE_TCP_SERVER_PORT; int port = CONFIG_OPENTHREAD_CLI_TCP_SERVER_PORT;
int client_sock = 0; int client_sock = 0;
struct timeval timeout; struct timeval timeout;
struct sockaddr_storage source_addr; // Large enough for both IPv6 struct sockaddr_storage source_addr; // Large enough for both IPv6
@@ -108,7 +106,7 @@ static void tcp_socket_client_task(void *pvParameters)
int client_sock; int client_sock;
int err = 0; int err = 0;
int len = 0; int len = 0;
int port = CONFIG_OPENTHEAD_EXAMPLE_TCP_SERVER_PORT; int port = CONFIG_OPENTHREAD_CLI_TCP_SERVER_PORT;
struct sockaddr_in6 dest_addr = { 0 }; struct sockaddr_in6 dest_addr = { 0 };
inet6_aton(host_ip, &dest_addr.sin6_addr); inet6_aton(host_ip, &dest_addr.sin6_addr);
@@ -162,4 +160,3 @@ void esp_ot_process_tcp_client(void *aContext, uint8_t aArgsLength, char *aArgs[
xTaskCreate(tcp_socket_client_task, "ot_tcp_socket_client", 4096, aArgs[0], 4, NULL); xTaskCreate(tcp_socket_client_task, "ot_tcp_socket_client", 4096, aArgs[0], 4, NULL);
} }
} }
#endif // CONFIG_OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE

View File

@@ -18,7 +18,6 @@
extern "C" { extern "C" {
#endif #endif
#if CONFIG_OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE
/** /**
* @brief User command "tcpsockserver" process. * @brief User command "tcpsockserver" process.
* *
@@ -31,7 +30,6 @@ void esp_ot_process_tcp_server(void *aContext, uint8_t aArgsLength, char *aArgs[
*/ */
void esp_ot_process_tcp_client(void *aContext, uint8_t aArgsLength, char *aArgs[]); void esp_ot_process_tcp_client(void *aContext, uint8_t aArgsLength, char *aArgs[]);
#endif // CONFIG_OPENTHREAD_ENABLE_TCP_SOCKET_EXAMPLE
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -19,7 +19,6 @@
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#if CONFIG_OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE
#define TAG "ot_socket" #define TAG "ot_socket"
static void udp_socket_server_task(void *pvParameters) static void udp_socket_server_task(void *pvParameters)
@@ -31,7 +30,7 @@ static void udp_socket_server_task(void *pvParameters)
int err = 0; int err = 0;
int len; int len;
int listen_sock; int listen_sock;
int port = CONFIG_OPENTHEAD_EXAMPLE_UDP_SERVER_PORT; int port = CONFIG_OPENTHREAD_CLI_UDP_SERVER_PORT;
struct timeval timeout; struct timeval timeout;
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
struct sockaddr_in6 listen_addr; struct sockaddr_in6 listen_addr;
@@ -88,7 +87,7 @@ static void udp_socket_client_task(void *pvParameters)
char *host_ip = (char *)pvParameters; char *host_ip = (char *)pvParameters;
char *payload = "This message is from client\n"; char *payload = "This message is from client\n";
int client_sock; int client_sock;
int port = CONFIG_OPENTHEAD_EXAMPLE_UDP_SERVER_PORT; int port = CONFIG_OPENTHREAD_CLI_UDP_SERVER_PORT;
int err = 0; int err = 0;
int len; int len;
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
@@ -142,4 +141,3 @@ void esp_ot_process_udp_client(void *aContext, uint8_t aArgsLength, char *aArgs[
xTaskCreate(udp_socket_client_task, "ot_udp_socket_client", 4096, aArgs[0], 4, NULL); xTaskCreate(udp_socket_client_task, "ot_udp_socket_client", 4096, aArgs[0], 4, NULL);
} }
} }
#endif // CONFIG_OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE

View File

@@ -18,7 +18,6 @@
extern "C" { extern "C" {
#endif #endif
#if CONFIG_OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE
/** /**
* @brief User command "udpsockserver" process. * @brief User command "udpsockserver" process.
* *
@@ -30,7 +29,6 @@ void esp_ot_process_udp_server(void *aContext, uint8_t aArgsLength, char *aArgs[
* *
*/ */
void esp_ot_process_udp_client(void *aContext, uint8_t aArgsLength, char *aArgs[]); void esp_ot_process_udp_client(void *aContext, uint8_t aArgsLength, char *aArgs[]);
#endif // CONFIG_OPENTHREAD_ENABLE_UDP_SOCKET_EXAMPLE
#ifdef __cplusplus #ifdef __cplusplus
} }