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>, N> {
  48. using Q = equeue<char, N, true>;
  49. using base_type = BG95_base<BG95<N>, 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 contents(char* data) {
  132. char* nullpos = std::copy(rx_q.begin(), rx_q.end(), data);
  133. *nullpos =0;
  134. return nullpos - data;
  135. }
  136. size_t put (const char* data, size_t n) {
  137. std::cerr << " ";
  138. const char* reply = cmd_responce (data);
  139. while (*reply)
  140. rx_q << *reply++;
  141. return n;
  142. }
  143. clock_t clock() { static clock_t t=0; return ++t; }
  144. // extra helper for testing purposes
  145. void async (event e) {
  146. const char* reply =event_responce (e);
  147. while (*reply)
  148. rx_q << *reply++;
  149. }
  150. };
  151. // Behavior flag
  152. bool handler_flag = false;
  153. void handler (const char* data, size_t n) {
  154. (void)*data;
  155. (void)n;
  156. // std::cout << "* handler called\n";
  157. handler_flag = true;
  158. }
  159. void clear_flag () {
  160. handler_flag = false;
  161. }
  162. /*
  163. * Test inetd in non blocking mode
  164. */
  165. TEST(TBG95_base, inetd_non_blocking) {
  166. BG95<256> modem;
  167. char buffer[256];
  168. const BG95<256>::inetd_handlers<2> async = {{
  169. {"+QMTOPEN:", BG95<256>::starts_with, handler},
  170. {"+QMT", BG95<256>::starts_with, handler},
  171. }};
  172. clear_flag();
  173. modem.inetd(false, &async);
  174. EXPECT_EQ (handler_flag, false);
  175. modem.async(BG95<256>::event::MQTT_DISCONNECT);
  176. modem.inetd(false, &async); // parse "\r\n"
  177. EXPECT_EQ (handler_flag, false);
  178. modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
  179. EXPECT_EQ (handler_flag, true);
  180. clear_flag(); // nothing to parse
  181. modem.inetd(false, &async);
  182. modem.inetd(false, &async);
  183. modem.inetd(false, &async);
  184. EXPECT_EQ (handler_flag, false);
  185. EXPECT_NE (modem.receive(buffer), 0UL); // "\r\n" in buffer
  186. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  187. clear_flag();
  188. modem.inetd(false, &async);
  189. EXPECT_EQ (handler_flag, false);
  190. modem.transmit("AT+CSQ\r\n", 8);
  191. EXPECT_EQ (modem.receive(buffer), 0UL);
  192. modem.inetd(false, &async); // parse "\r\n"
  193. EXPECT_NE (modem.receive(buffer), 0UL);
  194. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  195. modem.inetd(false, &async); // parse "+CSQ: 19,99\r\n"
  196. EXPECT_NE (modem.receive(buffer), 0UL);
  197. EXPECT_EQ (strcmp(buffer, "+CSQ: 19,99\r\n"), 0);
  198. modem.inetd(false, &async); // parse "\r\n"
  199. EXPECT_NE (modem.receive(buffer), 0UL);
  200. EXPECT_EQ (strcmp(buffer, "\r\n"), 0);
  201. modem.inetd(false, &async); // parse "OK\r\n"
  202. EXPECT_NE (modem.receive(buffer), 0UL);
  203. EXPECT_EQ (strcmp(buffer, "OK\r\n"), 0);
  204. modem.inetd(false, &async); // nothing to parse
  205. modem.inetd(false, &async);
  206. modem.inetd(false, &async);
  207. EXPECT_EQ (modem.receive(buffer), 0UL);
  208. }
  209. // TEST(TBG95_base, run) {
  210. // BG95<256> modem;
  211. //
  212. // using Control = BG95<256>::control_t;
  213. // using Action = BG95<256>::action_t;
  214. //
  215. // const BG95<256>::inetd_handlers<2> async = {{
  216. // {"+QMTOPEN:", BG95<256>::starts_with, handler},
  217. // {"+QMT", BG95<256>::starts_with, handler},
  218. // }};
  219. // const BG95<256>::script_t<7> script = {{
  220. // {Control::NOP, "", BG95<256>::nil, BG95<256>::nil, {Action::GOTO, 1}, 1000},
  221. // {Control::SEND, "ATE0\r\n", BG95<256>::nil, BG95<256>::nil, {Action::NEXT, 0}, 0},
  222. // {Control::EXPECT, "OK\r\n", BG95<256>::ends_with, BG95<256>::nil, {Action::NEXT, 0}, 1000},
  223. // {Control::OR_EXPECT,"ERROR", BG95<256>::contains, BG95<256>::nil, {Action::EXIT_ERROR, 0}, 0},
  224. // {Control::SEND, "AT+CSQ\r\n",BG95<256>::nil, BG95<256>::nil, {Action::NEXT, 0}, 0},
  225. // {Control::EXPECT, "OK\r\n", BG95<256>::ends_with, BG95<256>::nil, {Action::EXIT_OK, 0}, 1000},
  226. // {Control::OR_EXPECT,"ERROR", BG95<256>::contains, BG95<256>::nil, {Action::EXIT_ERROR, 0}, 0},
  227. // }};
  228. //
  229. // std::mutex m;
  230. // m.lock();
  231. // std::thread th1 ([&](){
  232. // do
  233. // modem.inetd(false, &async);
  234. // while (!m.try_lock());
  235. // m.unlock();
  236. // });
  237. // EXPECT_EQ (modem.run(script), true);
  238. // m.unlock(); // stop and join inetd
  239. // th1.join();
  240. // }
  241. TEST(TBG95_base, command) {
  242. BG95<256> modem;
  243. std::mutex m;
  244. m.lock();
  245. std::thread th1 ([&](){
  246. do
  247. modem.inetd(false);
  248. while (!m.try_lock());
  249. m.unlock();
  250. });
  251. int status;
  252. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", &status), true);
  253. EXPECT_EQ (status, 5);
  254. char substr[32];
  255. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n%\r\n\r\nOK\r\n", substr), true);
  256. EXPECT_EQ (strcmp("+CREG: 0,5", substr), 0);
  257. m.unlock(); // stop and join inetd
  258. th1.join();
  259. }
  260. }