esp_zb_light.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "esp_zb_light.h"
  39. static bulb_device_ctx_t esp_light_ctx = {
  40. /* basic cluster attributes data */
  41. .basic_attr.zcl_version = ZB_ZCL_VERSION,
  42. .basic_attr.power_source = ZB_ZCL_BASIC_POWER_SOURCE_UNKNOWN,
  43. /* identify cluster attributes data */
  44. .identify_attr.identify_time = 0,
  45. /* groups cluster attributes data */
  46. .groups_attr.name_support = 0,
  47. /* on/off cluster attributes data */
  48. .on_off_attr.on_off = ZB_ZCL_ON_OFF_IS_OFF,
  49. };
  50. static const char *TAG = "ESP_ZB_LIGHT";
  51. /******************* Declare attributes ************************/
  52. /* basic cluster attributes data */
  53. ZB_ZCL_DECLARE_BASIC_ATTRIB_LIST(basic_attr_list,
  54. &esp_light_ctx.basic_attr.zcl_version,
  55. &esp_light_ctx.basic_attr.power_source);
  56. /* identify cluster attributes data */
  57. ZB_ZCL_DECLARE_IDENTIFY_ATTRIB_LIST(identify_attr_list,
  58. &esp_light_ctx.identify_attr.identify_time);
  59. /* groups cluster attributes data */
  60. ZB_ZCL_DECLARE_GROUPS_ATTRIB_LIST(groups_attr_list,
  61. &esp_light_ctx.groups_attr.name_support);
  62. /* scenes cluster attribute data */
  63. ZB_ZCL_DECLARE_SCENES_ATTRIB_LIST(scenes_attr_list,
  64. &esp_light_ctx.scenes_attr.scene_count,
  65. &esp_light_ctx.scenes_attr.current_scene,
  66. &esp_light_ctx.scenes_attr.current_group,
  67. &esp_light_ctx.scenes_attr.scene_valid,
  68. &esp_light_ctx.scenes_attr.name_support);
  69. /* on/off cluster attributes data */
  70. ZB_ZCL_DECLARE_ON_OFF_ATTRIB_LIST(on_off_attr_list,
  71. &esp_light_ctx.on_off_attr.on_off);
  72. /********************* Declare device **************************/
  73. ZB_HA_DECLARE_ON_OFF_OUTPUT_CLUSTER_LIST(custom_light_clusters,
  74. on_off_attr_list,
  75. basic_attr_list,
  76. identify_attr_list,
  77. groups_attr_list,
  78. scenes_attr_list);
  79. ZB_HA_DECLARE_ON_OFF_OUTPUT_EP(custom_esp_light_ep,
  80. HA_ESP_LIGHT_ENDPOINT,
  81. custom_light_clusters);
  82. ZB_HA_DECLARE_ON_OFF_OUTPUT_CTX(esp_zb_light_ctx,
  83. custom_esp_light_ep);
  84. /********************* Define functions **************************/
  85. static void bdb_start_top_level_commissioning_cb(zb_uint8_t mode_mask)
  86. {
  87. if (!bdb_start_top_level_commissioning(mode_mask)) {
  88. ESP_LOGE(TAG, "In BDB commissioning, an error occurred (for example: the device has already been running)");
  89. }
  90. }
  91. /**
  92. * @brief Zigbee zboss stack event signal handler.
  93. *
  94. * @param bufid Zigbee zboss stack buffer id used to pass signal.
  95. */
  96. void zboss_signal_handler(zb_uint8_t bufid)
  97. {
  98. zb_uint8_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
  99. zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, NULL);
  100. switch (sig) {
  101. case ZB_ZDO_SIGNAL_SKIP_STARTUP:
  102. ESP_LOGI(TAG, "Zigbee stack initialized");
  103. bdb_start_top_level_commissioning(ZB_BDB_INITIALIZATION);
  104. break;
  105. case ZB_BDB_SIGNAL_DEVICE_FIRST_START:
  106. if (status == RET_OK) {
  107. ESP_LOGI(TAG, "Start network steering");
  108. bdb_start_top_level_commissioning(ZB_BDB_NETWORK_STEERING);
  109. } else {
  110. ESP_LOGE(TAG, "Failed to initialize Zigbee stack (status: %d)", status);
  111. }
  112. break;
  113. case ZB_BDB_SIGNAL_STEERING:
  114. if (status == RET_OK) {
  115. zb_ext_pan_id_t extended_pan_id;
  116. zb_get_extended_pan_id(extended_pan_id);
  117. ESP_LOGI(TAG, "Joined network successfully (Extended PAN ID: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx)",
  118. extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4],
  119. extended_pan_id[3], extended_pan_id[2], extended_pan_id[1], extended_pan_id[0],
  120. ZB_PIBCACHE_PAN_ID());
  121. } else {
  122. ESP_LOGI(TAG, "Network steering was not successful (status: %d)", status);
  123. ZB_SCHEDULE_APP_ALARM((zb_callback_t)bdb_start_top_level_commissioning_cb, ZB_BDB_NETWORK_STEERING, ZB_TIME_ONE_SECOND);
  124. }
  125. break;
  126. default:
  127. ESP_LOGI(TAG, "status: %d", status);
  128. break;
  129. }
  130. if (bufid) {
  131. zb_buf_free(bufid);
  132. }
  133. }
  134. /**
  135. * @brief Function for turning on/off the light bulb.
  136. *
  137. * @param power Boolean light bulb state power on/off.
  138. */
  139. static void esp_zb_light_set_value(zb_bool_t power)
  140. {
  141. ZB_ZCL_SET_ATTRIBUTE(HA_ESP_LIGHT_ENDPOINT,
  142. ZB_ZCL_CLUSTER_ID_ON_OFF,
  143. ZB_ZCL_CLUSTER_SERVER_ROLE,
  144. ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID,
  145. (zb_uint8_t *)&power,
  146. ZB_FALSE);
  147. light_driver_set_power(power);
  148. }
  149. /**
  150. * @brief Callback function for handling attribute value from ZCL.
  151. *
  152. * @param bufid Zigbee zboss stack buffer id used to pass received data.
  153. */
  154. static void esp_zb_light_cb(zb_bufid_t bufid)
  155. {
  156. zb_zcl_device_callback_param_t *p_device_cb_param = ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
  157. zb_zcl_device_callback_id_t device_cb_id = p_device_cb_param->device_cb_id;
  158. zb_uint16_t cluster_id = p_device_cb_param->cb_param.set_attr_value_param.cluster_id;
  159. zb_uint16_t attr_id = p_device_cb_param->cb_param.set_attr_value_param.attr_id;
  160. p_device_cb_param->status = RET_OK;
  161. switch (device_cb_id) {
  162. /* ZCL set attribute value */
  163. case ZB_ZCL_SET_ATTR_VALUE_CB_ID:
  164. if (cluster_id == ZB_ZCL_CLUSTER_ID_ON_OFF) {
  165. uint8_t value = p_device_cb_param->cb_param.set_attr_value_param.values.data8;
  166. ESP_LOGI(TAG, "on/off attribute setting to %hd", value);
  167. if (attr_id == ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID) {
  168. /* implemented light on/off control */
  169. esp_zb_light_set_value((zb_bool_t) value);
  170. }
  171. } else if (cluster_id == ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL) {
  172. uint16_t value = p_device_cb_param->cb_param.set_attr_value_param.values.data16;
  173. ESP_LOGI(TAG, "level control attribute setting to %hd", value);
  174. if (attr_id == ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID) {
  175. /* implement light level control if needed */
  176. }
  177. } else if (cluster_id == ZB_ZCL_CLUSTER_ID_COLOR_CONTROL) {
  178. uint16_t value = p_device_cb_param->cb_param.set_attr_value_param.values.data8;
  179. ESP_LOGI(TAG, "attribute 0x%x setting to %d", attr_id, value);
  180. if (attr_id == ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_HUE_ID) {
  181. /* implement light color change if needed */
  182. }
  183. } else {
  184. /* other clusters attribute handled here */
  185. ESP_LOGI(TAG, "Unhandled cluster attribute id: %d", cluster_id);
  186. }
  187. break;
  188. default:
  189. p_device_cb_param->status = RET_ERROR;
  190. break;
  191. }
  192. }
  193. void app_main()
  194. {
  195. zb_ret_t zb_err_code;
  196. zb_esp_platform_config_t config = {
  197. .radio_config = ZB_ESP_DEFAULT_RADIO_CONFIG(),
  198. .host_config = ZB_ESP_DEFAULT_HOST_CONFIG(),
  199. };
  200. ESP_ERROR_CHECK(zb_esp_platform_config(&config));
  201. /* initialize Zigbee stack */
  202. ZB_INIT("light_bulb");
  203. zb_set_network_router_role(IEEE_CHANNEL_MASK);
  204. zb_set_max_children(MAX_CHILDREN);
  205. zb_set_nvram_erase_at_start(ERASE_PERSISTENT_CONFIG);
  206. zb_set_keepalive_timeout(ZB_MILLISECONDS_TO_BEACON_INTERVAL(3000));
  207. /* hardware related and device init */
  208. light_driver_init(LIGHT_DEFAULT_OFF);
  209. /* register callback for handling ZCL commands */
  210. ZB_ZCL_REGISTER_DEVICE_CB(esp_zb_light_cb);
  211. /* register light device context (endpoints) */
  212. ZB_AF_REGISTER_DEVICE_CTX(&esp_zb_light_ctx);
  213. zb_err_code = zboss_start_no_autostart();
  214. ESP_ERROR_CHECK(zb_err_code);
  215. while (1) {
  216. zboss_main_loop_iteration();
  217. }
  218. }