Newer
Older
#include <string.h>
#ifndef ROBOTHW
#include <QDebug>
#endif
KrabiPacket::KrabiPacket(uint8_t id, W_TABLE watch) : mId(id), mCursor(1), mLength(2), mLengthSecurized(0), mValid(true)
KrabiPacket::KrabiPacket(uint8_t *data, uint8_t size) : mCursor(1), mLength(0), mLengthSecurized(size)
memcpy(mPacketSecurized, data, mLengthSecurized);
unsecurize();
mId = mPacket[0];
checkValidity();
#ifndef ROBOTHW
KrabiPacket::KrabiPacket(QByteArray data) : mCursor(1), mLength(0)
#ifdef BLUETOOTH
uint8_t* array = (uint8_t*)(data.toStdString().c_str());
mLengthSecurized = data.size();
memcpy(mPacketSecurized, array, mLengthSecurized);
mId = mPacket[0];
checkValidity();
#endif
QByteArray q((char*) data(), length());
return q;
uint8_t checksum = mPacket[mLength - 1];
uint8_t computedChecksum = 0;
for(int i(0); i < mLength - 1; i++)
computedChecksum += mPacket[i];
mValid = (checksum == computedChecksum);
#ifndef ROBOTHW
if (!mValid)
qDebug() << "ERROR : " << checksum << " != " << computedChecksum;
#endif
{
if (mCursor + size + KRABIPACKET_SUFFIX_SIZE > KRABIPACKET_MAXSIZE)
return;
memcpy(mPacket + mCursor, data, size);
mCursor += size;
mLength += size;
void KrabiPacket::copyData(void* dest, uint8_t size)
if (mCursor + size >= mLength)
return;
memcpy(dest, mPacket + mCursor, size);
mCursor += size;
int size = 0;
while( data[size] != '\0' )
size++;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
addData(data, size + 1);
}
char* KrabiPacket::getString()
{
char* ptr = (char*)(mPacket + mCursor);
while( mPacket[mCursor] != '\0' )
mCursor++;
return ptr;
}
void KrabiPacket::setId(uint8_t id)
{
mId = id;
}
uint8_t KrabiPacket::id()
{
return mId;
}
uint8_t KrabiPacket::length()
{
securize();
return mLengthSecurized;
}
void KrabiPacket::securize()
{
// securize (delete CR + LF)
mLengthSecurized = 0;
for(int i(0); i < mLength; i++)
if (mPacket[i] == 0x0A || mPacket[i] == 0xFF)
mPacketSecurized[mLengthSecurized++] = 0xFF;
mPacketSecurized[mLengthSecurized++] = mPacket[i];
}
void KrabiPacket::unsecurize()
{
mLength = 0;
for(int i(0); i < mLengthSecurized; i++)
if (mPacketSecurized[i] == 0xFF)
i++;
mPacket[mLength++] = mPacketSecurized[i];
}
uint8_t* KrabiPacket::data()
{
// checksum
uint8_t checksum = 0;
for(int i(0); i < mLength - 1; i++)
checksum += mPacket[i];
mPacket[mLength - 1] = checksum;
securize();