diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 7e57a4f2..f2809f38 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -37,6 +37,8 @@ file(GLOB SOURCES "src/hardware/protocol/*.cpp" "src/hardware/protocol/loconet/*.hpp" "src/hardware/protocol/loconet/*.cpp" + "src/hardware/protocol/xpressnet/*.hpp" + "src/hardware/protocol/xpressnet/*.cpp" "src/train/*.hpp" "src/train/*.cpp" "src/vehicle/*.hpp" diff --git a/server/src/hardware/commandstation/rocoz21.cpp b/server/src/hardware/commandstation/rocoz21.cpp index 6a3ad0d6..532a8e1a 100644 --- a/server/src/hardware/commandstation/rocoz21.cpp +++ b/server/src/hardware/commandstation/rocoz21.cpp @@ -25,8 +25,7 @@ #include "../../world/world.hpp" #include "../../core/eventloop.hpp" #include "../decoder/decoderchangeflags.hpp" -#include "../protocol/xpressnet.hpp" - +#include "../protocol/xpressnet/messages.hpp" #include "../protocol/z21.hpp" #include "../../utils/to_hex.hpp" @@ -201,7 +200,7 @@ void RocoZ21::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, if(decoder.direction.value() == Direction::Forward) cmd.speedAndDirection |= 0x80; - cmd.checksum = XpressNet::calcChecksum(&cmd.xheader); + cmd.checksum = XpressNet::calcChecksum(*reinterpret_cast(&cmd.xheader)); send(&cmd); } else if(has(changes, DecoderChangeFlags::FunctionValue)) @@ -215,7 +214,7 @@ void RocoZ21::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, cmd.header = Z21_LAN_X; SET_ADDRESS; cmd.db3 = (f->value ? 0x40 : 0x00) | static_cast(functionNumber); - cmd.checksum = XpressNet::calcChecksum(&cmd.xheader); + cmd.checksum = XpressNet::calcChecksum(*reinterpret_cast(&cmd.xheader)); send(&cmd); } } diff --git a/server/src/hardware/commandstation/usbxpressnetinterface.cpp b/server/src/hardware/commandstation/usbxpressnetinterface.cpp index 6c79b5e2..77b069e5 100644 --- a/server/src/hardware/commandstation/usbxpressnetinterface.cpp +++ b/server/src/hardware/commandstation/usbxpressnetinterface.cpp @@ -30,7 +30,7 @@ USBXpressNetInterface::USBXpressNetInterface(const std::weak_ptr& world, xpressnet{this, "xpressnet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject} { name = "USB XpressNet interface"; - xpressnet.setValueInternal(std::make_shared(*this, xpressnet.name(), std::bind(&USBXpressNetInterface::send, this, std::placeholders::_1))); + xpressnet.setValueInternal(std::make_shared(*this, xpressnet.name(), std::bind(&USBXpressNetInterface::send, this, std::placeholders::_1))); m_interfaceItems.insertBefore(serial, notes); m_interfaceItems.insertBefore(address, notes); diff --git a/server/src/hardware/commandstation/usbxpressnetinterface.hpp b/server/src/hardware/commandstation/usbxpressnetinterface.hpp index b58dccde..7d4b47eb 100644 --- a/server/src/hardware/commandstation/usbxpressnetinterface.hpp +++ b/server/src/hardware/commandstation/usbxpressnetinterface.hpp @@ -24,7 +24,7 @@ #define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_USBXPRESSNETINTERFACE_HPP #include "commandstation.hpp" -#include "../protocol/xpressnet.hpp" +#include "../protocol/xpressnet/xpressnet.hpp" #include class USBXpressNetInterface : public CommandStation @@ -48,7 +48,7 @@ class USBXpressNetInterface : public CommandStation Property serial; Property address; - ObjectProperty<::XpressNet> xpressnet; + ObjectProperty xpressnet; USBXpressNetInterface(const std::weak_ptr& world, std::string_view _id); ~USBXpressNetInterface() final; diff --git a/server/src/hardware/commandstation/xpressnetserial.cpp b/server/src/hardware/commandstation/xpressnetserial.cpp index 165dc21e..77d90b59 100644 --- a/server/src/hardware/commandstation/xpressnetserial.cpp +++ b/server/src/hardware/commandstation/xpressnetserial.cpp @@ -55,29 +55,12 @@ XpressNetSerial::XpressNetSerial(const std::weak_ptr& world, std::string_ xpressnet{this, "xpressnet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject} { name = "XpressNet (serial)"; - xpressnet.setValueInternal(std::make_shared(*this, xpressnet.name(), std::bind(&XpressNetSerial::send, this, std::placeholders::_1))); + xpressnet.setValueInternal(std::make_shared(*this, xpressnet.name(), std::bind(&XpressNetSerial::send, this, std::placeholders::_1))); m_interfaceItems.insertBefore(interface, baudrate); m_interfaceItems.insertBefore(xpressnet, notes); } -/* -bool XpressNetSerial::setOnline(bool& value) -{ - if(!m_serialPort.is_open() && value) - { - if(!start()) - { - value = false; - return false; - } - read(); - } - else if(m_serialPort.is_open() && !value) - stop(); - return true; -} -*/ void XpressNetSerial::emergencyStopChanged(bool value) { CommandStation::emergencyStopChanged(value); @@ -101,32 +84,7 @@ void XpressNetSerial::decoderChanged(const Decoder& decoder, DecoderChangeFlags if(online) xpressnet->decoderChanged(decoder, changes, functionNumber); } -/* -bool XpressNetSerial::start() -{ - boost::system::error_code ec; - m_serialPort.open(port, ec); - if(ec) - { - logError("open: " + ec.message()); - return false; - } - m_serialPort.set_option(boost::asio::serial_port_base::baud_rate(baudrate)); - m_serialPort.set_option(boost::asio::serial_port_base::character_size(8)); - m_serialPort.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one)); - m_serialPort.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none)); - m_serialPort.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::hardware)); - - return true; -} - -void XpressNetSerial::stop() -{ - // send bus off cmd - m_serialPort.close(); -} -*/ bool XpressNetSerial::send(const XpressNet::Message& msg) { assert(XpressNet::isChecksumValid(msg)); diff --git a/server/src/hardware/commandstation/xpressnetserial.hpp b/server/src/hardware/commandstation/xpressnetserial.hpp index f9eac621..40d9db18 100644 --- a/server/src/hardware/commandstation/xpressnetserial.hpp +++ b/server/src/hardware/commandstation/xpressnetserial.hpp @@ -20,42 +20,29 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_LI10X_HPP -#define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_LI10X_HPP +#ifndef TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_XPRESSNETSERIAL_HPP +#define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_XPRESSNETSERIAL_HPP #include "serialcommandstation.hpp" #include "../../enum/xpressnetserialinterface.hpp" -#include "../protocol/xpressnet.hpp" -//#include -//#include "../../core/objectproperty.hpp" -//#include "protocol/xpressnet.hpp" +#include "../protocol/xpressnet/xpressnet.hpp" class XpressNetSerial : public SerialCommandStation { protected: - //boost::asio::serial_port m_serialPort; - //std::array m_readBuffer; - //std::unique_ptr m_readMessage; - //uint8_t m_readMessageTodo; - //uint8_t m_readMessagePos; - - //bool setOnline(bool& value) final; void emergencyStopChanged(bool value) final; void trackVoltageOffChanged(bool value) final; void decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber) final; - bool start(); - void stop(); bool send(const XpressNet::Message& msg); - void receive(std::unique_ptr message); - void read(); + void read() final; public: CLASS_ID("command_station.xpressnet_serial") CREATE(XpressNetSerial) Property interface; - ObjectProperty xpressnet; + ObjectProperty xpressnet; XpressNetSerial(const std::weak_ptr& world, std::string_view _id); }; diff --git a/server/src/hardware/protocol/xpressnet.hpp b/server/src/hardware/protocol/xpressnet.hpp deleted file mode 100644 index df6dee0b..00000000 --- a/server/src/hardware/protocol/xpressnet.hpp +++ /dev/null @@ -1,370 +0,0 @@ -/** - * server/src/hardware/protocol/xpressnet.hpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 2019-2020 Reinder Feenstra - * - * This program 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 2 - * of the License, or (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_HPP -#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_HPP - -#include "../../core/subobject.hpp" -#include "../../core/property.hpp" -#include "../../enum/direction.hpp" -#include "../../enum/xpressnetcommandstation.hpp" -#include "../../hardware/decoder/decoderchangeflags.hpp" - -class CommandStation; -class Decoder; - -class XpressNet : public SubObject -{ - public: - struct Message; - - static uint8_t calcChecksum(const void* msg); - static uint8_t calcChecksum(const Message& msg); - static bool isChecksumValid(const Message& msg); - - struct Message - { - uint8_t header; - - uint8_t identification() const - { - return header & 0xF0; - } - - uint8_t dataSize() const - { - return header & 0x0F; - } - - uint8_t size() const - { - return 2 + dataSize(); - } - }; - static_assert(sizeof(Message) == 1); - - struct NormalOperationResumed : Message - { - uint8_t db1 = 0x01; - uint8_t checksum = 0x60; - - NormalOperationResumed() - { - header = 0x61; - } - }; - static_assert(sizeof(NormalOperationResumed) == 3); - - struct TrackPowerOff : Message - { - uint8_t db1 = 0x00; - uint8_t checksum = 0x61; - - TrackPowerOff() - { - header = 0x61; - } - }; - static_assert(sizeof(TrackPowerOff) == 3); - - struct EmergencyStop : Message - { - uint8_t db1 = 0x00; - uint8_t checksum = 0x81; - - EmergencyStop() - { - header = 0x81; - } - }; - static_assert(sizeof(EmergencyStop) == 3); - - struct EmergencyStopLocomotive : Message - { - uint8_t addressHigh; - uint8_t addressLow; - uint8_t checksum; - - EmergencyStopLocomotive(uint16_t address, bool longAddress) - { - header = 0x92; - if(longAddress) - { - assert(address >= 1 && address <= 9999); - addressHigh = 0x00; - addressLow = address & 0x7f; - } - else - { - assert(address >= 1 && address <= 127); - addressHigh = 0x00; - addressLow = address & 0x7f; - } - } - }; - static_assert(sizeof(EmergencyStopLocomotive) == 4); - - struct LocomotiveInstruction : Message - { - uint8_t identification; - uint8_t addressHigh; - uint8_t addressLow; - - LocomotiveInstruction(uint16_t address, bool longAddress) - { - header = 0xE4; - if(longAddress) - { - assert(address >= 1 && address <= 9999); - addressHigh = 0xc0 | address >> 8; - addressLow = address & 0xff; - } - else - { - assert(address >= 1 && address <= 127); - addressHigh = 0x00; - addressLow = address & 0x7f; - } - } - }; - static_assert(sizeof(LocomotiveInstruction) == 4); - - struct SpeedAndDirectionInstruction : LocomotiveInstruction - { - uint8_t speedAndDirection; - uint8_t checksum; - - SpeedAndDirectionInstruction(uint16_t address, bool longAddress, bool emergencyStop, Direction direction) : - LocomotiveInstruction(address, longAddress) - { - speedAndDirection = emergencyStop ? 0x01 : 0x00; - if(direction == Direction::Forward) - speedAndDirection |= 0x80; - } - }; - - struct SpeedAndDirectionInstruction14 : SpeedAndDirectionInstruction - { - SpeedAndDirectionInstruction14(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep, bool fl) : - SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) - { - assert(speedStep <= 14); - identification = 0x10; - if(!emergencyStop && speedStep > 0) - speedAndDirection |= speedStep + 1; - if(fl) - speedAndDirection |= 0x10; - checksum = calcChecksum(*this); - } - }; - - struct SpeedAndDirectionInstruction27 : SpeedAndDirectionInstruction - { - SpeedAndDirectionInstruction27(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) : - SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) - { - assert(speedStep <= 27); - identification = 0x11; - if(!emergencyStop && speedStep > 0) - speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1); - checksum = calcChecksum(*this); - } - }; - - struct SpeedAndDirectionInstruction28 : SpeedAndDirectionInstruction - { - SpeedAndDirectionInstruction28(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) : - SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) - { - assert(speedStep <= 28); - identification = 0x12; - if(!emergencyStop && speedStep > 0) - speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1); - checksum = calcChecksum(*this); - } - }; - - struct SpeedAndDirectionInstruction128 : SpeedAndDirectionInstruction - { - SpeedAndDirectionInstruction128(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) : - SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) - { - assert(speedStep <= 126); - identification = 0x13; - if(!emergencyStop && speedStep > 0) - speedAndDirection |= speedStep + 1; - checksum = calcChecksum(*this); - } - }; - - struct FunctionInstructionGroup : LocomotiveInstruction - { - uint8_t functions = 0x00; - uint8_t checksum; - - FunctionInstructionGroup(uint16_t address, bool longAddress, uint8_t group) : - LocomotiveInstruction(address, longAddress) - { - assert(group >= 1 && group <= 3); - identification = 0x1F + group; - } - }; - - struct FunctionInstructionGroup1 : FunctionInstructionGroup - { - FunctionInstructionGroup1(uint16_t address, bool longAddress, bool f0, bool f1, bool f2, bool f3, bool f4) : - FunctionInstructionGroup(address, longAddress, 1) - { - if(f0) - functions |= 0x10; - if(f1) - functions |= 0x01; - if(f2) - functions |= 0x02; - if(f3) - functions |= 0x04; - if(f4) - functions |= 0x08; - - checksum = calcChecksum(*this); - } - }; - - struct FunctionInstructionGroup2 : FunctionInstructionGroup - { - FunctionInstructionGroup2(uint16_t address, bool longAddress, bool f5, bool f6, bool f7, bool f8) : - FunctionInstructionGroup(address, longAddress, 2) - { - if(f5) - functions |= 0x01; - if(f6) - functions |= 0x02; - if(f7) - functions |= 0x04; - if(f8) - functions |= 0x08; - - checksum = calcChecksum(*this); - } - }; - - struct FunctionInstructionGroup3 : FunctionInstructionGroup - { - FunctionInstructionGroup3(uint16_t address, bool longAddress, bool f9, bool f10, bool f11, bool f12) : - FunctionInstructionGroup(address, longAddress, 3) - { - if(f9) - functions |= 0x01; - if(f10) - functions |= 0x02; - if(f11) - functions |= 0x04; - if(f12) - functions |= 0x08; - - checksum = calcChecksum(*this); - } - }; - - /* - struct setFunctionStateGroup : LocomotiveInstruction - { - uint8_t state = 0x00; - uint8_t checksum; - - setFunctionStateGroup(uint8_t group, uint16_t address) - { - assert(group >= 1 && group <= 3); - header = 0xE4; - identification = 0x23 + group; - addressLowHigh(address, addressLow, addressHigh); - } - } __attribute__((packed)); - */ - - struct RocoFunctionInstructionF13F20 : LocomotiveInstruction - { - uint8_t functions = 0x00; - uint8_t checksum; - - RocoFunctionInstructionF13F20(uint16_t address, bool longAddress, bool f13, bool f14, bool f15, bool f16, bool f17, bool f18, bool f19, bool f20) : - LocomotiveInstruction(address, longAddress) - { - identification = 0xF3; - - if(f13) - functions |= 0x01; - if(f14) - functions |= 0x02; - if(f15) - functions |= 0x04; - if(f16) - functions |= 0x08; - if(f17) - functions |= 0x10; - if(f18) - functions |= 0x20; - if(f19) - functions |= 0x40; - if(f20) - functions |= 0x80; - - checksum = calcChecksum(*this); - } - }; - - protected: - static bool getFunctionValue(const Decoder& decoder, uint32_t number); - - CommandStation* const m_commandStation; // valid if parent is command station, else nullptr - std::function m_send; - std::atomic_bool m_debugLog; - - void worldEvent(WorldState state, WorldEvent event) final; - - public: - CLASS_ID("protocol.xpressnet") - - Property commandStation; - Property useEmergencyStopLocomotiveCommand; - Property useFunctionStateCommands; - Property useRocoF13F20Command; - Property debugLog; - - XpressNet(Object& _parent, const std::string& parentPropertyName, std::function send); - - bool send(const Message& msg) { return m_send(msg); } - void receive(const Message& msg); - - void emergencyStopChanged(bool value); - void trackVoltageOffChanged(bool value); - void decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber); -}; - -inline bool operator ==(const XpressNet::Message& lhs, const XpressNet::Message& rhs) -{ - return lhs.size() == rhs.size() && std::memcmp(&lhs, &rhs, lhs.size()) == 0; -} - -std::string to_string(const XpressNet::Message& message, bool raw = false); - -#endif diff --git a/server/src/hardware/protocol/xpressnet/messages.cpp b/server/src/hardware/protocol/xpressnet/messages.cpp new file mode 100644 index 00000000..e58ce7c3 --- /dev/null +++ b/server/src/hardware/protocol/xpressnet/messages.cpp @@ -0,0 +1,70 @@ +/** + * server/src/hardware/protocol/xpressnet/messages.cpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2019-2020 Reinder Feenstra + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "messages.hpp" +#include "../../../utils/to_hex.hpp" + +namespace XpressNet { + +uint8_t calcChecksum(const Message& msg) +{ + const uint8_t* p = reinterpret_cast(&msg); + const int dataSize = msg.dataSize(); + uint8_t checksum = p[0]; + for(int i = 1; i <= dataSize; i++) + checksum ^= p[i]; + return checksum; +} + +bool isChecksumValid(const Message& msg) +{ + return calcChecksum(msg) == *(reinterpret_cast(&msg) + msg.dataSize() + 1); +} + +std::string to_string(const Message& message, bool raw) +{ + std::string s; + + switch(message.identification()) + { + default: + raw = true; + break; + } + + if(raw) + { + s.append("["); + const uint8_t* bytes = reinterpret_cast(&message); + for(int i = 0; i < message.size(); i++) + { + if(i != 0) + s.append(" "); + s.append(to_hex(bytes[i])); + } + s.append("]"); + } + + return s; +} + +} diff --git a/server/src/hardware/protocol/xpressnet/messages.hpp b/server/src/hardware/protocol/xpressnet/messages.hpp new file mode 100644 index 00000000..0661c276 --- /dev/null +++ b/server/src/hardware/protocol/xpressnet/messages.hpp @@ -0,0 +1,337 @@ +/** + * server/src/hardware/protocol/xpressnet/messages.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2019-2020 Reinder Feenstra + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_MESSAGES_HPP +#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_MESSAGES_HPP + +#include +#include +#include +#include "../../../enum/direction.hpp" + +namespace XpressNet { + +struct Message; + +uint8_t calcChecksum(const Message& msg); +bool isChecksumValid(const Message& msg); +std::string to_string(const Message& message, bool raw = false); + +struct Message +{ + uint8_t header; + + uint8_t identification() const + { + return header & 0xF0; + } + + uint8_t dataSize() const + { + return header & 0x0F; + } + + uint8_t size() const + { + return 2 + dataSize(); + } +}; +static_assert(sizeof(Message) == 1); + +struct NormalOperationResumed : Message +{ + uint8_t db1 = 0x01; + uint8_t checksum = 0x60; + + NormalOperationResumed() + { + header = 0x61; + } +}; +static_assert(sizeof(NormalOperationResumed) == 3); + +struct TrackPowerOff : Message +{ + uint8_t db1 = 0x00; + uint8_t checksum = 0x61; + + TrackPowerOff() + { + header = 0x61; + } +}; +static_assert(sizeof(TrackPowerOff) == 3); + +struct EmergencyStop : Message +{ + uint8_t db1 = 0x00; + uint8_t checksum = 0x81; + + EmergencyStop() + { + header = 0x81; + } +}; +static_assert(sizeof(EmergencyStop) == 3); + +struct EmergencyStopLocomotive : Message +{ + uint8_t addressHigh; + uint8_t addressLow; + uint8_t checksum; + + EmergencyStopLocomotive(uint16_t address, bool longAddress) + { + header = 0x92; + if(longAddress) + { + assert(address >= 1 && address <= 9999); + addressHigh = 0x00; + addressLow = address & 0x7f; + } + else + { + assert(address >= 1 && address <= 127); + addressHigh = 0x00; + addressLow = address & 0x7f; + } + } +}; +static_assert(sizeof(EmergencyStopLocomotive) == 4); + +struct LocomotiveInstruction : Message +{ + uint8_t identification; + uint8_t addressHigh; + uint8_t addressLow; + + LocomotiveInstruction(uint16_t address, bool longAddress) + { + header = 0xE4; + if(longAddress) + { + assert(address >= 1 && address <= 9999); + addressHigh = 0xc0 | address >> 8; + addressLow = address & 0xff; + } + else + { + assert(address >= 1 && address <= 127); + addressHigh = 0x00; + addressLow = address & 0x7f; + } + } +}; +static_assert(sizeof(LocomotiveInstruction) == 4); + +struct SpeedAndDirectionInstruction : LocomotiveInstruction +{ + uint8_t speedAndDirection; + uint8_t checksum; + + SpeedAndDirectionInstruction(uint16_t address, bool longAddress, bool emergencyStop, Direction direction) : + LocomotiveInstruction(address, longAddress) + { + speedAndDirection = emergencyStop ? 0x01 : 0x00; + if(direction == Direction::Forward) + speedAndDirection |= 0x80; + } +}; + +struct SpeedAndDirectionInstruction14 : SpeedAndDirectionInstruction +{ + SpeedAndDirectionInstruction14(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep, bool fl) : + SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) + { + assert(speedStep <= 14); + identification = 0x10; + if(!emergencyStop && speedStep > 0) + speedAndDirection |= speedStep + 1; + if(fl) + speedAndDirection |= 0x10; + checksum = calcChecksum(*this); + } +}; + +struct SpeedAndDirectionInstruction27 : SpeedAndDirectionInstruction +{ + SpeedAndDirectionInstruction27(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) : + SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) + { + assert(speedStep <= 27); + identification = 0x11; + if(!emergencyStop && speedStep > 0) + speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1); + checksum = calcChecksum(*this); + } +}; + +struct SpeedAndDirectionInstruction28 : SpeedAndDirectionInstruction +{ + SpeedAndDirectionInstruction28(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) : + SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) + { + assert(speedStep <= 28); + identification = 0x12; + if(!emergencyStop && speedStep > 0) + speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1); + checksum = calcChecksum(*this); + } +}; + +struct SpeedAndDirectionInstruction128 : SpeedAndDirectionInstruction +{ + SpeedAndDirectionInstruction128(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) : + SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction) + { + assert(speedStep <= 126); + identification = 0x13; + if(!emergencyStop && speedStep > 0) + speedAndDirection |= speedStep + 1; + checksum = calcChecksum(*this); + } +}; + +struct FunctionInstructionGroup : LocomotiveInstruction +{ + uint8_t functions = 0x00; + uint8_t checksum; + + FunctionInstructionGroup(uint16_t address, bool longAddress, uint8_t group) : + LocomotiveInstruction(address, longAddress) + { + assert(group >= 1 && group <= 3); + identification = 0x1F + group; + } +}; + +struct FunctionInstructionGroup1 : FunctionInstructionGroup +{ + FunctionInstructionGroup1(uint16_t address, bool longAddress, bool f0, bool f1, bool f2, bool f3, bool f4) : + FunctionInstructionGroup(address, longAddress, 1) + { + if(f0) + functions |= 0x10; + if(f1) + functions |= 0x01; + if(f2) + functions |= 0x02; + if(f3) + functions |= 0x04; + if(f4) + functions |= 0x08; + + checksum = calcChecksum(*this); + } +}; + +struct FunctionInstructionGroup2 : FunctionInstructionGroup +{ + FunctionInstructionGroup2(uint16_t address, bool longAddress, bool f5, bool f6, bool f7, bool f8) : + FunctionInstructionGroup(address, longAddress, 2) + { + if(f5) + functions |= 0x01; + if(f6) + functions |= 0x02; + if(f7) + functions |= 0x04; + if(f8) + functions |= 0x08; + + checksum = calcChecksum(*this); + } +}; + +struct FunctionInstructionGroup3 : FunctionInstructionGroup +{ + FunctionInstructionGroup3(uint16_t address, bool longAddress, bool f9, bool f10, bool f11, bool f12) : + FunctionInstructionGroup(address, longAddress, 3) + { + if(f9) + functions |= 0x01; + if(f10) + functions |= 0x02; + if(f11) + functions |= 0x04; + if(f12) + functions |= 0x08; + + checksum = calcChecksum(*this); + } +}; + +/* +struct setFunctionStateGroup : LocomotiveInstruction +{ + uint8_t state = 0x00; + uint8_t checksum; + + setFunctionStateGroup(uint8_t group, uint16_t address) + { + assert(group >= 1 && group <= 3); + header = 0xE4; + identification = 0x23 + group; + addressLowHigh(address, addressLow, addressHigh); + } +} __attribute__((packed)); +*/ + +struct RocoFunctionInstructionF13F20 : LocomotiveInstruction +{ + uint8_t functions = 0x00; + uint8_t checksum; + + RocoFunctionInstructionF13F20(uint16_t address, bool longAddress, bool f13, bool f14, bool f15, bool f16, bool f17, bool f18, bool f19, bool f20) : + LocomotiveInstruction(address, longAddress) + { + identification = 0xF3; + + if(f13) + functions |= 0x01; + if(f14) + functions |= 0x02; + if(f15) + functions |= 0x04; + if(f16) + functions |= 0x08; + if(f17) + functions |= 0x10; + if(f18) + functions |= 0x20; + if(f19) + functions |= 0x40; + if(f20) + functions |= 0x80; + + checksum = calcChecksum(*this); + } +}; + +} + +inline bool operator ==(const XpressNet::Message& lhs, const XpressNet::Message& rhs) +{ + return lhs.size() == rhs.size() && std::memcmp(&lhs, &rhs, lhs.size()) == 0; +} + +#endif diff --git a/server/src/hardware/protocol/xpressnet.cpp b/server/src/hardware/protocol/xpressnet/xpressnet.cpp similarity index 86% rename from server/src/hardware/protocol/xpressnet.cpp rename to server/src/hardware/protocol/xpressnet/xpressnet.cpp index 85e6ca1c..b39b3f35 100644 --- a/server/src/hardware/protocol/xpressnet.cpp +++ b/server/src/hardware/protocol/xpressnet/xpressnet.cpp @@ -1,5 +1,5 @@ /** - * server/src/hardware/protocol/xpressnet.cpp + * server/src/hardware/protocol/xpressnet/xpressnet.cpp * * This file is part of the traintastic source code. * @@ -21,31 +21,11 @@ */ #include "xpressnet.hpp" -#include "../../core/traintastic.hpp" -#include "../../core/eventloop.hpp" -#include "../decoder/decoder.hpp" -#include "../../utils/to_hex.hpp" +#include "../../../core/traintastic.hpp" +#include "../../../core/eventloop.hpp" +#include "../../decoder/decoder.hpp" -uint8_t XpressNet::calcChecksum(const void* msg) -{ - assert(msg); - return calcChecksum(*reinterpret_cast(msg)); -} - -uint8_t XpressNet::calcChecksum(const Message& msg) -{ - const uint8_t* p = reinterpret_cast(&msg); - const int dataSize = msg.dataSize(); - uint8_t checksum = p[0]; - for(int i = 1; i <= dataSize; i++) - checksum ^= p[i]; - return checksum; -} - -bool XpressNet::isChecksumValid(const Message& msg) -{ - return calcChecksum(msg) == *(reinterpret_cast(&msg) + msg.dataSize() + 1); -} +namespace XpressNet { XpressNet::XpressNet(Object& _parent, const std::string& parentPropertyName, std::function send) : SubObject(_parent, parentPropertyName), @@ -272,30 +252,4 @@ void XpressNet::decoderChanged(const Decoder& decoder, DecoderChangeFlags change } } - -std::string to_string(const XpressNet::Message& message, bool raw) -{ - std::string s; - - switch(message.identification()) - { - default: - raw = true; - break; - } - - if(raw) - { - s.append("["); - const uint8_t* bytes = reinterpret_cast(&message); - for(int i = 0; i < message.size(); i++) - { - if(i != 0) - s.append(" "); - s.append(to_hex(bytes[i])); - } - s.append("]"); - } - - return s; } diff --git a/server/src/hardware/protocol/xpressnet/xpressnet.hpp b/server/src/hardware/protocol/xpressnet/xpressnet.hpp new file mode 100644 index 00000000..ec1d3385 --- /dev/null +++ b/server/src/hardware/protocol/xpressnet/xpressnet.hpp @@ -0,0 +1,69 @@ +/** + * server/src/hardware/protocol/xpressnet/xpressnet.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2019-2020 Reinder Feenstra + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_XPRESSNET_HPP +#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_XPRESSNET_HPP + +#include "../../../core/subobject.hpp" +#include "../../../core/property.hpp" +#include "../../../enum/xpressnetcommandstation.hpp" +#include "../../../hardware/decoder/decoderchangeflags.hpp" +#include "messages.hpp" + +class CommandStation; +class Decoder; + +namespace XpressNet { + +class XpressNet : public SubObject +{ + protected: + static bool getFunctionValue(const Decoder& decoder, uint32_t number); + + CommandStation* const m_commandStation; // valid if parent is command station, else nullptr + std::function m_send; + std::atomic_bool m_debugLog; + + void worldEvent(WorldState state, WorldEvent event) final; + + public: + CLASS_ID("protocol.xpressnet") + + Property commandStation; + Property useEmergencyStopLocomotiveCommand; + Property useFunctionStateCommands; + Property useRocoF13F20Command; + Property debugLog; + + XpressNet(Object& _parent, const std::string& parentPropertyName, std::function send); + + bool send(const Message& msg) { return m_send(msg); } + void receive(const Message& msg); + + void emergencyStopChanged(bool value); + void trackVoltageOffChanged(bool value); + void decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber); +}; + +} + +#endif