| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <ctime> |
| 24 |
|
| 25 |
#include "Hardware/Protocols/Z21DataTypes.h" |
| 26 |
#include "Network/UdpClient.h" |
| 27 |
#include "Utils/Integer.h" |
| 28 |
|
| 29 |
namespace Z21Enums = Hardware::Protocols::Z21Enums; |
| 30 |
|
| 31 |
namespace Server { namespace Z21 |
| 32 |
{ |
| 33 |
class Z21Server; |
| 34 |
|
| 35 |
class Z21Client : protected Network::UdpClient |
| 36 |
{ |
| 37 |
public: |
| 38 |
Z21Client() = delete; |
| 39 |
Z21Client(const Z21Client&) = delete; |
| 40 |
Z21Client& operator=(const Z21Client&) = delete; |
| 41 |
|
| 42 |
Z21Client(const unsigned int id, |
| 43 |
Manager& manager, |
| 44 |
const int serverSocket, |
| 45 |
const struct sockaddr_storage* clientAddress); |
| 46 |
|
| 47 |
~Z21Client(); |
| 48 |
|
| 49 |
void Work(const unsigned char* buffer, const size_t size) override; |
| 50 |
|
| 51 |
size_t ParseData(const unsigned char* buffer, const size_t bufferLength); |
| 52 |
|
| 53 |
void ParseXHeader(const unsigned char* buffer); |
| 54 |
|
| 55 |
void ParseDB0(const unsigned char* buffer); |
| 56 |
|
| 57 |
void ParseLocoDrive(const unsigned char* buffer); |
| 58 |
|
| 59 |
inline uint16_t ParseLocoAddress(const unsigned char* buffer) |
| 60 |
{ |
| 61 |
return ((buffer[0] & 0x3F) << 8) + buffer[1]; |
| 62 |
} |
| 63 |
|
| 64 |
inline uint16_t ParseAccessoryAddress(const unsigned char* buffer) |
| 65 |
{ |
| 66 |
return ((buffer[0] << 8) + buffer[1]) + 1; |
| 67 |
} |
| 68 |
|
| 69 |
inline void SendPowerOff() |
| 70 |
{ |
| 71 |
const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x61, 0x00, 0x61 }; |
| 72 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 73 |
} |
| 74 |
|
| 75 |
inline void SendPowerOn() |
| 76 |
{ |
| 77 |
const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x61, 0x01, 0x60 }; |
| 78 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 79 |
} |
| 80 |
|
| 81 |
void SendLocoInfo(const DataModel::LocoBase* const locoBase); |
| 82 |
|
| 83 |
void SendTurnoutInfo(const DataModel::AccessoryBase* const accessoryBase); |
| 84 |
|
| 85 |
private: |
| 86 |
inline void SendSerialNumber() |
| 87 |
{ |
| 88 |
const unsigned char sendBuffer[8] = { 0x08, 0x00, 0x10, 0x00, 0x12, 0x34, 0x56, 0x78 }; |
| 89 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 90 |
} |
| 91 |
|
| 92 |
inline void SendCode() |
| 93 |
{ |
| 94 |
const unsigned char sendBuffer[5] = { 0x05, 0x00, 0x18, 0x00, 0x00 }; |
| 95 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 96 |
} |
| 97 |
|
| 98 |
inline void SendHardwareInfo() |
| 99 |
{ |
| 100 |
const unsigned char sendBuffer[12] = { 0x0C, 0x00, 0x1A, 0x00, 0x01, 0x02, 0x00, 0x00, 0x42, 0x01, 0x00, 0x00 }; |
| 101 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 102 |
} |
| 103 |
|
| 104 |
inline void SendSystemStatusChanged() |
| 105 |
{ |
| 106 |
const BoosterState booster = manager.Booster(); |
| 107 |
const unsigned char centralState = (!booster) << 1; |
| 108 |
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 }; |
| 109 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 110 |
} |
| 111 |
|
| 112 |
inline void SendBcStopped() |
| 113 |
{ |
| 114 |
const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x81, 0x00, 0x81 }; |
| 115 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 116 |
} |
| 117 |
|
| 118 |
inline void SendFirmwareVersion() |
| 119 |
{ |
| 120 |
const unsigned char sendBuffer[9] = { 0x09, 0x00, 0x40, 0x00, 0xF3, 0x0A, 0x01, 0x43, 0xBB }; |
| 121 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 122 |
} |
| 123 |
|
| 124 |
inline void SendUnknownCommand() |
| 125 |
{ |
| 126 |
const unsigned char sendBuffer[7] = { 0x07, 0x00, 0x40, 0x00, 0x61, 0x82, 0xE3 }; |
| 127 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 128 |
} |
| 129 |
|
| 130 |
inline void SendStatusChanged() |
| 131 |
{ |
| 132 |
const BoosterState booster = manager.Booster(); |
| 133 |
const unsigned char centralState = (!booster) << 1; |
| 134 |
unsigned char sendBuffer[8] = { 0x08, 0x00, 0x40, 0x00, 0x62, 0x22, centralState }; |
| 135 |
sendBuffer[sizeof(sendBuffer) - 1] = Utils::Utils::CalcXORCheckSum(sendBuffer + 4, sizeof(sendBuffer) - 5); |
| 136 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 137 |
} |
| 138 |
|
| 139 |
inline void SendVersion() |
| 140 |
{ |
| 141 |
unsigned char sendBuffer[9] = { 0x09, 0x00, 0x40, 0x00, 0x63, 0x21, 0x40, 0x12 }; |
| 142 |
sendBuffer[sizeof(sendBuffer) - 1] = Utils::Utils::CalcXORCheckSum(sendBuffer + 4, sizeof(sendBuffer) - 5); |
| 143 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 144 |
} |
| 145 |
|
| 146 |
inline void SendBroadcastFlags() |
| 147 |
{ |
| 148 |
unsigned char sendBuffer[8] = { 0x08, 0x00, 0x51, 0x00 }; |
| 149 |
Utils::Integer::IntToDataLittleEndian(broadCastFlags, sendBuffer + 4); |
| 150 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 151 |
} |
| 152 |
|
| 153 |
inline void SendLocoMode(const uint16_t address) |
| 154 |
{ |
| 155 |
unsigned char sendBuffer[7] = { 0x07, 0x00, 0x60, 0x00 }; |
| 156 |
Utils::Integer::ShortToDataBigEndian(address, sendBuffer + 4); |
| 157 |
sendBuffer[6] = 0x00; |
| 158 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 159 |
} |
| 160 |
|
| 161 |
inline void SendTurnoutMode(const uint16_t address) |
| 162 |
{ |
| 163 |
unsigned char sendBuffer[7] = { 0x07, 0x00, 0x70, 0x00 }; |
| 164 |
Utils::Integer::ShortToDataBigEndian(address, sendBuffer + 4); |
| 165 |
sendBuffer[6] = 0x00; |
| 166 |
Send(sendBuffer, sizeof(sendBuffer)); |
| 167 |
} |
| 168 |
|
| 169 |
inline void Send(const unsigned char* buffer, const size_t size) |
| 170 |
{ |
| 171 |
logger->HexOut(buffer, size); |
| 172 |
UdpClient::Send(buffer, size); |
| 173 |
} |
| 174 |
|
| 175 |
Logger::Logger* logger; |
| 176 |
|
| 177 |
Manager& manager; |
| 178 |
Z21Enums::BroadCastFlags broadCastFlags; |
| 179 |
}; |
| 180 |
}} |
| 181 |
|