1 //-----------------------------------------------------------------------------
2 //
3 //	SwitchToggleMultilevel.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_SWITCH_TOGGLE_MULTILEVEL
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/SwitchToggleMultilevel.h"
30 #include "Defs.h"
31 #include "Msg.h"
32 #include "Driver.h"
33 #include "Node.h"
34 #include "platform/Log.h"
35 
36 #include "value_classes/ValueByte.h"
37 
38 using namespace OpenZWave;
39 
40 enum SwitchToggleMultilevelCmd
41 {
42 	SwitchToggleMultilevelCmd_Set				= 0x01,
43 	SwitchToggleMultilevelCmd_Get				= 0x02,
44 	SwitchToggleMultilevelCmd_Report			= 0x03,
45 	SwitchToggleMultilevelCmd_StartLevelChange	= 0x04,
46 	SwitchToggleMultilevelCmd_StopLevelChange	= 0x05
47 };
48 
49 
50 //-----------------------------------------------------------------------------
51 // <SwitchToggleMultilevel::RequestState>
52 // Request current state from the device
53 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)54 bool SwitchToggleMultilevel::RequestState
55 (
56 	uint32 const _requestFlags,
57 	uint8 const _instance,
58 	Driver::MsgQueue const _queue
59 )
60 {
61 	if( _requestFlags & RequestFlag_Dynamic )
62 	{
63 		return RequestValue( _requestFlags, 0, _instance, _queue );
64 	}
65 
66 	return false;
67 }
68 
69 //-----------------------------------------------------------------------------
70 // <SwitchToggleMultilevel::RequestValue>
71 // Request current value from the device
72 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)73 bool SwitchToggleMultilevel::RequestValue
74 (
75 	uint32 const _requestFlags,
76 	uint8 const _dummy1,	// = 0 (not used)
77 	uint8 const _instance,
78 	Driver::MsgQueue const _queue
79 )
80 {
81 	if ( IsGetSupported() )
82 	{
83 		Msg* msg = new Msg( "SwitchToggleMultilevelCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
84 		msg->SetInstance( this, _instance );
85 		msg->Append( GetNodeId() );
86 		msg->Append( 2 );
87 		msg->Append( GetCommandClassId() );
88 		msg->Append( SwitchToggleMultilevelCmd_Get );
89 		msg->Append( GetDriver()->GetTransmitOptions() );
90 		GetDriver()->SendMsg( msg, _queue );
91 		return true;
92 	} else {
93 		Log::Write(  LogLevel_Info, GetNodeId(), "SwitchToggleMultilevelCmd_Get Not Supported on this node");
94 	}
95 	return false;
96 }
97 
98 //-----------------------------------------------------------------------------
99 // <SwitchToggleMultilevel::HandleMsg>
100 // Handle a message from the Z-Wave network
101 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)102 bool SwitchToggleMultilevel::HandleMsg
103 (
104 	uint8 const* _data,
105 	uint32 const _length,
106 	uint32 const _instance	// = 1
107 )
108 {
109 	if( SwitchToggleMultilevelCmd_Report == (SwitchToggleMultilevelCmd)_data[0] )
110 	{
111 		Log::Write( LogLevel_Info, GetNodeId(), "Received SwitchToggleMultiLevel report: level=%d", _data[1] );
112 
113 		if( ValueByte* value = static_cast<ValueByte*>( GetValue( _instance, 0 ) ) )
114 		{
115 			value->OnValueRefreshed( _data[1] );
116 			value->Release();
117 		}
118 		return true;
119 	}
120 
121 	return false;
122 }
123 
124 //-----------------------------------------------------------------------------
125 // <SwitchToggleMultilevel::SetValue>
126 // Toggle the state of the switch
127 //-----------------------------------------------------------------------------
SetValue(Value const & _value)128 bool SwitchToggleMultilevel::SetValue
129 (
130 	Value const& _value
131 )
132 {
133 	Log::Write( LogLevel_Info, GetNodeId(), "SwitchToggleMultilevel::Set - Toggling the state" );
134 	Msg* msg = new Msg( "SwitchToggleMultilevelCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
135 	msg->SetInstance( this, _value.GetID().GetInstance() );
136 	msg->Append( GetNodeId() );
137 	msg->Append( 2 );
138 	msg->Append( GetCommandClassId() );
139 	msg->Append( SwitchToggleMultilevelCmd_Set );
140 	msg->Append( GetDriver()->GetTransmitOptions() );
141 	GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
142 	return true;
143 }
144 
145 //-----------------------------------------------------------------------------
146 // <SwitchToggleMultilevel::StartLevelChange>
147 // Start the level changing
148 //-----------------------------------------------------------------------------
StartLevelChange(SwitchToggleMultilevelDirection const _direction,bool const _bIgnoreStartLevel,bool const _bRollover)149 void SwitchToggleMultilevel::StartLevelChange
150 (
151 	SwitchToggleMultilevelDirection const _direction,
152 	bool const _bIgnoreStartLevel,
153 	bool const _bRollover
154 )
155 {
156 	uint8 param = (uint8)_direction;
157 	param |= ( _bIgnoreStartLevel ? 0x20 : 0x00 );
158 	param |= ( _bRollover ? 0x80 : 0x00 );
159 
160 	Log::Write( LogLevel_Info, GetNodeId(), "SwitchMultilevel::StartLevelChange - Starting a level change, Direction=%d, IgnoreStartLevel=%s and rollover=%s", (_direction==SwitchToggleMultilevelDirection_Up) ? "Up" : "Down", _bIgnoreStartLevel ? "True" : "False", _bRollover ? "True" : "False" );
161 	Msg* msg = new Msg( "SwitchToggleMultilevelCmd_StartLevelChange", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
162 	msg->Append( GetNodeId() );
163 	msg->Append( 3 );
164 	msg->Append( GetCommandClassId() );
165 	msg->Append( SwitchToggleMultilevelCmd_StartLevelChange );
166 	msg->Append( param );
167 	msg->Append( GetDriver()->GetTransmitOptions() );
168 }
169 
170 //-----------------------------------------------------------------------------
171 // <SwitchToggleMultilevel::StopLevelChange>
172 // Stop the level changing
173 //-----------------------------------------------------------------------------
StopLevelChange()174 void SwitchToggleMultilevel::StopLevelChange
175 (
176 )
177 {
178 	Log::Write( LogLevel_Info, GetNodeId(), "SwitchToggleMultilevel::StopLevelChange - Stopping the level change" );
179 	Msg* msg = new Msg( "SwitchToggleMultilevelCmd_StopLevelChange", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
180 	msg->Append( GetNodeId() );
181 	msg->Append( 2 );
182 	msg->Append( GetCommandClassId() );
183 	msg->Append( SwitchToggleMultilevelCmd_StopLevelChange );
184 	msg->Append( GetDriver()->GetTransmitOptions() );
185 }
186 
187 //-----------------------------------------------------------------------------
188 // <SwitchToggleMultilevel::CreateVars>
189 // Create the values managed by this command class
190 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)191 void SwitchToggleMultilevel::CreateVars
192 (
193 	uint8 const _instance
194 )
195 {
196 	if( Node* node = GetNodeUnsafe() )
197 	{
198 	  	node->CreateValueByte( ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Level", "", false, false, 0, 0 );
199 	}
200 }
201 
202