Merge branch 'feature/ot-cli-extension-component' into 'master'

openthread: use managed component for extension commands

See merge request espressif/esp-idf!19025
This commit is contained in:
Shu Chen 2022-07-19 11:57:07 +08:00
commit 6ebdeb3e1c
18 changed files with 9 additions and 1013 deletions

View File

@ -1,22 +0,0 @@
set(srcs "src/esp_ot_cli_extension.c")
if(CONFIG_OPENTHREAD_CLI_IPERF)
list(APPEND srcs "src/esp_ot_iperf.c")
endif()
if(CONFIG_OPENTHREAD_CLI_TCP_SOCKET)
list(APPEND srcs "src/esp_ot_tcp_socket.c")
endif()
if(CONFIG_OPENTHREAD_CLI_UDP_SOCKET)
list(APPEND srcs "src/esp_ot_udp_socket.c")
endif()
if(CONFIG_OPENTHREAD_CLI_WIFI)
list(APPEND srcs "src/esp_ot_wifi_cmd.c")
endif()
set(include "include")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "${include}"
REQUIRES lwip openthread iperf)

View File

@ -1,44 +0,0 @@
menu "OpenThread Extension CLI"
menuconfig OPENTHREAD_CLI_ESP_EXTENSION
depends on OPENTHREAD_FTD || OPENTHREAD_MTD
bool "Enable Espressif's extended features"
default y
help
Enable Espressif's extended features.
config OPENTHREAD_CLI_IPERF
bool "Enable iperf command"
depends on OPENTHREAD_CLI_ESP_EXTENSION
default y
config OPENTHREAD_CLI_TCP_SOCKET
bool "Enable TCP socket command"
depends on OPENTHREAD_CLI_ESP_EXTENSION
default y
config OPENTHREAD_CLI_TCP_SERVER_PORT
int "the port of TCP server"
default 12345
depends on OPENTHREAD_CLI_TCP_SOCKET
help
Set the connect port of socket
config OPENTHREAD_CLI_UDP_SOCKET
bool "Enable UDP socket command"
depends on OPENTHREAD_CLI_ESP_EXTENSION
default y
config OPENTHREAD_CLI_UDP_SERVER_PORT
int "the port of UDP server"
default 54321
depends on OPENTHREAD_CLI_UDP_SOCKET
help
Set the connect port of socket
config OPENTHREAD_CLI_WIFI
bool "Enable wifi connection command"
depends on OPENTHREAD_CLI_ESP_EXTENSION && OPENTHREAD_BORDER_ROUTER
default y
endmenu

View File

@ -1,131 +0,0 @@
# Openthread Extension Commands
The ESP OpenThread examples provide a series of extension commands in addition to the standard [OpenThread CLI](https://github.com/openthread/openthread/blob/main/src/cli/README.md).
The extension commands are available in the following examples:
* [ot_cli](../ot_cli)
* [ot_br](../ot_br)
## Enabling the extension commands
To enable OpenThread extension commands, the following Kconfig option needs to be enabled:
`OpenThread Extension CLI` -> `Enable Espressif's extended features`.
## Commands
* [iperf](#iperf)
* [tcpsockclient](#tcpsockclient)
* [tcpsockserver](#tcpsockserver)
* [udpsockclient](#udpsockclient)
* [udpsockserver](#udpsockserver)
* [wifi](#wifi)
### iperf
Iperf is a tool for performing TCP or UDP throughput on the Thread network.
For running iperf, you need to have two Thread devices on the same network.
* General Options
```bash
iperf
---iperf parameter---
-s : server mode, only receive
-u : upd mode
-V : use IPV6 address
-c <addr> : client mode, only transmit
-i <interval> : seconds between periodic bandwidth reports
-t <time> : time in seconds to transmit for (default 10 secs)
-p <port> : server port to listen on/connect to
-l <len_send_buf> : the lenth of send buffer
---example---
create a tcp server : iperf -s -i 3 -p 5001 -t 60
create a udp client : iperf -c <addr> -u -i 3 -t 60 -p 5001 -l 512
Done
```
* Typical usage
For measuring the TCP throughput, first create an iperf service on one node:
```bash
> iperf -V -s -t 20 -i 3 -p 5001
Done
```
Then create an iperf client connecting to the service on another node. Here we use `fdde:ad00:beef:0:a7c6:6311:9c8c:271b` as the example service address.
```bash
> iperf -V -c fdde:ad00:beef:0:a7c6:6311:9c8c:271b -t 20 -i 1 -p 5001 -l 85
Done
Interval Bandwidth
0- 1 sec 0.05 Mbits/sec
1- 2 sec 0.05 Mbits/sec
2- 3 sec 0.05 Mbits/sec
3- 4 sec 0.05 Mbits/sec
4- 5 sec 0.05 Mbits/sec
...
19- 20 sec 0.05 Mbits/sec
0- 20 sec 0.05 Mbits/sec
```
For measuring the UDP throughput, first create an iperf service similarly:
```bash
> iperf -V -u -s -t 20 -i 3 -p 5001
Done
```
Then create an iperf client:
```bash
> iperf -V -u -c fdde:ad00:beef:0:a7c6:6311:9c8c:271b -t 20 -i 1 -p 5001 -l 85
Done
```
### tcpsockserver
Used for creating a tcp server.
```bash
> tcpsockserver
Done
I (1310225) ot_socket: Socket created
I (1310225) ot_socket: Socket bound, port 12345
I (1310225) ot_socket: Socket listening, timeout is 30 seconds
```
### tcpsockclient
Used for creating a tcp client.
```bash
> tcpsockclient fdde:ad00:beef:0:a7c6:6311:9c8c:271b
Done
ot_socket: Socket created, connecting to fdde:ad00:beef:0:a7c6:6311:9c8c:271b:12345
ot_socket: Successfully connected
...
```
### udpsockserver
Used for creating a udp server.
```bash
> udpsockserver
Done
I (1310225) ot_socket: Socket created
I (1310225) ot_socket: Socket bound, port 12345
I (1310225) ot_socket: Socket listening, timeout is 30 seconds
```
### udpsockclient
Used for creating a udp client. Note that the client shall be connected to the same Thread network as the server.
```bash
> udpsockclient fdde:ad00:beef:0:a7c6:6311:9c8c:271b
Done
ot_socket: Socket created, connecting to fdde:ad00:beef:0:a7c6:6311:9c8c:271b:12345
ot_socket: Successfully connected
...
```

View File

@ -1,28 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Init the custom command.
*
*/
void esp_cli_custom_command_init(void);
#ifdef __cplusplus
}
#endif

View File

@ -1,28 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief User command "iperf" process.
*
*/
void esp_ot_process_iperf(void *aContext, uint8_t aArgsLength, char *aArgs[]);
#ifdef __cplusplus
}
#endif

View File

@ -1,35 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief User command "tcpsockserver" process.
*
*/
void esp_ot_process_tcp_server(void *aContext, uint8_t aArgsLength, char *aArgs[]);
/**
* @brief User command "tcpsockclient" process.
*
*/
void esp_ot_process_tcp_client(void *aContext, uint8_t aArgsLength, char *aArgs[]);
#ifdef __cplusplus
}
#endif

View File

@ -1,43 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief User command "mcast" process.
*
*/
void esp_ot_process_mcast_group(void *aContext, uint8_t aArgsLength, char *aArgs[]);
/**
* @brief User command "udpsockserver" process.
*
*/
void esp_ot_process_udp_server(void *aContext, uint8_t aArgsLength, char *aArgs[]);
/**
* @brief User command "udpsockclient" process.
*
*/
void esp_ot_process_udp_client(void *aContext, uint8_t aArgsLength, char *aArgs[]);
#ifdef __cplusplus
}
#endif

View File

@ -1,36 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Border Router Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include "esp_netif.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief User command "wifi" process.
*
*/
void esp_ot_process_wifi_cmd(void *aContext, uint8_t aArgsLength, char *aArgs[]);
/**
* @brief Wifi netif init.
*
*/
void esp_ot_wifi_netif_init(void);
#ifdef __cplusplus
}
#endif

View File

@ -1,48 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "esp_openthread.h"
#include "esp_ot_cli_extension.h"
#include "esp_ot_iperf.h"
#include "esp_ot_tcp_socket.h"
#include "esp_ot_udp_socket.h"
#include "esp_ot_wifi_cmd.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/task.h"
#include "openthread/cli.h"
static const otCliCommand kCommands[] = {
#if CONFIG_OPENTHREAD_CLI_TCP_SOCKET
{"tcpsockserver", esp_ot_process_tcp_server},
{"tcpsockclient", esp_ot_process_tcp_client},
#endif // CONFIG_OPENTHREAD_CLI_TCP_SOCKET
#if CONFIG_OPENTHREAD_CLI_UDP_SOCKET
{"udpsockserver", esp_ot_process_udp_server},
{"udpsockclient", esp_ot_process_udp_client},
{"mcast", esp_ot_process_mcast_group},
#endif // CONFIG_OPENTHREAD_CLI_UDP_SOCKET
#if CONFIG_OPENTHREAD_CLI_IPERF
{"iperf", esp_ot_process_iperf},
#endif // CONFIG_OPENTHREAD_CLI_IPERF
#if CONFIG_OPENTHREAD_CLI_WIFI
{"wifi", esp_ot_process_wifi_cmd},
#endif // CONFIG_OPENTHREAD_CLI_WIFI
};
void esp_cli_custom_command_init()
{
otInstance *instance = esp_openthread_get_instance();
otCliSetUserCommands(kCommands, (sizeof(kCommands) / sizeof(kCommands[0])), instance);
}

View File

@ -1,108 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "esp_check.h"
#include "esp_log.h"
#include "esp_ot_iperf.h"
#include "iperf.h"
#include "openthread/cli.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TAG "ot-iperf"
static char s_dest_ip6_addr[50];
void esp_ot_process_iperf(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
(void)(aContext);
(void)(aArgsLength);
iperf_cfg_t cfg;
memset(&cfg, 0, sizeof(cfg));
// set iperf default flag: tcp server
IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_TCP);
IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_SERVER);
cfg.time = IPERF_DEFAULT_TIME;
cfg.type = IPERF_IP_TYPE_IPV4;
if (aArgsLength == 0) {
otCliOutputFormat("---iperf parameter---\n");
otCliOutputFormat("-V : use IPV6 address\n");
otCliOutputFormat("-s : server mode, only receive\n");
otCliOutputFormat("-u : upd mode\n");
otCliOutputFormat("-c <addr> : client mode, only transmit\n");
otCliOutputFormat("-i <interval> : seconds between periodic bandwidth reports\n");
otCliOutputFormat("-t <time> : time in seconds to transmit for (default 30 secs)\n");
otCliOutputFormat("-p <port> : server port to listen on/connect to\n");
otCliOutputFormat("-l <len_send_buf> : the lenth of send buffer\n");
otCliOutputFormat("---example---\n");
otCliOutputFormat("create a tcp server : iperf -s -i 3 -p 5001 -t 60\n");
otCliOutputFormat("create a udp client : iperf -c <addr> -u -i 3 -t 60 -p 5001 -l 512\n");
} else {
for (int i = 0; i < aArgsLength; i++) {
if (strcmp(aArgs[i], "-c") == 0) {
IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_CLIENT);
IPERF_FLAG_CLR(cfg.flag, IPERF_FLAG_SERVER);
i++;
strcpy(s_dest_ip6_addr, aArgs[i]);
cfg.destination_ip6 = s_dest_ip6_addr;
otCliOutputFormat("ip:%s\n", cfg.destination_ip6);
} else if (strcmp(aArgs[i], "-s") == 0) {
IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_SERVER);
IPERF_FLAG_CLR(cfg.flag, IPERF_FLAG_CLIENT);
} else if (strcmp(aArgs[i], "-V") == 0) {
cfg.type = IPERF_IP_TYPE_IPV6;
} else if (strcmp(aArgs[i], "-u") == 0) {
IPERF_FLAG_SET(cfg.flag, IPERF_FLAG_UDP);
IPERF_FLAG_CLR(cfg.flag, IPERF_FLAG_TCP);
} else if (strcmp(aArgs[i], "-p") == 0) {
i++;
if (cfg.flag & IPERF_FLAG_SERVER) {
cfg.sport = atoi(aArgs[i]);
cfg.dport = IPERF_DEFAULT_PORT;
} else {
cfg.sport = IPERF_DEFAULT_PORT;
cfg.dport = atoi(aArgs[i]);
}
otCliOutputFormat("dp:%d\n", cfg.dport);
otCliOutputFormat("sp:%d\n", cfg.sport);
} else if (strcmp(aArgs[i], "-i") == 0) {
i++;
if (atoi(aArgs[i]) < 0) {
cfg.interval = IPERF_DEFAULT_INTERVAL;
} else {
cfg.interval = atoi(aArgs[i]);
}
otCliOutputFormat("i:%d\n", cfg.interval);
} else if (strcmp(aArgs[i], "-t") == 0) {
i++;
cfg.time = atoi(aArgs[i]);
if (cfg.time <= cfg.interval) {
cfg.time = cfg.interval;
}
otCliOutputFormat("t:%d\n", cfg.time);
} else if (strcmp(aArgs[i], "-l") == 0) {
i++;
if (atoi(aArgs[i]) <= 0) {
ESP_LOGE(TAG, "Invalid arguments.");
} else {
cfg.len_send_buf = atoi(aArgs[i]);
}
} else if (strcmp(aArgs[i], "-a") == 0) {
iperf_stop();
return;
}
}
iperf_start(&cfg);
}
}

View File

@ -1,162 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "esp_check.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_ot_tcp_socket.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#define TAG "ot_socket"
static void tcp_socket_server_task(void *pvParameters)
{
char addr_str[128];
char rx_buffer[128];
esp_err_t ret = ESP_OK;
int err = 0;
int len = 0;
int listen_sock;
int opt = 1;
int port = CONFIG_OPENTHREAD_CLI_TCP_SERVER_PORT;
int client_sock = 0;
struct timeval timeout = { 0 };
struct sockaddr_storage source_addr; // Large enough for both IPv6
struct sockaddr_in6 listen_addr = { 0 };
// The TCP server listen at the address "::", which means all addresses can be listened to.
inet6_aton("::", &listen_addr.sin6_addr);
listen_addr.sin6_family = AF_INET6;
listen_addr.sin6_port = htons(port);
listen_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_IPV6);
ESP_GOTO_ON_FALSE((listen_sock >= 0), ESP_OK, exit, TAG, "Unable to create socket: errno %d", errno);
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// Note that by default IPV6 binds to both protocols, it is must be disabled
// if both protocols used at the same time (used in CI)
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
ESP_LOGI(TAG, "Socket created");
err = bind(listen_sock, (struct sockaddr *)&listen_addr, sizeof(struct sockaddr_in6) );
ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to bind: errno %d, IPPROTO: %d", errno, AF_INET6);
ESP_LOGI(TAG, "Socket bound, port %d", port);
err = listen(listen_sock, 1);
ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Error occurred during listen: errno %d", errno);
//blocking-mode accept, set timeout 30 seconds
timeout.tv_sec = 30;
setsockopt(listen_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
ESP_LOGI(TAG, "Socket listening, timeout is 30 seconds");
socklen_t addr_len = sizeof(source_addr);
client_sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
ESP_GOTO_ON_FALSE((client_sock >= 0), ESP_FAIL, exit, TAG, "Unable to accept connection: errno %d", errno);
ESP_GOTO_ON_FALSE((err >= 0), ESP_FAIL, exit, TAG, "Error occurred during sending: errno %d", errno);
//blocking-mode receive, set timeout 30 seconds
timeout.tv_sec = 30;
setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
len = recv(client_sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
// Error occurred during receiving
ESP_GOTO_ON_FALSE((len >= 0), ESP_FAIL, exit, TAG, "recv failed: errno %d", errno);
// Data received
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
ESP_LOGI(TAG, "Received %d bytes from client:", len);
ESP_LOGI(TAG, "%s", rx_buffer);
// Convert ip address to string
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
exit:
if (ret != ESP_OK || listen_sock != -1) {
shutdown(listen_sock, 0);
close(listen_sock);
}
if (client_sock != -1) {
shutdown(client_sock, 0);
close(client_sock);
}
ESP_LOGI(TAG, "Socket server is closed.");
vTaskDelete(NULL);
}
static void tcp_socket_client_task(void *pvParameters)
{
char *host_ip = (char *)pvParameters;
char *payload = "This message is from client\n";
char rx_buffer[128];
esp_err_t ret = ESP_OK;
int client_sock;
int err = 0;
int len = 0;
int port = CONFIG_OPENTHREAD_CLI_TCP_SERVER_PORT;
struct sockaddr_in6 dest_addr = { 0 };
inet6_aton(host_ip, &dest_addr.sin6_addr);
dest_addr.sin6_family = AF_INET6;
dest_addr.sin6_port = htons(port);
client_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_IPV6);
ESP_GOTO_ON_FALSE((client_sock >= 0), ESP_OK, exit, TAG, "Unable to create socket: errno %d", errno);
ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, port);
err = connect(client_sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to connect: errno %d", errno);
ESP_LOGI(TAG, "Successfully connected");
len = send(client_sock, payload, strlen(payload), 0);
ESP_GOTO_ON_FALSE((len >= 0), ESP_FAIL, exit, TAG, "Error occurred during sending: errno %d", errno);
len = recv(client_sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
// Error occurred during receiving
ESP_GOTO_ON_FALSE((len >= 0), ESP_FAIL, exit, TAG, "recv failed: errno %d", errno);
// Data received
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
ESP_LOGI(TAG, "Received %d bytes from %s", len, host_ip);
ESP_LOGI(TAG, "%s", rx_buffer);
exit:
if (ret != ESP_OK || client_sock != -1) {
shutdown(client_sock, 0);
close(client_sock);
}
ESP_LOGI(TAG, "Socket client is closed.");
vTaskDelete(NULL);
}
void esp_ot_process_tcp_server(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
(void)(aContext);
(void)(aArgsLength);
(void)(*aArgs);
xTaskCreate(tcp_socket_server_task, "ot_tcp_scoket_server", 4096, xTaskGetCurrentTaskHandle(), 4, NULL);
}
void esp_ot_process_tcp_client(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
(void)(aContext);
(void)(aArgsLength);
if (aArgsLength == 0) {
ESP_LOGE(TAG, "Invalid arguments.");
} else {
xTaskCreate(tcp_socket_client_task, "ot_tcp_socket_client", 4096, aArgs[0], 4, NULL);
}
}

View File

@ -1,170 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "esp_check.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_openthread_lock.h"
#include "esp_ot_udp_socket.h"
#include "lwip/err.h"
#include "lwip/mld6.h"
#include "lwip/sockets.h"
#define TAG "ot_socket"
static void udp_socket_server_task(void *pvParameters)
{
char addr_str[128];
char *payload = "This message is from server\n";
char rx_buffer[128];
esp_err_t ret = ESP_OK;
int err = 0;
int len;
int listen_sock;
int port = CONFIG_OPENTHREAD_CLI_UDP_SERVER_PORT;
struct timeval timeout = { 0 };
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
struct sockaddr_in6 listen_addr;
inet6_aton("::", &listen_addr.sin6_addr);
listen_addr.sin6_family = AF_INET6;
listen_addr.sin6_port = htons(port);
listen_sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
ESP_GOTO_ON_FALSE((listen_sock >= 0), ESP_OK, exit, TAG, "Unable to create socket: errno %d", errno);
ESP_LOGI(TAG, "Socket created");
// Note that by default IPV6 binds to both protocols, it is must be disabled
// if both protocols used at the same time (used in CI)
int opt = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
err = bind(listen_sock, (struct sockaddr *)&listen_addr, sizeof(listen_addr));
ESP_GOTO_ON_FALSE((err == 0), ESP_FAIL, exit, TAG, "Socket unable to bind: errno %d", errno);
ESP_LOGI(TAG, "Socket bound, port %d", port);
timeout.tv_sec = 30;
setsockopt(listen_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
ESP_LOGI(TAG, "Waiting for data, timeout is 30 seconds");
socklen_t socklen = sizeof(source_addr);
len = recvfrom(listen_sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
// Error occurred during receiving
ESP_GOTO_ON_FALSE((len >= 0), ESP_FAIL, exit, TAG, "recvfrom failed: errno %d", errno);
// Data received
// Get the sender's ip address as string
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...
ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
ESP_LOGI(TAG, "%s", rx_buffer);
err = sendto(listen_sock, payload, strlen(payload), 0, (struct sockaddr *)&source_addr, sizeof(source_addr));
ESP_GOTO_ON_FALSE((err >= 0), ESP_FAIL, exit, TAG, "Error occurred during sending: errno %d", errno);
exit:
if (ret != ESP_OK || listen_sock != -1) {
shutdown(listen_sock, 0);
close(listen_sock);
}
ESP_LOGI(TAG, "Socket server is closed.");
vTaskDelete(NULL);
}
static void udp_socket_client_task(void *pvParameters)
{
char rx_buffer[128];
char *host_ip = (char *)pvParameters;
char *payload = "This message is from client\n";
int client_sock;
int port = CONFIG_OPENTHREAD_CLI_UDP_SERVER_PORT;
int err = 0;
int len;
esp_err_t ret = ESP_OK;
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
struct sockaddr_in6 dest_addr = { 0 };
uint8_t netif_index = esp_netif_get_netif_impl_index(esp_netif_get_handle_from_ifkey("OT_DEF"));
inet6_aton(host_ip, &dest_addr.sin6_addr);
dest_addr.sin6_family = AF_INET6;
dest_addr.sin6_port = htons(port);
client_sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
ESP_GOTO_ON_FALSE((client_sock >= 0), ESP_OK, exit, TAG, "Unable to create socket: errno %d", errno);
ESP_LOGI(TAG, "Socket created, sending to %s:%d", host_ip, port);
setsockopt(client_sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, &netif_index, sizeof(netif_index));
err = sendto(client_sock, payload, strlen(payload), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
ESP_GOTO_ON_FALSE((err >= 0), ESP_FAIL, exit, TAG, "Error occurred during sending: errno %d", errno);
ESP_LOGI(TAG, "Message sent");
socklen_t socklen = sizeof(source_addr);
len = recvfrom(client_sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
// Error occurred during receiving
ESP_GOTO_ON_FALSE((len >= 0), ESP_FAIL, exit, TAG, "recvfrom failed: errno %d", errno);
// Data received
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
ESP_LOGI(TAG, "%s", rx_buffer);
exit:
if (ret != ESP_OK || client_sock != -1) {
shutdown(client_sock, 0);
close(client_sock);
}
ESP_LOGI(TAG, "Socket client is closed");
vTaskDelete(NULL);
}
void esp_ot_process_udp_server(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
(void)(aContext);
(void)(aArgsLength);
xTaskCreate(udp_socket_server_task, "ot_udp_scoket_server", 4096, xTaskGetCurrentTaskHandle(), 4, NULL);
}
void esp_ot_process_udp_client(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
(void)(aContext);
if (aArgsLength == 0) {
ESP_LOGE(TAG, "Invalid arguments.");
} else {
xTaskCreate(udp_socket_client_task, "ot_udp_socket_client", 4096, aArgs[0], 4, NULL);
}
}
void esp_ot_process_mcast_group(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
if (aArgsLength != 2 || (strncmp(aArgs[0], "join", 4) != 0 && strncmp(aArgs[0], "leave", 5) != 0) ) {
ESP_LOGE(TAG, "Invalid arguments: mcast [join|leave] group_address");
return;
}
ip6_addr_t group;
inet6_aton(aArgs[1], &group);
struct netif *netif = netif_get_by_index(esp_netif_get_netif_impl_index(esp_netif_get_handle_from_ifkey("OT_DEF")));
if (strncmp(aArgs[0], "join", 4) == 0) {
if (mld6_joingroup_netif(netif, &group) != ERR_OK) {
ESP_LOGE(TAG, "Failed to join group");
}
} else {
if (mld6_leavegroup_netif(netif, &group) != ERR_OK) {
ESP_LOGE(TAG, "Failed to leave group");
}
}
}

View File

@ -1,151 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Border Router Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "esp_netif_types.h"
#include "esp_openthread_border_router.h"
#include "esp_check.h"
#include "esp_event.h"
#include "esp_openthread_lock.h"
#include "esp_netif.h"
#include "esp_netif_ip_addr.h"
#include "esp_netif_net_stack.h"
#include "esp_wifi.h"
#include "freertos/event_groups.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/task.h"
#include "openthread/cli.h"
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_IP4_BIT = BIT0;
const int CONNECTED_IP6_BIT = BIT1;
static bool s_wifi_connected = false;
void esp_ot_wifi_netif_init(void)
{
esp_netif_t *esp_netif = esp_netif_create_default_wifi_sta();
assert(esp_netif);
}
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_DISCONNECTED) {
xEventGroupClearBits(wifi_event_group, CONNECTED_IP4_BIT);
xEventGroupClearBits(wifi_event_group, CONNECTED_IP6_BIT);
esp_wifi_connect();
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupSetBits(wifi_event_group, CONNECTED_IP4_BIT);
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
esp_netif_create_ip6_linklocal(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
} else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
xEventGroupSetBits(wifi_event_group, CONNECTED_IP6_BIT);
}
}
static void wifi_join(const char *ssid, const char *psk)
{
static bool s_initialized = false;
if (!s_initialized) {
wifi_event_group = xEventGroupCreate();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &event_handler, NULL) );
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_NULL);
s_initialized = true;
}
esp_wifi_start();
wifi_config_t wifi_config = { 0 };
strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
if (psk) {
strncpy((char *) wifi_config.sta.password, psk, sizeof(wifi_config.sta.password));
}
ESP_ERROR_CHECK( esp_wifi_set_ps(WIFI_PS_NONE) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
esp_wifi_connect();
int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_IP4_BIT | CONNECTED_IP6_BIT, pdFALSE, pdTRUE, 10000 / portTICK_PERIOD_MS);
if (((bits & CONNECTED_IP4_BIT) != 0) && ((bits & CONNECTED_IP6_BIT) != 0)) {
s_wifi_connected = true;
xEventGroupClearBits(wifi_event_group, CONNECTED_IP4_BIT);
xEventGroupClearBits(wifi_event_group, CONNECTED_IP6_BIT);
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler);
esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &event_handler);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &event_handler);
vEventGroupDelete(wifi_event_group);
s_initialized = false;
esp_openthread_lock_acquire(portMAX_DELAY);
esp_openthread_border_router_init();
esp_openthread_lock_release();
otCliOutputFormat("wifi sta is connected successfully\n");
} else {
esp_wifi_disconnect();
esp_wifi_stop();
ESP_LOGW("","Connection time out, please check your ssid & password, then retry.");
otCliOutputFormat("wifi sta connection is failed\n");
}
}
void esp_ot_process_wifi_cmd(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
char ssid[100] = "";
char psk[100] = "";
(void)(aContext);
if (aArgsLength == 0) {
otCliOutputFormat("---wifi parameter---\n");
otCliOutputFormat("connect\n");
otCliOutputFormat("-s : wifi ssid\n");
otCliOutputFormat("-p : wifi psk\n");
otCliOutputFormat("---example---\n");
otCliOutputFormat("join a wifi:\nssid: threadcertAP \npsk: threadcertAP : wifi connect -s threadcertAP -p threadcertAP\n");
otCliOutputFormat("state : get wifi state, disconnect or connect\n");
otCliOutputFormat("---example---\n");
otCliOutputFormat("get wifi state : wifi state\n");
} else if (strcmp(aArgs[0], "connect") == 0) {
for (int i = 1; i < aArgsLength; i++) {
if (strcmp(aArgs[i], "-s") == 0) {
i++;
strcpy(ssid, aArgs[i]);
otCliOutputFormat("ssid: %s\n", ssid);
} else if (strcmp(aArgs[i], "-p") == 0) {
i++;
strcpy(psk, aArgs[i]);
otCliOutputFormat("psk: %s\n", psk);
}
}
if (!s_wifi_connected) {
wifi_join(ssid, psk);
} else {
otCliOutputFormat("wifi has already connected\n");
}
} else if (strcmp(aArgs[0], "state") == 0) {
if (s_wifi_connected) {
otCliOutputFormat("connected\n");
} else {
otCliOutputFormat("disconnected\n");
}
} else {
otCliOutputFormat("invalid commands\n");
}
}

View File

@ -5,8 +5,7 @@ cmake_minimum_required(VERSION 3.16)
# (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 $ENV{IDF_PATH}/examples/common_components/protocol_examples_common
$ENV{IDF_PATH}/examples/common_components/iperf
$ENV{IDF_PATH}/examples/openthread/extension_command)
$ENV{IDF_PATH}/examples/common_components/iperf)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_ot_br)

View File

@ -182,9 +182,8 @@ static void ot_task_worker(void *aContext)
ESP_ERROR_CHECK(esp_openthread_border_router_init());
create_config_network(esp_openthread_get_instance());
launch_openthread_network(esp_openthread_get_instance());
#else
esp_cli_custom_command_init();
#endif // CONFIG_OPENTHREAD_BR_AUTO_START
esp_cli_custom_command_init();
esp_openthread_lock_release();
// Run the main loop

View File

@ -1,5 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_ot_cli_extension: "*"
espressif/mdns: "^1.0.3"
## Required IDF version
idf:

View File

@ -2,9 +2,7 @@
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/iperf
$ENV{IDF_PATH}/examples/openthread/extension_command)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/iperf)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_ot_cli)

View File

@ -0,0 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_ot_cli_extension: "*"
idf:
version: ">=4.1.0"