Debian Codegraph @

Hardware/Protocols/Z21.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <arpa/inet.h>
24 #include <cstring>
25 #include <string>
26 #include <thread>
27
28 #include "DataModel/AccessoryBase.h"
29 #include "Hardware/HardwareInterface.h"
30 #include "Hardware/HardwareParams.h"
31 #include "Hardware/Protocols/Z21DataTypes.h"
32 #include "Hardware/Protocols/Z21FeedbackCache.h"
33 #include "Hardware/Protocols/Z21LocoCache.h"
34 #include "Hardware/Protocols/Z21TurnoutCache.h"
35 #include "Logger/Logger.h"
36 #include "Network/UdpConnection.h"
37
38 namespace Z21Enums = Hardware::Protocols::Z21Enums;
39
40 // protocol specification at https://www.z21.eu/media/Kwc_Basic_DownloadTag_Component/47-1652-959-downloadTag/default/69bad87e/1558674980/z21-lan-protokoll.pdf
41
42 namespace Hardware
43 {
44 namespace Protocols
45 {
46 class Z21: Hardware::HardwareInterface
47 {
48 public:
49 Z21() = delete;
50 Z21(const Z21&) = delete;
51 Z21& operator=(const Z21&) = delete;
52
53 Z21(const HardwareParams* params, const std::string& controlName);
54 virtual ~Z21();
55
56 inline Hardware::Capabilities GetCapabilities() const override
57 {
58 return Hardware::CapabilityLoco
59 | Hardware::CapabilityAccessory
60 | Hardware::CapabilityFeedback
61 | Hardware::CapabilityProgram
62 | Hardware::CapabilityProgramMmWrite
63 | Hardware::CapabilityProgramDccDirectRead
64 | Hardware::CapabilityProgramDccDirectWrite
65 | Hardware::CapabilityProgramDccPomLocoRead
66 | Hardware::CapabilityProgramDccPomLocoWrite
67 | Hardware::CapabilityProgramDccPomAccessoryRead
68 | Hardware::CapabilityProgramDccPomAccessoryWrite;
69 }
70
71 void GetLocoProtocols(std::vector<Protocol>& protocols) const override
72 {
73 protocols.push_back(ProtocolMM1);
74 protocols.push_back(ProtocolMM15);
75 protocols.push_back(ProtocolMM2);
76 protocols.push_back(ProtocolDCC14);
77 protocols.push_back(ProtocolDCC28);
78 protocols.push_back(ProtocolDCC128);
79 }
80
81 bool LocoProtocolSupported(Protocol protocol) const override
82 {
83 return (protocol == ProtocolMM1
84 || protocol == ProtocolMM15
85 || protocol == ProtocolMM2
86 || protocol == ProtocolDCC14
87 || protocol == ProtocolDCC28
88 || protocol == ProtocolDCC128);
89 }
90
91 void GetAccessoryProtocols(std::vector<Protocol>& protocols) const override
92 {
93 protocols.push_back(ProtocolMM);
94 protocols.push_back(ProtocolDCC);
95 }
96
97 bool AccessoryProtocolSupported(Protocol protocol) const override
98 {
99 return (protocol == ProtocolMM
100 || protocol == ProtocolDCC);
101 }
102
103 static void GetArgumentTypesAndHint(std::map<unsigned char, ArgumentType>& argumentTypes,
104 std::string& hint)
105 {
106 argumentTypes[1] = ArgumentTypeIpAddress;
107 hint = Languages::GetText(Languages::TextHintZ21);
108 }
109
110 void Booster(const BoosterState status) override;
111 void LocoSpeed(const Protocol protocol, const Address address, const Speed speed) override;
112 void LocoOrientation(const Protocol protocol, const Address address, const Orientation orientation)
113 override;
114
115 void LocoFunction(const Protocol protocol,
116 const Address address,
117 const DataModel::LocoFunctionNr function,
118 const DataModel::LocoFunctionState on) override;
119
120 void LocoSpeedOrientationFunctions(const Protocol protocol,
121 const Address address,
122 const Speed speed,
123 const Orientation orientation,
124 std::vector<DataModel::LocoFunctionEntry>& functions) override;
125
126 void Accessory(const Protocol protocol,
127 const Address address,
128 const DataModel::AccessoryState state,
129 const bool on,
130 const DataModel::AccessoryPulseDuration duration) override;
131
132 void ProgramRead(const ProgramMode mode,
133 const Address address,
134 const CvNumber cv) override;
135
136 void ProgramWrite(const ProgramMode mode,
137 const Address address,
138 const CvNumber cv,
139 const CvValue value) override;
140
141 static unsigned char EncodeSpeed14(const Speed speed);
142 static unsigned char EncodeSpeed28(const Speed speed);
143 static unsigned char EncodeSpeed128(const Speed speed);
144 static Speed DecodeSpeed14(unsigned char data);
145 static Speed DecodeSpeed28(unsigned char data);
146 static Speed DecodeSpeed128(unsigned char data);
147
148 private:
149 static const unsigned short Z21Port = 21105;
150 static const unsigned int Z21CommandBufferLength = 1472; // = Max Ethernet MTU
151 static const Address MaxMMAddress = 255;
152
153 class AccessoryQueueEntry
154 {
155 public:
156 inline AccessoryQueueEntry()
157 : protocol(ProtocolNone),
158 address(AddressNone),
159 state(DataModel::DefaultState),
160 duration(DataModel::DefaultAccessoryPulseDuration)
161 {
162 }
163
164 inline AccessoryQueueEntry(const Protocol protocol, const Address address,
165 const DataModel::AccessoryState state, const DataModel::AccessoryPulseDuration duration)
166 : protocol(protocol),
167 address(address),
168 state(state),
169 duration(duration)
170 {
171 }
172
173 Protocol protocol;
174 Address address;
175 DataModel::AccessoryState state;
176 DataModel::AccessoryPulseDuration duration;
177 };
178
179 volatile bool run;
180 Network::UdpConnection connection;
181 std::thread receiverThread;
182 std::thread heartBeatThread;
183 std::thread accessorySenderThread;
184 Z21LocoCache locoCache;
185 Z21TurnoutCache turnoutCache;
186 Z21FeedbackCache feedbackCache;
187 ProgramMode lastProgramMode;
188 volatile bool connected;
189
190 Utils::ThreadSafeQueue<AccessoryQueueEntry> accessoryQueue;
191
192 void ProgramMm(const CvNumber cv, const CvValue value);
193 void ProgramDccRead(const CvNumber cv);
194 void ProgramDccWrite(const CvNumber cv, const CvValue value);
195 void ProgramDccPom(const Z21Enums::PomDB0 db0,
196 const Z21Enums::PomOption option,
197 const Address address, const CvNumber cv,
198 const CvValue value = 0);
199
200 void LocoSpeedOrientation(const Protocol protocol,
201 const Address address,
202 const Speed speed,
203 const Orientation orientation);
204
205 void AccessorySender();
206
207 void AccessoryOnOrOff(const Address address,
208 const DataModel::AccessoryState state,
209 const bool on);
210
211 void HeartBeatSender();
212
213 void Receiver();
214
215 ssize_t ParseData(const unsigned char* buffer, size_t bufferLength);
216 void ParseXHeader(const unsigned char* buffer);
217 void ParseDB0(const unsigned char* buffer);
218 void ParseTurnoutData(const unsigned char* buffer);
219 void ParseLocoData(const unsigned char* buffer);
220 void ParseCvData(const unsigned char* buffer);
221 void ParseRmBusData(const unsigned char* buffer);
222 void ParseLocoNetDetector(const unsigned char* buffer);
223 void ParseDetectorData(const unsigned char* buffer);
224
225 void StartUpConnection();
226 void SendGetSerialNumber();
227 void SendGetHardwareInfo();
228 void SendGetStatus();
229 void SendGetCode();
230 void SendGetDetectorState();
231 void SendLogOff();
232 void SendBroadcastFlags();
233 void SendBroadcastFlags(const Z21Enums::BroadCastFlags flags);
234 void SendSetMode(const Address address, const Z21Enums::Command command, const Z21Enums::ProtocolMode mode);
235 void SendSetLocoMode(const Address address, const Protocol protocol);
236 void SendSetLocoModeMM(const Address address);
237 void SendSetLocoModeDCC(const Address address);
238 void SendSetTurnoutMode(const Address address, const Protocol protocol);
239 void SendSetTurnoutModeMM(const Address address);
240 void SendSetTurnoutModeDCC(const Address address);
241 int Send(const unsigned char* buffer, const size_t bufferLength);
242
243 inline int Send(const char* buffer, const size_t bufferLength)
244 {
245 return Send(reinterpret_cast<const unsigned char*>(buffer), bufferLength);
246 }
247 };
248 } // namespace
249 } // namespace
250