|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- /*!
- * \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();
-
- }
- }
|