forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZigbeeCore.h
175 lines (144 loc) · 6.54 KB
/
ZigbeeCore.h
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
/* Zigbee core class */
#pragma once
#include "soc/soc_caps.h"
#include "sdkconfig.h"
#if CONFIG_ZB_ENABLED
#include "esp_zigbee_core.h"
#include "zdo/esp_zigbee_zdo_common.h"
#include <esp32-hal-log.h>
#include <list>
#include "ZigbeeEP.h"
class ZigbeeEP;
typedef void (*voidFuncPtr)(void);
typedef void (*voidFuncPtrArg)(void *);
typedef esp_zb_network_descriptor_t zigbee_scan_result_t;
// enum of Zigbee Roles
typedef enum {
ZIGBEE_COORDINATOR = 0,
ZIGBEE_ROUTER = 1,
ZIGBEE_END_DEVICE = 2
} zigbee_role_t;
#define ZB_SCAN_RUNNING (-1)
#define ZB_SCAN_FAILED (-2)
#define ZB_BEGIN_TIMEOUT_DEFAULT 30000 // 30 seconds
#define ZIGBEE_DEFAULT_ED_CONFIG() \
{ \
.esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, .install_code_policy = false, \
.nwk_cfg = { \
.zed_cfg = \
{ \
.ed_timeout = ESP_ZB_ED_AGING_TIMEOUT_64MIN, \
.keep_alive = 3000, \
}, \
}, \
}
#define ZIGBEE_DEFAULT_ROUTER_CONFIG() \
{ \
.esp_zb_role = ESP_ZB_DEVICE_TYPE_ROUTER, .install_code_policy = false, .nwk_cfg = { \
.zczr_cfg = \
{ \
.max_children = 10, \
}, \
} \
}
#define ZIGBEE_DEFAULT_COORDINATOR_CONFIG() \
{ \
.esp_zb_role = ESP_ZB_DEVICE_TYPE_COORDINATOR, .install_code_policy = false, .nwk_cfg = { \
.zczr_cfg = \
{ \
.max_children = 10, \
}, \
} \
}
#define ZIGBEE_DEFAULT_UART_RCP_RADIO_CONFIG() \
{ \
.radio_mode = ZB_RADIO_MODE_UART_RCP, \
.radio_uart_config = { \
.port = UART_NUM_1, \
.rx_pin = GPIO_NUM_NC, \
.tx_pin = GPIO_NUM_NC, \
.uart_config = \
{ \
.baud_rate = 460800, \
.data_bits = UART_DATA_8_BITS, \
.parity = UART_PARITY_DISABLE, \
.stop_bits = UART_STOP_BITS_1, \
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \
.rx_flow_ctrl_thresh = 0, \
.source_clk = UART_SCLK_DEFAULT, \
}, \
}, \
}
class ZigbeeCore {
private:
esp_zb_radio_config_t _radio_config;
esp_zb_host_config_t _host_config;
uint32_t _primary_channel_mask;
uint32_t _begin_timeout;
int16_t _scan_status;
uint8_t _scan_duration;
bool _rx_on_when_idle;
esp_zb_ep_list_t *_zb_ep_list;
zigbee_role_t _role;
bool _started;
bool _connected;
uint8_t _open_network;
zigbee_scan_result_t *_scan_result;
SemaphoreHandle_t lock;
bool zigbeeInit(esp_zb_cfg_t *zb_cfg, bool erase_nvs);
static void scanCompleteCallback(esp_zb_zdp_status_t zdo_status, uint8_t count, esp_zb_network_descriptor_t *nwk_descriptor);
const char *getDeviceTypeString(esp_zb_ha_standard_devices_t deviceId);
void searchBindings();
static void bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_info, void *user_ctx);
void resetNVRAMChannelMask(); // Reset to default mask also in NVRAM
void setNVRAMChannelMask(uint32_t mask); // Set channel mask in NVRAM
public:
ZigbeeCore();
~ZigbeeCore() {}
std::list<ZigbeeEP *> ep_objects;
bool begin(zigbee_role_t role = ZIGBEE_END_DEVICE, bool erase_nvs = false);
bool begin(esp_zb_cfg_t *role_cfg, bool erase_nvs = false);
// bool end();
bool started() {
return _started;
}
bool connected() {
return _connected;
}
zigbee_role_t getRole() {
return _role;
}
void addEndpoint(ZigbeeEP *ep);
//void removeEndpoint(ZigbeeEP *ep);
void setRadioConfig(esp_zb_radio_config_t config);
esp_zb_radio_config_t getRadioConfig();
void setHostConfig(esp_zb_host_config_t config);
esp_zb_host_config_t getHostConfig();
void setPrimaryChannelMask(uint32_t mask); // By default all channels are scanned (11-26) -> mask 0x07FFF800
void setScanDuration(uint8_t duration); // Can be set from 1 - 4. 1 is fastest, 4 is slowest
uint8_t getScanDuration() {
return _scan_duration;
}
void setRxOnWhenIdle(bool rx_on_when_idle) {
_rx_on_when_idle = rx_on_when_idle;
}
bool getRxOnWhenIdle() {
return _rx_on_when_idle;
}
void setTimeout(uint32_t timeout) {
_begin_timeout = timeout;
}
void setRebootOpenNetwork(uint8_t time);
void openNetwork(uint8_t time);
//scan_duration Time spent scanning each channel, in units of ((1 << scan_duration) + 1) * a beacon time. (15.36 microseconds)
void scanNetworks(uint32_t channel_mask = ESP_ZB_TRANSCEIVER_ALL_CHANNELS_MASK, uint8_t scan_duration = 5);
// Zigbee scan complete status check, -2: failed or not started, -1: running, 0: no networks found, >0: number of networks found
int16_t scanComplete();
zigbee_scan_result_t *getScanResult();
void scanDelete();
void factoryReset(bool restart = true);
// Friend function declaration to allow access to private members
friend void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct);
};
extern ZigbeeCore Zigbee;
#endif // CONFIG_ZB_ENABLED