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