| 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 <string> |
| 22 | #include <thread> |
| 23 | #include "DataModel/LocoFunctions.h" |
| 24 | #include "Hardware/Protocols/LocoNet.h" |
| 25 | #include "Utils/Integer.h" |
| 26 | |
| 27 | namespace Hardware |
| 28 | { |
| 29 | namespace Protocols |
| 30 | { |
| 31 | LocoNet::LocoNet(const HardwareParams* params, |
| 32 | const std::string& controlName, |
| 33 | const unsigned int dataSpeed) |
| 34 | : HardwareInterface(params->GetManager(), |
| 35 | params->GetControlID(), |
| 36 | controlName + " / " + params->GetName() + " at serial port " + params->GetArg1(), |
| 37 | params->GetName()), |
| 38 | run(true), |
| 39 | serialLine(logger, params->GetArg1(), dataSpeed, 8, 'N', 1), |
| 40 | lastCv(0), |
| 41 | isProgramming(false) |
| 42 | { |
| 43 | receiverThread = std::thread(&Hardware::Protocols::LocoNet::Receiver, this); |
| 44 | senderThread = std::thread(&Hardware::Protocols::LocoNet::Sender, this); |
| 45 | } |
| 46 | |
| 47 | LocoNet::~LocoNet() |
| 48 | { |
| 49 | run = false; |
| 50 | sendingQueue.EnqueueBack(SendingQueueEntry()); |
| 51 | senderThread.join(); |
| 52 | receiverThread.join(); |
| 53 | logger->Info(Languages::TextTerminatingSenderSocket); |
| 54 | } |
| 55 | |
| 56 | void LocoNet::Booster(const BoosterState status) |
| 57 | { |
| 58 | if (BoosterStateGo == status) |
| 59 | { |
| 60 | Send2ByteCommand(OPC_GPON); |
| 61 | logger->Info(Languages::TextTurningBoosterOn); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | Send2ByteCommand(OPC_GPOFF); |
| 66 | logger->Info(Languages::TextTurningBoosterOff); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void LocoNet::LocoSpeed(__attribute__((unused)) const Protocol protocol, |
| 71 | const Address address, |
| 72 | const Speed speed) |
| 73 | { |
| 74 | unsigned char slot = locoCache.GetSlotOfAddress(address); |
| 75 | if (0 == slot) |
| 76 | { |
| 77 | SendLocoAddress(address); |
| 78 | return; |
| 79 | } |
| 80 | SendLocoSpeed(slot, speed); |
| 81 | } |
| 82 | |
| 83 | void LocoNet::LocoOrientation(__attribute__((unused)) const Protocol protocol, |
| 84 | const Address address, |
| 85 | const Orientation orientation) |
| 86 | { |
| 87 | unsigned char slot = locoCache.GetSlotOfAddress(address); |
| 88 | if (0 == slot) |
| 89 | { |
| 90 | SendLocoAddress(address); |
| 91 | return; |
| 92 | } |
| 93 | const unsigned char orientationF0F4 = SetOrientationF0F4Bit(slot, orientation, 5); |
| 94 | SendLocoOrientationF0F4(slot, orientationF0F4); |
| 95 | } |
| 96 | |
| 97 | void LocoNet::LocoFunction(__attribute__((unused)) const Protocol protocol, |
| 98 | const Address address, |
| 99 | const DataModel::LocoFunctionNr function, |
| 100 | const DataModel::LocoFunctionState on) |
| 101 | { |
| 102 | unsigned char slot = locoCache.GetSlotOfAddress(address); |
| 103 | if (0 == slot) |
| 104 | { |
| 105 | SendLocoAddress(address); |
| 106 | return; |
| 107 | } |
| 108 | switch (function) |
| 109 | { |
| 110 | case 0: |
| 111 | { |
| 112 | const unsigned char shift = 4; |
| 113 | const unsigned char orientationF0F4 = SetOrientationF0F4Bit(slot, on, shift); |
| 114 | SendLocoOrientationF0F4(slot, orientationF0F4); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | case 1: |
| 119 | case 2: |
| 120 | case 3: |
| 121 | case 4: |
| 122 | { |
| 123 | const unsigned char shift = function - 1; |
| 124 | const unsigned char orientationF0F4 = SetOrientationF0F4Bit(slot, on, shift); |
| 125 | SendLocoOrientationF0F4(slot, orientationF0F4); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | case 5: |
| 130 | case 6: |
| 131 | case 7: |
| 132 | case 8: |
| 133 | { |
| 134 | const unsigned char shift = function - 5; |
| 135 | const unsigned char f5F8 = SetF5F8Bit(slot, on, shift); |
| 136 | SendLocoF5F8(slot, f5F8); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | case 9: |
| 141 | case 10: |
| 142 | case 11: |
| 143 | case 12: |
| 144 | { |
| 145 | const unsigned char shift = function - 9; |
| 146 | const unsigned char f9F12 = SetF9F12Bit(slot, on, shift); |
| 147 | SendLocoF9F12(slot, f9F12); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | // else function > 12 |
| 155 | // Sending function numbers bigger than 12 is an Uhlenbrock Intellibox II extension |
| 156 | |
| 157 | if (function > 28) |
| 158 | { |
| 159 | // even Uhlenbrock Intellibox does not interpret functions > 28 correct |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | const unsigned char shift = function - 13; |
| 164 | const uint32_t data = SetF13F44Bit(slot, on, shift); |
| 165 | if (function < 20) |
| 166 | { |
| 167 | const uint8_t data4 = static_cast<uint8_t>(data & 0x7F); |
| 168 | Send6ByteCommand(OPC_EXP_CMD, 0x20, 0x01, 0x08, data4); |
| 169 | return; |
| 170 | } |
| 171 | if (function == 20 || function == 28) |
| 172 | { |
| 173 | const uint8_t data4 = static_cast<uint8_t>(((data >> 2) & 0x20) | ((data >> 9) & 0x40)); |
| 174 | Send6ByteCommand(OPC_EXP_CMD, 0x20, 0x01, 0x05, data4); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // else function > 21 && function < 28 |
| 179 | const uint8_t data4 = static_cast<uint8_t>((data >> 8) & 0x7F); |
| 180 | Send6ByteCommand(OPC_EXP_CMD, 0x20, 0x01, 0x09, data4); |
| 181 | return; |
| 182 | |
| 183 | // else function > 28 |
| 184 | // Actually Uhlenbrock Intellibox II does not interpret and store these values. |
| 185 | // Therefore we don't send the values. |
| 186 | // const uint8_t data1 = static_cast<uint8_t>((data >> (function - 17 )) & 0x10); |
| 187 | // const uint8_t data4 = static_cast<uint8_t>(function - 29); |
| 188 | // Send6ByteCommand(OPC_LOCO_FUNC2, data1, 0x01, 0x1D, data4); |
| 189 | // return; |
| 190 | } |
| 191 | |
| 192 | void LocoNet::Accessory(__attribute__((unused)) const Protocol protocol, |
| 193 | const Address address, |
| 194 | const DataModel::AccessoryState state, |
| 195 | const bool on, |
| 196 | __attribute__((unused)) const DataModel::AccessoryPulseDuration duration) |
| 197 | { |
| 198 | logger->Info(Languages::TextSettingAccessoryOnOff, address, Languages::GetGreenRed(state), Languages::GetOnOff(on)); |
| 199 | const Address addressLocoNet = address - 1; |
| 200 | unsigned char addressLow = static_cast<unsigned char>(addressLocoNet & 0x007F); |
| 201 | unsigned char addressHigh = static_cast<unsigned char>(((addressLocoNet >> 7) & 0x000F) | ((state & 0x01) << 5) | ((on & 0x01) << 4)); |
| 202 | Send4ByteCommand(OPC_SW_REQ, addressLow, addressHigh); |
| 203 | } |
| 204 | |
| 205 | void LocoNet::ProgramWrite(const ProgramMode mode, |
| 206 | __attribute__((unused)) const Address address, |
| 207 | const CvNumber cv, |
| 208 | const CvValue value) |
| 209 | { |
| 210 | switch(mode) |
| 211 | { |
| 212 | case ProgramModeMm: |
| 213 | case ProgramModeDccRegister: |
| 214 | case ProgramModeDccPage: |
| 215 | case ProgramModeDccDirect: |
| 216 | ProgramStart(); |
| 217 | ProgramPT(true, mode, cv, value); |
| 218 | lastCv = cv; |
| 219 | break; |
| 220 | |
| 221 | /** |
| 222 | * actually not used |
| 223 | case ProgramModeDccPomLoco: |
| 224 | ProgramMain(address, cv, value); |
| 225 | lastCv = cv; |
| 226 | break; |
| 227 | */ |
| 228 | |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void LocoNet::ProgramRead(const ProgramMode mode, |
| 235 | __attribute__((unused)) const Address address, |
| 236 | const CvNumber cv) |
| 237 | { |
| 238 | switch(mode) |
| 239 | { |
| 240 | case ProgramModeDccRegister: |
| 241 | case ProgramModeDccPage: |
| 242 | case ProgramModeDccDirect: |
| 243 | ProgramStart(); |
| 244 | ProgramPT(false, mode, cv); |
| 245 | lastCv = cv; |
| 246 | break; |
| 247 | |
| 248 | default: |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void LocoNet::ProgramStart() |
| 254 | { |
| 255 | unsigned char data[0x07]; |
| 256 | data[0] = 0xE5; |
| 257 | data[1] = 0x07; |
| 258 | data[2] = 0x01; |
| 259 | data[3] = 0x49; |
| 260 | data[4] = 0x42; |
| 261 | data[5] = 0x41; |
| 262 | // data[6] is set in SendXByteCommand |
| 263 | |
| 264 | SendXByteCommand(data, sizeof(data)); |
| 265 | isProgramming = true; |
| 266 | } |
| 267 | |
| 268 | void LocoNet::ProgramEnd() |
| 269 | { |
| 270 | unsigned char data[0x07]; |
| 271 | data[0] = 0xE5; |
| 272 | data[1] = 0x07; |
| 273 | data[2] = 0x01; |
| 274 | data[3] = 0x49; |
| 275 | data[4] = 0x42; |
| 276 | data[5] = 0x40; |
| 277 | // data[6] is set in SendXByteCommand |
| 278 | |
| 279 | SendXByteCommand(data, sizeof(data)); |
| 280 | isProgramming = false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Reverse Engineered with a KM1 System control 7, but it does not work properly. |
| 285 | void LocoNet::ProgramMain(const Address address, |
| 286 | const CvNumber cv, |
| 287 | const CvValue value) |
| 288 | { |
| 289 | unsigned char data[0x1F]; // 31 bytes |
| 290 | data[0] = OPC_PROGRAM; |
| 291 | data[1] = 0x1F; |
| 292 | data[2] = 0x01; |
| 293 | data[3] = 0x49; |
| 294 | data[4] = 0x42; |
| 295 | data[5] = 0x71 | ((cv >> 4) & 0x08) | ((address >> 6) & 0x02); |
| 296 | data[6] = 0x5E; |
| 297 | data[7] = (address & 0x7F); |
| 298 | data[8] = ((address >> 1) & 0x7F); |
| 299 | data[9] = (cv & 0x7F); |
| 300 | data[10] = 0x00; |
| 301 | data[11] = ((cv >> 8) & 0x7F); |
| 302 | data[12] = (value & 0x7F); |
| 303 | data[13] = 0x00; |
| 304 | data[14] = 0x00; |
| 305 | data[15] = 0x10; |
| 306 | data[16] = 0x00; |
| 307 | data[17] = 0x00; |
| 308 | data[18] = 0x00; |
| 309 | data[19] = 0x00; |
| 310 | data[20] = 0x00; |
| 311 | data[21] = 0x00; |
| 312 | data[22] = 0x00; |
| 313 | data[23] = 0x00; |
| 314 | data[24] = 0x00; |
| 315 | data[25] = 0x00; |
| 316 | data[26] = 0x00; |
| 317 | data[27] = 0x00; |
| 318 | data[28] = 0x00; |
| 319 | data[29] = 0x00; |
| 320 | data[30] = 0x00; |
| 321 | //data[31] is set in SendXByteCommand |
| 322 | |
| 323 | SendXByteCommand(data, sizeof(data)); |
| 324 | } |
| 325 | */ |
| 326 | |
| 327 | void LocoNet::ProgramPT(const bool write, |
| 328 | const ProgramMode mode, |
| 329 | const CvNumber cv, |
| 330 | const CvValue value) |
| 331 | { |
| 332 | unsigned char data[0x1F]; // 31 bytes |
| 333 | switch(mode) |
| 334 | { |
| 335 | case ProgramModeMm: |
| 336 | data[6] = 0x62; |
| 337 | break; |
| 338 | |
| 339 | case ProgramModeDccRegister: |
| 340 | data[6] = 0x6C; |
| 341 | break; |
| 342 | |
| 343 | case ProgramModeDccPage: |
| 344 | data[6] = 0x6E; |
| 345 | break; |
| 346 | |
| 347 | case ProgramModeDccDirect: |
| 348 | data[6] = 0x70; |
| 349 | break; |
| 350 | |
| 351 | default: |
| 352 | return; |
| 353 | } |
| 354 | data[0] = OPC_PROGRAM; |
| 355 | data[1] = 0x1F; |
| 356 | data[2] = 0x01; |
| 357 | data[3] = 0x49; |
| 358 | data[4] = 0x42; |
| 359 | data[5] = 0x71 | ((value >> 4) & 0x08) | ((cv >> 6) & 0x02); |
| 360 | data[6] |= static_cast<unsigned char>(write); |
| 361 | data[7] = (cv & 0x7F); |
| 362 | data[8] = (cv >> 8); |
| 363 | data[9] = (value & 0x7F); |
| 364 | data[10] = 0x00; |
| 365 | data[11] = 0x00; |
| 366 | data[12] = 0x00; |
| 367 | data[13] = 0x00; |
| 368 | data[14] = 0x00; |
| 369 | data[15] = 0x10; |
| 370 | data[16] = 0x00; |
| 371 | data[17] = 0x00; |
| 372 | data[18] = 0x00; |
| 373 | data[19] = 0x00; |
| 374 | data[20] = 0x00; |
| 375 | data[21] = 0x00; |
| 376 | data[22] = 0x00; |
| 377 | data[23] = 0x00; |
| 378 | data[24] = 0x00; |
| 379 | data[25] = 0x00; |
| 380 | data[26] = 0x00; |
| 381 | data[27] = 0x00; |
| 382 | data[28] = 0x00; |
| 383 | data[29] = 0x00; |
| 384 | data[30] = 0x00; |
| 385 | //data[31] is set in SendXByteCommand |
| 386 | |
| 387 | SendXByteCommand(data, sizeof(data)); |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * This code is written according the Digitrax LocoNet Persional Use Edition 1.0 |
| 392 | * It does not work with Uhlenbrock controls... |
| 393 | |
| 394 | void LocoNet::Program(const bool write, |
| 395 | const ProgramMode mode, |
| 396 | const Address address, |
| 397 | const CvNumber cv, |
| 398 | const CvValue value) |
| 399 | { |
| 400 | unsigned char data[0x0E]; // 14 bytes |
| 401 | data[0] = OPC_WR_SL_DATA; |
| 402 | // data[1] is set in SendXByteCommand with sizeof(data) |
| 403 | data[2] = ProgrammingSlot; // slot 124 |
| 404 | // data[3] |
| 405 | switch(mode) |
| 406 | { |
| 407 | case ProgramModeDccRegister: |
| 408 | data[3] = 0x33; // reserved D0 and D1 must be set to 1 |
| 409 | data[5] = 0x00; |
| 410 | data[6] = 0x00; |
| 411 | break; |
| 412 | |
| 413 | case ProgramModeDccPage: |
| 414 | data[3] = 0x23; // reserved D0 and D1 must be set to 1 |
| 415 | data[5] = 0x00; |
| 416 | data[6] = 0x00; |
| 417 | |
| 418 | case ProgramModeDccDirect: |
| 419 | data[3] = 0x2B; // reserved D0 and D1 must be set to 1 |
| 420 | data[5] = 0x00; |
| 421 | data[6] = 0x00; |
| 422 | break; |
| 423 | |
| 424 | case ProgramModeDccPomLoco: |
| 425 | data[3] = 0x27; |
| 426 | data[5] = static_cast<unsigned char>((address >> 7) & 0x7F); |
| 427 | data[6] = static_cast<unsigned char>(address & 0x7F); |
| 428 | break; |
| 429 | |
| 430 | default: |
| 431 | return; |
| 432 | } |
| 433 | data[3] |= (static_cast<unsigned char>(write) << 6); |
| 434 | |
| 435 | data[4] = 0x00; |
| 436 | // TRK |
| 437 | data[7] = 0x00; |
| 438 | // CVH |
| 439 | data[8] = ((cv >> 4) & 0x30) | ((cv >> 7) & 0x01) | ((value >> 6) & 0x02); |
| 440 | // CVL |
| 441 | data[9] = static_cast<unsigned char>(cv & 0x7F); |
| 442 | // DATA7 |
| 443 | data[10] = static_cast<unsigned char>(value & 0x7F); |
| 444 | data[11] = 0x00; |
| 445 | data[12] = 0x00; |
| 446 | // data[13] is checksum and is calculated in SendXByteCommand |
| 447 | |
| 448 | SendXByteCommand(data, sizeof(data)); |
| 449 | } |
| 450 | */ |
| 451 | |
| 452 | void LocoNet::Sender() |
| 453 | { |
| 454 | Utils::Utils::SetThreadName("LocoNet Sender"); |
| 455 | logger->Info(Languages::TextSenderThreadStarted); |
| 456 | while (run) |
| 457 | { |
| 458 | { |
| 459 | std::unique_lock<std::mutex> lock(entryToVerifyMutex); |
| 460 | while (entryToVerify.GetSize() > 0) |
| 461 | { |
| 462 | // we wait 1s for the response from LocoNet |
| 463 | if (entryToVerifyCV.wait_for(lock, std::chrono::seconds(1)) == std::cv_status::timeout) |
| 464 | { |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | SendingQueueEntry temp = sendingQueue.Dequeue(); |
| 471 | { |
| 472 | std::unique_lock<std::mutex> lock(entryToVerifyMutex); |
| 473 | entryToVerify = temp; |
| 474 | } |
| 475 | |
| 476 | const unsigned char size = temp.GetSize(); |
| 477 | if (!size) |
| 478 | { |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | const unsigned char* data = temp.GetData(); |
| 483 | |
| 484 | logger->HexOut(data, size); |
| 485 | serialLine.Send(data, size); |
| 486 | } |
| 487 | logger->Info(Languages::TextTerminatingSenderThread); |
| 488 | } |
| 489 | |
| 490 | void LocoNet::Receiver() |
| 491 | { |
| 492 | Utils::Utils::SetThreadName("LocoNet Receiver"); |
| 493 | logger->Info(Languages::TextReceiverThreadStarted); |
| 494 | |
| 495 | const unsigned char BufferSize = 128u; |
| 496 | unsigned char buffer[BufferSize]; |
| 497 | unsigned char checkSumCalculated; |
| 498 | unsigned char commandLength; |
| 499 | while (run) |
| 500 | { |
| 501 | if (!serialLine.IsConnected()) |
| 502 | { |
| 503 | logger->Error(Languages::TextUnableToReceiveData); |
| 504 | logger->Info(Languages::TextTerminatingReceiverThread); |
| 505 | return; |
| 506 | } |
| 507 | ssize_t dataLength = serialLine.ReceiveExact(buffer, 1); |
| 508 | if (dataLength != 1) |
| 509 | { |
| 510 | continue; |
| 511 | } |
| 512 | unsigned char commandType = buffer[0] & 0xE0; |
| 513 | switch (commandType) |
| 514 | { |
| 515 | case 0x80: |
| 516 | dataLength = serialLine.ReceiveExact(buffer + 1, 1); |
| 517 | if (dataLength != 1) |
| 518 | { |
| 519 | continue; |
| 520 | } |
| 521 | commandLength = 2; |
| 522 | break; |
| 523 | |
| 524 | case 0xA0: |
| 525 | { |
| 526 | dataLength = serialLine.ReceiveExact(buffer + 1, 3); |
| 527 | if (dataLength != 3) |
| 528 | { |
| 529 | continue; |
| 530 | } |
| 531 | commandLength = 4; |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | case 0xC0: |
| 536 | { |
| 537 | dataLength = serialLine.ReceiveExact(buffer + 1, 5); |
| 538 | if (dataLength != 5) |
| 539 | { |
| 540 | continue; |
| 541 | } |
| 542 | commandLength = 6; |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | case 0xE0: |
| 547 | { |
| 548 | dataLength = serialLine.ReceiveExact(buffer + 1, 1); |
| 549 | if (dataLength != 1) |
| 550 | { |
| 551 | continue; |
| 552 | } |
| 553 | commandLength = buffer[1]; |
| 554 | if (commandLength < 3) |
| 555 | { |
| 556 | continue; |
| 557 | } |
| 558 | unsigned char dataToRead = commandLength - 2; |
| 559 | dataLength = serialLine.ReceiveExact(buffer + 2, dataToRead); |
| 560 | if (dataLength != dataToRead) |
| 561 | { |
| 562 | continue; |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | default: |
| 568 | continue; |
| 569 | } |
| 570 | CalcCheckSum(buffer, commandLength - 1, &checkSumCalculated); |
| 571 | if (checkSumCalculated != buffer[commandLength - 1]) |
| 572 | { |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | { |
| 577 | std::unique_lock<std::mutex> lock(entryToVerifyMutex); |
| 578 | const unsigned char size = entryToVerify.GetSize(); |
| 579 | if (size && memcmp(buffer, entryToVerify.GetData(), size) == 0) |
| 580 | { |
| 581 | // response of sent command received |
| 582 | entryToVerify.Reset(); |
| 583 | entryToVerifyCV.notify_all(); |
| 584 | continue; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (run == false) |
| 589 | { |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | logger->HexIn(buffer, commandLength); |
| 594 | Parse(buffer); |
| 595 | } |
| 596 | logger->Info(Languages::TextTerminatingReceiverThread); |
| 597 | } |
| 598 | |
| 599 | void LocoNet::CalcCheckSum(unsigned char* data, const unsigned char length, unsigned char* checkSum) |
| 600 | { |
| 601 | *checkSum = 0xFF; |
| 602 | for (unsigned char i = 0; i < length; ++i) |
| 603 | { |
| 604 | (*checkSum) ^= data[i]; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void LocoNet::Parse(unsigned char* data) |
| 609 | { |
| 610 | switch (data[0]) |
| 611 | { |
| 612 | case OPC_GPON: // 0x83 |
| 613 | logger->Info(Languages::TextTurningBoosterOn); |
| 614 | manager->Booster(ControlTypeHardware, BoosterStateGo); |
| 615 | return; |
| 616 | |
| 617 | case OPC_GPOFF: // 0x82 |
| 618 | logger->Info(Languages::TextTurningBoosterOff); |
| 619 | manager->Booster(ControlTypeHardware, BoosterStateStop); |
| 620 | return; |
| 621 | |
| 622 | case OPC_LOCO_SPD: // 0xA0 |
| 623 | { |
| 624 | const Address address = CheckSlot(data[1]); |
| 625 | if (!address) |
| 626 | { |
| 627 | return; |
| 628 | } |
| 629 | ParseSpeed(address, data[2]); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | case OPC_LOCO_DIRF: // 0xA1 |
| 634 | { |
| 635 | const unsigned char slot = data[1]; |
| 636 | const Address address = CheckSlot(slot); |
| 637 | if (!address) |
| 638 | { |
| 639 | return; |
| 640 | } |
| 641 | ParseOrientationF0F4(slot, address, data[2]); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | case OPC_LOCO_SND: // 0xA2 |
| 646 | { |
| 647 | const unsigned char slot = data[1]; |
| 648 | const Address address = CheckSlot(slot); |
| 649 | if (!address) |
| 650 | { |
| 651 | return; |
| 652 | } |
| 653 | ParseF5F8(slot, address, data[2]); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | case OPC_LOCO_FUNC: // 0xA3 |
| 658 | { |
| 659 | const unsigned char slot = data[1]; |
| 660 | const Address address = CheckSlot(slot); |
| 661 | if (!address) |
| 662 | { |
| 663 | return; |
| 664 | } |
| 665 | ParseF9F12(slot, address, data[2]); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | case OPC_SW_REQ: // 0xB0 |
| 670 | { |
| 671 | const bool on = static_cast<bool>((data[2] & 0x10) >> 4); |
| 672 | if (!on) |
| 673 | { |
| 674 | return; |
| 675 | } |
| 676 | const DataModel::AccessoryState state = static_cast<DataModel::AccessoryState>((data[2] & 0x20) >> 5); |
| 677 | const Address address = (static_cast<Address>(data[1] & 0x7F) | (static_cast<Address>(data[2] & 0x0F) << 7)) + 1; |
| 678 | logger->Info(Languages::TextSettingAccessory, address, Languages::GetGreenRed(state)); |
| 679 | manager->AccessoryBaseState(ControlTypeHardware, controlID, ProtocolServer, address, state); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | case OPC_INPUT_REP: // 0xB2 |
| 684 | { |
| 685 | ParseSensorData(data); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | case OPC_LONG_ACK: // 0xB4 |
| 690 | // Long ACK is not parsed |
| 691 | return; |
| 692 | |
| 693 | case OPC_EXP_CMD: // 0xD4 |
| 694 | { |
| 695 | // This is an Uhlenbrock Intellibox II extension |
| 696 | const unsigned char slot = data[2]; |
| 697 | const Address address = CheckSlot(slot); |
| 698 | if (!address) |
| 699 | { |
| 700 | return; |
| 701 | } |
| 702 | ParseF13F44(slot, address, data); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | case OPC_PEER_XFER: // 0xE5 |
| 707 | // Peer Xfer is not parsed |
| 708 | return; |
| 709 | |
| 710 | case OPC_SL_RD_DATA: // 0xE7 |
| 711 | ParseSlotReadData(data); |
| 712 | return; |
| 713 | |
| 714 | case OPC_PROGRAM: // 0xED |
| 715 | ParseProgram(data); |
| 716 | return; |
| 717 | |
| 718 | default: |
| 719 | logger->Debug(Languages::TextCommandUnknown, Utils::Integer::IntegerToHex(data[0])); |
| 720 | return; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | Address LocoNet::CheckSlot(const unsigned char slot) |
| 725 | { |
| 726 | const Address address = locoCache.GetAddressOfSlot(slot); |
| 727 | if (0 == address) |
| 728 | { |
| 729 | SendRequestLocoData(slot); |
| 730 | return 0; |
| 731 | } |
| 732 | return address; |
| 733 | } |
| 734 | |
| 735 | void LocoNet::ParseSlotReadData(const unsigned char* data) |
| 736 | { |
| 737 | const unsigned char dataLength = data[1]; |
| 738 | if (dataLength != 0x0E) |
| 739 | { |
| 740 | return; |
| 741 | } |
| 742 | const unsigned char slot = data[2]; |
| 743 | switch (slot) |
| 744 | { |
| 745 | case SlotHardwareType: |
| 746 | ParseSlotHardwareType(data); |
| 747 | return; |
| 748 | |
| 749 | case SlotProgramming: |
| 750 | ParseSlotProgramming(data); |
| 751 | return; |
| 752 | |
| 753 | default: |
| 754 | ParseSlotLocoData(data); |
| 755 | return; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | void LocoNet::ParseSlotHardwareType(const unsigned char* data) |
| 760 | { |
| 761 | const unsigned char ib[] = { 0x00, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00, 0x49, 0x42, 0x18 }; |
| 762 | if (memcmp(ib, data + 3, 11) == 0) |
| 763 | { |
| 764 | logger->Info(Languages::TextConnectedTo, "Intellibox / TwinCenter"); |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | const unsigned char ib2[] = { 0x02, 0x42, 0x03, 0x00, 0x07, 0x00, 0x00, 0x15, 0x49, 0x42, 0x4C }; |
| 769 | if (memcmp(ib2, data + 3, 11) == 0) |
| 770 | { |
| 771 | logger->Info(Languages::TextConnectedTo, "Intellibox II / IB-Basic / IB-Com"); |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | const unsigned char sc7[] = { 0x02, 0x42, 0x03, 0x00, 0x06, 0x00, 0x00, 0x15, 0x49, 0x42, 0x4D }; |
| 776 | if (memcmp(sc7, data + 3, 11) == 0) |
| 777 | { |
| 778 | logger->Info(Languages::TextConnectedTo, "System Control 7"); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | const unsigned char daisy[] = { 0x00, 0x44, 0x02, 0x00, 0x07, 0x00, 0x59, 0x01, 0x49, 0x42, 0x04 }; |
| 783 | if (memcmp(daisy, data + 3, 11) == 0) |
| 784 | { |
| 785 | logger->Info(Languages::TextConnectedTo, "Daisy"); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | const unsigned char adapter63820[] = { 0x00, 0x4C, 0x01, 0x00, 0x07, 0x00, 0x49, 0x02, 0x49, 0x42, 0x1C }; |
| 790 | if (memcmp(adapter63820, data + 3, 11) == 0) |
| 791 | { |
| 792 | logger->Info(Languages::TextConnectedTo, "Adapter 63820"); |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | const unsigned char digitraxChief[] = { 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }; |
| 797 | if (memcmp(digitraxChief, data + 3, 11) == 0) |
| 798 | { |
| 799 | logger->Info(Languages::TextConnectedTo, "Digitrax Chief"); |
| 800 | return; |
| 801 | } |
| 802 | |
| 803 | logger->Info(Languages::TextUnknownHardware); |
| 804 | } |
| 805 | |
| 806 | void LocoNet::ParseSlotProgramming(const unsigned char* data) |
| 807 | { |
| 808 | if (data[4] != 0) |
| 809 | { |
| 810 | return; |
| 811 | } |
| 812 | CvNumber cv = (static_cast<CvNumber>(data[9]) & 0x7F) | ((static_cast<CvNumber>(data[8]) & 0x01) << 7) | ((static_cast<CvNumber>(data[8]) & 0x30) << 5); |
| 813 | if (cv == 0) |
| 814 | { |
| 815 | cv = lastCv; |
| 816 | } |
| 817 | |
| 818 | const CvValue value = (static_cast<CvValue>(data[10]) & 0x7F) | ((static_cast<CvValue>(data[8]) & 0x02) << 6); |
| 819 | logger->Info(Languages::TextProgramReadValue, cv, value); |
| 820 | manager->ProgramValue(cv, value); |
| 821 | |
| 822 | if (isProgramming) |
| 823 | { |
| 824 | ProgramEnd(); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | void LocoNet::ParseSlotLocoData(const unsigned char* data) |
| 829 | { |
| 830 | const unsigned char slot = data[2]; |
| 831 | const Address address = ParseLocoAddress(data[4], data[9]); |
| 832 | logger->Debug(Languages::TextSlotHasAddress, slot, address); |
| 833 | |
| 834 | locoCache.SetAddress(slot, address); |
| 835 | ParseSpeed(address, data[5]); |
| 836 | ParseOrientationF0F4(slot, address, data[6]); |
| 837 | ParseF5F8(slot, address, data[10]); |
| 838 | } |
| 839 | |
| 840 | void LocoNet::ParseProgram(const unsigned char* data) |
| 841 | { |
| 842 | if (data[1] != 0x1F) |
| 843 | { |
| 844 | return; |
| 845 | } |
| 846 | switch (data[6]) |
| 847 | { |
| 848 | /* |
| 849 | * actually not used because it can't be verified |
| 850 | case 0x5E: |
| 851 | // PoM |
| 852 | lastCv = static_cast<CvNumber>(data[9] & 0x7F) | static_cast<CvNumber>((data[5] & 0x08) << 4) | static_cast<CvNumber>((data[11] & 0x7F) << 8); |
| 853 | break; |
| 854 | */ |
| 855 | |
| 856 | case 0x6C: // register read |
| 857 | case 0x6D: // register write |
| 858 | case 0x6E: // page read |
| 859 | case 0x6F: // page write |
| 860 | case 0x70: // direct read |
| 861 | case 0x71: // direct write |
| 862 | // Program track |
| 863 | lastCv = static_cast<CvNumber>(data[7] & 0x7F) | static_cast<CvNumber>((data[5] & 0x08) << 4) | static_cast<CvNumber>((data[8] & 0x7F) << 8); |
| 864 | break; |
| 865 | |
| 866 | default: |
| 867 | // unknown programming method |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | void LocoNet::ParseSpeed(const Address address, const unsigned char data) |
| 873 | { |
| 874 | Speed speed = static_cast<Speed>(data); |
| 875 | if (speed) |
| 876 | { |
| 877 | --speed; |
| 878 | } |
| 879 | speed <<= 3; |
| 880 | logger->Info(Languages::TextSettingSpeed, address, speed); |
| 881 | manager->LocoSpeed(ControlTypeHardware, controlID, ProtocolServer, address, speed); |
| 882 | } |
| 883 | |
| 884 | unsigned char LocoNet::CalcSpeed(const Speed speed) |
| 885 | { |
| 886 | Speed calculatedSpeed = speed; |
| 887 | calculatedSpeed >>= 3; |
| 888 | if (calculatedSpeed) |
| 889 | { |
| 890 | ++calculatedSpeed; |
| 891 | } |
| 892 | if (calculatedSpeed > 127) |
| 893 | { |
| 894 | calculatedSpeed = 127; |
| 895 | } |
| 896 | return static_cast<unsigned char>(calculatedSpeed); |
| 897 | } |
| 898 | |
| 899 | void LocoNet::ParseOrientationF0F4(const unsigned char slot, const Address address, const uint8_t data) |
| 900 | { |
| 901 | const uint8_t oldData = locoCache.GetOrientationF0F4(slot); |
| 902 | const uint8_t dataDiff = data ^ oldData; |
| 903 | locoCache.SetOrientationF0F4(slot, data); |
| 904 | if (dataDiff & 0x20) |
| 905 | { |
| 906 | Orientation orientation = static_cast<Orientation>((data >> 5) & 0x01); |
| 907 | logger->Info(Languages::TextSettingOrientation, address, orientation); |
| 908 | manager->LocoOrientation(ControlTypeHardware, controlID, ProtocolServer, address, orientation); |
| 909 | } |
| 910 | if (dataDiff & 0x10) |
| 911 | { |
| 912 | ParseFunction(address, data, 0, 4); |
| 913 | } |
| 914 | if (dataDiff & 0x01) |
| 915 | { |
| 916 | ParseFunction(address, data, 1, 0); |
| 917 | } |
| 918 | if (dataDiff & 0x02) |
| 919 | { |
| 920 | ParseFunction(address, data, 2, 1); |
| 921 | } |
| 922 | if (dataDiff & 0x04) |
| 923 | { |
| 924 | ParseFunction(address, data, 3, 2); |
| 925 | } |
| 926 | if (dataDiff & 0x08) |
| 927 | { |
| 928 | ParseFunction(address, data, 4, 3); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | void LocoNet::ParseF5F8(const unsigned char slot, const Address address, const uint8_t data) |
| 933 | { |
| 934 | const uint8_t oldData = locoCache.GetF5F8(slot); |
| 935 | const uint8_t dataDiff = data ^ oldData; |
| 936 | locoCache.SetF5F8(slot, data); |
| 937 | if (dataDiff & 0x01) |
| 938 | { |
| 939 | ParseFunction(address, data, 5, 0); |
| 940 | } |
| 941 | if (dataDiff & 0x02) |
| 942 | { |
| 943 | ParseFunction(address, data, 6, 1); |
| 944 | } |
| 945 | if (dataDiff & 0x04) |
| 946 | { |
| 947 | ParseFunction(address, data, 7, 2); |
| 948 | } |
| 949 | if (dataDiff & 0x08) |
| 950 | { |
| 951 | ParseFunction(address, data, 8, 3); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | void LocoNet::ParseF9F12(const unsigned char slot, const Address address, const uint8_t data) |
| 956 | { |
| 957 | const uint8_t oldData = locoCache.GetF9F12(slot); |
| 958 | const uint8_t dataDiff = data ^ oldData; |
| 959 | locoCache.SetF9F12(slot, data); |
| 960 | if (dataDiff & 0x01) |
| 961 | { |
| 962 | ParseFunction(address, data, 9, 0); |
| 963 | } |
| 964 | if (dataDiff & 0x02) |
| 965 | { |
| 966 | ParseFunction(address, data, 10, 1); |
| 967 | } |
| 968 | if (dataDiff & 0x04) |
| 969 | { |
| 970 | ParseFunction(address, data, 11, 2); |
| 971 | } |
| 972 | if (dataDiff & 0x08) |
| 973 | { |
| 974 | ParseFunction(address, data, 12, 3); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | void LocoNet::ParseFunction(const Address address, |
| 979 | const uint32_t data, |
| 980 | const DataModel::LocoFunctionNr nr, |
| 981 | const uint8_t shift) |
| 982 | { |
| 983 | DataModel::LocoFunctionState state = static_cast<DataModel::LocoFunctionState>((data >> shift) & 0x01); |
| 984 | logger->Info(Languages::TextSettingFunction, nr, address, state); |
| 985 | manager->LocoFunctionState(ControlTypeHardware, controlID, ProtocolServer, address, nr, state); |
| 986 | } |
| 987 | |
| 988 | void LocoNet::ParseF13F44(const unsigned char slot, const Address address, const unsigned char* data) |
| 989 | { |
| 990 | // This is an Uhlenbrock Intellibox II extension |
| 991 | const uint32_t oldData = locoCache.GetF13F44(slot); |
| 992 | uint32_t newData = oldData; |
| 993 | if (data[1] == 0x20) |
| 994 | { |
| 995 | if (data[3] == 0x08) |
| 996 | { |
| 997 | newData &= 0xFFFFFF80; |
| 998 | newData |= (static_cast<uint32_t>(data[4]) & 0x7F); |
| 999 | } |
| 1000 | else if (data[3] == 0x09) |
| 1001 | { |
| 1002 | newData &= 0xFFFF80FF; |
| 1003 | newData |= ((static_cast<uint32_t>(data[4]) << 8) & 0x7F00); |
| 1004 | } |
| 1005 | else if (data[3] == 0x05) |
| 1006 | { |
| 1007 | newData &= 0xFFFFFF7F; |
| 1008 | newData |= ((static_cast<uint32_t>(data[4]) << 2) & 0x80); |
| 1009 | newData &= 0xFFFF7FFF; |
| 1010 | newData |= ((static_cast<uint32_t>(data[4]) << 9) & 0x8000); |
| 1011 | } |
| 1012 | } |
| 1013 | else if ((data[1] & 0xEF) == 0) |
| 1014 | { |
| 1015 | const uint8_t nr = data[3]; |
| 1016 | if (nr < 29 || nr > 44) |
| 1017 | { |
| 1018 | return; |
| 1019 | } |
| 1020 | newData &= ~(1 << (nr - 13)); |
| 1021 | newData |= (static_cast<uint32_t>(data[1]) << (nr - 17)); |
| 1022 | } |
| 1023 | const uint32_t dataDiff = newData ^ oldData; |
| 1024 | locoCache.SetF13F44(slot, newData); |
| 1025 | for (int i = 0; i < 32; ++i) |
| 1026 | { |
| 1027 | if ((dataDiff >> i) & 0x01) |
| 1028 | { |
| 1029 | ParseFunction(address, newData, i + 13, i); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | void LocoNet::ParseSensorData(const unsigned char* data) |
| 1035 | { |
| 1036 | if (!(data[2] & 0x40)) |
| 1037 | { |
| 1038 | // control bit not set |
| 1039 | return; |
| 1040 | } |
| 1041 | const FeedbackPin pin = (((static_cast<FeedbackPin>(data[2]) >> 5) & 0x0001) |
| 1042 | | ((static_cast<FeedbackPin>(data[1]) <<1) & 0x00FE) |
| 1043 | | ((static_cast<FeedbackPin>(data[2]) << 8) & 0x7F)) |
| 1044 | + 1; // LocoNet is 0-based, RailControl is 1-based |
| 1045 | DataModel::Feedback::FeedbackState state = static_cast<DataModel::Feedback::FeedbackState>((data[2] >> 4) & 0x01); |
| 1046 | logger->Info(Languages::TextFeedbackChange, pin & 0x000F, pin >> 4, Languages::GetText(state ? Languages::TextOn : Languages::TextOff)); |
| 1047 | manager->FeedbackState(controlID, pin, state); |
| 1048 | } |
| 1049 | |
| 1050 | void LocoNet::Send2ByteCommand(const unsigned char data) |
| 1051 | { |
| 1052 | unsigned char buffer[2]; |
| 1053 | buffer[0] = data; |
| 1054 | CalcCheckSum(buffer, 1, buffer + 1); |
| 1055 | sendingQueue.EnqueueBack(SendingQueueEntry(sizeof(buffer), buffer)); |
| 1056 | } |
| 1057 | |
| 1058 | void LocoNet::Send4ByteCommand(const unsigned char data0, |
| 1059 | const unsigned char data1, |
| 1060 | const unsigned char data2) |
| 1061 | { |
| 1062 | unsigned char buffer[4]; |
| 1063 | buffer[0] = data0; |
| 1064 | buffer[1] = data1; |
| 1065 | buffer[2] = data2; |
| 1066 | CalcCheckSum(buffer, 3, buffer + 3); |
| 1067 | sendingQueue.EnqueueBack(SendingQueueEntry(sizeof(buffer), buffer)); |
| 1068 | } |
| 1069 | |
| 1070 | void LocoNet::Send6ByteCommand(const unsigned char data0, |
| 1071 | const unsigned char data1, |
| 1072 | const unsigned char data2, |
| 1073 | const unsigned char data3, |
| 1074 | const unsigned char data4) |
| 1075 | { |
| 1076 | unsigned char buffer[6]; |
| 1077 | buffer[0] = data0; |
| 1078 | buffer[1] = data1; |
| 1079 | buffer[2] = data2; |
| 1080 | buffer[3] = data3; |
| 1081 | buffer[4] = data4; |
| 1082 | CalcCheckSum(buffer, 5, buffer + 5); |
| 1083 | sendingQueue.EnqueueBack(SendingQueueEntry(sizeof(buffer), buffer)); |
| 1084 | } |
| 1085 | |
| 1086 | void LocoNet::SendXByteCommand(unsigned char* data, unsigned char dataLength) |
| 1087 | { |
| 1088 | data[1] = dataLength; |
| 1089 | CalcCheckSum(data, dataLength - 1, data + dataLength - 1); |
| 1090 | sendingQueue.EnqueueBack(SendingQueueEntry(dataLength, data)); |
| 1091 | } |
| 1092 | |
| 1093 | uint8_t LocoNet::SetOrientationF0F4Bit(const unsigned char slot, const bool on, const unsigned char shift) |
| 1094 | { |
| 1095 | uint8_t data = locoCache.GetOrientationF0F4(slot); |
| 1096 | data &= (~(0x01u << shift)); |
| 1097 | data |= (on << shift); |
| 1098 | locoCache.SetOrientationF0F4(slot, data); |
| 1099 | return data; |
| 1100 | } |
| 1101 | |
| 1102 | uint8_t LocoNet::SetF5F8Bit(const unsigned char slot, const bool on, const unsigned char shift) |
| 1103 | { |
| 1104 | uint8_t data = locoCache.GetF5F8(slot); |
| 1105 | data &= (~(0x01u << shift)); |
| 1106 | data |= (on << shift); |
| 1107 | locoCache.SetF5F8(slot, data); |
| 1108 | return data; |
| 1109 | } |
| 1110 | |
| 1111 | uint8_t LocoNet::SetF9F12Bit(const unsigned char slot, const bool on, const unsigned char shift) |
| 1112 | { |
| 1113 | uint8_t data = locoCache.GetF9F12(slot); |
| 1114 | data &= (~(0x01u << shift)); |
| 1115 | data |= (on << shift); |
| 1116 | locoCache.SetF9F12(slot, data); |
| 1117 | return data; |
| 1118 | } |
| 1119 | |
| 1120 | uint32_t LocoNet::SetF13F44Bit(const unsigned char slot, const bool on, const unsigned char shift) |
| 1121 | { |
| 1122 | uint32_t data = locoCache.GetF13F44(slot); |
| 1123 | data &= (~(0x00000001u << shift)); |
| 1124 | data |= (on << shift); |
| 1125 | locoCache.SetF13F44(slot, data); |
| 1126 | return data; |
| 1127 | } |
| 1128 | } // namespace |
| 1129 | } // namespace |