RoboidControl-cpp/Messages/CustomMsg.cpp
2025-01-26 20:41:34 +01:00

42 lines
1012 B
C++

#include "CustomMsg.h"
namespace Passer {
namespace Control {
CustomMsg::CustomMsg(char *buffer) {
unsigned char ix = 1;
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
this->bytes =
buffer + ix; // This is only valid because the code ensures the the msg
// lifetime is shorter than the buffer lifetime...
}
CustomMsg::CustomMsg(unsigned char networkId, Thing *thing) {
this->networkId = networkId;
this->thingId = thing->id;
this->thing = thing;
}
CustomMsg::~CustomMsg() {}
unsigned char CustomMsg::Serialize(char *buffer) {
unsigned char ix = this->length;
this->thing->SendBytes(buffer, &ix);
if (ix <= this->length) // in this case, no data is actually sent
return 0;
buffer[0] = this->id;
buffer[1] = this->networkId;
buffer[2] = this->thingId;
return ix;
}
CustomMsg CustomMsg::Receive(char *buffer, unsigned char bufferSize) {
CustomMsg msg = CustomMsg(buffer);
return msg;
}
} // namespace Control
} // namespace Passer