Debian Codegraph @

Hardware/Protocols/MaerklinCANCommon.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 "DataModel/AccessoryBase.h"
24 #include "DataModel/LocoFunctions.h"
25 #include "Hardware/Capabilities.h"
26 #include "Hardware/HardwareInterface.h"
27 #include "Hardware/HardwareParams.h"
28 #include "Hardware/LocoCache.h"
29 #include "Logger/Logger.h"
30 #include "Utils/Integer.h"
31
32 // CAN protocol specification at http://streaming.maerklin.de/public-media/cs2/cs2CAN-Protokoll-2_0.pdf
33 // Very interesting is also http://www.mbernstein.de/modellbahn/can/bem.htm
34
35 namespace Hardware { namespace Protocols
36 {
37 class MaerklinCANCommon
38 {
39 public:
40 MaerklinCANCommon() = delete;
41 MaerklinCANCommon(const MaerklinCANCommon&) = delete;
42 MaerklinCANCommon& operator=(const MaerklinCANCommon&) = delete;
43
44 void Booster(const BoosterState status);
45 void LocoSpeed(const Protocol protocol, const Address address, const Speed speed);
46 void LocoOrientation(const Protocol protocol, const Address address, const Orientation orientation);
47
48 void LocoFunction(const Protocol protocol,
49 const Address address,
50 const DataModel::LocoFunctionNr function,
51 const DataModel::LocoFunctionState on);
52
53 void Accessory(const Protocol protocol,
54 const Address address,
55 const DataModel::AccessoryState state,
56 const bool on);
57
58 void ProgramRead(const ProgramMode mode, const Address address, const CvNumber cv);
59 void ProgramWrite(const ProgramMode mode, const Address address, const CvNumber cv, const CvValue value);
60
61 static inline DataModel::LocoFunctionIcon MapLocoFunctionCs2ToRailControl(const uint8_t input)
62 {
63 return LocoFunctionMapCs2ToRailControl[input];
64 }
65
66 static inline uint8_t MapLocofunctionRailControlToCs2(const DataModel::LocoFunctionNr nr,
67 const DataModel::LocoFunctionIcon railcontrolIcon)
68 {
69 LocoFunctionCs2Icon icon = LocoFunctionMapRailControlToCs2[railcontrolIcon];
70 if (icon == LocoFunctionCs2IconDefault)
71 {
72 icon = static_cast<LocoFunctionCs2Icon>(icon + nr);
73 }
74 return icon;
75 }
76
77 protected:
78 inline MaerklinCANCommon(const std::string& uid,
79 ControlID controlID,
80 Manager* manager,
81 const bool isCs2Main,
82 Logger::Logger* logger)
83 : run(true),
84 manager(manager),
85 logger(logger),
86 uid(Utils::Integer::HexToInteger(uid, 0)),
87 hash(CalcHash(this->uid)),
88 hasCs2Main(false),
89 isCs2Main(isCs2Main),
90 canFileData(nullptr),
91 canFileDataPointer(nullptr),
92 canFileDataSize(0),
93 canFileCrc(0),
94 canFileCrcSize(0),
95 controlID(controlID)
96 {
97 logger->Debug(Languages::TextMyUidHash, uid, Utils::Integer::IntegerToHex(hash));
98 }
99
100 void Init();
101
102 virtual ~MaerklinCANCommon();
103
104 void Parse(const unsigned char* buffer);
105
106 void Ping();
107 void RequestLoks();
108
109 void ReceiverInternal();
110
111 virtual void Receiver() = 0;
112
113 virtual void CacheSave(LocoCacheEntry& entry) = 0;
114
115 virtual void CacheSave(LocoCacheEntry& entry, const std::string& oldMatchKey) = 0;
116
117 virtual LocoID CacheDelete(const std::string& matchKey) = 0;
118
119 virtual void CacheUpdateSlaves() = 0;
120
121
122 static const unsigned char CANCommandBufferLength = 13;
123
124 volatile bool run;
125
126 Manager* manager;
127
128 Logger::Logger* logger;
129
130 private:
131 enum CanCommand : unsigned char
132 {
133 CanCommandSystem = 0x00,
134 CanCommandLocoSpeed = 0x04,
135 CanCommandLocoDirection = 0x05,
136 CanCommandLocoFunction = 0x06,
137 CanCommandReadConfig = 0x07,
138 CanCommandWriteConfig = 0x08,
139 CanCommandAccessory = 0x0B,
140 CanCommandS88Event = 0x11,
141 CanCommandPing = 0x18,
142 CanCommandRequestConfigData = 0x20,
143 CanCommandConfigData = 0x21,
144 CanCommandHello = 0x42
145 };
146
147 enum CanSubCommand : unsigned char
148 {
149 CanSubCommandStop = 0x00,
150 CanSubCommandGo = 0x01
151 };
152
153 enum CanResponse : unsigned char
154 {
155 CanResponseCommand = 0x00,
156 CanResponseResponse = 0x01
157 };
158
159 enum CanDeviceType : uint16_t
160 {
161 CanDeviceGfp = 0x0000,
162 CanDeviceGleisbox = 0x0010,
163 CanDeviceGleisbox_2 = 0x0011, // undocumented
164 CanDeviceConnect6021 = 0x0020,
165 CanDeviceMs2 = 0x0030,
166 CanDeviceMs2_2 = 0x0032, // undocumented
167 CanDeviceMs2_3 = 0x0033, // undocumented
168 CanDeviceMs2_4 = 0x0034, // undocumented
169 CanDeviceLinkS88 = 0x0040, // undocumented
170 CanDeviceCs2Secondary_2 = 0xeeee, // undocumented
171 CanDeviceWireless = 0xffe0,
172 CanDeviceCs2Secondary = 0xfff0, // undocumented
173 CanDeviceCs2Main = 0xffff
174 };
175
176 // enum CanFileType : uint8_t
177 // {
178 // CanFileTypeNone,
179 // CanFileTypeLokinfo,
180 // CanFileTypeLoknamen,
181 // CanFileTypeMaginfo,
182 // CanFileTypeLokdb,
183 // CanFileTypeLang,
184 // CanFileTypeLdbver,
185 // CanFileTypeLangver,
186 // CanFileTypeLoks,
187 // CanFileTypeMags,
188 // CanFileTypeGbs,
189 // CanFileTypeFs,
190 // CanFileTypeLokstat,
191 // CanFileTypeMagstat,
192 // CanFileTypeGbsstat,
193 // CanFileTypeFsstat
194 // };
195
196 enum LocoFunctionCs2Icon : uint8_t
197 {
198 LocoFunctionCs2IconNone = 0,
199 LocoFunctionCs2IconLight,
200 LocoFunctionCs2IconInteriorLight1,
201 LocoFunctionCs2IconBackLightForward,
202 LocoFunctionCs2IconHeadlightHighBeamForward,
203 LocoFunctionCs2IconSoundGeneral,
204 LocoFunctionCs2IconPanto12,
205 LocoFunctionCs2IconSmokeGenerator,
206 LocoFunctionCs2IconShuntingMode,
207 LocoFunctionCs2IconTelex12,
208 LocoFunctionCs2IconHorn1,
209 LocoFunctionCs2IconWhistle1,
210 LocoFunctionCs2IconWhistle2,
211 LocoFunctionCs2IconBell,
212 LocoFunctionCs2IconLeftRight,
213 LocoFunctionCs2IconUpDown1,
214 LocoFunctionCs2IconTurnLeft,
215 LocoFunctionCs2IconUpDown2,
216 LocoFunctionCs2IconInertia,
217 LocoFunctionCs2IconFan2,
218 LocoFunctionCs2IconBreak1,
219 LocoFunctionCs2IconGearBox,
220 LocoFunctionCs2IconGenerator,
221 LocoFunctionCs2IconRunning1,
222 LocoFunctionCs2IconEngine1,
223 LocoFunctionCs2IconStationAnnouncement1,
224 LocoFunctionCs2IconShovelCoal,
225 LocoFunctionCs2IconCloseDoor,
226 LocoFunctionCs2IconOpenDoor,
227 LocoFunctionCs2IconFan1,
228 LocoFunctionCs2IconFan,
229 LocoFunctionCs2IconFireBox,
230 LocoFunctionCs2IconInteriorLight2,
231 LocoFunctionCs2IconTableLight3,
232 LocoFunctionCs2IconTableLight2,
233 LocoFunctionCs2IconTableLight1,
234 LocoFunctionCs2IconShakingRust,
235 LocoFunctionCs2IconRailJoint,
236 LocoFunctionCs2IconLocomotiveNumberIndicator,
237 LocoFunctionCs2IconMusic1,
238 LocoFunctionCs2IconTrainDestinationIndicator,
239 LocoFunctionCs2IconCabLight2,
240 LocoFunctionCs2IconCabLight1,
241 LocoFunctionCs2IconCoupler,
242 LocoFunctionCs2IconBufferPush,
243 LocoFunctionCs2IconStationAnnouncement3,
244 LocoFunctionCs2IconCraneHook,
245 LocoFunctionCs2IconBlinkingLight,
246 LocoFunctionCs2IconCabLight12,
247 LocoFunctionCs2IconCompressedAir,
248 LocoFunctionCs2IconDefault,
249
250 LocoFunctionCs2IconTelex2 = 82,
251 LocoFunctionCs2IconTelex1,
252 LocoFunctionCs2IconPanto2,
253 LocoFunctionCs2IconPanto1,
254 LocoFunctionCs2IconHeadlightReverse,
255 LocoFunctionCs2IconHeadlightForward,
256 LocoFunctionCs2IconUp2,
257 LocoFunctionCs2IconFan3,
258 LocoFunctionCs2IconEngineLight,
259 LocoFunctionCs2IconSteamBlowOut,
260 LocoFunctionCs2IconSteamBlow,
261 LocoFunctionCs2IconCrane,
262 LocoFunctionCs2IconUp1,
263 LocoFunctionCs2IconDown,
264 LocoFunctionCs2IconLeft,
265 LocoFunctionCs2IconRight,
266 LocoFunctionCs2IconTurnRight,
267 LocoFunctionCs2IconMagnet,
268
269 LocoFunctionCs2IconPanto = 101,
270
271 LocoFunctionCs2IconRadio = 103,
272 LocoFunctionCs2IconStationAnnouncement2,
273 LocoFunctionCs2IconBackLightReverse,
274 LocoFunctionCs2IconAirPump,
275 LocoFunctionCs2IconSpeak,
276 LocoFunctionCs2IconEngine2,
277 LocoFunctionCs2IconNoSound,
278 LocoFunctionCs2IconStairsLight,
279 LocoFunctionCs2IconFillWater,
280 LocoFunctionCs2IconBreak2
281 };
282
283 typedef unsigned char CanPrio;
284 typedef unsigned char CanLength;
285 typedef uint32_t CanUid;
286 typedef uint16_t CanHash;
287 typedef uint16_t CanFileCrc;
288
289 void CreateCommandHeader(unsigned char* const buffer,
290 const CanCommand command,
291 const CanResponse response,
292 const CanLength length);
293
294 inline void ParseAddressProtocol(const unsigned char* const buffer,
295 Address& address,
296 Protocol& protocol,
297 LocoType& type)
298 {
299 Address input = ParseAddress(buffer);
300 ParseAddressProtocol(input, address, protocol, type);
301 }
302
303 static void ParseAddressProtocol(const Address input,
304 Address& address,
305 Protocol& protocol,
306 LocoType& type);
307
308 static inline CanPrio ParsePrio(const unsigned char* const buffer)
309 {
310 return buffer[0] >> 1;
311 }
312
313 static inline CanCommand ParseCommand(const unsigned char* const buffer)
314 {
315 return static_cast<CanCommand>((buffer[0] << 7) | (buffer[1] >> 1));
316 }
317
318 static inline CanSubCommand ParseSubCommand(const unsigned char* const buffer)
319 {
320 return static_cast<CanSubCommand>(buffer[9]);
321 }
322
323 static inline CanResponse ParseResponse(const unsigned char* const buffer)
324 {
325 return static_cast<CanResponse>(buffer[1] & 0x01);
326 }
327
328 static inline CanLength ParseLength(const unsigned char* const buffer)
329 {
330 return buffer[4];
331 }
332
333 static inline Address ParseAddress(const unsigned char* const buffer)
334 {
335 return static_cast<Address>(Utils::Integer::DataBigEndianToInt(buffer + 5));
336 }
337
338 static inline FeedbackPin ParseFeedbackPin(const unsigned char* const buffer)
339 {
340 FeedbackPin pin = static_cast<FeedbackPin>(Utils::Integer::DataBigEndianToInt(buffer + 5));
341 pin = (pin & 0x00000FFF) | ((pin & 0x00FF0000) >> 4);
342 return pin;
343 }
344
345 static inline CanHash ParseHash(const unsigned char* const buffer)
346 {
347 return Utils::Integer::DataBigEndianToShort(buffer + 2);
348 }
349
350 static inline CanUid ParseUid(const unsigned char* const buffer)
351 {
352 return Utils::Integer::DataBigEndianToInt(buffer + 5);
353 }
354
355 void ParseCommandSystem(const unsigned char* const buffer);
356 void ParseResponseLocoSpeed(const unsigned char* const buffer);
357 void ParseResponseLocoDirection(const unsigned char* const buffer);
358 void ParseResponseLocoFunction(const unsigned char* const buffer);
359 void ParseResponseAccessory(const unsigned char* const buffer);
360 void ParseCommandPing(const unsigned char* const buffer);
361 void ParseCommandRequestConfigData(const unsigned char* const buffer);
362 void SendCompressedFile(const std::string& dataPlain, const unsigned char* fileName = nullptr);
363 void ParseCommandConfigData(const unsigned char* const buffer);
364 void ParseCommandConfigDataFirst(const unsigned char* const buffer);
365 void ParseCommandConfigDataNext(const unsigned char* const buffer);
366 void ParseResponseS88Event(const unsigned char* const buffer);
367 void ParseResponseReadConfig(const unsigned char* const buffer);
368 void ParseResponsePing(const unsigned char* const buffer);
369
370 bool ParseCs2FileKeyValue(const std::string& line, std::string& key, std::string& value);
371 bool ParseCs2FileSubkeyValue(const std::string& line, std::string& key, std::string& value);
372 void ParseCs2FileLocomotiveFunction(std::deque<std::string>& lines, LocoCacheEntry& cacheEntry);
373 void ParseCs2FileLocomotiveTraktion(std::deque<std::string>& lines, LocoCacheEntry& cacheEntry);
374 void ParseCs2FileLocomotive(std::deque<std::string>& lines);
375 void ParseCs2FileLocomotivesSession(std::deque<std::string>& lines);
376 void ParseCs2FileLocomotivesVersion(std::deque<std::string>& lines);
377 void ParseCs2FileLocomotives(std::deque<std::string>& lines);
378 void ParseCs2File(std::deque<std::string>& lines);
379
380 static CanFileCrc CalcCrc(const unsigned char *data, const size_t length);
381
382 static CanHash CalcHash(const CanUid uid);
383 void GenerateUidHash();
384
385 void CreateLocalIDLoco(unsigned char* buffer, const Protocol protocol, const Address address) const;
386 void CreateLocalIDAccessory(unsigned char* buffer, const Protocol protocol, const Address address) const;
387
388 void Wait(const unsigned int duration) const;
389 void PingSender();
390
391 inline void SendInternal(const unsigned char* buffer)
392 {
393 logger->HexOut(buffer, 5 + ParseLength(buffer));
394 Send(buffer);
395 }
396 virtual void Send(const unsigned char* buffer) = 0;
397
398 inline void CleanUpCanFileData()
399 {
400 free(canFileData);
401 canFileData = nullptr;
402 canFileDataPointer = nullptr;
403 canFileDataSize = 0;
404 canFileCrc = 0;
405 canFileCrcSize = 0;
406 }
407 CanUid uid;
408 CanHash hash;
409 volatile bool hasCs2Main;
410 const bool isCs2Main;
411 std::thread receiverThread;
412 std::thread pingThread;
413
414 unsigned char* canFileData;
415 unsigned char* canFileDataPointer;
416 size_t canFileDataSize;
417 CanFileCrc canFileCrc;
418 size_t canFileCrcSize;
419
420 ControlID controlID;
421
422 static const uint8_t MaxNrOfCs2FunctionIcons = 128;
423 static const DataModel::LocoFunctionIcon LocoFunctionMapCs2ToRailControl[MaxNrOfCs2FunctionIcons];
424 static const MaerklinCANCommon::LocoFunctionCs2Icon LocoFunctionMapRailControlToCs2[DataModel::MaxLocoFunctionIcons];
425 };
426 }} // namespace