-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathZigbeeEP.h
160 lines (130 loc) · 5.52 KB
/
ZigbeeEP.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
/* Common Class for Zigbee End point */
#pragma once
#include "ZigbeeCore.h"
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
#include <Arduino.h>
#include <ColorFormat.h>
/* Useful defines */
#define ZB_CMD_TIMEOUT 10000 // 10 seconds
#define OTA_UPGRADE_QUERY_INTERVAL (1 * 60) // 1 hour = 60 minutes
#define ZB_ARRAY_LENTH(arr) (sizeof(arr) / sizeof(arr[0]))
#define RGB_TO_XYZ(r, g, b, X, Y, Z) \
{ \
X = (float)(0.412453 * (r) + 0.357580 * (g) + 0.180423 * (b)); \
Y = (float)(0.212671 * (r) + 0.715160 * (g) + 0.072169 * (b)); \
Z = (float)(0.019334 * (r) + 0.119193 * (g) + 0.950227 * (b)); \
}
typedef struct zbstring_s {
uint8_t len;
char data[];
} ESP_ZB_PACKED_STRUCT zbstring_t;
typedef struct zb_device_params_s {
esp_zb_ieee_addr_t ieee_addr;
uint8_t endpoint;
uint16_t short_addr;
} zb_device_params_t;
typedef enum {
ZB_POWER_SOURCE_UNKNOWN = 0x00,
ZB_POWER_SOURCE_MAINS = 0x01,
ZB_POWER_SOURCE_BATTERY = 0x03,
} zb_power_source_t;
/* Zigbee End Device Class */
class ZigbeeEP {
public:
ZigbeeEP(uint8_t endpoint = 10);
~ZigbeeEP() {}
// Set ep config and cluster list
void setEpConfig(esp_zb_endpoint_config_t ep_config, esp_zb_cluster_list_t *cluster_list) {
_ep_config = ep_config;
_cluster_list = cluster_list;
}
void setVersion(uint8_t version);
uint8_t getEndpoint() {
return _endpoint;
}
void printBoundDevices();
void printBoundDevices(Print &print);
std::list<zb_device_params_t *> getBoundDevices() const {
return _bound_devices;
}
static bool bound() {
return _is_bound;
}
static void allowMultipleBinding(bool bind) {
_allow_multiple_binding = bind;
}
// Set Manufacturer name and model
void setManufacturerAndModel(const char *name, const char *model);
// Methods to read manufacturer and model name from selected endpoint and short address
char *readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_addr_t ieee_addr);
char *readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_addr_t ieee_addr);
// Set Power source and battery percentage for battery powered devices
void setPowerSource(zb_power_source_t power_source, uint8_t percentage = 255);
void setBatteryPercentage(uint8_t percentage);
void reportBatteryPercentage();
// Set time
void addTimeCluster(tm time = {}, int32_t gmt_offset = 0); // gmt offset in seconds
void setTime(tm time);
void setTimezone(int32_t gmt_offset);
// Get time from Coordinator or specific endpoint (blocking until response)
struct tm getTime(uint8_t endpoint = 1, int32_t short_addr = 0x0000, esp_zb_ieee_addr_t ieee_addr = {0});
int32_t getTimezone(uint8_t endpoint = 1, int32_t short_addr = 0x0000, esp_zb_ieee_addr_t ieee_addr = {0}); // gmt offset in seconds
bool epAllowMultipleBinding() {
return _allow_multiple_binding;
}
// OTA methods
/**
* @brief Add OTA client to the Zigbee endpoint.
*
* @param file_version The current file version of the OTA client.
* @param downloaded_file_ver The version of the downloaded file.
* @param hw_version The hardware version of the device.
* @param manufacturer The manufacturer code (default: 0x1001).
* @param image_type The image type code (default: 0x1011).
* @param max_data_size The maximum data size for OTA transfer (default and recommended: 223).
*/
void addOTAClient(
uint32_t file_version, uint32_t downloaded_file_ver, uint16_t hw_version, uint16_t manufacturer = 0x1001, uint16_t image_type = 0x1011,
uint8_t max_data_size = 223
);
/**
* @brief Request OTA update from the server, first request is within a minute and the next requests are sent every hour automatically.
*/
void requestOTAUpdate();
// findEndpoind may be implemented by EPs to find and bind devices
virtual void findEndpoint(esp_zb_zdo_match_desc_req_param_t *cmd_req) {};
//list of all handlers function calls, to be override by EPs implementation
virtual void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) {};
virtual void zbAttributeRead(uint16_t cluster_id, const esp_zb_zcl_attribute_t *attribute) {};
virtual void zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute); //already implemented
virtual void zbIdentify(const esp_zb_zcl_set_attr_value_message_t *message);
virtual void zbWindowCoveringMovementCmd(const esp_zb_zcl_window_covering_movement_message_t *message) {};
virtual void zbReadTimeCluster(const esp_zb_zcl_attribute_t *attribute); //already implemented
virtual void zbIASZoneStatusChangeNotification(const esp_zb_zcl_ias_zone_status_change_notification_message_t *message) {};
virtual void zbIASZoneEnrollResponse(const esp_zb_zcl_ias_zone_enroll_response_message_t *message) {};
virtual void addBoundDevice(zb_device_params_t *device) {
_bound_devices.push_back(device);
_is_bound = true;
}
void onIdentify(void (*callback)(uint16_t)) {
_on_identify = callback;
}
private:
char *_read_manufacturer;
char *_read_model;
void (*_on_identify)(uint16_t time);
time_t _read_time;
int32_t _read_timezone;
protected:
uint8_t _endpoint;
esp_zb_ha_standard_devices_t _device_id;
esp_zb_endpoint_config_t _ep_config;
esp_zb_cluster_list_t *_cluster_list;
static bool _is_bound;
static bool _allow_multiple_binding;
std::list<zb_device_params_t *> _bound_devices;
SemaphoreHandle_t lock;
zb_power_source_t _power_source;
friend class ZigbeeCore;
};
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED