esp_ot_cli.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include "esp_err.h"
  16. #include "esp_event.h"
  17. #include "esp_log.h"
  18. #include "esp_netif.h"
  19. #include "esp_netif_types.h"
  20. #include "esp_openthread.h"
  21. #include "esp_openthread_lock.h"
  22. #include "esp_openthread_netif_glue.h"
  23. #include "esp_openthread_types.h"
  24. #include "esp_ot_config.h"
  25. #include "esp_vfs_eventfd.h"
  26. #include "driver/uart.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/portmacro.h"
  29. #include "freertos/task.h"
  30. #include "hal/uart_types.h"
  31. #include "openthread/cli.h"
  32. #include "openthread/instance.h"
  33. #include "openthread/tasklet.h"
  34. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  35. #include "esp_ot_cli_extension.h"
  36. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  37. #define TAG "ot_esp_cli"
  38. extern void otAppCliInit(otInstance *aInstance);
  39. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  40. static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config)
  41. {
  42. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
  43. esp_netif_t *netif = esp_netif_new(&cfg);
  44. assert(netif != NULL);
  45. ESP_ERROR_CHECK(esp_netif_attach(netif, esp_openthread_netif_glue_init(config)));
  46. return netif;
  47. }
  48. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  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. // Initialize the OpenThread cli
  59. otAppCliInit(esp_openthread_get_instance());
  60. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  61. esp_netif_t *openthread_netif;
  62. // Initialize the esp_netif bindings
  63. openthread_netif = init_openthread_netif(&config);
  64. esp_cli_custom_command_init();
  65. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  66. // Run the main loop
  67. esp_openthread_launch_mainloop();
  68. // Clean up
  69. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  70. esp_netif_destroy(openthread_netif);
  71. esp_openthread_netif_glue_deinit();
  72. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  73. esp_vfs_eventfd_unregister();
  74. vTaskDelete(NULL);
  75. }
  76. void app_main(void)
  77. {
  78. // Used eventfds:
  79. // * netif
  80. // * ot task queue
  81. // * radio driver
  82. esp_vfs_eventfd_config_t eventfd_config = {
  83. .max_fds = 3,
  84. };
  85. ESP_ERROR_CHECK(esp_event_loop_create_default());
  86. #if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  87. ESP_ERROR_CHECK(esp_netif_init());
  88. #endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
  89. ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
  90. xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
  91. }