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.
 
 
 

271 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. using Q = equeue<char, 512, true>;
  46. // BG95 implementer mock
  47. class BG95 : public BG95_base<BG95, Q, 256> {
  48. using base_type = BG95_base<BG95, Q, 256>;
  49. public:
  50. enum class event {
  51. MQTT_DISCONNECT, MQTT_RXDATA
  52. };
  53. // simulated modem operation
  54. private:
  55. struct cmd_pair {
  56. const char *cmd;
  57. const char *resp;
  58. };
  59. struct event_pair {
  60. event e;
  61. const char* resp;
  62. };
  63. std::array<cmd_pair, 19> cmd_map = {{
  64. {"ERROR", "\r\nERROR\r\n"},
  65. {"ATE0\r\n", "\r\nATE0\r\nOK\r\n"},
  66. {"AT\r\n", "\r\nOK\r\n"},
  67. {"AT+QCFG=\"nwscanseq\"\r\n", "\r\n+QCFG: \"nwscanseq\",020301\r\n"},
  68. {"AT+QCFG=\"nwscanseq\",010302\r\n", "\r\nOK\r\n"},
  69. {"AT+CREG?\r\n", "\r\n+CREG: 0,5\r\n\r\nOK\r\n"},
  70. {"AT+CSQ\r\n", "\r\n+CSQ: 19,99\r\n\r\nOK\r\n"},
  71. {"AT+QNWINFO\r\n", "\r\n+QNWINFO: \"EDGE\",\"20201\",\"GSM 1800\",865\r\n\r\nOK\r\n"},
  72. // Files
  73. {"AT+QFLST\r\n", "\r\n+QFLST: \"cacert.pem\",1220\r\n+QFLST: \"security/\",2\r\nOK\r\n"},
  74. // MQTT config
  75. {"AT+QSSLCFG=\"ignorelocaltime\",2,1\r\n", "\r\nOK\r\n"},
  76. {"AT+QSSLCFG=\"seclevel\",2,1\r\n", "\r\nOK\r\n"},
  77. {"AT+QSSLCFG=\"sslversion\",2,4\r\n", "\r\nOK\r\n"},
  78. {"AT+QSSLCFG=\"ciphersuite\",2\r\n", "\r\n+QSSLCFG: \"ciphersuite\",2,0XFFFF\r\n\r\nOK\r\n"},
  79. {"AT+QMTCFG=\"ssl\",0,1,2\r\n", "\r\nOK\r\n"},
  80. {"AT+QMTCFG=\"keepalive\",0,3600\r\n", "\r\nOK\r\n"},
  81. // MQTT
  82. {"AT+QMTOPEN=0,\"server.com\",8883\r\n", "\r\nOK\r\n\r\n+QMTOPEN: 0,0\r\n"},
  83. {"AT+QMTCONN=0,\"myID\",\"user\",\"pass\"\r\n", "\r\nOK\r\n\r\n+QMTCONN: 0,0,0\r\n"},
  84. {"AT+QMTSUB=0,1,\"/path/topic1\",2\r\n", "\r\nOK\r\n\r\n+QMTSUB: 0,1,0,2\r\n"},
  85. {"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"},
  86. }};
  87. std::array<event_pair, 2> event_map {{
  88. {event::MQTT_DISCONNECT, "\r\n+QMTSTAT: 0,1\r\n"},
  89. {event::MQTT_RXDATA, "\r\n+QMTRECV: 0,1,\"/path/topic1\",\"BR: hello to all of my subscribers\""}
  90. }};
  91. const char* cmd_responce (const char* cmd) {
  92. for (auto& it : cmd_map) {
  93. if (!strcmp(it.cmd, cmd))
  94. return it.resp;
  95. }
  96. return cmd_map[0].resp;
  97. }
  98. const char* event_responce (const event e) {
  99. for (auto& it : event_map) {
  100. if (e == it.e)
  101. return it.resp;
  102. }
  103. return nullptr; // non reachable
  104. }
  105. // data
  106. Q rx_q{};
  107. std::atomic<size_t> lines{};
  108. public:
  109. using range_t = typename Q::range_t;
  110. public:
  111. // BG95_base driver requirements
  112. BG95() :
  113. rx_q(Q::data_match::MATCH_PUSH, base_type::delimiter, [&](){
  114. lines.fetch_add(1, std::memory_order_acq_rel);
  115. }), lines(0) { }
  116. size_t get(char* data, bool wait =false) {
  117. do {
  118. if (lines.load(std::memory_order_acquire)) {
  119. size_t n =0;
  120. do{
  121. *data << rx_q;
  122. ++n;
  123. } while (*data++ != base_type::delimiter);
  124. lines.fetch_sub(1, std::memory_order_acq_rel);
  125. return n;
  126. }
  127. } while (wait);
  128. return 0;
  129. }
  130. size_t put (const char* data, size_t n) {
  131. std::cerr << " ";
  132. const char* reply = cmd_responce (data);
  133. while (*reply)
  134. rx_q << *reply++;
  135. return n;
  136. }
  137. const range_t contents() const {
  138. return range_t {rx_q.begin(), rx_q.end()};
  139. }
  140. clock_t clock() { static clock_t t=0; return ++t; }
  141. // extra helper for testing purposes
  142. void async (event e) {
  143. const char* reply =event_responce (e);
  144. while (*reply)
  145. rx_q << *reply++;
  146. }
  147. };
  148. // Behavior flag
  149. bool handler_flag = false;
  150. void handler (const char* data, size_t n) {
  151. (void)*data;
  152. (void)n;
  153. // std::cout << "* handler called\n";
  154. handler_flag = true;
  155. }
  156. void clear_flag () {
  157. handler_flag = false;
  158. }
  159. /*
  160. * Test inetd in non blocking mode
  161. */
  162. TEST(TBG95_base, inetd_non_blocking) {
  163. BG95 modem;
  164. char buffer[256];
  165. const BG95::async_handlers<2> async = {{
  166. {"+QMTOPEN:", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
  167. {"+QMT", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
  168. }};
  169. clear_flag();
  170. modem.inetd(false, &async);
  171. EXPECT_EQ (handler_flag, false);
  172. modem.async(BG95::event::MQTT_DISCONNECT);
  173. modem.inetd(false, &async); // parse "\r\n"
  174. EXPECT_EQ (handler_flag, false);
  175. modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
  176. EXPECT_EQ (handler_flag, true);
  177. clear_flag(); // nothing to parse
  178. modem.inetd(false, &async);
  179. modem.inetd(false, &async);
  180. modem.inetd(false, &async);
  181. EXPECT_EQ (handler_flag, false);
  182. EXPECT_NE (modem.receive(buffer), 0UL); // "\r\n" in buffer
  183. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  184. clear_flag();
  185. modem.inetd(false, &async);
  186. EXPECT_EQ (handler_flag, false);
  187. modem.transmit("AT+CSQ\r\n", 8);
  188. EXPECT_EQ (modem.receive(buffer), 0UL);
  189. modem.inetd(false, &async); // parse "\r\n"
  190. EXPECT_NE (modem.receive(buffer), 0UL);
  191. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  192. modem.inetd(false, &async); // parse "+CSQ: 19,99\r\n"
  193. EXPECT_NE (modem.receive(buffer), 0UL);
  194. EXPECT_EQ (strcmp(buffer, "+CSQ: 19,99\r\n"), 0);
  195. modem.inetd(false, &async); // parse "\r\n"
  196. EXPECT_NE (modem.receive(buffer), 0UL);
  197. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  198. modem.inetd(false, &async); // parse "OK\r\n"
  199. EXPECT_NE (modem.receive(buffer), 0UL);
  200. EXPECT_EQ (strcmp(buffer, "OK\r\n"), 0);
  201. modem.inetd(false, &async); // nothing to parse
  202. modem.inetd(false, &async);
  203. modem.inetd(false, &async);
  204. EXPECT_EQ (modem.receive(buffer), 0UL);
  205. }
  206. TEST(TBG95_base, run) {
  207. BG95 modem;
  208. const BG95::async_handlers<2> async = {{
  209. {"+QMTOPEN:", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
  210. {"+QMT", BG95::match_t::STARTS_WITH, handler, BG95::action_t::NO, 0},
  211. }};
  212. const BG95::script_t<5> script = {{
  213. /* 0 */{BG95::control_t::NOP, {"", BG95::match_t::NO, nullptr, BG95::action_t::GOTO, 1}, 1000},
  214. /* 1 */{BG95::control_t::SEND, {"ATE0\r\n", BG95::match_t::NO, nullptr, BG95::action_t::NEXT, 0}, 0},
  215. /* 2 */{BG95::control_t::EXPECT, {{
  216. {"OK\r\n", BG95::match_t::ENDS_WITH, nullptr, BG95::action_t::NEXT, 0},
  217. {"ERROR", BG95::match_t::CONTAINS, nullptr, BG95::action_t::EXIT_ERROR, 0} }},
  218. 1000
  219. },
  220. /* 3 */{BG95::control_t::SEND, {"AT+CSQ\r\n", BG95::match_t::NO, nullptr, BG95::action_t::NEXT, 0}, 0},
  221. /* 4 */{BG95::control_t::EXPECT, {{
  222. {"OK\r\n", BG95::match_t::ENDS_WITH, nullptr, BG95::action_t::EXIT_OK, 0},
  223. {"ERROR", BG95::match_t::CONTAINS, nullptr, BG95::action_t::EXIT_ERROR, 0} }},
  224. 1000
  225. },
  226. }};
  227. std::atomic<bool> lock(true);
  228. std::thread th1 ([&](){
  229. do
  230. modem.inetd(false, &async);
  231. while (lock.load(std::memory_order_acquire));
  232. });
  233. EXPECT_EQ (modem.run(script), true);
  234. lock.store(false, std::memory_order_acq_rel);
  235. th1.join();
  236. }
  237. TEST(TBG95_base, command) {
  238. BG95 modem;
  239. std::atomic<bool> lock(true);
  240. std::thread th1 ([&](){
  241. do
  242. modem.inetd(false);
  243. while (lock.load(std::memory_order_acquire));
  244. });
  245. EXPECT_EQ (modem.registered(), true);
  246. lock.store(false, std::memory_order_acq_rel);
  247. th1.join();
  248. }
  249. }