WIP: BG95_base::command parser and sequencer script representation

This commit is contained in:
2021-09-24 18:06:45 +03:00
parent bab3fd04fe
commit fd64d8726c
4 changed files with 192 additions and 215 deletions
+80 -106
View File
@@ -41,7 +41,8 @@
#include <string_view>
#include <limits>
#include <type_traits>
#include <functional>
#include <utility>
#include <tuple>
namespace tbx {
@@ -87,26 +88,18 @@ class sequencer {
//! \name Public types
//! @{
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
//! \brief The control type of the script entry.
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
//! 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.
};
//! \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
//! \brief Token match types
enum class match_t {
@@ -130,24 +132,6 @@ class sequencer {
*/
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
* \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.
* The blocks contain the data and the matching information. And the timeout guards the entire line.
*/
template<size_t Nhandles =2>
struct record_t {
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
};
@@ -183,10 +168,14 @@ class sequencer {
* }};
* \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) {
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
* 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
* \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:
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:
return std::make_pair(std::numeric_limits<size_t>::max(), seq_status_t::EXIT_OK);
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
* \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 false;
@@ -364,82 +350,70 @@ class sequencer {
* \param script Reference to script to run
* \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];
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_();
for (size_t step =0, p_step =0 ; step < Steps ; ) {
const record_t<Nhandles>& it = script[step];
do {
if (step != p_step) {
p_step = step;
mark = clock_();
}
switch (it.control) {
switch (script[step].control) {
default:
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;
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;
status = action_ (step, it.block[0]);
std::tie(step, status) = action_ (script[step].action, step);
break;
case control_t::EXPECT:
case control_t::OR_EXPECT:
resp_size = get_(buffer);
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;
}
}
}
if (it.timeout && (clock_() - mark) >= it.timeout)
if (script[step].timeout && (clock_() - mark) >= script[step].timeout)
return false;
break;
case control_t::DETECT:
case control_t::OR_DETECT:
auto data = contents_();
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;
}
}
}
if (it.timeout && (clock_() - mark) >= it.timeout)
if (script[step].timeout && (clock_() - mark) >= script[step].timeout)
return false;
break;
} // 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_ */