File: Z21Client.h

package info (click to toggle)
railcontrol 24%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,680 kB
  • sloc: cpp: 40,834; javascript: 2,509; makefile: 144; php: 97; sh: 60
file content (181 lines) | stat: -rw-r--r-- 5,593 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
RailControl - Model Railway Control Software

Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org

RailControl is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

RailControl is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/

#pragma once

#include <ctime>

#include "Hardware/Protocols/Z21DataTypes.h"
#include "Network/UdpClient.h"
#include "Utils/Integer.h"

namespace Z21Enums = Hardware::Protocols::Z21Enums;

namespace Server { namespace Z21
{
	class Z21Server;

	class Z21Client : protected Network::UdpClient
	{
		public:
			Z21Client() = delete;
			Z21Client(const Z21Client&) = delete;
			Z21Client& operator=(const Z21Client&) = delete;

			Z21Client(const unsigned int id,
				Manager& manager,
				const int serverSocket,
				const struct sockaddr_storage* clientAddress);

			~Z21Client();

			void Work(const unsigned char* buffer, const size_t size) override;

			size_t ParseData(const unsigned char* buffer, const size_t bufferLength);

			void ParseXHeader(const unsigned char* buffer);

			void ParseDB0(const unsigned char* buffer);

			void ParseLocoDrive(const unsigned char* buffer);

			inline uint16_t ParseLocoAddress(const unsigned char* buffer)
			{
				return ((buffer[0] & 0x3F) << 8) + buffer[1];
			}

			inline uint16_t ParseAccessoryAddress(const unsigned char* buffer)
			{
				return ((buffer[0] << 8) + buffer[1]) + 1; // + 1 because Z21 protocol is 0 based
			}

			inline void SendPowerOff()
			{
				const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x61, 0x00, 0x61 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendPowerOn()
			{
				const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x61, 0x01, 0x60 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			void SendLocoInfo(const DataModel::LocoBase* const locoBase);

			void SendTurnoutInfo(const DataModel::AccessoryBase* const accessoryBase);

		private:
			inline void SendSerialNumber()
			{
				const unsigned char sendBuffer[8] = { 0x08, 0x00, 0x10, 0x00, 0x12, 0x34, 0x56, 0x78 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendCode()
			{
				const unsigned char sendBuffer[5] = { 0x05, 0x00, 0x18, 0x00, 0x00 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendHardwareInfo()
			{
				const unsigned char sendBuffer[12] = { 0x0C, 0x00, 0x1A, 0x00, 0x01, 0x02, 0x00, 0x00, 0x42, 0x01, 0x00, 0x00 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendSystemStatusChanged()
			{
				const BoosterState booster = manager.Booster();
				const unsigned char centralState = (!booster) << 1;
				const unsigned char sendBuffer[20] = { 0x14, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x4E, 0x50, 0x46, centralState, 0x00, 0x00, 0x71 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendBcStopped()
			{
				const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x81, 0x00, 0x81 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendFirmwareVersion()
			{
				const unsigned char sendBuffer[9] = { 0x09, 0x00, 0x40, 0x00, 0xF3, 0x0A, 0x01, 0x43, 0xBB };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendUnknownCommand()
			{
				const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x61, 0x82, 0xE3 };
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendStatusChanged()
			{
				const BoosterState booster = manager.Booster();
				const unsigned char centralState = (!booster) << 1;
				unsigned char sendBuffer[8] = { 0x08, 0x00, 0x40, 0x00, 0x62, 0x22, centralState };
				sendBuffer[sizeof(sendBuffer) - 1] = Utils::Utils::CalcXORCheckSum(sendBuffer + 4, sizeof(sendBuffer) - 5);
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendVersion()
			{
				unsigned char sendBuffer[9] = { 0x09, 0x00, 0x40, 0x00, 0x63, 0x21, 0x40, 0x12 };
				sendBuffer[sizeof(sendBuffer) - 1] = Utils::Utils::CalcXORCheckSum(sendBuffer + 4, sizeof(sendBuffer) - 5);
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendBroadcastFlags()
			{
				unsigned char sendBuffer[8] = { 0x08, 0x00, 0x51, 0x00 };
				Utils::Integer::IntToDataLittleEndian(broadCastFlags, sendBuffer + 4);
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendLocoMode(const uint16_t address)
			{
				unsigned char sendBuffer[7] = { 0x07, 0x00, 0x60, 0x00 };
				Utils::Integer::ShortToDataBigEndian(address, sendBuffer + 4);
				sendBuffer[6] = 0x00; // we always use DCC
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void SendTurnoutMode(const uint16_t address)
			{
				unsigned char sendBuffer[7] = { 0x07, 0x00, 0x70, 0x00 };
				Utils::Integer::ShortToDataBigEndian(address, sendBuffer + 4);
				sendBuffer[6] = 0x00; // we always use DCC
				Send(sendBuffer, sizeof(sendBuffer));
			}

			inline void Send(const unsigned char* buffer, const size_t size)
			{
				logger->HexOut(buffer, size);
				UdpClient::Send(buffer, size);
			}

			Logger::Logger* logger;

			Manager& manager;
			Z21Enums::BroadCastFlags broadCastFlags;
	};
}} // namespace Server::Z21