2021-10-20 04:48:09 -04:00
|
|
|
/*
|
2021-12-21 03:53:20 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-10-20 04:48:09 -04:00
|
|
|
*
|
2021-12-21 03:53:20 -05:00
|
|
|
* SPDX-License-Identifier: CC0-1.0
|
2021-10-20 04:48:09 -04:00
|
|
|
*
|
|
|
|
* 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.
|
2021-07-30 03:55:37 -04:00
|
|
|
*/
|
2021-04-02 02:49:49 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2021-12-21 03:53:20 -05:00
|
|
|
#include <string.h>
|
2021-04-02 02:49:49 -04:00
|
|
|
|
|
|
|
#include "esp_err.h"
|
2021-05-18 23:52:51 -04:00
|
|
|
#include "esp_event.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_netif.h"
|
2021-06-08 03:49:05 -04:00
|
|
|
#include "esp_netif_types.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "esp_openthread.h"
|
2021-09-27 00:10:00 -04:00
|
|
|
#include "esp_openthread_cli.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "esp_openthread_lock.h"
|
2021-05-18 23:52:51 -04:00
|
|
|
#include "esp_openthread_netif_glue.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "esp_openthread_types.h"
|
2021-07-09 00:18:41 -04:00
|
|
|
#include "esp_ot_config.h"
|
2021-03-22 03:44:58 -04:00
|
|
|
#include "esp_vfs_eventfd.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "hal/uart_types.h"
|
|
|
|
#include "openthread/cli.h"
|
|
|
|
#include "openthread/instance.h"
|
2021-09-23 05:23:38 -04:00
|
|
|
#include "openthread/logging.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "openthread/tasklet.h"
|
|
|
|
|
2021-07-19 04:37:56 -04:00
|
|
|
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
|
2021-05-25 09:13:41 -04:00
|
|
|
#include "esp_ot_cli_extension.h"
|
2021-07-19 04:37:56 -04:00
|
|
|
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
|
2021-05-25 09:13:41 -04:00
|
|
|
|
2021-04-02 02:49:49 -04:00
|
|
|
#define TAG "ot_esp_cli"
|
|
|
|
|
2021-06-28 07:20:51 -04:00
|
|
|
static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config)
|
2021-06-08 03:49:05 -04:00
|
|
|
{
|
|
|
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
|
|
|
|
esp_netif_t *netif = esp_netif_new(&cfg);
|
|
|
|
assert(netif != NULL);
|
2021-06-28 07:20:51 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(netif, esp_openthread_netif_glue_init(config)));
|
2021-06-08 03:49:05 -04:00
|
|
|
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:49:49 -04:00
|
|
|
static void ot_task_worker(void *aContext)
|
|
|
|
{
|
|
|
|
esp_openthread_platform_config_t config = {
|
2021-07-09 00:18:41 -04:00
|
|
|
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
|
|
|
|
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
|
2021-06-28 07:20:51 -04:00
|
|
|
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
|
2021-04-02 02:49:49 -04:00
|
|
|
};
|
2021-05-18 23:52:51 -04:00
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
// Initialize the OpenThread stack
|
|
|
|
ESP_ERROR_CHECK(esp_openthread_init(&config));
|
2021-05-18 23:52:51 -04:00
|
|
|
|
2022-09-27 02:30:46 -04:00
|
|
|
#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
|
2021-09-23 05:23:38 -04:00
|
|
|
// The OpenThread log level directly matches ESP log level
|
|
|
|
(void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
|
2022-09-27 02:30:46 -04:00
|
|
|
#endif
|
2021-06-08 03:49:05 -04:00
|
|
|
// Initialize the OpenThread cli
|
2021-09-27 00:10:00 -04:00
|
|
|
esp_openthread_cli_init();
|
2021-04-02 02:49:49 -04:00
|
|
|
|
2021-07-19 04:37:56 -04:00
|
|
|
esp_netif_t *openthread_netif;
|
2021-06-08 03:49:05 -04:00
|
|
|
// Initialize the esp_netif bindings
|
2021-06-28 07:20:51 -04:00
|
|
|
openthread_netif = init_openthread_netif(&config);
|
2021-04-02 02:49:49 -04:00
|
|
|
|
2022-09-27 02:30:46 -04:00
|
|
|
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
|
2021-05-25 09:13:41 -04:00
|
|
|
esp_cli_custom_command_init();
|
2021-07-19 04:37:56 -04:00
|
|
|
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
|
2021-04-02 02:49:49 -04:00
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
// Run the main loop
|
2021-09-27 00:10:00 -04:00
|
|
|
esp_openthread_cli_create_task();
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_openthread_launch_mainloop();
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
esp_netif_destroy(openthread_netif);
|
2021-05-18 23:52:51 -04:00
|
|
|
esp_openthread_netif_glue_deinit();
|
2021-07-19 04:37:56 -04:00
|
|
|
|
2021-03-22 03:44:58 -04:00
|
|
|
esp_vfs_eventfd_unregister();
|
2021-04-02 02:49:49 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2021-06-08 06:12:53 -04:00
|
|
|
// Used eventfds:
|
|
|
|
// * netif
|
2021-07-09 00:18:41 -04:00
|
|
|
// * ot task queue
|
2021-06-08 06:12:53 -04:00
|
|
|
// * radio driver
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_vfs_eventfd_config_t eventfd_config = {
|
2021-07-09 00:18:41 -04:00
|
|
|
.max_fds = 3,
|
2021-06-08 03:49:05 -04:00
|
|
|
};
|
|
|
|
|
2021-05-18 23:52:51 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2021-03-22 03:44:58 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2021-06-08 03:49:05 -04:00
|
|
|
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
|
2021-04-02 02:49:49 -04:00
|
|
|
xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
|
|
|
|
}
|