1 //-----------------------------------------------------------------------------
2 //
3 //	MultiChannelAssociation.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_MULTI_CHANNEL_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/MultiChannelAssociation.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 MultiChannelAssociationCmd
41 {
42 	MultiChannelAssociationCmd_Set				= 0x01,
43 	MultiChannelAssociationCmd_Get				= 0x02,
44 	MultiChannelAssociationCmd_Report			= 0x03,
45 	MultiChannelAssociationCmd_Remove			= 0x04,
46 	MultiChannelAssociationCmd_GroupingsGet	= 0x05,
47 	MultiChannelAssociationCmd_GroupingsReport	= 0x06
48 };
49 
50 // <MultiChannelAssociation::MultiChannelAssociation>
51 // Constructor
52 //-----------------------------------------------------------------------------
MultiChannelAssociation(uint32 const _homeId,uint8 const _nodeId)53 MultiChannelAssociation::MultiChannelAssociation
54 (
55 	uint32 const _homeId,
56 	uint8 const _nodeId
57 ):
58 	CommandClass( _homeId, _nodeId ),
59 	m_queryAll(false),
60 	m_numGroups(0),
61 	m_alwaysSetInstance(false)
62 {
63 	SetStaticRequest( StaticRequest_Values );
64 }
65 
66 //-----------------------------------------------------------------------------
67 // <MultiChannelAssociation::ReadXML>
68 // Read the saved association data
69 //-----------------------------------------------------------------------------
ReadXML(TiXmlElement const * _ccElement)70 void MultiChannelAssociation::ReadXML
71 (
72 	TiXmlElement const* _ccElement
73 )
74 {
75 	CommandClass::ReadXML( _ccElement );
76 
77 	TiXmlElement const* associationsElement = _ccElement->FirstChildElement();
78 	while( associationsElement )
79 	{
80 		char const* str = associationsElement->Value();
81 		if( str && !strcmp( str, "Associations" ) )
82 		{
83 			int intVal;
84 			if( TIXML_SUCCESS == associationsElement->QueryIntAttribute( "num_groups", &intVal ) )
85 			{
86 				m_numGroups = (uint8)intVal;
87 			}
88 
89 			TiXmlElement const* groupElement = associationsElement->FirstChildElement();
90 			while( groupElement )
91 			{
92 				if( Node* node = GetNodeUnsafe() )
93 				{
94 					Group* group = new Group( GetHomeId(), GetNodeId(), groupElement );
95 					node->AddGroup( group );
96 				}
97 
98 				groupElement = groupElement->NextSiblingElement();
99 			}
100 
101 			break;
102 		}
103 
104 		associationsElement = associationsElement->NextSiblingElement();
105 	}
106 	char const*  str = _ccElement->Attribute("ForceInstances");
107 	if( str )
108 	{
109                 m_alwaysSetInstance = !strcmp( str, "true");
110 	}
111 
112 }
113 
114 //-----------------------------------------------------------------------------
115 // <MultiChannelAssociation::WriteXML>
116 // Save the association data
117 //-----------------------------------------------------------------------------
WriteXML(TiXmlElement * _ccElement)118 void MultiChannelAssociation::WriteXML
119 (
120 	TiXmlElement* _ccElement
121 )
122 {
123 	CommandClass::WriteXML( _ccElement );
124 
125 	if( Node* node = GetNodeUnsafe() )
126 	{
127 		TiXmlElement* associationsElement = new TiXmlElement( "Associations" );
128 
129 		char str[8];
130 		snprintf( str, 8, "%d", m_numGroups );
131 		associationsElement->SetAttribute( "num_groups", str );
132 
133 		_ccElement->LinkEndChild( associationsElement );
134 		node->WriteGroups( associationsElement );
135 	}
136 	if (m_alwaysSetInstance) {
137 		_ccElement->SetAttribute("ForceInstances", "true");
138 	}
139 }
140 
141 //-----------------------------------------------------------------------------
142 // <MultiChannelAssociation::RequestState>
143 // Nothing to do for Association
144 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)145 bool MultiChannelAssociation::RequestState
146 (
147 	uint32 const _requestFlags,
148 	uint8 const _instance,
149 	Driver::MsgQueue const _queue
150 )
151 {
152 	if( ( _requestFlags & RequestFlag_Static ) && HasStaticRequest( StaticRequest_Values ) )
153 	{
154 		// Request the supported group info
155 		return RequestValue( _requestFlags, 0, _instance, _queue );
156 	}
157 
158 	return false;
159 }
160 
161 //-----------------------------------------------------------------------------
162 // <MultiChannelAssociation::RequestValue>
163 // Nothing to do for Association
164 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)165 bool MultiChannelAssociation::RequestValue
166 (
167 	uint32 const _requestFlags,
168 	uint8 const _dummy1,	// = 0 (not used)
169 	uint8 const _instance,
170 	Driver::MsgQueue const _queue
171 )
172 {
173 	if( _instance != 1 )
174 	{
175 		// This command class doesn't work with multiple instances
176 		return false;
177 	}
178 	// Request the supported group info
179 	Msg* msg = new Msg( "MultiChannelAssociationCmd_GroupingsGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
180 	msg->Append( GetNodeId() );
181 	msg->Append( 2 );
182 	msg->Append( GetCommandClassId() );
183 	msg->Append( MultiChannelAssociationCmd_GroupingsGet );
184 	msg->Append( GetDriver()->GetTransmitOptions() );
185 	GetDriver()->SendMsg( msg, _queue );
186 	return true;
187 }
188 
189 //-----------------------------------------------------------------------------
190 // <Association::RequestAllGroups>
191 // Request the contents of each group in turn
192 //-----------------------------------------------------------------------------
RequestAllGroups(uint32 const _requestFlags)193 void MultiChannelAssociation::RequestAllGroups
194 (
195 	uint32 const _requestFlags
196 )
197 {
198 	m_queryAll = true;
199 
200 	// Request the contents of the individual groups in turn.
201 	if( m_numGroups == 0xff )
202 	{
203 		// 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.
204 		Log::Write( LogLevel_Info, GetNodeId(), "Number of association groups reported for node %d is 255, which requires special case handling.", GetNodeId() );
205 		QueryGroup( 0xff, _requestFlags );
206 	}
207 	else
208 	{
209 		// 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.
210 		Log::Write( LogLevel_Info, GetNodeId(), "Number of association groups reported for node %d is %d.", GetNodeId(), m_numGroups );
211 		QueryGroup( 1, _requestFlags );
212 	}
213 }
214 
215 //-----------------------------------------------------------------------------
216 // <MultiChannelAssociation::HandleMsg>
217 // Handle a message from the Z-Wave network
218 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)219 bool MultiChannelAssociation::HandleMsg
220 (
221 	uint8 const* _data,
222 	uint32 const _length,
223 	uint32 const _instance	// = 1
224 )
225 {
226 	bool handled = false;
227 	uint32 i;
228 
229 	if( Node* node = GetNodeUnsafe() )
230 	{
231 		if( MultiChannelAssociationCmd_GroupingsReport == (MultiChannelAssociationCmd)_data[0] )
232 		{
233 			// Retrieve the number of groups this device supports.
234 			// The groups will be queried with the session data.
235 			m_numGroups = _data[1];
236 			Log::Write( LogLevel_Info, GetNodeId(), "Received Multi Instance Association Groupings report from node %d. Number of groups is %d", GetNodeId(), m_numGroups );
237 			ClearStaticRequest( StaticRequest_Values );
238 			handled = true;
239 		}
240 		else if( MultiChannelAssociationCmd_Report == (MultiChannelAssociationCmd)_data[0] )
241 		{
242 			// Get the group info
243 			uint8 groupIdx = _data[1];
244 			uint8 maxAssociations = _data[2];		// If the maxAssociations is zero, this is not a supported group.
245 			uint8 numReportsToFollow = _data[3];	// If a device supports a lot of associations, they may come in more than one message.
246 
247 			if( maxAssociations )
248 			{
249 				if( _length >= 5 )
250 				{
251 					// format:
252 					//   node A
253 					//   node B
254 					//   0x00 Marker
255 					//   node C
256 					//   instance #
257 					//   node D
258 					//   instance #
259 					Log::Write( LogLevel_Info, GetNodeId(), "Received Multi Instance Association report from node %d, group %d", GetNodeId(), groupIdx );
260 					Log::Write( LogLevel_Info, GetNodeId(), "  The group contains:" );
261 					bool pastMarker = false;
262 					for( i=0; i < _length-5; ++i )
263 					{
264 						if (_data[i+4] == 0x00)
265 						{
266 							pastMarker = true;
267 						}
268 						else
269 						{
270 							if (!pastMarker)
271 							{
272 								Log::Write( LogLevel_Info, GetNodeId(), "    Node %d",  _data[i+4] );
273 								InstanceAssociation association;
274 								association.m_nodeId=_data[i+4];
275 								association.m_instance=0x00;
276 								m_pendingMembers.push_back( association );
277 							}
278 							else
279 							{
280 								Log::Write( LogLevel_Info, GetNodeId(), "    Node %d instance %d",  _data[i+4], _data[i+5] );
281 								InstanceAssociation association;
282 								association.m_nodeId=_data[i+4];
283 								association.m_instance=_data[i+5];
284 								m_pendingMembers.push_back( association );
285 								i++;
286 							}
287 						}
288 					}
289 				}
290 
291 				if( numReportsToFollow )
292 				{
293 					// We're expecting more reports for this group
294 					Log::Write( LogLevel_Info, GetNodeId(), "%d more association reports expected for node %d, group %d", numReportsToFollow, GetNodeId(), groupIdx );
295 					return true;
296 				}
297 				else
298 				{
299 					// No more reports to come for this group, so we can apply the pending list
300 					Group* group = node->GetGroup( groupIdx );
301 					if( NULL == group )
302 					{
303 						// Group has not been created yet
304 						group = new Group( GetHomeId(), GetNodeId(), groupIdx, maxAssociations );
305 						node->AddGroup( group );
306 					}
307 					group->SetMultiInstance( true );
308 
309 					// Update the group with its new contents
310 					group->OnGroupChanged( m_pendingMembers );
311 					m_pendingMembers.clear();
312 				}
313 			}
314 			else
315 			{
316 				// maxAssociations is zero, so we've reached the end of the query process
317 				Log::Write( LogLevel_Info, GetNodeId(), "Max associations for node %d, group %d is zero.  Querying associations for this node is complete.", GetNodeId(), groupIdx );
318 				node->AutoAssociate();
319 				m_queryAll = false;
320 			}
321 
322 			if( m_queryAll )
323 			{
324 				// Work out which is the next group we will query.
325 				// If we are currently on group 255, the next group will be 1.
326 				uint8 nextGroup = groupIdx + 1;
327 				if( !nextGroup )
328 				{
329 					nextGroup = 1;
330 				}
331 
332 				if( nextGroup <= m_numGroups )
333 				{
334 					// Query the next group
335 					QueryGroup( nextGroup, 0 );
336 				}
337 				else
338 				{
339 					// We're all done
340 					Log::Write( LogLevel_Info, GetNodeId(), "Querying associations for node %d is complete.", GetNodeId() );
341 					node->AutoAssociate();
342 					m_queryAll = false;
343 				}
344 			}
345 
346 			handled = true;
347 		}
348 	}
349 
350 	return handled;
351 }
352 
353 //-----------------------------------------------------------------------------
354 // <MultiChannelAssociation::QueryGroup>
355 // Request details of an association group
356 //-----------------------------------------------------------------------------
QueryGroup(uint8 _groupIdx,uint32 const _requestFlags)357 void MultiChannelAssociation::QueryGroup
358 (
359 	uint8 _groupIdx,
360 	uint32 const _requestFlags
361 )
362 {
363 	if ( IsGetSupported() )
364 	{
365 		Log::Write( LogLevel_Info, GetNodeId(), "Get MultiChannelAssociation for group %d of node %d", _groupIdx, GetNodeId() );
366 		Msg* msg = new Msg( "MultiChannelAssociationCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
367 		msg->Append( GetNodeId() );
368 		msg->Append( 3 );
369 		msg->Append( GetCommandClassId() );
370 		msg->Append( MultiChannelAssociationCmd_Get );
371 		msg->Append( _groupIdx );
372 		msg->Append( GetDriver()->GetTransmitOptions() );
373 		GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
374 		return;
375 	} else {
376 		Log::Write(  LogLevel_Info, GetNodeId(), "MultiChannelAssociationCmd_Get Not Supported on this node");
377 	}
378 	return;
379 }
380 
381 //-----------------------------------------------------------------------------
382 // <MultiChannelAssociation::Set>
383 // Add an association between devices
384 //-----------------------------------------------------------------------------
Set(uint8 _groupIdx,uint8 _targetNodeId,uint8 _instance)385 void MultiChannelAssociation::Set
386 (
387 	uint8 _groupIdx,
388 	uint8 _targetNodeId,
389  	uint8 _instance
390 )
391 {
392 
393 	/* for Qubino devices, we should always set a Instance if its the ControllerNode, so MultChannelEncap works.  - See Bug #857 */
394 	if ( ( m_alwaysSetInstance  == true )
395 			&& ( _instance == 0 )
396 			&& ( GetDriver()->GetControllerNodeId() == _targetNodeId ) )
397 	{
398 		_instance = 0x01;
399 	}
400 
401 	Log::Write( LogLevel_Info, GetNodeId(), "MultiChannelAssociation::Set - Adding instance %d on node %d to group %d of node %d",
402 	           _instance, _targetNodeId, _groupIdx, GetNodeId() );
403 
404 	if ( _instance == 0x00 )
405 	{
406 		Msg* msg = new Msg( "MultiChannelAssociationCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
407 		msg->Append( GetNodeId() );
408 		msg->Append( 4 );
409 		msg->Append( GetCommandClassId() );
410 		msg->Append( MultiChannelAssociationCmd_Set );
411 		msg->Append( _groupIdx );
412 		msg->Append( _targetNodeId );
413 		msg->Append( GetDriver()->GetTransmitOptions() );
414 		GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
415 	}
416 	else
417 	{
418 		Msg* msg = new Msg( "MultiChannelAssociationCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
419 		msg->Append( GetNodeId() );
420 		msg->Append( 6 );
421 		msg->Append( GetCommandClassId() );
422 		msg->Append( MultiChannelAssociationCmd_Set );
423 		msg->Append( _groupIdx );
424 		msg->Append( 0x00 ); // marker
425 		msg->Append( _targetNodeId );
426 		msg->Append( _instance );
427 		msg->Append( GetDriver()->GetTransmitOptions() );
428 		GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
429 	}
430 }
431 
432 //-----------------------------------------------------------------------------
433 // <MultiChannelAssociation::Remove>
434 // Remove an association between devices
435 //-----------------------------------------------------------------------------
Remove(uint8 _groupIdx,uint8 _targetNodeId,uint8 _instance)436 void MultiChannelAssociation::Remove
437 (
438 	uint8 _groupIdx,
439 	uint8 _targetNodeId,
440  	uint8 _instance
441 )
442 {
443 	Log::Write( LogLevel_Info, GetNodeId(), "MultiChannelAssociation::Remove - Removing instance %d on node %d from group %d of node %d",
444 	           _instance, _targetNodeId, _groupIdx, GetNodeId());
445 
446 	if ( _instance == 0x00 )
447 	{
448 		Msg* msg = new Msg( "MultiChannelAssociationCmd_Remove", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
449 		msg->Append( GetNodeId() );
450 		msg->Append( 4 );
451 		msg->Append( GetCommandClassId() );
452 		msg->Append( MultiChannelAssociationCmd_Remove );
453 		msg->Append( _groupIdx );
454 		msg->Append( _targetNodeId );
455 		msg->Append( GetDriver()->GetTransmitOptions() );
456 		GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
457 	}
458 	else
459 	{
460 		Msg* msg = new Msg( "MultiChannelAssociationCmd_Remove", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
461 		msg->Append( GetNodeId() );
462 		msg->Append( 6 );
463 		msg->Append( GetCommandClassId() );
464 		msg->Append( MultiChannelAssociationCmd_Remove );
465 		msg->Append( _groupIdx );
466 		msg->Append( 0x00 ); // marker
467 		msg->Append( _targetNodeId );
468 		msg->Append( _instance );
469 		msg->Append( GetDriver()->GetTransmitOptions() );
470 		GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
471 	}
472 }
473 
474