diff --git a/services/util/SerialCommunicationLoopback.cpp b/services/util/SerialCommunicationLoopback.cpp index 644a8d15c..c0e166e90 100644 --- a/services/util/SerialCommunicationLoopback.cpp +++ b/services/util/SerialCommunicationLoopback.cpp @@ -1,13 +1,29 @@ #include "services/util/SerialCommunicationLoopback.hpp" +#include "hal/interfaces/SerialCommunication.hpp" #include "infra/event/EventDispatcher.hpp" namespace services { - SerialCommunicationLoopbackPeer::SerialCommunicationLoopbackPeer(SerialCommunicationLoopbackPeer& other) - : other{ other } + SerialCommunicationLoopback::SerialCommunicationLoopback() + : server(&client) + , client(&server) + {} + + hal::SerialCommunication& SerialCommunicationLoopback::Server() + { + return server; + } + + hal::SerialCommunication& SerialCommunicationLoopback::Client() + { + return client; + } + + SerialCommunicationLoopback::SerialCommunicationLoopbackPeer::SerialCommunicationLoopbackPeer(SerialCommunicationLoopbackPeer* other) + : other{ *other } {} - void SerialCommunicationLoopbackPeer::SendData(infra::ConstByteRange data, infra::Function actionOnCompletion) + void SerialCommunicationLoopback::SerialCommunicationLoopbackPeer::SendData(infra::ConstByteRange data, infra::Function actionOnCompletion) { this->data = data; this->actionOnCompletion = actionOnCompletion; @@ -19,23 +35,8 @@ namespace services }); } - void SerialCommunicationLoopbackPeer::ReceiveData(infra::Function dataReceived) + void SerialCommunicationLoopback::SerialCommunicationLoopbackPeer::ReceiveData(infra::Function dataReceived) { this->dataReceived = dataReceived; } - - SerialCommunicationLoopback::SerialCommunicationLoopback() - : server(client) - , client(server) - {} - - SerialCommunicationLoopbackPeer& SerialCommunicationLoopback::Server() - { - return server; - } - - SerialCommunicationLoopbackPeer& SerialCommunicationLoopback::Client() - { - return client; - } } diff --git a/services/util/SerialCommunicationLoopback.hpp b/services/util/SerialCommunicationLoopback.hpp index f7f39db35..5b63250b2 100644 --- a/services/util/SerialCommunicationLoopback.hpp +++ b/services/util/SerialCommunicationLoopback.hpp @@ -5,30 +5,34 @@ namespace services { - class SerialCommunicationLoopbackPeer - : public hal::SerialCommunication - { - public: - explicit SerialCommunicationLoopbackPeer(SerialCommunicationLoopbackPeer& other); - - void SendData(infra::ConstByteRange data, infra::Function actionOnCompletion) override; - void ReceiveData(infra::Function dataReceived) override; - - private: - SerialCommunicationLoopbackPeer& other; - - infra::ConstByteRange data; - infra::Function actionOnCompletion; - infra::Function dataReceived; - }; class SerialCommunicationLoopback { public: SerialCommunicationLoopback(); - SerialCommunicationLoopbackPeer& Server(); - SerialCommunicationLoopbackPeer& Client(); + hal::SerialCommunication& Server(); + hal::SerialCommunication& Client(); + + private: + class SerialCommunicationLoopbackPeer + : public hal::SerialCommunication + { + public: + // pointer instead of reference to avoid defining a copy constructor + // pass by reference generates a warning with clang and -Wunitialized + explicit SerialCommunicationLoopbackPeer(SerialCommunicationLoopbackPeer* other); + + void SendData(infra::ConstByteRange data, infra::Function actionOnCompletion) override; + void ReceiveData(infra::Function dataReceived) override; + + private: + SerialCommunicationLoopbackPeer& other; + + infra::ConstByteRange data; + infra::Function actionOnCompletion; + infra::Function dataReceived; + }; private: SerialCommunicationLoopbackPeer server; diff --git a/services/util/test/TestSerialCommunicationLoopback.cpp b/services/util/test/TestSerialCommunicationLoopback.cpp index cf388e1de..748bf5db8 100644 --- a/services/util/test/TestSerialCommunicationLoopback.cpp +++ b/services/util/test/TestSerialCommunicationLoopback.cpp @@ -1,3 +1,4 @@ +#include "hal/interfaces/SerialCommunication.hpp" #include "infra/event/test_helper/EventDispatcherFixture.hpp" #include "infra/util/ByteRange.hpp" #include "infra/util/Function.hpp" @@ -12,8 +13,8 @@ class SerialCommunicationLoopbackTest { public: services::SerialCommunicationLoopback SerialCommunicationLoopback; - services::SerialCommunicationLoopbackPeer& server{ SerialCommunicationLoopback.Server() }; - services::SerialCommunicationLoopbackPeer& client{ SerialCommunicationLoopback.Client() }; + hal::SerialCommunication& server{ SerialCommunicationLoopback.Server() }; + hal::SerialCommunication& client{ SerialCommunicationLoopback.Client() }; }; TEST_F(SerialCommunicationLoopbackTest, SendFromServerReceiveByClient)