Guest User

Untitled

a guest
Aug 29th, 2022
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2021 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
  5. */
  6.  
  7. /** @file
  8. *
  9. * @brief Zigbee application template.
  10. */
  11.  
  12. #include <zephyr/kernel.h>
  13. #include <zephyr/logging/log.h>
  14. #include <dk_buttons_and_leds.h>
  15.  
  16. #include <zboss_api.h>
  17. #include <zigbee/zigbee_error_handler.h>
  18. #include <zigbee/zigbee_app_utils.h>
  19. #include <zb_nrf_platform.h>
  20. #include "zb_range_extender.h"
  21.  
  22.  
  23. /* Device endpoint, used to receive ZCL commands. */
  24. #define APP_TEMPLATE_ENDPOINT 10
  25.  
  26. // Exemplary 11. endpoint will be used for On/Off Switch cluster
  27. #define ON_OFF_SWITCH_ENDPOINT 11
  28.  
  29. /* Type of power sources available for the device.
  30. * For possible values see section 3.2.2.2.8 of ZCL specification.
  31. */
  32. #define TEMPLATE_INIT_BASIC_POWER_SOURCE ZB_ZCL_BASIC_POWER_SOURCE_DC_SOURCE
  33.  
  34. /* LED indicating that device successfully joined Zigbee network. */
  35. #define ZIGBEE_NETWORK_STATE_LED DK_LED3
  36.  
  37. /* LED used for device identification. */
  38. #define IDENTIFY_LED DK_LED4
  39.  
  40. /* Button used to enter the Identify mode. */
  41. #define IDENTIFY_MODE_BUTTON DK_BTN4_MSK
  42.  
  43. /* Button to start Factory Reset */
  44. #define FACTORY_RESET_BUTTON IDENTIFY_MODE_BUTTON
  45.  
  46. LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
  47.  
  48. /* Main application customizable context.
  49. * Stores all settings and static values.
  50. */
  51. struct zb_device_ctx {
  52. zb_zcl_basic_attrs_t basic_attr;
  53. zb_zcl_identify_attrs_t identify_attr;
  54. zb_uint8_t on_off_switch_type_attr;
  55. zb_uint8_t on_off_switch_actions_attr;
  56. };
  57.  
  58. /* Zigbee device application context storage. */
  59. static struct zb_device_ctx dev_ctx;
  60.  
  61. ZB_ZCL_DECLARE_IDENTIFY_ATTRIB_LIST(
  62. identify_attr_list,
  63. &dev_ctx.identify_attr.identify_time);
  64.  
  65. ZB_ZCL_DECLARE_BASIC_ATTRIB_LIST(
  66. basic_attr_list,
  67. &dev_ctx.basic_attr.zcl_version,
  68. &dev_ctx.basic_attr.power_source);
  69.  
  70. ZB_ZCL_DECLARE_ON_OFF_SWITCH_CONFIGURATION_ATTRIB_LIST(
  71. on_off_switch_attr_list,
  72. &dev_ctx.on_off_switch_type_attr,
  73. &dev_ctx.on_off_switch_actions_attr);
  74.  
  75. ZB_HA_DECLARE_ON_OFF_SWITCH_CLUSTER_LIST(on_off_switch_clusters,
  76. on_off_switch_attr_list, basic_attr_list, identify_attr_list);
  77.  
  78. ZB_DECLARE_RANGE_EXTENDER_CLUSTER_LIST(
  79. app_template_clusters,
  80. basic_attr_list,
  81. identify_attr_list);
  82.  
  83. ZB_DECLARE_RANGE_EXTENDER_EP(
  84. app_template_ep,
  85. APP_TEMPLATE_ENDPOINT,
  86. app_template_clusters);
  87.  
  88.  
  89.  
  90. ZB_HA_DECLARE_ON_OFF_SWITCH_EP(on_off_switch_ep, ON_OFF_SWITCH_ENDPOINT, on_off_switch_clusters);
  91.  
  92. ZBOSS_DECLARE_DEVICE_CTX_2_EP(app_template_ctx, on_off_switch_ep, app_template_ep);
  93.  
  94.  
  95.  
  96. /**@brief Function for initializing all clusters attributes. */
  97. static void app_clusters_attr_init(void)
  98. {
  99. /* Basic cluster attributes data */
  100. dev_ctx.basic_attr.zcl_version = ZB_ZCL_VERSION;
  101. dev_ctx.basic_attr.power_source = TEMPLATE_INIT_BASIC_POWER_SOURCE;
  102.  
  103. /* Identify cluster attributes data. */
  104. dev_ctx.identify_attr.identify_time =
  105. ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE;
  106. }
  107.  
  108. /**@brief Function to toggle the identify LED
  109. *
  110. * @param bufid Unused parameter, required by ZBOSS scheduler API.
  111. */
  112. static void toggle_identify_led(zb_bufid_t bufid)
  113. {
  114. static int blink_status;
  115.  
  116. dk_set_led(IDENTIFY_LED, (++blink_status) % 2);
  117. ZB_SCHEDULE_APP_ALARM(toggle_identify_led, bufid, ZB_MILLISECONDS_TO_BEACON_INTERVAL(100));
  118. }
  119.  
  120. /**@brief Function to handle identify notification events on the first endpoint.
  121. *
  122. * @param bufid Unused parameter, required by ZBOSS scheduler API.
  123. */
  124. static void identify_cb(zb_bufid_t bufid)
  125. {
  126. zb_ret_t zb_err_code;
  127.  
  128. if (bufid) {
  129. /* Schedule a self-scheduling function that will toggle the LED */
  130. ZB_SCHEDULE_APP_CALLBACK(toggle_identify_led, bufid);
  131. } else {
  132. /* Cancel the toggling function alarm and turn off LED */
  133. zb_err_code = ZB_SCHEDULE_APP_ALARM_CANCEL(toggle_identify_led, ZB_ALARM_ANY_PARAM);
  134. ZVUNUSED(zb_err_code);
  135.  
  136. dk_set_led(IDENTIFY_LED, 0);
  137. }
  138. }
  139.  
  140. /**@breif Starts identifying the device.
  141. *
  142. * @param bufid Unused parameter, required by ZBOSS scheduler API.
  143. */
  144. static void start_identifying(zb_bufid_t bufid)
  145. {
  146. ZVUNUSED(bufid);
  147.  
  148. if (ZB_JOINED()) {
  149. /* Check if endpoint is in identifying mode,
  150. * if not put desired endpoint in identifying mode.
  151. */
  152. if (dev_ctx.identify_attr.identify_time ==
  153. ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE) {
  154.  
  155. zb_ret_t zb_err_code = zb_bdb_finding_binding_target(
  156. APP_TEMPLATE_ENDPOINT);
  157.  
  158. if (zb_err_code == RET_OK) {
  159. LOG_INF("Enter identify mode");
  160. } else if (zb_err_code == RET_INVALID_STATE) {
  161. LOG_WRN("RET_INVALID_STATE - Cannot enter identify mode");
  162. } else {
  163. ZB_ERROR_CHECK(zb_err_code);
  164. }
  165. } else {
  166. LOG_INF("Cancel identify mode");
  167. zb_bdb_finding_binding_target_cancel();
  168. }
  169. } else {
  170. LOG_WRN("Device not in a network - cannot enter identify mode");
  171. }
  172. }
  173.  
  174. /**@brief Callback for button events.
  175. *
  176. * @param[in] button_state Bitmask containing buttons state.
  177. * @param[in] has_changed Bitmask containing buttons
  178. * that have changed their state.
  179. */
  180. static void button_changed(uint32_t button_state, uint32_t has_changed)
  181. {
  182. if (IDENTIFY_MODE_BUTTON & has_changed) {
  183. if (IDENTIFY_MODE_BUTTON & button_state) {
  184. /* Button changed its state to pressed */
  185. } else {
  186. /* Button changed its state to released */
  187. if (was_factory_reset_done()) {
  188. /* The long press was for Factory Reset */
  189. LOG_DBG("After Factory Reset - ignore button release");
  190. } else {
  191. /* Button released before Factory Reset */
  192.  
  193. /* Start identification mode */
  194. ZB_SCHEDULE_APP_CALLBACK(start_identifying, 0);
  195. }
  196. }
  197. }
  198.  
  199. check_factory_reset_button(button_state, has_changed);
  200. }
  201.  
  202. /**@brief Function for initializing LEDs and Buttons. */
  203. static void configure_gpio(void)
  204. {
  205. int err;
  206.  
  207. err = dk_buttons_init(button_changed);
  208. if (err) {
  209. LOG_ERR("Cannot init buttons (err: %d)", err);
  210. }
  211.  
  212. err = dk_leds_init();
  213. if (err) {
  214. LOG_ERR("Cannot init LEDs (err: %d)", err);
  215. }
  216. }
  217.  
  218. /**@brief Zigbee stack event handler.
  219. *
  220. * @param[in] bufid Reference to the Zigbee stack buffer
  221. * used to pass signal.
  222. */
  223. void zboss_signal_handler(zb_bufid_t bufid)
  224. {
  225. /* Update network status LED. */
  226. zigbee_led_status_update(bufid, ZIGBEE_NETWORK_STATE_LED);
  227.  
  228. /* No application-specific behavior is required.
  229. * Call default signal handler.
  230. */
  231. ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
  232.  
  233. /* All callbacks should either reuse or free passed buffers.
  234. * If bufid == 0, the buffer is invalid (not passed).
  235. */
  236. if (bufid) {
  237. zb_buf_free(bufid);
  238. }
  239. }
  240.  
  241. void main(void)
  242. {
  243. LOG_INF("Starting Zigbee application template example");
  244.  
  245. /* Initialize */
  246. configure_gpio();
  247. register_factory_reset_button(FACTORY_RESET_BUTTON);
  248.  
  249. /* Register device context (endpoints). */
  250. ZB_AF_REGISTER_DEVICE_CTX(&app_template_ctx);
  251.  
  252. app_clusters_attr_init();
  253.  
  254. /* Register handlers to identify notifications */
  255. ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(APP_TEMPLATE_ENDPOINT, identify_cb);
  256.  
  257. /* Start Zigbee default thread */
  258. zigbee_enable();
  259.  
  260. LOG_INF("Zigbee application template started");
  261. }
  262.  
Advertisement
Add Comment
Please, Sign In to add comment