forked from nrfconnect/ncs-zigbee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_task_zigbee.c
More file actions
1020 lines (862 loc) · 28.2 KB
/
Copy pathapp_task_zigbee.c
File metadata and controls
1020 lines (862 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2024-2026 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
* @brief Dimmer switch for HA profile (Zigbee stack entry; Matter build shares the radio).
*
* Implemented in C because ZBOSS public macros use void * in ways that are not C++-compatible.
*/
#include "app_task_zigbee.h"
#include <zigbee/matter_protocol_state.h>
#include <zigbee/matter_coexistence.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
#include <dk_buttons_and_leds.h>
#include <ram_pwrdn.h>
#include <zboss_api.h>
#include <zboss_api_addons.h>
#include <zboss_api_zcl.h>
#include <zigbee/zigbee_app_utils.h>
#include <zigbee/zigbee_error_handler.h>
#include <zb_nrf_platform.h>
#include "zb_mem_config_custom.h"
#include "zb_dimmer_switch.h"
#if defined(CONFIG_LIGHT_SWITCH_CONFIGURE_TX_POWER)
#include <osif/mac_platform.h>
#endif
#if CONFIG_ZIGBEE_FOTA
#include <zigbee/zigbee_fota.h>
#include <zephyr/sys/reboot.h>
#endif /* CONFIG_ZIGBEE_FOTA */
#if defined(CONFIG_ZIGBEE_FOTA) || defined(CONFIG_ZIGBEE_BT_DFU)
#include <zephyr/dfu/mcuboot.h>
#endif
#if CONFIG_ZIGBEE_FOTA
/* LED indicating OTA Client Activity. */
#define OTA_ACTIVITY_LED DK_LED2
#endif /* CONFIG_ZIGBEE_FOTA */
#ifdef CONFIG_ZIGBEE_BT_DFU
#include <zigbee/zigbee_bt_dfu.h>
#endif
#if CONFIG_BT_NUS
#include "nus_cmd.h"
/* LED which indicates that Central is connected. */
#define NUS_STATUS_LED DK_LED1
/* UART command that will turn on found light bulb(s). */
#define COMMAND_ON "n"
/**< UART command that will turn off found light bulb(s). */
#define COMMAND_OFF "f"
/**< UART command that will turn toggle found light bulb(s). */
#define COMMAND_TOGGLE "t"
/**< UART command that will increase brightness of found light bulb(s). */
#define COMMAND_INCREASE "i"
/**< UART command that will decrease brightness of found light bulb(s). */
#define COMMAND_DECREASE "d"
#endif /* CONFIG_BT_NUS */
/* Source endpoint used to control light bulb. */
#define LIGHT_SWITCH_ENDPOINT 1
/* Delay between the light switch startup and light bulb finding procedure. */
#define MATCH_DESC_REQ_START_DELAY K_SECONDS(2)
/* Timeout for finding procedure. */
#define MATCH_DESC_REQ_TIMEOUT K_SECONDS(5)
/* Find only non-sleepy device. */
#define MATCH_DESC_REQ_ROLE ZB_NWK_BROADCAST_RX_ON_WHEN_IDLE
/* Do not erase NVRAM to save the network parameters after device reboot or
* power-off. NOTE: If this option is set to ZB_TRUE then do full device erase
* for all network devices before running other samples.
*/
#define ERASE_PERSISTENT_CONFIG ZB_FALSE
/* LED indicating that light switch successfully joind Zigbee network. */
#define ZIGBEE_NETWORK_STATE_LED DK_LED3
/* LED used for device identification. */
#define IDENTIFY_LED ZIGBEE_NETWORK_STATE_LED
/* LED indicating that light witch found a light bulb to control. */
#define BULB_FOUND_LED DK_LED4
/* Button ID used to switch on the light bulb. */
#define BUTTON_ON DK_BTN1_MSK
/* Button ID used to switch off the light bulb. */
#define BUTTON_OFF DK_BTN2_MSK
/* Dim step size - increases/decreses current level (range 0x000 - 0xfe). */
#define DIMM_STEP 15
/* Button ID used to enable sleepy behavior (sampled once at boot). */
#define BUTTON_SLEEPY DK_BTN3_MSK
#if defined(CONFIG_ZIGBEE_TOUCHLINK_INITIATOR)
/* Start Touchlink initiator at runtime (same pin as BUTTON_SLEEPY). */
#define BUTTON_TOUCHLINK DK_BTN3_MSK
#endif
#if defined(CONFIG_ZIGBEE_MATTER_COEXISTENCE_BUTTON_SWITCH)
/* Long-press to switch active protocol. */
#define PROTOCOL_SWITCH_BUTTON DK_BTN3_MSK
#endif
/* Button to start Factory Reset */
#define FACTORY_RESET_BUTTON DK_BTN4_MSK
/* Button used to enter the Identify mode. */
#define IDENTIFY_MODE_BUTTON DK_BTN4_MSK
/* Transition time for a single step operation in 0.1 sec units.
* 0xFFFF - immediate change.
*/
#define DIMM_TRANSACTION_TIME 2
/* Time after which the button state is checked again to detect button hold,
* the dimm command is sent again.
*/
#define BUTTON_LONG_POLL_TMO K_MSEC(500)
#if !defined ZB_ED_ROLE
#error Define ZB_ED_ROLE to compile light switch (End Device) source code.
#endif
LOG_MODULE_REGISTER(zb_lightswitch, LOG_LEVEL_INF);
/* Helper functions for LED control in low power mode */
#if defined(CONFIG_LIGHT_SWITCH_LOW_POWER)
static inline void led_set(uint32_t led, uint32_t val) { ARG_UNUSED(led); ARG_UNUSED(val); }
static inline void led_set_on(uint32_t led) { ARG_UNUSED(led); }
static inline void led_set_off(uint32_t led) { ARG_UNUSED(led); }
#else
static inline void led_set(uint32_t led, uint32_t val) { dk_set_led(led, val); }
static inline void led_set_on(uint32_t led) { dk_set_led_on(led); }
static inline void led_set_off(uint32_t led) { dk_set_led_off(led); }
#endif
struct bulb_context {
zb_uint8_t endpoint;
zb_uint16_t short_addr;
struct k_timer find_alarm;
};
struct buttons_context {
uint32_t state;
atomic_t long_poll;
struct k_timer alarm;
};
struct zb_device_ctx {
zb_zcl_basic_attrs_t basic_attr;
zb_zcl_identify_attrs_t identify_attr;
};
static struct bulb_context bulb_ctx;
static struct buttons_context buttons_ctx;
static struct zb_device_ctx dev_ctx;
/* Declare attribute list for Basic cluster (server). */
ZB_ZCL_DECLARE_BASIC_SERVER_ATTRIB_LIST(
basic_server_attr_list,
&dev_ctx.basic_attr.zcl_version,
&dev_ctx.basic_attr.power_source);
/* Declare attribute list for Identify cluster (client). */
ZB_ZCL_DECLARE_IDENTIFY_CLIENT_ATTRIB_LIST(
identify_client_attr_list);
/* Declare attribute list for Identify cluster (server). */
ZB_ZCL_DECLARE_IDENTIFY_SERVER_ATTRIB_LIST(
identify_server_attr_list,
&dev_ctx.identify_attr.identify_time);
/* Declare attribute list for Scenes cluster (client). */
ZB_ZCL_DECLARE_SCENES_CLIENT_ATTRIB_LIST(
scenes_client_attr_list);
/* Declare attribute list for Groups cluster (client). */
ZB_ZCL_DECLARE_GROUPS_CLIENT_ATTRIB_LIST(
groups_client_attr_list);
/* Declare attribute list for On/Off cluster (client). */
ZB_ZCL_DECLARE_ON_OFF_CLIENT_ATTRIB_LIST(
on_off_client_attr_list);
/* Declare attribute list for Level control cluster (client). */
ZB_ZCL_DECLARE_LEVEL_CONTROL_CLIENT_ATTRIB_LIST(
level_control_client_attr_list);
/* Declare cluster list for Dimmer Switch device. */
ZB_DECLARE_DIMMER_SWITCH_CLUSTER_LIST(
dimmer_switch_clusters,
basic_server_attr_list,
identify_client_attr_list,
identify_server_attr_list,
scenes_client_attr_list,
groups_client_attr_list,
on_off_client_attr_list,
level_control_client_attr_list);
/* Declare endpoint for Dimmer Switch device. */
ZB_DECLARE_DIMMER_SWITCH_EP(
dimmer_switch_ep,
LIGHT_SWITCH_ENDPOINT,
dimmer_switch_clusters);
/* Declare application's device context (list of registered endpoints)
* for Dimmer Switch device.
*/
#ifndef CONFIG_ZIGBEE_FOTA
ZBOSS_DECLARE_DEVICE_CTX_1_EP(dimmer_switch_ctx, dimmer_switch_ep);
#else
#if LIGHT_SWITCH_ENDPOINT == CONFIG_ZIGBEE_FOTA_ENDPOINT
#error "Light switch and Zigbee OTA endpoints should be different."
#endif
extern zb_af_endpoint_desc_t zigbee_fota_client_ep;
ZBOSS_DECLARE_DEVICE_CTX_2_EP(dimmer_switch_ctx,
zigbee_fota_client_ep,
dimmer_switch_ep);
#endif /* CONFIG_ZIGBEE_FOTA */
/* Forward declarations. */
static void light_switch_button_handler(struct k_timer *timer);
static void find_light_bulb_alarm(struct k_timer *timer);
static void find_light_bulb(zb_bufid_t bufid);
static void light_switch_send_on_off(zb_bufid_t bufid, zb_uint16_t on_off);
/**@brief Starts identifying the device.
*
* @param bufid Unused parameter, required by ZBOSS scheduler API.
*/
static void start_identifying(zb_bufid_t bufid)
{
ZVUNUSED(bufid);
if (ZB_JOINED()) {
/* Check if endpoint is in identifying mode,
* if not, put desired endpoint in identifying mode.
*/
if (dev_ctx.identify_attr.identify_time ==
ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE) {
zb_ret_t zb_err_code = zb_bdb_finding_binding_target(LIGHT_SWITCH_ENDPOINT);
if (zb_err_code == RET_OK) {
LOG_INF("Enter identify mode");
} else if (zb_err_code == RET_INVALID_STATE) {
LOG_WRN("RET_INVALID_STATE - Cannot enter identify mode");
} else {
ZB_ERROR_CHECK(zb_err_code);
}
} else {
LOG_INF("Cancel identify mode");
zb_bdb_finding_binding_target_cancel();
}
} else {
LOG_WRN("Device not in a network - cannot enter identify mode");
}
}
#if defined(CONFIG_ZIGBEE_TOUCHLINK_INITIATOR)
static void light_switch_touchlink_initiator_start_cb(zb_bufid_t bufid)
{
ZVUNUSED(bufid);
LOG_INF("Starting Touchlink initiator");
zigbee_network_join_commissioning_set_active(true);
zigbee_touchlink_initiator_prepare_scan_channels();
if (!bdb_start_top_level_commissioning(ZB_BDB_TOUCHLINK_COMMISSIONING)) {
LOG_WRN("Touchlink commissioning rejected (already in progress?)");
}
}
#endif
/**@brief Implementation of button event handling for Zigbee.
*
* @param[in] button_state Bitmask containing buttons state.
* @param[in] has_changed Bitmask containing buttons that has
* changed their state.
*/
static void zb_button_handler_impl(uint32_t button_state, uint32_t has_changed)
{
zb_uint16_t cmd_id;
zb_ret_t zb_err_code;
#ifdef CONFIG_ZIGBEE_MATTER_COEXISTENCE
#ifdef CONFIG_ZIGBEE_MATTER_COEXISTENCE_BUTTON_SWITCH
(void)zigbee_matter_coexistence_process_switch_button(button_state, has_changed,
PROTOCOL_SWITCH_BUTTON);
#endif
if (!protocol_is_zigbee_active()) {
return;
}
#endif
/* Inform default signal handler about user input at the device. */
user_input_indicate();
check_factory_reset_button(button_state, has_changed);
#if defined(CONFIG_ZIGBEE_TOUCHLINK_INITIATOR) && !defined(CONFIG_ZIGBEE_MATTER_COEXISTENCE)
if ((has_changed & BUTTON_TOUCHLINK) && (button_state & BUTTON_TOUCHLINK)) {
ZB_SCHEDULE_APP_CALLBACK(light_switch_touchlink_initiator_start_cb, 0);
return;
}
#endif
if (bulb_ctx.short_addr == 0xFFFF) {
LOG_DBG("No bulb found yet.");
return;
}
switch (has_changed) {
case BUTTON_ON:
LOG_DBG("ON - button changed");
cmd_id = ZB_ZCL_CMD_ON_OFF_ON_ID;
break;
case BUTTON_OFF:
LOG_DBG("OFF - button changed");
cmd_id = ZB_ZCL_CMD_ON_OFF_OFF_ID;
break;
case IDENTIFY_MODE_BUTTON:
if (IDENTIFY_MODE_BUTTON & button_state) {
/* Button changed its state to pressed */
} else {
/* Button changed its state to released */
if (was_factory_reset_done()) {
/* The long press was for Factory Reset */
LOG_DBG("After Factory Reset - ignore button release");
} else {
/* Button released before Factory Reset */
/* Start identification mode */
ZB_SCHEDULE_APP_CALLBACK(start_identifying, 0);
}
}
return;
default:
LOG_DBG("Unhandled button");
return;
}
switch (button_state) {
case BUTTON_ON:
case BUTTON_OFF:
LOG_DBG("Button pressed");
buttons_ctx.state = button_state;
/* Alarm can be scheduled only once. Next alarm only resets
* counting.
*/
k_timer_start(&buttons_ctx.alarm, BUTTON_LONG_POLL_TMO,
K_NO_WAIT);
break;
case 0:
LOG_DBG("Button released");
k_timer_stop(&buttons_ctx.alarm);
if (atomic_set(&buttons_ctx.long_poll, ZB_FALSE) == ZB_FALSE) {
/* Allocate output buffer and send on/off command. */
zb_err_code = zb_buf_get_out_delayed_ext(
light_switch_send_on_off, cmd_id, 0);
ZB_ERROR_CHECK(zb_err_code);
}
break;
default:
break;
}
}
#ifdef CONFIG_ZIGBEE_MATTER_COEXISTENCE
void zb_button_handler(uint32_t button_state, uint32_t has_changed)
{
zb_button_handler_impl(button_state, has_changed);
}
void zb_register_button_handler(void)
{
static struct button_handler handler = {
.cb = zb_button_handler,
};
dk_button_handler_add(&handler);
}
#else
/**@brief Callback wrapper for button events (non-Matter builds). */
static void button_handler(uint32_t button_state, uint32_t has_changed)
{
zb_button_handler_impl(button_state, has_changed);
}
/**@brief Function for initializing LEDs and Buttons. */
static void configure_gpio(void)
{
int err;
err = dk_buttons_init(button_handler);
if (err) {
LOG_ERR("Cannot init buttons (err: %d)", err);
}
#if !defined(CONFIG_LIGHT_SWITCH_LOW_POWER)
err = dk_leds_init();
if (err) {
LOG_ERR("Cannot init LEDs (err: %d)", err);
}
#endif
}
#endif /* CONFIG_ZIGBEE_MATTER_COEXISTENCE */
static void alarm_timers_init(void)
{
k_timer_init(&buttons_ctx.alarm, light_switch_button_handler, NULL);
k_timer_init(&bulb_ctx.find_alarm, find_light_bulb_alarm, NULL);
}
/**@brief Function for initializing all clusters attributes. */
static void app_clusters_attr_init(void)
{
/* Basic cluster attributes data. */
dev_ctx.basic_attr.zcl_version = ZB_ZCL_VERSION;
dev_ctx.basic_attr.power_source = ZB_ZCL_BASIC_POWER_SOURCE_UNKNOWN;
/* Identify cluster attributes data. */
dev_ctx.identify_attr.identify_time = ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE;
}
/**@brief Function to toggle the identify LED.
*
* @param bufid Unused parameter, required by ZBOSS scheduler API.
*/
static void toggle_identify_led(zb_bufid_t bufid)
{
static int blink_status;
led_set(IDENTIFY_LED, (++blink_status) % 2);
ZB_SCHEDULE_APP_ALARM(toggle_identify_led, bufid, ZB_MILLISECONDS_TO_BEACON_INTERVAL(100));
}
/**@brief Function to handle identify notification events on the first endpoint.
*
* @param bufid Unused parameter, required by ZBOSS scheduler API.
*/
static void identify_cb(zb_bufid_t bufid)
{
zb_ret_t zb_err_code;
if (bufid) {
/* Schedule a self-scheduling function that will toggle the LED. */
ZB_SCHEDULE_APP_CALLBACK(toggle_identify_led, bufid);
} else {
/* Cancel the toggling function alarm and turn off LED. */
zb_err_code = ZB_SCHEDULE_APP_ALARM_CANCEL(toggle_identify_led, ZB_ALARM_ANY_PARAM);
ZVUNUSED(zb_err_code);
/* Update network status/idenitfication LED. */
if (ZB_JOINED()) {
led_set_on(ZIGBEE_NETWORK_STATE_LED);
} else {
led_set_off(ZIGBEE_NETWORK_STATE_LED);
}
}
}
/**@brief Function for sending ON/OFF requests to the light bulb.
*
* @param[in] bufid Non-zero reference to Zigbee stack buffer that will be
* used to construct on/off request.
* @param[in] cmd_id ZCL command id.
*/
static void light_switch_send_on_off(zb_bufid_t bufid, zb_uint16_t cmd_id)
{
LOG_INF("Send ON/OFF command: %d", cmd_id);
ZB_ZCL_ON_OFF_SEND_REQ(bufid,
bulb_ctx.short_addr,
ZB_APS_ADDR_MODE_16_ENDP_PRESENT,
bulb_ctx.endpoint,
LIGHT_SWITCH_ENDPOINT,
ZB_AF_HA_PROFILE_ID,
ZB_ZCL_DISABLE_DEFAULT_RESPONSE,
cmd_id,
NULL);
}
/**@brief Function for sending step requests to the light bulb.
*
* @param[in] bufid Non-zero reference to Zigbee stack buffer that
* will be used to construct step request.
* @param[in] cmd_id ZCL command id.
*/
static void light_switch_send_step(zb_bufid_t bufid, zb_uint16_t cmd_id)
{
LOG_INF("Send step level command: %d", cmd_id);
ZB_ZCL_LEVEL_CONTROL_SEND_STEP_REQ(bufid,
bulb_ctx.short_addr,
ZB_APS_ADDR_MODE_16_ENDP_PRESENT,
bulb_ctx.endpoint,
LIGHT_SWITCH_ENDPOINT,
ZB_AF_HA_PROFILE_ID,
ZB_ZCL_DISABLE_DEFAULT_RESPONSE,
NULL,
cmd_id,
DIMM_STEP,
DIMM_TRANSACTION_TIME);
}
/**@brief Callback function receiving finding procedure results.
*
* @param[in] bufid Reference to Zigbee stack buffer used to pass
* received data.
*/
static void find_light_bulb_cb(zb_bufid_t bufid)
{
/* Get the beginning of the response. */
zb_zdo_match_desc_resp_t *resp =
(zb_zdo_match_desc_resp_t *) zb_buf_begin(bufid);
/* Get the pointer to the parameters buffer, which stores APS layer
* response.
*/
zb_apsde_data_indication_t *ind = ZB_BUF_GET_PARAM(bufid,
zb_apsde_data_indication_t);
zb_uint8_t *match_ep;
if ((resp->status == ZB_ZDP_STATUS_SUCCESS) &&
(resp->match_len > 0) &&
(bulb_ctx.short_addr == 0xFFFF)) {
/* Match EP list follows right after response header. */
match_ep = (zb_uint8_t *)(resp + 1);
/* We are searching for exact cluster, so only 1 EP
* may be found.
*/
bulb_ctx.endpoint = *match_ep;
bulb_ctx.short_addr = ind->src_addr;
LOG_INF("Found bulb addr: %d ep: %d",
bulb_ctx.short_addr,
bulb_ctx.endpoint);
k_timer_stop(&bulb_ctx.find_alarm);
led_set_on(BULB_FOUND_LED);
} else if (bulb_ctx.short_addr != 0xFFFF) {
LOG_DBG("Match descriptor response ignored (bulb already selected)");
} else if ((resp->status != ZB_ZDP_STATUS_SUCCESS) || (resp->match_len == 0)) {
LOG_INF("Bulb not found, try again");
}
if (bufid) {
zb_buf_free(bufid);
}
}
/**@brief Find bulb allarm handler.
*
* @param[in] timer Address of timer.
*/
static void find_light_bulb_alarm(struct k_timer *timer)
{
#ifdef CONFIG_ZIGBEE_MATTER_COEXISTENCE
if (!protocol_is_zigbee_active()) {
return;
}
#endif
ZB_ERROR_CHECK(zb_buf_get_out_delayed(find_light_bulb));
}
/**@brief Function for sending ON/OFF and Level Control find request.
*
* @param[in] bufid Reference to Zigbee stack buffer that will be used to
* construct find request.
*/
static void find_light_bulb(zb_bufid_t bufid)
{
zb_zdo_match_desc_param_t *req;
zb_uint8_t tsn = ZB_ZDO_INVALID_TSN;
/* Initialize pointers inside buffer and reserve space for
* zb_zdo_match_desc_param_t request.
*/
req = zb_buf_initial_alloc(bufid,
sizeof(zb_zdo_match_desc_param_t) + (1) * sizeof(zb_uint16_t));
req->nwk_addr = MATCH_DESC_REQ_ROLE;
req->addr_of_interest = MATCH_DESC_REQ_ROLE;
req->profile_id = ZB_AF_HA_PROFILE_ID;
/* We are searching for 2 clusters: On/Off and Level Control Server. */
req->num_in_clusters = 2;
req->num_out_clusters = 0;
req->cluster_list[0] = ZB_ZCL_CLUSTER_ID_ON_OFF;
req->cluster_list[1] = ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL;
/* Set 0xFFFF to reset short address in order to parse
* only one response.
*/
bulb_ctx.short_addr = 0xFFFF;
tsn = zb_zdo_match_desc_req(bufid, find_light_bulb_cb);
/* Free buffer if failed to send a request. */
if (tsn == ZB_ZDO_INVALID_TSN) {
zb_buf_free(bufid);
LOG_ERR("Failed to send Match Descriptor request");
}
}
/**@brief Callback for detecting button press duration.
*
* @param[in] timer Address of timer.
*/
static void light_switch_button_handler(struct k_timer *timer)
{
zb_ret_t zb_err_code;
zb_uint16_t cmd_id;
if (dk_get_buttons() & buttons_ctx.state) {
atomic_set(&buttons_ctx.long_poll, ZB_TRUE);
if (buttons_ctx.state == BUTTON_ON) {
cmd_id = ZB_ZCL_LEVEL_CONTROL_STEP_MODE_UP;
} else {
cmd_id = ZB_ZCL_LEVEL_CONTROL_STEP_MODE_DOWN;
}
/* Allocate output buffer and send step command. */
zb_err_code = zb_buf_get_out_delayed_ext(light_switch_send_step,
cmd_id,
0);
if (zb_err_code != RET_OK) {
LOG_ERR("Failed to schedule buffer allocation: %d", zb_err_code);
}
k_timer_start(&buttons_ctx.alarm, BUTTON_LONG_POLL_TMO,
K_NO_WAIT);
} else {
atomic_set(&buttons_ctx.long_poll, ZB_FALSE);
}
}
#if defined(CONFIG_ZIGBEE_FOTA) || defined(CONFIG_ZIGBEE_BT_DFU)
static void confirm_image(void)
{
if (!boot_is_img_confirmed()) {
int ret = boot_write_img_confirmed();
if (ret) {
LOG_ERR("Couldn't confirm image: %d", ret);
} else {
LOG_INF("Marked image as OK");
}
}
}
#endif
#ifdef CONFIG_ZIGBEE_FOTA
static void ota_evt_handler(const struct zigbee_fota_evt *evt)
{
switch (evt->id) {
case ZIGBEE_FOTA_EVT_PROGRESS:
led_set(OTA_ACTIVITY_LED, evt->dl.progress % 2);
break;
case ZIGBEE_FOTA_EVT_FINISHED:
LOG_INF("Reboot application.");
/* Power on unused sections of RAM to allow MCUboot to use it. */
if (IS_ENABLED(CONFIG_RAM_POWER_DOWN_LIBRARY)) {
power_up_unused_ram();
}
sys_reboot(SYS_REBOOT_COLD);
break;
case ZIGBEE_FOTA_EVT_ERROR:
LOG_ERR("OTA image transfer failed.");
break;
default:
break;
}
}
/**@brief Callback function for handling ZCL commands.
*
* @param[in] bufid Reference to Zigbee stack buffer
* used to pass received data.
*/
static void zcl_device_cb(zb_bufid_t bufid)
{
zb_zcl_device_callback_param_t *device_cb_param =
ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
if (device_cb_param->device_cb_id == ZB_ZCL_OTA_UPGRADE_VALUE_CB_ID) {
zigbee_fota_zcl_cb(bufid);
} else {
device_cb_param->status = RET_NOT_IMPLEMENTED;
}
}
#endif /* CONFIG_ZIGBEE_FOTA */
/**@brief Zigbee stack event handler.
*
* @param[in] bufid Reference to the Zigbee stack buffer
* used to pass signal.
*/
void zboss_signal_handler(zb_bufid_t bufid)
{
zb_zdo_app_signal_hdr_t *sig_hndler = NULL;
zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, &sig_hndler);
zb_ret_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
/* Update network status LED. */
zigbee_led_status_update(bufid, ZIGBEE_NETWORK_STATE_LED);
#ifdef CONFIG_ZIGBEE_FOTA
/* Pass signal to the OTA client implementation. */
zigbee_fota_signal_handler(bufid);
#endif /* CONFIG_ZIGBEE_FOTA */
switch (sig) {
#if defined(CONFIG_ZIGBEE_TOUCHLINK_INITIATOR)
case ZB_BDB_SIGNAL_TOUCHLINK:
ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
if (status == RET_OK) {
LOG_INF("Touchlink done: start finding bulb");
zb_zdo_pim_set_long_poll_interval(3000);
if (bulb_ctx.short_addr == 0xFFFF) {
k_timer_start(&bulb_ctx.find_alarm,
MATCH_DESC_REQ_START_DELAY,
MATCH_DESC_REQ_TIMEOUT);
}
}
break;
#endif /* CONFIG_ZIGBEE_TOUCHLINK_INITIATOR */
case ZB_BDB_SIGNAL_DEVICE_REBOOT:
/* fall-through */
case ZB_BDB_SIGNAL_STEERING:
/* Call default signal handler. */
ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
if (status == RET_OK) {
zb_zdo_pim_set_long_poll_interval(3000);
/* Check the light device address. */
if (bulb_ctx.short_addr == 0xFFFF) {
k_timer_start(&bulb_ctx.find_alarm,
MATCH_DESC_REQ_START_DELAY,
MATCH_DESC_REQ_TIMEOUT);
}
}
break;
case ZB_ZDO_SIGNAL_LEAVE:
/* If device leaves the network, reset bulb short_addr. */
if (status == RET_OK) {
zb_zdo_signal_leave_params_t *leave_params =
ZB_ZDO_SIGNAL_GET_PARAMS(sig_hndler, zb_zdo_signal_leave_params_t);
if (leave_params->leave_type == ZB_NWK_LEAVE_TYPE_RESET) {
bulb_ctx.short_addr = 0xFFFF;
}
}
/* Call default signal handler. */
ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
break;
default:
/* Call default signal handler. */
ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
break;
}
if (bufid) {
zb_buf_free(bufid);
}
}
#if CONFIG_BT_NUS
static void turn_on_cmd(struct k_work *item)
{
ARG_UNUSED(item);
zb_buf_get_out_delayed_ext(light_switch_send_on_off,
ZB_ZCL_CMD_ON_OFF_ON_ID, 0);
}
static void turn_off_cmd(struct k_work *item)
{
ARG_UNUSED(item);
zb_buf_get_out_delayed_ext(light_switch_send_on_off,
ZB_ZCL_CMD_ON_OFF_OFF_ID, 0);
}
static void toggle_cmd(struct k_work *item)
{
ARG_UNUSED(item);
zb_buf_get_out_delayed_ext(light_switch_send_on_off,
ZB_ZCL_CMD_ON_OFF_TOGGLE_ID, 0);
}
static void increase_cmd(struct k_work *item)
{
ARG_UNUSED(item);
zb_buf_get_out_delayed_ext(light_switch_send_step,
ZB_ZCL_LEVEL_CONTROL_STEP_MODE_UP, 0);
}
static void decrease_cmd(struct k_work *item)
{
ARG_UNUSED(item);
zb_buf_get_out_delayed_ext(light_switch_send_step,
ZB_ZCL_LEVEL_CONTROL_STEP_MODE_DOWN, 0);
}
static void on_nus_connect(struct k_work *item)
{
ARG_UNUSED(item);
led_set_on(NUS_STATUS_LED);
}
static void on_nus_disconnect(struct k_work *item)
{
ARG_UNUSED(item);
led_set_off(NUS_STATUS_LED);
}
static struct nus_entry commands[] = {
NUS_COMMAND(COMMAND_ON, turn_on_cmd),
NUS_COMMAND(COMMAND_OFF, turn_off_cmd),
NUS_COMMAND(COMMAND_TOGGLE, toggle_cmd),
NUS_COMMAND(COMMAND_INCREASE, increase_cmd),
NUS_COMMAND(COMMAND_DECREASE, decrease_cmd),
NUS_COMMAND(NULL, NULL),
};
#endif /* CONFIG_BT_NUS */
#if defined(CONFIG_LIGHT_SWITCH_CONFIGURE_TX_POWER)
/**@brief Callback for TX power setting result.
*
* @param param Reference to Zigbee stack buffer.
*/
static void tx_power_cb(zb_bufid_t param)
{
char *status = NULL;
zb_tx_power_params_t *power_params = zb_buf_begin(param);
switch (power_params->status) {
case RET_OK:
status = "success";
break;
case RET_INVALID_PARAMETER_1:
status = "invalid page";
break;
case RET_INVALID_PARAMETER_2:
status = "invalid channel";
break;
case RET_INVALID_PARAMETER_3:
status = "invalid tx power";
break;
default:
status = "unknown";
break;
}
LOG_INF("Zigbee transceiver tx power set result (pg: %d, ch: %d, pw: %d): %s",
power_params->page, power_params->channel, power_params->tx_power, status);
zb_buf_free(param);
}
/**@brief Function for scheduling TX power set.
*
* @param param Reference to Zigbee stack buffer.
* @param channel Channel number.
*/
static void schedule_tx_power_set(zb_bufid_t param, zb_uint16_t channel)
{
zb_tx_power_params_t *power_params;
power_params = zb_buf_initial_alloc(param, sizeof(zb_tx_power_params_t));
power_params->page = ZB_CHANNEL_PAGE0_2_4_GHZ;
power_params->channel = channel;
power_params->tx_power = CONFIG_LIGHT_SWITCH_TX_POWER;
power_params->cb = tx_power_cb;
ZB_SCHEDULE_APP_CALLBACK(zb_set_tx_power_async, param);
}
/**@brief Function for setting TX power for configured channels.
*/
void set_tx_power(void)
{
zb_ret_t ret;
uint32_t channel_mask;
uint8_t channel = ZB_TRANSCEIVER_START_CHANNEL_NUMBER;
#if defined(CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_SINGLE)
channel_mask = 1 << CONFIG_ZIGBEE_CHANNEL;
#elif defined(CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_MULTI)
channel_mask = CONFIG_ZIGBEE_CHANNEL_MASK;
#endif
#if defined(CONFIG_ZIGBEE_TOUCHLINK_INITIATOR)
channel_mask |= zigbee_touchlink_initiator_zll_primary_channel_mask();
#endif
for (; channel <= ZB_TRANSCEIVER_MAX_CHANNEL_NUMBER; channel++) {
if (channel_mask & (1 << channel)) {
LOG_INF("Setting tx power for channel %d: %d dBm", channel,
CONFIG_LIGHT_SWITCH_TX_POWER);
ret = zb_buf_get_out_delayed_ext(schedule_tx_power_set, channel, 0);
if (ret != RET_OK) {
LOG_ERR("Failed to set tx power for channel %d, error %d",
channel, ret);
}
}
}
}
#endif /* CONFIG_LIGHT_SWITCH_CONFIGURE_TX_POWER */
int ZigbeeStart(void)
{
LOG_INF("Starting Zigbee R23 Light Switch example");
#ifndef CONFIG_CHIP
configure_gpio();
register_factory_reset_button(FACTORY_RESET_BUTTON);
#endif
alarm_timers_init();
zigbee_erase_persistent_storage(ERASE_PERSISTENT_CONFIG);
zb_set_ed_timeout(ED_AGING_TIMEOUT_64MIN);
zb_set_keepalive_timeout(ZB_MILLISECONDS_TO_BEACON_INTERVAL(30000));
/* Set default bulb short_addr. */
bulb_ctx.short_addr = 0xFFFF;
/* Check if sleepy button is pressed during Zigbee initialization
* and enable sleepy behavior. In low power mode, always enable sleepy behavior.
*/
#if defined(CONFIG_LIGHT_SWITCH_LOW_POWER)
bool enable_sleepy = true;
#elif defined(CONFIG_CHIP)
bool enable_sleepy = false;
#else
bool enable_sleepy = (dk_get_buttons() & BUTTON_SLEEPY) ? true : false;
#endif
if (enable_sleepy) {
zigbee_configure_sleepy_behavior(true);
}
/* Power off unused sections of RAM to lower device power consumption. */
if (IS_ENABLED(CONFIG_RAM_POWER_DOWN_LIBRARY)) {
power_down_unused_ram();
}
#if defined(CONFIG_ZIGBEE_FOTA) || defined(CONFIG_ZIGBEE_BT_DFU)
/* Mark the current firmware as valid. */
confirm_image();
#endif
#ifdef CONFIG_ZIGBEE_FOTA
/* Initialize Zigbee FOTA download service. */
zigbee_fota_init(ota_evt_handler);
/* Register callback for handling ZCL commands. */
ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb);
#endif /* CONFIG_ZIGBEE_FOTA */
#ifdef CONFIG_ZIGBEE_BT_DFU
zigbee_bt_dfu_init();
#endif
/* Register dimmer switch device context (endpoints). */
ZB_AF_REGISTER_DEVICE_CTX(&dimmer_switch_ctx);
app_clusters_attr_init();
/* Register handlers to identify notifications */
ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(LIGHT_SWITCH_ENDPOINT, identify_cb);
#ifdef CONFIG_ZIGBEE_FOTA
ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(CONFIG_ZIGBEE_FOTA_ENDPOINT, identify_cb);
#endif /* CONFIG_ZIGBEE_FOTA */