server: Z21 better checksum calculation
- Tweak XpressNet checksum code to accept custom message size - Renamed LanX::calcChecksum() to LanX::updateChecksum() - Added LanX::isChecksumValid() - Internally reuse XpressNet checksum code - Removed old commented function in LanXGetLocoInfo
Dieser Commit ist enthalten in:
Ursprung
313d3b2d57
Commit
14df2a7344
@ -25,10 +25,9 @@
|
||||
|
||||
namespace XpressNet {
|
||||
|
||||
uint8_t calcChecksum(const Message& msg)
|
||||
uint8_t calcChecksum(const Message& msg, const int dataSize)
|
||||
{
|
||||
const uint8_t* p = reinterpret_cast<const uint8_t*>(&msg);
|
||||
const int dataSize = msg.dataSize();
|
||||
uint8_t checksum = p[0];
|
||||
for(int i = 1; i <= dataSize; i++)
|
||||
checksum ^= p[i];
|
||||
@ -40,9 +39,9 @@ void updateChecksum(Message& msg)
|
||||
*(reinterpret_cast<uint8_t*>(&msg) + msg.dataSize() + 1) = calcChecksum(msg);
|
||||
}
|
||||
|
||||
bool isChecksumValid(const Message& msg)
|
||||
bool isChecksumValid(const Message& msg, const int dataSize)
|
||||
{
|
||||
return calcChecksum(msg) == *(reinterpret_cast<const uint8_t*>(&msg) + msg.dataSize() + 1);
|
||||
return calcChecksum(msg, dataSize) == *(reinterpret_cast<const uint8_t*>(&msg) + dataSize + 1);
|
||||
}
|
||||
|
||||
std::string toString(const Message& message, bool raw)
|
||||
|
||||
@ -40,9 +40,14 @@ constexpr uint8_t idFeedbackBroadcast = 0x40;
|
||||
|
||||
struct Message;
|
||||
|
||||
uint8_t calcChecksum(const Message& msg);
|
||||
inline uint8_t calcChecksum(const Message& msg);
|
||||
uint8_t calcChecksum(const Message& msg, const int dataSize);
|
||||
|
||||
void updateChecksum(Message& msg);
|
||||
bool isChecksumValid(const Message& msg);
|
||||
|
||||
inline bool isChecksumValid(const Message& msg);
|
||||
bool isChecksumValid(const Message& msg, const int dataSize);
|
||||
|
||||
std::string toString(const Message& message, bool raw = false);
|
||||
|
||||
struct Message
|
||||
@ -591,6 +596,16 @@ namespace RoSoftS88XpressNetLI
|
||||
};
|
||||
}
|
||||
|
||||
inline uint8_t calcChecksum(const Message& msg)
|
||||
{
|
||||
return calcChecksum(msg, msg.dataSize());
|
||||
}
|
||||
|
||||
inline bool isChecksumValid(const Message& msg)
|
||||
{
|
||||
return isChecksumValid(msg, msg.dataSize());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline bool operator ==(const XpressNet::Message& lhs, const XpressNet::Message& rhs)
|
||||
|
||||
@ -22,7 +22,6 @@
|
||||
|
||||
#include "clientkernel.hpp"
|
||||
#include "messages.hpp"
|
||||
#include "../xpressnet/messages.hpp"
|
||||
#include "../../decoder/decoder.hpp"
|
||||
#include "../../decoder/decoderchangeflags.hpp"
|
||||
#include "../../input/inputcontroller.hpp"
|
||||
@ -64,7 +63,7 @@ void ClientKernel::receive(const Message& message)
|
||||
{
|
||||
const auto& lanX = static_cast<const LanX&>(message);
|
||||
|
||||
if(!XpressNet::isChecksumValid(*reinterpret_cast<const XpressNet::Message*>(&lanX.xheader)))
|
||||
if(!LanX::isChecksumValid(lanX))
|
||||
break;
|
||||
|
||||
switch(lanX.xheader)
|
||||
@ -349,7 +348,7 @@ void ClientKernel::decoderChanged(const Decoder& decoder, DecoderChangeFlags cha
|
||||
cmd.setSpeedStep(speedStep);
|
||||
}
|
||||
|
||||
cmd.checksum = XpressNet::calcChecksum(*reinterpret_cast<const XpressNet::Message*>(&cmd.xheader));
|
||||
cmd.updateChecksum();
|
||||
postSend(cmd);
|
||||
}
|
||||
else if(has(changes, DecoderChangeFlags::FunctionValue))
|
||||
|
||||
@ -60,7 +60,7 @@ bool SimulationIOHandler::send(const Message& message)
|
||||
response.db1 |= Z21_CENTRALSTATE_EMERGENCYSTOP;
|
||||
if(!m_trackPowerOn)
|
||||
response.db1 |= Z21_CENTRALSTATE_TRACKVOLTAGEOFF;
|
||||
response.calcChecksum();
|
||||
response.updateChecksum();
|
||||
reply(response);
|
||||
}
|
||||
else if(message == LanXSetTrackPowerOn())
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include "messages.hpp"
|
||||
#include "../xpressnet/messages.hpp"
|
||||
#include "../../decoder/decoder.hpp"
|
||||
#include "../../../core/objectproperty.tpp"
|
||||
#include "../../../utils/tohex.hpp"
|
||||
@ -291,7 +292,21 @@ LanXLocoInfo::LanXLocoInfo(const Decoder& decoder) :
|
||||
setSpeedStep(Decoder::throttleToSpeedStep(decoder.throttle, speedSteps()));
|
||||
for(const auto &function : *decoder.functions)
|
||||
setFunction(function->number, function->value);
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
void LanX::updateChecksum(uint8_t len)
|
||||
{
|
||||
uint8_t val = XpressNet::calcChecksum(*reinterpret_cast<const XpressNet::Message*>(&xheader), len);
|
||||
uint8_t* checksum = &xheader + len + 1;
|
||||
*checksum = val;
|
||||
}
|
||||
|
||||
bool LanX::isChecksumValid(const LanX &lanX)
|
||||
{
|
||||
const XpressNet::Message& msg = *reinterpret_cast<const XpressNet::Message*>(&lanX.xheader);
|
||||
int dataSize = msg.dataSize();
|
||||
return XpressNet::isChecksumValid(msg, dataSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -265,18 +265,15 @@ struct LanX : Message
|
||||
{
|
||||
}
|
||||
|
||||
void calcChecksum(uint8_t len)
|
||||
void updateChecksum(uint8_t len);
|
||||
|
||||
inline void updateChecksum()
|
||||
{
|
||||
uint8_t* checksum = &xheader + len + 1;
|
||||
*checksum = xheader;
|
||||
for(uint8_t* db = &xheader + 1; db < checksum; db++)
|
||||
*checksum ^= *db;
|
||||
updateChecksum(xheader & 0x0F);
|
||||
}
|
||||
|
||||
inline void calcChecksum()
|
||||
{
|
||||
calcChecksum(xheader & 0x0F);
|
||||
}
|
||||
static bool isChecksumValid(const LanX& lanX);
|
||||
|
||||
} ATTRIBUTE_PACKED;
|
||||
static_assert(sizeof(LanX) == 5);
|
||||
|
||||
@ -402,7 +399,7 @@ struct LanXGetTurnoutInfo : LanX
|
||||
, db0(address >> 8)
|
||||
, db1(address & 0xFF)
|
||||
{
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
uint16_t address() const
|
||||
@ -436,7 +433,7 @@ struct LanXSetTurnout : LanX
|
||||
if(linearAddress & 0x0001)
|
||||
db2 |= db2Port;
|
||||
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
uint16_t address() const
|
||||
@ -490,7 +487,7 @@ struct LanXGetLocoInfo : LanX
|
||||
LanX(sizeof(LanXGetLocoInfo), 0xE3)
|
||||
{
|
||||
setAddress(address, longAddress);
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
inline uint16_t address() const
|
||||
@ -508,11 +505,6 @@ struct LanXGetLocoInfo : LanX
|
||||
addressHigh = longAddress ? (0xC0 | (address >> 8)) : 0x00;
|
||||
addressLow = longAddress ? address & 0xFF : address & 0x7F;
|
||||
}
|
||||
|
||||
/*inline void calcChecksum()
|
||||
{
|
||||
checksum = xheader ^ db0 ^ addressHigh ^ addressLow;
|
||||
}*/
|
||||
} ATTRIBUTE_PACKED;
|
||||
static_assert(sizeof(LanXGetLocoInfo) == 9);
|
||||
|
||||
@ -641,7 +633,7 @@ struct LanXSetLocoFunction : LanX
|
||||
setAddress(address, longAddress);
|
||||
setFunctionIndex(functionIndex);
|
||||
setSwitchType(value);
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
inline uint16_t address() const
|
||||
@ -926,7 +918,7 @@ struct LanXGetVersionReply : LanX
|
||||
{
|
||||
setXBusVersion(_xBusVersion);
|
||||
commandStationId = _commandStationId;
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
inline uint8_t xBusVersion() const
|
||||
@ -962,7 +954,7 @@ struct LanXGetFirmwareVersionReply : LanX
|
||||
{
|
||||
setVersionMajor(_major);
|
||||
setVersionMinor(_minor);
|
||||
calcChecksum();
|
||||
updateChecksum();
|
||||
}
|
||||
|
||||
inline uint8_t versionMajor() const
|
||||
@ -1271,7 +1263,7 @@ struct LanXLocoInfo : LanX
|
||||
}
|
||||
}
|
||||
|
||||
void calcChecksum()
|
||||
void updateChecksum()
|
||||
{
|
||||
checksum = xheader;
|
||||
for(uint8_t* db = &addressHigh; db < &checksum; db++)
|
||||
|
||||
@ -109,7 +109,7 @@ void ServerKernel::receiveFrom(const Message& message, IOHandler::ClientId clien
|
||||
response.db1 |= Z21_CENTRALSTATE_EMERGENCYSTOP;
|
||||
if(m_trackPowerOn != TriState::True)
|
||||
response.db1 |= Z21_CENTRALSTATE_TRACKVOLTAGEOFF;
|
||||
response.calcChecksum();
|
||||
response.updateChecksum();
|
||||
sendTo(response, clientId);
|
||||
}
|
||||
else if(message == LanXSetTrackPowerOn())
|
||||
|
||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren