esp_zigbee_gateway.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: LicenseRef-Included
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Espressif Systems
  13. * integrated circuit in a product or a software update for such product,
  14. * must reproduce the above copyright notice, this list of conditions and
  15. * the following disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. *
  18. * 3. Neither the name of the copyright holder nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * 4. Any software provided in binary form under this license must not be reverse
  23. * engineered, decompiled, modified and/or disassembled.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <fcntl.h>
  38. #include "esp_log.h"
  39. #include "freertos/FreeRTOS.h"
  40. #include "freertos/task.h"
  41. #include "esp_netif.h"
  42. #include "esp_vfs_eventfd.h"
  43. #include "esp_wifi.h"
  44. #include "nvs_flash.h"
  45. #include "protocol_examples_common.h"
  46. #include "esp_coexist_internal.h"
  47. #include "esp_zigbee_gateway.h"
  48. #include "esp_vfs_dev.h"
  49. #include "esp_vfs_usb_serial_jtag.h"
  50. #include "driver/usb_serial_jtag.h"
  51. #if (!defined ZB_MACSPLIT_HOST && defined ZB_MACSPLIT_DEVICE)
  52. #error Only Zigbee gateway host device should be defined
  53. #endif
  54. static const char *TAG = "ESP_ZB_GATEWAY";
  55. /* Note: Please select the correct console output port based on the development board in menuconfig */
  56. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  57. esp_err_t esp_zb_gateway_console_init(void)
  58. {
  59. esp_err_t ret = ESP_OK;
  60. /* Disable buffering on stdin */
  61. setvbuf(stdin, NULL, _IONBF, 0);
  62. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  63. esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  64. /* Move the caret to the beginning of the next line on '\n' */
  65. esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  66. /* Enable non-blocking mode on stdin and stdout */
  67. fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);
  68. fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
  69. usb_serial_jtag_driver_config_t usb_serial_jtag_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
  70. ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
  71. esp_vfs_usb_serial_jtag_use_driver();
  72. esp_vfs_dev_uart_register();
  73. return ret;
  74. }
  75. #endif
  76. /********************* Define functions **************************/
  77. static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
  78. {
  79. ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask));
  80. }
  81. void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
  82. {
  83. uint32_t *p_sg_p = signal_struct->p_app_signal;
  84. esp_err_t err_status = signal_struct->esp_err_status;
  85. esp_zb_app_signal_type_t sig_type = *p_sg_p;
  86. esp_zb_zdo_signal_device_annce_params_t *dev_annce_params = NULL;
  87. esp_zb_zdo_signal_macsplit_dev_boot_params_t *rcp_version = NULL;
  88. switch (sig_type) {
  89. case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP:
  90. ESP_LOGI(TAG, "Zigbee stack initialized");
  91. esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION);
  92. break;
  93. case ESP_ZB_MACSPLIT_DEVICE_BOOT:
  94. ESP_LOGI(TAG, "Zigbee rcp device booted");
  95. rcp_version = (esp_zb_zdo_signal_macsplit_dev_boot_params_t*)esp_zb_app_signal_get_params(p_sg_p);
  96. ESP_LOGI(TAG, "Running RCP Version:%s", rcp_version->version_str);
  97. break;
  98. case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START:
  99. case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT:
  100. if (err_status == ESP_OK) {
  101. ESP_LOGI(TAG, "Start network formation");
  102. esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_FORMATION);
  103. } else {
  104. ESP_LOGE(TAG, "Failed to initialize Zigbee stack (status: %d)", err_status);
  105. }
  106. break;
  107. case ESP_ZB_BDB_SIGNAL_FORMATION:
  108. if (err_status == ESP_OK) {
  109. esp_zb_ieee_addr_t ieee_address;
  110. esp_zb_get_long_address(ieee_address);
  111. ESP_LOGI(TAG, "Formed network successfully (ieee_address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d)",
  112. ieee_address[7], ieee_address[6], ieee_address[5], ieee_address[4],
  113. ieee_address[3], ieee_address[2], ieee_address[1], ieee_address[0],
  114. esp_zb_get_pan_id(), esp_zb_get_current_channel());
  115. esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
  116. } else {
  117. ESP_LOGI(TAG, "Restart network formation (status: %d)", err_status);
  118. esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_FORMATION, 1000);
  119. }
  120. break;
  121. case ESP_ZB_BDB_SIGNAL_STEERING:
  122. if (err_status == ESP_OK) {
  123. ESP_LOGI(TAG, "Network steering started");
  124. }
  125. break;
  126. case ESP_ZB_ZDO_SIGNAL_DEVICE_ANNCE:
  127. dev_annce_params = (esp_zb_zdo_signal_device_annce_params_t *)esp_zb_app_signal_get_params(p_sg_p);
  128. ESP_LOGI(TAG, "New device commissioned or rejoined (short: 0x%04hx)", dev_annce_params->device_short_addr);
  129. break;
  130. default:
  131. ESP_LOGI(TAG, "ZDO signal: %d, status: %d", sig_type, err_status);
  132. break;
  133. }
  134. }
  135. static void esp_zb_task(void *pvParameters)
  136. {
  137. /* initialize Zigbee stack with Zigbee coordinator config */
  138. esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZC_CONFIG();
  139. esp_zb_init(&zb_nwk_cfg);
  140. /* initiate Zigbee Stack start without zb_send_no_autostart_signal auto-start */
  141. esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
  142. ESP_ERROR_CHECK(esp_zb_start(false));
  143. esp_zb_main_loop_iteration();
  144. }
  145. void app_main(void)
  146. {
  147. esp_zb_platform_config_t config = {
  148. .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
  149. .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
  150. };
  151. /* load Zigbee gateway platform config to initialization */
  152. ESP_ERROR_CHECK(esp_zb_platform_config(&config));
  153. ESP_ERROR_CHECK(nvs_flash_init());
  154. ESP_ERROR_CHECK(esp_netif_init());
  155. ESP_ERROR_CHECK(esp_event_loop_create_default());
  156. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  157. ESP_ERROR_CHECK(esp_zb_gateway_console_init());
  158. #endif
  159. #if CONFIG_EXAMPLE_CONNECT_WIFI
  160. ESP_ERROR_CHECK(example_connect());
  161. #if CONFIG_ESP_COEX_SW_COEXIST_ENABLE
  162. ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM));
  163. coex_enable();
  164. coex_schm_status_bit_set(1, 1);
  165. #else
  166. ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
  167. #endif
  168. #endif
  169. xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
  170. }