/
main.c
567 lines (470 loc) · 15.5 KB
/
main.c
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
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
*
* @brief Simple Zigbee light bulb implementation.
*/
#include <zephyr/types.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <soc.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/logging/log.h>
#include <dk_buttons_and_leds.h>
#include <zephyr/settings/settings.h>
#include <zboss_api.h>
#include <zboss_api_addons.h>
#include <zb_mem_config_med.h>
#include <zigbee/zigbee_app_utils.h>
#include <zigbee/zigbee_error_handler.h>
#include <zigbee/zigbee_zcl_scenes.h>
#include <zb_nrf_platform.h>
#include "zb_dimmable_light.h"
#define RUN_STATUS_LED DK_LED1
#define RUN_LED_BLINK_INTERVAL 1000
/* Device endpoint, used to receive light controlling commands. */
#define DIMMABLE_LIGHT_ENDPOINT 10
/* Version of the application software (1 byte). */
#define BULB_INIT_BASIC_APP_VERSION 01
/* Version of the implementation of the Zigbee stack (1 byte). */
#define BULB_INIT_BASIC_STACK_VERSION 10
/* Version of the hardware of the device (1 byte). */
#define BULB_INIT_BASIC_HW_VERSION 11
/* Manufacturer name (32 bytes). */
#define BULB_INIT_BASIC_MANUF_NAME "Nordic"
/* Model number assigned by manufacturer (32-bytes long string). */
#define BULB_INIT_BASIC_MODEL_ID "Dimable_Light_v0.1"
/* First 8 bytes specify the date of manufacturer of the device
* in ISO 8601 format (YYYYMMDD). The rest (8 bytes) are manufacturer specific.
*/
#define BULB_INIT_BASIC_DATE_CODE "20200329"
/* Type of power sources available for the device.
* For possible values see section 3.2.2.2.8 of ZCL specification.
*/
#define BULB_INIT_BASIC_POWER_SOURCE ZB_ZCL_BASIC_POWER_SOURCE_DC_SOURCE
/* Describes the physical location of the device (16 bytes).
* May be modified during commissioning process.
*/
#define BULB_INIT_BASIC_LOCATION_DESC "Office desk"
/* Describes the type of physical environment.
* For possible values see section 3.2.2.2.10 of ZCL specification.
*/
#define BULB_INIT_BASIC_PH_ENV ZB_ZCL_BASIC_ENV_UNSPECIFIED
/* LED indicating that light switch successfully joind Zigbee network. */
#define ZIGBEE_NETWORK_STATE_LED DK_LED3
/* LED immitaing dimmable light bulb - define for informational
* purposes only.
*/
#define BULB_LED DK_LED4
/* Button used to enter the Bulb into the Identify mode. */
#define IDENTIFY_MODE_BUTTON DK_BTN4_MSK
/* Use onboard led4 to act as a light bulb.
* The app.overlay file has this at node label "pwm_led3" in /pwmleds.
*/
#define PWM_DK_LED4_NODE DT_NODELABEL(pwm_led3)
#if DT_NODE_HAS_STATUS(PWM_DK_LED4_NODE, okay)
static const struct pwm_dt_spec led_pwm = PWM_DT_SPEC_GET(PWM_DK_LED4_NODE);
#else
#error "Choose supported PWM driver"
#endif
/* Led PWM period, calculated for 100 Hz signal - in microseconds. */
#define LED_PWM_PERIOD_US (USEC_PER_SEC / 100U)
#ifndef ZB_ROUTER_ROLE
#error Define ZB_ROUTER_ROLE to compile router source code.
#endif
/* Button to start Factory Reset */
#define FACTORY_RESET_BUTTON IDENTIFY_MODE_BUTTON
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
/* Main application customizable context.
* Stores all settings and static values.
*/
typedef struct {
zb_zcl_basic_attrs_ext_t basic_attr;
zb_zcl_identify_attrs_t identify_attr;
zb_zcl_scenes_attrs_t scenes_attr;
zb_zcl_groups_attrs_t groups_attr;
zb_zcl_on_off_attrs_t on_off_attr;
zb_zcl_level_control_attrs_t level_control_attr;
} bulb_device_ctx_t;
/* Zigbee device application context storage. */
static bulb_device_ctx_t dev_ctx;
ZB_ZCL_DECLARE_IDENTIFY_ATTRIB_LIST(
identify_attr_list,
&dev_ctx.identify_attr.identify_time);
ZB_ZCL_DECLARE_GROUPS_ATTRIB_LIST(
groups_attr_list,
&dev_ctx.groups_attr.name_support);
ZB_ZCL_DECLARE_SCENES_ATTRIB_LIST(
scenes_attr_list,
&dev_ctx.scenes_attr.scene_count,
&dev_ctx.scenes_attr.current_scene,
&dev_ctx.scenes_attr.current_group,
&dev_ctx.scenes_attr.scene_valid,
&dev_ctx.scenes_attr.name_support);
ZB_ZCL_DECLARE_BASIC_ATTRIB_LIST_EXT(
basic_attr_list,
&dev_ctx.basic_attr.zcl_version,
&dev_ctx.basic_attr.app_version,
&dev_ctx.basic_attr.stack_version,
&dev_ctx.basic_attr.hw_version,
dev_ctx.basic_attr.mf_name,
dev_ctx.basic_attr.model_id,
dev_ctx.basic_attr.date_code,
&dev_ctx.basic_attr.power_source,
dev_ctx.basic_attr.location_id,
&dev_ctx.basic_attr.ph_env,
dev_ctx.basic_attr.sw_ver);
/* On/Off cluster attributes additions data */
ZB_ZCL_DECLARE_ON_OFF_ATTRIB_LIST(
on_off_attr_list,
&dev_ctx.on_off_attr.on_off);
ZB_ZCL_DECLARE_LEVEL_CONTROL_ATTRIB_LIST(
level_control_attr_list,
&dev_ctx.level_control_attr.current_level,
&dev_ctx.level_control_attr.remaining_time);
ZB_DECLARE_DIMMABLE_LIGHT_CLUSTER_LIST(
dimmable_light_clusters,
basic_attr_list,
identify_attr_list,
groups_attr_list,
scenes_attr_list,
on_off_attr_list,
level_control_attr_list);
ZB_DECLARE_DIMMABLE_LIGHT_EP(
dimmable_light_ep,
DIMMABLE_LIGHT_ENDPOINT,
dimmable_light_clusters);
ZBOSS_DECLARE_DEVICE_CTX_1_EP(
dimmable_light_ctx,
dimmable_light_ep);
/**@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(
DIMMABLE_LIGHT_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");
}
}
/**@brief Callback for button events.
*
* @param[in] button_state Bitmask containing the state of the buttons.
* @param[in] has_changed Bitmask containing buttons that have changed their state.
*/
static void button_changed(uint32_t button_state, uint32_t has_changed)
{
if (IDENTIFY_MODE_BUTTON & has_changed) {
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);
}
}
}
check_factory_reset_button(button_state, has_changed);
}
/**@brief Function for initializing additional PWM leds. */
static void pwm_led_init(void)
{
if (!device_is_ready(led_pwm.dev)) {
LOG_ERR("Error: PWM device %s is not ready",
led_pwm.dev->name);
}
}
/**@brief Function for initializing LEDs and Buttons. */
static void configure_gpio(void)
{
int err;
err = dk_buttons_init(button_changed);
if (err) {
LOG_ERR("Cannot init buttons (err: %d)", err);
}
err = dk_leds_init();
if (err) {
LOG_ERR("Cannot init LEDs (err: %d)", err);
}
pwm_led_init();
}
/**@brief Sets brightness of bulb luminous executive element
*
* @param[in] brightness_level Brightness level, allowed values 0 ... 255,
* 0 - turn off, 255 - full brightness.
*/
static void light_bulb_set_brightness(zb_uint8_t brightness_level)
{
uint32_t pulse = brightness_level * LED_PWM_PERIOD_US / 255U;
if (pwm_set_dt(&led_pwm, PWM_USEC(LED_PWM_PERIOD_US), PWM_USEC(pulse))) {
LOG_ERR("Pwm led 4 set fails:\n");
return;
}
}
/**@brief Function for setting the light bulb brightness.
*
* @param[in] new_level Light bulb brightness value.
*/
static void level_control_set_value(zb_uint16_t new_level)
{
LOG_INF("Set level value: %i", new_level);
ZB_ZCL_SET_ATTRIBUTE(
DIMMABLE_LIGHT_ENDPOINT,
ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL,
ZB_ZCL_CLUSTER_SERVER_ROLE,
ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID,
(zb_uint8_t *)&new_level,
ZB_FALSE);
light_bulb_set_brightness(new_level);
}
/**@brief Function for turning ON/OFF the light bulb.
*
* @param[in] on Boolean light bulb state.
*/
static void on_off_set_value(zb_bool_t on)
{
LOG_INF("Set ON/OFF value: %i", on);
ZB_ZCL_SET_ATTRIBUTE(
DIMMABLE_LIGHT_ENDPOINT,
ZB_ZCL_CLUSTER_ID_ON_OFF,
ZB_ZCL_CLUSTER_SERVER_ROLE,
ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID,
(zb_uint8_t *)&on,
ZB_FALSE);
if (on) {
light_bulb_set_brightness(dev_ctx.level_control_attr.current_level);
} else {
light_bulb_set_brightness(0U);
}
}
/**@brief Function to toggle the identify LED - BULB_LED is used for this.
*
* @param bufid Unused parameter, required by ZBOSS scheduler API.
*/
static void toggle_identify_led(zb_bufid_t bufid)
{
static int blink_status;
light_bulb_set_brightness(((++blink_status) % 2) ? (255U) : (0U));
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 restore current Zigbee LED state. */
zb_err_code = ZB_SCHEDULE_APP_ALARM_CANCEL(toggle_identify_led, ZB_ALARM_ANY_PARAM);
ZVUNUSED(zb_err_code);
if (dev_ctx.on_off_attr.on_off) {
light_bulb_set_brightness(dev_ctx.level_control_attr.current_level);
} else {
light_bulb_set_brightness(0U);
}
}
}
/**@brief Function for initializing all clusters attributes.
*/
static void bulb_clusters_attr_init(void)
{
/* Basic cluster attributes data */
dev_ctx.basic_attr.zcl_version = ZB_ZCL_VERSION;
dev_ctx.basic_attr.app_version = BULB_INIT_BASIC_APP_VERSION;
dev_ctx.basic_attr.stack_version = BULB_INIT_BASIC_STACK_VERSION;
dev_ctx.basic_attr.hw_version = BULB_INIT_BASIC_HW_VERSION;
/* Use ZB_ZCL_SET_STRING_VAL to set strings, because the first byte
* should contain string length without trailing zero.
*
* For example "test" string will be encoded as:
* [(0x4), 't', 'e', 's', 't']
*/
ZB_ZCL_SET_STRING_VAL(
dev_ctx.basic_attr.mf_name,
BULB_INIT_BASIC_MANUF_NAME,
ZB_ZCL_STRING_CONST_SIZE(BULB_INIT_BASIC_MANUF_NAME));
ZB_ZCL_SET_STRING_VAL(
dev_ctx.basic_attr.model_id,
BULB_INIT_BASIC_MODEL_ID,
ZB_ZCL_STRING_CONST_SIZE(BULB_INIT_BASIC_MODEL_ID));
ZB_ZCL_SET_STRING_VAL(
dev_ctx.basic_attr.date_code,
BULB_INIT_BASIC_DATE_CODE,
ZB_ZCL_STRING_CONST_SIZE(BULB_INIT_BASIC_DATE_CODE));
dev_ctx.basic_attr.power_source = BULB_INIT_BASIC_POWER_SOURCE;
ZB_ZCL_SET_STRING_VAL(
dev_ctx.basic_attr.location_id,
BULB_INIT_BASIC_LOCATION_DESC,
ZB_ZCL_STRING_CONST_SIZE(BULB_INIT_BASIC_LOCATION_DESC));
dev_ctx.basic_attr.ph_env = BULB_INIT_BASIC_PH_ENV;
/* Identify cluster attributes data. */
dev_ctx.identify_attr.identify_time =
ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE;
/* On/Off cluster attributes data. */
dev_ctx.on_off_attr.on_off = (zb_bool_t)ZB_ZCL_ON_OFF_IS_ON;
dev_ctx.level_control_attr.current_level =
ZB_ZCL_LEVEL_CONTROL_LEVEL_MAX_VALUE;
dev_ctx.level_control_attr.remaining_time =
ZB_ZCL_LEVEL_CONTROL_REMAINING_TIME_DEFAULT_VALUE;
ZB_ZCL_SET_ATTRIBUTE(
DIMMABLE_LIGHT_ENDPOINT,
ZB_ZCL_CLUSTER_ID_ON_OFF,
ZB_ZCL_CLUSTER_SERVER_ROLE,
ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID,
(zb_uint8_t *)&dev_ctx.on_off_attr.on_off,
ZB_FALSE);
ZB_ZCL_SET_ATTRIBUTE(
DIMMABLE_LIGHT_ENDPOINT,
ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL,
ZB_ZCL_CLUSTER_SERVER_ROLE,
ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID,
(zb_uint8_t *)&dev_ctx.level_control_attr.current_level,
ZB_FALSE);
}
/**@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_uint8_t cluster_id;
zb_uint8_t attr_id;
zb_zcl_device_callback_param_t *device_cb_param =
ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
LOG_INF("%s id %hd", __func__, device_cb_param->device_cb_id);
/* Set default response value. */
device_cb_param->status = RET_OK;
switch (device_cb_param->device_cb_id) {
case ZB_ZCL_LEVEL_CONTROL_SET_VALUE_CB_ID:
LOG_INF("Level control setting to %d",
device_cb_param->cb_param.level_control_set_value_param
.new_value);
level_control_set_value(
device_cb_param->cb_param.level_control_set_value_param
.new_value);
break;
case ZB_ZCL_SET_ATTR_VALUE_CB_ID:
cluster_id = device_cb_param->cb_param.
set_attr_value_param.cluster_id;
attr_id = device_cb_param->cb_param.
set_attr_value_param.attr_id;
if (cluster_id == ZB_ZCL_CLUSTER_ID_ON_OFF) {
uint8_t value =
device_cb_param->cb_param.set_attr_value_param
.values.data8;
LOG_INF("on/off attribute setting to %hd", value);
if (attr_id == ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID) {
on_off_set_value((zb_bool_t)value);
}
} else if (cluster_id == ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL) {
uint16_t value = device_cb_param->cb_param.
set_attr_value_param.values.data16;
LOG_INF("level control attribute setting to %hd",
value);
if (attr_id ==
ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID) {
level_control_set_value(value);
}
} else {
/* Other clusters can be processed here */
LOG_INF("Unhandled cluster attribute id: %d",
cluster_id);
device_cb_param->status = RET_NOT_IMPLEMENTED;
}
break;
default:
if (zcl_scenes_cb(bufid) == ZB_FALSE) {
device_cb_param->status = RET_NOT_IMPLEMENTED;
}
break;
}
LOG_INF("%s status: %hd", __func__, device_cb_param->status);
}
/**@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)
{
/* Update network status LED. */
zigbee_led_status_update(bufid, ZIGBEE_NETWORK_STATE_LED);
/* No application-specific behavior is required.
* Call default signal handler.
*/
ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
/* All callbacks should either reuse or free passed buffers.
* If bufid == 0, the buffer is invalid (not passed).
*/
if (bufid) {
zb_buf_free(bufid);
}
}
int main(void)
{
int blink_status = 0;
int err;
LOG_INF("Starting ZBOSS Light Bulb example");
/* Initialize */
configure_gpio();
err = settings_subsys_init();
if (err) {
LOG_ERR("settings initialization failed");
}
register_factory_reset_button(FACTORY_RESET_BUTTON);
/* Register callback for handling ZCL commands. */
ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb);
/* Register dimmer switch device context (endpoints). */
ZB_AF_REGISTER_DEVICE_CTX(&dimmable_light_ctx);
bulb_clusters_attr_init();
level_control_set_value(dev_ctx.level_control_attr.current_level);
/* Register handler to identify notifications. */
ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(DIMMABLE_LIGHT_ENDPOINT, identify_cb);
/* Initialize ZCL scene table */
zcl_scenes_init();
/* Settings should be loaded after zcl_scenes_init */
err = settings_load();
if (err) {
LOG_ERR("settings loading failed");
}
/* Start Zigbee default thread */
zigbee_enable();
LOG_INF("ZBOSS Light Bulb example started");
while (1) {
dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
}
}