diff --git a/ControlCore/Client.cpp b/ControlCore/Client.cpp deleted file mode 100644 index 57cc7d0..0000000 --- a/ControlCore/Client.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "Client.h" - -bool Passer::Control::ControlClient::SendMsg(unsigned char *buffer, - unsigned char bufferSize) { - return false; -} \ No newline at end of file diff --git a/ControlCore/Client.h b/ControlCore/Client.h deleted file mode 100644 index e0edf13..0000000 --- a/ControlCore/Client.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace Passer::Control { - -class ControlClient { -public: - unsigned char *buffer; - bool SendMsg(unsigned char *buffer, unsigned char bufferSize); -}; - -} // namespace Passer::Control \ No newline at end of file diff --git a/ControlCore/CoreThing.cpp b/ControlCore/CoreThing.cpp new file mode 100644 index 0000000..40c7fa6 --- /dev/null +++ b/ControlCore/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/ControlCore/CoreThing.h b/ControlCore/CoreThing.h new file mode 100644 index 0000000..8937455 --- /dev/null +++ b/ControlCore/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/ControlCore/Messages.cpp b/ControlCore/Messages.cpp index 7705a91..799e7d6 100644 --- a/ControlCore/Messages.cpp +++ b/ControlCore/Messages.cpp @@ -1,19 +1,15 @@ #include "Messages.h" #include "LowLevelMessages.h" +#include "string.h" #pragma region IMessage unsigned char IMessage::Serialize(unsigned char *buffer) { return 0; } -bool IMessage::SendMsg(ControlClient client, IMessage msg, - unsigned char bufferSize) { - return SendMsg(client, client.buffer, msg.Serialize(client.buffer)); -} - -bool IMessage::SendMsg(ControlClient client, unsigned char *buffer, - unsigned char bufferSize) { - return false; // client.SendMsg(buffer, bufferSize); +bool IMessage::SendMsg(Participant *client, IMessage msg) { + // return SendMsg(client, client.buffer, );nameLength + return client->SendBuffer(msg.Serialize(client->buffer)); } #pragma endregion @@ -33,10 +29,10 @@ unsigned char InvestigateMsg::Serialize(unsigned char *buffer) { return ix; } -bool InvestigateMsg::Send(ControlClient client, unsigned char networkId, +bool InvestigateMsg::Send(Participant *client, unsigned char networkId, unsigned char thingId) { InvestigateMsg msg = InvestigateMsg(networkId, thingId); - return SendMsg(client, msg, InvestigateMsg::length); + return SendMsg(client, msg); } // Investigate @@ -44,10 +40,8 @@ bool InvestigateMsg::Send(ControlClient client, unsigned char networkId, #pragma region Thing -Passer::Control::ThingMsg::ThingMsg(unsigned char networkId, - unsigned char thingId, - unsigned char thingType, - unsigned char parentId) { +ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId, + unsigned char thingType, unsigned char parentId) { this->networkId = networkId; this->thingId = thingId; this->thingType = thingType; @@ -64,13 +58,11 @@ unsigned char ThingMsg::Serialize(unsigned char *buffer) { return ix; } -bool Passer::Control::ThingMsg::Send(ControlClient client, - unsigned char networkId, - unsigned char thingId, - unsigned char thingType, - unsigned char parentId) { +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, ThingMsg::length); + return SendMsg(client, msg); } // Thing @@ -79,24 +71,37 @@ bool Passer::Control::ThingMsg::Send(ControlClient client, #pragma region Name NameMsg::NameMsg(unsigned char networkId, unsigned char thingId, - unsigned char nameLength, const char *name) { + const char *name, unsigned char nameLength) { this->networkId = networkId; this->thingId = thingId; - this->nameLength = nameLength; this->name = name; + this->nameLength = nameLength; } -unsigned char Passer::Control::NameMsg::Serialize(unsigned char *buffer) { +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++] = name[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 diff --git a/ControlCore/Messages.h b/ControlCore/Messages.h index e5f5164..0033f0a 100644 --- a/ControlCore/Messages.h +++ b/ControlCore/Messages.h @@ -1,19 +1,15 @@ #pragma once #include "../float16/float16.h" -#include "Client.h" +#include "CoreThing.h" +#include "Participant.h" namespace Passer::Control { class IMessage { public: - // static unsigned char buffer[256]; - virtual unsigned char Serialize(unsigned char *buffer); - static bool SendMsg(ControlClient client, IMessage msg, - unsigned char bufferSize); - static bool SendMsg(ControlClient client, unsigned char *buffer, - unsigned char bufferSize); + static bool SendMsg(Participant *client, IMessage msg); }; class InvestigateMsg : public IMessage { @@ -27,7 +23,7 @@ public: virtual unsigned char Serialize(unsigned char *buffer) override; - static bool Send(ControlClient client, unsigned char networkId, + static bool Send(Participant *client, unsigned char networkId, unsigned char thingId); }; @@ -45,7 +41,7 @@ public: virtual unsigned char Serialize(unsigned char *buffer) override; - static bool Send(ControlClient client, unsigned char networkId, + static bool Send(Participant *client, unsigned char networkId, unsigned char thingId, unsigned char thingType, unsigned char parentId); }; @@ -59,10 +55,13 @@ public: unsigned char nameLength; const char *name; - NameMsg(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 { diff --git a/ControlCore/Participant.cpp b/ControlCore/Participant.cpp new file mode 100644 index 0000000..1056dd0 --- /dev/null +++ b/ControlCore/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/ControlCore/Participant.h b/ControlCore/Participant.h new file mode 100644 index 0000000..1ab3190 --- /dev/null +++ b/ControlCore/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 diff --git a/NetworkSync.cpp b/NetworkSync.cpp index 3c3d2ef..4ac9784 100644 --- a/NetworkSync.cpp +++ b/NetworkSync.cpp @@ -166,9 +166,11 @@ void NetworkSync::SendName(Thing *thing) { // SendBuffer(ix); Passer::Control::NameMsg msg = - Passer::Control::NameMsg(this->networkId, thing->id, len, thing->name); + Passer::Control::NameMsg(this->networkId, thing->id, thing->name, len); SendBuffer(msg.Serialize(this->buffer)); + // Passer::Control::NameMsg::Send(this, thing); + #ifdef RC_DEBUG SERIALPORT.printf("Sent Name [%d/%d] %s\n", networkId, buffer[1], thing->name); @@ -406,9 +408,10 @@ void NetworkSync::SendInt(const int x) { // Low-level functions -void NetworkSync::SendBuffer(unsigned char bufferSize) {} +// bool NetworkSync::SendBuffer(unsigned char bufferSize) { return false; } -void NetworkSync::PublishBuffer(unsigned char bufferSize) {} +// bool NetworkSync::PublishBuffer(unsigned char bufferSize) { return false; } -void NetworkSync::SendCBuffer(unsigned char bufferSize, unsigned char *buffer) { -} \ No newline at end of file +// void NetworkSync::SendCBuffer(unsigned char bufferSize, unsigned char +// *buffer) { +//} \ No newline at end of file diff --git a/NetworkSync.h b/NetworkSync.h index 1a4b30e..a20191b 100644 --- a/NetworkSync.h +++ b/NetworkSync.h @@ -1,6 +1,7 @@ #pragma once #include "ControlCore/Messages.h" +#include "ControlCore/Participant.h" #include "NetworkPerception.h" #include "Perception.h" #include "Roboid.h" @@ -10,7 +11,7 @@ namespace Passer { namespace RoboidControl { /// @brief Interface for synchronizaing state between clients across a network -class NetworkSync { +class NetworkSync : public Participant { public: NetworkSync() {}; NetworkSync(Roboid *roboid); @@ -97,11 +98,11 @@ protected: // UInt8 parentId, // InterestingThing* object); - unsigned char buffer[256]; - virtual void SendBuffer(unsigned char bufferSize); - virtual void PublishBuffer(unsigned char bufferSize); + // unsigned char buffer[256]; + // virtual bool SendBuffer(unsigned char bufferSize); + // virtual bool PublishBuffer(unsigned char bufferSize); - virtual void SendCBuffer(unsigned char bufferSize, unsigned char *buffer); + // virtual void SendCBuffer(unsigned char bufferSize, unsigned char *buffer); void PublishClient(); }; diff --git a/Roboid.cpp b/Roboid.cpp index 4212e22..3649202 100644 --- a/Roboid.cpp +++ b/Roboid.cpp @@ -15,7 +15,7 @@ Roboid::Roboid() : Thing(0) { #ifdef RC_DEBUG Serial.begin(115200); #endif - this->type = RoboidType; + this->type = (unsigned char)RoboidType; this->perception = new Perception(); this->perception->roboid = this; this->propulsion = new Propulsion(); diff --git a/Sensor.cpp b/Sensor.cpp index f44e411..2def1af 100644 --- a/Sensor.cpp +++ b/Sensor.cpp @@ -3,7 +3,7 @@ #include "Roboid.h" Sensor::Sensor() : Thing() { // for now, id should be set properly later - this->type = Thing::SensorType; + this->type = (unsigned char)Thing::SensorType; } void Sensor::SetParent(Thing *parent) { diff --git a/Thing.cpp b/Thing.cpp index c5b646c..377a048 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -7,6 +7,7 @@ using namespace Passer::RoboidControl; int Thing::lastThingId = 1; Thing::Thing() { + //: CoreThing(0, lastThingId++, (unsigned char)Type::Undetermined) { this->id = lastThingId++; this->type = (unsigned int)Type::Undetermined; this->childCount = 0; @@ -14,8 +15,10 @@ Thing::Thing() { this->children = nullptr; } -Thing::Thing(unsigned char id) : id(id) { - // this->position = SphericalOf::zero; +Thing::Thing(unsigned char id) { + //: CoreThing(0, id, (unsigned char)Type::Undetermined) { + this->id = id; + // this->position = SphericalOf::zero; this->type = (unsigned int)Type::Undetermined; this->childCount = 0; this->parent = nullptr; diff --git a/Thing.h b/Thing.h index 2c8ee21..bd27e63 100644 --- a/Thing.h +++ b/Thing.h @@ -1,5 +1,6 @@ #pragma once +#include "ControlCore/CoreThing.h" #include "LinearAlgebra/AngleAxis.h" #include "LinearAlgebra/Quaternion.h" #include "LinearAlgebra/Spherical.h" @@ -9,7 +10,7 @@ namespace Passer { namespace RoboidControl { /// @brief A thing is a functional component on a robot -class Thing { +class Thing { //: public CoreThing { public: Thing(); /// @brief Default constructor for a Thing