RoboidControl-cpp/Messages/ModelUrlMsg.cpp
2025-02-19 17:54:55 +01:00

55 lines
1.5 KiB
C++

#include "ModelUrlMsg.h"
#include <string.h>
namespace Passer {
namespace RoboidControl {
// ModelUrlMsg::ModelUrlMsg(unsigned char networkId, unsigned char thingId,
// unsigned char urlLength, const char *url,
// float scale) {
// this->networkId = networkId;
// this->thingId = thingId;
// this->urlLength = urlLength;
// this->url = url;
// this->scale = scale;
// }
ModelUrlMsg::ModelUrlMsg(const char *buffer) {
unsigned char ix = 1; // first byte is msg id
this->networkId = buffer[ix++];
this->thingId = buffer[ix++];
this->urlLength = buffer[ix++];
this->url = &buffer[ix]; // dangerous! name should not be used anymore after
// buffer has been re-used...
}
ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing *thing) {
this->networkId = networkId;
this->thingId = thing->id;
if (thing->modelUrl == nullptr)
this->urlLength = 0;
else
this->urlLength = strlen(thing->modelUrl);
this->url = thing->modelUrl; // dangerous!
}
ModelUrlMsg::~ModelUrlMsg() {}
unsigned char ModelUrlMsg::Serialize(char *buffer) {
if (this->urlLength == 0 || this->url == nullptr)
return 0;
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
// LowLevelMessages::SendFloat16(buffer, &ix, this->scale);
buffer[ix++] = this->urlLength;
for (int urlIx = 0; urlIx < this->urlLength; urlIx++)
buffer[ix++] = url[urlIx];
return ix;
}
} // namespace RoboidControl
} // namespace Passer