esp_ot_cli.c 3.0 KB

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