forked from OpenZWave/open-zwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtection.cpp
More file actions
177 lines (159 loc) · 6.06 KB
/
Copy pathProtection.cpp
File metadata and controls
177 lines (159 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//-----------------------------------------------------------------------------
//
// Protection.cpp
//
// Implementation of the Z-Wave COMMAND_CLASS_PROTECTION
//
// Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <vector>
#include "command_classes/CommandClasses.h"
#include "command_classes/Protection.h"
#include "Defs.h"
#include "Msg.h"
#include "Node.h"
#include "Driver.h"
#include "platform/Log.h"
#include "value_classes/ValueList.h"
namespace OpenZWave
{
namespace Internal
{
namespace CC
{
enum ProtectionCmd
{
ProtectionCmd_Set = 0x01,
ProtectionCmd_Get = 0x02,
ProtectionCmd_Report = 0x03
};
static char const* c_protectionStateNames[] =
{ "Unprotected", "Protection by Sequence", "No Operation Possible", "Unknown" };
//-----------------------------------------------------------------------------
// <Protection::RequestState>
// Request current state from the device
//-----------------------------------------------------------------------------
bool Protection::RequestState(uint32 const _requestFlags, uint8 const _instance, Driver::MsgQueue const _queue)
{
if (_requestFlags & RequestFlag_Session)
{
return RequestValue(_requestFlags, 0, _instance, _queue);
}
return false;
}
//-----------------------------------------------------------------------------
// <Protection::RequestValue>
// Request current value from the device
//-----------------------------------------------------------------------------
bool Protection::RequestValue(uint32 const _requestFlags, uint16 const _dummy1, // = 0 (not used)
uint8 const _instance, Driver::MsgQueue const _queue)
{
if (m_com.GetFlagBool(COMPAT_FLAG_GETSUPPORTED))
{
Msg* msg = new Msg("ProtectionCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId());
msg->SetInstance(this, _instance);
msg->Append(GetNodeId());
msg->Append(2);
msg->Append(GetCommandClassId());
msg->Append(ProtectionCmd_Get);
msg->Append(GetDriver()->GetTransmitOptions());
GetDriver()->SendMsg(msg, _queue);
return true;
}
else
{
Log::Write(LogLevel_Info, GetNodeId(), "ProtectionCmd_Get Not Supported on this node");
}
return false;
}
//-----------------------------------------------------------------------------
// <Protection::HandleMsg>
// Handle a message from the Z-Wave network
//-----------------------------------------------------------------------------
bool Protection::HandleMsg(uint8 const* _data, uint32 const _length, uint32 const _instance // = 1
)
{
if (ProtectionCmd_Report == (ProtectionCmd) _data[0])
{
int8 stateValue = _data[1];
if (stateValue > 2) /* size of c_protectionStateNames minus Invalid */
{
Log::Write(LogLevel_Warning, GetNodeId(), "State Value was greater than range. Setting to Invalid");
stateValue = 3;
}
Log::Write(LogLevel_Info, GetNodeId(), "Received a Protection report: %s", c_protectionStateNames[_data[1]]);
if (Internal::VC::ValueList* value = static_cast<Internal::VC::ValueList*>(GetValue(_instance, ValueID_Index_Protection::Protection)))
{
value->OnValueRefreshed((int) _data[1]);
value->Release();
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// <Protection::SetValue>
// Set the device's protection state
//-----------------------------------------------------------------------------
bool Protection::SetValue(Internal::VC::Value const& _value)
{
if (ValueID::ValueType_List == _value.GetID().GetType())
{
Internal::VC::ValueList const* value = static_cast<Internal::VC::ValueList const*>(&_value);
Internal::VC::ValueList::Item const *item = value->GetItem();
if (item == NULL)
return false;
Log::Write(LogLevel_Info, GetNodeId(), "Protection::Set - Setting protection state to '%s'", item->m_label.c_str());
Msg* msg = new Msg("ProtectionCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true);
msg->SetInstance(this, _value.GetID().GetInstance());
msg->Append(GetNodeId());
msg->Append(3);
msg->Append(GetCommandClassId());
msg->Append(ProtectionCmd_Set);
msg->Append((uint8) item->m_value);
msg->Append(GetDriver()->GetTransmitOptions());
GetDriver()->SendMsg(msg, Driver::MsgQueue_Send);
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// <Protection::CreateVars>
// Create the values managed by this command class
//-----------------------------------------------------------------------------
void Protection::CreateVars(uint8 const _instance)
{
if (Node* node = GetNodeUnsafe())
{
vector<Internal::VC::ValueList::Item> items;
Internal::VC::ValueList::Item item;
for (uint8 i = 0; i < 3; ++i)
{
item.m_label = c_protectionStateNames[i];
item.m_value = i;
items.push_back(item);
}
node->CreateValueList(ValueID::ValueGenre_System, GetCommandClassId(), _instance, ValueID_Index_Protection::Protection, "Protection", "", false, false, 1, items, 0, 0);
}
}
} // namespace CC
} // namespace Internal
} // namespace OpenZWave