HDLC-Daemon
AliveState.cpp
Go to the documentation of this file.
1 
39 #include "AliveState.h"
40 #include <assert.h>
41 
46 AliveState::AliveState(boost::asio::io_service& a_IOService): m_StateTimer(a_IOService), m_ProbeTimer(a_IOService) {
47  Reset();
48 }
49 
55  Stop();
56 }
57 
62 void AliveState::Reset() {
63  m_eAliveState = ALIVESTATE_PROBING;
64  m_bFrameWasReceived = false;
65  m_ProbeCounter = 0;
66 }
67 
73  assert(m_SendProbeCallback);
74  assert(m_ChangeBaudrateCallback);
75  m_ProbeCounter = 0;
76  m_eAliveState = ALIVESTATE_PROBING;
77  m_bFrameWasReceived = false;
78  m_StateTimer.cancel();
79  m_ProbeTimer.cancel();
80  StartStateTimer();
81  StartProbeTimer();
82  m_SendProbeCallback(); // send first probe
83 }
84 
90  m_StateTimer.cancel();
91  m_ProbeTimer.cancel();
92  Reset();
93 }
94 
101 void AliveState::SetSendProbeCallback(std::function<void()> a_SendProbeCallback) {
102  m_SendProbeCallback = a_SendProbeCallback;
103 }
104 
111 void AliveState::SetChangeBaudrateCallback(std::function<void()> a_ChangeBaudrateCallback) {
112  m_ChangeBaudrateCallback = a_ChangeBaudrateCallback;
113 }
114 
123  // We received an HDLC frame
124  m_bFrameWasReceived = true;
125  if (m_eAliveState == ALIVESTATE_PROBING) {
126  // Reprobing succeeded
127  m_eAliveState = ALIVESTATE_FOUND;
128  return true;
129  } else {
130  m_eAliveState = ALIVESTATE_FOUND;
131  return false; // TODO: Why false? Seems to be correct, but check again and add a comment!
132  } // else
133 }
134 
141 bool AliveState::IsAlive() const {
142  // Return true if transmission of data is currently allowed
143  return ((m_eAliveState == ALIVESTATE_FOUND) || (m_eAliveState == ALIVESTATE_REPROBING));
144 }
145 
150 void AliveState::StartStateTimer() {
151  auto self(shared_from_this());
152  m_StateTimer.expires_from_now(boost::posix_time::seconds(15));
153  m_StateTimer.async_wait([this, self](const boost::system::error_code& ec) {
154  if (!ec) {
155  // A state timeout occured
156  OnStateTimeout();
157  StartStateTimer(); // start again
158  } // if
159  });
160 }
161 
166 void AliveState::StartProbeTimer() {
167  auto self(shared_from_this());
168  m_ProbeTimer.expires_from_now(boost::posix_time::milliseconds(500));
169  m_ProbeTimer.async_wait([this, self](const boost::system::error_code& ec) {
170  if (!ec) {
171  // A probe timeout occured
172  OnProbeTimeout();
173  StartProbeTimer(); // start again
174  } // if
175  });
176 }
177 
182 void AliveState::OnStateTimeout() {
183  if (m_eAliveState == ALIVESTATE_FOUND) {
184  if (m_bFrameWasReceived) {
185  m_bFrameWasReceived = false;
186  } else {
187  m_eAliveState = ALIVESTATE_REPROBING;
188  } // else
189  } else if (m_eAliveState == ALIVESTATE_REPROBING) {
190  if (!m_bFrameWasReceived) {
191  m_eAliveState = ALIVESTATE_PROBING;
192  } else {
193  // This must not happen
194  assert(false);
195  } // else
196  } // else
197 }
198 
203 void AliveState::OnProbeTimeout() {
204  if (m_eAliveState == ALIVESTATE_PROBING) {
205  // For each fourth probe we select another baud rate
206  if ((m_ProbeCounter = ((m_ProbeCounter + 1) & 0x03)) == 0) {
207  m_ChangeBaudrateCallback();
208  } // if
209 
210  m_SendProbeCallback();
211  } else if (m_eAliveState == ALIVESTATE_REPROBING) {
212  m_SendProbeCallback();
213  } // else
214 }
AliveState(boost::asio::io_service &a_IOService)
The constructor of AliveState objects.
Definition: AliveState.cpp:46
bool IsAlive() const
Query whether the related serialport is currently considered alive.
Definition: AliveState.cpp:141
void SetSendProbeCallback(std::function< void()> a_SendProbeCallback)
Register callback method for sending a probe request via HDLC.
Definition: AliveState.cpp:101
~AliveState()
The destructor of AliveState objects.
Definition: AliveState.cpp:54
bool OnFrameReceived()
Indicate that a HDLC frame was received via the related serial port.
Definition: AliveState.cpp:122
void Stop()
Stop all activities.
Definition: AliveState.cpp:89
void SetChangeBaudrateCallback(std::function< void()> a_ChangeBaudrateCallback)
Register callback method for changing the baud rate of the related serial port.
Definition: AliveState.cpp:111
void Start()
Start all activities.
Definition: AliveState.cpp:72
This file contains the header declaration of class AliveState.