-
Notifications
You must be signed in to change notification settings - Fork 59
/
zigbee_utils.lua
168 lines (140 loc) · 4.6 KB
/
zigbee_utils.lua
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
local capabilities = require "st.capabilities"
local zcl_clusters = require "st.zigbee.zcl.clusters"
local constants = require "st.zigbee.constants"
local defaults = require "st.zigbee.defaults"
local utils = require "st.utils"
local json = require "dkjson"
local log = require "log"
local zigbee_utils = require "st.zigbee.utils"
local messages = require "st.zigbee.messages"
local bind_request = require "st.zigbee.zdo.bind_request"
local zdo_messages = require "st.zigbee.zdo"
local zutils = {}
zutils.supports_client_cluster = function(device, cluster_id)
for ep_id, ep in pairs(device.zigbee_endpoints) do
for _, cluster in ipairs(ep.client_clusters) do
if cluster == cluster_id then
return true
end
end
end
return false
end
zutils.find_first_ep = function (eps, cluster)
local tkeys = {}
for k in pairs(eps) do table.insert(tkeys, k) end
table.sort(tkeys)
for _, k in ipairs(tkeys) do
local ep = eps[k]
for _, clus in ipairs(ep.server_clusters) do
if clus == cluster then
return ep.id
end
end
end
return nil
end
local function to_hex(value)
return string.format("0x%04X", value)
end
local id_to_name_map = utils.deep_copy(zcl_clusters.id_to_name_map)
id_to_name_map[0x0010] = "BinaryOutput"
id_to_name_map[0x0012] = "MultistateInput"
id_to_name_map[0xFCC0] = "AqaraOpple"
local function clusters_to_string(name, cluster_table)
local res = name.."["
for _, cluster in ipairs(cluster_table) do
local name = id_to_name_map[cluster] or to_hex(cluster)
res = res .. name .. ", "
end
return res .. "]"
end
zutils.print_clusters = function(device)
for ep_id, ep in pairs(device.zigbee_endpoints) do
local msg = "Ep#" .. ep.id ..
" device_id:" .. to_hex(ep.device_id) ..
" profile_id:" .. to_hex(ep.profile_id)
if ep.model then
msg = msg .. " model:'" .. ep.model .. "'"
end
if ep.manufacturer then
msg = msg .. " manufacturer:'" .. ep.manufacturer .. "'"
end
log.info( msg ..
clusters_to_string(" Client clusters:", ep.client_clusters) ..
clusters_to_string(" Server clusters:", ep.server_clusters) )
end
end
zutils.build_bind_request = function(device, cluster, group)
local addr_header = messages.AddressHeader(
constants.HUB.ADDR,
constants.HUB.ENDPOINT,
device:get_short_address(),
device.fingerprinted_endpoint_id,
constants.ZDO_PROFILE_ID,
bind_request.BindRequest.ID)
local bind_req = bind_request.BindRequest(
device.zigbee_eui,
device:get_endpoint(cluster),
cluster,
bind_request.ADDRESS_MODE_16_BIT,
group)
return messages.ZigbeeMessageTx({
address_header = addr_header,
body = zdo_messages.ZdoMessageBody({zdo_body = bind_req})
})
end
zutils.build_read_binding_table = function(device)
local mgmt_bind_req = require "st.zigbee.zdo.mgmt_bind_request"
local addr_header = messages.AddressHeader(
constants.HUB.ADDR,
constants.HUB.ENDPOINT,
device:get_short_address(),
device.fingerprinted_endpoint_id,
constants.ZDO_PROFILE_ID,
mgmt_bind_req.BINDING_TABLE_REQUEST_CLUSTER_ID
)
return messages.ZigbeeMessageTx({
address_header = addr_header,
body = zdo_messages.ZdoMessageBody({zdo_body = mgmt_bind_req.MgmtBindRequest(0)})
})
end
zutils.send_bind_request = function(device, cluster, group)
return device:send( zutils.build_bind_request(device, cluster, group) )
end
zutils.send_unbind_request = function(device, cluster, group)
-- not tested
local addr_header = messages.AddressHeader(
constants.HUB.ADDR,
constants.HUB.ENDPOINT,
device:get_short_address(),
device.fingerprinted_endpoint_id,
constants.ZDO_PROFILE_ID,
0x0022)
local bind_req = bind_request.BindRequest(
device.zigbee_eui,
device:get_endpoint(cluster),
cluster,
bind_request.ADDRESS_MODE_16_BIT,
group)
return device:send( messages.ZigbeeMessageTx({
address_header = addr_header,
body = zdo_messages.ZdoMessageBody({zdo_body = bind_req})
}) )
end
zutils.send_read_binding_table = function(device)
local mgmt_bind_req = require "st.zigbee.zdo.mgmt_bind_request"
local addr_header = messages.AddressHeader(
constants.HUB.ADDR,
constants.HUB.ENDPOINT,
device:get_short_address(),
device.fingerprinted_endpoint_id,
constants.ZDO_PROFILE_ID,
mgmt_bind_req.BINDING_TABLE_REQUEST_CLUSTER_ID
)
return device:send( messages.ZigbeeMessageTx({
address_header = addr_header,
body = zdo_messages.ZdoMessageBody({zdo_body = mgmt_bind_req.MgmtBindRequest(0)})
}) )
end
return zutils