1 //-----------------------------------------------------------------------------
2 //
3 //	Indicator.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_INDICATOR
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/Indicator.h"
30 #include "Defs.h"
31 #include "Msg.h"
32 #include "Node.h"
33 #include "Driver.h"
34 #include "platform/Log.h"
35 
36 #include "value_classes/ValueByte.h"
37 
38 using namespace OpenZWave;
39 
40 enum IndicatorCmd
41 {
42 	IndicatorCmd_Set	= 0x01,
43 	IndicatorCmd_Get	= 0x02,
44 	IndicatorCmd_Report	= 0x03
45 };
46 
47 //-----------------------------------------------------------------------------
48 // <Indicator::RequestState>
49 // Request current state from the device
50 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)51 bool Indicator::RequestState
52 (
53 	uint32 const _requestFlags,
54 	uint8 const _instance,
55 	Driver::MsgQueue const _queue
56 )
57 {
58 	if( _requestFlags & RequestFlag_Dynamic )
59 	{
60 		return RequestValue( _requestFlags, 0, _instance, _queue );
61 	}
62 
63 	return false;
64 }
65 
66 //-----------------------------------------------------------------------------
67 // <Indicator::RequestValue>
68 // Request current value from the device
69 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)70 bool Indicator::RequestValue
71 (
72 	uint32 const _requestFlags,
73 	uint8 const _dummy1,	// = 0 (not used)
74 	uint8 const _instance,
75 	Driver::MsgQueue const _queue
76 )
77 {
78 	if ( IsGetSupported() )
79 	{
80 		Msg* msg = new Msg( "IndicatorCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
81 		msg->SetInstance( this, _instance );
82 		msg->Append( GetNodeId() );
83 		msg->Append( 2 );
84 		msg->Append( GetCommandClassId() );
85 		msg->Append( IndicatorCmd_Get );
86 		msg->Append( GetDriver()->GetTransmitOptions() );
87 		GetDriver()->SendMsg( msg, _queue );
88 		return true;
89 	} else {
90 		Log::Write(  LogLevel_Info, GetNodeId(), "IndicatorCmd_Get Not Supported on this node");
91 	}
92 	return false;
93 }
94 
95 
96 //-----------------------------------------------------------------------------
97 // <Indicator::HandleMsg>
98 // Handle a message from the Z-Wave network
99 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)100 bool Indicator::HandleMsg
101 (
102 	uint8 const* _data,
103 	uint32 const _length,
104 	uint32 const _instance	// = 1
105 )
106 {
107 	if( IndicatorCmd_Report == (IndicatorCmd)_data[0] )
108 	{
109 		Log::Write( LogLevel_Info, GetNodeId(), "Received an Indicator report: Indicator=%d", _data[1] );
110 
111 		if( ValueByte* value = static_cast<ValueByte*>( GetValue( _instance, 0 ) ) )
112 		{
113 			value->OnValueRefreshed( _data[1] );
114 			value->Release();
115 		}
116 		return true;
117 	}
118 
119 	return false;
120 }
121 
122 //-----------------------------------------------------------------------------
123 // <Indicator::SetValue>
124 // Set the device's indicator value
125 //-----------------------------------------------------------------------------
SetValue(Value const & _value)126 bool Indicator::SetValue
127 (
128 	Value const& _value
129 )
130 {
131 	if( ValueID::ValueType_Byte == _value.GetID().GetType() )
132 	{
133 		ValueByte const* value = static_cast<ValueByte const*>(&_value);
134 
135 		Log::Write( LogLevel_Info, GetNodeId(), "Indicator::SetValue - Setting indicator to %d", value->GetValue());
136 		Msg* msg = new Msg( "IndicatorCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
137 		msg->SetInstance( this, _value.GetID().GetInstance() );
138 		msg->Append( GetNodeId() );
139 		msg->Append( 3 );
140 		msg->Append( GetCommandClassId() );
141 		msg->Append( IndicatorCmd_Set );
142 		msg->Append( value->GetValue() );
143 		msg->Append( GetDriver()->GetTransmitOptions() );
144 		GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
145 		return true;
146 	}
147 
148 	return false;
149 }
150 
151 //-----------------------------------------------------------------------------
152 // <Indicator::CreateVars>
153 // Create the values managed by this command class
154 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)155 void Indicator::CreateVars
156 (
157 	uint8 const _instance
158 )
159 {
160 	if( Node* node = GetNodeUnsafe() )
161 	{
162 	  	node->CreateValueByte( ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Indicator", "", false, false, false, 0 );
163 	}
164 }
165