1 //-----------------------------------------------------------------------------
2 //
3 //	ZWavePlusInfo.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_ZWAVEPLUS_INFO
6 //
7 //	Copyright (c) 2015
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/ZWavePlusInfo.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 #include "value_classes/ValueShort.h"
38 #include "value_classes/ValueList.h"
39 
40 using namespace OpenZWave;
41 
42 enum ZWavePlusInfoCmdEnum
43 {
44 	ZWavePlusInfoCmd_Get    = 0x01,
45 	ZWavePlusInfoCmd_Report
46 };
47 
48 enum
49 {
50 	ZWavePlusInfoIndex_Version = 0x00,
51 	ZWavePlusInfoIndex_InstallerIcon,
52 	ZWavePlusInfoIndex_UserIcon,
53 };
54 
55 
56 
57 //-----------------------------------------------------------------------------
58 // <ZWavePlusInfo::ZWavePlusInfo>
59 // Constructor
60 //-----------------------------------------------------------------------------
ZWavePlusInfo(uint32 const _homeId,uint8 const _nodeId)61 ZWavePlusInfo::ZWavePlusInfo
62 (
63     uint32 const _homeId,
64     uint8 const _nodeId
65 ):
66     CommandClass( _homeId, _nodeId )
67 {
68     SetStaticRequest( StaticRequest_Values );
69 }
70 
71 //-----------------------------------------------------------------------------
72 // <ZWavePlusInfo::RequestState>
73 // Request current state from the device
74 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)75 bool ZWavePlusInfo::RequestState
76 (
77 	uint32 const _requestFlags,
78 	uint8 const _instance,
79 	Driver::MsgQueue const _queue
80 )
81 {
82 	if( (_requestFlags & RequestFlag_Static) && HasStaticRequest( StaticRequest_Values ) )
83 	{
84 		return RequestValue( _requestFlags, 0, _instance, _queue );
85 	}
86 
87 	return false;
88 }
89 
90 //-----------------------------------------------------------------------------
91 // <ZWavePlusInfo::RequestValue>
92 // Request current value from the device
93 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)94 bool ZWavePlusInfo::RequestValue
95 (
96 	uint32 const _requestFlags,
97 	uint8 const _dummy1,	// = 0 (not used)
98 	uint8 const _instance,
99 	Driver::MsgQueue const _queue
100 )
101 {
102 	if( _instance != 1 )
103 	{
104 		// This command class doesn't work with multiple instances
105 		return false;
106 	}
107 	if ( IsGetSupported() )
108 	{
109 		Msg* msg = new Msg( "ZWavePlusInfoCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
110 		msg->SetInstance( this, _instance );
111 		msg->Append( GetNodeId() );
112 		msg->Append( 2 );
113 		msg->Append( GetCommandClassId() );
114 		msg->Append( ZWavePlusInfoCmd_Get );
115 		msg->Append( GetDriver()->GetTransmitOptions() );
116 		GetDriver()->SendMsg( msg, _queue );
117 		return true;
118 	} else {
119 		Log::Write(  LogLevel_Info, GetNodeId(), "ZWavePlusInfoCmd_Get Not Supported on this node");
120 	}
121 	return false;
122 }
123 
124 
125 //-----------------------------------------------------------------------------
126 // <ZWavePlusInfo::HandleMsg>
127 // Handle a message from the Z-Wave network
128 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)129 bool ZWavePlusInfo::HandleMsg
130 (
131 	uint8 const* _data,
132 	uint32 const _length,
133 	uint32 const _instance	// = 1
134 )
135 {
136 	if( ZWavePlusInfoCmd_Report == _data[0] )
137 	{
138 		uint8 version = _data[1];
139 		uint8 role    = _data[2];
140 		uint8 nodeType    = _data[3];
141 		uint16 installerIcon = (_data[4]<< 8) | _data[5];
142 		uint16 deviceType		 = (_data[6]<< 8) | _data[7];
143 
144 		if( Node* node = GetNodeUnsafe() )
145 		{
146 			node->SetPlusDeviceClasses(	role, nodeType, deviceType );
147 		}
148 		ClearStaticRequest( StaticRequest_Values );
149 
150 		ValueByte* value;
151 		if( (value = static_cast<ValueByte*>( GetValue( _instance, ZWavePlusInfoIndex_Version ) )) )
152 		{
153 			value->OnValueRefreshed( version );
154 			value->Release();
155 		}
156 
157 		ValueShort* svalue;
158 		if( (svalue = static_cast<ValueShort*>( GetValue( _instance, ZWavePlusInfoIndex_InstallerIcon ) )) )
159 		{
160 			svalue->OnValueRefreshed( installerIcon );
161 			svalue->Release();
162 		}
163 
164 		if( (svalue = static_cast<ValueShort*>( GetValue( _instance, ZWavePlusInfoIndex_UserIcon ) )) )
165 		{
166 			svalue->OnValueRefreshed( deviceType );
167 			svalue->Release();
168 		}
169 
170 
171 
172 
173 		return true;
174 	}
175 	return false;
176 }
177 
178 //-----------------------------------------------------------------------------
179 // <ZWavePlusInfo::CreateVars>
180 // Create the values managed by this command class
181 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)182 void ZWavePlusInfo::CreateVars
183 (
184 		uint8 const _instance
185 )
186 {
187 	if( Node* node = GetNodeUnsafe() )
188 	{
189 		node->CreateValueByte( ValueID::ValueGenre_System, GetCommandClassId(), _instance, ZWavePlusInfoIndex_Version, "ZWave+ Version", "", true, false, 0, 0 );
190 		node->CreateValueShort( ValueID::ValueGenre_System, GetCommandClassId(), _instance, ZWavePlusInfoIndex_InstallerIcon, "InstallerIcon", "", true, false, 0, 0 );
191 		node->CreateValueShort( ValueID::ValueGenre_System, GetCommandClassId(), _instance, ZWavePlusInfoIndex_UserIcon, "UserIcon", "", true, false, 0, 0 );
192 
193 	}
194 }
195 
196 
197