|
| 1 | +//----------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Supervision.h |
| 4 | +// |
| 5 | +// Implementation of the Z-Wave COMMAND_CLASS_SUPERVISION |
| 6 | +// |
| 7 | +// Copyright (c) 2020 Mark Ruys <mark@paracas.nl> |
| 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/Supervision.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/Value.h" |
| 37 | + |
| 38 | +namespace OpenZWave |
| 39 | +{ |
| 40 | + namespace Internal |
| 41 | + { |
| 42 | + namespace CC |
| 43 | + { |
| 44 | + uint8 Supervision::CreateSupervisionSession(uint8 _command_class_id, uint8 _index) |
| 45 | + { |
| 46 | + m_last_session_id++; |
| 47 | + m_last_session_id &= 0x3f; |
| 48 | + |
| 49 | + if (m_sessions.size() >= 6) |
| 50 | + { |
| 51 | + // Clean up oldest session, we support max 6 simultaneous sessions per node |
| 52 | + m_sessions.pop_front(); |
| 53 | + } |
| 54 | + |
| 55 | + m_sessions.push_back({ |
| 56 | + .session_id = m_last_session_id, |
| 57 | + .command_class_id = _command_class_id, |
| 58 | + .index = _index |
| 59 | + }); |
| 60 | + |
| 61 | + return m_last_session_id; |
| 62 | + } |
| 63 | + |
| 64 | + uint32 Supervision::GetSupervisionIndex(uint8 _session_id) |
| 65 | + { |
| 66 | + for (auto it = m_sessions.cbegin(); it != m_sessions.cend(); ++it) |
| 67 | + { |
| 68 | + if (it->session_id == _session_id) |
| 69 | + { |
| 70 | + return it->index; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return StaticNoIndex(); |
| 75 | + } |
| 76 | + |
| 77 | +//----------------------------------------------------------------------------- |
| 78 | +// <Supervision::HandleSupervisionReport> |
| 79 | +// Handle a supervision report message from the Z-Wave network |
| 80 | +//----------------------------------------------------------------------------- |
| 81 | + void Supervision::HandleSupervisionReport(uint8 const* _data, uint32 const _length, uint32 const _instance) |
| 82 | + { |
| 83 | + if (Node* node = GetNodeUnsafe()) |
| 84 | + { |
| 85 | + if (_length >= 4) |
| 86 | + { |
| 87 | + uint8 more_status_updates = _data[1] >> 7; |
| 88 | + uint8 session_id = _data[1] & 0x3f; |
| 89 | + uint8 status = _data[2]; |
| 90 | + int duration = _data[3]; |
| 91 | + |
| 92 | + const char *status_identifier; |
| 93 | + switch (status) { |
| 94 | + case 0x00: status_identifier = "NO_SUPPORT"; break; |
| 95 | + case 0x01: status_identifier = "WORKING"; break; |
| 96 | + case 0x02: status_identifier = "FAIL"; break; |
| 97 | + case 0xff: status_identifier = "SUCCESS"; break; |
| 98 | + default: status_identifier = "UNKNOWN"; break; |
| 99 | + } |
| 100 | + |
| 101 | + for (auto it = m_sessions.cbegin(); it != m_sessions.cend(); ++it) |
| 102 | + { |
| 103 | + if (it->session_id == session_id) |
| 104 | + { |
| 105 | + if (CommandClass* pCommandClass = node->GetCommandClass(it->command_class_id)) |
| 106 | + { |
| 107 | + Log::Write(LogLevel_Info, GetNodeId(), "Received SupervisionReport: session %d, %s index %d, status %s, duration %d sec, more status updates %d", |
| 108 | + session_id, |
| 109 | + pCommandClass->GetCommandClassName().c_str(), it->index, |
| 110 | + status_identifier, decodeDuration(duration), more_status_updates); |
| 111 | + |
| 112 | + if (status == SupervisionStatus::SupervisionStatus_Success) |
| 113 | + { |
| 114 | + pCommandClass->SupervisionSessionSuccess(session_id, _instance); |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + Log::Write(LogLevel_Warning, GetNodeId(), "Received SupervisionReport for unknown CC %d", it->command_class_id); |
| 120 | + } |
| 121 | + |
| 122 | + if (more_status_updates == 0) |
| 123 | + { |
| 124 | + m_sessions.erase(it); |
| 125 | + } |
| 126 | + |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + Log::Write(LogLevel_Warning, GetNodeId(), "Received SupervisionReport: unknown session %d, status %s, duration %d sec, more status updates %d", |
| 132 | + session_id, |
| 133 | + status_identifier, decodeDuration(duration), more_status_updates); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + bool Supervision::HandleIncomingMsg(uint8 const* _data, uint32 const _length, uint32 const _instance) |
| 139 | + { |
| 140 | + return HandleMsg(_data, _length, _instance); |
| 141 | + } |
| 142 | + |
| 143 | +//----------------------------------------------------------------------------- |
| 144 | +// <Supervision::HandleMsg> |
| 145 | +// Handle a message from the Z-Wave network |
| 146 | +//----------------------------------------------------------------------------- |
| 147 | + bool Supervision::HandleMsg(uint8 const* _data, uint32 const _length, uint32 const _instance) // = 1 |
| 148 | + { |
| 149 | + bool handled = false; |
| 150 | + Node* node = GetNodeUnsafe(); |
| 151 | + if (node != NULL) |
| 152 | + { |
| 153 | + handled = true; |
| 154 | + switch ((SupervisionCmd) _data[0]) |
| 155 | + { |
| 156 | + case SupervisionCmd_Report: |
| 157 | + { |
| 158 | + HandleSupervisionReport(_data, _length, _instance); |
| 159 | + break; |
| 160 | + } |
| 161 | + default: |
| 162 | + { |
| 163 | + handled = false; |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return handled; |
| 170 | + } |
| 171 | + } // namespace CC |
| 172 | + } // namespace Internal |
| 173 | +} // namespace OpenZWave |
| 174 | + |
0 commit comments