esp_ot_cli.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 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 "nvs_flash.h"
  34. #include "openthread/cli.h"
  35. #include "openthread/instance.h"
  36. #include "openthread/logging.h"
  37. #include "openthread/tasklet.h"
  38. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  39. #include "esp_ot_cli_extension.h"
  40. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  41. #define TAG "ot_esp_cli"
  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. return netif;
  49. }
  50. static void ot_task_worker(void *aContext)
  51. {
  52. esp_openthread_platform_config_t config = {
  53. .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
  54. .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
  55. .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
  56. };
  57. // Initialize the OpenThread stack
  58. ESP_ERROR_CHECK(esp_openthread_init(&config));
  59. #if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
  60. // The OpenThread log level directly matches ESP log level
  61. (void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
  62. #endif
  63. // Initialize the OpenThread cli
  64. #if CONFIG_OPENTHREAD_CLI
  65. esp_openthread_cli_init();
  66. #endif
  67. esp_netif_t *openthread_netif;
  68. // Initialize the esp_netif bindings
  69. openthread_netif = init_openthread_netif(&config);
  70. esp_netif_set_default_netif(openthread_netif);
  71. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  72. esp_cli_custom_command_init();
  73. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  74. // Run the main loop
  75. #if CONFIG_OPENTHREAD_CLI
  76. esp_openthread_cli_create_task();
  77. #endif
  78. #if CONFIG_OPENTHREAD_AUTO_START
  79. otOperationalDatasetTlvs dataset;
  80. otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
  81. ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
  82. #endif
  83. esp_openthread_launch_mainloop();
  84. // Clean up
  85. esp_netif_destroy(openthread_netif);
  86. esp_openthread_netif_glue_deinit();
  87. esp_vfs_eventfd_unregister();
  88. vTaskDelete(NULL);
  89. }
  90. void app_main(void)
  91. {
  92. // Used eventfds:
  93. // * netif
  94. // * ot task queue
  95. // * radio driver
  96. esp_vfs_eventfd_config_t eventfd_config = {
  97. .max_fds = 3,
  98. };
  99. ESP_ERROR_CHECK(nvs_flash_init());
  100. ESP_ERROR_CHECK(esp_event_loop_create_default());
  101. ESP_ERROR_CHECK(esp_netif_init());
  102. ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
  103. xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
  104. }