RoboidControl-cpp/Messages/ModelUrlMsg.cpp
2025-02-24 17:42:37 +01:00

69 lines
2.0 KiB
C++

#include "ModelUrlMsg.h"
#include <string.h>
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(unsigned char networkId, Thing* thing) {
this->networkId = networkId;
this->thingId = thing->id;
if (thing->modelUrl == nullptr)
this->urlLength = 0;
else
this->urlLength = (unsigned char)strlen(thing->modelUrl);
//this->url = thing->modelUrl; // dangerous!
// the url string in the buffer is not \0 terminated!
char* url = new char[this->urlLength + 1];
for (int i = 0; i < this->urlLength; i++)
url[i] = thing->modelUrl[i];
url[this->urlLength] = '\0';
this->url = url;}
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...
// the url string in the buffer is not \0 terminated!
char* url = new char[this->urlLength + 1];
for (int i = 0; i < this->urlLength; i++)
url[i] = buffer[ix++];
url[this->urlLength] = '\0';
this->url = url;
}
ModelUrlMsg::~ModelUrlMsg() {
delete[] this->url;
}
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