-
Notifications
You must be signed in to change notification settings - Fork 507
/
ias_ace.cpp
372 lines (305 loc) · 12.1 KB
/
ias_ace.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// TODO remove dependency on plugin
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "ias_ace.h"
#include "ias_zone.h"
// Arm mode command
//-------------------
// 0x00 Disarm
// 0x01 Arm Day/Home Zones Only
// 0x02 Arm Night/Sleep Zones Only
// 0x03 Arm All Zones
// Arm mode response
//-------------------
// 0x00 All Zones Disarmed
// 0x01 Only Day/Home Zones Armed
// 0x02 Only Night/Sleep Zones Armed
// 0x03 All Zones Armed
// 0x04 Invalid Arm/Disarm Code
// 0x05 Not ready to arm
// 0x06 Already disarmed
// Panel status
// --------------
// 0x00 Panel disarmed (all zones disarmed) and ready to arm
// 0x01 Armed stay
// 0x02 Armed night
// 0x03 Armed away
// 0x04 Exit delay
// 0x05 Entry delay
// 0x06 Not ready to arm
// 0x07 In alarm
// 0x08 Arming Stay
// 0x09 Arming Night
// 0x0a Arming Away
// Alarm Status
// ------------
// 0x00 No alarm
// 0x01 Burglar
// 0x02 Fire
// 0x03 Emergency
// 0x04 Police Panic
// 0x05 Fire Panic
// 0x06 Emergency Panic (i.e., medical issue)
// Audible Notification
// ----------------------
// 0x00 Mute (i.e., no audible notification)
// 0x01 Default sound
// 0x80-0xff Manufacturer specific
#define IAS_ACE_ARM_MODE_DISARM 0x00
#define IAS_ACE_ARM_MODE_ARM_DAY_HOME_ZONES_ONLY 0x01
#define IAS_ACE_ARM_MODE_ARM_NIGHT_SLEEP_ZONES_ONLY 0x02
#define IAS_ACE_ARM_MODE_ARM_ALL_ZONES 0x03
#define IAS_ACE_ARM_NOTF_ALL_ZONES_DISARMED 0x00
#define IAS_ACE_ARM_NOTF_ONLY_DAY_HOME_ZONES_ARMED 0x01
#define IAS_ACE_ARM_NOTF_ONLY_NIGHT_SLEEP_ZONES_ARMED 0x02
#define IAS_ACE_ARM_NOTF_ALL_ZONES_ARMED 0x03
#define IAS_ACE_ARM_NOTF_INVALID_ARM_DISARM_CODE 0x04
#define IAS_ACE_ARM_NOTF_NOT_READY_TO_ARM 0x05
#define IAS_ACE_ARM_NOTF_ALREADY_DISARMED 0x06
// strings mapping directly to IAS_ACE_ARM_MODE_* and IAS_ACE_ARM_NOTF_*
static const std::array<QLatin1String, 7> IAS_ArmResponse = {
QLatin1String("disarmed"),
QLatin1String("armed_stay"),
QLatin1String("armed_night"),
QLatin1String("armed_away"),
QLatin1String("invalid_code"),
QLatin1String("not_ready"),
QLatin1String("already_disarmed")
};
static const std::array<QLatin1String, 11> IAS_PanelStates = {
QLatin1String("disarmed"),
QLatin1String("armed_stay"),
QLatin1String("armed_night"),
QLatin1String("armed_away"),
QLatin1String("exit_delay"),
QLatin1String("entry_delay"),
QLatin1String("not_ready"),
QLatin1String("in_alarm"),
QLatin1String("arming_stay"),
QLatin1String("arming_night"),
QLatin1String("arming_away")
};
static void sendArmResponse(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame, quint8 armMode, ApsControllerWrapper &apsCtrlWrapper);
static void sendGetPanelStatusResponse(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame, quint8 panelStatus, quint8 secs, ApsControllerWrapper &apsCtrlWrapper);
QLatin1String IAS_PanelStatusToString(quint8 panelStatus)
{
if (panelStatus < IAS_PanelStates.size())
{
return IAS_PanelStates[panelStatus];
}
return QLatin1String("");
}
int IAS_PanelStatusFromString(const QString &panelStatus)
{
const auto i = std::find(IAS_PanelStates.cbegin(), IAS_PanelStates.cend(), panelStatus);
if (i != IAS_PanelStates.cend())
{
return std::distance(IAS_PanelStates.cbegin(), i);
}
return -1;
}
static quint8 handleArmCommand(AlarmSystem *alarmSys, quint8 armMode, const QString &pinCode, quint64 srcAddress)
{
if (!alarmSys || armMode > IAS_ACE_ARM_MODE_ARM_ALL_ZONES)
{
return IAS_ACE_ARM_NOTF_NOT_READY_TO_ARM;
}
if (!alarmSys->isValidCode(pinCode, srcAddress))
{
return IAS_ACE_ARM_NOTF_INVALID_ARM_DISARM_CODE;
}
const quint8 armMode0 = alarmSys->targetArmMode();
if (armMode0 == IAS_ACE_ARM_MODE_DISARM && armMode == armMode0)
{
return IAS_ACE_ARM_NOTF_ALREADY_DISARMED;
}
static_assert (IAS_ACE_ARM_MODE_DISARM == AS_ArmModeDisarmed, "");
static_assert (IAS_ACE_ARM_MODE_ARM_DAY_HOME_ZONES_ONLY == AS_ArmModeArmedStay, "");
static_assert (IAS_ACE_ARM_MODE_ARM_NIGHT_SLEEP_ZONES_ONLY == AS_ArmModeArmedNight, "");
static_assert (IAS_ACE_ARM_MODE_ARM_ALL_ZONES == AS_ArmModeArmedAway, "");
if (armMode0 != armMode)
{
alarmSys->setTargetArmMode(AS_ArmMode(armMode));
}
return armMode;
}
void IAS_IasAceClusterIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame, AlarmSystems *alarmSystems, ApsControllerWrapper &apsCtrlWrapper)
{
if (zclFrame.isDefaultResponse())
{
return;
}
if (zclFrame.frameControl() & deCONZ::ZclFCDirectionServerToClient)
{
return;
}
Sensor *sensor = plugin->getSensorNodeForAddressAndEndpoint(ind.srcAddress(), ind.srcEndpoint(), QLatin1String("ZHAAncillaryControl"));
if (!sensor)
{
return;
}
bool stateUpdated = false;
if (zclFrame.commandId() == IAS_ACE_CMD_ARM && zclFrame.payload().size() >= 2)
{
// [0] arm mode (enum8)
const quint8 armMode = quint8(zclFrame.payload().at(0));
if (armMode > IAS_ACE_ARM_MODE_ARM_ALL_ZONES)
{
DBG_Printf(DBG_IAS, "[IAS ACE] 0x%016llX invalid arm mode: %d, skip\n", ind.srcAddress().ext(), armMode);
return;
}
quint8 armRsp = IAS_ACE_ARM_NOTF_NOT_READY_TO_ARM;
// [1] arm/disarm code in payload (pascal string, allowed to be empty, e.g. for keyfobs)
QString armCode;
if (zclFrame.payload().size() > 2)
{
int length = zclFrame.payload().at(1);
if (length <= zclFrame.payload().size() - 2)
{
armCode = QString::fromUtf8(zclFrame.payload().constData() + 2, length);
}
else
{
armRsp = IAS_ACE_ARM_NOTF_INVALID_ARM_DISARM_CODE;
armCode = QLatin1String("invalid_code");
}
}
// [2] zone id (uint8, ignore, we don't do anything with it)
// const quint8 zoneId = static_cast<quint8>(zclFrame.payload().at(zclFrame.payload().size() - 1));
DBG_Printf(DBG_IAS, "[IAS ACE] 0x%016llX arm command received, arm mode: 0x%02X, code length: %d\n", ind.srcAddress().ext(), armMode, armCode.size());
AlarmSystem *alarmSys = AS_GetAlarmSystemForDevice(ind.srcAddress().ext(), *alarmSystems);
if (alarmSys)
{
armRsp = handleArmCommand(alarmSys, armMode, armCode, ind.srcAddress().ext());
}
{
ResourceItem *actionItem = sensor->item(RStateAction);
if (actionItem && armRsp < IAS_ArmResponse.size())
{
actionItem->setValue(QString(IAS_ArmResponse[armRsp]));
enqueueEvent(Event(sensor->prefix(), actionItem->descriptor().suffix, sensor->id(), armMode));
stateUpdated = true;
}
}
sendArmResponse(ind, zclFrame, armRsp, apsCtrlWrapper);
}
else if (zclFrame.commandId() == IAS_ACE_CMD_GET_PANEL_STATUS)
{
quint8 panelStatus = IAS_ACE_PANEL_STATUS_NOT_READY_TO_ARM;
quint8 secondsRemaining = 0;
AlarmSystem *alarmSys = AS_GetAlarmSystemForDevice(ind.srcAddress().ext(), *alarmSystems);
if (alarmSys)
{
panelStatus = alarmSys->iasAcePanelStatus();
if (panelStatus == IAS_ACE_PANEL_STATUS_ENTRY_DELAY || panelStatus == IAS_ACE_PANEL_STATUS_EXIT_DELAY)
{
secondsRemaining = quint8(alarmSys->secondsRemaining());
}
}
sendGetPanelStatusResponse(ind, zclFrame, panelStatus, secondsRemaining, apsCtrlWrapper);
}
else if (zclFrame.commandId() >= IAS_ACE_CMD_EMERGENCY && zclFrame.commandId() <= IAS_ACE_CMD_PANIC)
{
ResourceItem *actionItem = sensor->item(RStateAction);
const std::array<QLatin1String, 3> cmds = {
QLatin1String("emergency"),
QLatin1String("fire"),
QLatin1String("panic")
};
const quint8 index = zclFrame.commandId() - IAS_ACE_CMD_EMERGENCY;
if (actionItem && index < cmds.size())
{
actionItem->setValue(QString(cmds[index]));
enqueueEvent(Event(sensor->prefix(), actionItem->descriptor().suffix, sensor->id(), zclFrame.commandId()));
stateUpdated = true;
}
}
else
{
DBG_Printf(DBG_IAS, "[IAS ACE] 0x%016llX unhandled command: 0x%02X\n", ind.srcAddress().ext(), zclFrame.commandId());
}
if (stateUpdated)
{
sensor->updateStateTimestamp();
enqueueEvent(Event(RSensors, RStateLastUpdated, sensor->id()));
plugin->updateSensorEtag(sensor);
sensor->setNeedSaveDatabase(true);
plugin->queSaveDb(DB_SENSORS, DB_SHORT_SAVE_DELAY);
}
}
static void sendArmResponse(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame, quint8 armMode, ApsControllerWrapper &apsCtrlWrapper)
{
DBG_Assert(armMode <= IAS_ACE_ARM_NOTF_ALREADY_DISARMED);
if (armMode > IAS_ACE_ARM_NOTF_ALREADY_DISARMED)
{
return;
}
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(plugin->endpoint());
outZclFrame.setSequenceNumber(zclFrame.sequenceNumber());
outZclFrame.setCommandId(IAS_ACE_CMD_ARM_RESPONSE);
outZclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionServerToClient |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&outZclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << armMode;
}
{ // ZCL frame
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
outZclFrame.writeToStream(stream);
}
if (apsCtrlWrapper.apsdeDataRequest(req) != deCONZ::Success)
{
DBG_Printf(DBG_IAS, "[IAS ACE] 0x%016llX failed to send IAS ACE arm reponse.\n", ind.srcAddress().ext());
}
}
static void sendGetPanelStatusResponse(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame, quint8 panelStatus, quint8 secs, ApsControllerWrapper &apsCtrlWrapper)
{
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(plugin->endpoint());
DBG_Printf(DBG_IAS, "[IAS ACE] 0x%016llX panel status response: 0x%02X\n", ind.srcAddress().ext(), panelStatus);
outZclFrame.setSequenceNumber(zclFrame.sequenceNumber());
outZclFrame.setCommandId(IAS_ACE_CMD_GET_PANEL_STATUS_RESPONSE);
outZclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionServerToClient); // deCONZ::ZclFCDisableDefaultResponse
{ // payload
QDataStream stream(&outZclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << (quint8) panelStatus; // Panel status
stream << (quint8) secs; // Seconds Remaining
stream << (quint8) 0x01; // Audible Notification
if (panelStatus == IAS_ACE_PANEL_STATUS_IN_ALARM)
{
// TODO make this dynamic and managed by alarm system
stream << (quint8) 0x03; // Alarm status, emergency
}
else
{
stream << (quint8) 0x00; // Alarm status
}
}
{ // ZCL frame
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
outZclFrame.writeToStream(stream);
}
if (apsCtrlWrapper.apsdeDataRequest(req) != deCONZ::Success)
{
DBG_Printf(DBG_IAS, "[IAS ACE] 0x%016llX failed to send IAS ACE get panel reponse.\n", ind.srcAddress().ext());
}
}