-
Notifications
You must be signed in to change notification settings - Fork 505
/
basic.cpp
196 lines (169 loc) · 6.49 KB
/
basic.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
/*
* basic.cpp
*
* Implementation of Basic cluster server.
* Send ZCL attribute response to read request on Basic Cluster attributes.
*
* 0x0000 ZCL Version / Just to test
* 0xF000 Running time / Used for Legrand device
*
*/
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
/*! Handle packets related to the ZCL Basic cluster.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the read attribute request
*/
void DeRestPluginPrivate::handleBasicClusterIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReadAttributesId)
{
sendBasicClusterResponse(ind, zclFrame);
}
}
/*! Sends read attributes response to Basic client.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the read attributes request
*/
void DeRestPluginPrivate::sendBasicClusterResponse(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
deCONZ::ApsDataRequest req;
deCONZ::ZclFrame outZclFrame;
quint16 manucode = 0xFFFF;
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(deCONZ::ZclReadAttributesResponseId);
outZclFrame.setFrameControl(deCONZ::ZclFCProfileCommand |
deCONZ::ZclFCDirectionServerToClient |
deCONZ::ZclFCDisableDefaultResponse);
//is there manufacture field in the request, if yes add it.
if (zclFrame.frameControl() & deCONZ::ZclFCManufacturerSpecific)
{
manucode = zclFrame.manufacturerCode();
outZclFrame.setFrameControl(outZclFrame.frameControl() | deCONZ::ZclFCManufacturerSpecific);
outZclFrame.setManufacturerCode(manucode);
}
{ // payload
QDataStream stream(&outZclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
QDataStream instream(zclFrame.payload());
instream.setByteOrder(QDataStream::LittleEndian);
quint8 code = 0x00; // success
quint16 attr;
while (!instream.atEnd())
{
instream >> attr;
stream << attr;
switch (attr)
{
case 0x0000: // ZCL Version
stream << code;
stream << (quint8) deCONZ::Zcl8BitUint;
stream << (quint8) 0x02;
break;
case 0x0001: // Application Version
{
stream << code;
stream << (quint8) deCONZ::Zcl8BitUint;
Sensor *sensor = getSensorNodeForAddressAndEndpoint(ind.srcAddress(), ind.srcEndpoint());
if (sensor && sensor->modelId() == QLatin1String("TRADFRI remote control"))
{
// Since firmware version 2.3.014 when the large middle button is pressed the remote reads this attribute.
// If it isn't 17 as reported by earlier remote firmware, the left/right buttons don't send hold and long press commands anymore.
stream << quint8(17);
}
else
{
stream << quint8(0x00);
}
}
break;
case 0x0002: // Stack Version
stream << code;
stream << (quint8) deCONZ::Zcl8BitUint;
stream << (quint8) 0x00;
break;
case 0x0003: // HW Version
stream << code;
stream << (quint8) deCONZ::Zcl8BitUint;
stream << (quint8) 0x00;
break;
case 0x0004: // Manufacturer Name
{
const char *name = "dresden elektronik";
const quint8 length = strlen(name);
stream << code;
stream << (quint8) deCONZ::ZclCharacterString;
stream << length;
for (uint i = 0; i < length; i++)
{
stream << (quint8) name[i];
}
}
break;
case 0x0005: // Model Identifier
{
const QByteArray id = apsCtrl->getParameter(deCONZ::ParamDeviceName).toLatin1();
const quint8 length = id.length();
stream << code;
stream << (quint8) deCONZ::ZclCharacterString;
stream << length;
for (uint i = 0; i < length; i++)
{
stream << (quint8) id[i];
}
}
break;
case 0x0007: // Power Source
stream << code;
stream << (quint8) deCONZ::Zcl8BitEnum;
stream << (quint8) 0x04; // DC Power
break;
case 0x4000: // SW Build ID
{
const QByteArray version = gwFirmwareVersion.toLatin1();
const quint8 length = version.length();
stream << code;
stream << (quint8) deCONZ::ZclCharacterString;
stream << length;
for (uint i = 0; i < length; i++)
{
stream << (quint8) version[i];
}
}
break;
case 0xF000: // Legrand attribute used for pairing
if (manucode == VENDOR_LEGRAND)
{
stream << code;
stream << (quint8) deCONZ::Zcl32BitUint;
stream << (quint32) 0x000000d5;
}
else
{
stream << (quint8) 0x86; // unsupported_attribute
}
break;
default:
{
stream << (quint8) 0x86; // unsupported_attribute
}
break;
}
}
}
{ // ZCL frame
QDataStream stream(&req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
outZclFrame.writeToStream(stream);
}
if (apsCtrlWrapper.apsdeDataRequest(req) != deCONZ::Success)
{
DBG_Printf(DBG_INFO, "Basic failed to send reponse\n");
}
}