Debian Codegraph @

Hardware/Protocols/LocoNet.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 <algorithm>
24 #include <ctime>
25
26 #include "Hardware/HardwareInterface.h"
27 #include "Hardware/HardwareParams.h"
28 #include "Hardware/Protocols/LocoNetLocoCache.h"
29 #include "Logger/Logger.h"
30 #include "Network/Serial.h"
31
32 // Protocol specification at https://www.digitrax.com/static/apps/cms/media/documents/loconet/loconetpersonaledition.pdf
33 // Programming specification does not fit for Uhlenbrock Intellibox II
34
35 namespace Hardware
36 {
37 namespace Protocols
38 {
39 class LocoNet: Hardware::HardwareInterface
40 {
41 public:
42 LocoNet() = delete;
43 LocoNet(const LocoNet&) = delete;
44 LocoNet& operator=(const LocoNet&) = delete;
45
46 LocoNet(const HardwareParams* params,
47 const std::string& controlName,
48 const unsigned int dataSpeed);
49
50 virtual ~LocoNet();
51
52 inline void Start() override
53 {
54 SendRequestLocoData(0x7F);
55 Utils::Utils::SleepForMilliseconds(25);
56 SendRequestLocoData(0);
57 Utils::Utils::SleepForMilliseconds(25);
58 }
59
60 inline Hardware::Capabilities GetCapabilities() const override
61 {
62 return Hardware::CapabilityLoco
63 | Hardware::CapabilityAccessory
64 | Hardware::CapabilityFeedback
65 | Hardware::CapabilityProgram
66 | Hardware::CapabilityProgramMmWrite
67 | Hardware::CapabilityProgramDccRegisterRead
68 | Hardware::CapabilityProgramDccRegisterWrite
69 | Hardware::CapabilityProgramDccPageRead
70 | Hardware::CapabilityProgramDccPageWrite
71 | Hardware::CapabilityProgramDccDirectRead
72 | Hardware::CapabilityProgramDccDirectWrite;
73 }
74
75 void GetLocoProtocols(std::vector<Protocol>& protocols) const override
76 {
77 protocols.push_back(ProtocolServer);
78 }
79
80 bool LocoProtocolSupported(Protocol protocol) const override
81 {
82 return (protocol == ProtocolServer);
83 }
84
85 void GetAccessoryProtocols(std::vector<Protocol>& protocols) const override
86 {
87 protocols.push_back(ProtocolServer);
88 }
89
90 bool AccessoryProtocolSupported(Protocol protocol) const override
91 {
92 return (protocol == ProtocolServer);
93 }
94
95 void Booster(const BoosterState status) override;
96
97 void LocoSpeed(const Protocol protocol,
98 const Address address,
99 const Speed speed) override;
100
101 void LocoOrientation(const Protocol protocol,
102 const Address address,
103 const Orientation orientation) override;
104
105 void LocoFunction(const Protocol protocol,
106 const Address address,
107 const DataModel::LocoFunctionNr function,
108 const DataModel::LocoFunctionState on) override;
109
110 void Accessory(const Protocol protocol,
111 const Address address,
112 const DataModel::AccessoryState state,
113 const bool on,
114 __attribute__((unused)) const DataModel::AccessoryPulseDuration duration) override;
115
116 void ProgramWrite(const ProgramMode mode,
117 const Address address,
118 const CvNumber cv,
119 const CvValue value) override;
120
121 void ProgramRead(const ProgramMode mode,
122 const Address address,
123 const CvNumber cv) override;
124
125 private:
126 // longest known LocoNet-Command is OPC_START_PROGRAM (Uhlenbrock Extension)
127 static const unsigned char MaxDataLength = 0x1F;
128
129 enum OpCodes : unsigned char
130 {
131 OPC_BUSY = 0x81,
132 OPC_GPOFF = 0x82,
133 OPC_GPON = 0x83,
134 OPC_IDLE = 0x85,
135 OPC_LOCO_SPD = 0xA0,
136 OPC_LOCO_DIRF = 0xA1,
137 OPC_LOCO_SND = 0xA2,
138 OPC_LOCO_FUNC = 0xA3,
139 OPC_SW_REQ = 0xB0,
140 OPC_SW_REP = 0xB1,
141 OPC_INPUT_REP = 0xB2,
142 OPC_LONG_ACK = 0xB4,
143 OPC_SLOT_STAT1 = 0xB5,
144 OPC_CONSIST_FUNC = 0xB6,
145 OPC_UNLINK_SLOTS = 0xB8,
146 OPC_LINK_SLOTS = 0xB9,
147 OPC_MOVE_SLOTS = 0xBA,
148 OPC_RQ_SL_DATA = 0xBB,
149 OPC_SW_STATE = 0xBC,
150 OPC_SW_ACK = 0xBD,
151 OPC_LOCO_ADR = 0xBF,
152 OPC_PEER_XFER = 0xE5, // Intellibox-II see below
153 OPC_SL_RD_DATA = 0xE7,
154 OPC_IMM_PACKET = 0xED, // Intellibox-II see below
155 OPC_WR_SL_DATA = 0xEF,
156 // Intellibox-II codes
157 OPC_LOCO_XADR = 0xBE,
158 OPC_EXP_CMD = 0xD4,
159 OPC_START_PROGRAM = 0xE5, // not documented
160 OPC_SL_RD_DATA_EXT = 0xE6,
161 OPC_PROGRAM = 0xED, // not documented
162 OPC_WR_SL_DATA_EXT = 0xEE
163 };
164
165 static const unsigned char SlotHardwareType = 0;
166 static const unsigned char SlotProgramming = 124;
167
168 class SendingQueueEntry
169 {
170 public:
171 inline SendingQueueEntry()
172 : size(0)
173 {
174 }
175
176 inline SendingQueueEntry(unsigned char size, const unsigned char* data)
177 : size(std::min(size, static_cast<unsigned char>(MaxDataLength)))
178 {
179 memcpy(this->data, data, this->size);
180 }
181
182 inline SendingQueueEntry(const SendingQueueEntry& rhs) = default;
183
184 inline SendingQueueEntry& operator=(const SendingQueueEntry& rhs) = default;
185
186 inline unsigned char GetSize() const
187 {
188 return size;
189 }
190
191 inline const unsigned char* GetData() const
192 {
193 return data;
194 }
195
196 inline void Reset()
197 {
198 size = 0;
199 }
200
201 private:
202 volatile unsigned char size;
203 unsigned char data[MaxDataLength];
204 };
205
206 void Sender();
207
208 void Receiver();
209
210 static void CalcCheckSum(unsigned char* data, const unsigned char length, unsigned char* checkSum);
211
212 void Parse(unsigned char* data);
213
214 Address CheckSlot(const unsigned char slot);
215
216 void ParseSlotReadData(const unsigned char* data);
217
218 void ParseSlotHardwareType(const unsigned char* data);
219
220 void ParseSlotProgramming(const unsigned char* data);
221
222 void ParseSlotLocoData(const unsigned char* data);
223
224 void ParseProgram(const unsigned char* data);
225
226 inline Address ParseLocoAddress(const unsigned char data1, const unsigned char data2)
227 {
228 return static_cast<Address>(data1 & 0x7F) | (static_cast<Address>(data2 & 0x3F) << 7);
229 }
230
231 void ParseSpeed(const Address address, const unsigned char data);
232
233 static unsigned char CalcSpeed(const Speed speed);
234
235 void ParseOrientationF0F4(const unsigned char slot, const Address address, const uint8_t data);
236
237 void ParseF5F8(const unsigned char slot, const Address address, const uint8_t data);
238
239 void ParseF9F12(const unsigned char slot, const Address address, const uint8_t data);
240
241 void ParseFunction(const Address address,
242 const uint32_t data,
243 const DataModel::LocoFunctionNr nr,
244 const uint8_t shift);
245
246 void ParseF13F44(const unsigned char slot, const Address address, const unsigned char* data);
247
248 void ParseSensorData(const unsigned char* data);
249
250 void Send2ByteCommand(const unsigned char data0);
251
252 void Send4ByteCommand(const unsigned char data0,
253 const unsigned char data1,
254 const unsigned char data2);
255
256 void Send6ByteCommand(const unsigned char data0,
257 const unsigned char data1,
258 const unsigned char data2,
259 const unsigned char data3,
260 const unsigned char data4);
261
262 void SendXByteCommand(unsigned char* data, unsigned char dataLength);
263
264 inline void SendRequestLocoData(const unsigned char slot)
265 {
266 Send4ByteCommand(OPC_RQ_SL_DATA, slot, 0);
267 }
268
269 inline void SendLocoAddress(const Address address)
270 {
271 Send4ByteCommand(OPC_LOCO_ADR,
272 static_cast<unsigned char>((address >> 7) & 0x7F),
273 static_cast<unsigned char>(address & 0x7F));
274 }
275
276 inline void SendLocoSpeed(const unsigned char slot, const Speed speed)
277 {
278 Send4ByteCommand(OPC_LOCO_SPD, slot, CalcSpeed(speed));
279 }
280
281 inline void SendLocoOrientationF0F4(const unsigned char slot, const uint8_t orientationF0F4)
282 {
283 Send4ByteCommand(OPC_LOCO_DIRF, slot, orientationF0F4);
284 }
285
286 inline void SendLocoF5F8(const unsigned char slot, const uint8_t f5F8)
287 {
288 Send4ByteCommand(OPC_LOCO_SND, slot, f5F8);
289 }
290
291 inline void SendLocoF9F12(const unsigned char slot, const uint8_t f9F12)
292 {
293 Send4ByteCommand(OPC_LOCO_FUNC, slot, f9F12);
294 }
295
296 uint8_t SetOrientationF0F4Bit(const unsigned char slot, const bool on, const unsigned char shift);
297
298 uint8_t SetF5F8Bit(const unsigned char slot, const bool on, const unsigned char shift);
299
300 uint8_t SetF9F12Bit(const unsigned char slot, const bool on, const unsigned char shift);
301
302 uint32_t SetF13F44Bit(const unsigned char slot, const bool on, const unsigned char shift);
303
304 void ProgramStart();
305
306 void ProgramEnd();
307
308 // void ProgramMain(const Address address,
309 // const CvNumber cv,
310 // const CvValue value);
311
312 void ProgramPT(const bool write,
313 const ProgramMode mode,
314 const CvNumber cv,
315 const CvValue value = 0);
316
317 volatile bool run;
318 mutable Network::Serial serialLine;
319 std::thread senderThread;
320 std::thread receiverThread;
321
322 LocoNetLocoCache locoCache;
323
324 Utils::ThreadSafeQueue<SendingQueueEntry> sendingQueue;
325 SendingQueueEntry entryToVerify;
326 mutable std::mutex entryToVerifyMutex;
327 std::condition_variable entryToVerifyCV;
328
329 CvNumber lastCv;
330 bool isProgramming;
331 };
332 } // namespace
333 } // namespace
334