/*! * \file cont/BG95_base.h * \brief * BG95 driver functionality as CRTP base class * * \copyright Copyright (C) 2021 Christos Choutouridis * *
License
* The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *
*/ #ifndef TBX_DRV_BG95_base_H_ #define TBX_DRV_BG95_base_H_ #define __cplusplus 201703L #include #include #include #include #include #include #include #include #include #include namespace tbx { /*! * \class BG95_base * \brief * * \example implementation example * \code * \endcode * * \tparam Impl_t * \tparam Cont_t * \tparam N * \tparam Delimiter */ template class BG95_base : public sequencer, char, N>{ _CRTP_IMPL(Impl_t); // local type dispatch using base_type = sequencer; //! \name Public types //! @{ public: using value_type = char; using pointer_type = char*; using size_type = size_t; using string_view = typename base_type::string_view; using action_t = typename base_type::action_t; using control_t = typename base_type::control_t; using match_ft = typename base_type::match_ft; using handler_ft = typename base_type::handler_ft; template using script_t = typename base_type::template script_t; //! Publish delimiter constexpr static char delimiter = Delimiter; //! Required types for inetd async handler operation //! @{ struct inetd_handler_t { string_view token; match_ft match; handler_ft handler; }; template using inetd_handlers = std::array; //! @} //! @} //! \name Constructor / Destructor //!@{ protected: //!< \brief A default constructor from derived only BG95_base() = default; ~BG95_base () = default; //!< \brief Allow destructor from derived only BG95_base(const BG95_base&) = delete; //!< No copies BG95_base& operator= (const BG95_base&) = delete; //!< No copy assignments //!@} //! \name Sequencer interface requirements //! Forwarded to implementer the calls and cascade the the incoming channel //! sequencer::get --resolved--> this->receive() --calls--> impl().get() //! @{ friend base_type; private: size_t get_ (char* data) { return impl().get (data); } size_t get (char* data) { return receive (data); } size_t put (const char* data, size_t n) { return impl().put (data, n); } size_t contents (char* data) { return impl().contents(data); } clock_t clock () { return impl().clock(); } //! @} //! \name Private functionality //! @{ private: template void extract (const char* str, T* value) { static_assert ( std::is_same_v, int> || std::is_same_v, double> || std::is_same_v, char>, "Not supported conversion type."); if constexpr (std::is_same_v, int>) { *value = std::atoi(str); } else if (std::is_same_v, double>) { *value = std::atof(str); } else if (std::is_same_v, char>) { std::strcpy(value, str); } } template std::pair parse (const char* expected, const string_view buffer, char* token) { do { if (*expected == Marker) { // We have Marker. Copy to token the next part of buffer, from begin up to expected[1] where // the expected[1] character is and return the size of that part. auto next = std::find(buffer.begin(), buffer.end(), expected[1]); if (next == buffer.end()) break; char* nullpos = std::copy(buffer.begin(), next, token); *nullpos =0; return std::make_pair(next - buffer.begin(), true); } else if (*expected == buffer.front()) { // We have character match, copy the character to token and return 1 (the char size) *token++ = buffer.front(); *token =0; return std::make_pair(1, false); } } while (0); // Fail to parse *token =0; return std::make_pair(0, false); } //! @} //! \name public functionality //! @{ public: /*! * \brief * Transmit data to modem * \param data Pointer to data to send * \param n The size of data buffer * \return The number of transmitted chars */ size_t transmit (const char* data, size_t n) { return put (data, n); } size_t transmit (const char* data) { return put (data, strlen(data)); } /*! * \brief * Try to receive data from modem. If there are data copy them to \c data pointer and retur * the size. Otherwise return zero. In the case \c wait is true block until there are data to get. * * \param data Pointer to data buffer to write * \param wait Flag to select blocking / non-blocking functionality * \return The number of copied data. */ size_t receive (char* data, bool wait =false) { do { if (streams_.load(std::memory_order_acquire)) { size_t n =0; do { *data << rx_q; ++n; } while (*data++ != delimiter); *data =0; streams_.fetch_sub(1, std::memory_order_acq_rel); return n; } } while (wait); // on wait flag we block until available stream return 0; } /*! * \brief * Send a command to modem and check if the response matches to * \c expected. If so read any token inside response marked with * \c Marker, convert the value into type \c T and write it to \c t * * \param cmd The comand to send (null terminated) * \param expected The expected response * \param t The value to return * \param timeout The timeout in CPU time (leave it for 0 - no timeout) * * \tparam T The type of the value to read from response marked with \c Marker * \tparam Marker The marker character * \return True on success * * example * \code * BG95<256> modem; * std::thread th1 ([&](){ * modem.inetd(false); * }); * int status; * bool done = modem.command("AT+CREG?\r\n", "\r\n+CREG: 0,%\r\n\r\nOK\r\n", status); * if (done && status == 1) * std::cout << "Connected to home network\n" * \endcode */ template bool command (const string_view cmd, const string_view expected, T* value, clock_t timeout =0) { char buffer[N]; char token[N]; transmit(cmd.data()); // send command for (auto ex = expected.begin() ; ex != expected.end() ; ) { clock_t mark = clock(); // load the answer with timeout size_t sz =0; do { sz = receive(buffer); if ((timeout != 0 )&& ((clock() - mark) >= timeout)) return false; } while (!sz); for (size_t i=0 ; i (ex++, {&buffer[i], sz-i}, token); if (!step) return false; if (marker) extract(token, value); i += step; } } return true; } /*! * \brief * inetd daemon functionality provided as member function of the driver. This should be running * in the background either as consecutive calls from an periodic ISR with \c loop = false, or * as a thread in an RTOS environment with \c loop = true. * * \tparam Nm The number of handler array entries * * \param async_handles Reference to asynchronous handler array * \param loop Flag to indicate blocking mode. If true blocking. */ template void inetd (bool loop =true, const inetd_handlers* inetd_handlers =nullptr) { std::array buffer; size_t resp_size; do { if ((resp_size = get_(buffer.data())) != 0) { // on data check for async handlers bool match = false; if (inetd_handlers != nullptr) { for (auto& h : *inetd_handlers) match |= base_type::check_handle({buffer.data(), resp_size}, h.token, h.match, h.handler); } // if no match forward data to receive channel. if (!match) { char* it = buffer.data(); do { rx_q << *it; } while (*it++ != delimiter); streams_.fetch_add(1, std::memory_order_acq_rel); } } } while (loop); } //! @} private: equeue rx_q{}; std::atomic streams_{}; }; } // namespace tbx; #endif /* TBX_DRV_BG95_base_H_ */