esp_ot_cli.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_lock.h"
  16. #include "esp_openthread_netif_glue.h"
  17. #include "esp_openthread_types.h"
  18. #include "esp_ot_config.h"
  19. #include "esp_vfs_eventfd.h"
  20. #include "driver/uart.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/portmacro.h"
  23. #include "freertos/task.h"
  24. #include "hal/uart_types.h"
  25. #include "openthread/cli.h"
  26. #include "openthread/instance.h"
  27. #include "openthread/tasklet.h"
  28. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  29. #include "esp_ot_cli_extension.h"
  30. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  31. #define TAG "ot_esp_cli"
  32. extern void otAppCliInit(otInstance *aInstance);
  33. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  34. static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config)
  35. {
  36. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
  37. esp_netif_t *netif = esp_netif_new(&cfg);
  38. assert(netif != NULL);
  39. ESP_ERROR_CHECK(esp_netif_attach(netif, esp_openthread_netif_glue_init(config)));
  40. return netif;
  41. }
  42. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  43. static void ot_task_worker(void *aContext)
  44. {
  45. esp_openthread_platform_config_t config = {
  46. .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
  47. .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
  48. .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
  49. };
  50. // Initialize the OpenThread stack
  51. ESP_ERROR_CHECK(esp_openthread_init(&config));
  52. // Initialize the OpenThread cli
  53. otAppCliInit(esp_openthread_get_instance());
  54. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  55. esp_netif_t *openthread_netif;
  56. // Initialize the esp_netif bindings
  57. openthread_netif = init_openthread_netif(&config);
  58. esp_cli_custom_command_init();
  59. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  60. // Run the main loop
  61. esp_openthread_launch_mainloop();
  62. // Clean up
  63. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  64. esp_netif_destroy(openthread_netif);
  65. esp_openthread_netif_glue_deinit();
  66. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  67. esp_vfs_eventfd_unregister();
  68. vTaskDelete(NULL);
  69. }
  70. void app_main(void)
  71. {
  72. // Used eventfds:
  73. // * netif
  74. // * ot task queue
  75. // * radio driver
  76. esp_vfs_eventfd_config_t eventfd_config = {
  77. .max_fds = 3,
  78. };
  79. ESP_ERROR_CHECK(esp_event_loop_create_default());
  80. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  81. ESP_ERROR_CHECK(esp_netif_init());
  82. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  83. ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
  84. xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
  85. }