esp_ot_cli.c 3.0 KB

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