1
1
/* Zigbee Common Functions */
2
2
#include " Zigbee_core.h"
3
+ #include " Zigbee_handlers.cpp"
3
4
#include " Arduino.h"
4
5
5
6
Zigbee_Core::Zigbee_Core () {
@@ -11,37 +12,44 @@ Zigbee_Core::Zigbee_Core() {
11
12
}
12
13
Zigbee_Core::~Zigbee_Core () {}
13
14
15
+ // forward declaration
16
+ static esp_err_t zb_action_handler (esp_zb_core_action_callback_id_t callback_id, const void *message);
17
+
14
18
bool Zigbee_Core::begin (esp_zb_cfg_t *role_cfg, bool erase_nvs) {
15
- zigbeeInit (role_cfg, erase_nvs);
19
+ if (!zigbeeInit (role_cfg, erase_nvs)){
20
+ return false ;
21
+ }
16
22
_role = (zigbee_role_t )role_cfg->esp_zb_role ;
23
+ return true ;
17
24
}
18
25
19
26
bool Zigbee_Core::begin (zigbee_role_t role, bool erase_nvs) {
27
+ bool status = true ;
20
28
switch (role)
21
29
{
22
- case Zigbee_Coordinator : {
23
- _role = Zigbee_Coordinator ;
30
+ case ZIGBEE_COORDINATOR : {
31
+ _role = ZIGBEE_COORDINATOR ;
24
32
esp_zb_cfg_t zb_nwk_cfg = ZIGBEE_DEFAULT_COORDINATOR_CONFIG ();
25
- zigbeeInit (&zb_nwk_cfg, erase_nvs);
33
+ status = zigbeeInit (&zb_nwk_cfg, erase_nvs);
26
34
break ;
27
35
}
28
- case Zigbee_Router : {
29
- _role = Zigbee_Router ;
36
+ case ZIGBEE_ROUTER : {
37
+ _role = ZIGBEE_ROUTER ;
30
38
esp_zb_cfg_t zb_nwk_cfg = ZIGBEE_DEFAULT_ROUTER_CONFIG ();
31
- zigbeeInit (&zb_nwk_cfg, erase_nvs);
39
+ status = zigbeeInit (&zb_nwk_cfg, erase_nvs);
32
40
break ;
33
41
}
34
- case Zigbee_End_Device : {
35
- _role = Zigbee_End_Device ;
42
+ case ZIGBEE_END_DEVICE : {
43
+ _role = ZIGBEE_END_DEVICE ;
36
44
esp_zb_cfg_t zb_nwk_cfg = ZIGBEE_DEFAULT_ED_CONFIG ();
37
- zigbeeInit (&zb_nwk_cfg, erase_nvs);
45
+ status = zigbeeInit (&zb_nwk_cfg, erase_nvs);
38
46
break ;
39
47
}
40
48
default :
41
49
log_e (" Invalid Zigbee Role" );
42
50
return false ;
43
51
}
44
- return true ;
52
+ return status ;
45
53
}
46
54
47
55
void Zigbee_Core::addEndpoint (Zigbee_EP *ep) {
@@ -57,74 +65,37 @@ void Zigbee_Core::addEndpoint(Zigbee_EP *ep) {
57
65
esp_zb_ep_list_add_ep (_zb_ep_list, ep->_cluster_list , ep->_ep_config );
58
66
}
59
67
60
- static esp_err_t zb_attribute_handler (const esp_zb_zcl_set_attr_value_message_t *message) {
61
- esp_err_t ret = ESP_OK;
62
- bool light_state = 0 ;
63
-
64
- if (!message) {
65
- log_e (" Empty message" );
66
- }
67
- if (message->info .status != ESP_ZB_ZCL_STATUS_SUCCESS) {
68
- log_e (" Received message: error status(%d)" , message->info .status );
69
- }
70
-
71
- log_i (
72
- " Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)" , message->info .dst_endpoint , message->info .cluster , message->attribute .id ,
73
- message->attribute .data .size
74
- );
75
-
76
- // List through all Zigbee EPs and call the callback function, with the message
77
- for (std::list<Zigbee_EP*>::iterator it = Zigbee.ep_objects .begin (); it != Zigbee.ep_objects .end (); ++it) {
78
- if (message->info .dst_endpoint == (*it)->_endpoint ) {
79
- // TODO: implement argument passing to the callback function
80
- // if Zigbee_EP argument is set, pass it to the callback function
81
- // if ((*it)->_arg) {
82
- // (*it)->_cb(message, (*it)->_arg);
83
- // }
84
- // else {
85
- (*it)->_cb (message);
86
- // }
87
- }
88
- }
89
- return ret;
90
- }
91
-
92
-
93
- static esp_err_t zb_action_handler (esp_zb_core_action_callback_id_t callback_id, const void *message) {
94
- esp_err_t ret = ESP_OK;
95
- /* TODO:
96
- Implement handlers for different Zigbee actions (callback_id's)
97
- */
98
- switch (callback_id) {
99
- case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID: ret = zb_attribute_handler ((esp_zb_zcl_set_attr_value_message_t *)message); break ;
100
- case ESP_ZB_CORE_CMD_DEFAULT_RESP_CB_ID: log_i (" Received default response" ); break ;
101
- default : log_w (" Receive unhandled Zigbee action(0x%x) callback" , callback_id); break ;
102
- }
103
- return ret;
104
- }
105
-
106
68
static void esp_zb_task (void *pvParameters) {
107
69
/* initialize Zigbee stack */
108
70
ESP_ERROR_CHECK (esp_zb_start (false ));
109
71
esp_zb_main_loop_iteration ();
110
72
}
111
73
112
74
// Zigbee core init function
113
- void Zigbee_Core::zigbeeInit (esp_zb_cfg_t *zb_cfg, bool erase_nvs) {
75
+ bool Zigbee_Core::zigbeeInit (esp_zb_cfg_t *zb_cfg, bool erase_nvs) {
114
76
// Zigbee platform configuration
115
77
esp_zb_platform_config_t platform_config = {
116
78
.radio_config = _radio_config,
117
79
.host_config = _host_config,
118
80
};
119
- ESP_ERROR_CHECK (esp_zb_platform_config (&platform_config));
81
+
82
+ esp_err_t err = esp_zb_platform_config (&platform_config);
83
+ if (err != ESP_OK) {
84
+ log_e (" Failed to configure Zigbee platform" );
85
+ return false ;
86
+ }
120
87
121
88
// Initialize Zigbee stack
122
89
log_d (" Initialize Zigbee stack" );
123
90
esp_zb_init (zb_cfg);
124
91
125
92
// Register all Zigbee EPs in list
126
93
log_d (" Register all Zigbee EPs in list" );
127
- esp_zb_device_register (_zb_ep_list);
94
+ err = esp_zb_device_register (_zb_ep_list);
95
+ if (err != ESP_OK) {
96
+ log_e (" Failed to register Zigbee EPs" );
97
+ return false ;
98
+ }
128
99
129
100
// print the list of Zigbee EPs from ep_objects
130
101
log_i (" List of registered Zigbee EPs:" );
@@ -134,7 +105,11 @@ void Zigbee_Core::zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs) {
134
105
135
106
// Register Zigbee action handler
136
107
esp_zb_core_action_handler_register (zb_action_handler);
137
- esp_zb_set_primary_network_channel_set (_primary_channel_mask);
108
+ err = esp_zb_set_primary_network_channel_set (_primary_channel_mask);
109
+ if (err != ESP_OK) {
110
+ log_e (" Failed to set primary network channel mask" );
111
+ return false ;
112
+ }
138
113
139
114
// Erase NVRAM before creating connection to new Coordinator
140
115
if (erase_nvs) {
@@ -143,6 +118,8 @@ void Zigbee_Core::zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs) {
143
118
144
119
// Create Zigbee task and start Zigbee stack
145
120
xTaskCreate (esp_zb_task, " Zigbee_main" , 4096 , NULL , 5 , NULL );
121
+
122
+ return true ;
146
123
}
147
124
148
125
void Zigbee_Core::setRadioConfig (esp_zb_radio_config_t config) {
@@ -300,6 +277,89 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
300
277
}
301
278
}
302
279
280
+ // // Zigbee action handlers
281
+ // static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id, const void *message) {
282
+ // esp_err_t ret = ESP_OK;
283
+ // /* TODO:
284
+ // Implement handlers for different Zigbee actions (callback_id's)
285
+ // */
286
+ // // NOTE: Implement all Zigbee actions that can be handled by the Zigbee_Core class, or just call user defined callback function and let the user handle the action and read the message properly
287
+ // // NOTE: This may me harder for users, to know what callback_id's are available and what message type is received
288
+ // switch (callback_id) {
289
+ // case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID: ret = zb_attribute_set_handler((esp_zb_zcl_set_attr_value_message_t *)message); break;
290
+ // case ESP_ZB_CORE_REPORT_ATTR_CB_ID: ret = zb_attribute_reporting_handler((esp_zb_zcl_report_attr_message_t *)message); break;
291
+ // case ESP_ZB_CORE_CMD_READ_ATTR_RESP_CB_ID: ret = zb_read_attr_resp_handler((esp_zb_zcl_cmd_read_attr_resp_message_t *)message); break;
292
+ // case ESP_ZB_CORE_CMD_REPORT_CONFIG_RESP_CB_ID: ret = zb_configure_report_resp_handler((esp_zb_zcl_cmd_config_report_resp_message_t *)message); break;
293
+ // case ESP_ZB_CORE_CMD_DEFAULT_RESP_CB_ID: log_i("Received default response"); break;
294
+ // default: log_w("Receive unhandled Zigbee action(0x%x) callback", callback_id); break;
295
+ // }
296
+
297
+ // //TODO: get destination endpoint from the message:
298
+ // uint8_t dst_endpoint = ((esp_zb_zcl_set_attr_value_message_t *)message)->info.dst_endpoint;
299
+
300
+
301
+
302
+ // return ret;
303
+ // }
304
+
305
+ // static esp_err_t zb_attribute_set_handler(const esp_zb_zcl_set_attr_value_message_t *message) {
306
+ // if (!message) {
307
+ // log_e("Empty message");
308
+ // }
309
+ // if (message->info.status != ESP_ZB_ZCL_STATUS_SUCCESS) {
310
+ // log_e("Received message: error status(%d)", message->info.status);
311
+ // }
312
+
313
+ // log_i(
314
+ // "Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)", message->info.dst_endpoint, message->info.cluster, message->attribute.id,
315
+ // message->attribute.data.size
316
+ // );
317
+
318
+ // // List through all Zigbee EPs and call the callback function, with the message
319
+ // for (std::list<Zigbee_EP*>::iterator it = Zigbee.ep_objects.begin(); it != Zigbee.ep_objects.end(); ++it) {
320
+ // if (message->info.dst_endpoint == (*it)->_endpoint) {
321
+ // //TODO: implement argument passing to the callback function
322
+ // //if Zigbee_EP argument is set, pass it to the callback function
323
+ // // if ((*it)->_arg) {
324
+ // // (*it)->_cb(message, (*it)->_arg);
325
+ // // }
326
+ // // else {
327
+ // (*it)->_cb(message); //method zb_attribute_set_handler in the LIGHT EP
328
+ // // }
329
+ // }
330
+ // }
331
+ // return ESP_OK;
332
+ // }
333
+
334
+ // static esp_err_t zb_attribute_reporting_handler(const esp_zb_zcl_report_attr_message_t *message) {
335
+ // if (!message) {
336
+ // log_e("Empty message");
337
+ // }
338
+ // if (message->status != ESP_ZB_ZCL_STATUS_SUCCESS) {
339
+ // log_e("Received message: error status(%d)", message->status);
340
+ // }
341
+ // log_i(
342
+ // "Received report from address(0x%x) src endpoint(%d) to dst endpoint(%d) cluster(0x%x)", message->src_address.u.short_addr, message->src_endpoint,
343
+ // message->dst_endpoint, message->cluster
344
+ // );
345
+ // // List through all Zigbee EPs and call the callback function, with the message
346
+ // for (std::list<Zigbee_EP*>::iterator it = Zigbee.ep_objects.begin(); it != Zigbee.ep_objects.end(); ++it) {
347
+ // if (message->info.dst_endpoint == (*it)->_endpoint) {
348
+ // //TODO: implement argument passing to the callback function
349
+ // //if Zigbee_EP argument is set, pass it to the callback function
350
+ // // if ((*it)->_arg) {
351
+ // // (*it)->_cb(message, (*it)->_arg);
352
+ // // }
353
+ // // else {
354
+ // (*it)->_cb(message);
355
+ // // }
356
+ // }
357
+ // }
358
+ // return ESP_OK;
359
+ // }
360
+
361
+
362
+
303
363
// TODO: Implement scanning network
304
364
// /**
305
365
// * @brief Active scan available network.
0 commit comments