openzwave-sys 0.1.1

FFI Library to the opensource OpenZwave library
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//-----------------------------------------------------------------------------
//
//	Group.cpp
//
//	A set of associations in a Z-Wave device.
//
//	Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
//
//	SOFTWARE NOTICE AND LICENSE
//
//	This file is part of OpenZWave.
//
//	OpenZWave is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as published
//	by the Free Software Foundation, either version 3 of the License,
//	or (at your option) any later version.
//
//	OpenZWave is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU Lesser General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public License
//	along with OpenZWave.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------

#include <cstring>
#include "Group.h"
#include "Manager.h"
#include "Driver.h"
#include "Node.h"
#include "Notification.h"
#include "Options.h"
#include "command_classes/Association.h"
#include "command_classes/AssociationCommandConfiguration.h"
#include "command_classes/MultiInstanceAssociation.h"
#include "platform/Log.h"

#include "tinyxml.h"

using namespace OpenZWave;


//-----------------------------------------------------------------------------
// <Group::Group>
// Constructor
//-----------------------------------------------------------------------------
Group::Group
( 
	uint32 const _homeId,
	uint8 const _nodeId,
	uint8 const _groupIdx,
	uint8 const _maxAssociations
):
	m_homeId( _homeId ),
	m_nodeId( _nodeId ),
	m_groupIdx( _groupIdx ),
	m_maxAssociations( _maxAssociations ),
	m_auto( false ),
    m_multiInstance( false )
{
	char str[16];
	snprintf( str, sizeof(str), "Group %d", m_groupIdx );
	m_label = str;

	CheckAuto();
}

//-----------------------------------------------------------------------------
// <Group::Group>
// Constructor (from XML)
//-----------------------------------------------------------------------------
Group::Group
(
	uint32 const _homeId,
	uint8 const _nodeId,
	TiXmlElement const* _groupElement
):
	m_homeId( _homeId ),
	m_nodeId( _nodeId ),
	m_groupIdx( 0 ),
	m_maxAssociations( 0 ),
	m_auto( false ),
	m_multiInstance( false )
{
	int intVal;
	char const* str;
	vector<InstanceAssociation> pending;



	if( TIXML_SUCCESS == _groupElement->QueryIntAttribute( "index", &intVal ) )
	{
		m_groupIdx = (uint8)intVal;
	}

	/* call this so the config can override if necessary */
	CheckAuto();

	if( TIXML_SUCCESS == _groupElement->QueryIntAttribute( "max_associations", &intVal ) )
	{
		m_maxAssociations = (uint8)intVal;
	}

	str = _groupElement->Attribute( "auto" );
	if( str )
	{
		m_auto = !strcmp( str, "true" );
	}

	str = _groupElement->Attribute( "label" );
	if( str )
	{
		m_label = str;
	}

	str = _groupElement->Attribute( "multiInstance" );
	if( str )
	{
		m_multiInstance = !strcmp( str, "true" );
	}
		
	// Read the associations for this group
	TiXmlElement const* associationElement = _groupElement->FirstChildElement();
	while( associationElement )
	{
		char const* elementName = associationElement->Value();
		if( elementName && !strcmp( elementName, "Node" ) )
		{
			
			if( associationElement->QueryIntAttribute( "id", &intVal ) == TIXML_SUCCESS )
			{
				InstanceAssociation association;
				association.m_nodeId = (uint8)intVal;
				if( associationElement->QueryIntAttribute( "instance", &intVal ) == TIXML_SUCCESS )
					association.m_instance = (uint8)intVal;
				else
					association.m_instance = 0x00;

				pending.push_back( association );
			}
		}

		associationElement = associationElement->NextSiblingElement();
	}

	// Group must be added before OnGroupChanged is called so UpdateNodeRoutes can find it.
	// Since we do not want to update return routes UpdateNodeRoutes won't find the group
	// so nothing will go out from here. The not sending of return routes information
	// only works by a side effect of not finding the group.
	OnGroupChanged( pending );
}

//-----------------------------------------------------------------------------
// <Group::CheckAuto>
// Check if we should AutoAssociate for this group
//-----------------------------------------------------------------------------
void Group::CheckAuto
(

)
{
	// Auto-association by default is with group 1 or 255, with group 1 taking precedence.
	// Group 255 is always created first, so if this is group 1, we need to turn off the
	// auto flag in group 255.  All this messing about is to support the various behaviours
	// of certain Cooper devices.
	if( m_groupIdx == 255 )
	{
		m_auto = true;
	}
	else if( m_groupIdx == 1 )
	{
		m_auto = true;

		// Clear the flag from Group 255, if it exists.
		if( Driver* driver = Manager::Get()->GetDriver( m_homeId ) )
		{
			if( Node* node = driver->GetNodeUnsafe( m_nodeId ) )
			{
				if( Group* group = node->GetGroup( 255 ) )
				{
					group->SetAuto( false );
				}
			}
		}
	}
}



//-----------------------------------------------------------------------------
// <Group::WriteXML>
// Write ourselves to an XML document
//-----------------------------------------------------------------------------
void Group::WriteXML
(
	TiXmlElement* _groupElement
)
{
	char str[16];

	snprintf( str, 16, "%d", m_groupIdx );
	_groupElement->SetAttribute( "index", str );

	snprintf( str, 16, "%d", m_maxAssociations );
	_groupElement->SetAttribute( "max_associations", str );

	_groupElement->SetAttribute( "label", m_label.c_str() );
	_groupElement->SetAttribute( "auto", m_auto ? "true" : "false" );
	if( m_multiInstance )
	{
		_groupElement->SetAttribute( "multiInstance", m_multiInstance ? "true" : "false" );
	}
	
	for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
	{
		TiXmlElement* associationElement = new TiXmlElement( "Node" );
		
		snprintf( str, 16, "%d", it->first.m_nodeId );
		associationElement->SetAttribute( "id", str );
		if (it->first.m_instance != 0)
		{
			snprintf( str, 16, "%d", it->first.m_instance );
			associationElement->SetAttribute( "instance", str );
		}

		_groupElement->LinkEndChild( associationElement );
	}
}

//-----------------------------------------------------------------------------
// <Group::Contains>
// Whether a group contains a particular node
//-----------------------------------------------------------------------------
bool Group::Contains
(
	uint8 const _nodeId,
	uint8 const _instance
)
{
	for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
	{
		if ((it->first.m_nodeId == _nodeId) && (it->first.m_instance == _instance))
		{
			return true;
		}
	}
	return false;
}

//-----------------------------------------------------------------------------
// <Group::AddAssociation>
// Associate a node with this group
//-----------------------------------------------------------------------------
void Group::AddAssociation
(
	uint8 const _nodeId,
	uint8 const _instance
)
{
	if( Driver* driver = Manager::Get()->GetDriver( m_homeId ) )
	{
		if( Node* node = driver->GetNodeUnsafe( m_nodeId ) )
		{
			MultiInstanceAssociation* cc = static_cast<MultiInstanceAssociation*>( node->GetCommandClass( MultiInstanceAssociation::StaticGetCommandClassId() ));
			if( cc && IsMultiInstance() )
			{
				cc->Set( m_groupIdx, _nodeId, _instance );
				cc->QueryGroup( m_groupIdx, 0 );
			}
			else if( Association* cc = static_cast<Association*>( node->GetCommandClass( Association::StaticGetCommandClassId() ) ) )
			{
				cc->Set( m_groupIdx, _nodeId );
				cc->QueryGroup( m_groupIdx, 0 );
			}
			else
			{
				Log::Write( LogLevel_Info, m_nodeId, "No supported Association CC found" );
			} 
		}
	}
}

//-----------------------------------------------------------------------------
// <Group:RemoveAssociation>
// Remove a node from this group
//-----------------------------------------------------------------------------
void Group::RemoveAssociation
(
	uint8 const _nodeId,
	uint8 const _instance
)
{
	if( Driver* driver = Manager::Get()->GetDriver( m_homeId ) )
	{
		if( Node* node = driver->GetNodeUnsafe( m_nodeId ) )
		{
			MultiInstanceAssociation* cc = static_cast<MultiInstanceAssociation*>( node->GetCommandClass( MultiInstanceAssociation::StaticGetCommandClassId() ));
			if( cc && IsMultiInstance() )
			{
				cc->Remove( m_groupIdx, _nodeId, _instance );
				cc->QueryGroup( m_groupIdx, 0 );
			} 
			else if( Association* cc = static_cast<Association*>( node->GetCommandClass( Association::StaticGetCommandClassId() ) ) )
			{
					cc->Remove( m_groupIdx, _nodeId );
					cc->QueryGroup( m_groupIdx, 0 );
			}
			else
			{
				Log::Write( LogLevel_Info, m_nodeId, "No supported Association CC found" );
			}	
		}
	}
}

//-----------------------------------------------------------------------------
// <Group::OnGroupChanged>
// Change the group contents and notify the watchers
//-----------------------------------------------------------------------------
void Group::OnGroupChanged
(
	vector<uint8> const& _associations
)
{
	vector<InstanceAssociation> instanceAssociations;
	uint8 i;
	for( i=0; i<_associations.size(); ++i )
	{
		InstanceAssociation association;
		association.m_nodeId	= _associations[i];
		association.m_instance  = 0x00;								
		instanceAssociations.push_back( association );
	}
	OnGroupChanged(instanceAssociations);
	instanceAssociations.clear();
}

//-----------------------------------------------------------------------------
// <Group::OnGroupChanged>
// Change the group contents and notify the watchers
//-----------------------------------------------------------------------------
void Group::OnGroupChanged
(
	vector<InstanceAssociation> const& _associations
)
{
	bool notify = false;

	// If the number of associations is different, we'll save 
	// ourselves some work and clear the old set now.
	if( _associations.size() != m_associations.size() )
	{
		m_associations.clear();
		notify = true;
	}
	else
	{
		// Handle initial group creation case
		if ( _associations.size() == 0 && m_associations.size() == 0 )
		{
			notify = true;
		}
	}

	// Add the new associations. 
	uint8 oldSize = (uint8)m_associations.size();

	uint8 i;
	for( i=0; i<_associations.size(); ++i )
	{
		m_associations[_associations[i]] = AssociationCommandVec();
	}

	if( (!notify) && ( oldSize != m_associations.size() ) )
	{
		// The number of nodes in the original and new groups is the same, but
		// the number of associations has grown. There must be different nodes 
		// in the original and new sets of nodes in the group.  The easiest way
		// to sort this out is to clear the associations and add the new nodes again.
		m_associations.clear();
		for( i=0; i<_associations.size(); ++i )
		{
			m_associations[_associations[i]] = AssociationCommandVec();
		}
		notify = true;
	}

	if( notify )
	{
		// If the node supports COMMAND_CLASS_ASSOCIATION_COMMAND_CONFIGURATION, we need to request the command data.
		if( Driver* driver = Manager::Get()->GetDriver( m_homeId ) )
		{
			if( Node* node = driver->GetNodeUnsafe( m_nodeId ) )
			{
				if( AssociationCommandConfiguration* cc = static_cast<AssociationCommandConfiguration*>( node->GetCommandClass( AssociationCommandConfiguration::StaticGetCommandClassId() ) ) )
				{
					for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
					{
						cc->RequestCommands( m_groupIdx, it->first.m_nodeId );
					}
				}
			}
		}

		// Send notification that the group contents have changed
		Notification* notification = new Notification( Notification::Type_Group );
		notification->SetHomeAndNodeIds( m_homeId, m_nodeId );
		notification->SetGroupIdx( m_groupIdx );
		Manager::Get()->GetDriver( m_homeId )->QueueNotification( notification ); 
		// Update routes on remote node if necessary
		bool update = false;
		Options::Get()->GetOptionAsBool( "PerformReturnRoutes", &update );
		if( update )
		{
			Driver *drv = Manager::Get()->GetDriver( m_homeId );
			if (drv)
				drv->UpdateNodeRoutes( m_nodeId );
		}
	}
}

//-----------------------------------------------------------------------------
// <Group::GetAssociations>
// Get a list of associations for this group
//-----------------------------------------------------------------------------
uint32 Group::GetAssociations
( 
	uint8** o_associations 
)
{
	size_t numNodes = m_associations.size();
	if( !numNodes )
	{
		*o_associations = NULL;
		return 0;
	}

	uint8* associations = new uint8[numNodes]; // room for all associations, we only need room for the associations without instance
	uint32 i = 0;
	for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
	{
		if ( it->first.m_instance == 0x00) 
		{
			associations[i++] = it->first.m_nodeId;
		}
	}

	*o_associations = associations;
	return (uint32) i;
}

//-----------------------------------------------------------------------------
// <Group::GetAssociations>
// Get a list of associations for this group
//-----------------------------------------------------------------------------
uint32 Group::GetAssociations
( 
	InstanceAssociation** o_associations 
)
{
	size_t numNodes = m_associations.size();
	if( !numNodes )
	{
		*o_associations = NULL;
		return 0;
	}

	InstanceAssociation* associations = new InstanceAssociation[numNodes];
	uint32 i = 0;
	for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
	{
		associations[i++] = it->first;
	}

	*o_associations = associations;
	return (uint32) numNodes;
}

//-----------------------------------------------------------------------------
// Command methods (COMMAND_CLASS_ASSOCIATION_COMMAND_CONFIGURATION)
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// <Group::ClearCommands>
// Clear all the commands for the specified node
//-----------------------------------------------------------------------------
bool Group::ClearCommands
( 
	uint8 const _nodeId,
	uint8 const _instance
)
{	
	for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
	{
		if( (it->first.m_nodeId == _nodeId) && (it->first.m_instance == _instance) )
		{
			it->second.clear();
			return true;
		}
    }

	return false;
}

//-----------------------------------------------------------------------------
// <Group::AddCommand>
// Add a command to the list for the specified node
//-----------------------------------------------------------------------------
bool Group::AddCommand
(
	uint8 const _nodeId,
	uint8 const _length,
	uint8 const* _data,
	uint8 const _instance
)
{
	for( map<InstanceAssociation,AssociationCommandVec,classcomp>::iterator it = m_associations.begin(); it != m_associations.end(); ++it )
	{
		if( (it->first.m_nodeId == _nodeId) && (it->first.m_instance == _instance) )
		{
			it->second.push_back( AssociationCommand( _length, _data ) );
			return true;
		}
	}
	
	return false;
}

//-----------------------------------------------------------------------------
// <Group::AssociationCommand::AssociationCommand>
// Constructor
//-----------------------------------------------------------------------------
Group::AssociationCommand::AssociationCommand
(
	uint8 const _length,
	uint8 const* _data
):
	m_length( _length )
{
	m_data = new uint8[_length];
	memcpy( m_data, _data, _length );
}

//-----------------------------------------------------------------------------
// <Group::AssociationCommand::AssociationCommand>
// Destructor
//-----------------------------------------------------------------------------
Group::AssociationCommand::~AssociationCommand
(
)
{
	delete [] m_data;
}