@@ -31,6 +31,7 @@ | |||||
#ifndef TBX_DRV_BG95_base_H_ | #ifndef TBX_DRV_BG95_base_H_ | ||||
#define TBX_DRV_BG95_base_H_ | #define TBX_DRV_BG95_base_H_ | ||||
#define __cplusplus 201703L | |||||
#include <core/core.h> | #include <core/core.h> | ||||
#include <core/crtp.h> | #include <core/crtp.h> | ||||
@@ -39,10 +40,10 @@ | |||||
#include <utils/sequencer.h> | #include <utils/sequencer.h> | ||||
#include <cstring> | #include <cstring> | ||||
#include <cstdio> | |||||
#include <cstdlib> | |||||
#include <algorithm> | #include <algorithm> | ||||
//#include <iostream> | |||||
#include <utility> | |||||
#include <atomic> | #include <atomic> | ||||
namespace tbx { | namespace tbx { | ||||
@@ -116,7 +117,6 @@ class BG95_base | |||||
using base_type = sequencer<BG95_base, Cont_t, char, N>; | using base_type = sequencer<BG95_base, Cont_t, char, N>; | ||||
using str_view_t = typename base_type::str_view_t; | using str_view_t = typename base_type::str_view_t; | ||||
using range_t = typename Cont_t::range_t; | using range_t = typename Cont_t::range_t; | ||||
using status_t = typename base_type::status_t; | |||||
//! \name Public types | //! \name Public types | ||||
//! @{ | //! @{ | ||||
@@ -124,17 +124,22 @@ class BG95_base | |||||
using action_t = typename base_type::action_t; | using action_t = typename base_type::action_t; | ||||
using control_t = typename base_type::control_t; | using control_t = typename base_type::control_t; | ||||
using match_t = typename base_type::match_t; | using match_t = typename base_type::match_t; | ||||
using handler_t = typename base_type::handler_ft; | |||||
template<size_t Nr, size_t Nh =2> | |||||
using script_t = typename base_type::template script_t<Nr, Nh>; | |||||
using handler_ft = typename base_type::handler_ft; | |||||
template<size_t Nm> | |||||
using script_t = typename base_type::template script_t<Nm>; | |||||
//! Publish delimiter | //! Publish delimiter | ||||
constexpr static char delimiter = Delimiter; | constexpr static char delimiter = Delimiter; | ||||
//! Required typenames for async operation | |||||
//! Required types for inetd async handler operation | |||||
//! @{ | //! @{ | ||||
struct inetd_handler_t { | |||||
str_view_t token; | |||||
match_t match; | |||||
handler_ft handler; | |||||
}; | |||||
template <size_t Nm> | template <size_t Nm> | ||||
using async_handlers = std::array<typename base_type::handle_t, Nm>; | |||||
using inetd_handlers = std::array<inetd_handler_t, Nm>; | |||||
//! @} | //! @} | ||||
//! @} | //! @} | ||||
@@ -176,32 +181,48 @@ class BG95_base | |||||
//! @{ | //! @{ | ||||
private: | private: | ||||
template<typename T> | template<typename T> | ||||
ptrdiff_t parse (char* str, size_t n, char next, T& value) { | |||||
auto next_ptr = std::find(str, &str[n], next); | |||||
char save = *next_ptr; | |||||
*next_ptr =0; | |||||
void extract (const char* str, T* value) { | |||||
static_assert ( | |||||
std::is_same_v<std::remove_cv_t<T>, int> | |||||
|| std::is_same_v<std::remove_cv_t<T>, double> | |||||
|| std::is_same_v<std::remove_cv_t<T>, char>, | |||||
"Not supported conversion type."); | |||||
if constexpr (std::is_same_v<std::remove_cv_t<T>, int>) { | if constexpr (std::is_same_v<std::remove_cv_t<T>, int>) { | ||||
sscanf(str, "%d", &value); | |||||
} else if (std::is_same_v<std::remove_cv_t<T>, float>) { | |||||
sscanf(str, "%f", &value); | |||||
*value = std::atoi(str); | |||||
} else if (std::is_same_v<std::remove_cv_t<T>, double>) { | } else if (std::is_same_v<std::remove_cv_t<T>, double>) { | ||||
sscanf(str, "%lf", &value); | |||||
*value = std::atof(str); | |||||
} else if (std::is_same_v<std::remove_cv_t<T>, char>) { | } else if (std::is_same_v<std::remove_cv_t<T>, char>) { | ||||
sscanf(str, "%c", &value); | |||||
std::strcpy(value, str); | |||||
} | } | ||||
*next_ptr = save; | |||||
return next_ptr - str; | |||||
} | } | ||||
ptrdiff_t parse (char* str, size_t n, char next, char* value) { | |||||
auto next_ptr = std::find(str, &str[n], next); | |||||
char save = *next_ptr; | |||||
*next_ptr =0; | |||||
strcpy(value, str); | |||||
*next_ptr = save; | |||||
return next_ptr - str; | |||||
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()) { | |||||
*token =0; | |||||
return std::make_pair(0, 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); | |||||
} | |||||
} | } | ||||
//! @} | //! @} | ||||
//! \name public functionality | //! \name public functionality | ||||
@@ -248,9 +269,6 @@ class BG95_base | |||||
return 0; | return 0; | ||||
} | } | ||||
// cmd: "AT+CREG?" | |||||
// expected: "\r\n+CREG: 0,%\r\n\r\nOK\r\n" | |||||
/*! | /*! | ||||
* \brief | * \brief | ||||
* Send a command to modem and check if the response matches to | * Send a command to modem and check if the response matches to | ||||
@@ -279,32 +297,26 @@ class BG95_base | |||||
* \endcode | * \endcode | ||||
*/ | */ | ||||
template <typename T, char Marker = '%'> | template <typename T, char Marker = '%'> | ||||
bool command (const char* cmd, const str_view_t expected, T& t, clock_t timeout =0) { | |||||
bool command (const str_view_t cmd, const str_view_t expected, T* value, clock_t timeout =0) { | |||||
char buffer[N]; | char buffer[N]; | ||||
char token[N]; | |||||
transmit(cmd); | |||||
transmit(cmd.data()); // send command | |||||
for (size_t pos =0, i=0, j=0 ; pos < expected.size(); pos += j) { | |||||
str_view_t ex = expected.substr(pos); // get starting point of expected | |||||
for (auto ex = expected.begin() ; ex != expected.end() ; ) { | |||||
clock_t mark = clock(); // load the answer with timeout | |||||
size_t sz =0; | size_t sz =0; | ||||
clock_t mark = clock(); | |||||
do { | do { | ||||
sz = receive(buffer); // load the answer with timeout | |||||
if ((timeout != 0 )&& ((clock() - mark) >= timeout)) { | |||||
sz = receive(buffer); | |||||
if ((timeout != 0 )&& ((clock() - mark) >= timeout)) | |||||
return false; | return false; | ||||
} | |||||
} while (!sz); | } while (!sz); | ||||
for (i=0, j=0 ; i<sz ; ) { | |||||
if (ex[j] == Marker) { | |||||
i += parse(&buffer[i], sz, ex[j+1], t); // parse and convert | |||||
++j; | |||||
} | |||||
else if (ex[j] == buffer[i]) { | |||||
++i; // if same consume current character | |||||
++j; | |||||
} | |||||
else | |||||
return false; // Fail to parse | |||||
for (size_t i=0 ; i<sz ; ) { // parse buffer based on expected | |||||
auto [step, marker] = parse<Marker> (ex++, {&buffer[i], sz-i}, token); | |||||
if (!step) return false; | |||||
if (marker) extract(token, value); | |||||
i += step; | |||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
@@ -322,16 +334,16 @@ class BG95_base | |||||
* \param loop Flag to indicate blocking mode. If true blocking. | * \param loop Flag to indicate blocking mode. If true blocking. | ||||
*/ | */ | ||||
template <size_t Nm =0> | template <size_t Nm =0> | ||||
void inetd (bool loop =true, const async_handlers<Nm>* async_handles =nullptr) { | |||||
void inetd (bool loop =true, const inetd_handlers<Nm>* inetd_handlers =nullptr) { | |||||
std::array<char, N> buffer; | std::array<char, N> buffer; | ||||
size_t resp_size; | size_t resp_size; | ||||
do { | do { | ||||
if ((resp_size = get_(buffer.data())) != 0) { | if ((resp_size = get_(buffer.data())) != 0) { | ||||
// on data check for async handlers | // on data check for async handlers | ||||
bool match = false; | bool match = false; | ||||
if (async_handles != nullptr) { | |||||
for (auto& h : *async_handles) | |||||
match |= base_type::check_handle (h, buffer.data()); | |||||
if (inetd_handlers != nullptr) { | |||||
for (auto& h : *inetd_handlers) | |||||
match |= base_type::check_handle(h.token, h.match, h.handler, {buffer.data(), resp_size}); | |||||
} | } | ||||
// if no match forward data to receive channel. | // if no match forward data to receive channel. | ||||
if (!match) { | if (!match) { | ||||
@@ -41,7 +41,8 @@ | |||||
#include <string_view> | #include <string_view> | ||||
#include <limits> | #include <limits> | ||||
#include <type_traits> | #include <type_traits> | ||||
#include <functional> | |||||
#include <utility> | |||||
#include <tuple> | |||||
namespace tbx { | namespace tbx { | ||||
@@ -87,26 +88,18 @@ class sequencer { | |||||
//! \name Public types | //! \name Public types | ||||
//! @{ | //! @{ | ||||
public: | public: | ||||
//! \enum status_t | |||||
//! \brief The sequencer run status | |||||
enum class status_t { | |||||
OK, ERROR | |||||
}; | |||||
//! \enum action_t | |||||
//! \brief Possible response actions for the sequencer | |||||
enum class action_t { | |||||
NO, NEXT, GOTO, EXIT_OK, EXIT_ERROR | |||||
}; | |||||
using str_view_t = std::basic_string_view<Data_t>; | |||||
//! \enum control_t | //! \enum control_t | ||||
//! \brief The control type of the script entry. | //! \brief The control type of the script entry. | ||||
enum class control_t { | enum class control_t { | ||||
NOP, //!< No command, dont send or expect anything, used for delays | |||||
SEND, //!< Send data to implementation through put() | |||||
EXPECT, //!< Expects data from implementation via get() | |||||
DETECT //!< Detects data into rx buffer without receiving them via contents() | |||||
NOP, //!< No command, dont send or expect anything, used for delays | |||||
SEND, //!< Send data to implementation through put() | |||||
EXPECT, //!< Expects data from implementation via get() | |||||
OR_EXPECT, //!< Expects data from implementation via get() in conjunction with previous EXPECT | |||||
DETECT, //!< Detects data into rx buffer without receiving them via contents() | |||||
OR_DETECT //!< Detects data into rx buffer without receiving them via contents() in conjunction with | |||||
//!< previous DETECT | |||||
//! \note | //! \note | ||||
//! The \c DETECT extra incoming channel serve the purpose of sneak into receive | //! The \c DETECT extra incoming channel serve the purpose of sneak into receive | ||||
@@ -118,6 +111,15 @@ class sequencer { | |||||
//! lets say ">$ " without '\n' at the end. | //! lets say ">$ " without '\n' at the end. | ||||
}; | }; | ||||
//! \enum action_t | |||||
//! \brief Possible response actions for the sequencer | |||||
struct action_t { | |||||
enum { | |||||
NO =0, NEXT, GOTO, EXIT_OK, EXIT_ERROR | |||||
} type; | |||||
size_t step; | |||||
}; | |||||
//! \enum match_t | //! \enum match_t | ||||
//! \brief Token match types | //! \brief Token match types | ||||
enum class match_t { | enum class match_t { | ||||
@@ -130,24 +132,6 @@ class sequencer { | |||||
*/ | */ | ||||
using handler_ft = void (*) (const Data_t*, size_t); | using handler_ft = void (*) (const Data_t*, size_t); | ||||
/*! | |||||
* \struct handle_t | |||||
* \brief | |||||
* The script record handle block. | |||||
* | |||||
* Each script record contains some blocks for matching functionality. Each block | |||||
* has a token and a matching type. If the response matches the token, the sequencer calls | |||||
* the handler and perform the action. | |||||
*/ | |||||
struct handle_t { | |||||
std::basic_string_view<Data_t> | |||||
token; //!< The token for the match | |||||
match_t match_type; //!< The matching type functionality | |||||
handler_ft handler; //!< The handler to called if the match is successful. | |||||
action_t action; //!< The action to be performer if the match is successful | |||||
size_t idx; //!< The index for the action_t::GOTO action. Otherwise can be left 0. | |||||
}; | |||||
/*! | /*! | ||||
* \struct record_t | * \struct record_t | ||||
* \brief | * \brief | ||||
@@ -156,11 +140,12 @@ class sequencer { | |||||
* Each line consist from a control, 2 blocks and a timeout. The control says if we send or receive data. | * 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. | * The blocks contain the data and the matching information. And the timeout guards the entire line. | ||||
*/ | */ | ||||
template<size_t Nhandles =2> | |||||
struct record_t { | struct record_t { | ||||
control_t control; //!< The type of the entry | control_t control; //!< The type of the entry | ||||
std::array<handle_t, Nhandles> | |||||
block; //!< The matching blocks | |||||
str_view_t token; | |||||
match_t match; | |||||
handler_ft handler; //!< The handler to called if the match is successful. | |||||
action_t action; | |||||
clock_t timeout; //!< Timeout in CPU time | clock_t timeout; //!< Timeout in CPU time | ||||
}; | }; | ||||
@@ -183,10 +168,14 @@ class sequencer { | |||||
* }}; | * }}; | ||||
* \endcode | * \endcode | ||||
*/ | */ | ||||
template <size_t Nrecords, size_t Nhandles =2> | |||||
using script_t = std::array<record_t<Nhandles>, Nrecords>; | |||||
template <size_t Nrecords> | |||||
using script_t = std::array<record_t, Nrecords>; | |||||
enum class seq_status_t { | |||||
CONTINUE, EXIT_OK, EXIT_ERROR | |||||
}; | |||||
using str_view_t = std::basic_string_view<Data_t>; | |||||
//! @} | //! @} | ||||
@@ -251,6 +240,19 @@ class sequencer { | |||||
static bool contains_ (const str_view_t haystack, const str_view_t needle) { | static bool contains_ (const str_view_t haystack, const str_view_t needle) { | ||||
return (haystack.find(needle) != str_view_t::npos); | 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) { | |||||
handler (buffer.begin(), buffer.size()); | |||||
return true; | |||||
} | |||||
return false; | |||||
} | |||||
/*! | /*! | ||||
* \brief | * \brief | ||||
* Return the new sequencer's step value. | * Return the new sequencer's step value. | ||||
@@ -262,33 +264,18 @@ class sequencer { | |||||
* \param go_idx The new value of the step in the case of GOTO type | * \param go_idx The new value of the step in the case of GOTO type | ||||
* \return The new sequencer's step value | * \return The new sequencer's step value | ||||
*/ | */ | ||||
static size_t step_ (size_t current_idx, action_t action, size_t go_idx =0) { | |||||
switch (action) { | |||||
static std::pair<size_t, seq_status_t> action_ (const action_t& action, size_t step) { | |||||
switch (action.type) { | |||||
default: | default: | ||||
case action_t::NO: return current_idx; | |||||
case action_t::NEXT: return ++current_idx; | |||||
case action_t::GOTO: return go_idx; | |||||
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: | 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: | case action_t::EXIT_ERROR: | ||||
return 0; | |||||
return std::make_pair(std::numeric_limits<size_t>::max(), seq_status_t::EXIT_ERROR); | |||||
} | } | ||||
} | } | ||||
static status_t action_ (size_t& step, const handle_t& block, const str_view_t buffer = str_view_t{}) { | |||||
if (block.handler != nullptr) | |||||
block.handler(buffer.begin(), buffer.size()); | |||||
switch (block.action) { | |||||
case action_t::EXIT_OK: | |||||
step = std::numeric_limits<size_t>::max(); | |||||
return status_t::OK; | |||||
case action_t::EXIT_ERROR: | |||||
step = std::numeric_limits<size_t>::max(); | |||||
return status_t::ERROR; | |||||
default: | |||||
step = step_(step, block.action, block.idx); | |||||
return status_t::OK; | |||||
} | |||||
} | |||||
//! @} | //! @} | ||||
@@ -325,10 +312,9 @@ class sequencer { | |||||
* \param buffer The buffer to check | * \param buffer The buffer to check | ||||
* \return True on match, false otherwise | * \return True on match, false otherwise | ||||
*/ | */ | ||||
static bool check_handle (const handle_t& handle, const str_view_t buffer) { | |||||
size_t tmp{}; | |||||
if (match(buffer, handle.token, handle.match_type)) { | |||||
action_ (tmp, handle, buffer); | |||||
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; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
@@ -364,82 +350,70 @@ class sequencer { | |||||
* \param script Reference to script to run | * \param script Reference to script to run | ||||
* \return The status of entire operation as described above | * \return The status of entire operation as described above | ||||
*/ | */ | ||||
template <size_t Steps, size_t Nhandles> | |||||
bool run (const script_t<Steps, Nhandles>& script) { | |||||
template <size_t Steps> | |||||
bool run (const script_t<Steps>& script) { | |||||
Data_t buffer[N]; | Data_t buffer[N]; | ||||
size_t resp_size{}; | |||||
status_t status{}; | |||||
size_t resp_size; | |||||
seq_status_t status{seq_status_t::CONTINUE}; | |||||
size_t step =0, p_step =0; | |||||
clock_t mark = clock_(); | clock_t mark = clock_(); | ||||
for (size_t step =0, p_step =0 ; step < Steps ; ) { | |||||
const record_t<Nhandles>& it = script[step]; | |||||
do { | |||||
if (step != p_step) { | if (step != p_step) { | ||||
p_step = step; | p_step = step; | ||||
mark = clock_(); | mark = clock_(); | ||||
} | } | ||||
switch (it.control) { | |||||
switch (script[step].control) { | |||||
default: | default: | ||||
case control_t::NOP: | case control_t::NOP: | ||||
if ((clock_() - mark) >= it.timeout) | |||||
status = action_ (step, it.block[0]); | |||||
if ((clock_() - mark) >= script[step].timeout) | |||||
std::tie(step, status) = action_ (script[step].action, step); | |||||
break; | break; | ||||
case control_t::SEND: | case control_t::SEND: | ||||
if (put_(it.block[0].token.data(), it.block[0].token.size()) != it.block[0].token.size()) | |||||
if (put_(script[step].token.data(), script[step].token.size()) != script[step].token.size()) | |||||
return false; | return false; | ||||
status = action_ (step, it.block[0]); | |||||
std::tie(step, status) = action_ (script[step].action, step); | |||||
break; | break; | ||||
case control_t::EXPECT: | case control_t::EXPECT: | ||||
case control_t::OR_EXPECT: | |||||
resp_size = get_(buffer); | resp_size = get_(buffer); | ||||
if (resp_size) { | if (resp_size) { | ||||
for (auto& block : it.block) { | |||||
if (match( | |||||
{buffer, resp_size}, | |||||
block.token, | |||||
block.match_type)) { | |||||
status = action_ (step, block, {buffer, 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)) { | |||||
handle_ (script[s].handler, {buffer, resp_size}); | |||||
std::tie(step, status) = action_ (script[s].action, s); | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (it.timeout && (clock_() - mark) >= it.timeout) | |||||
if (script[step].timeout && (clock_() - mark) >= script[step].timeout) | |||||
return false; | return false; | ||||
break; | break; | ||||
case control_t::DETECT: | case control_t::DETECT: | ||||
case control_t::OR_DETECT: | |||||
auto data = contents_(); | auto data = contents_(); | ||||
if (data.begin() != data.end()) { | if (data.begin() != data.end()) { | ||||
for (auto& block : it.block) { | |||||
if (match( | |||||
{data.begin(), static_cast<size_t>(data.end() - data.begin())}, | |||||
block.token, | |||||
block.match_type)) { | |||||
status = action_ (step, block, {buffer, resp_size}); | |||||
for (size_t s = step ; script[s].control == control_t::OR_DETECT ; ++s) { | |||||
if (match({buffer, resp_size}, script[s].token, script[s].match)) { | |||||
handle_ (script[s].handler, {buffer, resp_size}); | |||||
std::tie(step, status) = action_ (script[s].action, s); | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (it.timeout && (clock_() - mark) >= it.timeout) | |||||
if (script[step].timeout && (clock_() - mark) >= script[step].timeout) | |||||
return false; | return false; | ||||
break; | break; | ||||
} // switch (it.control) | } // switch (it.control) | ||||
} | |||||
return (status == status_t::OK); | |||||
} while ( status == seq_status_t::CONTINUE); | |||||
return (status == seq_status_t::EXIT_OK); | |||||
} | } | ||||
}; | }; | ||||
/*! | |||||
* An "empty" block for convenience. | |||||
*/ | |||||
template <typename Impl_t, typename Cont_t, typename Data_t, size_t N> | |||||
constexpr typename sequencer<Impl_t, Cont_t, Data_t, N>::handle_t Sequencer_null_block = { | |||||
"", | |||||
sequencer<Impl_t, Cont_t, Data_t, N>::match_t::NO, | |||||
nullptr, | |||||
sequencer<Impl_t, Cont_t, Data_t, N>::action_t::NO, | |||||
0 | |||||
}; | |||||
} | } | ||||
#endif /* TBX_UTILS_SEQUENCER_H_ */ | #endif /* TBX_UTILS_SEQUENCER_H_ */ |
@@ -178,9 +178,9 @@ namespace test_bg95_base { | |||||
BG95<256> modem; | BG95<256> modem; | ||||
char buffer[256]; | char buffer[256]; | ||||
const BG95<256>::async_handlers<2> async = {{ | |||||
{"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0}, | |||||
{"+QMT", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0}, | |||||
const BG95<256>::inetd_handlers<2> async = {{ | |||||
{"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler}, | |||||
{"+QMT", BG95<256>::match_t::STARTS_WITH, handler}, | |||||
}}; | }}; | ||||
clear_flag(); | clear_flag(); | ||||
@@ -225,24 +225,22 @@ namespace test_bg95_base { | |||||
TEST(TBG95_base, run) { | TEST(TBG95_base, run) { | ||||
BG95<256> modem; | BG95<256> modem; | ||||
const BG95<256>::async_handlers<2> async = {{ | |||||
{"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0}, | |||||
{"+QMT", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0}, | |||||
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<5> script = {{ | |||||
/* 0 */{BG95<256>::control_t::NOP, {"", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::GOTO, 1}, 1000}, | |||||
/* 1 */{BG95<256>::control_t::SEND, {"ATE0\r\n", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::NEXT, 0}, 0}, | |||||
/* 2 */{BG95<256>::control_t::EXPECT, {{ | |||||
{"OK\r\n", BG95<256>::match_t::ENDS_WITH, nullptr, BG95<256>::action_t::NEXT, 0}, | |||||
{"ERROR", BG95<256>::match_t::CONTAINS, nullptr, BG95<256>::action_t::EXIT_ERROR, 0} }}, | |||||
1000 | |||||
}, | |||||
/* 3 */{BG95<256>::control_t::SEND, {"AT+CSQ\r\n", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::NEXT, 0}, 0}, | |||||
/* 4 */{BG95<256>::control_t::EXPECT, {{ | |||||
{"OK\r\n", BG95<256>::match_t::ENDS_WITH, nullptr, BG95<256>::action_t::EXIT_OK, 0}, | |||||
{"ERROR", BG95<256>::match_t::CONTAINS, nullptr, BG95<256>::action_t::EXIT_ERROR, 0} }}, | |||||
1000 | |||||
}, | |||||
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; | std::mutex m; | ||||
@@ -270,7 +268,7 @@ namespace test_bg95_base { | |||||
m.unlock(); | m.unlock(); | ||||
}); | }); | ||||
int status; | int status; | ||||
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", status), true); | |||||
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", &status), true); | |||||
EXPECT_EQ (status, 5); | EXPECT_EQ (status, 5); | ||||
char substr[32]; | char substr[32]; | ||||
@@ -76,62 +76,55 @@ namespace test_sequencer { | |||||
*/ | */ | ||||
TEST(Tsequencer, run_delay) { | TEST(Tsequencer, run_delay) { | ||||
Seq s; | Seq s; | ||||
const std::array<Seq::record_t<1>, 1> script = {{ | |||||
/* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000} | |||||
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.run(script), true); | ||||
} | } | ||||
TEST(Tsequencer, run_dummy_output) { | TEST(Tsequencer, run_dummy_output) { | ||||
Seq s; | Seq s; | ||||
const std::array<Seq::record_t<1>, 2> script = {{ | |||||
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000}, | |||||
/* 1 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000} | |||||
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); | EXPECT_EQ (s.run(script), true); | ||||
} | } | ||||
TEST(Tsequencer, run_exits) { | TEST(Tsequencer, run_exits) { | ||||
Seq s; | Seq s; | ||||
const std::array<Seq::record_t<1>, 1> script1 = {{ | |||||
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}, | |||||
const Seq::script_t<1> script1 = {{ | |||||
{Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}, | |||||
}}; | }}; | ||||
EXPECT_EQ (s.run(script1), true); | EXPECT_EQ (s.run(script1), true); | ||||
const std::array<Seq::record_t<1>, 1> script2 = {{ | |||||
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_ERROR, 0}, 1000}, | |||||
const Seq::script_t<1> script2 = {{ | |||||
{Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000}, | |||||
}}; | }}; | ||||
EXPECT_EQ (s.run(script2), false); | EXPECT_EQ (s.run(script2), false); | ||||
} | } | ||||
TEST(Tsequencer, run_sequence) { | TEST(Tsequencer, run_sequence) { | ||||
Seq s; | Seq s; | ||||
const std::array<Seq::record_t<2>, 9> 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}, | |||||
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }}, | |||||
1000 | |||||
}, | |||||
/* 3 */{Seq::control_t::DETECT, {{ | |||||
{"+CCLK", Seq::match_t::CONTAINS, nullptr, Seq::action_t::NEXT, 0}, | |||||
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }}, | |||||
1000 | |||||
}, | |||||
/* 4 */{Seq::control_t::SEND, {"AT+CCLK?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000}, | |||||
/* 5 */{Seq::control_t::EXPECT, {{ | |||||
{"OK\r\n", Seq::match_t::ENDS_WITH, Seq::my_handler, Seq::action_t::NEXT, 0}, | |||||
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }}, | |||||
1000 | |||||
}, | |||||
/* 6 */{Seq::control_t::SEND, {"AT+CT?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000}, | |||||
/* 7 */{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 | |||||
}, | |||||
/* 8 */{Seq::control_t::SEND, {"AT+POWD=0", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000} | |||||
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}, | |||||
/* 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}, | |||||
/* 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}, | |||||
/* 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}, | |||||
/*12 */{Seq::control_t::SEND, "AT+POWD=0", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000} | |||||
}}; | }}; | ||||
EXPECT_EQ (s.run(script), false); | EXPECT_EQ (s.run(script), false); | ||||
} | } | ||||