1 //-----------------------------------------------------------------------------
2 //
3 // Protection.cpp
4 //
5 // Implementation of the Z-Wave COMMAND_CLASS_PROTECTION
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 <vector>
29
30 #include "command_classes/CommandClasses.h"
31 #include "command_classes/Protection.h"
32 #include "Defs.h"
33 #include "Msg.h"
34 #include "Node.h"
35 #include "Driver.h"
36 #include "platform/Log.h"
37
38 #include "value_classes/ValueList.h"
39
40 using namespace OpenZWave;
41
42 enum ProtectionCmd
43 {
44 ProtectionCmd_Set = 0x01,
45 ProtectionCmd_Get = 0x02,
46 ProtectionCmd_Report = 0x03
47 };
48
49 static char const* c_protectionStateNames[] =
50 {
51 "Unprotected",
52 "Protection by Sequence",
53 "No Operation Possible",
54 "Unknown"
55 };
56
57 //-----------------------------------------------------------------------------
58 // <Protection::RequestState>
59 // Request current state from the device
60 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)61 bool Protection::RequestState
62 (
63 uint32 const _requestFlags,
64 uint8 const _instance,
65 Driver::MsgQueue const _queue
66 )
67 {
68 if( _requestFlags & RequestFlag_Session )
69 {
70 return RequestValue( _requestFlags, 0, _instance, _queue );
71 }
72
73 return false;
74 }
75
76 //-----------------------------------------------------------------------------
77 // <Protection::RequestValue>
78 // Request current value from the device
79 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)80 bool Protection::RequestValue
81 (
82 uint32 const _requestFlags,
83 uint8 const _dummy1, // = 0 (not used)
84 uint8 const _instance,
85 Driver::MsgQueue const _queue
86 )
87 {
88 if ( IsGetSupported() )
89 {
90 Msg* msg = new Msg( "ProtectionCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
91 msg->SetInstance( this, _instance );
92 msg->Append( GetNodeId() );
93 msg->Append( 2 );
94 msg->Append( GetCommandClassId() );
95 msg->Append( ProtectionCmd_Get );
96 msg->Append( GetDriver()->GetTransmitOptions() );
97 GetDriver()->SendMsg( msg, _queue );
98 return true;
99 } else {
100 Log::Write( LogLevel_Info, GetNodeId(), "ProtectionCmd_Get Not Supported on this node");
101 }
102 return false;
103 }
104
105 //-----------------------------------------------------------------------------
106 // <Protection::HandleMsg>
107 // Handle a message from the Z-Wave network
108 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)109 bool Protection::HandleMsg
110 (
111 uint8 const* _data,
112 uint32 const _length,
113 uint32 const _instance // = 1
114 )
115 {
116 if (ProtectionCmd_Report == (ProtectionCmd)_data[0])
117 {
118 int8 stateValue = _data[1];
119 if (stateValue > 2) /* size of c_protectionStateNames minus Invalid */
120 {
121 Log::Write (LogLevel_Warning, GetNodeId(), "State Value was greater than range. Setting to Invalid");
122 stateValue = 3;
123 }
124 Log::Write( LogLevel_Info, GetNodeId(), "Received a Protection report: %s", c_protectionStateNames[_data[1]] );
125 if( ValueList* value = static_cast<ValueList*>( GetValue( _instance, 0 ) ) )
126 {
127 value->OnValueRefreshed( (int)_data[1] );
128 value->Release();
129 }
130
131 return true;
132 }
133
134 return false;
135 }
136
137 //-----------------------------------------------------------------------------
138 // <Protection::SetValue>
139 // Set the device's protection state
140 //-----------------------------------------------------------------------------
SetValue(Value const & _value)141 bool Protection::SetValue
142 (
143 Value const& _value
144 )
145 {
146 if( ValueID::ValueType_List == _value.GetID().GetType() )
147 {
148 ValueList const* value = static_cast<ValueList const*>(&_value);
149 ValueList::Item const *item = value->GetItem();
150 if (item == NULL)
151 return false;
152
153 Log::Write( LogLevel_Info, GetNodeId(), "Protection::Set - Setting protection state to '%s'", item->m_label.c_str() );
154 Msg* msg = new Msg( "ProtectionCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
155 msg->SetInstance( this, _value.GetID().GetInstance() );
156 msg->Append( GetNodeId() );
157 msg->Append( 3 );
158 msg->Append( GetCommandClassId() );
159 msg->Append( ProtectionCmd_Set );
160 msg->Append( (uint8)item->m_value );
161 msg->Append( GetDriver()->GetTransmitOptions() );
162 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
163 return true;
164 }
165
166 return false;
167 }
168
169 //-----------------------------------------------------------------------------
170 // <Protection::CreateVars>
171 // Create the values managed by this command class
172 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)173 void Protection::CreateVars
174 (
175 uint8 const _instance
176 )
177 {
178 if( Node* node = GetNodeUnsafe() )
179 {
180 vector<ValueList::Item> items;
181
182 ValueList::Item item;
183 for( uint8 i=0; i<3; ++i )
184 {
185 item.m_label = c_protectionStateNames[i];
186 item.m_value = i;
187 items.push_back( item );
188 }
189
190 node->CreateValueList( ValueID::ValueGenre_System, GetCommandClassId(), _instance, 0, "Protection", "", false, false, 1, items, 0, 0 );
191 }
192 }
193