-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbasic_aps_plugin.cpp
293 lines (252 loc) · 8.31 KB
/
basic_aps_plugin.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
/*
* Copyright (C) 2014 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 <QtPlugin>
#include <QTimer>
#include "basic_aps_plugin.h"
/*! Duration for which the idle state will be hold before state machine proceeds. */
#define IDLE_TIMEOUT (10 * 1000)
/*! Duration to wait for Match_Descr_rsp frames after sending the request. */
#define WAIT_MATCH_DESCR_RESP_TIMEOUT (10 * 1000)
/*! Plugin constructor.
\param parent - the parent object
*/
BasicApsPlugin::BasicApsPlugin(QObject *parent) :
QObject(parent)
{
m_state = StateIdle;
// keep a pointer to the ApsController
m_apsCtrl = deCONZ::ApsController::instance();
DBG_Assert(m_apsCtrl != 0);
// APSDE-DATA.confirm handler
connect(m_apsCtrl, SIGNAL(apsdeDataConfirm(const deCONZ::ApsDataConfirm&)),
this, SLOT(apsdeDataConfirm(const deCONZ::ApsDataConfirm&)));
// APSDE-DATA.indication handler
connect(m_apsCtrl, SIGNAL(apsdeDataIndication(const deCONZ::ApsDataIndication&)),
this, SLOT(apsdeDataIndication(const deCONZ::ApsDataIndication&)));
// timer used for state changes and timeouts
m_timer = new QTimer(this);
m_timer->setSingleShot(true);
connect(m_timer, SIGNAL(timeout()),
this, SLOT(timerFired()));
// start the state machine
m_timer->start(1000);
}
/*! Deconstructor for plugin.
*/
BasicApsPlugin::~BasicApsPlugin()
{
m_apsCtrl = 0;
}
/*! APSDE-DATA.indication callback.
\param ind - the indication primitive
\note Will be called from the main application for every incoming indication.
Any filtering for nodes, profiles, clusters must be handled by this plugin.
*/
void BasicApsPlugin::apsdeDataIndication(const deCONZ::ApsDataIndication &ind)
{
if (ind.profileId() == ZDP_PROFILE_ID)
{
if (ind.clusterId() == ZDP_MATCH_DESCRIPTOR_RSP_CLID)
{
handleMatchDescriptorResponse(ind);
}
}
}
/*! APSDE-DATA.confirm callback.
\param conf - the confirm primitive
\note Will be called from the main application for each incoming confirmation,
even if the APSDE-DATA.request was not issued by this plugin.
*/
void BasicApsPlugin::apsdeDataConfirm(const deCONZ::ApsDataConfirm &conf)
{
std::list<deCONZ::ApsDataRequest>::iterator i = m_apsReqQueue.begin();
std::list<deCONZ::ApsDataRequest>::iterator end = m_apsReqQueue.end();
// search the list of currently active requests
// and check if the confirmation belongs to one of them
for (; i != end; ++i)
{
if (i->id() == conf.id())
{
m_apsReqQueue.erase(i);
if (conf.status() == deCONZ::ApsSuccessStatus)
{
stateMachineEventHandler(EventSendDone);
}
else
{
DBG_Printf(DBG_INFO, "APS-DATA.confirm failed with status: 0x%02X\n", conf.status());
stateMachineEventHandler(EventSendFailed);
}
return;
}
}
}
/*! Handles a match descriptor response.
\param ind a ZDP Match_Descr_rsp
*/
void BasicApsPlugin::handleMatchDescriptorResponse(const deCONZ::ApsDataIndication &ind)
{
QDataStream stream(ind.asdu());
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t zdpSeq;
uint8_t status;
uint16_t nwkAddrOfInterest;
uint8_t matchLength;
uint8_t endpoint;
stream >> zdpSeq;
// only handle the Match_Descr_rsp which belongs to our request
if (zdpSeq != m_matchDescrZdpSeq)
{
return;
}
stream >> status;
DBG_Printf(DBG_INFO, "received match descriptor response (id: %u) from %s\n", m_matchDescrZdpSeq, qPrintable(ind.srcAddress().toStringExt()));
if (status == 0x00) // SUCCESS
{
stream >> nwkAddrOfInterest;
stream >> matchLength;
while (matchLength && !stream.atEnd())
{
matchLength--;
stream >> endpoint;
DBG_Printf(DBG_INFO, "\tmatch descriptor endpoint: 0x%02X\n", endpoint);
}
// done restart state machine
if (m_state == StateWaitMatchDescriptorResponse)
{
setState(StateIdle);
m_timer->stop();
m_timer->start(IDLE_TIMEOUT);
}
}
}
/*! Handler for simple timeout timer.
*/
void BasicApsPlugin::timerFired()
{
stateMachineEventHandler(EventTimeout);
}
/*! Sends a ZDP Match_Descr_req for On/Off cluster (ClusterID=0x0006).
\return true if request was added to queue
*/
bool BasicApsPlugin::sendMatchDescriptorRequest()
{
DBG_Assert(m_state == StateIdle);
if (m_apsCtrl->networkState() != deCONZ::InNetwork)
{
return false;
}
deCONZ::ApsDataRequest apsReq;
// set destination addressing
apsReq.setDstAddressMode(deCONZ::ApsNwkAddress);
apsReq.dstAddress().setNwk(deCONZ::BroadcastRxOnWhenIdle);
apsReq.setDstEndpoint(ZDO_ENDPOINT);
apsReq.setSrcEndpoint(ZDO_ENDPOINT);
apsReq.setProfileId(ZDP_PROFILE_ID);
apsReq.setClusterId(ZDP_MATCH_DESCRIPTOR_CLID);
// prepare payload
QDataStream stream(&apsReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
// generate and remember a new ZDP transaction sequence number
m_matchDescrZdpSeq = (uint8_t)qrand();
// write payload according to ZigBee specification (2.4.3.1.7 Match_Descr_req)
// here we search for ZLL device which provides a OnOff server cluster
// NOTE: explicit castings ensure correct size of the fields
stream << m_matchDescrZdpSeq; // ZDP transaction sequence number
stream << (quint16)deCONZ::BroadcastRxOnWhenIdle; // NWKAddrOfInterest
stream << (quint16)ZLL_PROFILE_ID; // ProfileID
stream << (quint8)0x01; // NumInClusters
stream << (quint16)0x0006; // OnOff ClusterID
stream << (quint8)0x00; // NumOutClusters
if (m_apsCtrl && (m_apsCtrl->apsdeDataRequest(apsReq) == deCONZ::Success))
{
// remember request
m_apsReqQueue.push_back(apsReq);
return true;
}
return false;
}
/*! Sets the state machine state.
\param state the new state
*/
void BasicApsPlugin::setState(BasicApsPlugin::State state)
{
if (m_state != state)
{
m_state = state;
}
}
/*! deCONZ will ask this plugin which features are supported.
\param feature - feature to be checked
\return true if supported
*/
bool BasicApsPlugin::hasFeature(Features feature)
{
switch (feature)
{
default:
break;
}
return false;
}
/*! Main state machine event handler.
\param event the event which occured
*/
void BasicApsPlugin::stateMachineEventHandler(BasicApsPlugin::Event event)
{
if (m_state == StateIdle)
{
if (event == EventTimeout)
{
m_apsReqQueue.clear();
if (sendMatchDescriptorRequest())
{
DBG_Printf(DBG_INFO, "send match descriptor request (id: %u)\n", m_matchDescrZdpSeq);
setState(StateWaitMatchDescriptorResponse);
m_timer->start(WAIT_MATCH_DESCR_RESP_TIMEOUT);
}
else
{
// try again later
m_timer->start(IDLE_TIMEOUT);
}
}
}
else if (m_state == StateWaitMatchDescriptorResponse)
{
if (event == EventSendDone)
{
DBG_Printf(DBG_INFO, "send match descriptor request done (id: %u)\n", m_matchDescrZdpSeq);
}
else if (event == EventSendFailed)
{
DBG_Printf(DBG_INFO, "send match descriptor request failed (id: %u)\n", m_matchDescrZdpSeq);
// go back to idle state and wait some time
setState(StateIdle);
m_timer->start(IDLE_TIMEOUT);
}
else if (event == EventTimeout)
{
DBG_Printf(DBG_INFO, "stop wait for match descriptor response (id: %u)\n", m_matchDescrZdpSeq);
// go back to idle state and wait some time
setState(StateIdle);
m_timer->start(IDLE_TIMEOUT);
}
}
}
/*! Returns the name of this plugin.
*/
const char *BasicApsPlugin::name()
{
return "Basic APS Plugin";
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(basic_aps_plugin, BasicApsPlugin)
#endif