esp_zigbee_gateway.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2021 Espressif Systems (Shanghai) CO LTD
  3. * All rights reserved.
  4. *
  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 "esp_log.h"
  38. #include "freertos/FreeRTOS.h"
  39. #include "freertos/task.h"
  40. #include "esp_zigbee_gateway.h"
  41. #if (!defined ZB_MACSPLIT_HOST && defined ZB_MACSPLIT_DEVICE)
  42. #error Only Zigbee gateway hostdevice should be defined
  43. #endif
  44. static const char *TAG = "ESP_ZB_GATEWAY";
  45. /********************* Define functions **************************/
  46. static void bdb_start_top_level_commissioning_cb(zb_uint8_t mode_mask)
  47. {
  48. if (!bdb_start_top_level_commissioning(mode_mask)) {
  49. ESP_LOGE(TAG, "In BDB commissioning, an error occurred (for example: the device has already been running)");
  50. }
  51. }
  52. void zboss_signal_handler(zb_bufid_t bufid)
  53. {
  54. zb_zdo_app_signal_hdr_t *p_sg_p = NULL;
  55. zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, &p_sg_p);
  56. zb_ret_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
  57. zb_zdo_signal_device_annce_params_t *dev_annce_params = NULL;
  58. zb_zdo_signal_macsplit_dev_boot_params_t *rcp_version = NULL;
  59. zb_uint32_t gateway_version;
  60. switch (sig) {
  61. case ZB_ZDO_SIGNAL_SKIP_STARTUP:
  62. ESP_LOGI(TAG, "Zigbee stack initialized");
  63. bdb_start_top_level_commissioning(ZB_BDB_INITIALIZATION);
  64. break;
  65. case ZB_MACSPLIT_DEVICE_BOOT:
  66. ESP_LOGI(TAG, "Zigbee rcp device booted");
  67. gateway_version = zb_esp_macsplit_get_version();
  68. rcp_version = ZB_ZDO_SIGNAL_GET_PARAMS(p_sg_p, zb_zdo_signal_macsplit_dev_boot_params_t);
  69. ESP_LOGI(TAG, "Zigbee rcp device version: %d.%d.%d", (rcp_version->dev_version >> 24 & 0x000000FF), (rcp_version->dev_version >> 16 & 0x000000FF), (rcp_version->dev_version & 0x000000FF));
  70. ESP_LOGI(TAG, "Zigbee gateway version: %d.%d.%d", (gateway_version >> 24 & 0x000000FF), (gateway_version >> 16 & 0x000000FF), (gateway_version & 0x000000FF));
  71. if (gateway_version != rcp_version->dev_version) {
  72. ESP_LOGE(TAG, "rcp has different Zigbee stack version with Zigbee gateway! Please check the rcp software or other issues");
  73. }
  74. break;
  75. case ZB_BDB_SIGNAL_DEVICE_FIRST_START:
  76. if (status == RET_OK) {
  77. ESP_LOGI(TAG, "Start network formation");
  78. bdb_start_top_level_commissioning(ZB_BDB_NETWORK_FORMATION);
  79. } else {
  80. ESP_LOGE(TAG, "Failed to initialize Zigbee stack (status: %d)", status);
  81. }
  82. break;
  83. case ZB_BDB_SIGNAL_FORMATION:
  84. if (status == RET_OK) {
  85. zb_ieee_addr_t ieee_address;
  86. zb_get_long_address(ieee_address);
  87. ESP_LOGI(TAG, "Formed network successfully (ieee extended address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx)",
  88. ieee_address[7], ieee_address[6], ieee_address[5], ieee_address[4],
  89. ieee_address[3], ieee_address[2], ieee_address[1], ieee_address[0],
  90. ZB_PIBCACHE_PAN_ID());
  91. bdb_start_top_level_commissioning(ZB_BDB_NETWORK_STEERING);
  92. } else {
  93. ESP_LOGI(TAG, "Restart network formation (status: %d)", status);
  94. ZB_SCHEDULE_APP_ALARM((zb_callback_t)bdb_start_top_level_commissioning_cb, ZB_BDB_NETWORK_FORMATION, ZB_TIME_ONE_SECOND);
  95. }
  96. break;
  97. case ZB_BDB_SIGNAL_STEERING:
  98. if (status == RET_OK) {
  99. ESP_LOGI(TAG, "Network steering started");
  100. }
  101. break;
  102. case ZB_ZDO_SIGNAL_DEVICE_ANNCE:
  103. dev_annce_params = ZB_ZDO_SIGNAL_GET_PARAMS(p_sg_p, zb_zdo_signal_device_annce_params_t);
  104. ESP_LOGI(TAG, "New device commissioned or rejoined (short: 0x%04hx)", dev_annce_params->device_short_addr);
  105. break;
  106. default:
  107. ESP_LOGI(TAG, "status: %d", status);
  108. break;
  109. }
  110. /* All callbacks should either reuse or free passed buffers. If bufid == 0, the buffer is invalid (not passed) */
  111. if (bufid) {
  112. zb_buf_free(bufid);
  113. }
  114. }
  115. static void zboss_task(void *pvParameters)
  116. {
  117. ZB_INIT("zigbee gateway");
  118. zb_set_network_coordinator_role(IEEE_CHANNEL_MASK);
  119. zb_set_nvram_erase_at_start(ERASE_PERSISTENT_CONFIG);
  120. zb_set_max_children(MAX_CHILDREN);
  121. /* initiate Zigbee Stack start without zb_send_no_autostart_signal auto-start */
  122. ESP_ERROR_CHECK(zboss_start_no_autostart());
  123. while (1) {
  124. zboss_main_loop_iteration();
  125. }
  126. }
  127. void app_main(void)
  128. {
  129. zb_esp_platform_config_t config = {
  130. .radio_config = ZB_ESP_DEFAULT_RADIO_CONFIG(),
  131. .host_config = ZB_ESP_DEFAULT_HOST_CONFIG(),
  132. };
  133. /* load Zigbee gateway platform config to initialization */
  134. ESP_ERROR_CHECK(zb_esp_platform_config(&config));
  135. xTaskCreate(zboss_task, "zboss_main", 4096, NULL, 5, NULL);
  136. }