Debian Codegraph @

Hardware/CS2Udp.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 "Hardware/CS2Udp.h"
22 #include "Utils/Utils.h"
23
24 namespace Hardware
25 {
26 CS2Udp::CS2Udp(const HardwareParams* params)
27 : MaerklinCAN(params,
28 "Maerklin Central Station 2 (CS2) UDP / " + params->GetName() + " at IP " + params->GetArg1(),
29 params->GetName()),
30 senderConnection(HardwareInterface::logger, params->GetArg1(), CS2SenderPort),
31 receiverConnection(HardwareInterface::logger, "0.0.0.0", CS2ReceiverPort)
32 {
33 HardwareInterface::logger->Info(Languages::TextStarting, GetFullName());
34
35 if (senderConnection.IsConnected())
36 {
37 HardwareInterface::logger->Info(Languages::TextSenderSocketCreated);
38 }
39 else
40 {
41 HardwareInterface::logger->Error(Languages::TextUnableToCreateUdpSocketForSendingData);
42 }
43
44 Init();
45 }
46
47 CS2Udp::~CS2Udp()
48 {
49 receiverConnection.Terminate();
50 HardwareInterface::logger->Info(Languages::TextTerminatingSenderSocket);
51 }
52
53 void CS2Udp::Send(const unsigned char* buffer)
54 {
55 if (senderConnection.Send(buffer, CANCommandBufferLength) == -1)
56 {
57 HardwareInterface::logger->Error(Languages::TextUnableToSendDataToControl);
58 }
59 }
60
61 void CS2Udp::Receiver()
62 {
63 if (!receiverConnection.IsConnected())
64 {
65 HardwareInterface::logger->Error(Languages::TextUnableToCreateUdpSocketForReceivingData);
66 return;
67 }
68
69 bool ret = receiverConnection.Bind();
70 if (!ret)
71 {
72 HardwareInterface::logger->Error(Languages::TextUnableToBindUdpSocket);
73 return;
74 }
75 unsigned char buffer[CANCommandBufferLength];
76 while(run)
77 {
78 ssize_t datalen = receiverConnection.Receive(buffer, sizeof(buffer));
79 if (!run)
80 {
81 break;
82 }
83
84 if (datalen < 0)
85 {
86 HardwareInterface::logger->Error(Languages::TextUnableToReceiveData);
87 break;
88 }
89
90 if (datalen == 0)
91 {
92 // no data received
93 continue;
94 }
95
96 if (datalen != 13)
97 {
98 HardwareInterface::logger->Error(Languages::TextInvalidDataReceived);
99 continue;
100 }
101 Parse(buffer);
102 }
103 receiverConnection.Terminate();
104 }
105 } // namespace