/
zb_nrf_platform.c
781 lines (652 loc) · 18.7 KB
/
zb_nrf_platform.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
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
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <stdlib.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/init.h>
#include <ram_pwrdn.h>
#include <hal/nrf_power.h>
#if !NRF_POWER_HAS_RESETREAS
#include <hal/nrf_reset.h>
#endif
#ifdef CONFIG_ZIGBEE_SHELL
#include <zigbee/zigbee_shell.h>
#endif
#include <zboss_api.h>
#include "zb_nrf_platform.h"
#include "zb_nrf_crypto.h"
#ifdef CONFIG_ZIGBEE_LIBRARY_NCP_DEV
#define SYS_REBOOT_NCP 0x10
#endif /* CONFIG_ZIGBEE_LIBRARY_NCP_DEV */
/* Value that is returned while reading a single byte from the erased flash page .*/
#define FLASH_EMPTY_BYTE 0xFF
/* The number of bytes to be checked before concluding that the ZBOSS NVRAM is not initialized. */
#define ZB_PAGE_INIT_CHECK_LEN 32
/**
* Enumeration representing type of application callback to execute from ZBOSS
* context.
*/
typedef enum {
ZB_CALLBACK_TYPE_SINGLE_PARAM,
ZB_CALLBACK_TYPE_TWO_PARAMS,
ZB_CALLBACK_TYPE_ALARM_SET,
ZB_CALLBACK_TYPE_ALARM_CANCEL,
ZB_GET_OUT_BUF_DELAYED,
ZB_GET_IN_BUF_DELAYED,
ZB_GET_OUT_BUF_DELAYED_EXT,
ZB_GET_IN_BUF_DELAYED_EXT,
} zb_callback_type_t;
/**
* Type definition of element of the application callback and alarm queue.
*/
typedef struct {
zb_callback_type_t type;
zb_callback_t func;
zb_callback2_t func2;
zb_uint16_t param;
zb_uint16_t user_param;
int64_t alarm_timestamp;
} zb_app_cb_t;
LOG_MODULE_REGISTER(zboss_osif, CONFIG_ZBOSS_OSIF_LOG_LEVEL);
/* Signal object to indicate that frame has been received */
static struct k_poll_signal zigbee_sig = K_POLL_SIGNAL_INITIALIZER(zigbee_sig);
/** Global mutex to protect access to the ZBOSS global state.
*
* @note Functions for locking/unlocking the mutex are called directly from
* ZBOSS core, when the main ZBOSS global variable is accessed.
*/
static K_MUTEX_DEFINE(zigbee_mutex);
/**
* Message queue, that is used to pass ZBOSS callbacks and alarms from
* ISR and other threads to ZBOSS main loop context.
*/
K_MSGQ_DEFINE(zb_app_cb_msgq, sizeof(zb_app_cb_t),
CONFIG_ZIGBEE_APP_CB_QUEUE_LENGTH, 4);
/**
* Work queue that will schedule processing of callbacks from the message queue.
*/
static struct k_work zb_app_cb_work;
/**
* Atomic flag, indicating that the processing callback is still scheduled for
* execution,
*/
volatile atomic_t zb_app_cb_process_scheduled = ATOMIC_INIT(0);
K_THREAD_STACK_DEFINE(zboss_stack_area, CONFIG_ZBOSS_DEFAULT_THREAD_STACK_SIZE);
static struct k_thread zboss_thread_data;
static k_tid_t zboss_tid;
static bool stack_is_started;
#ifdef CONFIG_ZIGBEE_DEBUG_FUNCTIONS
/**@brief Function for checking if the ZBOSS thread has been created.
*/
bool zigbee_debug_zboss_thread_is_created(void)
{
if (zboss_tid) {
return true;
}
return false;
}
/**@brief Function for suspending ZBOSS thread.
*/
void zigbee_debug_suspend_zboss_thread(void)
{
k_thread_suspend(zboss_tid);
}
/**@brief Function for resuming ZBOSS thread.
*/
void zigbee_debug_resume_zboss_thread(void)
{
k_thread_resume(zboss_tid);
}
/**@brief Function for getting the state of the Zigbee stack thread
* processing suspension.
*/
bool zigbee_is_zboss_thread_suspended(void)
{
if (zboss_tid) {
if (!(zboss_tid->base.thread_state & _THREAD_SUSPENDED)) {
return false;
}
}
return true;
}
#endif /* defined(CONFIG_ZIGBEE_DEBUG_FUNCTIONS) */
/**@brief Function for checking if the Zigbee stack has been started.
*
* @retval true Zigbee stack has been started.
* @retval false Zigbee stack has not been started yet.
*/
bool zigbee_is_stack_started(void)
{
return stack_is_started;
}
static void zb_app_cb_process(zb_bufid_t bufid)
{
zb_ret_t ret_code = RET_OK;
zb_app_cb_t new_app_cb;
/* Mark the processing callback as non-scheduled. */
(void)atomic_set((atomic_t *)&zb_app_cb_process_scheduled, 0);
/**
* From ZBOSS main loop context: process all requests.
*
* Note: the ZB_SCHEDULE_APP_ALARM is not thread-safe.
*/
while (!k_msgq_peek(&zb_app_cb_msgq, &new_app_cb)) {
switch (new_app_cb.type) {
case ZB_CALLBACK_TYPE_SINGLE_PARAM:
ret_code = zb_schedule_app_callback(
new_app_cb.func,
(zb_uint8_t)new_app_cb.param);
break;
case ZB_CALLBACK_TYPE_TWO_PARAMS:
ret_code = zb_schedule_app_callback2(
new_app_cb.func2,
(zb_uint8_t)new_app_cb.param,
new_app_cb.user_param);
break;
case ZB_CALLBACK_TYPE_ALARM_SET:
{
/**
* Check if the timeout already passed. If so, use the
* lowest value that schedules an alarm, so the user
* is still able to cancel the alarm.
*/
zb_time_t delay =
(k_uptime_get() > new_app_cb.alarm_timestamp ?
1 :
ZB_MILLISECONDS_TO_BEACON_INTERVAL(
new_app_cb.alarm_timestamp -
k_uptime_get())
);
ret_code = zb_schedule_app_alarm(
new_app_cb.func,
(zb_uint8_t)new_app_cb.param,
delay);
break;
}
case ZB_CALLBACK_TYPE_ALARM_CANCEL:
ret_code = zb_schedule_alarm_cancel(
new_app_cb.func,
(zb_uint8_t)new_app_cb.param,
NULL);
break;
case ZB_GET_OUT_BUF_DELAYED:
ret_code = zb_buf_get_out_delayed_func(
TRACE_CALL(new_app_cb.func));
break;
case ZB_GET_IN_BUF_DELAYED:
ret_code = zb_buf_get_in_delayed_func(
TRACE_CALL(new_app_cb.func));
break;
case ZB_GET_OUT_BUF_DELAYED_EXT:
ret_code = zb_buf_get_out_delayed_ext_func(
TRACE_CALL(new_app_cb.func2),
new_app_cb.user_param,
new_app_cb.param);
break;
case ZB_GET_IN_BUF_DELAYED_EXT:
ret_code = zb_buf_get_in_delayed_ext_func(
TRACE_CALL(new_app_cb.func2),
new_app_cb.user_param,
new_app_cb.param);
break;
default:
break;
}
/* Check for ZBOSS scheduler queue overflow. */
if (ret_code == RET_OVERFLOW) {
break;
}
/* Flush the element from the message queue. */
k_msgq_get(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT);
}
/**
* In case of overflow error - reschedule the processing callback
* to process remaining requests later.
*/
if (ret_code == RET_OVERFLOW) {
k_work_submit(&zb_app_cb_work);
}
}
static void zb_app_cb_process_schedule(struct k_work *item)
{
zb_app_cb_t new_app_cb;
if (k_msgq_peek(&zb_app_cb_msgq, &new_app_cb)) {
return;
}
/* Check if processing callback is already scheduled. */
if (atomic_set((atomic_t *)&zb_app_cb_process_scheduled, 1) == 1) {
return;
}
/**
* From working thread, non-ISR context: schedule processing callback.
* Repeat endlessly, because the user was already informed that the
* request will be handled.
*
* Note: the ZB_SCHEDULE_APP_CALLBACK is thread-safe.
*/
while (zb_schedule_app_callback(zb_app_cb_process, 0) != RET_OK) {
k_sleep(K_MSEC(1000));
}
zigbee_event_notify(ZIGBEE_EVENT_APP);
(void)item;
}
int zigbee_init(void)
{
/* Initialise work queue for processing app callback and alarms. */
k_work_init(&zb_app_cb_work, zb_app_cb_process_schedule);
#if ZB_TRACE_LEVEL
/* Set Zigbee stack logging level and traffic dump subsystem. */
ZB_SET_TRACE_LEVEL(CONFIG_ZBOSS_TRACE_LOG_LEVEL);
ZB_SET_TRACE_MASK(CONFIG_ZBOSS_TRACE_MASK);
#if CONFIG_ZBOSS_TRAF_DUMP
ZB_SET_TRAF_DUMP_ON();
#else /* CONFIG_ZBOSS_TRAF_DUMP */
ZB_SET_TRAF_DUMP_OFF();
#endif /* CONFIG_ZBOSS_TRAF_DUMP */
#endif /* ZB_TRACE_LEVEL */
#ifndef CONFIG_ZB_TEST_MODE_MAC
/* Initialize Zigbee stack. */
ZB_INIT("zigbee_thread");
/* Set device address to the value read from FICR registers. */
zb_ieee_addr_t ieee_addr;
zb_osif_get_ieee_eui64(ieee_addr);
zb_set_long_address(ieee_addr);
/* Keep or erase NVRAM to save the network parameters
* after device reboot or power-off.
*/
zb_set_nvram_erase_at_start(ZB_FALSE);
if (IS_ENABLED(CONFIG_ZIGBEE_TC_REJOIN_ENABLED)) {
zb_secur_set_tc_rejoin_enabled((zb_bool_t)CONFIG_ZIGBEE_TC_REJOIN_ENABLED);
}
/* Don't set zigbee role for NCP device */
#ifndef CONFIG_ZIGBEE_LIBRARY_NCP_DEV
/* Set channels on which the coordinator will try
* to create a new network
*/
#if defined(CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_SINGLE)
zb_uint32_t channel_mask = (1UL << CONFIG_ZIGBEE_CHANNEL);
#elif defined(CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_MULTI)
zb_uint32_t channel_mask = CONFIG_ZIGBEE_CHANNEL_MASK;
#else
#error Channel mask undefined!
#endif
#if defined(CONFIG_ZIGBEE_ROLE_COORDINATOR)
zb_set_network_coordinator_role(channel_mask);
#elif defined(CONFIG_ZIGBEE_ROLE_ROUTER)
zb_set_network_router_role(channel_mask);
/* Enable full distributed network operability. */
#if (ZBOSS_MAJOR == 3U) && (ZBOSS_MINOR == 11U)
zb_enable_distributed();
#endif
#elif defined(CONFIG_ZIGBEE_ROLE_END_DEVICE)
zb_set_network_ed_role(channel_mask);
#else
#error Zigbee device role undefined!
#endif
#endif /* CONFIG_ZIGBEE_LIBRARY_NCP_DEV */
#endif /* CONFIG_ZB_TEST_MODE_MAC */
return 0;
}
static void zboss_thread(void *arg1, void *arg2, void *arg3)
{
zb_ret_t zb_err_code;
zb_err_code = zboss_start_no_autostart();
__ASSERT(zb_err_code == RET_OK, "Error when starting ZBOSS stack!");
stack_is_started = true;
#ifdef CONFIG_ZIGBEE_SHELL
zb_shell_configure_endpoint();
#endif /* defined(CONFIG_ZIGBEE_SHELL) */
while (1) {
zboss_main_loop_iteration();
}
}
zb_bool_t zb_osif_is_inside_isr(void)
{
return (zb_bool_t)(__get_IPSR() != 0);
}
void zb_osif_enable_all_inter(void)
{
__ASSERT(zb_osif_is_inside_isr() == 0,
"Unable to unlock mutex from interrupt context");
k_mutex_unlock(&zigbee_mutex);
}
void zb_osif_disable_all_inter(void)
{
__ASSERT(zb_osif_is_inside_isr() == 0,
"Unable to lock mutex from interrupt context");
k_mutex_lock(&zigbee_mutex, K_FOREVER);
}
zb_ret_t zigbee_schedule_callback(zb_callback_t func, zb_uint8_t param)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_schedule_app_callback(func, param);
}
zb_app_cb_t new_app_cb = {
.type = ZB_CALLBACK_TYPE_SINGLE_PARAM,
.func = func,
.param = param,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_schedule_callback2(zb_callback2_t func,
zb_uint8_t param,
zb_uint16_t user_param)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_schedule_app_callback2(func, param, user_param);
}
zb_app_cb_t new_app_cb = {
.type = ZB_CALLBACK_TYPE_TWO_PARAMS,
.func2 = func,
.param = param,
.user_param = user_param,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_schedule_alarm(zb_callback_t func,
zb_uint8_t param,
zb_time_t run_after)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_schedule_app_alarm(func, param, run_after);
}
zb_app_cb_t new_app_cb = {
.type = ZB_CALLBACK_TYPE_ALARM_SET,
.func = func,
.param = param,
.alarm_timestamp = k_uptime_get() +
ZB_TIME_BEACON_INTERVAL_TO_MSEC(run_after),
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_schedule_alarm_cancel(zb_callback_t func, zb_uint8_t param)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_schedule_alarm_cancel(func, param, NULL);
}
zb_app_cb_t new_app_cb = {
.type = ZB_CALLBACK_TYPE_ALARM_CANCEL,
.func = func,
.param = param,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_get_out_buf_delayed(zb_callback_t func)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_buf_get_out_delayed_func(func);
}
zb_app_cb_t new_app_cb = {
.type = ZB_GET_OUT_BUF_DELAYED,
.func = func,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_get_in_buf_delayed(zb_callback_t func)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_buf_get_in_delayed_func(func);
}
zb_app_cb_t new_app_cb = {
.type = ZB_GET_IN_BUF_DELAYED,
.func = func,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_get_out_buf_delayed_ext(zb_callback2_t func, zb_uint16_t param,
zb_uint16_t max_size)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_buf_get_out_delayed_ext_func(func, param, max_size);
}
zb_app_cb_t new_app_cb = {
.type = ZB_GET_OUT_BUF_DELAYED_EXT,
.func2 = func,
.user_param = param,
.param = max_size,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
zb_ret_t zigbee_get_in_buf_delayed_ext(zb_callback2_t func, zb_uint16_t param,
zb_uint16_t max_size)
{
if ((zboss_tid) && (k_current_get() == zboss_tid) && (!zb_osif_is_inside_isr())) {
return zb_buf_get_in_delayed_ext_func(func, param, max_size);
}
zb_app_cb_t new_app_cb = {
.type = ZB_GET_IN_BUF_DELAYED_EXT,
.func2 = func,
.user_param = param,
.param = max_size,
};
if (k_msgq_put(&zb_app_cb_msgq, &new_app_cb, K_NO_WAIT)) {
return RET_OVERFLOW;
}
k_work_submit(&zb_app_cb_work);
return RET_OK;
}
/**@brief SoC general initialization. */
void zb_osif_init(void)
{
static bool platform_inited;
if (platform_inited) {
return;
}
platform_inited = true;
#ifdef CONFIG_ZB_HAVE_SERIAL
/* Initialise serial trace */
zb_osif_serial_init();
#endif
/* Initialise random generator */
zb_osif_rng_init();
/* Initialise AES ECB */
zb_osif_aes_init();
#ifdef ZB_USE_SLEEP
/* Initialise power consumption routines */
zb_osif_sleep_init();
#endif /*ZB_USE_SLEEP*/
}
void zb_osif_abort(void)
{
/* Log ZBOSS error message and flush logs. */
LOG_ERR("ZBOSS fatal error occurred");
LOG_PANIC();
/* Flush ZBOSS trace logs. */
ZB_OSIF_SERIAL_FLUSH();
/* By default reset device or halt if so configured. */
if (IS_ENABLED(CONFIG_ZBOSS_RESET_ON_ASSERT)) {
zb_reset(0);
}
if (IS_ENABLED(CONFIG_ZBOSS_HALT_ON_ASSERT)) {
k_fatal_halt(K_ERR_KERNEL_PANIC);
}
}
void zb_reset(zb_uint8_t param)
{
uint8_t reas = (uint8_t)SYS_REBOOT_COLD;
ZVUNUSED(param);
#ifdef CONFIG_ZIGBEE_LIBRARY_NCP_DEV
reas = (uint8_t)SYS_REBOOT_NCP;
#endif /* CONFIG_ZIGBEE_LIBRARY_NCP_DEV */
/* For nRF5340DK sys_reboot() does not set reset reason.
* Do it manually in this case - NCP samples require this.
*/
#ifdef CONFIG_SOC_NRF5340_CPUAPP
nrf_power_gpregret_set(NRF_POWER, reas);
#endif /* CONFIG_SOC_NRF5340_CPUAPP */
/* 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(reas);
}
void zb_osif_busy_loop_delay(zb_uint32_t count)
{
k_busy_wait(count);
}
__weak zb_uint32_t zb_get_utc_time(void)
{
LOG_ERR("Unable to obtain UTC time. "
"Please implement %s in your application to provide the current UTC time.",
__func__);
return ZB_TIME_BEACON_INTERVAL_TO_MSEC(ZB_TIMER_GET()) / 1000;
}
void zigbee_event_notify(zigbee_event_t event)
{
k_poll_signal_raise(&zigbee_sig, event);
}
uint32_t zigbee_event_poll(uint32_t timeout_us)
{
/* Configure event/signals to wait for in zigbee_event_poll function. */
static struct k_poll_event wait_events[] = {
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
K_POLL_MODE_NOTIFY_ONLY,
&zigbee_sig),
};
unsigned int signaled = 0;
int result;
/* Store timestamp of event polling start. */
int64_t timestamp_poll_start = k_uptime_ticks();
k_poll(wait_events, 1, K_USEC(timeout_us));
k_poll_signal_check(&zigbee_sig, &signaled, &result);
if (signaled) {
k_poll_signal_reset(&zigbee_sig);
LOG_DBG("Received new Zigbee event: 0x%02x", result);
}
return k_ticks_to_us_floor32(k_uptime_ticks() - timestamp_poll_start);
}
void zigbee_enable(void)
{
zboss_tid = k_thread_create(&zboss_thread_data,
zboss_stack_area,
K_THREAD_STACK_SIZEOF(zboss_stack_area),
zboss_thread,
NULL, NULL, NULL,
CONFIG_ZBOSS_DEFAULT_THREAD_PRIORITY,
0, K_NO_WAIT);
k_thread_name_set(&zboss_thread_data, "zboss");
}
/**
* @brief Get the reason that triggered the last reset
*
* @return @ref reset_source
* */
zb_uint8_t zb_get_reset_source(void)
{
uint32_t reas;
uint8_t zb_reason;
#ifdef CONFIG_ZIGBEE_LIBRARY_NCP_DEV
static uint8_t zephyr_reset_type = 0xFF;
/* Read the value at the first API call, then use data from RAM. */
if (zephyr_reset_type == 0xFF) {
zephyr_reset_type = nrf_power_gpregret_get(NRF_POWER);
}
#endif /* CONFIG_ZIGBEE_LIBRARY_NCP_DEV */
#if NRF_POWER_HAS_RESETREAS
reas = nrf_power_resetreas_get(NRF_POWER);
nrf_power_resetreas_clear(NRF_POWER, reas);
if (reas & NRF_POWER_RESETREAS_RESETPIN_MASK) {
zb_reason = ZB_RESET_SRC_RESET_PIN;
} else if (reas & NRF_POWER_RESETREAS_SREQ_MASK) {
zb_reason = ZB_RESET_SRC_SW_RESET;
} else if (reas) {
zb_reason = ZB_RESET_SRC_OTHER;
} else {
zb_reason = ZB_RESET_SRC_POWER_ON;
}
#else
reas = nrf_reset_resetreas_get(NRF_RESET);
nrf_reset_resetreas_clear(NRF_RESET, reas);
if (reas & NRF_RESET_RESETREAS_RESETPIN_MASK) {
zb_reason = ZB_RESET_SRC_RESET_PIN;
} else if (reas & NRF_RESET_RESETREAS_SREQ_MASK) {
zb_reason = ZB_RESET_SRC_SW_RESET;
} else if (reas) {
zb_reason = ZB_RESET_SRC_OTHER;
} else {
zb_reason = ZB_RESET_SRC_POWER_ON;
}
#endif
#ifdef CONFIG_ZIGBEE_LIBRARY_NCP_DEV
if ((zb_reason == ZB_RESET_SRC_SW_RESET) &&
(zephyr_reset_type != SYS_REBOOT_NCP)) {
zb_reason = ZB_RESET_SRC_OTHER;
}
/* The NCP reset type is used only by this API call.
* Reset the value inside the register, so after the next, external
* SW reset, the value will not trigger NCP logic.
*/
if (zephyr_reset_type == SYS_REBOOT_NCP) {
nrf_power_gpregret_set(NRF_POWER, (uint8_t)SYS_REBOOT_COLD);
}
#endif /* CONFIG_ZIGBEE_LIBRARY_NCP_DEV */
return zb_reason;
}
zb_bool_t zigbee_is_nvram_initialised(void)
{
zb_uint8_t buf[ZB_PAGE_INIT_CHECK_LEN] = {0};
zb_uint8_t i;
zb_ret_t ret_code;
ret_code = zb_osif_nvram_read(0, 0, buf, sizeof(buf));
if (ret_code != RET_OK) {
return ZB_FALSE;
}
for (i = 0; i < sizeof(buf); i++) {
if (buf[i] != FLASH_EMPTY_BYTE) {
return ZB_TRUE;
}
}
return ZB_FALSE;
}
ZB_WEAK_PRE zb_uint32_t ZB_WEAK zb_osif_get_fw_version(void)
{
return 0x01;
}
ZB_WEAK_PRE zb_uint32_t ZB_WEAK zb_osif_get_ncp_protocol_version(void)
{
#ifdef ZB_NCP_PROTOCOL_VERSION
return ZB_NCP_PROTOCOL_VERSION;
#else /* ZB_NCP_PROTOCOL_VERSION */
return 0x01;
#endif /* ZB_NCP_PROTOCOL_VERSION */
}
ZB_WEAK_PRE zb_ret_t ZB_WEAK zb_osif_bootloader_run_after_reboot(void)
{
return RET_OK;
}
ZB_WEAK_PRE void ZB_WEAK zb_osif_bootloader_report_successful_loading(void)
{
}