1 //-----------------------------------------------------------------------------
2 //
3 //	Battery.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_BATTERY
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/Battery.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 BatteryCmd
41 {
42 	BatteryCmd_Get		= 0x02,
43 	BatteryCmd_Report	= 0x03
44 };
45 
46 //-----------------------------------------------------------------------------
47 // <Battery::RequestState>
48 // Request current state from the device
49 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)50 bool Battery::RequestState
51 (
52 	uint32 const _requestFlags,
53 	uint8 const _instance,
54 	Driver::MsgQueue const _queue
55 )
56 {
57 	if( _requestFlags & RequestFlag_Dynamic )
58 	{
59 		return RequestValue( _requestFlags, 0, _instance, _queue );
60 	}
61 
62 	return false;
63 }
64 
65 //-----------------------------------------------------------------------------
66 // <Battery::RequestValue>
67 // Request current value from the device
68 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)69 bool Battery::RequestValue
70 (
71 	uint32 const _requestFlags,
72 	uint8 const _dummy1,	// = 0 (not used)
73 	uint8 const _instance,
74 	Driver::MsgQueue const _queue
75 )
76 {
77 	if( _instance != 1 )
78 	{
79 		// This command class doesn't work with multiple instances
80 		return false;
81 	}
82 	if ( IsGetSupported() )
83 	{
84 		Msg* msg = new Msg( "BatteryCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
85 		msg->Append( GetNodeId() );
86 		msg->Append( 2 );
87 		msg->Append( GetCommandClassId() );
88 		msg->Append( BatteryCmd_Get );
89 		msg->Append( GetDriver()->GetTransmitOptions() );
90 		GetDriver()->SendMsg( msg, _queue );
91 		return true;
92 	} else {
93 		Log::Write(  LogLevel_Info, GetNodeId(), "BatteryCmd_Get Not Supported on this node");
94 	}
95 	return false;
96 }
97 
98 //-----------------------------------------------------------------------------
99 // <Battery::HandleMsg>
100 // Handle a message from the Z-Wave network
101 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)102 bool Battery::HandleMsg
103 (
104 	uint8 const* _data,
105 	uint32 const _length,
106 	uint32 const _instance	// = 1
107 )
108 {
109 	if (BatteryCmd_Report == (BatteryCmd)_data[0])
110 	{
111 		// We have received a battery level report from the Z-Wave device.
112 		// Devices send 0xff instead of zero for a low battery warning.
113 		uint8 batteryLevel = _data[1];
114 		if( batteryLevel == 0xff )
115 		{
116 			batteryLevel = 0;
117 		}
118 
119 		Log::Write( LogLevel_Info, GetNodeId(), "Received Battery report from node %d: level=%d", GetNodeId(), batteryLevel );
120 
121 		if( ValueByte* value = static_cast<ValueByte*>( GetValue( _instance, 0 ) ) )
122 		{
123 			value->OnValueRefreshed( batteryLevel );
124 			value->Release();
125 		}
126 		return true;
127 	}
128 	return false;
129 }
130 
131 //-----------------------------------------------------------------------------
132 // <Battery::CreateVars>
133 // Create the values managed by this command class
134 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)135 void Battery::CreateVars
136 (
137 	uint8 const _instance
138 )
139 {
140 	if( Node* node = GetNodeUnsafe() )
141 	{
142 	  	node->CreateValueByte( ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Battery Level", "%", true, false, 100, 0 );
143 	}
144 }
145 
146 
147