DEV: add sequencer and cli devices to utl
This commit is contained in:
parent
813bc0977a
commit
99b8609c87
473
include/utl/dev/cli_device.h
Normal file
473
include/utl/dev/cli_device.h
Normal file
@ -0,0 +1,473 @@
|
|||||||
|
/*!
|
||||||
|
* \file dev/cli_device.h
|
||||||
|
* \brief
|
||||||
|
* command line device driver functionality as CRTP base class
|
||||||
|
*
|
||||||
|
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||||
|
*
|
||||||
|
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
* </dd></dl>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef utl_dev_cli_device_h__
|
||||||
|
#define utl_dev_cli_device_h__
|
||||||
|
|
||||||
|
#include <utl/core/impl.h>
|
||||||
|
#include <utl/core/crtp.h>
|
||||||
|
#include <utl/container/equeue.h>
|
||||||
|
#include <utl/dev/sequencer.h>
|
||||||
|
|
||||||
|
#include <utl/meta/meta.h>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
namespace utl {
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \class cli_device
|
||||||
|
* \brief
|
||||||
|
* Its a base class for command-line based devices
|
||||||
|
*
|
||||||
|
* Inherits the sequencer functionality and provides a command interface for sending
|
||||||
|
* commands and parse the response.
|
||||||
|
*
|
||||||
|
* \example implementation example
|
||||||
|
* \code
|
||||||
|
* class BG95 : public cli_device<BG95, 256> {
|
||||||
|
* using base_type = cli_device<BG95, 256>;
|
||||||
|
* using Queue = equeue<typename base_type::value_type, 256, true>;
|
||||||
|
* Queue RxQ{};
|
||||||
|
* std::atomic<size_t> lines{};
|
||||||
|
* public:
|
||||||
|
* // cli_device driver requirements
|
||||||
|
* BG95() noexcept :
|
||||||
|
* RxQ(Queue::data_match::MATCH_PUSH, base_type::delimiter, [&](){
|
||||||
|
* lines.fetch_add(1, std::memory_order_acq_rel);
|
||||||
|
* }), lines(0) { }
|
||||||
|
* void feed(char x) { RxQ << x; } // To be used inside ISR
|
||||||
|
* size_t get(char* data, bool wait =false) {
|
||||||
|
* do {
|
||||||
|
* if (lines.load(std::memory_order_acquire)) {
|
||||||
|
* size_t n =0;
|
||||||
|
* do{
|
||||||
|
* *data << RxQ;
|
||||||
|
* ++n;
|
||||||
|
* } while (*data++ != base_type::delimiter);
|
||||||
|
* lines.fetch_sub(1, std::memory_order_acq_rel);
|
||||||
|
* return n;
|
||||||
|
* }
|
||||||
|
* } while (wait);
|
||||||
|
* return 0;
|
||||||
|
* }
|
||||||
|
* size_t contents(char* data) {
|
||||||
|
* char* nullpos = std::copy(RxQ.begin(), RxQ.end(), data);
|
||||||
|
* *nullpos =0;
|
||||||
|
* return nullpos - data;
|
||||||
|
* }
|
||||||
|
* size_t put (const char* data, size_t n) {
|
||||||
|
* // send data to BG95
|
||||||
|
* return n;
|
||||||
|
* }
|
||||||
|
* clock_t clock() noexcept { //return CPU time }
|
||||||
|
* };
|
||||||
|
* \endcode
|
||||||
|
*
|
||||||
|
* \tparam Impl_t The type of derived class
|
||||||
|
* \tparam N The size of the queue buffer for the receive/command interface
|
||||||
|
* \tparam Delimiter The incoming data delimiter [default line buffered -- Delimiter = '\n']
|
||||||
|
*/
|
||||||
|
template<typename Impl_t, size_t N, char Delimiter ='\n'>
|
||||||
|
class cli_device
|
||||||
|
: public sequencer<cli_device<Impl_t, N, Delimiter>, char, N>{
|
||||||
|
_CRTP_IMPL(Impl_t);
|
||||||
|
|
||||||
|
// local type dispatch
|
||||||
|
using base_type = sequencer<cli_device, char, N>;
|
||||||
|
|
||||||
|
//! \name Public types
|
||||||
|
//! @{
|
||||||
|
public:
|
||||||
|
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>;
|
||||||
|
|
||||||
|
//! Publish delimiter
|
||||||
|
constexpr static char delimiter = Delimiter;
|
||||||
|
|
||||||
|
enum flush_type { keep =0, flush };
|
||||||
|
|
||||||
|
//! Required types for inetd async handler operation
|
||||||
|
//! @{
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* inetd handler structure for asynchronous incoming data dispatching
|
||||||
|
*/
|
||||||
|
struct inetd_handler_t {
|
||||||
|
string_view token; //!< The token we match against
|
||||||
|
match_ft match; //!< The predicate we use to match
|
||||||
|
handler_ft handler; //!< The handler to call on match
|
||||||
|
};
|
||||||
|
//! Alias template for the async handler array
|
||||||
|
template <size_t Nm>
|
||||||
|
using inetd_handlers = std::array<inetd_handler_t, Nm>;
|
||||||
|
//! @}
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
//! \name object lifetime
|
||||||
|
//!@{
|
||||||
|
protected:
|
||||||
|
//!< \brief A default constructor from derived only
|
||||||
|
cli_device() noexcept = default;
|
||||||
|
~cli_device () = default; //!< \brief Allow destructor from derived only
|
||||||
|
cli_device(const cli_device&) = delete; //!< No copies
|
||||||
|
cli_device& operator= (const cli_device&) = delete; //!< No copy assignments
|
||||||
|
//!@}
|
||||||
|
|
||||||
|
//! \name Sequencer interface requirements
|
||||||
|
//! Forwarded to implementer the calls and cascade the the incoming channel
|
||||||
|
//! @{
|
||||||
|
friend base_type;
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t get_ (char* data) {
|
||||||
|
return impl().get (data);
|
||||||
|
}
|
||||||
|
size_t get (char* data) {
|
||||||
|
return receive (data);
|
||||||
|
}
|
||||||
|
size_t contents (char* data) {
|
||||||
|
return impl().contents(data);
|
||||||
|
}
|
||||||
|
size_t put (const char* data, size_t n) {
|
||||||
|
return impl().put (data, n);
|
||||||
|
}
|
||||||
|
clock_t clock () noexcept {
|
||||||
|
return impl().clock();
|
||||||
|
}
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
//! \name Private functionality
|
||||||
|
//! @{
|
||||||
|
private:
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Convert the text pointed by \c str to a value and store it to
|
||||||
|
* \c value. The type of conversion is deduced by the compiler
|
||||||
|
* \tparam T The type of the value
|
||||||
|
* \param str pointer to string with the value
|
||||||
|
* \param value pointer to converted value
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
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>) {
|
||||||
|
*value = std::atoi(str);
|
||||||
|
} else if (std::is_same_v<std::remove_cv_t<T>, double>) {
|
||||||
|
*value = std::atof(str);
|
||||||
|
} else if (std::is_same_v<std::remove_cv_t<T>, char>) {
|
||||||
|
std::strcpy(value, str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//! Specialization (as overload function) to handle void* types
|
||||||
|
void extract_ (const char* str, void* value) noexcept {
|
||||||
|
(void)*str; (void)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Parse a chunk of the buffer based on \c expected character
|
||||||
|
*
|
||||||
|
* Tries to match the \c *expected character in buffer and if so it copies the
|
||||||
|
* character to token.
|
||||||
|
* If the \c *expected is the \c Marker character, copy the entire chunk of the buffer
|
||||||
|
* up to the character that matches the next expected character (expected[1]).
|
||||||
|
* If there is no next expected character or if its not found in the buffer,
|
||||||
|
* copy the entire buffer.
|
||||||
|
*
|
||||||
|
* \tparam Marker The special character to indicate chunk extraction
|
||||||
|
*
|
||||||
|
* \param expected The character to parse/remove from the buffer
|
||||||
|
* \param buffer The buffer we parse
|
||||||
|
* \param token Pointer to store the parsed tokens
|
||||||
|
* \return A (number of characters parsed, marker found) pair
|
||||||
|
*/
|
||||||
|
template <char Marker = '%'>
|
||||||
|
std::pair<size_t, bool> parse_ (const char* expected, const string_view buffer, char* token) {
|
||||||
|
do {
|
||||||
|
if (*expected == Marker) {
|
||||||
|
// We have Marker. Copy the entire chunk of the buffer
|
||||||
|
// up to the character that matches the next expected character (expected[1]).
|
||||||
|
// If there is none next expected character or if its not found in the buffer,
|
||||||
|
// copy the entire buffer.
|
||||||
|
auto next = std::find(buffer.begin(), buffer.end(), expected[1]);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
// Fail to parse
|
||||||
|
*token =0;
|
||||||
|
return std::make_pair(0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Analyze the response of a command based on \c expected.
|
||||||
|
*
|
||||||
|
* Tries to receive data with timeout and match them against expected string_view.
|
||||||
|
* For each Marker inside the expected string the value gets extracted, converted and
|
||||||
|
* copied to \c vargs pointer array.
|
||||||
|
*
|
||||||
|
* \param expected The expected string view
|
||||||
|
* \param timeout the timeout in CPU time
|
||||||
|
* \param vargs Pointer to variable arguments array
|
||||||
|
* \param nargs Size of variable arguments array
|
||||||
|
* \return
|
||||||
|
*/
|
||||||
|
template<char Marker = '%', typename T>
|
||||||
|
bool response_ (const string_view expected, clock_t timeout, T* vargs, size_t nargs) {
|
||||||
|
char buffer[N], token[N], *pbuffer = buffer;
|
||||||
|
|
||||||
|
size_t v =0, sz =0;
|
||||||
|
for (auto ex = expected.begin() ; ex != expected.end() ; ) {
|
||||||
|
clock_t mark = clock(); // mark the time
|
||||||
|
while (sz <= 0) { // if buffer is empty get buffer with timeout
|
||||||
|
sz = receive(buffer);
|
||||||
|
pbuffer = buffer;
|
||||||
|
if ((timeout != 0 )&& ((clock() - mark) >= timeout))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// try to parse
|
||||||
|
auto [step, marker] = parse_<Marker> (ex, {pbuffer, sz}, token);
|
||||||
|
if (!step)
|
||||||
|
return false; // discard buffer and fail
|
||||||
|
|
||||||
|
if (marker && v < nargs)
|
||||||
|
extract_(token, vargs[v++]);
|
||||||
|
|
||||||
|
pbuffer += step;
|
||||||
|
sz -= (step <= sz) ? step: sz;
|
||||||
|
++ex;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
//! \name public functionality
|
||||||
|
//! @{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* Transmit data to modem
|
||||||
|
* \param data Pointer to data to send
|
||||||
|
* \param n The size of data buffer
|
||||||
|
* \return The number of transmitted chars
|
||||||
|
*/
|
||||||
|
size_t transmit (const char* data, size_t n) {
|
||||||
|
if (data == nullptr)
|
||||||
|
return 0;
|
||||||
|
return put (data, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* Transmit data to modem
|
||||||
|
* \param data Pointer to data to send
|
||||||
|
* \return The number of transmitted chars
|
||||||
|
*/
|
||||||
|
size_t transmit (const char* data) {
|
||||||
|
if (data == nullptr)
|
||||||
|
return 0;
|
||||||
|
return put (data, std::strlen(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* Try to receive data from modem. If there are data copy them to \c data pointer and return
|
||||||
|
* the size. Otherwise return zero. In the case \c wait is true block until there are data to get.
|
||||||
|
*
|
||||||
|
* \param data Pointer to data buffer to write
|
||||||
|
* \param wait Flag to select blocking / non-blocking functionality
|
||||||
|
* \return The number of copied data.
|
||||||
|
*/
|
||||||
|
size_t receive (char* data, bool wait =false) {
|
||||||
|
do {
|
||||||
|
if (streams_.load(std::memory_order_acquire)) {
|
||||||
|
size_t n =0;
|
||||||
|
do {
|
||||||
|
*data << rx_q;
|
||||||
|
++n;
|
||||||
|
} while (*data++ != delimiter);
|
||||||
|
*data =0;
|
||||||
|
streams_.fetch_sub(1, std::memory_order_acq_rel);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
} while (wait); // on wait flag we block until available stream
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Clears the incoming data buffer
|
||||||
|
void clear () noexcept {
|
||||||
|
rx_q.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \return Returns the size of the incoming data buffer
|
||||||
|
size_t size() noexcept {
|
||||||
|
return rx_q.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* Send a command to modem and check if the response matches to \c expected.
|
||||||
|
*
|
||||||
|
* This function executes 3 steps.
|
||||||
|
* - Clears the incoming buffer if requested by template parameter
|
||||||
|
* - Sends the command to device
|
||||||
|
* - Waits to get the response and parse it accordingly to \c expected \see response_()
|
||||||
|
*
|
||||||
|
* The user can mark spots inside the expected string using the \c Marker ['%'] character.
|
||||||
|
* These spots will be extracted to tokens upon parsing. If the user passes \c values parameters,
|
||||||
|
* then the extracted tokens will be converted to the type of the \c values (\c Ts) and copied to them
|
||||||
|
* one by one. If the values are less than spots, the rest of the tokens get discarded.
|
||||||
|
*
|
||||||
|
* \param cmd The command to send (null terminated)
|
||||||
|
* \param expected The expected response
|
||||||
|
* \param timeout The timeout in CPU time (leave it for 0 - no timeout)
|
||||||
|
* \param values The value pointer arguments to get the converted tokens
|
||||||
|
*
|
||||||
|
* \tparam Flush Flag to indicate if we flush the buffer before command or not
|
||||||
|
* \tparam Marker The marker character
|
||||||
|
* \tparam Ts The type of the values to read from response marked with \c Marker
|
||||||
|
* \warning The types MUST be the same
|
||||||
|
*
|
||||||
|
* \return True on success
|
||||||
|
*
|
||||||
|
* \example examples
|
||||||
|
* \code
|
||||||
|
* Derived cli;
|
||||||
|
* int status;
|
||||||
|
* char str[32];
|
||||||
|
*
|
||||||
|
* // discard 3 lines and expect OK\r\n at the end with 1000[CPU time] timeout
|
||||||
|
* cli.command("AT+CREG?\r\n", "%%%OK\r\n", 1000);
|
||||||
|
*
|
||||||
|
* // extract a number from response without timeout (blocking)
|
||||||
|
* cli.command<flush>("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", 0, &status);
|
||||||
|
*
|
||||||
|
* // extract a number and discard the last 2 lines
|
||||||
|
* cli.command<flush>("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n%%", 1000, &status);
|
||||||
|
*
|
||||||
|
* // discard first line, read the 2nd to str, discard the 3rd line.
|
||||||
|
* // expect the last to be "OK\r\n"
|
||||||
|
* cli.command<flush>("AT+CREG?\r\n", "", 100000);
|
||||||
|
* cli.command<keep>("", "%", 1000);
|
||||||
|
* cli.command<keep>("", "%%", 1000, str);
|
||||||
|
* cli.command<keep>("", "OK\r\n", 1000);
|
||||||
|
* \endcode
|
||||||
|
*/
|
||||||
|
template<flush_type Flush =flush, char Marker = '%', typename ...Ts>
|
||||||
|
bool command (const string_view cmd, const string_view expected, clock_t timeout, Ts* ...values) {
|
||||||
|
constexpr size_t Nr = sizeof...(Ts);
|
||||||
|
|
||||||
|
meta::if_c<
|
||||||
|
(sizeof...(Ts) != 0),
|
||||||
|
meta::front<meta::typelist<Ts...>>*,
|
||||||
|
void*
|
||||||
|
> vargs[Nr] = {values...}; // read all args to local buffer
|
||||||
|
if constexpr (Flush == flush) {
|
||||||
|
clear ();
|
||||||
|
}
|
||||||
|
if (transmit(cmd.data(), cmd.size()) != cmd.size()) // send command
|
||||||
|
return false;
|
||||||
|
// parse the response and return the status
|
||||||
|
return response_<Marker>(expected, timeout, vargs, Nr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* inetd daemon functionality provided as member function of the driver. This should be running
|
||||||
|
* in the background either as consecutive calls from an periodic ISR with \c loop = false, or
|
||||||
|
* as a thread in an RTOS environment with \c loop = true.
|
||||||
|
*
|
||||||
|
* \tparam Nm The number of handler array entries
|
||||||
|
*
|
||||||
|
* \param async_handles Reference to asynchronous handler array
|
||||||
|
* \param loop Flag to indicate blocking mode. If true blocking.
|
||||||
|
*/
|
||||||
|
template <size_t Nm =0>
|
||||||
|
void inetd (bool loop =true, const inetd_handlers<Nm>* inetd_handlers =nullptr) {
|
||||||
|
std::array<char, N> buffer;
|
||||||
|
size_t resp_size;
|
||||||
|
do {
|
||||||
|
if ((resp_size = get_(buffer.data())) != 0) {
|
||||||
|
// on data check for async handlers
|
||||||
|
bool match = false;
|
||||||
|
if (inetd_handlers != nullptr) {
|
||||||
|
for (auto& h : *inetd_handlers)
|
||||||
|
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) {
|
||||||
|
char* it = buffer.data();
|
||||||
|
do {
|
||||||
|
rx_q << *it;
|
||||||
|
} while (*it++ != delimiter);
|
||||||
|
streams_.fetch_add(1, std::memory_order_acq_rel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
private:
|
||||||
|
equeue<char, N, true> rx_q{};
|
||||||
|
std::atomic<size_t> streams_{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace utl;
|
||||||
|
|
||||||
|
#endif /* #ifndef utl_dev_cli_device_h__ */
|
492
include/utl/dev/sequencer.h
Normal file
492
include/utl/dev/sequencer.h
Normal file
@ -0,0 +1,492 @@
|
|||||||
|
/*!
|
||||||
|
* \file dev/sequencer.h
|
||||||
|
* \brief
|
||||||
|
* A script based automation tool for send/receive communications
|
||||||
|
*
|
||||||
|
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
|
||||||
|
*
|
||||||
|
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
* </dd></dl>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef utl_dev_dequencer_h__
|
||||||
|
#define utl_dev_dequencer_h__
|
||||||
|
|
||||||
|
#include <utl/core/impl.h>
|
||||||
|
#include <utl/container/range.h>
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
#include <array>
|
||||||
|
#include <string_view>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
namespace utl {
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \class sequencer
|
||||||
|
* \brief
|
||||||
|
* A CRTP base class to provide the sequencer functionality.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* 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 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().
|
||||||
|
*
|
||||||
|
* \note
|
||||||
|
* 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 Data_t, size_t N>
|
||||||
|
class sequencer {
|
||||||
|
_CRTP_IMPL(Impl_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
|
||||||
|
};
|
||||||
|
|
||||||
|
//! \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()
|
||||||
|
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
|
||||||
|
//! buffer and check for data without getting them. This is useful when the receive driver
|
||||||
|
//! is buffered with a delimiter and we seek for data that don't follow the delimiter pattern.
|
||||||
|
//!
|
||||||
|
//! For example:
|
||||||
|
//! 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. 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, //!< 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 value; //!< Used by \c GOTO to indicate the next sequencer's step.
|
||||||
|
};
|
||||||
|
|
||||||
|
//! A no_action action_t
|
||||||
|
static constexpr action_t no_action = {action_t::NO, 0};
|
||||||
|
//! A next action_t
|
||||||
|
static constexpr action_t next = {action_t::NEXT, 0};
|
||||||
|
//! A goto action_t template
|
||||||
|
template <size_t GOTO>
|
||||||
|
static constexpr action_t go_to = {action_t::GOTO, static_cast<size_t>(GOTO)};
|
||||||
|
|
||||||
|
//! An exit ok action_t
|
||||||
|
static constexpr action_t exit_ok = {action_t::EXIT, 0};
|
||||||
|
//! An exit error action_t
|
||||||
|
static constexpr action_t exit_error = {action_t::EXIT, static_cast<size_t>(-1)};
|
||||||
|
//! A generic exit action_t template
|
||||||
|
template <size_t Status>
|
||||||
|
static constexpr action_t exit = {action_t::EXIT, static_cast<size_t>(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);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \struct record_t
|
||||||
|
* \brief
|
||||||
|
* Describes the sequencer's script record entry (line).
|
||||||
|
*/
|
||||||
|
struct record_t {
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \struct script_t
|
||||||
|
* \brief
|
||||||
|
* Describes the sequencer's script.
|
||||||
|
*
|
||||||
|
* The user can create arrays as the example bellow to act as a script.
|
||||||
|
* \code
|
||||||
|
* 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>;
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \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) noexcept -> 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) noexcept -> 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) noexcept -> bool {
|
||||||
|
return (haystack.find(needle) != string_view::npos);
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Always false predicate
|
||||||
|
static constexpr auto always_true = [](const string_view s1, const string_view s2) noexcept -> bool {
|
||||||
|
(void)s1; (void)s2;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Always false predicate
|
||||||
|
static constexpr auto always_false = [](const string_view s1, const string_view s2) noexcept -> bool {
|
||||||
|
(void)s1; (void)s2;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Empty predicate or handler
|
||||||
|
static constexpr auto nil = nullptr;
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//! \name Object lifetime
|
||||||
|
//!@{
|
||||||
|
protected:
|
||||||
|
~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
|
||||||
|
//!@}
|
||||||
|
|
||||||
|
//! \name Sequencer interface requirements for implementer
|
||||||
|
//! @{
|
||||||
|
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); }
|
||||||
|
clock_t clock_ () noexcept { return impl().clock(); }
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
//! \name Private functionality
|
||||||
|
//! @{
|
||||||
|
private:
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
constexpr bool handle_ (handler_ft handler, const string_view buffer = string_view{}) {
|
||||||
|
if (handler != nullptr) {
|
||||||
|
handler (buffer.begin(), buffer.size());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* Return the new sequencer's step value and the sequencer's loop status as pair.
|
||||||
|
*
|
||||||
|
* \param script Reference to entire script.
|
||||||
|
* \param step The current step
|
||||||
|
* \return new step - status pair
|
||||||
|
*/
|
||||||
|
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:
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! \return The buffer size of the sequencer
|
||||||
|
constexpr size_t size() const noexcept { 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 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
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief
|
||||||
|
* Run the script array
|
||||||
|
*
|
||||||
|
* The main sequencer functionality. It starts with the first entry of the array.
|
||||||
|
*
|
||||||
|
* - 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>
|
||||||
|
size_t run (const script_t<Steps>& script) {
|
||||||
|
Data_t buffer[N];
|
||||||
|
size_t resp_size;
|
||||||
|
|
||||||
|
size_t step =0, p_step =0;
|
||||||
|
clock_t mark = clock_();
|
||||||
|
|
||||||
|
seq_status_t status{seq_status_t::CONTINUE}; do {
|
||||||
|
if (step >= Steps)
|
||||||
|
return exit_error.value;
|
||||||
|
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 (record.control) {
|
||||||
|
default:
|
||||||
|
case control_t::NOP:
|
||||||
|
if ((clock_() - mark) >= record.timeout)
|
||||||
|
std::tie(step, status) = action_ (script, step);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case control_t::SEND:
|
||||||
|
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) {
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while ((++s < Steps) && (script[s].control == control_t::OR_EXPECT));
|
||||||
|
}
|
||||||
|
if (record.timeout && (clock_() - mark) >= record.timeout)
|
||||||
|
return exit_error.value;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case control_t::DETECT:
|
||||||
|
case control_t::OR_DETECT:
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while ((++s < Steps) && (script[s].control == control_t::OR_DETECT));
|
||||||
|
}
|
||||||
|
if (record.timeout && (clock_() - mark) >= record.timeout)
|
||||||
|
return exit_error.value;
|
||||||
|
break;
|
||||||
|
|
||||||
|
} // switch (record.control)
|
||||||
|
|
||||||
|
} while ( status == seq_status_t::CONTINUE);
|
||||||
|
|
||||||
|
return step; // step here is set by action_ as the return status
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} //namespace utl;
|
||||||
|
#endif /* utl_dev_dequencer_h__ */
|
422
test/tests/cli_device.cpp
Normal file
422
test/tests/cli_device.cpp
Normal file
@ -0,0 +1,422 @@
|
|||||||
|
/*!
|
||||||
|
* \file cli_device.cpp
|
||||||
|
*
|
||||||
|
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||||
|
*
|
||||||
|
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
* </dd></dl>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include <utl/dev/cli_device.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <utl/container/equeue.h>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <utility>
|
||||||
|
#include <type_traits>
|
||||||
|
#ifndef WIN_TRHEADS
|
||||||
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
|
#else
|
||||||
|
#include <mingw.thread.h>
|
||||||
|
#include <mingw.mutex.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace test_cli_device {
|
||||||
|
using namespace utl;
|
||||||
|
|
||||||
|
// test settings
|
||||||
|
constexpr size_t Size = 128;
|
||||||
|
using data_type = char;
|
||||||
|
|
||||||
|
// cli_device implementer mock. We simulate a BG95 ATmodem for that purpose
|
||||||
|
template<size_t N>
|
||||||
|
class BG95 : public cli_device<BG95<N>, N> {
|
||||||
|
using base_type = cli_device<BG95<N>, N>;
|
||||||
|
using Queue = equeue<typename base_type::value_type, N, true>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum class event {
|
||||||
|
MQTT_DISCONNECT, MQTT_RXDATA
|
||||||
|
};
|
||||||
|
// simulated modem operation
|
||||||
|
private:
|
||||||
|
struct cmd_pair {
|
||||||
|
const char *cmd;
|
||||||
|
const char *resp;
|
||||||
|
};
|
||||||
|
struct event_pair {
|
||||||
|
event e;
|
||||||
|
const char* resp;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::array<cmd_pair, 20> cmd_map = {{
|
||||||
|
{"", ""},
|
||||||
|
{"ERROR", "\r\nERROR\r\n"},
|
||||||
|
{"ATE0\r\n", "\r\nATE0\r\nOK\r\n"},
|
||||||
|
{"AT\r\n", "\r\nOK\r\n"},
|
||||||
|
{"AT+QCFG=\"nwscanseq\"\r\n", "\r\n+QCFG: \"nwscanseq\",020301\r\n"},
|
||||||
|
{"AT+QCFG=\"nwscanseq\",010302\r\n", "\r\nOK\r\n"},
|
||||||
|
{"AT+CREG?\r\n", "\r\n+CREG: 0,5\r\n\r\nOK\r\n"},
|
||||||
|
{"AT+CSQ\r\n", "\r\n+CSQ: 19,99\r\n\r\nOK\r\n"},
|
||||||
|
{"AT+QNWINFO\r\n", "\r\n+QNWINFO: \"EDGE\",\"20201\",\"GSM 1800\",865\r\n\r\nOK\r\n"},
|
||||||
|
// Files
|
||||||
|
{"AT+QFLST\r\n", "\r\n+QFLST: \"cacert.pem\",1220\r\n+QFLST: \"security/\",2\r\nOK\r\n"},
|
||||||
|
// MQTT config
|
||||||
|
{"AT+QSSLCFG=\"ignorelocaltime\",2,1\r\n", "\r\nOK\r\n"},
|
||||||
|
{"AT+QSSLCFG=\"seclevel\",2,1\r\n", "\r\nOK\r\n"},
|
||||||
|
{"AT+QSSLCFG=\"sslversion\",2,4\r\n", "\r\nOK\r\n"},
|
||||||
|
{"AT+QSSLCFG=\"ciphersuite\",2\r\n", "\r\n+QSSLCFG: \"ciphersuite\",2,0XFFFF\r\n\r\nOK\r\n"},
|
||||||
|
{"AT+QMTCFG=\"ssl\",0,1,2\r\n", "\r\nOK\r\n"},
|
||||||
|
{"AT+QMTCFG=\"keepalive\",0,3600\r\n", "\r\nOK\r\n"},
|
||||||
|
// MQTT
|
||||||
|
{"AT+QMTOPEN=0,\"server.com\",8883\r\n", "\r\nOK\r\n\r\n+QMTOPEN: 0,0\r\n"},
|
||||||
|
{"AT+QMTCONN=0,\"myID\",\"user\",\"pass\"\r\n", "\r\nOK\r\n\r\n+QMTCONN: 0,0,0\r\n"},
|
||||||
|
{"AT+QMTSUB=0,1,\"/path/topic1\",2\r\n", "\r\nOK\r\n\r\n+QMTSUB: 0,1,0,2\r\n"},
|
||||||
|
{"AT+QMTPUB=0,0,0,0,\"/path/topic2\",9\r\n", "\r\n> \r\nOK\r\n\r\n+QMTPUB: 0,0,0\r\n"},
|
||||||
|
}};
|
||||||
|
std::array<event_pair, 2> event_map {{
|
||||||
|
{event::MQTT_DISCONNECT, "\r\n+QMTSTAT: 0,1\r\n"},
|
||||||
|
{event::MQTT_RXDATA, "\r\n+QMTRECV: 0,1,\"/path/topic1\",\"BR: hello to all of my subscribers\""}
|
||||||
|
}};
|
||||||
|
const char* cmd_responce (const char* cmd) {
|
||||||
|
if (cmd != nullptr) {
|
||||||
|
for (auto& it : cmd_map) {
|
||||||
|
if (!std::strcmp(it.cmd, cmd))
|
||||||
|
return it.resp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cmd_map[1].resp;
|
||||||
|
}
|
||||||
|
const char* event_responce (const event e) {
|
||||||
|
for (auto& it : event_map) {
|
||||||
|
if (e == it.e)
|
||||||
|
return it.resp;
|
||||||
|
}
|
||||||
|
return nullptr; // non reachable
|
||||||
|
}
|
||||||
|
|
||||||
|
// data
|
||||||
|
Queue RxQ{};
|
||||||
|
std::atomic<size_t> lines{};
|
||||||
|
clock_t t=0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// cli_device driver requirements
|
||||||
|
BG95() noexcept :
|
||||||
|
RxQ(Queue::data_match::MATCH_PUSH, base_type::delimiter, [&](){
|
||||||
|
lines.fetch_add(1, std::memory_order_acq_rel);
|
||||||
|
}), lines(0) { }
|
||||||
|
|
||||||
|
size_t get(char* data, bool wait =false) {
|
||||||
|
do {
|
||||||
|
if (lines.load(std::memory_order_acquire)) {
|
||||||
|
size_t n =0;
|
||||||
|
do{
|
||||||
|
*data << RxQ;
|
||||||
|
++n;
|
||||||
|
} while (*data++ != base_type::delimiter);
|
||||||
|
lines.fetch_sub(1, std::memory_order_acq_rel);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
} while (wait);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
size_t contents(char* data) {
|
||||||
|
char* nullpos = std::copy(RxQ.begin(), RxQ.end(), data);
|
||||||
|
*nullpos =0;
|
||||||
|
return nullpos - data;
|
||||||
|
}
|
||||||
|
size_t put (const char* data, size_t n) {
|
||||||
|
const char* reply = cmd_responce (data);
|
||||||
|
while (*reply)
|
||||||
|
RxQ << *reply++;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
clock_t clock() noexcept { return ++t; }
|
||||||
|
void clear_clock() noexcept { t=0; }
|
||||||
|
|
||||||
|
// extra helper for testing purposes
|
||||||
|
void async (event e) {
|
||||||
|
const char* reply =event_responce (e);
|
||||||
|
while (*reply)
|
||||||
|
RxQ << *reply++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Behavior flag
|
||||||
|
bool handler_flag = false;
|
||||||
|
void handler (const char* data, size_t n) {
|
||||||
|
(void)*data;
|
||||||
|
(void)n;
|
||||||
|
handler_flag = true;
|
||||||
|
}
|
||||||
|
void clear_flag () {
|
||||||
|
handler_flag = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Tcli_device, traits) {
|
||||||
|
EXPECT_EQ ( std::is_default_constructible<BG95<Size>>::value, true);
|
||||||
|
EXPECT_EQ ( std::is_nothrow_default_constructible<BG95<Size>>::value, true);
|
||||||
|
EXPECT_EQ (!std::is_copy_constructible<BG95<Size>>::value, true);
|
||||||
|
EXPECT_EQ (!std::is_copy_assignable<BG95<Size>>::value, true);
|
||||||
|
|
||||||
|
EXPECT_EQ ((std::is_same_v<BG95<Size>::value_type, data_type>), true);
|
||||||
|
EXPECT_EQ ((std::is_same_v<BG95<Size>::pointer_type, data_type*>), true);
|
||||||
|
EXPECT_EQ ((std::is_same_v<BG95<Size>::size_type, size_t>), true);
|
||||||
|
EXPECT_EQ ((std::is_same_v<BG95<Size>::string_view, std::basic_string_view<data_type>>), true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Test inetd in non blocking mode
|
||||||
|
*/
|
||||||
|
TEST(Tcli_device, txrx_inetd) {
|
||||||
|
BG95<Size> modem;
|
||||||
|
char buffer[Size];
|
||||||
|
size_t s =0;
|
||||||
|
|
||||||
|
const BG95<Size>::inetd_handlers<2> async = {{
|
||||||
|
{"+QMTSTAT:", BG95<Size>::starts_with, handler},
|
||||||
|
{"+QMTRECV", BG95<Size>::contains, handler},
|
||||||
|
}};
|
||||||
|
|
||||||
|
s = modem.transmit("", std::strlen(""));
|
||||||
|
EXPECT_EQ (s, 0UL);
|
||||||
|
s = modem.transmit("");
|
||||||
|
EXPECT_EQ (s, 0UL);
|
||||||
|
s = modem.transmit(nullptr);
|
||||||
|
EXPECT_EQ (s, 0UL);
|
||||||
|
|
||||||
|
clear_flag();
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
EXPECT_EQ (handler_flag, false);
|
||||||
|
modem.async(BG95<Size>::event::MQTT_DISCONNECT);
|
||||||
|
modem.inetd(false, &async); // parse "\r\n"
|
||||||
|
EXPECT_EQ (handler_flag, false);
|
||||||
|
modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
|
||||||
|
EXPECT_EQ (handler_flag, true);
|
||||||
|
clear_flag(); // nothing to parse
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
EXPECT_EQ (handler_flag, false);
|
||||||
|
EXPECT_NE (modem.receive(buffer), 0UL); // "\r\n" in buffer
|
||||||
|
EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
|
||||||
|
|
||||||
|
clear_flag();
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
EXPECT_EQ (handler_flag, false);
|
||||||
|
modem.transmit("AT+CSQ\r\n", 8);
|
||||||
|
EXPECT_EQ (modem.receive(buffer), 0UL);
|
||||||
|
modem.inetd(false, &async); // parse "\r\n"
|
||||||
|
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||||
|
EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
|
||||||
|
modem.inetd(false, &async); // parse "+CSQ: 19,99\r\n"
|
||||||
|
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||||
|
EXPECT_EQ (std::strcmp(buffer, "+CSQ: 19,99\r\n"), 0);
|
||||||
|
modem.inetd(false, &async); // parse "\r\n"
|
||||||
|
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||||
|
EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
|
||||||
|
modem.inetd(false, &async); // parse "OK\r\n"
|
||||||
|
EXPECT_NE (modem.receive(buffer), 0UL);
|
||||||
|
EXPECT_EQ (std::strcmp(buffer, "OK\r\n"), 0);
|
||||||
|
modem.inetd(false, &async); // nothing to parse
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
modem.inetd(false, &async);
|
||||||
|
EXPECT_EQ (modem.receive(buffer), 0UL);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Tcli_device, run) {
|
||||||
|
BG95<Size> modem;
|
||||||
|
|
||||||
|
using Control = BG95<Size>::control_t;
|
||||||
|
|
||||||
|
const BG95<Size>::script_t<4> script = {{
|
||||||
|
{Control::NOP, "", BG95<Size>::nil, BG95<Size>::nil, BG95<Size>::go_to<1>, 100000},
|
||||||
|
{Control::SEND, "ATE0\r\n", BG95<Size>::nil, BG95<Size>::nil, BG95<Size>::next, 0},
|
||||||
|
{Control::EXPECT, "OK\r\n", BG95<Size>::ends_with, BG95<Size>::nil, BG95<Size>::exit_ok, 100000},
|
||||||
|
{Control::OR_EXPECT,"ERROR", BG95<Size>::contains, BG95<Size>::nil, BG95<Size>::exit_error, 0}
|
||||||
|
}};
|
||||||
|
|
||||||
|
std::mutex m;
|
||||||
|
m.lock();
|
||||||
|
std::thread th1 ([&](){
|
||||||
|
do
|
||||||
|
modem.inetd(false);
|
||||||
|
while (!m.try_lock());
|
||||||
|
m.unlock();
|
||||||
|
});
|
||||||
|
EXPECT_EQ (modem.run(script), BG95<Size>::exit_ok.value);
|
||||||
|
m.unlock(); // stop and join inetd
|
||||||
|
th1.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Tcli_device, clear_size) {
|
||||||
|
BG95<Size> modem;
|
||||||
|
char buffer[Size];
|
||||||
|
|
||||||
|
modem.clear();
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (modem.receive(buffer), 0UL);
|
||||||
|
|
||||||
|
modem.transmit("abcd", 4);
|
||||||
|
modem.inetd(false);
|
||||||
|
modem.inetd(false);
|
||||||
|
EXPECT_NE (modem.size(), 0UL);
|
||||||
|
modem.clear();
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Tcli_device, command_non_extraction) {
|
||||||
|
BG95<Size> modem;
|
||||||
|
char buffer[Size];
|
||||||
|
|
||||||
|
std::mutex m;
|
||||||
|
m.lock();
|
||||||
|
std::thread th1 ([&](){
|
||||||
|
do
|
||||||
|
modem.inetd(false);
|
||||||
|
while (!m.try_lock());
|
||||||
|
m.unlock();
|
||||||
|
});
|
||||||
|
|
||||||
|
auto run_receive = [&](size_t times) -> size_t {
|
||||||
|
size_t s =0;
|
||||||
|
for (size_t i=0 ; i<times ; ++i)
|
||||||
|
s += modem.receive(buffer);
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command("", "", 0), true); // returns: ""
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command("", "abcd", 100000), false);// returns: ""
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command("abcd", "", 0), true); // returns: "\r\nERROR\r\n"
|
||||||
|
EXPECT_GT (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command("AT\r\n", "Something else", 100000), false);// returns: "\r\nOK\r\n"
|
||||||
|
EXPECT_GT (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command("AT\r\n", "\r\nOK\r\n", 100000), true); // returns: "\r\nOK\r\n"
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command("AT\r\n", "%OK\r\n", 100000), true); // returns: "\r\nOK\r\n"
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||||
|
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%OK\r\n", 0), false);
|
||||||
|
EXPECT_GT (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||||
|
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%%%OK\r\n", 0), true);
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||||
|
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%", 0), true);
|
||||||
|
EXPECT_GT (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT\r\n", "%%", 0), true); // returns: "\r\nOK\r\n"
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command<BG95<Size>::flush>("AT\r\n", "%%%", 10000), false); // returns: "\r\nOK\r\n"
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||||
|
EXPECT_EQ (modem.command<modem.flush>("AT+CREG?\r\n", "", 0), true);
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%", 0), true);
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%%", 0), true);
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%", 0), true);
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%", 10000), false);
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
m.unlock(); // stop and join inetd
|
||||||
|
th1.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Tcli_device, command_extraction) {
|
||||||
|
BG95<Size> modem;
|
||||||
|
char buffer[Size];
|
||||||
|
|
||||||
|
std::mutex m;
|
||||||
|
m.lock();
|
||||||
|
std::thread th1 ([&](){
|
||||||
|
do
|
||||||
|
modem.inetd(false);
|
||||||
|
while (!m.try_lock());
|
||||||
|
m.unlock();
|
||||||
|
});
|
||||||
|
|
||||||
|
auto run_receive = [&](size_t times) -> size_t {
|
||||||
|
size_t s =0;
|
||||||
|
for (size_t i=0 ; i<times ; ++i)
|
||||||
|
s += modem.receive(buffer);
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
int status1, status2;
|
||||||
|
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", 100000, &status1), true);
|
||||||
|
EXPECT_EQ (status1, 5);
|
||||||
|
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: %,%\r\n\r\nOK\r\n", 100000, &status1, &status2), true);
|
||||||
|
EXPECT_EQ (status1, 0);
|
||||||
|
EXPECT_EQ (status2, 5);
|
||||||
|
|
||||||
|
char substr1[32], substr2[32];
|
||||||
|
EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n%\r\n\r\n%\r\n", 100000, substr1, substr2), true);
|
||||||
|
EXPECT_EQ (std::strcmp("+CREG: 0,5", substr1), 0);
|
||||||
|
EXPECT_EQ (std::strcmp("OK", substr2), 0);
|
||||||
|
|
||||||
|
// returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
|
||||||
|
EXPECT_EQ (modem.command<modem.flush>("AT+CREG?\r\n", "", 100000), true);
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%", 100000, substr1), true);
|
||||||
|
EXPECT_EQ (std::strcmp("\r\n", substr1), 0);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%%", 100000, substr1, substr2), true);
|
||||||
|
EXPECT_EQ (std::strcmp("+CREG: 0,5\r\n", substr1), 0);
|
||||||
|
EXPECT_EQ (std::strcmp("\r\n", substr2), 0);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%", 100000, substr1), true);
|
||||||
|
EXPECT_EQ (std::strcmp("OK\r\n", substr1), 0);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.command<modem.keep>("", "%", 10000), false);
|
||||||
|
|
||||||
|
EXPECT_EQ (modem.size(), 0UL);
|
||||||
|
EXPECT_EQ (run_receive(100000), 0UL);
|
||||||
|
|
||||||
|
m.unlock(); // stop and join inetd
|
||||||
|
th1.join();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
451
test/tests/sequencer.cpp
Normal file
451
test/tests/sequencer.cpp
Normal file
@ -0,0 +1,451 @@
|
|||||||
|
/*!
|
||||||
|
* \file sequencer.cpp
|
||||||
|
*
|
||||||
|
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
||||||
|
*
|
||||||
|
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
* </dd></dl>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include <utl/dev/sequencer.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cstring>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
namespace test_sequencer {
|
||||||
|
using namespace utl;
|
||||||
|
|
||||||
|
// test settings
|
||||||
|
using data_type = char;
|
||||||
|
constexpr size_t size = 64;
|
||||||
|
|
||||||
|
// Sequencer implementer mock
|
||||||
|
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 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) {
|
||||||
|
for (size_t i =0 ; i<NrCommands ; ++i) {
|
||||||
|
if (!std::strcmp(data, command[i])) {
|
||||||
|
cmd =i;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd =NoCommand;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
clock_t clock() { return ++t; }
|
||||||
|
void clear_clock() { t =0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Test sequencer object
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
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, 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;
|
||||||
|
// 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_nop_and_exits) {
|
||||||
|
Seq s;
|
||||||
|
const Seq::script_t<1> script1 = {{
|
||||||
|
{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}
|
||||||
|
}};
|
||||||
|
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, "abcd", Seq::nil, send_chk_text, Seq::exit_ok, 0}
|
||||||
|
}};
|
||||||
|
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_detect) {
|
||||||
|
Seq s;
|
||||||
|
|
||||||
|
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}
|
||||||
|
}};
|
||||||
|
|
||||||
|
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), Seq::exit_error.value);
|
||||||
|
EXPECT_GT (s.clock(), (clock_t)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);
|
||||||
|
|
||||||
|
const Seq::script_t<1> script4 = {{
|
||||||
|
{Seq::control_t::SEND, "cmd1", Seq::nil, handler, Seq::exit_ok, 0}
|
||||||
|
}};
|
||||||
|
handler_flag = false;
|
||||||
|
EXPECT_EQ (s.run(script4), Seq::exit_ok.value);
|
||||||
|
EXPECT_EQ (handler_flag, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Tsequencer, run_boundaries) {
|
||||||
|
Seq s;
|
||||||
|
|
||||||
|
const Seq::script_t<1> script1 = {{
|
||||||
|
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::next, 0},
|
||||||
|
}};
|
||||||
|
EXPECT_EQ (s.run(script1), Seq::exit_error.value);
|
||||||
|
|
||||||
|
const Seq::script_t<1> script2 = {{
|
||||||
|
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::go_to<1>, 0},
|
||||||
|
}};
|
||||||
|
EXPECT_EQ (s.run(script2), Seq::exit_error.value);
|
||||||
|
|
||||||
|
const Seq::script_t<1> script3 = {{
|
||||||
|
{Seq::control_t::NOP, "", Seq::nil, Seq::nil, Seq::go_to<(size_t)-1>, 0},
|
||||||
|
}};
|
||||||
|
EXPECT_EQ (s.run(script3), Seq::exit_error.value);
|
||||||
|
|
||||||
|
const Seq::script_t<1> script4 = {{
|
||||||
|
{Seq::control_t::EXPECT, "abc", Seq::nil, Seq::nil, Seq::next, 1000},
|
||||||
|
}};
|
||||||
|
s.clear_clock();
|
||||||
|
EXPECT_EQ (s.run(script4), Seq::exit_error.value);
|
||||||
|
EXPECT_GT (s.clock(), (clock_t)1000);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user