1 //-----------------------------------------------------------------------------
2 //
3 // Association.cpp
4 //
5 // Implementation of the Z-Wave COMMAND_CLASS_ASSOCIATION
6 //
7 // Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
8 //
9 // SOFTWARE NOTICE AND LICENSE
10 //
11 // This file is part of OpenZWave.
12 //
13 // OpenZWave is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU Lesser General Public License as published
15 // by the Free Software Foundation, either version 3 of the License,
16 // or (at your option) any later version.
17 //
18 // OpenZWave is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public License
24 // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
25 //
26 //-----------------------------------------------------------------------------
27
28 #include "tinyxml.h"
29 #include "command_classes/CommandClasses.h"
30 #include "command_classes/Association.h"
31 #include "Defs.h"
32 #include "Msg.h"
33 #include "Driver.h"
34 #include "Node.h"
35 #include "Group.h"
36 #include "platform/Log.h"
37
38 using namespace OpenZWave;
39
40 enum AssociationCmd
41 {
42 AssociationCmd_Set = 0x01,
43 AssociationCmd_Get = 0x02,
44 AssociationCmd_Report = 0x03,
45 AssociationCmd_Remove = 0x04,
46 AssociationCmd_GroupingsGet = 0x05,
47 AssociationCmd_GroupingsReport = 0x06
48 };
49
50
51 //-----------------------------------------------------------------------------
52 // <Association::Association>
53 // Constructor
54 //-----------------------------------------------------------------------------
Association(uint32 const _homeId,uint8 const _nodeId)55 Association::Association
56 (
57 uint32 const _homeId,
58 uint8 const _nodeId
59 ):
60 CommandClass( _homeId, _nodeId ),
61 m_queryAll(false),
62 m_numGroups(0)
63 {
64 SetStaticRequest( StaticRequest_Values );
65 }
66
67 //-----------------------------------------------------------------------------
68 // <Association::ReadXML>
69 // Read the saved association data
70 //-----------------------------------------------------------------------------
ReadXML(TiXmlElement const * _ccElement)71 void Association::ReadXML
72 (
73 TiXmlElement const* _ccElement
74 )
75 {
76 CommandClass::ReadXML( _ccElement );
77
78 TiXmlElement const* associationsElement = _ccElement->FirstChildElement();
79 while( associationsElement )
80 {
81 char const* str = associationsElement->Value();
82 if( str && !strcmp( str, "Associations" ) )
83 {
84 int intVal;
85 if( TIXML_SUCCESS == associationsElement->QueryIntAttribute( "num_groups", &intVal ) )
86 {
87 m_numGroups = (uint8)intVal;
88 }
89
90 TiXmlElement const* groupElement = associationsElement->FirstChildElement();
91 while( groupElement )
92 {
93 if( Node* node = GetNodeUnsafe() )
94 {
95 Group* group = new Group( GetHomeId(), GetNodeId(), groupElement );
96 node->AddGroup( group );
97 }
98
99 groupElement = groupElement->NextSiblingElement();
100 }
101
102 break;
103 }
104
105 associationsElement = associationsElement->NextSiblingElement();
106 }
107 }
108
109 //-----------------------------------------------------------------------------
110 // <Association::WriteXML>
111 // Save the association data
112 //-----------------------------------------------------------------------------
WriteXML(TiXmlElement * _ccElement)113 void Association::WriteXML
114 (
115 TiXmlElement* _ccElement
116 )
117 {
118 CommandClass::WriteXML( _ccElement );
119
120 if( Node* node = GetNodeUnsafe() )
121 {
122 TiXmlElement* associationsElement = new TiXmlElement( "Associations" );
123
124 char str[8];
125 snprintf( str, 8, "%d", m_numGroups );
126 associationsElement->SetAttribute( "num_groups", str );
127
128 _ccElement->LinkEndChild( associationsElement );
129 node->WriteGroups( associationsElement );
130 }
131 }
132
133 //-----------------------------------------------------------------------------
134 // <Association::RequestState>
135 // Nothing to do for Association
136 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)137 bool Association::RequestState
138 (
139 uint32 const _requestFlags,
140 uint8 const _instance,
141 Driver::MsgQueue const _queue
142 )
143 {
144 if( ( _requestFlags & RequestFlag_Static ) && HasStaticRequest( StaticRequest_Values ) )
145 {
146 // Request the supported group info
147 return RequestValue( _requestFlags, 0, _instance, _queue );
148 }
149
150 return false;
151 }
152
153 //-----------------------------------------------------------------------------
154 // <Association::RequestValue>
155 // Nothing to do for Association
156 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)157 bool Association::RequestValue
158 (
159 uint32 const _requestFlags,
160 uint8 const _dummy1, // = 0 (not used)
161 uint8 const _instance,
162 Driver::MsgQueue const _queue
163 )
164 {
165 if( _instance != 1 )
166 {
167 // This command class doesn't work with multiple instances
168 return false;
169 }
170 // Request the supported group info
171 Msg* msg = new Msg( "AssociationCmd_GroupingsGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
172 msg->Append( GetNodeId() );
173 msg->Append( 2 );
174 msg->Append( GetCommandClassId() );
175 msg->Append( AssociationCmd_GroupingsGet );
176 msg->Append( GetDriver()->GetTransmitOptions() );
177 GetDriver()->SendMsg( msg, _queue );
178 return true;
179 }
180
181 //-----------------------------------------------------------------------------
182 // <Association::RequestAllGroups>
183 // Request the contents of each group in turn
184 //-----------------------------------------------------------------------------
RequestAllGroups(uint32 const _requestFlags)185 void Association::RequestAllGroups
186 (
187 uint32 const _requestFlags
188 )
189 {
190 m_queryAll = true;
191
192 // Request the contents of the individual groups in turn.
193 if( m_numGroups == 0xff )
194 {
195 // We start with group 255, and will then move to group 1, 2 etc and stop when we find a group with a maxAssociations of zero.
196 Log::Write( LogLevel_Info, GetNodeId(), "Number of association groups reported for node %d is 255, which requires special case handling.", GetNodeId() );
197 QueryGroup( 0xff, _requestFlags );
198 }
199 else
200 {
201 // We start with group 1, and will then move to group 2, 3 etc and stop when the group index is greater than m_numGroups.
202 Log::Write( LogLevel_Info, GetNodeId(), "Number of association groups reported for node %d is %d.", GetNodeId(), m_numGroups );
203 QueryGroup( 1, _requestFlags );
204 }
205 }
206
207 //-----------------------------------------------------------------------------
208 // <Association::HandleMsg>
209 // Handle a message from the Z-Wave network
210 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)211 bool Association::HandleMsg
212 (
213 uint8 const* _data,
214 uint32 const _length,
215 uint32 const _instance // = 1
216 )
217 {
218 bool handled = false;
219 uint32 i;
220
221 if( Node* node = GetNodeUnsafe() )
222 {
223 if( AssociationCmd_GroupingsReport == (AssociationCmd)_data[0] )
224 {
225 // Retrieve the number of groups this device supports.
226 // The groups will be queried with the session data.
227 m_numGroups = _data[1];
228 Log::Write( LogLevel_Info, GetNodeId(), "Received Association Groupings report from node %d. Number of groups is %d", GetNodeId(), m_numGroups );
229 ClearStaticRequest( StaticRequest_Values );
230 handled = true;
231 }
232 else if( AssociationCmd_Report == (AssociationCmd)_data[0] )
233 {
234 // Get the group info
235 uint8 groupIdx = _data[1];
236 uint8 maxAssociations = _data[2]; // If the maxAssociations is zero, this is not a supported group.
237 uint8 numReportsToFollow = _data[3]; // If a device supports a lot of associations, they may come in more than one message.
238
239 if( maxAssociations )
240 {
241 if( _length >= 5 )
242 {
243 uint8 numAssociations = _length - 5;
244
245 Log::Write( LogLevel_Info, GetNodeId(), "Received Association report from node %d, group %d, containing %d associations", GetNodeId(), groupIdx, numAssociations );
246 if( numAssociations )
247 {
248 Log::Write( LogLevel_Info, GetNodeId(), " The group contains:" );
249 for( i=0; i<numAssociations; ++i )
250 {
251 Log::Write( LogLevel_Info, GetNodeId(), " Node %d", _data[i+4] );
252 m_pendingMembers.push_back( _data[i+4] );
253 }
254 }
255 }
256
257 if( numReportsToFollow )
258 {
259 // We're expecting more reports for this group
260 Log::Write( LogLevel_Info, GetNodeId(), "%d more association reports expected for node %d, group %d", numReportsToFollow, GetNodeId(), groupIdx );
261 return true;
262 }
263 else
264 {
265 // No more reports to come for this group, so we can apply the pending list
266 Group* group = node->GetGroup( groupIdx );
267 if( NULL == group )
268 {
269 // Group has not been created yet
270 group = new Group( GetHomeId(), GetNodeId(), groupIdx, maxAssociations );
271 node->AddGroup( group );
272 }
273
274 // Update the group with its new contents
275 group->OnGroupChanged( m_pendingMembers );
276 m_pendingMembers.clear();
277 }
278 }
279 else
280 {
281 // maxAssociations is zero, so we've reached the end of the query process
282 Log::Write( LogLevel_Info, GetNodeId(), "Max associations for node %d, group %d is zero. Querying associations for this node is complete.", GetNodeId(), groupIdx );
283 node->AutoAssociate();
284 m_queryAll = false;
285 }
286
287 if( m_queryAll )
288 {
289 // Work out which is the next group we will query.
290 // If we are currently on group 255, the next group will be 1.
291 uint8 nextGroup = groupIdx + 1;
292 if( !nextGroup )
293 {
294 nextGroup = 1;
295 }
296
297 if( nextGroup <= m_numGroups )
298 {
299 // Query the next group
300 QueryGroup( nextGroup, 0 );
301 }
302 else
303 {
304 // We're all done
305 Log::Write( LogLevel_Info, GetNodeId(), "Querying associations for node %d is complete.", GetNodeId() );
306 node->AutoAssociate();
307 m_queryAll = false;
308 }
309 }
310
311 handled = true;
312 }
313 }
314
315 return handled;
316 }
317
318 //-----------------------------------------------------------------------------
319 // <Association::QueryGroup>
320 // Request details of an association group
321 //-----------------------------------------------------------------------------
QueryGroup(uint8 _groupIdx,uint32 const _requestFlags)322 void Association::QueryGroup
323 (
324 uint8 _groupIdx,
325 uint32 const _requestFlags
326 )
327 {
328 if ( IsGetSupported() )
329 {
330 Log::Write( LogLevel_Info, GetNodeId(), "Get Associations for group %d of node %d", _groupIdx, GetNodeId() );
331 Msg* msg = new Msg( "AssociationCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
332 msg->Append( GetNodeId() );
333 msg->Append( 3 );
334 msg->Append( GetCommandClassId() );
335 msg->Append( AssociationCmd_Get );
336 msg->Append( _groupIdx );
337 msg->Append( GetDriver()->GetTransmitOptions() );
338 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
339 return;
340 } else {
341 Log::Write( LogLevel_Info, GetNodeId(), "AssociationCmd_Get Not Supported on this node");
342 }
343 return;
344 }
345 //-----------------------------------------------------------------------------
346 // <Association::Set>
347 // Add an association between devices
348 //-----------------------------------------------------------------------------
Set(uint8 _groupIdx,uint8 _targetNodeId)349 void Association::Set
350 (
351 uint8 _groupIdx,
352 uint8 _targetNodeId
353 )
354 {
355 Log::Write( LogLevel_Info, GetNodeId(), "Association::Set - Adding node %d to group %d of node %d", _targetNodeId, _groupIdx, GetNodeId() );
356
357 Msg* msg = new Msg( "AssociationCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
358 msg->Append( GetNodeId() );
359 msg->Append( 4 );
360 msg->Append( GetCommandClassId() );
361 msg->Append( AssociationCmd_Set );
362 msg->Append( _groupIdx );
363 msg->Append( _targetNodeId );
364 msg->Append( GetDriver()->GetTransmitOptions() );
365 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
366 }
367
368 //-----------------------------------------------------------------------------
369 // <Association::Remove>
370 // Remove an association between devices
371 //-----------------------------------------------------------------------------
Remove(uint8 _groupIdx,uint8 _targetNodeId)372 void Association::Remove
373 (
374 uint8 _groupIdx,
375 uint8 _targetNodeId
376 )
377 {
378 Log::Write( LogLevel_Info, GetNodeId(), "Association::Remove - Removing node %d from group %d of node %d", _targetNodeId, _groupIdx, GetNodeId() );
379
380 Msg* msg = new Msg( "AssociationCmd_Remove", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
381 msg->Append( GetNodeId() );
382 msg->Append( 4 );
383 msg->Append( GetCommandClassId() );
384 msg->Append( AssociationCmd_Remove );
385 msg->Append( _groupIdx );
386 msg->Append( _targetNodeId );
387 msg->Append( GetDriver()->GetTransmitOptions() );
388 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
389 }
390
391