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.
 
 
 

322 lines
11 KiB

  1. /*!
  2. * \file cont/BG95_base.h
  3. * \brief
  4. * BG95 driver functionality as CRTP base class
  5. *
  6. * \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
  7. *
  8. * <dl class=\"section copyright\"><dt>License</dt><dd>
  9. * The MIT License (MIT)
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. * </dd></dl>
  29. */
  30. #ifndef TBX_DRV_BG95_base_H_
  31. #define TBX_DRV_BG95_base_H_
  32. #include <core/core.h>
  33. #include <core/crtp.h>
  34. #include <cont/equeue.h>
  35. #include <cont/range.h>
  36. #include <utils/sequencer.h>
  37. #include <cstring>
  38. #include <cstdio>
  39. #include <algorithm>
  40. //#include <iostream>
  41. #include <atomic>
  42. namespace tbx {
  43. /*!
  44. * \class BG95_base
  45. * \brief
  46. *
  47. * \example implementation example
  48. * \code
  49. * using Queue = equeue<char, 512, true>;
  50. *
  51. * class BG95 :
  52. * public BG95_base<BG95, Queue, 256> {
  53. * using base_type = BG95_base<BG95, Queue, 256>;
  54. * using range_t = typename Queue::range_t;
  55. * private: // data
  56. * Queue rx_q{};
  57. * std::atomic<size_t> lines{};
  58. * public:
  59. * BG95() :
  60. * rx_q(equeue<char, 512, true>::data_match::MATCH_PUSH, base_type::delimiter, [&](){
  61. * lines.fetch_add(1, std::memory_order_acq_rel);
  62. * }), lines(0) {
  63. * // init code here ...
  64. * }
  65. * // ISR handler
  66. * void usart_isr_ (void) {
  67. * rx_q << // char from ISR
  68. * }
  69. * // CRTP requirements
  70. * size_t get(char* data, bool wait =false) {
  71. * do {
  72. * if (lines.load(std::memory_order_acquire)) {
  73. * size_t n =0;
  74. * do{
  75. * *data << rx_q;
  76. * ++n;
  77. * } while (*data++ != base_type::delimiter);
  78. * lines.fetch_sub(1, std::memory_order_acq_rel);
  79. * return n;
  80. * }
  81. * } while (wait);
  82. * return 0;
  83. * }
  84. * size_t put (const char* data, size_t n) {
  85. * // send data to UART
  86. * return n;
  87. * }
  88. * const range_t contents() const { return range_t {rx_q.begin(), rx_q.end()}; }
  89. * clock_t clock() { } // return systems CPU time
  90. * };
  91. * \endcode
  92. *
  93. * \tparam Impl_t
  94. * \tparam Cont_t
  95. * \tparam N
  96. * \tparam Delimiter
  97. */
  98. template<typename Impl_t, typename Cont_t, size_t N, char Delimiter ='\n'>
  99. class BG95_base
  100. : public sequencer<BG95_base<Impl_t, Cont_t, N, Delimiter>, Cont_t, char, N>{
  101. _CRTP_IMPL(Impl_t);
  102. static_assert(
  103. std::is_same_v<typename Cont_t::value_type, char>,
  104. "Cont_t must be a container of type char"
  105. );
  106. // local type dispatch
  107. using base_type = sequencer<BG95_base, Cont_t, char, N>;
  108. using str_view_t = typename base_type::str_view_t;
  109. using range_t = typename Cont_t::range_t;
  110. using status_t = typename base_type::status_t;
  111. //! \name Public types
  112. //! @{
  113. public:
  114. using action_t = typename base_type::action_t;
  115. using control_t = typename base_type::control_t;
  116. using match_t = typename base_type::match_t;
  117. using handler_t = typename base_type::handler_ft;
  118. template<size_t Nr, size_t Nh =2>
  119. using script_t = typename base_type::template script_t<Nr, Nh>;
  120. //! Publish delimiter
  121. constexpr static char delimiter = Delimiter;
  122. //! Required typenames for async operation
  123. //! @{
  124. template <size_t Nm>
  125. using async_handlers = std::array<typename base_type::handle_t, Nm>;
  126. //! @}
  127. //! @}
  128. //! \name Constructor / Destructor
  129. //!@{
  130. protected:
  131. //!< \brief A default constructor from derived only
  132. BG95_base() = default;
  133. ~BG95_base () = default; //!< \brief Allow destructor from derived only
  134. BG95_base(const BG95_base&) = delete; //!< No copies
  135. BG95_base& operator= (const BG95_base&) = delete; //!< No copy assignments
  136. //!@}
  137. //! \name Sequencer interface requirements
  138. //! Forwarded to implementer the calls and cascade the the incoming channel
  139. //! sequencer::get --resolved--> this->receive() --calls--> impl().get()
  140. //! @{
  141. friend base_type;
  142. private:
  143. size_t get_ (char* data) {
  144. return impl().get (data);
  145. }
  146. size_t get (char* data) {
  147. return receive (data);
  148. }
  149. size_t put (const char* data, size_t n) {
  150. return impl().put (data, n);
  151. }
  152. const range_t contents () const {
  153. return impl().contents();
  154. }
  155. clock_t clock () {
  156. return impl().clock();
  157. }
  158. //! @}
  159. //! \name Private functionality
  160. //! @{
  161. private:
  162. //! @}
  163. //! \name public functionality
  164. //! @{
  165. public:
  166. /*!
  167. * \brief
  168. * Transmit data to modem
  169. * \param data Pointer to data to send
  170. * \param n The size of data buffer
  171. * \return The number of transmitted chars
  172. */
  173. size_t transmit (const char* data, size_t n) {
  174. return put (data, n);
  175. }
  176. size_t transmit (const char* data) {
  177. return put (data, strlen(data));
  178. }
  179. /*!
  180. * \brief
  181. * Try to receive data from modem. If there are data copy them to \c data pointer and retur
  182. * the size. Otherwise return zero. In the case \c wait is true block until there are data to get.
  183. *
  184. * \param data Pointer to data buffer to write
  185. * \param wait Flag to select blocking / non-blocking functionality
  186. * \return The number of copied data.
  187. */
  188. size_t receive (char* data, bool wait =false) {
  189. do {
  190. if (streams_.load(std::memory_order_acquire)) {
  191. size_t n =0;
  192. do {
  193. *data << rx_q;
  194. ++n;
  195. } while (*data++ != delimiter);
  196. *data =0;
  197. streams_.fetch_sub(1, std::memory_order_acq_rel);
  198. return n;
  199. }
  200. } while (wait); // on wait flag we block until available stream
  201. return 0;
  202. }
  203. /*!
  204. * \brief
  205. * inetd daemon functionality provided as member function of the driver. This should be running
  206. * in the background either as consecutive calls from an periodic ISR with \c loop = false, or
  207. * as a thread in an RTOS environment with \c loop = true.
  208. *
  209. * \tparam Nm The number of handler array entries
  210. *
  211. * \param async_handles Reference to asynchronous handler array
  212. * \param loop Flag to indicate blocking mode. If true blocking.
  213. */
  214. template <size_t Nm =0>
  215. void inetd (bool loop =true, const async_handlers<Nm>* async_handles =nullptr) {
  216. std::array<char, N> buffer;
  217. size_t resp_size;
  218. do {
  219. if ((resp_size = get_(buffer.data())) != 0) {
  220. // on data check for async handlers
  221. bool match = false;
  222. if (async_handles != nullptr) {
  223. for (auto& h : *async_handles)
  224. match |= base_type::check_handle (h, buffer.data());
  225. }
  226. // if no match forward data to receive channel.
  227. if (!match) {
  228. char* it = buffer.data();
  229. do {
  230. rx_q << *it;
  231. } while (*it++ != delimiter);
  232. streams_.fetch_add(1, std::memory_order_acq_rel);
  233. }
  234. }
  235. } while (loop);
  236. }
  237. template <size_t Steps, size_t Nhandles>
  238. bool configure (const script_t<Steps, Nhandles>& script) {
  239. return base_type::run (script);
  240. }
  241. // // General API
  242. // static constexpr typename base_type::handle_t error_handle_ = {
  243. // "ERROR", match_t::CONTAINS, nullptr, action_t::EXIT_ERROR, 0
  244. // };
  245. template<typename T>
  246. void parse (char* str, size_t n, char next, T value) {
  247. auto next_ptr = std::find(str, &str[n], next);
  248. char save = *next_ptr;
  249. *next_ptr =0;
  250. if constexpr (std::is_same_v<std::remove_cv<T>, int>) {
  251. sscanf(str, "%d", &value);
  252. } else if (std::is_same_v<std::remove_cv<T>, float>) {
  253. sscanf(str, "%f", &value);
  254. } else if (std::is_same_v<std::remove_cv<T>, double>) {
  255. sscanf(str, "%lf", &value);
  256. } else if (std::is_same_v<std::remove_cv<T>, char>) {
  257. sscanf(str, "%c", &value);
  258. } else if (std::is_same_v<std::remove_cv<T>, char*>) {
  259. strcpy(value, str);
  260. }
  261. *next_ptr = save;
  262. }
  263. // cmd: "AT+CREG?"
  264. // expected: "\r\n+CREG: 0,%\r\nOK\r\n"
  265. template <typename T, char Marker = '%'>
  266. bool command (const str_view_t cmd, const str_view_t expected, T& t) {
  267. char buffer[N];
  268. transmit(cmd);
  269. for (size_t pos =0 ; pos < expected.size(); ) {
  270. str_view_t ex = expected.substr(pos); // get starting point of expected
  271. size_t sz = receive(buffer, 1); // load the answer
  272. for (size_t i ; i<sz ; ) {
  273. if (ex[i] == Marker)
  274. parse(buffer[i], sz, ex[i+1], t); // parse and convert
  275. else if (ex[i] == buffer[i])
  276. ++i; // consume current character
  277. else
  278. return false; // Fail to parse
  279. }
  280. }
  281. return true;
  282. }
  283. //! @}
  284. private:
  285. equeue<char, N> rx_q{};
  286. std::atomic<size_t> streams_{};
  287. };
  288. } // namespace tbx;
  289. #endif /* TBX_DRV_BG95_base_H_ */