Files
MuditaOS/module-utils/state/ServiceState.hpp
kkleczkowski e46d2a2829 Egd 3244 antenna manager (#490)
* [EGD-3244] Service antenna + utils State.

* [EGD-3244] CellularAPI + at::response.

* [EGD-3244-antenna-manager] Changes in application Antenna.

* [EGD-3244-antenna-manager] Changes in antenna app ( antenna switch multicast ).

* [EGD-3244-antenna-manager] Changed Antenna app GUI.

* [EGD-3244-antenna-manager] Basic algorithm.

* [EGD-3244-antenna-manager] Fixes in algorithm.

* [EGD-3244-antenna-manager] Small fixes + service blocking.

* [EGD-3244-antenna-manager] Little cleanup.

* [EGD-3244-antenna-manager] Restored Cellular call in Application Antenna.

* [EGD-3244-antenna-manager] Style fix.

* [EGD-3244-antenna-manager] Sewrvice antenna disabled.

* [EGD-3244-antenna-manager] PR suggestions + linux buil fixed.

* [EGD-3244-antenna-manager] Updated changelog

* [EGD-3244-antenna-manager] Style fix.

* [EGD-3244-antenna-manager] Fixed submodule.

* [EGD-3244-antenna-manager] PR suggestions + toNumeric UT.

* [EGD-3244-antenna-manager] PR suggstions again.

Co-authored-by: Kuba Kleczkowski <dd>
2020-07-07 10:54:47 +02:00

81 lines
2.2 KiB
C++

#pragma once
#include <algorithm>
#include <cassert>
#include "Service/Service.hpp"
#include "Service/Message.hpp"
#include "MessageType.hpp"
namespace utils
{
namespace state
{
template <typename T> class State
{
private:
T currentState;
T lastState;
sys::Service *owner = nullptr;
bool timeoutActive = false;
uint32_t timeoutElapseTime = 0;
T timeoutState;
bool notifyOwner(void)
{
auto msg = std::make_shared<sys::DataMessage>(MessageType::StateChange);
if (owner != nullptr) {
sys::Bus::SendUnicast(msg, owner->GetName(), owner);
return true;
}
return false;
}
public:
State(sys::Service *service) : owner(service)
{}
void set(T state)
{
LOG_INFO(
"%s state change: [ %s ] -> [ %s ]", owner->GetName().c_str(), c_str(currentState), c_str(state));
lastState = currentState;
currentState = state;
notifyOwner();
}
T get(void)
{
return currentState;
}
T getLast(void)
{
return lastState;
}
T getTimeoutState(void)
{
return timeoutState;
}
void disableTimeout(void)
{
timeoutActive = false;
}
void enableStateTimeout(uint32_t currentTime, uint32_t timeout, T timeoutOccuredState)
{
timeoutElapseTime = currentTime + timeout;
timeoutState = timeoutOccuredState;
timeoutActive = true;
}
bool timeoutOccured(uint32_t time)
{
if (time >= timeoutElapseTime && timeoutActive) {
disableTimeout();
return true;
}
return false;
}
};
} // namespace state
} // namespace utils