A C++ toolbox repo until the pair uTL/dTL arives
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

285 lines
11 KiB

  1. /*!
  2. * \file sequencer.cpp
  3. *
  4. * \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
  5. *
  6. * <dl class=\"section copyright\"><dt>License</dt><dd>
  7. * The MIT License (MIT)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. * </dd></dl>
  27. *
  28. */
  29. #include <drv/BG95_base.h>
  30. #include <gtest/gtest.h>
  31. #include <cont/equeue.h>
  32. //#include <map>
  33. //#include <iostream>
  34. #include <cstring>
  35. #include <utility>
  36. #ifndef WIN_TRHEADS
  37. #include <mutex>
  38. #include <thread>
  39. #else
  40. #include <mingw.thread.h>
  41. #include <mingw.mutex.h>
  42. #endif
  43. namespace test_bg95_base {
  44. using namespace tbx;
  45. // BG95 implementer mock
  46. template<size_t N>
  47. class BG95 : public BG95_base<BG95<N>, equeue<char, N, true>, N> {
  48. using Q = equeue<char, N, true>;
  49. using base_type = BG95_base<BG95<N>, Q, N>;
  50. public:
  51. enum class event {
  52. MQTT_DISCONNECT, MQTT_RXDATA
  53. };
  54. // simulated modem operation
  55. private:
  56. struct cmd_pair {
  57. const char *cmd;
  58. const char *resp;
  59. };
  60. struct event_pair {
  61. event e;
  62. const char* resp;
  63. };
  64. std::array<cmd_pair, 19> cmd_map = {{
  65. {"ERROR", "\r\nERROR\r\n"},
  66. {"ATE0\r\n", "\r\nATE0\r\nOK\r\n"},
  67. {"AT\r\n", "\r\nOK\r\n"},
  68. {"AT+QCFG=\"nwscanseq\"\r\n", "\r\n+QCFG: \"nwscanseq\",020301\r\n"},
  69. {"AT+QCFG=\"nwscanseq\",010302\r\n", "\r\nOK\r\n"},
  70. {"AT+CREG?\r\n", "\r\n+CREG: 0,5\r\n\r\nOK\r\n"},
  71. {"AT+CSQ\r\n", "\r\n+CSQ: 19,99\r\n\r\nOK\r\n"},
  72. {"AT+QNWINFO\r\n", "\r\n+QNWINFO: \"EDGE\",\"20201\",\"GSM 1800\",865\r\n\r\nOK\r\n"},
  73. // Files
  74. {"AT+QFLST\r\n", "\r\n+QFLST: \"cacert.pem\",1220\r\n+QFLST: \"security/\",2\r\nOK\r\n"},
  75. // MQTT config
  76. {"AT+QSSLCFG=\"ignorelocaltime\",2,1\r\n", "\r\nOK\r\n"},
  77. {"AT+QSSLCFG=\"seclevel\",2,1\r\n", "\r\nOK\r\n"},
  78. {"AT+QSSLCFG=\"sslversion\",2,4\r\n", "\r\nOK\r\n"},
  79. {"AT+QSSLCFG=\"ciphersuite\",2\r\n", "\r\n+QSSLCFG: \"ciphersuite\",2,0XFFFF\r\n\r\nOK\r\n"},
  80. {"AT+QMTCFG=\"ssl\",0,1,2\r\n", "\r\nOK\r\n"},
  81. {"AT+QMTCFG=\"keepalive\",0,3600\r\n", "\r\nOK\r\n"},
  82. // MQTT
  83. {"AT+QMTOPEN=0,\"server.com\",8883\r\n", "\r\nOK\r\n\r\n+QMTOPEN: 0,0\r\n"},
  84. {"AT+QMTCONN=0,\"myID\",\"user\",\"pass\"\r\n", "\r\nOK\r\n\r\n+QMTCONN: 0,0,0\r\n"},
  85. {"AT+QMTSUB=0,1,\"/path/topic1\",2\r\n", "\r\nOK\r\n\r\n+QMTSUB: 0,1,0,2\r\n"},
  86. {"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"},
  87. }};
  88. std::array<event_pair, 2> event_map {{
  89. {event::MQTT_DISCONNECT, "\r\n+QMTSTAT: 0,1\r\n"},
  90. {event::MQTT_RXDATA, "\r\n+QMTRECV: 0,1,\"/path/topic1\",\"BR: hello to all of my subscribers\""}
  91. }};
  92. const char* cmd_responce (const char* cmd) {
  93. for (auto& it : cmd_map) {
  94. if (!strcmp(it.cmd, cmd))
  95. return it.resp;
  96. }
  97. return cmd_map[0].resp;
  98. }
  99. const char* event_responce (const event e) {
  100. for (auto& it : event_map) {
  101. if (e == it.e)
  102. return it.resp;
  103. }
  104. return nullptr; // non reachable
  105. }
  106. // data
  107. Q rx_q{};
  108. std::atomic<size_t> lines{};
  109. public:
  110. using range_t = typename Q::range_t;
  111. public:
  112. // BG95_base driver requirements
  113. BG95() :
  114. rx_q(Q::data_match::MATCH_PUSH, base_type::delimiter, [&](){
  115. lines.fetch_add(1, std::memory_order_acq_rel);
  116. }), lines(0) { }
  117. size_t get(char* data, bool wait =false) {
  118. do {
  119. if (lines.load(std::memory_order_acquire)) {
  120. size_t n =0;
  121. do{
  122. *data << rx_q;
  123. ++n;
  124. } while (*data++ != base_type::delimiter);
  125. lines.fetch_sub(1, std::memory_order_acq_rel);
  126. return n;
  127. }
  128. } while (wait);
  129. return 0;
  130. }
  131. size_t put (const char* data, size_t n) {
  132. std::cerr << " ";
  133. const char* reply = cmd_responce (data);
  134. while (*reply)
  135. rx_q << *reply++;
  136. return n;
  137. }
  138. const range_t contents() const {
  139. return range_t {rx_q.begin(), rx_q.end()};
  140. }
  141. clock_t clock() { static clock_t t=0; return ++t; }
  142. // extra helper for testing purposes
  143. void async (event e) {
  144. const char* reply =event_responce (e);
  145. while (*reply)
  146. rx_q << *reply++;
  147. }
  148. };
  149. // Behavior flag
  150. bool handler_flag = false;
  151. void handler (const char* data, size_t n) {
  152. (void)*data;
  153. (void)n;
  154. // std::cout << "* handler called\n";
  155. handler_flag = true;
  156. }
  157. void clear_flag () {
  158. handler_flag = false;
  159. }
  160. /*
  161. * Test inetd in non blocking mode
  162. */
  163. TEST(TBG95_base, inetd_non_blocking) {
  164. BG95<256> modem;
  165. char buffer[256];
  166. const BG95<256>::async_handlers<2> async = {{
  167. {"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
  168. {"+QMT", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
  169. }};
  170. clear_flag();
  171. modem.inetd(false, &async);
  172. EXPECT_EQ (handler_flag, false);
  173. modem.async(BG95<256>::event::MQTT_DISCONNECT);
  174. modem.inetd(false, &async); // parse "\r\n"
  175. EXPECT_EQ (handler_flag, false);
  176. modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
  177. EXPECT_EQ (handler_flag, true);
  178. clear_flag(); // nothing to parse
  179. modem.inetd(false, &async);
  180. modem.inetd(false, &async);
  181. modem.inetd(false, &async);
  182. EXPECT_EQ (handler_flag, false);
  183. EXPECT_NE (modem.receive(buffer), 0UL); // "\r\n" in buffer
  184. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  185. clear_flag();
  186. modem.inetd(false, &async);
  187. EXPECT_EQ (handler_flag, false);
  188. modem.transmit("AT+CSQ\r\n", 8);
  189. EXPECT_EQ (modem.receive(buffer), 0UL);
  190. modem.inetd(false, &async); // parse "\r\n"
  191. EXPECT_NE (modem.receive(buffer), 0UL);
  192. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  193. modem.inetd(false, &async); // parse "+CSQ: 19,99\r\n"
  194. EXPECT_NE (modem.receive(buffer), 0UL);
  195. EXPECT_EQ (strcmp(buffer, "+CSQ: 19,99\r\n"), 0);
  196. modem.inetd(false, &async); // parse "\r\n"
  197. EXPECT_NE (modem.receive(buffer), 0UL);
  198. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  199. modem.inetd(false, &async); // parse "OK\r\n"
  200. EXPECT_NE (modem.receive(buffer), 0UL);
  201. EXPECT_EQ (strcmp(buffer, "OK\r\n"), 0);
  202. modem.inetd(false, &async); // nothing to parse
  203. modem.inetd(false, &async);
  204. modem.inetd(false, &async);
  205. EXPECT_EQ (modem.receive(buffer), 0UL);
  206. }
  207. TEST(TBG95_base, run) {
  208. BG95<256> modem;
  209. const BG95<256>::async_handlers<2> async = {{
  210. {"+QMTOPEN:", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
  211. {"+QMT", BG95<256>::match_t::STARTS_WITH, handler, BG95<256>::action_t::NO, 0},
  212. }};
  213. const BG95<256>::script_t<5> script = {{
  214. /* 0 */{BG95<256>::control_t::NOP, {"", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::GOTO, 1}, 1000},
  215. /* 1 */{BG95<256>::control_t::SEND, {"ATE0\r\n", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::NEXT, 0}, 0},
  216. /* 2 */{BG95<256>::control_t::EXPECT, {{
  217. {"OK\r\n", BG95<256>::match_t::ENDS_WITH, nullptr, BG95<256>::action_t::NEXT, 0},
  218. {"ERROR", BG95<256>::match_t::CONTAINS, nullptr, BG95<256>::action_t::EXIT_ERROR, 0} }},
  219. 1000
  220. },
  221. /* 3 */{BG95<256>::control_t::SEND, {"AT+CSQ\r\n", BG95<256>::match_t::NO, nullptr, BG95<256>::action_t::NEXT, 0}, 0},
  222. /* 4 */{BG95<256>::control_t::EXPECT, {{
  223. {"OK\r\n", BG95<256>::match_t::ENDS_WITH, nullptr, BG95<256>::action_t::EXIT_OK, 0},
  224. {"ERROR", BG95<256>::match_t::CONTAINS, nullptr, BG95<256>::action_t::EXIT_ERROR, 0} }},
  225. 1000
  226. },
  227. }};
  228. std::mutex m;
  229. m.lock();
  230. std::thread th1 ([&](){
  231. do
  232. modem.inetd(false, &async);
  233. while (!m.try_lock());
  234. m.unlock();
  235. });
  236. EXPECT_EQ (modem.run(script), true);
  237. m.unlock(); // stop and join inetd
  238. th1.join();
  239. }
  240. TEST(TBG95_base, command) {
  241. BG95<256> modem;
  242. std::mutex m;
  243. m.lock();
  244. std::thread th1 ([&](){
  245. do
  246. modem.inetd(false);
  247. while (!m.try_lock());
  248. m.unlock();
  249. });
  250. int status;
  251. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", status), true);
  252. EXPECT_EQ (status, 5);
  253. char substr[32];
  254. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n%\r\n\r\nOK\r\n", substr), true);
  255. EXPECT_EQ (strcmp("+CREG: 0,5", substr), 0);
  256. m.unlock(); // stop and join inetd
  257. th1.join();
  258. }
  259. }