1 //-----------------------------------------------------------------------------
2 //
3 // AssociationCommandConfiguration.cpp
4 //
5 // Implementation of the COMMAND_CLASS_ASSOCIATION_COMMAND_CONFIGURATION
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 "command_classes/CommandClasses.h"
29 #include "command_classes/AssociationCommandConfiguration.h"
30 #include "Defs.h"
31 #include "Msg.h"
32 #include "Node.h"
33 #include "Group.h"
34 #include "Driver.h"
35 #include "platform/Log.h"
36 #include "value_classes/ValueBool.h"
37 #include "value_classes/ValueByte.h"
38 #include "value_classes/ValueShort.h"
39
40 using namespace OpenZWave;
41
42 enum AssociationCommandConfigurationCmd
43 {
44 AssociationCommandConfigurationCmd_SupportedRecordsGet = 0x01,
45 AssociationCommandConfigurationCmd_SupportedRecordsReport = 0x02,
46 AssociationCommandConfigurationCmd_Set = 0x03,
47 AssociationCommandConfigurationCmd_Get = 0x04,
48 AssociationCommandConfigurationCmd_Report = 0x05
49 };
50
51 enum
52 {
53 AssociationCommandConfigurationIndex_MaxCommandLength = 0,
54 AssociationCommandConfigurationIndex_CommandsAreValues,
55 AssociationCommandConfigurationIndex_CommandsAreConfigurable,
56 AssociationCommandConfigurationIndex_NumFreeCommands,
57 AssociationCommandConfigurationIndex_MaxCommands
58 };
59
60
61 //-----------------------------------------------------------------------------
62 // <AssociationCommandConfiguration::RequestState>
63 // Request current state from the device
64 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)65 bool AssociationCommandConfiguration::RequestState
66 (
67 uint32 const _requestFlags,
68 uint8 const _instance,
69 Driver::MsgQueue const _queue
70 )
71 {
72 if( _requestFlags & RequestFlag_Session )
73 {
74 return RequestValue( _requestFlags, 0, _instance, _queue );
75 }
76
77 return false;
78 }
79
80 //-----------------------------------------------------------------------------
81 // <AssociationCommandConfiguration::RequestValue>
82 // Request current value from the device
83 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)84 bool AssociationCommandConfiguration::RequestValue
85 (
86 uint32 const _requestFlags,
87 uint8 const _dummy1, // = 0 (not used)
88 uint8 const _instance,
89 Driver::MsgQueue const _queue
90 )
91 {
92 if( _instance != 1 )
93 {
94 // This command class doesn't work with multiple instances
95 return false;
96 }
97
98 Msg* msg = new Msg( "AssociationCommandConfigurationCmd_SupportedRecordsGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
99 msg->Append( GetNodeId() );
100 msg->Append( 2 );
101 msg->Append( GetCommandClassId() );
102 msg->Append( AssociationCommandConfigurationCmd_SupportedRecordsGet );
103 msg->Append( GetDriver()->GetTransmitOptions() );
104 GetDriver()->SendMsg( msg, _queue );
105 return true;
106 }
107
108 //-----------------------------------------------------------------------------
109 // <AssociationCommandConfiguration::RequestCommands>
110 // Request the command data
111 //-----------------------------------------------------------------------------
RequestCommands(uint8 const _groupIdx,uint8 const _nodeId)112 void AssociationCommandConfiguration::RequestCommands
113 (
114 uint8 const _groupIdx,
115 uint8 const _nodeId
116 )
117 {
118 if ( IsGetSupported() )
119 {
120 Msg* msg = new Msg( "AssociationCommandConfigurationCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
121 msg->Append( GetNodeId() );
122 msg->Append( 4 );
123 msg->Append( GetCommandClassId() );
124 msg->Append( AssociationCommandConfigurationCmd_Get );
125 msg->Append( _groupIdx );
126 msg->Append( _nodeId );
127 msg->Append( GetDriver()->GetTransmitOptions() );
128 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
129 } else {
130 Log::Write( LogLevel_Info, GetNodeId(), "AssociationCommandConfigurationCmd_Get Not Supported on this node");
131 }
132 }
133 //-----------------------------------------------------------------------------
134 // <AssociationCommandConfiguration::HandleMsg>
135 // Handle a message from the Z-Wave network
136 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)137 bool AssociationCommandConfiguration::HandleMsg
138 (
139 uint8 const* _data,
140 uint32 const _length,
141 uint32 const _instance // = 1
142 )
143 {
144 if (AssociationCommandConfigurationCmd_SupportedRecordsReport == (AssociationCommandConfigurationCmd)_data[0])
145 {
146 uint8 maxCommandLength = _data[1] >> 2;
147 bool commandsAreValues = ( (_data[1] & 0x02) != 0 );
148 bool commandsAreConfigurable = ( (_data[1] & 0x01) != 0 );
149 int16 numFreeCommands = (((int16)_data[2])<<16) | (int16)_data[3];
150 int16 maxCommands = (((int16)_data[4])<<16) | (int16)_data[5];
151
152 Log::Write( LogLevel_Info, GetNodeId(), "Received AssociationCommandConfiguration Supported Records Report:" );
153 Log::Write( LogLevel_Info, GetNodeId(), " Maximum command length = %d bytes", maxCommandLength );
154 Log::Write( LogLevel_Info, GetNodeId(), " Maximum number of commands = %d", maxCommands );
155 Log::Write( LogLevel_Info, GetNodeId(), " Number of free commands = %d", numFreeCommands );
156 Log::Write( LogLevel_Info, GetNodeId(), " Commands are %s and are %s", commandsAreValues ? "values" : "not values", commandsAreConfigurable ? "configurable" : "not configurable" );
157
158 ValueBool* valueBool;
159 ValueByte* valueByte;
160 ValueShort* valueShort;
161
162 if( (valueByte = static_cast<ValueByte*>( GetValue( _instance, AssociationCommandConfigurationIndex_MaxCommandLength ) )) )
163 {
164 valueByte->OnValueRefreshed( maxCommandLength );
165 valueByte->Release();
166 }
167
168 if( (valueBool = static_cast<ValueBool*>( GetValue( _instance, AssociationCommandConfigurationIndex_CommandsAreValues ) )) )
169 {
170 valueBool->OnValueRefreshed( commandsAreValues );
171 valueBool->Release();
172 }
173
174 if( (valueBool = static_cast<ValueBool*>( GetValue( _instance, AssociationCommandConfigurationIndex_CommandsAreConfigurable ) )) )
175 {
176 valueBool->OnValueRefreshed( commandsAreConfigurable );
177 valueBool->Release();
178 }
179
180 if( (valueShort = static_cast<ValueShort*>( GetValue( _instance, AssociationCommandConfigurationIndex_NumFreeCommands ) )) )
181 {
182 valueShort->OnValueRefreshed( numFreeCommands );
183 valueShort->Release();
184 }
185
186 if( (valueShort = static_cast<ValueShort*>( GetValue( _instance, AssociationCommandConfigurationIndex_MaxCommands ) )) )
187 {
188 valueShort->OnValueRefreshed( maxCommands );
189 valueShort->Release();
190 }
191 return true;
192 }
193
194 if (AssociationCommandConfigurationCmd_Report == (AssociationCommandConfigurationCmd)_data[0])
195 {
196 uint8 groupIdx = _data[1];
197 uint8 nodeIdx = _data[2];
198 bool firstReports = ( ( _data[3] & 0x80 ) != 0 ); // True if this is the first message containing commands for this group and node.
199 uint8 numReports = _data[3] & 0x0f;
200
201 Log::Write( LogLevel_Info, GetNodeId(), "Received AssociationCommandConfiguration Report from:" );
202 Log::Write( LogLevel_Info, GetNodeId(), " Commands for node %d in group %d,", nodeIdx, groupIdx );
203
204 if( Node* node = GetNodeUnsafe() )
205 {
206 Group* group = node->GetGroup( groupIdx );
207 if( NULL == group )
208 {
209 if( firstReports )
210 {
211 // This is the first report message, so we should clear any existing command data
212 group->ClearCommands( nodeIdx );
213 }
214
215 uint8 const* start = &_data[4];
216
217 for( uint8 i=0; i<numReports; ++i )
218 {
219 uint8 length = start[0];
220 group->AddCommand( nodeIdx, length, start+1 );
221 start += length;
222 }
223 }
224 }
225
226 return true;
227 }
228 return false;
229 }
230
231 //-----------------------------------------------------------------------------
232 // <AssociationCommandConfiguration::CreateVars>
233 // Create the values managed by this command class
234 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)235 void AssociationCommandConfiguration::CreateVars
236 (
237 uint8 const _instance
238 )
239 {
240 if( Node* node = GetNodeUnsafe() )
241 {
242 node->CreateValueByte ( ValueID::ValueGenre_System, GetCommandClassId(), _instance, AssociationCommandConfigurationIndex_MaxCommandLength, "Max Command Length", "", true, false, 0, 0 );
243 node->CreateValueBool ( ValueID::ValueGenre_System, GetCommandClassId(), _instance, AssociationCommandConfigurationIndex_CommandsAreValues, "Commands are Values", "", true, false, false, 0 );
244 node->CreateValueBool ( ValueID::ValueGenre_System, GetCommandClassId(), _instance, AssociationCommandConfigurationIndex_CommandsAreConfigurable, "Commands are Configurable", "", true, false, false, 0 );
245 node->CreateValueShort( ValueID::ValueGenre_System, GetCommandClassId(), _instance, AssociationCommandConfigurationIndex_NumFreeCommands, "Free Commands", "", true, false, 0, 0 );
246 node->CreateValueShort( ValueID::ValueGenre_System, GetCommandClassId(), _instance, AssociationCommandConfigurationIndex_MaxCommands, "Max Commands", "", true, false, 0, 0 );
247 }
248 }
249
250 //-----------------------------------------------------------------------------
251 // <AssociationCommandConfiguration::SetCommand>
252 // Set a command for the association
253 //-----------------------------------------------------------------------------
SetCommand(uint8 const _groupIdx,uint8 const _nodeId,uint8 const _length,uint8 const * _data)254 void AssociationCommandConfiguration::SetCommand
255 (
256 uint8 const _groupIdx,
257 uint8 const _nodeId,
258 uint8 const _length,
259 uint8 const* _data
260 )
261 {
262 Msg* msg = new Msg( "AssociationCommandConfigurationCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
263 msg->Append( GetNodeId() );
264 msg->Append( _length + 5 );
265 msg->Append( GetCommandClassId() );
266 msg->Append( AssociationCommandConfigurationCmd_Set );
267 msg->Append( _groupIdx );
268 msg->Append( _nodeId );
269 msg->Append( _length );
270
271 for( uint8 i=0; i<_length; ++i )
272 {
273 msg->Append( _data[i] );
274 }
275
276 msg->Append( GetDriver()->GetTransmitOptions() );
277 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
278 }
279
280
281
282