esp_ot_cli.c 3.1 KB

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