1 /* OpenThread Command Line Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9
10 #include <stdio.h>
11 #include <unistd.h>
12
13 #include "esp_err.h"
14 #include "esp_event.h"
15 #include "esp_log.h"
16 #include "esp_netif.h"
17 #include "esp_netif_types.h"
18 #include "esp_openthread.h"
19 #include "esp_openthread_cli.h"
20 #include "esp_openthread_lock.h"
21 #include "esp_openthread_netif_glue.h"
22 #include "esp_openthread_types.h"
23 #include "esp_ot_config.h"
24 #include "esp_vfs_eventfd.h"
25 #include "driver/uart.h"
26 #include "freertos/FreeRTOS.h"
27 #include "freertos/portmacro.h"
28 #include "freertos/task.h"
29 #include "hal/uart_types.h"
30 #include "openthread/cli.h"
31 #include "openthread/instance.h"
32 #include "openthread/logging.h"
33 #include "openthread/tasklet.h"
34
35 #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
36 #include "esp_ot_cli_extension.h"
37 #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
38
39 #define TAG "ot_esp_cli"
40
41 #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
init_openthread_netif(const esp_openthread_platform_config_t * config)42 static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config)
43 {
44 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
45 esp_netif_t *netif = esp_netif_new(&cfg);
46 assert(netif != NULL);
47 ESP_ERROR_CHECK(esp_netif_attach(netif, esp_openthread_netif_glue_init(config)));
48
49 return netif;
50 }
51 #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
52
ot_task_worker(void * aContext)53 static void ot_task_worker(void *aContext)
54 {
55 esp_openthread_platform_config_t config = {
56 .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
57 .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
58 .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
59 };
60
61 // Initialize the OpenThread stack
62 ESP_ERROR_CHECK(esp_openthread_init(&config));
63
64 // The OpenThread log level directly matches ESP log level
65 (void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
66 // Initialize the OpenThread cli
67 esp_openthread_cli_init();
68
69 #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
70 esp_netif_t *openthread_netif;
71 // Initialize the esp_netif bindings
72 openthread_netif = init_openthread_netif(&config);
73
74 esp_cli_custom_command_init();
75 #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
76
77 // Run the main loop
78 esp_openthread_cli_create_task();
79 esp_openthread_launch_mainloop();
80
81 // Clean up
82 #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
83 esp_netif_destroy(openthread_netif);
84 esp_openthread_netif_glue_deinit();
85 #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
86
87 esp_vfs_eventfd_unregister();
88 vTaskDelete(NULL);
89 }
90
app_main(void)91 void app_main(void)
92 {
93 // Used eventfds:
94 // * netif
95 // * ot task queue
96 // * radio driver
97 esp_vfs_eventfd_config_t eventfd_config = {
98 .max_fds = 3,
99 };
100
101 ESP_ERROR_CHECK(esp_event_loop_create_default());
102 #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
103 ESP_ERROR_CHECK(esp_netif_init());
104 #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
105 ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
106 xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
107 }
108