Debian Codegraph @

Hardware/Protocols/P50x.cpp

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 #include <queue>
22
23 #include "Hardware/Protocols/P50x.h"
24 #include "Languages.h"
25 #include "Utils/Integer.h"
26
27 namespace Hardware
28 {
29 namespace Protocols
30 {
31 P50x::P50x(const HardwareParams* const params,
32 const std::string& controlName,
33 const P50xType type)
34 : HardwareInterface(params->GetManager(),
35 params->GetControlID(),
36 controlName,
37 params->GetName()),
38 s88Modules(0),
39 params(params),
40 type(type),
41 run(false)
42 {
43 logger->Info(Languages::TextStarting, GetFullName());
44 }
45
46 P50x::~P50x()
47 {
48 if (!run)
49 {
50 return;
51 }
52 run = false;
53 checkEventsThread.join();
54 }
55
56 void P50x::Init()
57 {
58 switch (type)
59 {
60 case TypeOpenDcc:
61 InitOpenDcc();
62 break;
63
64 case TypeUhlenbrock:
65 case TypeTams:
66 InitUhlenbrockTams();
67 break;
68 }
69 checkEventsThread = std::thread(&Hardware::Protocols::P50x::CheckEventsWorker, this);
70 }
71
72 void P50x::InitOpenDcc()
73 {
74 static const unsigned char MaxS88Modules = 128;
75
76 SendP50XOnly();
77 bool ok = SendNop();
78 if (!ok)
79 {
80 logger->Error(Languages::TextControlDoesNotAnswer);
81 return;
82 }
83
84 unsigned char s88Modules1 = Utils::Integer::StringToInteger(params->GetArg2(), 0);
85 unsigned char s88Modules2 = Utils::Integer::StringToInteger(params->GetArg3(), 0);
86 unsigned char s88Modules3 = Utils::Integer::StringToInteger(params->GetArg4(), 0);
87 s88Modules = s88Modules1 + s88Modules2 + s88Modules3;
88
89 if (s88Modules > MaxS88Modules)
90 {
91 logger->Error(Languages::TextTooManyS88Modules, s88Modules, MaxS88Modules);
92 return;
93 }
94
95 if (s88Modules == 0)
96 {
97 logger->Info(Languages::TextNoS88Modules);
98 return;
99 }
100
101 logger->Info(Languages::TextHsi88Configured, s88Modules, s88Modules1, s88Modules2, s88Modules3);
102 bool restart = false;
103 unsigned char modules = SendXP88Get(0);
104 if (modules != s88Modules)
105 {
106 logger->Info(Languages::TextNrOfS88Modules, s88Modules);
107 SendXP88Set(0, s88Modules);
108 restart = true;
109 }
110
111 modules = SendXP88Get(1);
112 if (modules != s88Modules1)
113 {
114 logger->Info(Languages::TextNrOfS88ModulesOnBus, s88Modules1, 1);
115 SendXP88Set(1, s88Modules1);
116 restart = true;
117 }
118
119 modules = SendXP88Get(2);
120 if (modules != s88Modules2)
121 {
122 logger->Info(Languages::TextNrOfS88ModulesOnBus, s88Modules2, 2);
123 SendXP88Set(2, s88Modules2);
124 restart = true;
125 }
126
127 modules = SendXP88Get(3);
128 if (modules != s88Modules3)
129 {
130 logger->Info(Languages::TextNrOfS88ModulesOnBus, s88Modules3, 3);
131 SendXP88Set(3, s88Modules3);
132 restart = true;
133 }
134
135 if (restart)
136 {
137 SendXP88Set(4, 0);
138 SendRestart();
139 Utils::Utils::SleepForMilliseconds(100);
140 SendP50XOnly();
141 ok = SendNop();
142 if (!ok)
143 {
144 logger->Error(Languages::TextControlDoesNotAnswer);
145 return;
146 }
147 }
148 }
149
150 void P50x::InitUhlenbrockTams()
151 {
152 static const unsigned char MaxS88Modules = 104;
153 SendP50XOnly();
154 const bool ok = SendNop();
155 if (!ok)
156 {
157 logger->Error(Languages::TextControlDoesNotAnswer);
158 return;
159 }
160
161 s88Modules = Utils::Integer::StringToInteger(params->GetArg2(), 0);
162
163 if (s88Modules > MaxS88Modules)
164 {
165 logger->Error(Languages::TextTooManyS88Modules, s88Modules, MaxS88Modules);
166 return;
167 }
168
169 if (s88Modules == 0)
170 {
171 logger->Info(Languages::TextNoS88Modules);
172 return;
173 }
174
175 logger->Info(Languages::TextNrOfS88Modules, s88Modules);
176 unsigned char modules = SendXP88Get(0);
177 if (modules != s88Modules)
178 {
179 SendXP88Set(0, s88Modules);
180 }
181 }
182
183 void P50x::Booster(const BoosterState status)
184 {
185 if (status)
186 {
187 logger->Info(Languages::TextTurningBoosterOn);
188 SendPowerOn();
189 }
190 else
191 {
192 logger->Info(Languages::TextTurningBoosterOff);
193 SendPowerOff();
194 }
195 }
196
197 void P50x::LocoSpeed(__attribute__((unused)) const Protocol protocol, const Address address,
198 const Speed speed)
199 {
200 if (!CheckLocoAddress(address))
201 {
202 return;
203 }
204
205 cache.SetSpeed(address, speed);
206 SendXLok(address);
207 }
208
209 void P50x::LocoOrientation(__attribute__((unused)) const Protocol protocol, const Address address,
210 const Orientation orientation)
211 {
212 if (!CheckLocoAddress(address))
213 {
214 return;
215 }
216
217 cache.SetOrientation(address, orientation);
218 SendXLok(address);
219 }
220
221 void P50x::LocoFunction(__attribute__((unused)) const Protocol protocol,
222 const Address address,
223 const DataModel::LocoFunctionNr function,
224 const DataModel::LocoFunctionState on)
225 {
226 if (!CheckLocoAddress(address))
227 {
228 return;
229 }
230
231 if (function > MaxLocoFunctions)
232 {
233 return;
234 }
235 cache.SetFunction(address, function, on);
236 std::lock_guard < std::mutex > guard(communicationLock);
237 if (function == 0)
238 {
239 SendXLok(address);
240 return;
241 }
242
243 if (function <= 8)
244 {
245 SendXFunc(address);
246 return;
247 }
248
249 if (function <= 16)
250 {
251 SendXFunc2(address);
252 return;
253 }
254
255 SendXFunc34(address);
256 }
257
258 void P50x::LocoSpeedOrientationFunctions(__attribute__((unused)) const Protocol protocol,
259 const Address address,
260 const Speed speed,
261 const Orientation orientation,
262 std::vector<DataModel::LocoFunctionEntry>& functions)
263 {
264 if (!CheckLocoAddress(address))
265 {
266 return;
267 }
268
269 cache.SetSpeed(address, speed);
270 cache.SetOrientation(address, orientation);
271 unsigned char nrFunctions = functions.size();
272 for (const DataModel::LocoFunctionEntry& function : functions)
273 {
274 cache.SetFunction(address, function.nr, function.state);
275 }
276 std::lock_guard < std::mutex > guard(communicationLock);
277 SendXLok(address);
278 if (nrFunctions > 1)
279 {
280 SendXFunc(address);
281 if (nrFunctions > 8)
282 {
283 SendXFunc2(address);
284 if (nrFunctions > 16)
285 {
286 SendXFunc34(address);
287 }
288 }
289 }
290 }
291
292 bool P50x::SendXLok(const Address address) const
293 {
294 P50xCacheEntry entry = cache.GetData(address);
295 logger->Info(Languages::TextSettingSpeedOrientationLight, address, entry.speed,
296 Languages::GetLeftRight(static_cast<Orientation>((entry.orientationF0 >> 5) & 0x01)),
297 Languages::GetOnOff((entry.orientationF0 >> 4) & 0x01));
298 const unsigned char addressLSB = (address & 0xFF);
299 const unsigned char addressMSB = (address >> 8);
300 const unsigned char data[5] = { XLok, addressLSB, addressMSB, entry.speed, entry.orientationF0 };
301
302 SendInternal(data, sizeof(data));
303 unsigned char input;
304 bool ret = ReceiveExactInternal(&input, 1);
305 if (ret != 1)
306 {
307 logger->Warning(Languages::TextControlDoesNotAnswer);
308 return false;
309 }
310 switch (input)
311 {
312 case OK:
313 return true;
314
315 case XBADPRM:
316 logger->Warning(Languages::TextControlReturnedBadParameter);
317 return false;
318
319 case XLKHALT:
320 logger->Warning(Languages::TextControlReturnedOnHalt);
321 return false;
322
323 case XLKPOFF:
324 logger->Warning(Languages::TextControlReturnedPowerOff);
325 return false;
326
327 default:
328 logger->Warning(Languages::TextControlReturnedUnknownErrorCode, static_cast<int>(input));
329 return false;
330 }
331 }
332
333 bool P50x::SendXFunc(const Address address) const
334 {
335 P50xCacheEntry entry = cache.GetData(address);
336 logger->Info(Languages::TextSettingFunctions1_8, address, entry.function[0]);
337 const unsigned char addressLSB = (address & 0xFF);
338 const unsigned char addressMSB = (address >> 8);
339 const unsigned char data[4] = { XFunc, addressLSB, addressMSB, entry.function[0] };
340 SendInternal(data, sizeof(data));
341 return ReceiveFunctionCommandAnswer();
342 }
343
344 bool P50x::SendXFunc2(const Address address) const
345 {
346 P50xCacheEntry entry = cache.GetData(address);
347 logger->Info(Languages::TextSettingFunctions9_16, address, entry.function[1]);
348 const unsigned char addressLSB = (address & 0xFF);
349 const unsigned char addressMSB = (address >> 8);
350 const unsigned char data[4] = { XFunc2, addressLSB, addressMSB, entry.function[1] };
351 SendInternal(data, sizeof(data));
352 return ReceiveFunctionCommandAnswer();
353 }
354
355 bool P50x::SendXFunc34(const Address address) const
356 {
357 P50xCacheEntry entry = cache.GetData(address);
358 logger->Info(Languages::TextSettingFunctions17_28, address, entry.function[2], entry.function[3]);
359 const unsigned char addressLSB = (address & 0xFF);
360 const unsigned char addressMSB = (address >> 8);
361 const unsigned char data[5] = { XFunc34, addressLSB, addressMSB, entry.function[2], entry.function[3] };
362 SendInternal(data, sizeof(data));
363 return ReceiveFunctionCommandAnswer();
364 }
365
366 bool P50x::ReceiveFunctionCommandAnswer() const
367 {
368 unsigned char input;
369 bool ret = ReceiveExactInternal(&input, 1);
370 if (ret != 1)
371 {
372 logger->Warning(Languages::TextControlDoesNotAnswer);
373 return false;
374 }
375 switch (input)
376 {
377 case OK:
378 return true;
379
380 case XBADPRM:
381 logger->Warning(Languages::TextControlReturnedBadParameter);
382 return false;
383
384 case XNOSLOT:
385 logger->Warning(Languages::TextControlReturnedQueueFull);
386 return false;
387
388 default:
389 logger->Warning(Languages::TextControlReturnedUnknownErrorCode, static_cast<int>(input));
390 return false;
391 }
392 }
393
394 void P50x::Accessory(__attribute__((unused)) const Protocol protocol,
395 const Address address,
396 const DataModel::AccessoryState state,
397 const bool on,
398 __attribute__((unused)) const DataModel::AccessoryPulseDuration duration)
399 {
400 if (!CheckAccessoryAddress(address))
401 {
402 return;
403 }
404 logger->Info(Languages::TextSettingAccessoryWithProtocol, ProtocolDCC, address, state, on);
405 const unsigned char addressLSB = (address & 0xFF);
406 const unsigned char addressMSB = (address >> 8);
407 const unsigned char statusBits = ((state == DataModel::AccessoryStateOn) << 7) | (on << 6);
408 const unsigned char addressStatus = addressMSB | statusBits;
409 const unsigned char data[3] = { XTrnt, addressLSB, addressStatus };
410 std::lock_guard < std::mutex > guard(communicationLock);
411 SendInternal(data, sizeof(data));
412 unsigned char input;
413 bool ret = ReceiveExactInternal(&input, 1);
414 if (ret != 1)
415 {
416 logger->Warning(Languages::TextControlDoesNotAnswer);
417 return;
418 }
419 switch (input)
420 {
421 case OK:
422 return;
423
424 case XBADPRM:
425 logger->Warning(Languages::TextControlReturnedBadParameter);
426 return;
427
428 case XLOWTSP:
429 logger->Warning(Languages::TextControlReturnedQueueNearlyFull);
430 return;
431
432 default:
433 logger->Warning(Languages::TextControlReturnedUnknownErrorCode, static_cast<int>(input));
434 return;
435 }
436 }
437
438 bool P50x::SendP50XOnly() const
439 {
440 unsigned char data[6] = { 'X', 'Z', 'z', 'A', '1', 0x0D };
441 SendInternal(data, sizeof(data));
442 const unsigned char dataLengthRead = 34;
443 unsigned char input[dataLengthRead];
444 ReceiveExactInternal(input, dataLengthRead);
445 return true;
446 }
447
448 bool P50x::SendOneByteCommand(const unsigned char data) const
449 {
450 std::lock_guard < std::mutex > guard(communicationLock);
451 SendInternal(data);
452 unsigned char input;
453 int ret = ReceiveInternal(&input, 1);
454 return ret > 0 && input == OK;
455 }
456
457 bool P50x::SendRestart() const
458 {
459 unsigned char data[3] = { '@', '@', 0x0D };
460 logger->Info(Languages::TextRestarting);
461 std::lock_guard < std::mutex > guard(communicationLock);
462 SendInternal(data, sizeof(data));
463 return true;
464 }
465
466 unsigned char P50x::SendXP88Get(unsigned char param) const
467 {
468 unsigned char data[2] = { XP88Get, param };
469 std::lock_guard < std::mutex > guard(communicationLock);
470 SendInternal(data, sizeof(data));
471 unsigned char input;
472 size_t ret = ReceiveExactInternal(&input, 1);
473 if (ret == 0 || input != OK)
474 {
475 return 0xFF;
476 }
477 ret = ReceiveExactInternal(&input, 1);
478 if (ret == 0)
479 {
480 return 0xFF;
481 }
482 return input;
483 }
484
485 bool P50x::SendXP88Set(unsigned char param, unsigned char value) const
486 {
487 unsigned char data[3] = { XP88Set, param, value };
488 std::lock_guard < std::mutex > guard(communicationLock);
489 SendInternal(data, sizeof(data));
490 unsigned char input;
491 size_t ret = ReceiveExactInternal(&input, 1);
492 if (ret == 0)
493 {
494 return false;
495 }
496 return (input == OK);
497 }
498
499 void P50x::CheckSensorData(const unsigned char module, const unsigned char data) const
500 {
501 unsigned char diff = s88Memory[module] ^ data;
502 s88Memory[module] = data;
503 FeedbackPin pinOverAll = module;
504 pinOverAll <<= 3;
505 for (unsigned char pinOnModule = 1; pinOnModule <= 8; ++pinOnModule)
506 {
507 if ((diff >> (8 - pinOnModule)) & 0x01)
508 {
509 DataModel::Feedback::FeedbackState state = static_cast<DataModel::Feedback::FeedbackState>((data
510 >> (8 - pinOnModule)) & 0x01);
511 logger->Info(Languages::TextFeedbackChange, pinOnModule, module,
512 Languages::GetText(state ? Languages::TextOn : Languages::TextOff));
513 manager->FeedbackState(controlID, pinOverAll + pinOnModule, state);
514 }
515 }
516 }
517
518 void P50x::SendXEvtSen() const
519 {
520 std::lock_guard < std::mutex > guard(communicationLock);
521 unsigned char data[1] = { XEvtSen };
522 SendInternal(data, sizeof(data));
523 while (true)
524 {
525 unsigned char module;
526 ssize_t ret = ReceiveExactInternal(&module, 1);
527 if (ret != 1 || module == 0)
528 {
529 return;
530 }
531
532 --module;
533 module <<= 1;
534
535 unsigned char input[2];
536 ret = ReceiveExactInternal(input, sizeof(input));
537 if (ret < 2)
538 {
539 return;
540 }
541
542 if (s88Memory[module] != input[0])
543 {
544 CheckSensorData(module, input[0]);
545 }
546
547 ++module;
548
549 if (s88Memory[module] != input[1])
550 {
551 CheckSensorData(module, input[1]);
552 }
553 }
554 }
555
556 void P50x::SendXEvtLok() const
557 {
558 std::queue<struct LocoCommand> commandQueue;
559 std::queue<struct LocoFunction> functionQueue;
560 {
561 std::lock_guard < std::mutex > guard(communicationLock);
562 unsigned char data[1] = { XEvtLok };
563 SendInternal(data, sizeof(data));
564 while (true)
565 {
566 unsigned char input[5];
567 ssize_t ret = ReceiveExactInternal(input, 1);
568 if (ret < 1 || input[0] == 0x80)
569 {
570 break;
571 }
572
573 ret = ReceiveExactInternal(input + 1, sizeof(input) - 1);
574 if (ret < 1)
575 {
576 break;
577 }
578
579 struct LocoCommand command;
580 command.address = (input[3] & 0x3F);
581 command.address <<= 8;
582 command.address += input[2];
583
584 command.speed = input[0];
585 if (command.speed == 1)
586 {
587 command.speed = 0;
588 }
589 else if (command.speed > 1)
590 {
591 --command.speed;
592 }
593 command.speed <<= 3;
594
595 command.orientation = static_cast<Orientation>(input[3] >> 7);
596
597 commandQueue.push(command);
598
599 struct LocoFunction function;
600 function.address = command.address;
601
602 uint16_t functions = input[1];
603 functions <<= 1;
604 functions += (input[3] >> 6) & 0x01;
605
606 for (DataModel::LocoFunctionNr nr = 0; nr <= 8; ++nr)
607 {
608 function.nr = nr;
609 function.state = static_cast<DataModel::LocoFunctionState>((functions >> nr) & 0x01);
610 functionQueue.push(function);
611 }
612 }
613 }
614
615 while (commandQueue.size())
616 {
617 struct LocoCommand command = commandQueue.front();
618 commandQueue.pop();
619 manager->LocoSpeed(ControlTypeHardware, controlID, ProtocolServer, command.address, command.speed);
620 manager->LocoOrientation(ControlTypeHardware, controlID, ProtocolServer, command.address,
621 command.orientation);
622 }
623
624 while (functionQueue.size())
625 {
626 struct LocoFunction function = functionQueue.front();
627 functionQueue.pop();
628 manager->LocoFunctionState(ControlTypeHardware, controlID, ProtocolServer, function.address,
629 function.nr, function.state);
630 }
631 }
632
633 void P50x::SendXEvtTrn() const
634 {
635 std::queue<struct TurnoutCommand> commandQueue;
636 {
637 std::lock_guard < std::mutex > guard(communicationLock);
638 unsigned char data[1] = { XEvtTrnt };
639 SendInternal(data, sizeof(data));
640 unsigned char number;
641 ssize_t ret = ReceiveExactInternal(&number, 1);
642 if (ret < 1)
643 {
644 return;
645 }
646 while (number)
647 {
648 unsigned char input[2];
649 ssize_t ret = ReceiveExactInternal(input, sizeof(input));
650 if (ret != sizeof(input))
651 {
652 break;
653 }
654
655 --number;
656
657 bool on = static_cast<bool>((input[1] >> 6) & 0x01);
658 if (!on)
659 {
660 continue;
661 }
662
663 struct TurnoutCommand command;
664 command.address = (input[1] & 0x07);
665 command.address <<= 8;
666 command.address += input[0];
667
668 command.state = static_cast<DataModel::AccessoryState>((input[1] >> 7) & 0x01);
669
670 commandQueue.push(command);
671 }
672 }
673
674 while (commandQueue.size())
675 {
676 struct TurnoutCommand command = commandQueue.front();
677 commandQueue.pop();
678 manager->AccessoryBaseState(ControlTypeHardware, controlID, ProtocolServer, command.address, command.state);
679 }
680 }
681
682 void P50x::SendXStatus() const
683 {
684 std::queue < BoosterState > queue;
685 {
686 std::lock_guard < std::mutex > guard(communicationLock);
687 unsigned char data[1] = { XStatus };
688 SendInternal(data, sizeof(data));
689 unsigned char byte = 1;
690 while (true)
691 {
692 unsigned char input;
693 ssize_t ret = ReceiveExactInternal(&input, sizeof(input));
694 if (ret < 1)
695 {
696 return;
697 }
698
699 if (byte == 1)
700 {
701 queue.push(static_cast<BoosterState>((input >> 3) & 0x01));
702 }
703
704 if ((input & 0x80) == 0x00)
705 {
706 break;
707 }
708 ++byte;
709 }
710 }
711
712 while (queue.size())
713 {
714 manager->Booster(ControlTypeHardware, queue.front());
715 queue.pop();
716 }
717 }
718
719 void P50x::SendXEvent() const
720 {
721 unsigned char input;
722 bool statusEvent = false;
723 {
724 std::lock_guard < std::mutex > guard(communicationLock);
725 unsigned char data[1] = { XEvent };
726 SendInternal(data, sizeof(data));
727 size_t ret = ReceiveExactInternal(&input, 1);
728 if (ret != 1)
729 {
730 return;
731 }
732
733 bool moreData = (input >> 7) & 0x01;
734 unsigned char byte = 1;
735 while (true)
736 {
737 if (!moreData)
738 {
739 break;
740 }
741 unsigned char input2;
742 size_t ret = ReceiveExactInternal(&input2, 1);
743 if (ret != 1)
744 {
745 break;
746 }
747 if (byte == 1)
748 {
749 if ((input2 >> 6) & 0x01)
750 {
751 statusEvent = true;
752 }
753 }
754 ++byte;
755 moreData = (input2 >> 7) & 0x01;
756 }
757 }
758
759 bool sensorEvent = (input >> 2) & 0x01;
760 if (sensorEvent)
761 {
762 SendXEvtSen();
763 }
764
765 bool locoEvent = input & 0x01;
766 if (locoEvent)
767 {
768 SendXEvtLok();
769 }
770
771 bool powerEvent = (input >> 3) & 0x01;
772 if (powerEvent)
773 {
774 manager->Booster(ControlTypeHardware, BoosterStateStop);
775 }
776
777 bool switchEvent = (input >> 5) & 0x01;
778 if (switchEvent)
779 {
780 SendXEvtTrn();
781 }
782
783 if (statusEvent)
784 {
785 SendXStatus();
786 }
787 }
788
789 void P50x::CheckEventsWorker()
790 {
791 Utils::Utils::SetThreadName (GetShortName());
792 run= true;
793 while (run)
794 {
795 SendXEvent();
796 Utils::Utils::SleepForMilliseconds(50);
797 }
798 }
799 } // namespace
800 }// namespace