Woman, Life, Freedom


Adaptive AUTOSAR
ARA public interface header documentation
timer_set_state.h
1#ifndef TIMER_SET_STATE_H
2#define TIMER_SET_STATE_H
3
4#include <stdexcept>
5#include "../../../helper/machine_state.h"
6
7namespace ara
8{
9 namespace com
10 {
11 namespace someip
12 {
13 namespace sd
14 {
17 namespace fsm
18 {
22 template <typename T>
23 class TimerSetState : virtual public helper::MachineState<T>
24 {
25 private:
26 const T mStoppedState;
27 T mNextState;
28 bool mStopped;
29 bool mInterrupted;
30
31 void setTimerBase()
32 {
33 SetTimer();
34 // Transition to the next state or to the stopped state
35 if (mStopped)
36 {
38 }
39 else
40 {
42 }
43 }
44
45 protected:
49 bool WaitFor(std::chrono::milliseconds duration)
50 {
51 std::this_thread::sleep_for(duration);
52 bool _result = mStopped || mInterrupted;
53
54 return _result;
55 }
56
60 bool WaitFor(int duration)
61 {
62 auto _milliseconds = std::chrono::milliseconds(duration);
63 bool _result = WaitFor(_milliseconds);
64
65 return _result;
66 }
67
70 void Interrupt() noexcept
71 {
72 mInterrupted = true;
73 }
74
76 const std::function<void()> OnTimerExpired;
77
79 virtual void SetTimer() = 0;
80
86 T nextState,
87 T stoppedState,
88 std::function<void()> onTimerExpired) : mNextState{nextState},
89 mStoppedState{stoppedState},
90 OnTimerExpired{onTimerExpired},
91 mStopped{false},
92 mInterrupted{false}
93 {
94 }
95
96 void Deactivate(T nextState) override
97 {
98 // Reset 'service interrupted' flag
99 mInterrupted = false;
100 // Reset 'service stopped' flag
101 mStopped = false;
102 }
103
104 public:
105 TimerSetState(const TimerSetState &) = delete;
106 TimerSetState &operator=(const TimerSetState &) = delete;
107
108 virtual void Activate(T previousState) override
109 {
110 if (!mStopped)
111 {
112 setTimerBase();
113 }
114 }
115
117 void ServiceStopped() noexcept
118 {
119 mStopped = true;
120 }
121
124 void SetNextState(T nextState) noexcept
125 {
126 mNextState = nextState;
127 }
128
129 virtual ~TimerSetState() override
130 {
131 // Set a fake stop signal, otherwise the timer loop may never end (e.g., in the main phase).
133 }
134 };
135 }
136 }
137 }
138 }
139}
140
141#endif
Machine state abstract class.
Definition: machine_state.h:46
void Transit(T nextState)
Transit to the next state.
Definition: machine_state.h:59
Server's or client's service timer set state.
Definition: timer_set_state.h:24
virtual void Activate(T previousState) override
Activate the state.
Definition: timer_set_state.h:108
bool WaitFor(std::chrono::milliseconds duration)
Wait for certian period of time.
Definition: timer_set_state.h:49
bool WaitFor(int duration)
Wait for certian period of time.
Definition: timer_set_state.h:60
void Interrupt() noexcept
Interrupt the timer.
Definition: timer_set_state.h:70
const std::function< void()> OnTimerExpired
Delegate which is invoked by timer's thread when the timer is expired.
Definition: timer_set_state.h:76
void SetNextState(T nextState) noexcept
Set next state.
Definition: timer_set_state.h:124
TimerSetState(T nextState, T stoppedState, std::function< void()> onTimerExpired)
Constructor.
Definition: timer_set_state.h:85
void Deactivate(T nextState) override
Deactivating the current state before transiting to the next state.
Definition: timer_set_state.h:96
void ServiceStopped() noexcept
Inform the state that the server's service is stopped.
Definition: timer_set_state.h:117
virtual void SetTimer()=0
Set the phase time on state activation.