Debian Codegraph @

Hardware/Protocols/P50x.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 <mutex>
24 #include <string>
25
26 #include "Hardware/HardwareInterface.h"
27 #include "Hardware/HardwareParams.h"
28 #include "Hardware/Protocols/P50xCache.h"
29 #include "Logger/Logger.h"
30
31 namespace Hardware
32 {
33 namespace Protocols
34 {
35 class P50x: protected Hardware::HardwareInterface
36 {
37 public:
38 enum P50xType : unsigned char
39 {
40 TypeOpenDcc,
41 TypeUhlenbrock,
42 TypeTams
43 };
44
45 P50x() = delete;
46 P50x(const P50x&) = delete;
47 P50x& operator=(const P50x&) = delete;
48
49 P50x(const HardwareParams* const params,
50 const std::string& controlName,
51 const P50xType type);
52
53 virtual ~P50x();
54
55 inline Hardware::Capabilities GetCapabilities() const override
56 {
57 return Hardware::CapabilityLoco
58 | Hardware::CapabilityAccessory
59 | Hardware::CapabilityFeedback;
60 }
61
62 inline void GetLocoProtocols(std::vector<Protocol>& protocols) const override
63 {
64 protocols.push_back(ProtocolServer);
65 }
66
67 inline bool LocoProtocolSupported(Protocol protocol) const override
68 {
69 return (protocol == ProtocolServer);
70 }
71
72 inline void GetAccessoryProtocols(std::vector<Protocol>& protocols) const override
73 {
74 protocols.push_back(ProtocolServer);
75 }
76
77 inline bool AccessoryProtocolSupported(Protocol protocol) const override
78 {
79 return (protocol == ProtocolServer);
80 }
81
82 void Booster(const BoosterState status) override;
83 void LocoSpeed(const Protocol protocol, const Address address, const Speed speed) override;
84 void LocoOrientation(const Protocol protocol, const Address address, const Orientation orientation)
85 override;
86
87 void LocoFunction(const Protocol protocol,
88 const Address address,
89 const DataModel::LocoFunctionNr function,
90 const DataModel::LocoFunctionState on) override;
91
92 void LocoSpeedOrientationFunctions(const Protocol protocol,
93 const Address address,
94 const Speed speed,
95 const Orientation orientation,
96 std::vector<DataModel::LocoFunctionEntry>& functions) override;
97
98 void Accessory(const Protocol protocol,
99 const Address address,
100 const DataModel::AccessoryState state,
101 const bool on,
102 const DataModel::AccessoryPulseDuration duration) override;
103
104 void CheckEventsWorker();
105
106 protected:
107 virtual int Send(const unsigned char* buffer, const size_t bufferLength) const = 0;
108 virtual ssize_t Receive(unsigned char* data, const size_t length) const = 0;
109 virtual ssize_t ReceiveExact(unsigned char* data, const size_t length) const = 0;
110
111 void Init();
112
113 unsigned short s88Modules;
114 std::thread checkEventsThread;
115
116 private:
117 enum Commands : unsigned char
118 {
119 XNop = 0xC4,
120 XPwrOn = 0xA7,
121 XPwrOff = 0xA6,
122 XLok = 0x80,
123 XFunc = 0x88,
124 XFunc2 = 0x89,
125 XFunc34 = 0x8A,
126 XTrnt = 0x90,
127 XP88Get = 0x9C,
128 XP88Set = 0x9D,
129 XStatus = 0xA2,
130 XEvent = 0xC8,
131 XEvtLok = 0xC9,
132 XEvtTrnt = 0xCA,
133 XEvtSen = 0xCB
134 };
135
136 enum Answers : unsigned char
137 {
138 OK = 0x00,
139 XBADPRM = 0x02,
140 XPWOFF = 0x06,
141 XNODATA = 0x0A,
142 XNOSLOT = 0x0B,
143 XLOWTSP = 0x40,
144 XLKHALT = 0x41,
145 XLKPOFF = 0x42
146 };
147
148 struct LocoCommand
149 {
150 Address address;
151 Speed speed;
152 Orientation orientation;
153 };
154
155 struct LocoFunction
156 {
157 Address address;
158 DataModel::LocoFunctionNr nr;
159 DataModel::LocoFunctionState state;
160 };
161
162 struct TurnoutCommand
163 {
164 Address address;
165 DataModel::AccessoryState state;
166 };
167
168 static const unsigned char MaxLocoFunctions = 28;
169 static const unsigned short MaxLocoAddress = 10239;
170 static const unsigned short MaxAccessoryAddress = 2043;
171
172 void InitOpenDcc();
173 void InitUhlenbrockTams();
174
175 bool SendP50XOnly() const;
176 bool SendRestart() const;
177 unsigned char SendXP88Get(unsigned char param) const;
178 bool SendXP88Set(unsigned char param, unsigned char value) const;
179
180 inline bool SendNop() const
181 {
182 return SendOneByteCommand(XNop);
183 }
184
185 inline ssize_t SendInternal(const unsigned char data) const
186 {
187 return SendInternal(&data, 1);
188 }
189
190 inline ssize_t SendInternal(const unsigned char* data, const size_t dataLength) const
191 {
192 logger->HexOut(data, dataLength);
193 return Send(data, dataLength);
194 }
195
196 inline ssize_t ReceiveInternal(unsigned char* data, const size_t length) const
197 {
198 const ssize_t ret = Receive(data, length);
199 if (ret > 0)
200 {
201 logger->HexIn(data, ret);
202 }
203 return ret;
204 }
205
206 inline ssize_t ReceiveExactInternal(unsigned char* data, const size_t length) const
207 {
208 const ssize_t ret = ReceiveExact(data, length);
209 if (ret > 0)
210 {
211 logger->HexIn(data, ret);
212 }
213 return ret;
214 }
215
216 static inline bool CheckLocoAddress(const Address address)
217 {
218 return 0 < address && address <= MaxLocoAddress;
219 }
220
221 static inline bool CheckAccessoryAddress(const Address address)
222 {
223 return 0 < address && address <= MaxAccessoryAddress;
224 }
225
226 bool SendOneByteCommand(const unsigned char data) const;
227
228 inline bool SendPowerOn() const
229 {
230 return SendOneByteCommand(XPwrOn);
231 }
232
233 inline bool SendPowerOff() const
234 {
235 return SendOneByteCommand(XPwrOff);
236 }
237
238 bool SendXLok(const Address address) const;
239 bool SendXFunc(const Address address) const;
240 bool SendXFunc2(const Address address) const;
241 bool SendXFunc34(const Address address) const;
242 bool ReceiveFunctionCommandAnswer() const;
243 void CheckSensorData(const unsigned char module, const unsigned char data) const;
244 void SendXEvtSen() const;
245 void SendXEvtLok() const;
246 void SendXEvtTrn() const;
247 void SendXEvent() const;
248 void SendXStatus() const;
249
250 const HardwareParams* const params;
251 const P50xType type;
252
253 volatile bool run;
254
255 mutable unsigned char s88Memory[128];
256
257 P50xCache cache;
258
259 mutable std::mutex communicationLock;
260 };
261 } // namespace
262 } // namespace