1 //-----------------------------------------------------------------------------
2 //
3 //	SensorBinary.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_SENSOR_BINARY
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/SensorBinary.h"
30 #include "command_classes/WakeUp.h"
31 #include "Defs.h"
32 #include "Msg.h"
33 #include "Node.h"
34 #include "Driver.h"
35 #include "platform/Log.h"
36 #include "value_classes/ValueBool.h"
37 #include "tinyxml.h"
38 
39 using namespace OpenZWave;
40 
41 enum SensorBinaryCmd
42 {
43 	SensorBinaryCmd_Get		= 0x02,
44 	SensorBinaryCmd_Report		= 0x03
45 };
46 
47 //-----------------------------------------------------------------------------
48 // <SensorBinary::ReadXML>
49 // Read node configuration data
50 //-----------------------------------------------------------------------------
ReadXML(TiXmlElement const * _ccElement)51 void SensorBinary::ReadXML
52 (
53 	TiXmlElement const* _ccElement
54 )
55 {
56 	CommandClass::ReadXML( _ccElement );
57 
58 	TiXmlElement const* child = _ccElement->FirstChildElement();
59 
60 	char const* str; int index; int type;
61 
62 	while( child )
63 	{
64 		str = child->Value();
65 
66 		if( str )
67 		{
68 			if( !strcmp( str, "SensorMap" ) )
69 			{
70 				if( TIXML_SUCCESS == child->QueryIntAttribute( "index", &index ) &&
71 					TIXML_SUCCESS == child->QueryIntAttribute( "type", &type ) )
72 				{
73 					m_sensorsMap[(uint8)type] = (uint8)index;
74 				}
75 			}
76 		}
77 
78 		child = child->NextSiblingElement();
79 	}
80 }
81 //-----------------------------------------------------------------------------
82 // <SensorBinary::WriteXML>
83 // Write node configuration data
84 //-----------------------------------------------------------------------------
WriteXML(TiXmlElement * _ccElement)85 void SensorBinary::WriteXML
86 (
87 	TiXmlElement* _ccElement
88 )
89 {
90 	CommandClass::WriteXML( _ccElement );
91 
92 	char str[8];
93 
94 	for( map<uint8,uint8>::iterator it = m_sensorsMap.begin(); it != m_sensorsMap.end(); it++ )
95 	{
96 		TiXmlElement* sensorMapElement = new TiXmlElement( "SensorMap" );
97 		_ccElement->LinkEndChild( sensorMapElement );
98 
99 		snprintf( str, 8, "%d", it->second );
100 		sensorMapElement->SetAttribute( "index", str );
101 
102 		snprintf( str, 8, "%d", it->first );
103 		sensorMapElement->SetAttribute( "type", str );
104 	}
105 }
106 //-----------------------------------------------------------------------------
107 // <SensorBinary::RequestState>
108 // Request current state from the device
109 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)110 bool SensorBinary::RequestState
111 (
112 	uint32 const _requestFlags,
113 	uint8 const _instance,
114 	Driver::MsgQueue const _queue
115 )
116 {
117 	if( _requestFlags & RequestFlag_Dynamic )
118 	{
119 		return RequestValue( _requestFlags, 0, _instance, _queue );
120 	}
121 
122 	return false;
123 }
124 
125 //-----------------------------------------------------------------------------
126 // <SensorBinary::RequestValue>
127 // Request current value from the device
128 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)129 bool SensorBinary::RequestValue
130 (
131 	uint32 const _requestFlags,
132 	uint8 const _dummy1,	// = 0 (not used)
133 	uint8 const _instance,
134 	Driver::MsgQueue const _queue
135 )
136 {
137 	if ( IsGetSupported() )
138 	{
139 		Msg* msg = new Msg( "SensorBinaryCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
140 		msg->SetInstance( this, _instance );
141 		msg->Append( GetNodeId() );
142 		msg->Append( 2 );
143 		msg->Append( GetCommandClassId() );
144 		msg->Append( SensorBinaryCmd_Get );
145 		msg->Append( GetDriver()->GetTransmitOptions() );
146 		GetDriver()->SendMsg( msg, _queue );
147 		return true;
148 	} else {
149 		Log::Write(  LogLevel_Info, GetNodeId(), "SensorBinaryCmd_Get Not Supported on this node");
150 	}
151 	return false;
152 }
153 
154 //-----------------------------------------------------------------------------
155 // <SensorBinary::HandleMsg>
156 // Handle a message from the Z-Wave network
157 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)158 bool SensorBinary::HandleMsg
159 (
160 	uint8 const* _data,
161 	uint32 const _length,
162 	uint32 const _instance	// = 1
163 )
164 {
165 	if (SensorBinaryCmd_Report == (SensorBinaryCmd)_data[0])
166 	{
167 	    if( _length > 2 )
168 	    {
169 	        uint8 index = m_sensorsMap[_data[2]];
170 
171             Log::Write( LogLevel_Info, GetNodeId(), "Received SensorBinary report: Sensor:%d State=%s", _data[2], _data[1] ? "On" : "Off" );
172 
173             if( ValueBool* value = static_cast<ValueBool*>( GetValue( _instance, index ) ) )
174             {
175                 value->OnValueRefreshed( _data[1] != 0 );
176                 value->Release();
177             }
178 
179             return true;
180 	    }
181 	    else
182 	    {
183             Log::Write( LogLevel_Info, GetNodeId(), "Received SensorBinary report: State=%s", _data[1] ? "On" : "Off" );
184 
185             if( ValueBool* value = static_cast<ValueBool*>( GetValue( _instance, 0 ) ) )
186             {
187                 value->OnValueRefreshed( _data[1] != 0 );
188                 value->Release();
189             }
190 
191             return true;
192 	    }
193 	}
194 
195 	return false;
196 }
197 
198 //-----------------------------------------------------------------------------
199 // <SensorBinary::SetValueBasic>
200 // Update class values based in BASIC mapping
201 //-----------------------------------------------------------------------------
SetValueBasic(uint8 const _instance,uint8 const _value)202 void SensorBinary::SetValueBasic
203 (
204 	uint8 const _instance,
205 	uint8 const _value
206 )
207 {
208 	// Send a request for new value to synchronize it with the BASIC set/report.
209 	// In case the device is sleeping, we set the value anyway so the BASIC set/report
210 	// stays in sync with it. We must be careful mapping the uint8 BASIC value
211 	// into a class specific value.
212 	// When the device wakes up, the real requested value will be retrieved.
213 	RequestValue( 0, 0, _instance, Driver::MsgQueue_Send );
214 	if( Node* node = GetNodeUnsafe() )
215 	{
216 		if( WakeUp* wakeUp = static_cast<WakeUp*>( node->GetCommandClass( WakeUp::StaticGetCommandClassId() ) ) )
217 		{
218 			if( !wakeUp->IsAwake() )
219 			{
220 				if( ValueBool* value = static_cast<ValueBool*>( GetValue( _instance, 0 ) ) )
221 				{
222 					value->OnValueRefreshed( _value != 0 );
223 					value->Release();
224 				}
225 			}
226 		}
227 	}
228 }
229 
230 //-----------------------------------------------------------------------------
231 // <SensorBinary::CreateVars>
232 // Create the values managed by this command class
233 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)234 void SensorBinary::CreateVars
235 (
236 	uint8 const _instance
237 )
238 {
239 	if( Node* node = GetNodeUnsafe() )
240 	{
241 	  	node->CreateValueBool(  ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Sensor", "", true, false, false, 0 );
242 	}
243 }
244