forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ias_zone.cpp
215 lines (179 loc) · 6.5 KB
/
ias_zone.cpp
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
/*
* Copyright (c) 2017 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <QString>
#include <QVariantMap>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "json.h"
// server send
#define CMD_STATUS_CHANGE_NOTIFICATION 0x00
#define CMD_ZONE_ENROLL_REQUEST 0x01
// server receive
#define CMD_ZONE_ENROLL_RESPONSE 0x00
// Zone status flags
#define STATUS_ALARM1 0x0001
#define STATUS_ALARM2 0x0002
#define STATUS_TAMPER 0x0004
#define STATUS_BATTERY 0x0008
#define STATUS_SUPERVISION 0x0010
#define STATUS_RESTORE_REP 0x0020
#define STATUS_TROUBLE 0x0040
#define STATUS_AC_MAINS 0x0080
#define STATUS_TEST 0x0100
#define STATUS_BATTERY_DEFECT 0x0200
/*! Handle packets related to the ZCL IAS Zone cluster.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the IAS zone server command
*/
void DeRestPluginPrivate::handleIasZoneClusterIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
Q_UNUSED(ind);
QDataStream stream(zclFrame.payload());
stream.setByteOrder(QDataStream::LittleEndian);
if (!(zclFrame.frameControl() & deCONZ::ZclFCDirectionServerToClient))
{
return;
}
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReadAttributesResponseId)
{
// during setup the IAS Zone type will be read
// start to proceed discovery here
if (findSensorsState == FindSensorsActive)
{
if (!fastProbeTimer->isActive())
{
fastProbeTimer->start(5);
}
}
}
if (!zclFrame.isClusterCommand())
{
return;
}
if (zclFrame.commandId() == CMD_STATUS_CHANGE_NOTIFICATION)
{
quint16 zoneStatus;
quint8 extendedStatus;
quint8 zoneId;
quint16 delay;
stream >> zoneStatus;
stream >> extendedStatus; // reserved, set to 0
stream >> zoneId;
stream >> delay;
DBG_Printf(DBG_ZCL, "IAS Zone Status Change, status: 0x%04X, zoneId: %u, delay: %u\n", zoneStatus, zoneId, delay);
Sensor *sensor = 0;
for (Sensor &s : sensors)
{
if (s.deletedState() != Sensor::StateNormal)
{
continue;
}
if (s.type() != QLatin1String("ZHAPresence") &&
s.type() != QLatin1String("ZHAOpenClose"))
{
continue;
}
if ((ind.srcAddress().hasExt() && s.address().ext() == ind.srcAddress().ext()) ||
(ind.srcAddress().hasNwk() && s.address().nwk() == ind.srcAddress().nwk()))
{
sensor = &s;
break;
}
}
if (!sensor)
{
return;
}
ResourceItem *item = sensor->item(RStatePresence);
if (!item)
{
item = sensor->item(RStateOpen);
}
if (item)
{
sensor->incrementRxCounter();
bool alarm = (zoneStatus & (STATUS_ALARM1 | STATUS_ALARM2)) ? true : false;
item->setValue(alarm);
sensor->updateStateTimestamp();
sensor->setNeedSaveDatabase(true);
deCONZ::NumericUnion num = {0};
num.u16 = zoneStatus;
sensor->setZclValue(NodeValue::UpdateByZclReport, IAS_ZONE_CLUSTER_ID, 0x0000, num);
ResourceItem *item2 = sensor->item(RConfigReachable);
if (item2 && !item2->toBool())
{
item2->setValue(true);
Event e(RSensors, RConfigReachable, sensor->id(), item2);
enqueueEvent(e);
}
updateSensorEtag(sensor);
if (item->lastSet() == item->lastChanged())
{
Event e(RSensors, item->descriptor().suffix, sensor->id(), item);
enqueueEvent(e);
}
enqueueEvent(Event(RSensors, RStateLastUpdated, sensor->id()));
if (alarm && item->descriptor().suffix == RStatePresence)
{ // prepare to automatically set presence to false
item2 = sensor->item(RConfigDuration);
if (item2 && item2->toNumber() > 0)
{
sensor->durationDue = item->lastSet().addSecs(item2->toNumber());
}
}
}
}
else if (zclFrame.commandId() == CMD_ZONE_ENROLL_REQUEST)
{
quint16 zoneType;
quint16 manufacturer;
stream >> zoneType;
stream >> manufacturer;
DBG_Printf(DBG_ZCL, "IAS Zone Enroll Request, zone type: 0x%04X, manufacturer: 0x%04X\n", zoneType, manufacturer);
sendIasZoneEnrollResponse(ind, zclFrame);
}
}
/*! Sends IAS Zone enroll response to IAS Zone server.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the IAS Zone enroll request
*/
void DeRestPluginPrivate::sendIasZoneEnrollResponse(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
deCONZ::ApsDataRequest req;
deCONZ::ZclFrame outZclFrame;
req.setProfileId(ind.profileId());
req.setClusterId(ind.clusterId());
req.setDstAddressMode(ind.srcAddressMode());
req.dstAddress() = ind.srcAddress();
req.setDstEndpoint(ind.srcEndpoint());
req.setSrcEndpoint(endpoint());
outZclFrame.setSequenceNumber(zclFrame.sequenceNumber());
outZclFrame.setCommandId(CMD_ZONE_ENROLL_RESPONSE);
outZclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&outZclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
quint8 code = 0x00; // success
quint8 zoneId = 100;
stream << code;
stream << zoneId;
}
{ // ZCL frame
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
outZclFrame.writeToStream(stream);
}
if (apsCtrl && apsCtrl->apsdeDataRequest(req) != deCONZ::Success)
{
DBG_Printf(DBG_INFO, "IAS Zone failed to send enroll reponse\n");
}
}