Go to the documentation of this file.
69 #define DUMP_BUFFER_ON_ERROR 512 // this define doubles as the maximum buffer dump size, set to 0 to remove limit
70 #ifdef DUMP_BUFFER_ON_ERROR
87 : m_ignoreMaxMsgSize(false)
118 #ifdef DUMP_BUFFER_ON_ERROR
119 std::ostringstream ostr;
120 ostr << std::hex << std::setfill(
'0');
121 #if DUMP_BUFFER_ON_ERROR > 0
124 for (
XsSize i = 0; i < sz; ++i)
125 ostr <<
" " << std::setw(2) << (int) buff[i];
128 return std::string();
140 int bufferSize = (int)raw.size();
144 const unsigned char* buffer = raw.data();
147 for (
int pre = 0; pre < bufferSize; ++pre)
151 JLTRACEG(
"Preamble found at " << pre);
152 int remaining = bufferSize - pre;
156 JLTRACEG(
"Not enough header data read");
166 const uint8_t* msgStart = &(buffer[pre]);
180 JLTRACEG(
"Bytes in buffer=" << remaining <<
", full target = " << target);
185 JLALERTG(
"Invalid message length: " << target);
189 if (remaining < target)
192 JLTRACEG(
"Not enough data read: " << remaining <<
" / " << target);
204 if (rcv.loadFromString(msgStart, (uint16_t)target))
206 JLTRACEG(
"OK, size = " << (
int) rcv.getTotalMessageSize() <<
" buffer: " <<
dumpBuffer(msgStart, target));
207 rv.
m_size = (int) rcv.getTotalMessageSize();
213 " pre = " << pre <<
" msg " <<
dumpBuffer(msgStart, target) <<
214 " buffer " <<
dumpBuffer(buffer, bufferSize));
223 "Invalid checksum for msg at offset " << pre <<
" bufferSize = " << bufferSize
224 <<
" buffer at offset: " <<
dumpBuffer(raw.data() + pre, raw.size() - pre));
228 JLTRACEG(
"Invalid checksum, size = " << (
int)rcv.getTotalMessageSize() <<
" buffer: " <<
dumpBuffer(msgStart, target));
243 const unsigned char* buffer = raw.data();
244 const uint8_t* msgStart = &(buffer[location.
m_startPos]);
246 if (message.loadFromString(msgStart, (uint16_t)location.
m_size))
248 JLTRACEG(
"OK, size = " << (
int)message.getTotalMessageSize() <<
" buffer: " <<
dumpBuffer(msgStart, location.
m_size));
249 location.
m_size = (int)message.getTotalMessageSize();
280 if (
msg.getTotalMessageSize() < 5)
283 raw.assign(
msg.getTotalMessageSize(),
msg.getMessageStart());
284 return (
int) raw.size();
XsMessage convertToMessage(MessageLocation &location, const XsByteArray &raw) const override
Converts raw data using location into a XsMessage object.
A list of uint8_t values.
virtual ~ProtocolHandler()
Destructor.
int maximumMessageSize() const override
Returns the maximum size of a valid message of this protocol including preambles and checksums.
int type() const override
Returns the type of the protocol handler.
static int composeMessage(XsByteArray &raw, const XsMessage &msg)
Compose a message for transmission.
#define XS_LEN_MSGHEADERCS
int m_size
The size of the message, when less than 0 it indicates the expected message size.
#define DUMP_BUFFER_ON_ERROR
size_t XsSize
XsSize must be unsigned number!
#define XS_LEN_MSGEXTHEADERCS
static int expectedMessageSize(const unsigned char *buffer, int sz)
Compute the expected message size given a possibly incomplete message.
Structure for storing a single message.
@ XPT_Xbus
The Xsens Xbus protocol, enabled by default, always 0.
void ignoreMaximumMessageSize(bool ignore) override
Tells the protocol handler to ignore/expand its maximum message size.
XsProtocolType
Protocol types (XsDevice::enableProtocol())
std::string dumpBuffer(const uint8_t *buff, XsSize sz)
Write the contents of a uint8 buffer to string as hex characters.
ProtocolHandler()
Default constructor.
int minimumMessageSize() const override
Returns the minimum size of a valid message of this protocol including preambles and checksums.
Stores the location of a message in a buffer using a start position and a size.
int m_startPos
The offset of the first byte of the message or -1 if no message.
MessageLocation findMessage(XsProtocolType &type, const XsByteArray &raw) const override
Find the first message in the raw byte stream.