WIP: BG95/sequencer sanitizers

This commit is contained in:
2021-09-23 19:07:56 +03:00
parent 2c67fb6069
commit bab3fd04fe
4 changed files with 91 additions and 215 deletions
+42 -7
View File
@@ -250,23 +250,58 @@ class BG95_base
// cmd: "AT+CREG?"
// expected: "\r\n+CREG: 0,%\r\n\r\nOK\r\n"
/*!
* \brief
* Send a command to modem and check if the response matches to
* \c expected. If so read any token inside response marked with
* \c Marker, convert the value into type \c T and write it to \c t
*
* \param cmd The comand to send (null terminated)
* \param expected The expected response
* \param t The value to return
* \param timeout The timeout in CPU time (leave it for 0 - no timeout)
*
* \tparam T The type of the value to read from response marked with \c Marker
* \tparam Marker The marker character
* \return True on success
*
* example
* \code
* BG95<256> modem;
* std::thread th1 ([&](){
* modem.inetd(false);
* });
* int status;
* bool done = modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", status);
* if (done && status == 1)
* std::cout << "Connected to home network\n"
* \endcode
*/
template <typename T, char Marker = '%'>
bool command (const char* cmd, const str_view_t expected, T& t) {
bool command (const char* cmd, const str_view_t expected, T& t, clock_t timeout =0) {
char buffer[N];
transmit(cmd);
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
size_t sz = receive(buffer, 1); // load the answer
size_t sz =0;
clock_t mark = clock();
do {
sz = receive(buffer); // load the answer with timeout
if ((timeout != 0 )&& ((clock() - mark) >= timeout)) {
return false;
}
} 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
i += parse(&buffer[i], sz, ex[j+1], t); // parse and convert
++j;
}
else if (ex[j] == buffer[i]) {
++i;
++j; // consume current character
++i; // if same consume current character
++j;
}
else
return false; // Fail to parse
@@ -313,8 +348,8 @@ class BG95_base
//! @}
private:
equeue<char, N> rx_q{};
std::atomic<size_t> streams_{};
equeue<char, N, true> rx_q{};
std::atomic<size_t> streams_{};
};
} // namespace tbx;