| 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 <cstring> //memset |
| 22 | |
| 23 | #include "DataModel/ObjectIdentifier.h" |
| 24 | #include "Hardware/Protocols/Z21.h" |
| 25 | #include "Manager.h" |
| 26 | #include "Server/Z21/Z21Client.h" |
| 27 | |
| 28 | using DataModel::ObjectIdentifier; |
| 29 | |
| 30 | namespace Z21Enums = Hardware::Protocols::Z21Enums; |
| 31 | |
| 32 | namespace Server { namespace Z21 |
| 33 | { |
| 34 | Z21Client::Z21Client(const unsigned int id, |
| 35 | Manager& manager, |
| 36 | const int serverSocket, |
| 37 | const struct sockaddr_storage* clientAddress) |
| 38 | : Network::UdpClient(serverSocket, clientAddress, 60), |
| 39 | logger(Logger::Logger::GetLogger("Z21Client # " + std::to_string(id))), |
| 40 | manager(manager), |
| 41 | broadCastFlags(Z21Enums::BroadCastFlagNone) |
| 42 | { |
| 43 | logger->Debug(Languages::TextUdpConnectionEstablished, AddressAsString()); |
| 44 | SendSystemStatusChanged(); |
| 45 | } |
| 46 | |
| 47 | Z21Client::~Z21Client() |
| 48 | { |
| 49 | logger->Debug(Languages::TextUdpConnectionClosed, AddressAsString()); |
| 50 | } |
| 51 | |
| 52 | void Z21Client::Work(const unsigned char* buffer, const size_t dataLength) |
| 53 | { |
| 54 | logger->HexIn(buffer, dataLength); |
| 55 | |
| 56 | size_t dataRead = 0; |
| 57 | while (dataRead < dataLength) |
| 58 | { |
| 59 | size_t ret = ParseData(buffer + dataRead, dataLength - dataRead); |
| 60 | if (ret == 0) |
| 61 | { |
| 62 | break; |
| 63 | } |
| 64 | dataRead += ret; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | size_t Z21Client::ParseData(const unsigned char* buffer, const size_t bufferLength) |
| 69 | { |
| 70 | unsigned short dataLength = Utils::Integer::DataLittleEndianToShort(buffer); |
| 71 | if (dataLength < 4 || dataLength > bufferLength) |
| 72 | { |
| 73 | return 1472; // 1472 = Max UDP data size |
| 74 | } |
| 75 | |
| 76 | const uint16_t header = Utils::Integer::DataLittleEndianToShort(buffer + 2); |
| 77 | switch (header) |
| 78 | { |
| 79 | case Z21Enums::HeaderSerialNumber: |
| 80 | SendSerialNumber(); |
| 81 | break; |
| 82 | |
| 83 | case Z21Enums::HeaderGetCode: |
| 84 | SendCode(); |
| 85 | break; |
| 86 | |
| 87 | case Z21Enums::HeaderGetHardwareInfo: |
| 88 | SendHardwareInfo(); |
| 89 | break; |
| 90 | |
| 91 | case Z21Enums::HeaderLogOff: |
| 92 | Terminate(); |
| 93 | break; |
| 94 | |
| 95 | case Z21Enums::HeaderSeeXHeader: |
| 96 | ParseXHeader(buffer); |
| 97 | break; |
| 98 | |
| 99 | case Z21Enums::HeaderSetBroadcastFlags: |
| 100 | { |
| 101 | broadCastFlags = static_cast<Z21Enums::BroadCastFlags>(Utils::Integer::DataLittleEndianToInt(buffer + 4)); |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | case Z21Enums::HeaderGetBroadcastFlags: |
| 106 | SendBroadcastFlags(); |
| 107 | break; |
| 108 | |
| 109 | case Z21Enums::HeaderGetLocoMode: |
| 110 | { |
| 111 | const uint16_t address = *(reinterpret_cast<const uint16_t*>(buffer + 4)); |
| 112 | SendLocoMode(address); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | case Z21Enums::HeaderSetLocoMode: |
| 117 | // ignore loco mode, we always use DCC |
| 118 | break; |
| 119 | |
| 120 | case Z21Enums::HeaderGetTurnoutMode: |
| 121 | { |
| 122 | const uint16_t address = *(reinterpret_cast<const uint16_t*>(buffer + 4)); |
| 123 | SendTurnoutMode(address); |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | case Z21Enums::HeaderSetTurnoutMode: |
| 128 | // ignore turnout mode, we always use DCC |
| 129 | break; |
| 130 | |
| 131 | case Z21Enums::HeaderGetSystemState: |
| 132 | SendSystemStatusChanged(); |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | SendUnknownCommand(); |
| 137 | break; |
| 138 | } |
| 139 | return dataLength; |
| 140 | } |
| 141 | |
| 142 | void Z21Client::ParseXHeader(const unsigned char* buffer) |
| 143 | { |
| 144 | Z21Enums::XHeader xHeader = static_cast<Z21Enums::XHeader>(buffer[4]); |
| 145 | switch (xHeader) |
| 146 | { |
| 147 | case Z21Enums::XHeaderSeeDB0_1: |
| 148 | ParseDB0(buffer); |
| 149 | return; |
| 150 | |
| 151 | case Z21Enums::XHeaderSetStop: |
| 152 | logger->Debug(Languages::TextBoosterIsTurnedOff); |
| 153 | manager.Booster(ControlTypeZ21Server, BoosterStateStop); |
| 154 | SendBcStopped(); |
| 155 | return; |
| 156 | |
| 157 | case Z21Enums::XHeaderGetLocoInfo: |
| 158 | { |
| 159 | const Address address = ParseLocoAddress(buffer + 6); |
| 160 | logger->Debug("Address: {0}", address); |
| 161 | const DataModel::LocoBase* const locoBase = manager.GetLocoBase(manager.GetIdentifierOfServerLocoAddress(address)); |
| 162 | if (nullptr == locoBase) |
| 163 | { |
| 164 | return; |
| 165 | } |
| 166 | SendLocoInfo(locoBase); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | case Z21Enums::XHeaderGetTurnoutInfo: |
| 171 | { |
| 172 | const Address address = ParseLocoAddress(buffer + 5); |
| 173 | logger->Debug("Address: {0}", address); |
| 174 | const DataModel::AccessoryBase* const accessoryBase = manager.GetAccessoryBase(manager.GetIdentifierOfServerAccessoryAddress(address)); |
| 175 | if (nullptr == accessoryBase) |
| 176 | { |
| 177 | return; |
| 178 | } |
| 179 | SendTurnoutInfo(accessoryBase); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | case Z21Enums::XHeaderLocoDrive: |
| 184 | switch(buffer[5]) |
| 185 | { |
| 186 | case Z21Enums::DB0SetLocoDrive14: |
| 187 | case Z21Enums::DB0SetLocoDrive28: |
| 188 | case Z21Enums::DB0SetLocoDrive128: |
| 189 | case Z21Enums::DB0LocoFunction: |
| 190 | ParseLocoDrive(buffer); |
| 191 | return; |
| 192 | |
| 193 | default: |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | case Z21Enums::XHeaderSetLocoBinaryState: |
| 198 | // we do not care |
| 199 | return; |
| 200 | |
| 201 | case Z21Enums::XHeaderSetTurnout: |
| 202 | { |
| 203 | const Address address = ParseAccessoryAddress(buffer + 5); |
| 204 | const ObjectIdentifier accessoryBaseIdentifier(manager.GetIdentifierOfServerAccessoryAddress(address)); |
| 205 | const bool activate = (buffer[7] & 0x08) == 0x08; |
| 206 | if (!activate) |
| 207 | { |
| 208 | return; |
| 209 | } |
| 210 | const DataModel::AccessoryState state = static_cast<DataModel::AccessoryState>(buffer[7] & 0x01); |
| 211 | logger->Debug(Languages::Languages::TextReceivedAccessoryCommand, Utils::Utils::ProtocolToString(ProtocolDCC), address, state); |
| 212 | manager.AccessoryBaseState(ControlTypeZ21Server, accessoryBaseIdentifier, state); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | case Z21Enums::XHeaderGetFirmwareVersion: |
| 217 | SendFirmwareVersion(); |
| 218 | return; |
| 219 | |
| 220 | default: |
| 221 | SendUnknownCommand(); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void Z21Client::ParseDB0(const unsigned char* buffer) |
| 227 | { |
| 228 | switch (buffer[5]) |
| 229 | { |
| 230 | case Z21Enums::DB0Version: |
| 231 | SendVersion(); |
| 232 | return; |
| 233 | |
| 234 | case Z21Enums::DB0Status: |
| 235 | SendStatusChanged(); |
| 236 | return; |
| 237 | |
| 238 | case Z21Enums::DB0SetPowerOff: |
| 239 | logger->Debug(Languages::TextBoosterIsTurnedOff); |
| 240 | manager.Booster(ControlTypeZ21Server, BoosterStateStop); |
| 241 | return; |
| 242 | |
| 243 | case Z21Enums::DB0SetPowerOn: |
| 244 | logger->Debug(Languages::TextBoosterIsTurnedOn); |
| 245 | manager.Booster(ControlTypeZ21Server, BoosterStateGo); |
| 246 | return; |
| 247 | |
| 248 | default: |
| 249 | SendUnknownCommand(); |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void Z21Client::ParseLocoDrive(const unsigned char* buffer) |
| 255 | { |
| 256 | const ObjectIdentifier locoBaseIdentifier(manager.GetIdentifierOfServerLocoAddress(ParseLocoAddress(buffer + 6))); |
| 257 | Speed speed; |
| 258 | switch(buffer[5]) |
| 259 | { |
| 260 | case Z21Enums::DB0SetLocoDrive14: |
| 261 | speed = Hardware::Protocols::Z21::DecodeSpeed14(buffer[8] & 0x7F); |
| 262 | break; |
| 263 | |
| 264 | case Z21Enums::DB0SetLocoDrive28: |
| 265 | speed = Hardware::Protocols::Z21::DecodeSpeed28(buffer[8] & 0x7F); |
| 266 | break; |
| 267 | |
| 268 | case Z21Enums::DB0SetLocoDrive128: |
| 269 | speed = Hardware::Protocols::Z21::DecodeSpeed128(buffer[8] & 0x7F); |
| 270 | break; |
| 271 | |
| 272 | case Z21Enums::DB0LocoFunction: |
| 273 | { |
| 274 | const DataModel::LocoFunctionNr nr = buffer[8] & 0x3F; |
| 275 | const DataModel::LocoFunctionState on = static_cast<DataModel::LocoFunctionState>((buffer[8] >> 6) & 0x01); |
| 276 | manager.LocoBaseFunctionState(ControlTypeZ21Server, locoBaseIdentifier, nr, on); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | default: |
| 281 | speed = 0; |
| 282 | break; |
| 283 | } |
| 284 | manager.LocoBaseSpeed(ControlTypeZ21Server, locoBaseIdentifier, speed); |
| 285 | manager.LocoBaseOrientation(ControlTypeZ21Server, locoBaseIdentifier, static_cast<Orientation>(buffer[8] >> 7)); |
| 286 | } |
| 287 | |
| 288 | void Z21Client::SendLocoInfo(const DataModel::LocoBase* const locoBase) |
| 289 | { |
| 290 | unsigned char sendBuffer[15] = { 0x0F, 0x00, 0x40, 0x00, 0xEF, 0x00, 0x00, 0x04 }; |
| 291 | const Address address = locoBase->GetServerAddress(); |
| 292 | if (address == AddressNone) |
| 293 | { |
| 294 | return; |
| 295 | } |
| 296 | Utils::Integer::ShortToDataBigEndian(address, sendBuffer + 5); |
| 297 | sendBuffer[8] = (locoBase->GetOrientation() << 7) | Hardware::Protocols::Z21::EncodeSpeed128(locoBase->GetSpeed()); |
| 298 | sendBuffer[9] = ((locoBase->GetFunctionState(0) & 0x01) << 4) |
| 299 | | ((locoBase->GetFunctionState(4) & 0x01) << 3) |
| 300 | | ((locoBase->GetFunctionState(3) & 0x01) << 2) |
| 301 | | ((locoBase->GetFunctionState(2) & 0x01) << 1) |
| 302 | | (locoBase->GetFunctionState(1) & 0x01); |
| 303 | sendBuffer[10] = ((locoBase->GetFunctionState(12) & 0x01) << 7) |
| 304 | | ((locoBase->GetFunctionState(11) & 0x01) << 6) |
| 305 | | ((locoBase->GetFunctionState(10) & 0x01) << 5) |
| 306 | | ((locoBase->GetFunctionState(9) & 0x01) << 4) |
| 307 | | ((locoBase->GetFunctionState(8) & 0x01) << 3) |
| 308 | | ((locoBase->GetFunctionState(7) & 0x01) << 2) |
| 309 | | ((locoBase->GetFunctionState(6) & 0x01) << 1) |
| 310 | | (locoBase->GetFunctionState(5) & 0x01); |
| 311 | sendBuffer[11] = ((locoBase->GetFunctionState(20) & 0x01) << 7) |
| 312 | | ((locoBase->GetFunctionState(19) & 0x01) << 6) |
| 313 | | ((locoBase->GetFunctionState(18) & 0x01) << 5) |
| 314 | | ((locoBase->GetFunctionState(17) & 0x01) << 4) |
| 315 | | ((locoBase->GetFunctionState(16) & 0x01) << 3) |
| 316 | | ((locoBase->GetFunctionState(15) & 0x01) << 2) |
| 317 | | ((locoBase->GetFunctionState(14) & 0x01) << 1) |
| 318 | | (locoBase->GetFunctionState(12) & 0x01); |
| 319 | sendBuffer[12] = ((locoBase->GetFunctionState(28) & 0x01) << 7) |
| 320 | | ((locoBase->GetFunctionState(27) & 0x01) << 6) |
| 321 | | ((locoBase->GetFunctionState(26) & 0x01) << 5) |
| 322 | | ((locoBase->GetFunctionState(25) & 0x01) << 4) |
| 323 | | ((locoBase->GetFunctionState(24) & 0x01) << 3) |
| 324 | | ((locoBase->GetFunctionState(23) & 0x01) << 2) |
| 325 | | ((locoBase->GetFunctionState(22) & 0x01) << 1) |
| 326 | | (locoBase->GetFunctionState(21) & 0x01); |
| 327 | sendBuffer[13] = ((locoBase->GetFunctionState(31) & 0x01) << 2) |
| 328 | | ((locoBase->GetFunctionState(30) & 0x01) << 1) |
| 329 | | (locoBase->GetFunctionState(29) & 0x01); |
| 330 | sendBuffer[sizeof(sendBuffer) - 1] = Utils::Utils::CalcXORCheckSum(sendBuffer, sizeof(sendBuffer) - 1); |
| 331 | logger->Debug(Languages::Languages::TextSendingLocoInfo, address); |
| 332 | Send(sendBuffer, sizeof(sendBuffer)); |
| 333 | } |
| 334 | |
| 335 | void Z21Client::SendTurnoutInfo(const DataModel::AccessoryBase* const accessoryBase) |
| 336 | { |
| 337 | unsigned char sendBuffer[9] = { 0x09, 0x00, 0x40, 0x00, 0x43 }; |
| 338 | const Address address = accessoryBase->GetServerAddress(); |
| 339 | if (address == AddressNone) |
| 340 | { |
| 341 | return; |
| 342 | } |
| 343 | Utils::Integer::ShortToDataBigEndian(address - 1, sendBuffer + 5); // - 1 because Z21 address is 0-based |
| 344 | sendBuffer[7] = 1 << (accessoryBase->GetAccessoryState() & 0x01); |
| 345 | sendBuffer[sizeof(sendBuffer) - 1] = Utils::Utils::CalcXORCheckSum(sendBuffer, sizeof(sendBuffer) - 1); |
| 346 | logger->Debug(Languages::Languages::TextSendingTurnoutInfo, address); |
| 347 | Send(sendBuffer, sizeof(sendBuffer)); |
| 348 | } |
| 349 | }} // namespace Server::Z21 |