Micro template library A library for building device drivers
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

423 рядки
16 KiB

  1. /*!
  2. * \file cli_device.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 <utl/dev/cli_device.h>
  30. #include <gtest/gtest.h>
  31. #include <utl/container/equeue.h>
  32. #include <cstring>
  33. #include <utility>
  34. #include <type_traits>
  35. #ifndef WIN_TRHEADS
  36. #include <mutex>
  37. #include <thread>
  38. #else
  39. #include <mingw.thread.h>
  40. #include <mingw.mutex.h>
  41. #endif
  42. namespace test_cli_device {
  43. using namespace utl;
  44. // test settings
  45. constexpr size_t Size = 128;
  46. using data_type = char;
  47. // cli_device implementer mock. We simulate a BG95 ATmodem for that purpose
  48. template<size_t N>
  49. class BG95 : public cli_device<BG95<N>, N> {
  50. using base_type = cli_device<BG95<N>, N>;
  51. using Queue = equeue<typename base_type::value_type, N, true>;
  52. public:
  53. enum class event {
  54. MQTT_DISCONNECT, MQTT_RXDATA
  55. };
  56. // simulated modem operation
  57. private:
  58. struct cmd_pair {
  59. const char *cmd;
  60. const char *resp;
  61. };
  62. struct event_pair {
  63. event e;
  64. const char* resp;
  65. };
  66. std::array<cmd_pair, 20> cmd_map = {{
  67. {"", ""},
  68. {"ERROR", "\r\nERROR\r\n"},
  69. {"ATE0\r\n", "\r\nATE0\r\nOK\r\n"},
  70. {"AT\r\n", "\r\nOK\r\n"},
  71. {"AT+QCFG=\"nwscanseq\"\r\n", "\r\n+QCFG: \"nwscanseq\",020301\r\n"},
  72. {"AT+QCFG=\"nwscanseq\",010302\r\n", "\r\nOK\r\n"},
  73. {"AT+CREG?\r\n", "\r\n+CREG: 0,5\r\n\r\nOK\r\n"},
  74. {"AT+CSQ\r\n", "\r\n+CSQ: 19,99\r\n\r\nOK\r\n"},
  75. {"AT+QNWINFO\r\n", "\r\n+QNWINFO: \"EDGE\",\"20201\",\"GSM 1800\",865\r\n\r\nOK\r\n"},
  76. // Files
  77. {"AT+QFLST\r\n", "\r\n+QFLST: \"cacert.pem\",1220\r\n+QFLST: \"security/\",2\r\nOK\r\n"},
  78. // MQTT config
  79. {"AT+QSSLCFG=\"ignorelocaltime\",2,1\r\n", "\r\nOK\r\n"},
  80. {"AT+QSSLCFG=\"seclevel\",2,1\r\n", "\r\nOK\r\n"},
  81. {"AT+QSSLCFG=\"sslversion\",2,4\r\n", "\r\nOK\r\n"},
  82. {"AT+QSSLCFG=\"ciphersuite\",2\r\n", "\r\n+QSSLCFG: \"ciphersuite\",2,0XFFFF\r\n\r\nOK\r\n"},
  83. {"AT+QMTCFG=\"ssl\",0,1,2\r\n", "\r\nOK\r\n"},
  84. {"AT+QMTCFG=\"keepalive\",0,3600\r\n", "\r\nOK\r\n"},
  85. // MQTT
  86. {"AT+QMTOPEN=0,\"server.com\",8883\r\n", "\r\nOK\r\n\r\n+QMTOPEN: 0,0\r\n"},
  87. {"AT+QMTCONN=0,\"myID\",\"user\",\"pass\"\r\n", "\r\nOK\r\n\r\n+QMTCONN: 0,0,0\r\n"},
  88. {"AT+QMTSUB=0,1,\"/path/topic1\",2\r\n", "\r\nOK\r\n\r\n+QMTSUB: 0,1,0,2\r\n"},
  89. {"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"},
  90. }};
  91. std::array<event_pair, 2> event_map {{
  92. {event::MQTT_DISCONNECT, "\r\n+QMTSTAT: 0,1\r\n"},
  93. {event::MQTT_RXDATA, "\r\n+QMTRECV: 0,1,\"/path/topic1\",\"BR: hello to all of my subscribers\""}
  94. }};
  95. const char* cmd_responce (const char* cmd) {
  96. if (cmd != nullptr) {
  97. for (auto& it : cmd_map) {
  98. if (!std::strcmp(it.cmd, cmd))
  99. return it.resp;
  100. }
  101. }
  102. return cmd_map[1].resp;
  103. }
  104. const char* event_responce (const event e) {
  105. for (auto& it : event_map) {
  106. if (e == it.e)
  107. return it.resp;
  108. }
  109. return nullptr; // non reachable
  110. }
  111. // data
  112. Queue RxQ{};
  113. std::atomic<size_t> lines{};
  114. clock_t t=0;
  115. public:
  116. // cli_device driver requirements
  117. BG95() noexcept :
  118. RxQ(Queue::data_match::MATCH_PUSH, base_type::delimiter, [&](){
  119. lines.fetch_add(1, std::memory_order_acq_rel);
  120. }), lines(0) { }
  121. size_t get(char* data, bool wait =false) {
  122. do {
  123. if (lines.load(std::memory_order_acquire)) {
  124. size_t n =0;
  125. do{
  126. *data << RxQ;
  127. ++n;
  128. } while (*data++ != base_type::delimiter);
  129. lines.fetch_sub(1, std::memory_order_acq_rel);
  130. return n;
  131. }
  132. } while (wait);
  133. return 0;
  134. }
  135. size_t contents(char* data) {
  136. char* nullpos = std::copy(RxQ.begin(), RxQ.end(), data);
  137. *nullpos =0;
  138. return nullpos - data;
  139. }
  140. size_t put (const char* data, size_t n) {
  141. const char* reply = cmd_responce (data);
  142. while (*reply)
  143. RxQ << *reply++;
  144. return n;
  145. }
  146. clock_t clock() noexcept { return ++t; }
  147. void clear_clock() noexcept { t=0; }
  148. // extra helper for testing purposes
  149. void async (event e) {
  150. const char* reply =event_responce (e);
  151. while (*reply)
  152. RxQ << *reply++;
  153. }
  154. };
  155. // Behavior flag
  156. bool handler_flag = false;
  157. void handler (const char* data, size_t n) {
  158. (void)*data;
  159. (void)n;
  160. handler_flag = true;
  161. }
  162. void clear_flag () {
  163. handler_flag = false;
  164. }
  165. TEST(Tcli_device, traits) {
  166. EXPECT_EQ ( std::is_default_constructible<BG95<Size>>::value, true);
  167. EXPECT_EQ ( std::is_nothrow_default_constructible<BG95<Size>>::value, true);
  168. EXPECT_EQ (!std::is_copy_constructible<BG95<Size>>::value, true);
  169. EXPECT_EQ (!std::is_copy_assignable<BG95<Size>>::value, true);
  170. EXPECT_EQ ((std::is_same_v<BG95<Size>::value_type, data_type>), true);
  171. EXPECT_EQ ((std::is_same_v<BG95<Size>::pointer_type, data_type*>), true);
  172. EXPECT_EQ ((std::is_same_v<BG95<Size>::size_type, size_t>), true);
  173. EXPECT_EQ ((std::is_same_v<BG95<Size>::string_view, std::basic_string_view<data_type>>), true);
  174. }
  175. /*
  176. * Test inetd in non blocking mode
  177. */
  178. TEST(Tcli_device, txrx_inetd) {
  179. BG95<Size> modem;
  180. char buffer[Size];
  181. size_t s =0;
  182. const BG95<Size>::inetd_handlers<2> async = {{
  183. {"+QMTSTAT:", BG95<Size>::starts_with, handler},
  184. {"+QMTRECV", BG95<Size>::contains, handler},
  185. }};
  186. s = modem.transmit("", std::strlen(""));
  187. EXPECT_EQ (s, 0UL);
  188. s = modem.transmit("");
  189. EXPECT_EQ (s, 0UL);
  190. s = modem.transmit(nullptr);
  191. EXPECT_EQ (s, 0UL);
  192. clear_flag();
  193. modem.inetd(false, &async);
  194. EXPECT_EQ (handler_flag, false);
  195. modem.async(BG95<Size>::event::MQTT_DISCONNECT);
  196. modem.inetd(false, &async); // parse "\r\n"
  197. EXPECT_EQ (handler_flag, false);
  198. modem.inetd(false, &async); // parse "+QMT*\r\n" and dispatch to handler()
  199. EXPECT_EQ (handler_flag, true);
  200. clear_flag(); // nothing to parse
  201. modem.inetd(false, &async);
  202. modem.inetd(false, &async);
  203. modem.inetd(false, &async);
  204. EXPECT_EQ (handler_flag, false);
  205. EXPECT_NE (modem.receive(buffer), 0UL); // "\r\n" in buffer
  206. EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
  207. clear_flag();
  208. modem.inetd(false, &async);
  209. EXPECT_EQ (handler_flag, false);
  210. modem.transmit("AT+CSQ\r\n", 8);
  211. EXPECT_EQ (modem.receive(buffer), 0UL);
  212. modem.inetd(false, &async); // parse "\r\n"
  213. EXPECT_NE (modem.receive(buffer), 0UL);
  214. EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
  215. modem.inetd(false, &async); // parse "+CSQ: 19,99\r\n"
  216. EXPECT_NE (modem.receive(buffer), 0UL);
  217. EXPECT_EQ (std::strcmp(buffer, "+CSQ: 19,99\r\n"), 0);
  218. modem.inetd(false, &async); // parse "\r\n"
  219. EXPECT_NE (modem.receive(buffer), 0UL);
  220. EXPECT_EQ (std::strcmp(buffer, "\r\n"), 0);
  221. modem.inetd(false, &async); // parse "OK\r\n"
  222. EXPECT_NE (modem.receive(buffer), 0UL);
  223. EXPECT_EQ (std::strcmp(buffer, "OK\r\n"), 0);
  224. modem.inetd(false, &async); // nothing to parse
  225. modem.inetd(false, &async);
  226. modem.inetd(false, &async);
  227. EXPECT_EQ (modem.receive(buffer), 0UL);
  228. }
  229. TEST(Tcli_device, run) {
  230. BG95<Size> modem;
  231. using Control = BG95<Size>::control_t;
  232. const BG95<Size>::script_t<4> script = {{
  233. {Control::NOP, "", BG95<Size>::nil, BG95<Size>::nil, BG95<Size>::go_to<1>, 100000},
  234. {Control::SEND, "ATE0\r\n", BG95<Size>::nil, BG95<Size>::nil, BG95<Size>::next, 0},
  235. {Control::EXPECT, "OK\r\n", BG95<Size>::ends_with, BG95<Size>::nil, BG95<Size>::exit_ok, 100000},
  236. {Control::OR_EXPECT,"ERROR", BG95<Size>::contains, BG95<Size>::nil, BG95<Size>::exit_error, 0}
  237. }};
  238. std::mutex m;
  239. m.lock();
  240. std::thread th1 ([&](){
  241. do
  242. modem.inetd(false);
  243. while (!m.try_lock());
  244. m.unlock();
  245. });
  246. EXPECT_EQ (modem.run(script), BG95<Size>::exit_ok.value);
  247. m.unlock(); // stop and join inetd
  248. th1.join();
  249. }
  250. TEST(Tcli_device, clear_size) {
  251. BG95<Size> modem;
  252. char buffer[Size];
  253. modem.clear();
  254. EXPECT_EQ (modem.size(), 0UL);
  255. EXPECT_EQ (modem.receive(buffer), 0UL);
  256. modem.transmit("abcd", 4);
  257. modem.inetd(false);
  258. modem.inetd(false);
  259. EXPECT_NE (modem.size(), 0UL);
  260. modem.clear();
  261. EXPECT_EQ (modem.size(), 0UL);
  262. }
  263. TEST(Tcli_device, command_non_extraction) {
  264. BG95<Size> modem;
  265. char buffer[Size];
  266. std::mutex m;
  267. m.lock();
  268. std::thread th1 ([&](){
  269. do
  270. modem.inetd(false);
  271. while (!m.try_lock());
  272. m.unlock();
  273. });
  274. auto run_receive = [&](size_t times) -> size_t {
  275. size_t s =0;
  276. for (size_t i=0 ; i<times ; ++i)
  277. s += modem.receive(buffer);
  278. return s;
  279. };
  280. EXPECT_EQ (modem.command("", "", 0), true); // returns: ""
  281. EXPECT_EQ (modem.size(), 0UL);
  282. EXPECT_EQ (run_receive(100000), 0UL);
  283. EXPECT_EQ (modem.command("", "abcd", 100000), false);// returns: ""
  284. EXPECT_EQ (modem.size(), 0UL);
  285. EXPECT_EQ (run_receive(100000), 0UL);
  286. EXPECT_EQ (modem.command("abcd", "", 0), true); // returns: "\r\nERROR\r\n"
  287. EXPECT_GT (run_receive(100000), 0UL);
  288. EXPECT_EQ (modem.command("AT\r\n", "Something else", 100000), false);// returns: "\r\nOK\r\n"
  289. EXPECT_GT (run_receive(100000), 0UL);
  290. EXPECT_EQ (modem.command("AT\r\n", "\r\nOK\r\n", 100000), true); // returns: "\r\nOK\r\n"
  291. EXPECT_EQ (modem.size(), 0UL);
  292. EXPECT_EQ (run_receive(100000), 0UL);
  293. EXPECT_EQ (modem.command("AT\r\n", "%OK\r\n", 100000), true); // returns: "\r\nOK\r\n"
  294. EXPECT_EQ (modem.size(), 0UL);
  295. EXPECT_EQ (run_receive(100000), 0UL);
  296. // returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
  297. EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%OK\r\n", 0), false);
  298. EXPECT_GT (run_receive(100000), 0UL);
  299. // returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
  300. EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%%%OK\r\n", 0), true);
  301. EXPECT_EQ (modem.size(), 0UL);
  302. EXPECT_EQ (run_receive(100000), 0UL);
  303. // returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
  304. EXPECT_EQ (modem.command<BG95<Size>::flush>("AT+CREG?\r\n", "%", 0), true);
  305. EXPECT_GT (run_receive(100000), 0UL);
  306. EXPECT_EQ (modem.command<BG95<Size>::flush>("AT\r\n", "%%", 0), true); // returns: "\r\nOK\r\n"
  307. EXPECT_EQ (modem.size(), 0UL);
  308. EXPECT_EQ (run_receive(100000), 0UL);
  309. EXPECT_EQ (modem.command<BG95<Size>::flush>("AT\r\n", "%%%", 10000), false); // returns: "\r\nOK\r\n"
  310. EXPECT_EQ (modem.size(), 0UL);
  311. EXPECT_EQ (run_receive(100000), 0UL);
  312. // returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
  313. EXPECT_EQ (modem.command<modem.flush>("AT+CREG?\r\n", "", 0), true);
  314. EXPECT_EQ (modem.command<modem.keep>("", "%", 0), true);
  315. EXPECT_EQ (modem.command<modem.keep>("", "%%", 0), true);
  316. EXPECT_EQ (modem.command<modem.keep>("", "%", 0), true);
  317. EXPECT_EQ (modem.command<modem.keep>("", "%", 10000), false);
  318. EXPECT_EQ (modem.size(), 0UL);
  319. EXPECT_EQ (run_receive(100000), 0UL);
  320. m.unlock(); // stop and join inetd
  321. th1.join();
  322. }
  323. TEST(Tcli_device, command_extraction) {
  324. BG95<Size> modem;
  325. char buffer[Size];
  326. std::mutex m;
  327. m.lock();
  328. std::thread th1 ([&](){
  329. do
  330. modem.inetd(false);
  331. while (!m.try_lock());
  332. m.unlock();
  333. });
  334. auto run_receive = [&](size_t times) -> size_t {
  335. size_t s =0;
  336. for (size_t i=0 ; i<times ; ++i)
  337. s += modem.receive(buffer);
  338. return s;
  339. };
  340. int status1, status2;
  341. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", 100000, &status1), true);
  342. EXPECT_EQ (status1, 5);
  343. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n+CREG: %,%\r\n\r\nOK\r\n", 100000, &status1, &status2), true);
  344. EXPECT_EQ (status1, 0);
  345. EXPECT_EQ (status2, 5);
  346. char substr1[32], substr2[32];
  347. EXPECT_EQ (modem.command("AT+CREG?\r\n", "\r\n%\r\n\r\n%\r\n", 100000, substr1, substr2), true);
  348. EXPECT_EQ (std::strcmp("+CREG: 0,5", substr1), 0);
  349. EXPECT_EQ (std::strcmp("OK", substr2), 0);
  350. // returns: "\r\n+CREG: 0,5\r\n\r\nOK\r\n
  351. EXPECT_EQ (modem.command<modem.flush>("AT+CREG?\r\n", "", 100000), true);
  352. EXPECT_EQ (modem.command<modem.keep>("", "%", 100000, substr1), true);
  353. EXPECT_EQ (std::strcmp("\r\n", substr1), 0);
  354. EXPECT_EQ (modem.command<modem.keep>("", "%%", 100000, substr1, substr2), true);
  355. EXPECT_EQ (std::strcmp("+CREG: 0,5\r\n", substr1), 0);
  356. EXPECT_EQ (std::strcmp("\r\n", substr2), 0);
  357. EXPECT_EQ (modem.command<modem.keep>("", "%", 100000, substr1), true);
  358. EXPECT_EQ (std::strcmp("OK\r\n", substr1), 0);
  359. EXPECT_EQ (modem.command<modem.keep>("", "%", 10000), false);
  360. EXPECT_EQ (modem.size(), 0UL);
  361. EXPECT_EQ (run_receive(100000), 0UL);
  362. m.unlock(); // stop and join inetd
  363. th1.join();
  364. }
  365. }