-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
261 lines (218 loc) · 9.19 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
/**
* Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/** @file
*
* @defgroup zigbee_examples_cli_agent_router main.c
* @{
* @ingroup zigbee_examples
* @brief CLI agent for probing the Zigbee network.
*/
#include "sdk_config.h"
#include "zboss_api.h"
#ifdef ZIGBEE_MEM_CONFIG_MODEL
#if (ZIGBEE_MEM_CONFIG_MODEL == 0)
/* None of the files zb_mem_config_*.h included, use default memory settings */
#elif (ZIGBEE_MEM_CONFIG_MODEL == 1)
#include "zb_mem_config_min.h"
#elif (ZIGBEE_MEM_CONFIG_MODEL == 2)
#include "zb_mem_config_med.h"
#elif (ZIGBEE_MEM_CONFIG_MODEL == 3)
#include "zb_mem_config_max.h"
#else
#error ZIGBEE_MEM_CONFIG_MODEL unsupported value, please check sdk_config.h
#endif
#endif
#include "zb_ha_configuration_tool.h"
#include "zb_error_handler.h"
#include "zigbee_helpers.h"
#include "zigbee_cli.h"
#include "nrf_cli.h"
#include "zigbee_logger_eprxzcl.h"
#include "nrf_drv_clock.h"
#include "boards.h"
#include "app_timer.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define IEEE_CHANNEL_MASK (1l << ZIGBEE_CHANNEL) /**< Scan only one, predefined channel to find the coordinator. */
#define ERASE_PERSISTENT_CONFIG ZB_FALSE /**< 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 ZIGBEE_NETWORK_STATE_LED BSP_BOARD_LED_3 /**< LED indicating that light switch successfully joind Zigbee network. */
#if !defined ZB_COORDINATOR_ROLE
#error Define ZB_COORDINATOR_ROLE to compile CLI agent (Router) source code.
#endif
static zb_uint8_t m_attr_zcl_version = ZB_ZCL_VERSION;
static zb_uint8_t m_attr_power_source = ZB_ZCL_BASIC_POWER_SOURCE_UNKNOWN;
static zb_uint16_t m_attr_identify_time = 0;
/* Declare attribute list for Basic cluster. */
ZB_ZCL_DECLARE_BASIC_ATTRIB_LIST(basic_attr_list, &m_attr_zcl_version, &m_attr_power_source);
/* Declare attribute list for Identify cluster. */
ZB_ZCL_DECLARE_IDENTIFY_ATTRIB_LIST(identify_attr_list, &m_attr_identify_time);
/* Declare cluster list for CLI Agent device. */
/* Only clusters Identify and Basic have attributes. */
ZB_HA_DECLARE_CONFIGURATION_TOOL_CLUSTER_LIST(cli_agent_clusters,
basic_attr_list,
identify_attr_list);
/* Declare endpoint for CLI Agent device. */
ZB_HA_DECLARE_CONFIGURATION_TOOL_EP(cli_agent_ep,
ZIGBEE_CLI_ENDPOINT,
cli_agent_clusters);
/* Declare application's device context (list of registered endpoints) for CLI Agent device. */
ZB_HA_DECLARE_CONFIGURATION_TOOL_CTX(cli_agent_ctx, cli_agent_ep);
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
/**@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 * p_sg_p = NULL;
zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, &p_sg_p);
zb_ret_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
/* Update network status LED */
zigbee_led_status_update(bufid, ZIGBEE_NETWORK_STATE_LED);
#ifdef NRF_DEBUG
UNUSED_RETURN_VALUE(zigbee_logger_eprxzcl_ep_handler(bufid));
#endif
switch (sig)
{
case ZB_ZDO_SIGNAL_LEAVE:
/* Handle the LEAVE signal inside main instead of using default signal handler
* so the CLI will not attempt to rejoin the network after it receives the LEAVE command
*/
if (status == RET_OK)
{
zb_zdo_signal_leave_params_t * p_leave_params = ZB_ZDO_SIGNAL_GET_PARAMS(p_sg_p, zb_zdo_signal_leave_params_t);
NRF_LOG_INFO("Network left (leave type: %d)", p_leave_params->leave_type);
}
else
{
NRF_LOG_ERROR("Unable to leave network (status: %d)", status);
}
zb_enable_auto_pan_id_conflict_resolution(ZB_FALSE);
break;
default:
/* Call default signal handler. */
ZB_ERROR_CHECK(zigbee_default_signal_handler(bufid));
break;
}
if (bufid)
{
zb_buf_free(bufid);
}
}
void cmd_erase_nvram(nrf_cli_t const * p_cli, size_t argc, char ** argv) {
zb_nvram_erase();
}
NRF_CLI_CMD_REGISTER(erase, NULL, "erase nv RAM", cmd_erase_nvram);
/**@brief Function for application main entry.
*/
int main(void)
{
ret_code_t ret;
zb_ieee_addr_t ieee_addr;
/* Intiialise the leds */
bsp_board_init(BSP_INIT_LEDS);
bsp_board_leds_off();
/* Initialize loging system and GPIOs. */
log_init();
#if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
nrf_drv_clock_lfclk_request(NULL);
#endif
ret = app_timer_init();
APP_ERROR_CHECK(ret);
/* Initialize the Zigbee CLI subsystem */
zb_cli_init(ZIGBEE_CLI_ENDPOINT);
/* Set Zigbee stack logging level and traffic dump subsystem. */
ZB_SET_TRACE_LEVEL(ZIGBEE_TRACE_LEVEL);
ZB_SET_TRACE_MASK(ZIGBEE_TRACE_MASK);
ZB_SET_TRAF_DUMP_OFF();
/* Initialize Zigbee stack. */
ZB_INIT("cli_agent_coordinator");
// zb_nvram_erase();
// zigbee_erase_persistent_storage(ERASE_PERSISTENT_CONFIG);
/* Set device address to the value read from FICR registers. */
zb_osif_get_ieee_eui64(ieee_addr);
zb_set_long_address(ieee_addr);
// Use ZigbeeAlliance09 Network Key
static const zb_uint8_t g_key_nwk[16] = {0x5A, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6C, 0x6C, 0x69, 0x61, 0x6E, 0x63, 0x65, 0x30, 0x39};
zb_secur_setup_nwk_key((zb_uint8_t*) g_key_nwk, 0);
// zb_set_network_coordinator_role(IEEE_CHANNEL_MASK);
zb_set_max_children(32);
zb_set_bdb_primary_channel_set(IEEE_CHANNEL_MASK);
zb_set_bdb_secondary_channel_set(IEEE_CHANNEL_MASK);
zb_ext_pan_id_t extpanid;
zb_get_extended_pan_id(extpanid);
parse_long_address("B00BDEADBEEFBABE", extpanid);
zb_set_extended_pan_id(extpanid);
zb_set_pan_id(0xB00B);
/* Register CLI Agent device context (endpoints). */
ZB_AF_REGISTER_DEVICE_CTX(&cli_agent_ctx);
/* Set the endpoint receive hook */
ZB_AF_SET_ENDPOINT_HANDLER(ZIGBEE_CLI_ENDPOINT, cli_agent_ep_handler);
/* Start Zigbee CLI subsystem. */
zb_cli_start();
/* Start Zigbee stack. */
while(1)
{
if (zb_cli_is_stack_started())
{
#ifdef ZIGBEE_CLI_DEBUG
if (!zb_cli_stack_is_suspended())
{
zboss_main_loop_iteration();
}
#else
zboss_main_loop_iteration();
#endif
}
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
UNUSED_RETURN_VALUE(zb_cli_process());
}
}
/**
* @}
*/