Debian Codegraph @

Hardware/CS2Tcp.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/CS2Tcp.h"
22 #include "Utils/Utils.h"
23
24 namespace Hardware
25 {
26 CS2Tcp::CS2Tcp(const HardwareParams* params)
27 : MaerklinCAN(params,
28 "Maerklin Central Station 2 (CS2) TCP / " + params->GetName() + " at IP " + params->GetArg1(),
29 params->GetName()),
30 connection(Network::TcpClient::GetTcpClientConnection(HardwareInterface::logger, params->GetArg1(), CS2Port))
31 {
32 HardwareInterface::logger->Info(Languages::TextStarting, GetFullName());
33
34 if (connection.IsConnected() == false)
35 {
36 HardwareInterface::logger->Error(Languages::TextUnableToCreateTcpSocket, params->GetArg1(), CS2Port);
37 }
38 Init();
39 }
40
41 void CS2Tcp::Send(const unsigned char* buffer)
42 {
43 if (connection.Send(buffer, CANCommandBufferLength) == -1)
44 {
45 HardwareInterface::logger->Error(Languages::TextUnableToSendDataToControl);
46 }
47 }
48
49 void CS2Tcp::Receiver()
50 {
51 if (!connection.IsConnected())
52 {
53 HardwareInterface::logger->Error(Languages::TextUnableToReceiveData);
54 return;
55 }
56
57 unsigned char buffer[CANCommandBufferLength];
58 while(run)
59 {
60 ssize_t datalen = connection.ReceiveExact(buffer, sizeof(buffer));
61 if (!run)
62 {
63 break;
64 }
65
66 if (datalen == -1)
67 {
68 if (errno == ETIMEDOUT)
69 {
70 continue;
71 }
72 HardwareInterface::logger->Error(Languages::TextErrorReadingData, strerror(errno));
73
74 break;
75 }
76
77 if (datalen == 0)
78 {
79 // no data received
80 continue;
81 }
82
83 if (datalen != 13)
84 {
85 HardwareInterface::logger->Error(Languages::TextErrorReadingData, strerror(errno));
86 continue;
87 }
88 Parse(buffer);
89 }
90 connection.Terminate();
91 }
92 } // namespace