A C++ toolbox repo until the pair uTL/dTL arives
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

132 líneas
5.5 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 <utils/sequencer.h>
  30. #include <gtest/gtest.h>
  31. namespace test_sequencer {
  32. using namespace tbx;
  33. struct seq_cont_t {
  34. const char *msg_[10] = {
  35. "", "", "", "\r\nOK\r\n",
  36. "", "", "+CCLK = \"21/08/26-12:16:30+12\"\r\nOK\r\n"
  37. "", "", "\r\nERROR\r\n"
  38. };
  39. using value_type = char;
  40. using range_t = range<const char*>;
  41. };
  42. seq_cont_t seq_cont;
  43. // Sequencer implementer mock
  44. class Seq : public sequencer<Seq, seq_cont_t, char, 128> {
  45. using range_t = typename seq_cont_t::range_t;
  46. public:
  47. size_t get(char* data) {
  48. static int msg =0;
  49. size_t len = strlen(seq_cont.msg_[msg]);
  50. strcpy(data, seq_cont.msg_[msg++]);
  51. if (msg >= 10) msg =0;
  52. return len;
  53. }
  54. size_t put (const char* data, size_t n) {
  55. (void)*data;
  56. return n;
  57. }
  58. const range_t contents () const {
  59. return range_t {&seq_cont.msg_[6][0], &seq_cont.msg_[6][5]};
  60. }
  61. clock_t clock() { static clock_t t=0; return ++t; }
  62. static void my_handler (const char* data, size_t size) {
  63. (void)*data;
  64. (void)size;
  65. }
  66. };
  67. /*
  68. * Test sequencer.run()
  69. */
  70. TEST(Tsequencer, run_delay) {
  71. Seq s;
  72. const Seq::script_t<1> script = {{
  73. {Seq::control_t::NOP, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}
  74. }};
  75. EXPECT_EQ (s.run(script), true);
  76. }
  77. TEST(Tsequencer, run_dummy_output) {
  78. Seq s;
  79. const Seq::script_t<2> script = {{
  80. {Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  81. {Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}
  82. }};
  83. EXPECT_EQ (s.run(script), true);
  84. }
  85. TEST(Tsequencer, run_exits) {
  86. Seq s;
  87. const Seq::script_t<1> script1 = {{
  88. {Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000},
  89. }};
  90. EXPECT_EQ (s.run(script1), true);
  91. const Seq::script_t<1> script2 = {{
  92. {Seq::control_t::SEND, "", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
  93. }};
  94. EXPECT_EQ (s.run(script2), false);
  95. }
  96. TEST(Tsequencer, run_sequence) {
  97. Seq s;
  98. const Seq::script_t<13> script = {{
  99. /* 0 */{Seq::control_t::NOP, "", Seq::match_t::NO, nullptr, {Seq::action_t::GOTO, 1}, 1000},
  100. /* 1 */{Seq::control_t::SEND, "ATE0\r\n", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  101. /* 2 */{Seq::control_t::EXPECT, "OK\r\n", Seq::match_t::ENDS_WITH, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  102. /* 3 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
  103. /* 4 */{Seq::control_t::DETECT, "+CCLK", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  104. /* 5 */{Seq::control_t::OR_DETECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
  105. /* 6 */{Seq::control_t::SEND, "AT+CCLK?", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  106. /* 7 */{Seq::control_t::EXPECT, "OK\r\n", Seq::match_t::ENDS_WITH, Seq::my_handler, {Seq::action_t::NEXT, 0}, 1000},
  107. /* 8 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
  108. /* 9 */{Seq::control_t::SEND, "AT+CT?", Seq::match_t::NO, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  109. /*10 */{Seq::control_t::EXPECT, "OK\r\n", Seq::match_t::ENDS_WITH, nullptr, {Seq::action_t::NEXT, 0}, 1000},
  110. /*11 */{Seq::control_t::OR_EXPECT, "ERROR", Seq::match_t::CONTAINS, nullptr, {Seq::action_t::EXIT_ERROR, 0}, 1000},
  111. /*12 */{Seq::control_t::SEND, "AT+POWD=0", Seq::match_t::NO, nullptr, {Seq::action_t::EXIT_OK, 0}, 1000}
  112. }};
  113. EXPECT_EQ (s.run(script), false);
  114. }
  115. }