From 6518cb62be0febed235b6ad5da07bca2b1b9d113 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 13 Dec 2024 17:12:06 +0100 Subject: [PATCH] Everything re-added? --- CoreThing.cpp | 38 +++++++++++++ CoreThing.h | 41 +++++++++++++ LowLevelMessages.cpp | 12 ++++ LowLevelMessages.h | 12 ++++ Messages.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++ Messages.h | 83 +++++++++++++++++++++++++++ Participant.cpp | 10 ++++ Participant.h | 16 ++++++ 8 files changed, 345 insertions(+) create mode 100644 CoreThing.cpp create mode 100644 CoreThing.h create mode 100644 LowLevelMessages.cpp create mode 100644 LowLevelMessages.h create mode 100644 Messages.cpp create mode 100644 Messages.h create mode 100644 Participant.cpp create mode 100644 Participant.h diff --git a/CoreThing.cpp b/CoreThing.cpp new file mode 100644 index 0000000..40c7fa6 --- /dev/null +++ b/CoreThing.cpp @@ -0,0 +1,38 @@ +#include "CoreThing.h" + +CoreThing::CoreThing( + // Participant *client, + unsigned char networkId, unsigned char thingId, unsigned char thingType) { + // this->client = client; + this->id = thingId; + this->type = thingType; + this->networkId = networkId; + this->Init(); + // CoreThings::Add(this); +} + +void CoreThing::Init() {} + +// CoreThing *CoreThings::allThings[256] = {nullptr}; + +// CoreThing *CoreThings::Get(unsigned char networkId, unsigned char thingId) { +// for (unsigned char ix = 0; ix < 256; ix++) { +// CoreThing *thing = allThings[ix]; +// if (thing == nullptr) +// continue; +// if (thing->networkId == networkId && thing->id == thingId) +// return thing; +// } +// return nullptr; +// } + +// bool CoreThings::Add(CoreThing *newThing) { +// for (unsigned char ix = 0; ix < 256; ix++) { +// CoreThing *thing = allThings[ix]; +// if (thing == nullptr) { +// allThings[ix] = newThing; +// return true; +// } +// } +// return false; +// } \ No newline at end of file diff --git a/CoreThing.h b/CoreThing.h new file mode 100644 index 0000000..8937455 --- /dev/null +++ b/CoreThing.h @@ -0,0 +1,41 @@ +#pragma once + +#include "Participant.h" + +namespace Passer::Control { + +class CoreThing { +public: + // Participant *client; + unsigned char networkId; + unsigned char id; + // CoreThing *parent; + unsigned char type; + const char *name; + const char *modelUrl; + // protected Sensor sensor; + +public: + CoreThing( + // Participant *client, + unsigned char networkId, unsigned char thingId, + unsigned char thingType = 0); + +protected: + virtual void Init(); + + // Things +}; + +// class CoreThings { +// friend class CoreThing; + +// public: +// static CoreThing *allThings[]; + +// public: +// static CoreThing *Get(unsigned char networkId, unsigned char thingId); +// static bool Add(CoreThing *thing); +// }; + +} // namespace Passer::Control diff --git a/LowLevelMessages.cpp b/LowLevelMessages.cpp new file mode 100644 index 0000000..a72ada4 --- /dev/null +++ b/LowLevelMessages.cpp @@ -0,0 +1,12 @@ +#include "LowLevelMessages.h" + +#include "../float16/float16.h" + +void LowLevelMessages::SendFloat16(unsigned char *buffer, unsigned char *ix, + float value) { + float16 value16 = float16(value); + short binary = value16.getBinary(); + + buffer[(*ix)++] = (binary >> 8) & 0xFF; + buffer[(*ix)++] = binary & 0xFF; +} \ No newline at end of file diff --git a/LowLevelMessages.h b/LowLevelMessages.h new file mode 100644 index 0000000..67a6095 --- /dev/null +++ b/LowLevelMessages.h @@ -0,0 +1,12 @@ +#include "../LinearAlgebra/Spherical.h" + +namespace Passer::Control { + +class LowLevelMessages { +public: + static void SendFloat16(unsigned char *buffer, unsigned char *ix, + float value); +}; + +} // namespace Passer::Control +using namespace Passer::Control; \ No newline at end of file diff --git a/Messages.cpp b/Messages.cpp new file mode 100644 index 0000000..799e7d6 --- /dev/null +++ b/Messages.cpp @@ -0,0 +1,133 @@ +#include "Messages.h" + +#include "LowLevelMessages.h" +#include "string.h" + +#pragma region IMessage + +unsigned char IMessage::Serialize(unsigned char *buffer) { return 0; } + +bool IMessage::SendMsg(Participant *client, IMessage msg) { + // return SendMsg(client, client.buffer, );nameLength + return client->SendBuffer(msg.Serialize(client->buffer)); +} + +#pragma endregion + +#pragma region Investigate + +InvestigateMsg::InvestigateMsg(unsigned char networkId, unsigned char thingId) { + this->networkId = networkId; + this->thingId = thingId; +} + +unsigned char InvestigateMsg::Serialize(unsigned char *buffer) { + unsigned char ix = 0; + buffer[ix++] = this->id; + buffer[ix++] = this->networkId; + buffer[ix++] = this->thingId; + return ix; +} + +bool InvestigateMsg::Send(Participant *client, unsigned char networkId, + unsigned char thingId) { + InvestigateMsg msg = InvestigateMsg(networkId, thingId); + return SendMsg(client, msg); +} + +// Investigate +#pragma endregion + +#pragma region Thing + +ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId, + unsigned char thingType, unsigned char parentId) { + this->networkId = networkId; + this->thingId = thingId; + this->thingType = thingType; + this->parentId = parentId; +} + +unsigned char ThingMsg::Serialize(unsigned char *buffer) { + unsigned char ix = 0; + buffer[ix++] = this->id; + buffer[ix++] = this->networkId; + buffer[ix++] = this->thingId; + buffer[ix++] = this->thingType; + buffer[ix++] = this->parentId; + return ix; +} + +bool ThingMsg::Send(Participant *client, unsigned char networkId, + unsigned char thingId, unsigned char thingType, + unsigned char parentId) { + ThingMsg msg = ThingMsg(networkId, thingId, thingType, parentId); + return SendMsg(client, msg); +} + +// Thing +#pragma endregion + +#pragma region Name + +NameMsg::NameMsg(unsigned char networkId, unsigned char thingId, + const char *name, unsigned char nameLength) { + this->networkId = networkId; + this->thingId = thingId; + this->name = name; + this->nameLength = nameLength; +} + +unsigned char NameMsg::Serialize(unsigned char *buffer) { + unsigned char ix = 0; + buffer[ix++] = this->id; + buffer[ix++] = this->networkId; + buffer[ix++] = this->thingId; + buffer[ix++] = this->nameLength; + for (int nameIx = 0; nameIx < this->nameLength; nameIx++) + buffer[ix++] = this->name[nameIx]; + + return ix; +} + +bool NameMsg::Send(Participant *client, CoreThing *thing, + unsigned char nameLength) { + if (thing->name == nullptr) + return true; // nothing sent, but still a success! + + if (strlen(thing->name) == 0) + return true; + + NameMsg msg = NameMsg(thing->networkId, thing->id, thing->name, nameLength); + return SendMsg(client, msg); +} + +// Name +#pragma endregion + +#pragma region ModelUrl + +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; +} + +unsigned char ModelUrlMsg::Serialize(unsigned char *buffer) { + 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; +} + +// Model Url +#pragma endregion \ No newline at end of file diff --git a/Messages.h b/Messages.h new file mode 100644 index 0000000..0033f0a --- /dev/null +++ b/Messages.h @@ -0,0 +1,83 @@ +#pragma once +#include "../float16/float16.h" +#include "CoreThing.h" +#include "Participant.h" + +namespace Passer::Control { + +class IMessage { +public: + virtual unsigned char Serialize(unsigned char *buffer); + + static bool SendMsg(Participant *client, IMessage msg); +}; + +class InvestigateMsg : public IMessage { +public: + static const unsigned char id = 0x81; + static const unsigned char length = 3; + unsigned char networkId; + unsigned char thingId; + + InvestigateMsg(unsigned char networkId, unsigned char thingId); + + virtual unsigned char Serialize(unsigned char *buffer) override; + + static bool Send(Participant *client, unsigned char networkId, + unsigned char thingId); +}; + +class ThingMsg : public IMessage { +public: + static const unsigned char id = 0x80; + static const unsigned char length = 5; + unsigned char networkId; + unsigned char thingId; + unsigned char thingType; + unsigned char parentId; + + ThingMsg(unsigned char networkId, unsigned char thingId, + unsigned char thingType, unsigned char parentId); + + virtual unsigned char Serialize(unsigned char *buffer) override; + + static bool Send(Participant *client, unsigned char networkId, + unsigned char thingId, unsigned char thingType, + unsigned char parentId); +}; + +class NameMsg : public IMessage { +public: + static const unsigned char id = 0x91; + static const unsigned char length = 4; + unsigned char networkId; + unsigned char thingId; + unsigned char nameLength; + const char *name; + + NameMsg(unsigned char networkId, unsigned char thingId, const char *name, + unsigned char nameLength); + + virtual unsigned char Serialize(unsigned char *buffer) override; + + static bool Send(Participant *client, CoreThing *thing, + unsigned char nameLength); +}; + +class ModelUrlMsg : public IMessage { +public: + static const unsigned char id = 0x90; + unsigned char networkId; + unsigned char thingId; + float scale; + unsigned char urlLength; + const char *url; + + ModelUrlMsg(unsigned char networkId, unsigned char thingId, + unsigned char urlLegth, const char *url, float scale = 1); + + virtual unsigned char Serialize(unsigned char *buffer) override; +}; + +} // namespace Passer::Control +using namespace Passer::Control; \ No newline at end of file diff --git a/Participant.cpp b/Participant.cpp new file mode 100644 index 0000000..1056dd0 --- /dev/null +++ b/Participant.cpp @@ -0,0 +1,10 @@ +#include "Participant.h" + +// bool Passer::Control::ControlClient::SendMsg(unsigned char *buffer, +// unsigned char bufferSize) { +// return false; +// } + +bool Participant::SendBuffer(unsigned char bufferSize) { return false; } + +bool Participant::PublishBuffer(unsigned char bufferSize) { return false; } diff --git a/Participant.h b/Participant.h new file mode 100644 index 0000000..1ab3190 --- /dev/null +++ b/Participant.h @@ -0,0 +1,16 @@ +#pragma once + +namespace Passer::Control { + +class Participant { +public: + // unsigned char *buffer; + // bool SendMsg(unsigned char *buffer, unsigned char bufferSize); + + unsigned char buffer[1024]; + virtual bool SendBuffer(unsigned char bufferSize); + virtual bool PublishBuffer(unsigned char bufferSize); +}; + +} // namespace Passer::Control +using namespace Passer::Control; \ No newline at end of file