Browse Source

WIP: sequencer rework

master
parent
commit
ca38dfa2f1
4 changed files with 689 additions and 367 deletions
  1. +43
    -85
      include/drv/BG95_base.h
  2. +250
    -182
      include/utils/sequencer.h
  3. +42
    -40
      test/tests/BG95_base.cpp
  4. +354
    -60
      test/tests/sequencer.cpp

+ 43
- 85
include/drv/BG95_base.h View File

@@ -54,48 +54,7 @@ namespace tbx {
*
* \example implementation example
* \code
* using Queue = equeue<char, 512, true>;
*
* class BG95 :
* public BG95_base<BG95, Queue, 256> {
* using base_type = BG95_base<BG95, Queue, 256>;
* using range_t = typename Queue::range_t;
* private: // data
* Queue rx_q{};
* std::atomic<size_t> lines{};
* public:
* BG95() :
* rx_q(equeue<char, 512, true>::data_match::MATCH_PUSH, base_type::delimiter, [&](){
* lines.fetch_add(1, std::memory_order_acq_rel);
* }), lines(0) {
* // init code here ...
* }
* // ISR handler
* void usart_isr_ (void) {
* rx_q << // char from ISR
* }
* // CRTP requirements
* size_t get(char* data, bool wait =false) {
* do {
* if (lines.load(std::memory_order_acquire)) {
* size_t n =0;
* do{
* *data << rx_q;
* ++n;
* } while (*data++ != base_type::delimiter);
* lines.fetch_sub(1, std::memory_order_acq_rel);
* return n;
* }
* } while (wait);
* return 0;
* }
* size_t put (const char* data, size_t n) {
* // send data to UART
* return n;
* }
* const range_t contents() const { return range_t {rx_q.begin(), rx_q.end()}; }
* clock_t clock() { } // return systems CPU time
* };
* \endcode
*
* \tparam Impl_t
@@ -103,30 +62,30 @@ namespace tbx {
* \tparam N
* \tparam Delimiter
*/
template<typename Impl_t, typename Cont_t, size_t N, char Delimiter ='\n'>
template<typename Impl_t, size_t N, char Delimiter ='\n'>
class BG95_base
: public sequencer<BG95_base<Impl_t, Cont_t, N, Delimiter>, Cont_t, char, N>{
: public sequencer<BG95_base<Impl_t, N, Delimiter>, char, N>{
_CRTP_IMPL(Impl_t);
static_assert(
std::is_same_v<typename Cont_t::value_type, char>,
"Cont_t must be a container of type char"
);
// local type dispatch
using base_type = sequencer<BG95_base, Cont_t, char, N>;
using str_view_t = typename base_type::str_view_t;
using range_t = typename Cont_t::range_t;
using base_type = sequencer<BG95_base, char, N>;
//! \name Public types
//! @{
public:
using action_t = typename base_type::action_t;
using control_t = typename base_type::control_t;
using match_t = typename base_type::match_t;
using handler_ft = typename base_type::handler_ft;
using value_type = char;
using pointer_type = char*;
using size_type = size_t;
using string_view = typename base_type::string_view;
using action_t = typename base_type::action_t;
using control_t = typename base_type::control_t;
using match_ft = typename base_type::match_ft;
using handler_ft = typename base_type::handler_ft;
template<size_t Nm>
using script_t = typename base_type::template script_t<Nm>;
using script_t = typename base_type::template script_t<Nm>;
//! Publish delimiter
constexpr static char delimiter = Delimiter;
@@ -134,8 +93,8 @@ class BG95_base
//! Required types for inetd async handler operation
//! @{
struct inetd_handler_t {
str_view_t token;
match_t match;
string_view token;
match_ft match;
handler_ft handler;
};
template <size_t Nm>
@@ -169,8 +128,8 @@ class BG95_base
size_t put (const char* data, size_t n) {
return impl().put (data, n);
}
const range_t contents () const {
return impl().contents();
size_t contents (char* data) {
return impl().contents(data);
}
clock_t clock () {
return impl().clock();
@@ -197,30 +156,29 @@ class BG95_base
}
template <char Marker = '%'>
std::pair<size_t, bool> parse (const char* expected, const str_view_t buffer, char* token) {
if (*expected == Marker) {
// We have Marker. Copy to token the next part of buffer, from begin up to expected[1] where
// the expected[1] character is and return the size of that part.
auto next = std::find(buffer.begin(), buffer.end(), expected[1]);
if (next == buffer.end()) {
std::pair<size_t, bool> parse (const char* expected, const string_view buffer, char* token) {
do {
if (*expected == Marker) {
// We have Marker. Copy to token the next part of buffer, from begin up to expected[1] where
// the expected[1] character is and return the size of that part.
auto next = std::find(buffer.begin(), buffer.end(), expected[1]);
if (next == buffer.end())
break;
char* nullpos = std::copy(buffer.begin(), next, token);
*nullpos =0;
return std::make_pair(next - buffer.begin(), true);
}
else if (*expected == buffer.front()) {
// We have character match, copy the character to token and return 1 (the char size)
*token++ = buffer.front();
*token =0;
return std::make_pair(0, false);
return std::make_pair(1, false);
}
char* nullpos = std::copy(buffer.begin(), next, token);
*nullpos =0;
return std::make_pair(next - buffer.begin(), true);
}
else if (*expected == buffer.front()) {
// We have character match, copy the character to token and return 1 (the char size)
*token++ = buffer.front();
*token =0;
return std::make_pair(1, false);
}
else {
// Discrepancy. Return 0 (none parsed)
*token =0;
return std::make_pair(0, false);
}
} while (0);
// Fail to parse
*token =0;
return std::make_pair(0, false);
}
//! @}
@@ -297,7 +255,7 @@ class BG95_base
* \endcode
*/
template <typename T, char Marker = '%'>
bool command (const str_view_t cmd, const str_view_t expected, T* value, clock_t timeout =0) {
bool command (const string_view cmd, const string_view expected, T* value, clock_t timeout =0) {
char buffer[N];
char token[N];
@@ -343,7 +301,7 @@ class BG95_base
bool match = false;
if (inetd_handlers != nullptr) {
for (auto& h : *inetd_handlers)
match |= base_type::check_handle(h.token, h.match, h.handler, {buffer.data(), resp_size});
match |= base_type::check_handle({buffer.data(), resp_size}, h.token, h.match, h.handler);
}
// if no match forward data to receive channel.
if (!match) {


+ 250
- 182
include/utils/sequencer.h View File

@@ -39,7 +39,6 @@
#include <ctime>
#include <array>
#include <string_view>
#include <limits>
#include <type_traits>
#include <utility>
#include <tuple>
@@ -51,21 +50,36 @@ namespace tbx {
* \brief
* A CRTP base class to provide the sequencer functionality.
*
* Sequencer can automate communication with a terminal-like device such as AT-command modems etc...
* Sequencer is a script engine with receive/transmit functionalities based on predicates. It has:
* - A program counter like variable named \c step.
* - \c step actions like NEXT, GOTO exit with status etc...
* - Input data match predicates to trigger those actions.
* - Input data handlers to trigger external functionality on predicate match
* - Output data handlers to "edit" data before transmiting them
* - A small predicate set provided to the user. (starts_with, ends_with, contains).
*
* Sequencer can automate communication with a terminal-like device such as AT-command modems, can
* be used to implement communication protocols, or even small http servers.
*
* It can operate based on a script array and handle the outgoing commands and incoming responses.
* The user can create matching rules on received data and hook handlers and actions on them.
*
* The derived class (implementation) has to provide:
* 1) size_t get(Data_t* data);
* This function return 0 or a number of Data_t items. The data points to buffer for the input data.
*
* 3) size_t contents_ (Data_t* data);
* This function return 0 or a number of Data_t items without removing them from the implementer's container
* The data points to buffer for the input data.
*
* 2) size_t put(const Data_t* data, size_t n);
* This function sends to implementation the data pointed by \c data witch have size \c n.
* 3) clock_t clock();
*
* 4) clock_t clock();
* This function return a number to be used as time. The units of this function may be arbitrary but they
* match the units in \c record_t::timeout field.
*
* \tparam Impl_t The type of derived class
* \tparam Cont_t The container type holding the data of type \c Data_t for the derived class.
* \tparam Data_t The char-like stream item type. Usually \c char
* \tparam N The size of the sequence buffer to temporary store each line from get().
*
@@ -73,23 +87,27 @@ namespace tbx {
* We need access to derived class container to sneaky get a range of the data beside
* the normal data flow, in order to implement the \see control_t::DETECT operation.
*/
template <typename Impl_t, typename Cont_t, typename Data_t, size_t N>
template <typename Impl_t, typename Data_t, size_t N>
class sequencer {
_CRTP_IMPL(Impl_t);
static_assert(
std::is_same_v<typename Cont_t::value_type, Data_t>,
"Cont_t must be a container of type Data_t"
);
// local type dispatch
using range_t = typename Cont_t::range_t;
//! \name Public types
//! @{
public:
using value_type = Data_t;
using pointer_type = Data_t*;
using size_type = size_t;
using string_view = std::basic_string_view<Data_t>;
/*!
* The sequencer engine status. A variable of this type is returned by
* \see action_().
*/
enum class seq_status_t {
CONTINUE, //!< Means we keep looping
EXIT //!< Means, we exit with status the one indicated by \c action_t of the \c record_t
};
using str_view_t = std::basic_string_view<Data_t>;
//! \enum control_t
//! \brief The control type of the script entry.
enum class control_t {
@@ -107,28 +125,50 @@ class sequencer {
//! is buffered with a delimiter and we seek for data that don't follow the delimiter pattern.
//!
//! For example:
//! A modem sends reponses with '\n' termination but for some "special" command it opens a cursor
//! A modem sends responses with '\n' termination but for some "special" command it opens a cursor
//! lets say ">$ " without '\n' at the end.
};
//! \enum action_t
//! \brief Possible response actions for the sequencer
//! \brief
//! Possible response actions for the sequencer. This is the
//! equivalent of changing the program counter of the sequencer
//! and is composed by a type and a value.
//!
struct action_t {
enum {
NO =0, NEXT, GOTO, EXIT_OK, EXIT_ERROR
NO =0, //!< Do not change sequencer's step
NEXT, //!< Go to next sequencer step. In case of EXPECT/DETECT block of records
//!< skip the entire block of EXPECT[, OR_EXPECT[, OR_EXPECT ...]] and go
//!< to the next (non OR_*) control record.
GOTO, //!< Manually sets the step counter to the number of the \c step member.
EXIT, //!< Instruct for an exit returning the action.value as status
} type;
size_t step;
size_t value; //!< Used by \c GOTO to indicate the next sequencer's step.
};
//! \enum match_t
//! \brief Token match types
enum class match_t {
NO, STARTS_WITH, ENDS_WITH, CONTAINS, nSTARTS_WITH, nENDS_WITH, nCONTAINS
};
static constexpr action_t no_action = {action_t::NO, 0};
static constexpr action_t next = {action_t::NEXT, 0};
template <size_t GOTO>
static constexpr action_t go_to = {action_t::GOTO, static_cast<size_t>(GOTO)};
static constexpr action_t exit_ok = {action_t::EXIT, 0};
static constexpr action_t exit_error = {action_t::EXIT, static_cast<size_t>(-1)};
template <size_t Status>
static constexpr action_t exit = {action_t::EXIT, static_cast<size_t>(Status)};
/*!
* Match handler function pointer type.
* Expects a pointer to buffer and a size and returns status
* Match binary predicate function pointer type.
* Expects two string views and return a boolean.
* It is used by EXPECT/DETECT blocks to trigger their {handler, action} pair.
*/
using match_ft = bool (*) (const string_view haystack, const string_view needle);
/*!
* Send/Receive handler function pointer type.
* Expects a pointer to buffer and a size and returns status.
* It is used on predicate match on EXPECT/DETECT blocks, or as data wrapper on SEND blocks.
*/
using handler_ft = void (*) (const Data_t*, size_t);
@@ -136,16 +176,18 @@ class sequencer {
* \struct record_t
* \brief
* Describes the sequencer's script record entry (line).
*
* Each line consist from a control, 2 blocks and a timeout. The control says if we send or receive data.
* The blocks contain the data and the matching information. And the timeout guards the entire line.
*/
struct record_t {
control_t control; //!< The type of the entry
str_view_t token;
match_t match;
handler_ft handler; //!< The handler to called if the match is successful.
action_t action;
control_t control; //!< The control type of the entry
string_view token; //!< String view to token data. [MUST BE null terminated].
//!< This is passed as 2nd argument to match predicate on EXPECT/DETECT, or as
//! {data, size} pair to SEND handler and put_().
//!< If unused set it to ""
match_ft match; //!< Match predicate to used in EXPECT/DETECT blocks
//!< If unused set it to nullptr
handler_ft handler; //!< The handler to called if the match is successful, or before put_()
//!< If unused set it to nullptr
action_t action; //!< Indicates the step manipulation if the match is successful or after NOP and put_()
clock_t timeout; //!< Timeout in CPU time
};
@@ -156,35 +198,95 @@ class sequencer {
*
* The user can create arrays as the example bellow to act as a script.
* \code
* const std::array<Seq::record_t, 8> script = {{
* / * 0 * / {Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::GOTO, 1}, 1000}, //delay 1000 clocks
* / * 1 * / {Seq::control_t::SEND, {"ATE0\r\n", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
* / * 2 * / {Seq::control_t::EXPECT, {{
* {"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
* {"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
* 1000
* },
* // ...
* }};
* Seq s;
* const Seq::script_t<4> script = {{
* {Seq::control_t::NOP, "", Seq::nil, Seq::nil, {Seq::action_t::GOTO, 1}, 1000},
*
* {Seq::control_t::SEND, "ATE0\r\n", Seq::nil, Seq::nil, {Seq::action_t::NEXT, 0}, 0},
* {Seq::control_t::EXPECT, "OK\r\n", Seq::ends_with, Seq::nil, {Seq::action_t::EXIT_OK, 0}, 1000},
* {Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, {Seq::action_t::EXIT_ERROR, 0}, 0}
* }};
* s.run(script);
* \endcode
*/
template <size_t Nrecords>
using script_t = std::array<record_t, Nrecords>;
enum class seq_status_t {
CONTINUE, EXIT_OK, EXIT_ERROR
/*!
* \brief
* Check if the \c stream1 is equal to \c stream2
* \param stream1 The stream in witch we search [The input buffer]
* \param stream2 What we search [The record's token]
* \return True on success, false otherwise
*/
static constexpr auto equals = [](const string_view stream1, const string_view stream2) -> bool {
return (stream1 == stream2);
};
/*!
* \brief
* Check if the \c stream starts with the \c prefix
* \param stream The stream in witch we search [The input buffer]
* \param prefix What we search [The record's token]
* \return True on success, false otherwise
*/
static constexpr auto starts_with = [](const string_view stream, const string_view prefix) -> bool {
return (stream.rfind(prefix, 0) != string_view::npos);
};
/*!
* \brief
* Check if the \c stream ends with the \c postfix
* \param stream The stream in witch we search [The input buffer]
* \param postfix What we search [The record's token]
* \return True on success, false otherwise
*/
static constexpr auto ends_with = [](const string_view stream, const string_view postfix) -> bool {
if (stream.size() < postfix.size())
return false;
return (
stream.compare(
stream.size() - postfix.size(),
postfix.size(),
postfix) == 0
);
};
/*!
* \brief
* Check if the \c haystack contains the \c needle
* \param haystack The stream in witch we search [The input buffer]
* \param needle What we search [The record's token]
* \return True on success, false otherwise
*/
static constexpr auto contains = [](const string_view haystack, const string_view needle) -> bool {
return (haystack.find(needle) != string_view::npos);
};
//! Always false predicate
static constexpr auto always_true = [](const string_view s1, const string_view s2) -> bool {
(void)s1; (void)s2;
return true;
};
//! Always false predicate
static constexpr auto always_false = [](const string_view s1, const string_view s2) -> bool {
(void)s1; (void)s2;
return false;
};
//! Empty predicate or handler
static constexpr auto nil = nullptr;
//! @}
//! \name Constructor / Destructor
//! \name Object lifetime
//!@{
protected:
~sequencer () = default; //!< \brief Allow destructor from derived only
sequencer () = default; //!< \brief A default constructor from derived only
~sequencer () = default; //!< \brief Allow destructor from derived only
constexpr sequencer () noexcept = default; //!< \brief A default constructor from derived only
sequencer(const sequencer&) = delete; //!< No copies
sequencer& operator= (const sequencer&) = delete; //!< No copy assignments
//!@}
@@ -193,60 +295,23 @@ class sequencer {
//! @{
private:
size_t get_ (Data_t* data) { return impl().get (data); }
size_t contents_ (Data_t* data) { return impl().contents(data); }
size_t put_ (const Data_t* data, size_t n) { return impl().put (data, n); }
const range_t contents_ () const { return impl().contents(); }
clock_t clock_ () { return impl().clock(); }
//! @}
//! \name Private functionality
//! @{
private:
/*!
* \brief
* Check if the \c stream starts with the \c prefix
* \param stream The stream in witch we search
* \param prefix What we search
* \return True on success, false otherwise
*/
static bool starts_with_ (const str_view_t stream, const str_view_t prefix) {
return (stream.rfind(prefix, 0) != str_view_t::npos);
}
/*!
* \brief
* Check if the \c stream ends with the \c postfix
* \param stream The stream in witch we search
* \param postfix What we search
* \return True on success, false otherwise
*/
static bool ends_with_ (const str_view_t stream, const str_view_t postfix) {
if (stream.size() < postfix.size())
return false;
return (
stream.compare(
stream.size() - postfix.size(),
postfix.size(),
postfix) == 0
);
}
/*!
* \brief
* Check if the \c haystack contains the \c needle
* \param haystack The stream in witch we search
* \param needle What we search
* \return True on success, false otherwise
* Check if there is a handler and call it
* \param handler The handler to check
* \param buffer String view to buffer to pass to handler
* \return True if handler is called
*/
static bool contains_ (const str_view_t haystack, const str_view_t needle) {
return (haystack.find(needle) != str_view_t::npos);
}
bool is_step_extend (control_t control) {
return (control == control_t::OR_EXPECT || control == control_t::OR_DETECT);
}
static bool handle_ (handler_ft handler, const str_view_t buffer = str_view_t{}) {
if (handler) {
constexpr bool handle_ (handler_ft handler, const string_view buffer = string_view{}) {
if (handler != nullptr) {
handler (buffer.begin(), buffer.size());
return true;
}
@@ -255,25 +320,36 @@ class sequencer {
/*!
* \brief
* Return the new sequencer's step value.
* Return the new sequencer's step value and the sequencer's loop status as pair.
*
* Step is index to the sequencer's script array.
*
* \param current_idx The current step value
* \param action The advancing type
* \param go_idx The new value of the step in the case of GOTO type
* \return The new sequencer's step value
* \param script Reference to entire script.
* \param step The current step
* \return new step - status pair
*/
static std::pair<size_t, seq_status_t> action_ (const action_t& action, size_t step) {
switch (action.type) {
template <size_t Steps>
constexpr std::pair<size_t, seq_status_t> action_ (const script_t<Steps>& script, size_t step) {
control_t skip_while{};
size_t s;
switch (script[step].action.type) {
default:
case action_t::NO: return std::make_pair(step, seq_status_t::CONTINUE);
case action_t::NEXT: return std::make_pair(++step, seq_status_t::CONTINUE);
case action_t::GOTO: return std::make_pair(action.step, seq_status_t::CONTINUE);
case action_t::EXIT_OK:
return std::make_pair(std::numeric_limits<size_t>::max(), seq_status_t::EXIT_OK);
case action_t::EXIT_ERROR:
return std::make_pair(std::numeric_limits<size_t>::max(), seq_status_t::EXIT_ERROR);
case action_t::NO: return std::make_pair(step, seq_status_t::CONTINUE);
case action_t::NEXT:
switch (script[step].control) {
case control_t::NOP: return std::make_pair(++step, seq_status_t::CONTINUE);
case control_t::SEND: return std::make_pair(++step, seq_status_t::CONTINUE);
case control_t::EXPECT:
case control_t::OR_EXPECT: skip_while = control_t::OR_EXPECT; break;
case control_t::DETECT:
case control_t::OR_DETECT: skip_while = control_t::OR_DETECT; break;
}
s = step;
while (script[++s].control == skip_while)
;
return std::make_pair(s, seq_status_t::CONTINUE);
case action_t::GOTO: return std::make_pair(script[step].action.value, seq_status_t::CONTINUE);
case action_t::EXIT: return std::make_pair(script[step].action.value, seq_status_t::EXIT);
}
}
@@ -281,42 +357,25 @@ class sequencer {
public:
/*!
* \brief
* Checks if the \c needle matches the \c haystack.
*
* \param type The type of matching functionality
* \param haystack The stream in witch we search
* \param needle The stream we search
* \return True on match
*/
static bool match (const str_view_t haystack, const str_view_t needle, match_t type) {
switch (type) {
default:
case match_t::NO: return true;
case match_t::STARTS_WITH: return starts_with_(haystack, needle);
case match_t::ENDS_WITH: return ends_with_(haystack, needle);
case match_t::CONTAINS: return contains_(haystack, needle);
case match_t::nSTARTS_WITH: return !starts_with_(haystack, needle);
case match_t::nENDS_WITH: return !ends_with_(haystack, needle);
case match_t::nCONTAINS: return !contains_(haystack, needle);
}
}
//! \return The buffer size of the sequencer
constexpr size_t size() const { return N; }
/*!
* \brief
* A static functionality to provide access to sequencer's inner matching mechanism.
* Checks the \c buffer against \c handle and calls its action if needed.
*
* \param handle Reference to handle
* \param buffer The buffer to check
* \param buffer The buffer to check (1st parameter to match)
* \param token String view to check against buffer (2nd parameter to match)
* \param handler Function pointer to match predicate to use
* \param handle Reference to handle to call on match
*
* \return True on match, false otherwise
*/
static bool check_handle (const str_view_t token, match_t match_type, handler_ft handle, const str_view_t buffer) {
if (match(buffer, token, match_type)) {
handle_ (handle, buffer);
return true;
}
constexpr bool check_handle (const string_view buffer, const string_view token, match_ft match, handler_ft handle) {
if (match != nullptr && match(buffer, token))
return handle_ (handle, buffer);
return false;
}
@@ -325,92 +384,101 @@ class sequencer {
* Run the script array
*
* The main sequencer functionality. It starts with the first entry of the array.
* - If the entry is \c NOP it executes the action after the timeout.
* \c token and \c handler are discarded.
* - If the entry is \c SEND it uses the first handle block's token to send and executes the action after that.
* \c timeout is discarded.
* - If the entry is \c EXCEPTS it continuously try to receive data using implementation's get until one
* of the handle blocks match.
* On match:
* - Calls the handler if there is one
* - Executes the action
* - Skips the next handle blocks if there is any.
* If there is no match on timeout it return status_t::EXIT_ERROR
* - If the entry is \c DETECT it continuously try to detect data using implementation's contents until one
* of the handle blocks match.
* On match:
* - Calls the handler if there is one
* - Executes the action
* - Skips the next handle blocks if there is any.
* If there is no match on timeout it return status_t::EXIT_ERROR
*
* \tparam Steps The number of steps of the script
* \tparam Nhandles The number of handle blocks in the each script record.
* - If the record is \c NOP it executes the action after the timeout.
* \c NOP uses {\c action_t, \c timeout}.
* - If the record is \c SEND passes the token to handler (if any), then to put_() and executes the action after that.
* \c SEND uses {\c token, \c handler, \c action_t}
* - If the record is \c EXCEPT it continuously try to receive data using \see get_()
* * If no data until timeout, exit with failure
* * On data reception for this record AND for each OR_EXPECT that follows, calls the match predicate
* by passing the received data and token.
* On predicate match
* - Calls the handler if there is one
* - Executes the action. No farther EXPECT, OR_EXPECT, ... checks are made.
* - If the record is \c DETECT it continuously try to receive data using \see contents_()
* * If no data until timeout, exit with failure
* * On data reception for this record AND for each OR_DETECT that follows, calls the match predicate
* by passing the received data and token.
* On predicate match
* - Calls the handler if there is one
* - Executes the action. No farther DETECT, OR_DETECT, ... checks are made.
*
* \tparam Steps The number of records of the script
*
* \param script Reference to script to run
* \return The status of entire operation as described above
* \arg 0 Success
* \arg (size_t)-1 Failure
* \arg other Arbitrary return status
*/
template <size_t Steps>
bool run (const script_t<Steps>& script) {
size_t run (const script_t<Steps>& script) {
Data_t buffer[N];
size_t resp_size;
seq_status_t status{seq_status_t::CONTINUE};
size_t step =0, p_step =0;
clock_t mark = clock_();
do {
if (step != p_step) {
seq_status_t status{seq_status_t::CONTINUE}; do {
const record_t& record = script[step]; // get reference ot current line
if (step != p_step) { // renew time marker in each step
p_step = step;
mark = clock_();
}
switch (script[step].control) {
switch (record.control) {
default:
case control_t::NOP:
if ((clock_() - mark) >= script[step].timeout)
std::tie(step, status) = action_ (script[step].action, step);
if ((clock_() - mark) >= record.timeout)
std::tie(step, status) = action_ (script, step);
break;
case control_t::SEND:
if (put_(script[step].token.data(), script[step].token.size()) != script[step].token.size())
return false;
std::tie(step, status) = action_ (script[step].action, step);
if (record.handler != nullptr)
record.handler(record.token.data(), record.token.size());
if (put_(record.token.data(), record.token.size()) != record.token.size())
return exit_error.value;
std::tie(step, status) = action_ (script, step);
break;
case control_t::EXPECT:
case control_t::OR_EXPECT:
resp_size = get_(buffer);
if (resp_size) {
for (size_t s = step ; script[s].control == control_t::OR_EXPECT; ++s) {
if (match({buffer, resp_size}, script[s].token, script[s].match)) {
size_t s = step ; do{
if (script[s].match != nullptr && script[s].match({buffer, resp_size}, script[s].token)) {
handle_ (script[s].handler, {buffer, resp_size});
std::tie(step, status) = action_ (script[s].action, s);
std::tie(step, status) = action_ (script, s);
break;
}
}
} while (script[++s].control == control_t::OR_EXPECT);
}
if (script[step].timeout && (clock_() - mark) >= script[step].timeout)
return false;
if (record.timeout && (clock_() - mark) >= record.timeout)
return exit_error.value;
break;
case control_t::DETECT:
case control_t::OR_DETECT:
auto data = contents_();
if (data.begin() != data.end()) {
for (size_t s = step ; script[s].control == control_t::OR_DETECT ; ++s) {
if (match({buffer, resp_size}, script[s].token, script[s].match)) {
resp_size = contents_(buffer);
if (resp_size) {
size_t s = step ; do {
if (script[s].match != nullptr && script[s].match({buffer, resp_size}, script[s].token)) {
handle_ (script[s].handler, {buffer, resp_size});
std::tie(step, status) = action_ (script[s].action, s);
std::tie(step, status) = action_ (script, s);
break;
}
}
} while (script[++s].control == control_t::OR_DETECT);
}
if (script[step].timeout && (clock_() - mark) >= script[step].timeout)
return false;
if (record.timeout && (clock_() - mark) >= record.timeout)
return exit_error.value;
break;
} // switch (it.control)
} // switch (record.control)
} while ( status == seq_status_t::CONTINUE);
return (status == seq_status_t::EXIT_OK);
return step; // step here is set by action_ as the return status
}
};


+ 42
- 40
test/tests/BG95_base.cpp View File

@@ -50,10 +50,10 @@ namespace test_bg95_base {
// BG95 implementer mock
template<size_t N>
class BG95 : public BG95_base<BG95<N>, equeue<char, N, true>, N> {
class BG95 : public BG95_base<BG95<N>, N> {
using Q = equeue<char, N, true>;
using base_type = BG95_base<BG95<N>, Q, N>;
using base_type = BG95_base<BG95<N>, N>;
public:
enum class event {
@@ -140,6 +140,11 @@ namespace test_bg95_base {
} while (wait);
return 0;
}
size_t contents(char* data) {
char* nullpos = std::copy(rx_q.begin(), rx_q.end(), data);
*nullpos =0;
return nullpos - data;
}
size_t put (const char* data, size_t n) {
std::cerr << " ";
const char* reply = cmd_responce (data);
@@ -147,9 +152,7 @@ namespace test_bg95_base {
rx_q << *reply++;
return n;
}
const range_t contents() const {
return range_t {rx_q.begin(), rx_q.end()};
}
clock_t clock() { static clock_t t=0; return ++t; }
// extra helper for testing purposes
@@ -179,8 +182,8 @@ namespace test_bg95_base {
char buffer[256];
const BG95<256>::inetd_handlers<2> async = {{
{"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler},
{"+QMT", BG95<256>::match_t::STARTS_WITH, handler},
{"+QMTOPEN:", BG95<256>::starts_with, handler},
{"+QMT", BG95<256>::starts_with, handler},
}};
clear_flag();
@@ -222,39 +225,38 @@ namespace test_bg95_base {
EXPECT_EQ (modem.receive(buffer), 0UL);
}
TEST(TBG95_base, run) {
BG95<256> modem;
using Control = BG95<256>::control_t;
using Match = BG95<256>::match_t;
using Action = BG95<256>::action_t;
const BG95<256>::inetd_handlers<2> async = {{
{"+QMTOPEN:", Match::STARTS_WITH, handler},
{"+QMT", Match::STARTS_WITH, handler},
}};
const BG95<256>::script_t<7> script = {{
{Control::NOP, "", Match::NO, nullptr, {Action::GOTO, 1}, 1000},
{Control::SEND, "ATE0\r\n", Match::NO, nullptr, {Action::NEXT, 0}, 0},
{Control::EXPECT, "OK\r\n", Match::ENDS_WITH, nullptr, {Action::NEXT, 0}, 1000},
{Control::OR_EXPECT,"ERROR", Match::CONTAINS, nullptr, {Action::EXIT_ERROR, 0}, 1000},
{Control::SEND, "AT+CSQ\r\n", Match::NO, nullptr, {Action::NEXT, 0}, 0},
{Control::EXPECT, "OK\r\n", Match::ENDS_WITH, nullptr, {Action::NEXT, 0}, 1000},
{Control::OR_EXPECT,"ERROR", Match::CONTAINS, nullptr, {Action::EXIT_ERROR, 0}, 1000},
}};
std::mutex m;
m.lock();
std::thread th1 ([&](){
do
modem.inetd(false, &async);
while (!m.try_lock());
m.unlock();
});
EXPECT_EQ (modem.run(script), true);
m.unlock(); // stop and join inetd
th1.join();
}
// TEST(TBG95_base, run) {
// BG95<256> modem;
//
// using Control = BG95<256>::control_t;
// using Action = BG95<256>::action_t;
//
// const BG95<256>::inetd_handlers<2> async = {{
// {"+QMTOPEN:", BG95<256>::starts_with, handler},
// {"+QMT", BG95<256>::starts_with, handler},
// }};
// const BG95<256>::script_t<7> script = {{
// {Control::NOP, "", BG95<256>::nil, BG95<256>::nil, {Action::GOTO, 1}, 1000},
// {Control::SEND, "ATE0\r\n", BG95<256>::nil, BG95<256>::nil, {Action::NEXT, 0}, 0},
// {Control::EXPECT, "OK\r\n", BG95<256>::ends_with, BG95<256>::nil, {Action::NEXT, 0}, 1000},
// {Control::OR_EXPECT,"ERROR", BG95<256>::contains, BG95<256>::nil, {Action::EXIT_ERROR, 0}, 0},
// {Control::SEND, "AT+CSQ\r\n",BG95<256>::nil, BG95<256>::nil, {Action::NEXT, 0}, 0},
// {Control::EXPECT, "OK\r\n", BG95<256>::ends_with, BG95<256>::nil, {Action::EXIT_OK, 0}, 1000},
// {Control::OR_EXPECT,"ERROR", BG95<256>::contains, BG95<256>::nil, {Action::EXIT_ERROR, 0}, 0},
// }};
//
// std::mutex m;
// m.lock();
// std::thread th1 ([&](){
// do
// modem.inetd(false, &async);
// while (!m.try_lock());
// m.unlock();
// });
// EXPECT_EQ (modem.run(script), true);
// m.unlock(); // stop and join inetd
// th1.join();
// }
TEST(TBG95_base, command) {
BG95<256> modem;


+ 354
- 60
test/tests/sequencer.cpp View File

@@ -29,103 +29,397 @@
#include <utils/sequencer.h>
#include <gtest/gtest.h>
#include <type_traits>
#include <cstring>
#include <ctime>
namespace test_sequencer {
using namespace tbx;
struct seq_cont_t {
const char *msg_[10] = {
"", "", "", "\r\nOK\r\n",
"", "", "+CCLK = \"21/08/26-12:16:30+12\"\r\nOK\r\n"
"", "", "\r\nERROR\r\n"
};
using value_type = char;
using range_t = range<const char*>;
};
seq_cont_t seq_cont;
// test settings
using data_type = char;
constexpr size_t size = 64;
// Sequencer implementer mock
class Seq : public sequencer<Seq, seq_cont_t, char, 128> {
using range_t = typename seq_cont_t::range_t;
class Seq : public sequencer<Seq, data_type, size> {
static constexpr int NrCommands =5;
static constexpr int NoCommand =-1;
std::array<const char*, NrCommands> command = {
"cmd1",
"cmd2\n",
"cmd3\r\n",
"cmd4\n\r",
"cmd5\n",
};
std::array<const char*, NrCommands> reply {
"reply1",
"reply2\n",
"reply3\n text \r text text\n",
"reply4\n text\n text \r text\r\n",
"reply5\n",
};
int cmd =NoCommand;
clock_t t =0;
public:
size_t get(char* data) {
static int msg =0;
size_t len = strlen(seq_cont.msg_[msg]);
strcpy(data, seq_cont.msg_[msg++]);
if (msg >= 10) msg =0;
return len;
static int ans = 0;
if ((++ans % 3) == 0)
return 0;
if (cmd == NoCommand) {
std::strcpy(data, "ERROR\n");
return 6;
} else {
std::strcpy(data, reply[cmd]);
size_t s = std::strlen(reply[cmd]);
cmd =NoCommand;
return s;
}
}
size_t contents (char* data) {
if (cmd == NoCommand) {
std::strcpy(data, "");
return 0;
} else {
std::strcpy(data, reply[cmd]);
return std::strlen(reply[cmd]);
}
}
size_t put (const char* data, size_t n) {
(void)*data;
for (size_t i =0 ; i<NrCommands ; ++i) {
if (!std::strcmp(data, command[i])) {
cmd =i;
return n;
}
}
cmd =NoCommand;
return n;
}
const range_t contents () const {
return range_t {&seq_cont.msg_[6][0], &seq_cont.msg_[6][5]};
}
clock_t clock() { static clock_t t=0; return ++t; }
static void my_handler (const char* data, size_t size) {
(void)*data;
(void)size;
}
clock_t clock() { return ++t; }
void clear_clock() { t =0; }
};
/*
* Test sequencer.run()
* Test sequencer object
*/
TEST(Tsequencer, run_delay) {
TEST (Tsequencer, traits) {
EXPECT_EQ ( std::is_default_constructible<Seq>::value, true);
EXPECT_EQ ( std::is_nothrow_default_constructible<Seq>::value, true);
EXPECT_EQ (!std::is_copy_constructible<Seq>::value, true);
EXPECT_EQ (!std::is_copy_assignable<Seq>::value, true);
EXPECT_EQ ((std::is_same_v<Seq::value_type, data_type>), true);
EXPECT_EQ ((std::is_same_v<Seq::pointer_type, data_type*>), true);
EXPECT_EQ ((std::is_same_v<Seq::size_type, size_t>), true);
EXPECT_EQ ((std::is_same_v<Seq::string_view, std::basic_string_view<data_type>>), true);
Seq s;
const Seq::script_t<1> script = {{
{Seq::control_t::NOP, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}
}};
EXPECT_EQ (s.run(script), true);
EXPECT_EQ (s.size(), size);
}
TEST (Tsequencer, predicates) {
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::equals), Seq::string_view, Seq::string_view>::value), true);
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::starts_with), Seq::string_view, Seq::string_view>::value), true);
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::ends_with), Seq::string_view, Seq::string_view>::value), true);
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::contains), Seq::string_view, Seq::string_view>::value), true);
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::always_true), Seq::string_view, Seq::string_view>::value), true);
EXPECT_EQ ((std::is_invocable_r<bool, decltype(Seq::always_false), Seq::string_view, Seq::string_view>::value), true);
EXPECT_EQ (Seq::nil, nullptr);
}
TEST(Tsequencer, run_dummy_output) {
TEST (Tsequencer, actions) {
EXPECT_EQ ( std::is_default_constructible<Seq::action_t>::value, true);
EXPECT_EQ ( std::is_nothrow_default_constructible<Seq::action_t>::value, true);
EXPECT_EQ ( std::is_copy_constructible<Seq::action_t>::value, true);
EXPECT_EQ ( std::is_copy_assignable<Seq::action_t>::value, true);
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::no_action)>), true);
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::next)>), true);
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::exit_ok)>), true);
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::exit_error)>), true);
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::go_to<0>)>), true);
EXPECT_EQ ((std::is_same_v<const Seq::action_t, decltype(Seq::exit<0>)>), true);
}
bool handler_flag = false;
const char* text = "abc";
//static bool check_handle (const str_view_t buffer, const str_view_t token, match_ft match, handler_ft handle)
TEST(Tsequencer, check_handle) {
Seq s;
const Seq::script_t<2> script = {{
{Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
{Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}
}};
EXPECT_EQ (s.run(script), true);
// foo (5);
// bar (5);
using str_t = Seq::string_view;
using val_t = Seq::value_type;
auto match = [](const str_t x, const str_t y) ->bool { (void)x; (void)y; return true; };
auto no_match = [](const str_t x, const str_t y) ->bool { (void)x; (void)y; return false; };
auto check_match = [] (const str_t x, const str_t y) ->bool {
return x == y;
};
auto handler = [](const val_t* v, size_t s){ (void)*v; (void)s; handler_flag = true; };
auto set_if_abc = [](const val_t* v, size_t s){
(void)*v; (void)s;
handler_flag = (str_t(v, s) == "abc");
};
EXPECT_EQ (s.check_handle("", "", nullptr, nullptr), false);
EXPECT_EQ (s.check_handle("", "", no_match, nullptr), false);
EXPECT_EQ (s.check_handle("", "", match, nullptr), false);
handler_flag = false;
EXPECT_EQ (s.check_handle("", "", no_match, handler), false);
EXPECT_EQ (handler_flag, false);
handler_flag = false;
EXPECT_EQ (s.check_handle("", "", match, handler), true);
EXPECT_EQ (handler_flag, true);
handler_flag = false;
EXPECT_EQ (s.check_handle("abcd", "abc", check_match, set_if_abc), false);
EXPECT_EQ (handler_flag, false);
handler_flag = false;
EXPECT_EQ (s.check_handle("abc", "abc", check_match, set_if_abc), true);
EXPECT_EQ (handler_flag, true);
handler_flag = false;
EXPECT_EQ (s.check_handle("abc", "abcd", check_match, set_if_abc), false);
EXPECT_EQ (handler_flag, false);
}
TEST(Tsequencer, run_exits) {
TEST(Tsequencer, run_nop_and_exits) {
Seq s;
const Seq::script_t<1> script1 = {{
{Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000},
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
}};
s.clear_clock();
EXPECT_EQ (s.run(script1), Seq::exit_ok.value);
EXPECT_GE (s.clock(), (clock_t)1000);
const Seq::script_t<1> script2 = {{
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_error, 1000}
}};
s.clear_clock();
EXPECT_EQ (s.run(script2), Seq::exit_error.value);
EXPECT_GE (s.clock(), (clock_t)1000);
const Seq::script_t<3> script3 = {{
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::next, 1000},
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::next, 1000},
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
}};
s.clear_clock();
EXPECT_EQ (s.run(script3), Seq::exit_ok.value);
EXPECT_GE (s.clock(), (clock_t)3000);
}
TEST(Tsequencer, run_send) {
Seq s;
auto send_wrapper = [](const data_type* d, size_t s){
(void)*d; (void)s; handler_flag = true;
};
auto send_chk_text = [](const data_type* d, size_t s){
handler_flag = (Seq::string_view(d,s) == Seq::string_view(text));
};
const Seq::script_t<2> script1 = {{
{Seq::control_t::SEND, "", Seq::nil, Seq::nil, Seq::next, 0},
{Seq::control_t::SEND, "", Seq::nil, send_wrapper, Seq::exit_ok, 0}
}};
EXPECT_EQ (s.run(script1), true);
handler_flag =false;
EXPECT_EQ (s.run(script1), Seq::exit_ok.value);
EXPECT_EQ (handler_flag, true);
const Seq::script_t<1> script2 = {{
{Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
{Seq::control_t::SEND, "abcd", Seq::nil, send_chk_text, Seq::exit_ok, 0}
}};
EXPECT_EQ (s.run(script2), false);
handler_flag =false;
EXPECT_EQ (s.run(script2), Seq::exit_ok.value);
EXPECT_EQ (handler_flag, false);
const Seq::script_t<2> script3 = {{
{Seq::control_t::SEND, text, Seq::nil, send_chk_text, Seq::exit_ok, 0}
}};
handler_flag =false;
EXPECT_EQ (s.run(script3), Seq::exit_ok.value);
EXPECT_EQ (handler_flag, true);
}
TEST(Tsequencer, run_expect) {
Seq s;
const Seq::script_t<7> script = {{
{Seq::control_t::EXPECT, "reply1", Seq::equals, Seq::nil, Seq::exit<1UL>, 1000},
{Seq::control_t::OR_EXPECT, "reply2", Seq::starts_with, Seq::nil, Seq::exit<2UL>, 0},
{Seq::control_t::OR_EXPECT, "reply3", Seq::starts_with, Seq::nil, Seq::exit<3UL>, 0},
{Seq::control_t::OR_EXPECT, "reply4\n", Seq::starts_with, Seq::nil, Seq::exit<4UL>, 0},
{Seq::control_t::OR_EXPECT, "reply5", Seq::starts_with, Seq::nil, Seq::exit<5UL>, 0},
{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit<6UL>, 0},
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_error, 1000}
}};
s.clear_clock();
s.put("cmd1", std::strlen("cmd1"));
EXPECT_EQ (s.run(script), 1UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd2\n", std::strlen("cmd2\n"));
EXPECT_EQ (s.run(script), 2UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd3\r\n", std::strlen("cmd3\r\n"));
EXPECT_EQ (s.run(script), 3UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd4\n\r", std::strlen("cmd4\n\r"));
EXPECT_EQ (s.run(script), 4UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd5\n", std::strlen("cmd5\n"));
EXPECT_EQ (s.run(script), 5UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd", std::strlen("cmd"));
EXPECT_EQ (s.run(script), 6UL);
EXPECT_LT (s.clock(), (clock_t)1000);
}
TEST(Tsequencer, run_sequence) {
TEST(Tsequencer, run_detect) {
Seq s;
const Seq::script_t<13> script = {{
/* 0 */{Seq::control_t::NOP, "", Seq::match_t::NO, nullptr, {Seq::action_t::GOTO, 1}, 1000},
/* 1 */{Seq::control_t::SEND, "ATE0\r\n", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
/* 2 */{Seq::control_t::EXPECT, "OK\r\n", Seq::match_t::ENDS_WITH, nullptr, {Seq::action_t::NEXT, 0}, 1000},
/* 3 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
const Seq::script_t<7> script = {{
{Seq::control_t::DETECT, "reply1", Seq::equals, Seq::nil, Seq::exit<1UL>, 1000},
{Seq::control_t::OR_DETECT, "reply2", Seq::starts_with, Seq::nil, Seq::exit<2UL>, 0},
{Seq::control_t::OR_DETECT, "reply3", Seq::starts_with, Seq::nil, Seq::exit<3UL>, 0},
{Seq::control_t::OR_DETECT, "reply4\n", Seq::starts_with, Seq::nil, Seq::exit<4UL>, 0},
{Seq::control_t::OR_DETECT, "reply5", Seq::starts_with, Seq::nil, Seq::exit<5UL>, 0},
{Seq::control_t::OR_DETECT, "ERROR", Seq::contains, Seq::nil, Seq::exit<6UL>, 0},
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
}};
/* 4 */{Seq::control_t::DETECT, "+CCLK", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::NEXT, 0}, 1000},
/* 5 */{Seq::control_t::OR_DETECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
s.clear_clock();
s.put("cmd1", std::strlen("cmd1"));
EXPECT_EQ (s.run(script), 1UL);
EXPECT_LT (s.clock(), (clock_t)1000);
/* 6 */{Seq::control_t::SEND, "AT+CCLK?", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
/* 7 */{Seq::control_t::EXPECT, "OK\r\n", Seq::match_t::ENDS_WITH, Seq::my_handler, {Seq::action_t::NEXT, 0}, 1000},
/* 8 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
s.clear_clock();
s.put("cmd2\n", std::strlen("cmd2\n"));
EXPECT_EQ (s.run(script), 2UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd3\r\n", std::strlen("cmd3\r\n"));
EXPECT_EQ (s.run(script), 3UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd4\n\r", std::strlen("cmd4\n\r"));
EXPECT_EQ (s.run(script), 4UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd5\n", std::strlen("cmd5\n"));
EXPECT_EQ (s.run(script), 5UL);
EXPECT_LT (s.clock(), (clock_t)1000);
s.clear_clock();
s.put("cmd", std::strlen("cmd"));
EXPECT_EQ (s.run(script), Seq::exit_error.value);
EXPECT_GT (s.clock(), (clock_t)1000);
}
/* 9 */{Seq::control_t::SEND, "AT+CT?", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
/*10 */{Seq::control_t::EXPECT, "OK\r\n", Seq::match_t::ENDS_WITH, nullptr, {Seq::action_t::NEXT, 0}, 1000},
/*11 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
TEST(Tsequencer, run_script_blocks_n_gotos) {
Seq s;
const Seq::script_t<15> script = {{
/* 0 */{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::go_to<1>, 1000},
/* 1 */{Seq::control_t::SEND, "cmd1", Seq::nil, Seq::nil, Seq::next, 0},
/* 2 */{Seq::control_t::EXPECT, "reply1", Seq::starts_with, Seq::nil, Seq::next, 1000},
/* 3 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 0},
/* 4 */{Seq::control_t::SEND, "cmd2\n", Seq::nil, Seq::nil, Seq::next, 0},
/* 5 */{Seq::control_t::DETECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 1000},
/* 6 */{Seq::control_t::OR_DETECT, "reply2", Seq::contains, Seq::nil, Seq::go_to<11>, 0},
/* 7 */{Seq::control_t::SEND, "cmd3\r\n", Seq::nil, Seq::nil, Seq::next, 0},
/* 8 */{Seq::control_t::EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 1000},
/* 9 */{Seq::control_t::OR_EXPECT, "lalala", Seq::starts_with, Seq::nil, Seq::exit_error, 0},
/*10 */{Seq::control_t::OR_EXPECT, "text\n", Seq::ends_with, Seq::nil, Seq::go_to<14>, 0},
/*11 */{Seq::control_t::SEND, "cmd4\n\r", Seq::nil, Seq::nil, Seq::next, 0},
/*12 */{Seq::control_t::EXPECT, "reply4\n", Seq::starts_with, Seq::nil, Seq::go_to<7>, 1000},
/*13 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 0},
/*14 */{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::exit_ok, 1000}
}};
s.clear_clock();
EXPECT_EQ (s.run(script), Seq::exit_ok.value);
EXPECT_GT (s.clock(), (clock_t)2000);
}
TEST(Tsequencer, run_match_n_handler) {
Seq s;
using str_t = Seq::string_view;
using val_t = Seq::value_type;
auto match = [](const str_t x, const str_t y) ->bool { (void)x; (void)y; return true; };
auto check_match = [] (const str_t x, const str_t y) ->bool {
return x == y;
};
auto handler = [](const val_t* v, size_t s){ (void)*v; (void)s; handler_flag = true; };
auto set_if_rpl2 = [](const val_t* v, size_t s){
(void)*v; (void)s;
handler_flag = (str_t(v, s) == "reply2\n");
};
const Seq::script_t<4> script1 = {{
{Seq::control_t::SEND, "cmd1", Seq::nil, Seq::nil, Seq::next, 0},
{Seq::control_t::EXPECT, "", match, Seq::nil, Seq::next, 1000},
{Seq::control_t::OR_EXPECT, "ERROR", Seq::contains, Seq::nil, Seq::exit_error, 0},
{Seq::control_t::SEND, "cmd1", Seq::nil, handler, Seq::exit_ok, 0}
}};
handler_flag = false;
s.clear_clock();
EXPECT_EQ (s.run(script1), Seq::exit_ok.value);
EXPECT_LT (s.clock(), (clock_t)1000);
EXPECT_EQ (handler_flag, true);
const Seq::script_t<2> script2 = {{
{Seq::control_t::SEND, "cmd1", Seq::nil, Seq::nil, Seq::next, 0},
{Seq::control_t::EXPECT, "reply1", check_match, set_if_rpl2, Seq::exit_ok, 1000},
}};
handler_flag = false;
EXPECT_EQ (s.run(script2), Seq::exit_ok.value);
EXPECT_EQ (handler_flag, false);
const Seq::script_t<2> script3 = {{
{Seq::control_t::SEND, "cmd2\n", Seq::nil, Seq::nil, Seq::next, 0},
{Seq::control_t::EXPECT, "reply2\n", check_match, set_if_rpl2, Seq::exit_ok, 1000},
}};
handler_flag = false;
EXPECT_EQ (s.run(script3), Seq::exit_ok.value);
EXPECT_EQ (handler_flag, true);
/*12 */{Seq::control_t::SEND, "AT+POWD=0", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}
const Seq::script_t<1> script4 = {{
{Seq::control_t::SEND, "cmd1", Seq::nil, handler, Seq::exit_ok, 0}
}};
EXPECT_EQ (s.run(script), false);
handler_flag = false;
EXPECT_EQ (s.run(script4), Seq::exit_ok.value);
EXPECT_EQ (handler_flag, true);
}
}

Loading…
Cancel
Save