Skip to content

Commit

Permalink
Merge branch 'feature/log_level_set' into 'main'
Browse files Browse the repository at this point in the history
feat(openthread): Add log level set into ot extension commands

See merge request espressif/esp-thread-br!71
  • Loading branch information
chshu committed Aug 23, 2023
2 parents f03f1ca + 49feb92 commit 4f767fe
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 108 deletions.
4 changes: 1 addition & 3 deletions components/esp_ot_cli_extension/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(srcs "src/esp_ot_cli_extension.c")
set(srcs src/esp_ot_cli_extension.c src/esp_ot_ip.c src/esp_ot_loglevel.c)

if(CONFIG_OPENTHREAD_CLI_IPERF)
list(APPEND srcs "src/esp_ot_iperf.c")
Expand All @@ -25,8 +25,6 @@ if(CONFIG_OPENTHREAD_CLI_CURL)
list(APPEND srcs "src/esp_ot_curl.c")
endif()

list(APPEND srcs "src/esp_ot_ip.c")

set(include "include")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "${include}"
Expand Down
2 changes: 1 addition & 1 deletion components/esp_ot_cli_extension/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.4.7"
version: "0.5.0"
description: Espressif OpenThread CLI Extension
url: https://github.com/espressif/esp-thread-br/tree/main/components/esp_ot_cli_extension
dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef enum {
*/
void esp_cli_custom_command_init(void);

#define OT_EXT_CLI_TAG "ot_ext_cli"

#ifdef __cplusplus
}
#endif
23 changes: 23 additions & 0 deletions components/esp_ot_cli_extension/include/esp_ot_loglevel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "stdint.h"
#include <openthread/error.h>

#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief User command "loglevel" process.
*
*/
otError esp_ot_process_logset(void *aContext, uint8_t aArgsLength, char *aArgs[]);

#ifdef __cplusplus
}
#endif
4 changes: 3 additions & 1 deletion components/esp_ot_cli_extension/src/esp_ot_cli_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "esp_ot_dns64.h"
#include "esp_ot_ip.h"
#include "esp_ot_iperf.h"
#include "esp_ot_loglevel.h"
#include "esp_ot_ota_commands.h"
#include "esp_ot_tcp_socket.h"
#include "esp_ot_udp_socket.h"
Expand Down Expand Up @@ -42,7 +43,8 @@ static const otCliCommand kCommands[] = {
#if CONFIG_OPENTHREAD_DNS64_CLIENT
{"dns64server", esp_openthread_process_dns64_server},
#endif // CONFIG_OPENTHREAD_DNS64_CLIENT
{"ip", esp_ot_process_ip}};
{"ip", esp_ot_process_ip},
{"loglevel", esp_ot_process_logset}};

void esp_cli_custom_command_init()
{
Expand Down
19 changes: 9 additions & 10 deletions components/esp_ot_cli_extension/src/esp_ot_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
#include "esp_http_client.h"
#include "esp_log.h"
#include "esp_openthread.h"
#include "esp_ot_cli_extension.h"
#include "http_parser.h"
#include "openthread/cli.h"

#define TAG "curl"

static char s_arg_buf[128];

static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code)
{
if (status_code == HttpStatus_NotFound || status_code == HttpStatus_Forbidden) {
ESP_LOGE(TAG, "File not found(%d)", status_code);
ESP_LOGE(OT_EXT_CLI_TAG, "File not found(%d)", status_code);
return ESP_FAIL;
} else if (status_code >= HttpStatus_BadRequest && status_code < HttpStatus_InternalError) {
ESP_LOGE(TAG, "Client error (%d)", status_code);
ESP_LOGE(OT_EXT_CLI_TAG, "Client error (%d)", status_code);
return ESP_FAIL;
} else if (status_code >= HttpStatus_InternalError) {
ESP_LOGE(TAG, "Server error (%d)", status_code);
ESP_LOGE(OT_EXT_CLI_TAG, "Server error (%d)", status_code);
return ESP_FAIL;
}

Expand All @@ -43,15 +42,15 @@ static esp_err_t _http_connect(esp_http_client_handle_t http_client)
int post_len = esp_http_client_get_post_field(http_client, &post_data);
err = esp_http_client_open(http_client, post_len);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
ESP_LOGE(OT_EXT_CLI_TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
return err;
}
if (post_len) {
int write_len = 0;
while (post_len > 0) {
write_len = esp_http_client_write(http_client, post_data, post_len);
if (write_len < 0) {
ESP_LOGE(TAG, "Write failed");
ESP_LOGE(OT_EXT_CLI_TAG, "Write failed");
return ESP_FAIL;
}
post_len -= write_len;
Expand Down Expand Up @@ -79,12 +78,12 @@ static void curl_task(void *pvParameters)
esp_http_client_handle_t http_client = esp_http_client_init(&config);

if (http_client == NULL) {
ESP_LOGE(TAG, "Failed to initialize HTTP client");
ESP_LOGE(OT_EXT_CLI_TAG, "Failed to initialize HTTP client");
vTaskDelete(NULL);
return;
}
if (_http_connect(http_client) != ESP_OK) {
ESP_LOGE(TAG, "Failed to connect to HTTP server");
ESP_LOGE(OT_EXT_CLI_TAG, "Failed to connect to HTTP server");
vTaskDelete(NULL);
return;
}
Expand All @@ -93,7 +92,7 @@ static void curl_task(void *pvParameters)
char data_buf[1024];
int len = esp_http_client_read(http_client, data_buf, sizeof(data_buf));
if (len < 0 || (len == 0 && (errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED))) {
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
ESP_LOGE(OT_EXT_CLI_TAG, "Connection closed, errno = %d", errno);
vTaskDelete(NULL);
return;
}
Expand Down
15 changes: 8 additions & 7 deletions components/esp_ot_cli_extension/src/esp_ot_dns64.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include "esp_event.h"
#include "esp_openthread.h"
#include "esp_openthread_dns64.h"
#include "esp_ot_cli_extension.h"
#include "lwip/dns.h"
#include "openthread/cli.h"
#include "openthread/netdata.h"

#define OT_CLI_LOG_TAG "OT_CLI"
#define DNS_SERVER_ALTERNATIVE_INDEX 1

static esp_err_t set_dns64(const ip4_addr_t *dns_server)
Expand All @@ -25,23 +25,24 @@ static esp_err_t set_dns64(const ip4_addr_t *dns_server)
dns_server_addr.type = IPADDR_TYPE_V6;

ESP_RETURN_ON_FALSE(esp_openthread_get_nat64_prefix(&dns_server_addr.u_addr.ip6) == ESP_OK, ESP_ERR_NOT_FOUND,
OT_CLI_LOG_TAG, "Cannot find NAT64 prefix\n");
OT_EXT_CLI_TAG, "Cannot find NAT64 prefix");
dns_server_addr.u_addr.ip6.addr[3] = dns_server->addr;
dns_setserver(DNS_SERVER_ALTERNATIVE_INDEX, &dns_server_addr);

ESP_RETURN_ON_ERROR(esp_event_post(OPENTHREAD_EVENT, OPENTHREAD_EVENT_SET_DNS_SERVER, NULL, 0, 0), OT_CLI_LOG_TAG,
ESP_RETURN_ON_ERROR(esp_event_post(OPENTHREAD_EVENT, OPENTHREAD_EVENT_SET_DNS_SERVER, NULL, 0, 0), OT_EXT_CLI_TAG,
"Failed to post OpenThread set DNS server event");
return ESP_OK;
}

otError esp_openthread_process_dns64_server(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
ESP_RETURN_ON_FALSE(aArgsLength != 0, OT_ERROR_INVALID_ARGS, OT_CLI_LOG_TAG, "dns64server DNS_SERVER_URL\n");
ESP_RETURN_ON_FALSE(aArgsLength != 0, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG, "dns64server DNS_SERVER_URL");
ip4_addr_t server_addr;

ESP_RETURN_ON_FALSE(ip4addr_aton(aArgs[0], &server_addr) == 1, OT_ERROR_INVALID_ARGS, OT_CLI_LOG_TAG,
"Invalid DNS server\n");
ESP_RETURN_ON_ERROR(set_dns64(&server_addr), OT_CLI_LOG_TAG, "Failed to set DNS server\n");
ESP_RETURN_ON_FALSE(ip4addr_aton(aArgs[0], &server_addr) == 1, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
"Invalid DNS server");
ESP_RETURN_ON_FALSE(set_dns64(&server_addr) == ESP_OK, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
"Failed to set DNS server");

return OT_ERROR_NONE;
}
4 changes: 2 additions & 2 deletions components/esp_ot_cli_extension/src/esp_ot_iperf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#include "esp_ot_iperf.h"
#include "esp_check.h"
#include "esp_log.h"
#include "esp_ot_cli_extension.h"
#include "iperf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lwip/inet.h"
#include "openthread/cli.h"

#define TAG "ot-iperf"
static char s_dest_ip_addr[50];

otError esp_ot_process_iperf(void *aContext, uint8_t aArgsLength, char *aArgs[])
Expand Down Expand Up @@ -91,7 +91,7 @@ otError esp_ot_process_iperf(void *aContext, uint8_t aArgsLength, char *aArgs[])
} else if (strcmp(aArgs[i], "-l") == 0) {
i++;
if (atoi(aArgs[i]) <= 0) {
ESP_LOGE(TAG, "Invalid arguments.");
ESP_LOGE(OT_EXT_CLI_TAG, "Invalid arguments.");
} else {
cfg.len_send_buf = atoi(aArgs[i]);
}
Expand Down
53 changes: 53 additions & 0 deletions components/esp_ot_cli_extension/src/esp_ot_loglevel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "esp_ot_loglevel.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_ot_cli_extension.h"
#include "string.h"
#include <stdlib.h>
#include "openthread/cli.h"
#include "openthread/error.h"

esp_err_t ext_loglevel_set(const char *tag, const char *level)
{
int levelset = atoi(level);
if (levelset < ESP_LOG_NONE || levelset > ESP_LOG_VERBOSE || (levelset == ESP_LOG_NONE && strcmp(level, "0"))) {
otCliOutputFormat("A wrong log level \"%s\"\n", level);
return ESP_FAIL;
}
esp_log_level_set(tag, (esp_log_level_t)levelset);
return ESP_OK;
}

otError esp_ot_process_logset(void *aContext, uint8_t aArgsLength, char *aArgs[])
{
(void)(aContext);
if (aArgsLength == 0) {
otCliOutputFormat("---loglevel---\n");
otCliOutputFormat("set <TAG> <level> : set log level of the <TAG> to <level>\n");
otCliOutputFormat("---example---\n");
otCliOutputFormat("loglevel set * 3 : set log level of all the tags to INFO\n");
otCliOutputFormat("loglevel set OPENTHREAD 0 : set log level of OpenThread to None\n");
otCliOutputFormat("loglevel set wifi 4 : set log level of Wi-Fi to DEBUG\n");
otCliOutputFormat("----Note----\n");
otCliOutputFormat(
"Support 6 levels : 0(NONE), 1(ERROR), 2(WARN), 3(INFO), 4(DEBUG), 5(VERBOSE)\n");
otCliOutputFormat("The log level of the tags cannot be bigger than the maximum log level\n");
otCliOutputFormat("You can set the maximum log level via:\n");
otCliOutputFormat("idf.py menuconfig > Component config---> Log output---> Maximum log verbosity\n");
} else {
if (strcmp(aArgs[0], "set") == 0 && aArgsLength == 3) {
ESP_RETURN_ON_FALSE(ext_loglevel_set(aArgs[1], aArgs[2]) == ESP_OK, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
"Failed to set log level");
} else {
return OT_ERROR_INVALID_ARGS;
}
}
return OT_ERROR_NONE;
}
Loading

0 comments on commit 4f767fe

Please sign in to comment.