Files
MuditaOS/module-sys/Service/Message.cpp
Adam Dobrowolski 2cee195e09 EGD-3056 subscribtion based execution (on typeid) added and works fine
Till now there was no clear way to register hanlder for message in
Service, with this approach one can register function/lambda handler
for Message which encurages handling messages in functions rather than
in hudge switch cases.
With aproach like this our Messages resembe something between Command
and State pattern. In state pattern `Message` would be `Event` send,
whereas Service would be `Context`.
2020-04-15 16:50:19 +02:00

81 lines
1.9 KiB
C++

//
// Created by mati on 08.03.19.
//
#include "Message.hpp"
#include "Service.hpp"
#include "Bus.hpp"
#include "Channel.hpp"
#include "ticks.hpp"
#include <string.h>
namespace sys
{
MessageRet_t CreateMessageRet(ReturnCodes retCode, Message_t msg)
{
return std::make_pair(retCode, msg);
};
Message_t DataMessage::Execute(Service *service)
{
// Ignore incoming data message if this service is not yet initialized
if (service->isReady) {
return Proxy::handle(service, this, nullptr);
}
else {
return std::make_shared<ResponseMessage>();
}
}
Message_t SystemMessage::Execute(Service *service)
{
ReturnCodes ret = ReturnCodes ::Success;
switch (sysMsgType) {
case SystemMessageType ::Ping:
service->pingTimestamp = cpp_freertos::Ticks::GetTicks();
break;
case SystemMessageType ::SwitchPowerMode:
service->SwitchPowerModeHandler(powerMode);
break;
case SystemMessageType ::Exit:
ret = service->DeinitHandler();
service->CloseHandler();
break;
case SystemMessageType::Start:
ret = service->InitHandler();
if (ret == ReturnCodes::Success) {
service->isReady = true;
}
break;
}
return std::make_shared<ResponseMessage>(ret);
}
Message_t ResponseMessage::Execute(Service *service)
{
// Ignore incoming data message if this service is not yet initialized
if (service->isReady) {
DataMessage dummy(MessageType::MessageTypeUninitialized);
return Proxy::handle(service, &dummy, this);
}
else {
return std::make_shared<ResponseMessage>();
}
}
#ifdef UNIT_TESTS
uint32_t Message::unitestsMsgInstancesCount = 0;
#endif
} // namespace sys