openthread: ot extension command refactor
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user