nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
lpm.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2018-2019 Peter A. Bigot */
3 
7 #ifndef NRFCXX_LPM_HPP
8 #define NRFCXX_LPM_HPP
9 #pragma once
10 
11 #include <nrfcxx/clock.hpp>
12 
13 namespace nrfcxx {
14 
16 namespace lpm {
17 
37 {
38 public:
46  using state_type = unsigned int;
47 
52  using process_flags_type = unsigned int;
53 
58  using error_type = int;
59 
65  static constexpr state_type STATE_MASK = ((1U << 24) - 1);
66 
74  static constexpr state_type STATE_HAS_ERROR = (1U << 24);
75 
89  static constexpr state_type STATE_BLOCKING_ALARM = (1U << 25);
90 
106  static constexpr state_type STATE_FALLBACK_ALARM = (1U << 26);
107 
110  static constexpr state_type STATE_STOP_PENDING = (1U << 26);
111 
118  static constexpr state_type MS_OFF = 0x0000;
119 
122  static constexpr state_type MS_ENTRY_START = 0x0001;
123 
130  static constexpr state_type MS_LOST = 0x0003;
131 
134  static constexpr state_type MS_ENTRY_RESET = 0x0010;
135 
142  static constexpr state_type MS_EXIT_RESET = 0x0011;
143 
144  static constexpr state_type MS_ENTRY_ERRORED = 0x0012;
145  static constexpr state_type MS_ERRORED = 0x0013;
146  static constexpr state_type MS_ENTRY_FAILED = 0x0014;
147  static constexpr state_type MS_FAILED = 0x0015;
148  static constexpr state_type MS_ENTRY_STOPPED = 0x0016;
149 
159  static constexpr state_type MS_ENTRY_SETUP = 0x0020;
160 
166  static constexpr state_type MS_EXIT_SETUP = 0x0030;
167 
176  static constexpr state_type MS_IDLE = 0x0040;
177 
183  static constexpr state_type MS_ENTRY_SAMPLE = 0x0050;
184 
192  static constexpr state_type MS_SAMPLE = 0x0060;
193 
201  static constexpr state_type MS_EXIT_SAMPLE = 0x0070;
202 
204  static constexpr state_type MS_APP_BASE{0x100};
205 
211  static constexpr process_flags_type PF_OFF = 0x01;
212 
222  static constexpr process_flags_type PF_STARTED = 0x02;
223 
235  static constexpr process_flags_type PF_RESET = 0x04;
236 
242  static constexpr process_flags_type PF_FAILED = 0x08;
243 
244  static constexpr process_flags_type PF_STOPPED = 0x10;
245 
248  static constexpr process_flags_type PF_OBSERVATION = 0x20;
249 
255  static constexpr process_flags_type PF_APP_BASE = 0x100;
256 
258  /* You can't move or copy these. */
259  state_machine (const state_machine&) = delete;
260  state_machine& operator= (const state_machine&) = delete;
261  state_machine (state_machine&&) = delete;
262  state_machine& operator= (state_machine&&) = delete;
271  notify_{notify},
272  alarm_{alarm_callback_, this}
273  { }
274 
277  void reset();
278 
284  state_type state () const
285  {
286  return state_ & STATE_MASK;
287  }
288 
297  {
298  return state_;
299  }
300 
308  bool post = false)
309  {
310  state_ = state;
311  if (post) {
312  post_event();
313  }
314  }
315 
322  bool has_error () const
323  {
324  return STATE_HAS_ERROR & state_;
325  }
326 
330  void set_error ()
331  {
332  state_ |= STATE_HAS_ERROR;
333  }
334 
339  {
340  state_ |= STATE_HAS_ERROR;
341  error_ = error;
342  }
343 
346  void set_lost ();
347 
365  error_type error () const
366  {
367  return error_;
368  }
369 
373  {
374  return STATE_BLOCKING_ALARM & state_;
375  }
376 
380  {
381  return STATE_FALLBACK_ALARM & state_;
382  }
383 
386  void post_event ()
387  {
388  notify_();
389  }
390 
393  int deadline () const
394  {
395  return alarm_.deadline();
396  }
397 
406  void set_delay (int delay_utt);
407 
409  void cancel_delay ();
410 
411 protected:
412  static bool alarm_callback_ (clock::alarm& alarm);
413 
414 private:
415  notifier_type notify_;
416  clock::alarm alarm_;
417  state_type state_ = 0;
418  error_type error_ = 0;
419 };
420 
427 {
428  int process_rc_ = 0;
429 protected:
435  machine_{notify}
436  { }
437 
438  state_machine machine_;
439 
440  using state_type = state_machine::state_type;
441  using process_flags_type = state_machine::process_flags_type;
442 
464  virtual int lpsm_process_ (int& delay,
465  process_flags_type& pf)
466  {
467  return -1;
468  }
469 
471  virtual void lpsm_reset_ ()
472  { }
473 
474 public:
475  virtual ~lpsm_capable () = default;
476 
493  virtual int lpsm_start ();
494 
510  virtual int lpsm_sample ();
511 
522 
524  int lpsm_stop ()
525  {
526  return -1;
527  }
528 
529  int lpsm_reset ()
530  {
531  /* This is not functionally complete, but is stubbed to trace the
532  * dependence between lpsm_capable and its subclasses for clearing
533  * external state such as flags not maintained in the state
534  * machine. */
535  lpsm_reset_();
536  machine_.reset();
537  return 0;
538  }
539 
545  int lpsm_process_rc () const
546  {
547  return process_rc_;
548  }
549 
551  const lpm::state_machine& machine () const
552  {
553  return machine_;
554  }
555 
556 };
557 
558 } // ns lpm
559 } // ns nrfcxx
560 
561 #endif /* NRFCXX_LPM_HPP */
nrfcxx::lpm::state_machine::MS_LOST
static constexpr state_type MS_LOST
State indicating the machine entered processing in a state not supported by the machine.
Definition: lpm.hpp:130
nrfcxx::lpm::state_machine::set_error
void set_error()
Mark the current state with STATE_HAS_ERROR.
Definition: lpm.hpp:330
nrfcxx::lpm::state_machine::STATE_HAS_ERROR
static constexpr state_type STATE_HAS_ERROR
Bit set in state_type when the machine is in an error state.
Definition: lpm.hpp:74
nrfcxx::lpm::state_machine::set_state
void set_state(state_type state, bool post=false)
Set the machine state.
Definition: lpm.hpp:307
nrfcxx::lpm::state_machine::blocking_alarm_pending
bool blocking_alarm_pending() const
true iff the current state is marked with STATE_BLOCKING_ALARM.
Definition: lpm.hpp:372
nrfcxx::lpm::state_machine::PF_OBSERVATION
static constexpr process_flags_type PF_OBSERVATION
lpsm_capable::lpsm_process() flag bit when a new observation is available.
Definition: lpm.hpp:248
nrfcxx::lpm::lpsm_capable::lpsm_process_rc
int lpsm_process_rc() const
Get the internal result code from the last invocation of lpsm_process_().
Definition: lpm.hpp:545
nrfcxx::lpm::state_machine::MS_EXIT_SETUP
static constexpr state_type MS_EXIT_SETUP
States available to guard exit from a setup step.
Definition: lpm.hpp:166
nrfcxx::lpm::state_machine::PF_FAILED
static constexpr process_flags_type PF_FAILED
Bit set in non-negative lpsm_capable::lpsm_process() result when the machine is in an unrecoverable f...
Definition: lpm.hpp:242
nrfcxx::lpm::lpsm_capable::lpsm_stop
int lpsm_stop()
Definition: lpm.hpp:524
nrfcxx::lpm::state_machine::full_state
state_type full_state() const
Return the current machine state including error/pending flags.
Definition: lpm.hpp:296
nrfcxx::lpm::lpsm_capable::lpsm_start
virtual int lpsm_start()
Validate and prepare to initiate an LPM collection.
clock.hpp
Core clock-related functionality.
nrfcxx::lpm::state_machine::deadline
int deadline() const
Read the deadline for the most recently scheduled alarm.
Definition: lpm.hpp:393
nrfcxx::lpm::state_machine::set_error
void set_error(error_type error)
Mark the current state with STATE_HAS_ERROR.
Definition: lpm.hpp:338
nrfcxx::lpm::state_machine::PF_STARTED
static constexpr process_flags_type PF_STARTED
lpsm_capable::lpsm_process() flag bit when the machine is ready to provide observations.
Definition: lpm.hpp:222
nrfcxx::lpm::lpsm_capable::machine
const lpm::state_machine & machine() const
Gain read-only access to the LPM machine state.
Definition: lpm.hpp:551
nrfcxx::lpm::state_machine::PF_RESET
static constexpr process_flags_type PF_RESET
lpsm_capable::lpsm_process() flag bit when the machine has initiated a peripheral reset.
Definition: lpm.hpp:235
nrfcxx::lpm::state_machine::MS_SAMPLE
static constexpr state_type MS_SAMPLE
States available to implement a sample state.
Definition: lpm.hpp:192
nrfcxx::lpm::state_machine::PF_OFF
static constexpr process_flags_type PF_OFF
lpsm_capable::lpsm_process() flag bit when the machine is turned off.
Definition: lpm.hpp:211
nrfcxx::lpm::state_machine::STATE_STOP_PENDING
static constexpr state_type STATE_STOP_PENDING
Bit set in state_type when lpsm_capable::lpsm_stop() has been invoked by the application to request a...
Definition: lpm.hpp:110
nrfcxx::lpm::state_machine::MS_ENTRY_RESET
static constexpr state_type MS_ENTRY_RESET
State available to indicate that the machine needs to executing an internal reset operation.
Definition: lpm.hpp:134
nrfcxx::lpm::lpsm_capable::lpsm_sample
virtual int lpsm_sample()
Ask the LPM infrastructure to initiate a new sample.
nrfcxx::lpm::state_machine::process_flags_type
unsigned int process_flags_type
Representation for flags returned from lpsm_capable::lpsm_process().
Definition: lpm.hpp:52
nrfcxx::lpm::state_machine::MS_OFF
static constexpr state_type MS_OFF
State when the machine is off and all resources associated with it are disabled.
Definition: lpm.hpp:118
nrfcxx::lpm::state_machine::state_machine
state_machine(notifier_type notify)
Create a state machine that records state and supports delayed transitions.
Definition: lpm.hpp:270
nrfcxx::lpm::lpsm_capable::lpsm_capable
lpsm_capable(notifier_type notify)
Create a state machine that records state and supports delayed transitions.
Definition: lpm.hpp:434
nrfcxx::lpm::state_machine::MS_IDLE
static constexpr state_type MS_IDLE
State indicating the machine is in a functional state from which an application signal is required to...
Definition: lpm.hpp:176
nrfcxx::lpm::state_machine::cancel_delay
void cancel_delay()
Cancel any delay initiated by set_delay().
nrfcxx::lpm::state_machine::state
state_type state() const
Return the current machine state excluding error/pending flags.
Definition: lpm.hpp:284
nrfcxx::lpm::state_machine::has_error
bool has_error() const
true iff the current state is marked with STATE_HAS_ERROR.
Definition: lpm.hpp:322
nrfcxx::lpm::state_machine::MS_APP_BASE
static constexpr state_type MS_APP_BASE
Base state value available for application-specific states.
Definition: lpm.hpp:204
nrfcxx::lpm::lpsm_capable::lpsm_process
lpm::state_machine::process_flags_type lpsm_process()
Make progress on an LPM collection.
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::lpm::state_machine::MS_ENTRY_START
static constexpr state_type MS_ENTRY_START
State indicating the machine should begin automated transitions.
Definition: lpm.hpp:122
nrfcxx::clock::alarm
Class supporting an alarm with custom callback and repeatability.
Definition: clock.hpp:498
nrfcxx::lpm::state_machine::MS_EXIT_SAMPLE
static constexpr state_type MS_EXIT_SAMPLE
States available to guard exit from a sample state.
Definition: lpm.hpp:201
nrfcxx::lpm::state_machine::set_lost
void set_lost()
Set the current state as MS_LOST and store the unrecognized state in error().
nrfcxx::lpm::lpsm_capable
Base (or mixin) class for anything that supports a state_machine.
Definition: lpm.hpp:426
nrfcxx::lpm::state_machine
State machine abstraction for time-delayed transitions and error captures.
Definition: lpm.hpp:36
nrfcxx::lpm::state_machine::set_delay
void set_delay(int delay_utt)
Set an alarm to invoke post_event() after a delay.
nrfcxx::clock::alarm::deadline
unsigned int deadline() const
The value of (the low 32 bits of) uptime::now() at which the alarm should fire.
Definition: clock.hpp:767
nrfcxx::lpm::lpsm_capable::lpsm_process_
virtual int lpsm_process_(int &delay, process_flags_type &pf)
Override to implement machine-specific operations of lpsm_process().
Definition: lpm.hpp:464
nrfcxx::lpm::state_machine::STATE_MASK
static constexpr state_type STATE_MASK
Mask isolating the bits of state_type that are available to record machine state.
Definition: lpm.hpp:65
nrfcxx::lpm::state_machine::STATE_BLOCKING_ALARM
static constexpr state_type STATE_BLOCKING_ALARM
Bit set in state_type when an alarm guarding entry to the configured state has not yet fired.
Definition: lpm.hpp:89
nrfcxx::lpm::state_machine::MS_ENTRY_SETUP
static constexpr state_type MS_ENTRY_SETUP
States available to guard entry to a setup state.
Definition: lpm.hpp:159
nrfcxx::lpm::state_machine::MS_ENTRY_SAMPLE
static constexpr state_type MS_ENTRY_SAMPLE
States available to guard entry to a sample state.
Definition: lpm.hpp:183
nrfcxx::lpm::state_machine::fallback_alarm_pending
bool fallback_alarm_pending() const
true iff the current state is marked with STATE_FALLBACK_ALARM.
Definition: lpm.hpp:379
nrfcxx::lpm::state_machine::PF_APP_BASE
static constexpr process_flags_type PF_APP_BASE
First lpsm_capable::lpsm_process() flag bit available for application-specific result code bits.
Definition: lpm.hpp:255
nrfcxx::lpm::state_machine::post_event
void post_event()
Use the constructor-provided notifier to inform the application that the machine needs to be serviced...
Definition: lpm.hpp:386
nrfcxx::lpm::lpsm_capable::lpsm_reset_
virtual void lpsm_reset_()
Override to reset state that is held outside the machine.
Definition: lpm.hpp:471
nrfcxx::lpm::state_machine::MS_EXIT_RESET
static constexpr state_type MS_EXIT_RESET
State available to indicate that the machine is waiting to complete an internal reset operation.
Definition: lpm.hpp:142
nrfcxx::lpm::state_machine::STATE_FALLBACK_ALARM
static constexpr state_type STATE_FALLBACK_ALARM
Bit set in state_type when an alarm triggering post_event() has not yet fired.
Definition: lpm.hpp:106
nrfcxx::lpm::state_machine::reset
void reset()
Cancel any pending transitions and restore the state to its as-constructed value.
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::lpm::state_machine::state_type
unsigned int state_type
Representation for both generic and specific machine state.
Definition: lpm.hpp:46
nrfcxx::lpm::state_machine::error
error_type error() const
Read the current error code.
Definition: lpm.hpp:365
nrfcxx::lpm::state_machine::error_type
int error_type
Representation for values stored in error().
Definition: lpm.hpp:58