diff --git a/esp_modem/examples/modem_console/main/modem_console_main.cpp b/esp_modem/examples/modem_console/main/modem_console_main.cpp index 51f5890fa..4c29de555 100644 --- a/esp_modem/examples/modem_console/main/modem_console_main.cpp +++ b/esp_modem/examples/modem_console/main/modem_console_main.cpp @@ -318,6 +318,8 @@ TEMPLATE(2) static esp_console_repl_t *s_repl = NULL; +using namespace esp_modem; + //template constexpr size_t offset_of(U T::*member) //{ // return (char*)&((T*)nullptr->*member) - (char*)nullptr; diff --git a/esp_modem/include/cxx_include/esp_modem_api.hpp b/esp_modem/include/cxx_include/esp_modem_api.hpp index 2bd96603c..d08d30791 100644 --- a/esp_modem/include/cxx_include/esp_modem_api.hpp +++ b/esp_modem/include/cxx_include/esp_modem_api.hpp @@ -1,27 +1,88 @@ -#pragma once +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_API_HPP_ +#define _ESP_MODEM_API_HPP_ #include #include "cxx_include/esp_modem_dce.hpp" #include "cxx_include/esp_modem_dce_module.hpp" -class DTE; -class GenericModule; -class SIM7600; -class SIM800; -class BG96; struct esp_modem_dte_config; +struct esp_modem_dce_config; + + +namespace esp_modem { + +class DTE; +using dce_config = ::esp_modem_dce_config; +using dte_config = ::esp_modem_dte_config; + typedef struct esp_netif_obj esp_netif_t; -std::shared_ptr create_uart_dte(const esp_modem_dte_config *config); +/** + * @defgroup ESP_MODEM_INIT_DTE ESP_MODEM Initialization API for DTE + * @brief Create DTE's + */ +/** @addtogroup ESP_MODEM_INIT_DTE +* @{ +*/ -std::shared_ptr create_generic_module(const std::shared_ptr& dte, std::string &apn); -std::shared_ptr create_SIM7600_module(const std::shared_ptr& dte, std::string &apn); +/** + * @brief Create UART DTE + * @param config DTE configuration + * @return shared ptr to DTE + */ +std::shared_ptr create_uart_dte(const dte_config *config); +/** + * @} + */ -std::unique_ptr create_generic_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif); -std::unique_ptr create_SIM7600_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif); +/** + * @defgroup ESP_MODEM_INIT_DCE ESP_MODEM Initialization API for DCE + * @brief Create DCE's + */ +/** @addtogroup ESP_MODEM_INIT_DCE +* @{ +*/ +/** + * @brief Create DCE based on SIM7600 module + * @param config DCE configuration + * @param DTE reference to the communicating DTE + * @param netif reference to the network interface + * + * @return unique ptr to the created DCE on success + * nullptr on failure + */ +std::unique_ptr create_SIM7600_dce(const dce_config *config, std::shared_ptr dte, esp_netif_t *netif); -std::unique_ptr create_SIM7600_dce(const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif); -std::unique_ptr create_SIM800_dce(const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif); -std::unique_ptr create_BG96_dce(const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif); +/** + * @brief Create DCE based on SIM800 module + */ +std::unique_ptr create_SIM800_dce(const dce_config *config, std::shared_ptr dte, esp_netif_t *netif); + +/** + * @brief Create DCE based on BG96 module + */ +std::unique_ptr create_BG96_dce(const dce_config *config, std::shared_ptr dte, esp_netif_t *netif); + +/** + * @} + */ + +} // namespace esp_modem + +#endif // _ESP_MODEM_API_HPP_ \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_cmux.hpp b/esp_modem/include/cxx_include/esp_modem_cmux.hpp index 95ba4e9ef..1457950bf 100644 --- a/esp_modem/include/cxx_include/esp_modem_cmux.hpp +++ b/esp_modem/include/cxx_include/esp_modem_cmux.hpp @@ -1,10 +1,32 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/8/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP +#ifndef _ESP_MODEM_CMUX_HPP_ +#define _ESP_MODEM_CMUX_HPP_ +namespace esp_modem { +/** + * @defgroup ESP_MODEM_CMUX ESP_MODEM CMUX class + * @brief Definition of CMUX terminal + */ +/** @addtogroup ESP_MODEM_CMUX +* @{ +*/ + +/** + * @brief CMUX state machine + */ enum class cmux_state { INIT, HEADER, @@ -13,6 +35,14 @@ enum class cmux_state { RECOVER, }; +/** + * @brief CMUX terminal abstraction + * + * This class inherits from Terminal class, as it is a Terminal, but is also composed of another Terminal, + * which is used to communicate with the modem, i.e. the original Terminal which has been multiplexed. + * + * @note Implementation of CMUX protocol is experimental + */ class CMUXedTerminal: public Terminal { public: explicit CMUXedTerminal(std::unique_ptr t, std::unique_ptr b, size_t buff_size): @@ -38,8 +68,12 @@ private: size_t consumed; std::unique_ptr buffer; bool on_cmux(size_t len); - }; +/** + * @} + */ -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP +} // namespace esp_modem + +#endif // _ESP_MODEM_CMUX_HPP_ diff --git a/esp_modem/include/cxx_include/esp_modem_command_library.hpp b/esp_modem/include/cxx_include/esp_modem_command_library.hpp index e9c4a3b5a..45fbe592b 100644 --- a/esp_modem/include/cxx_include/esp_modem_command_library.hpp +++ b/esp_modem/include/cxx_include/esp_modem_command_library.hpp @@ -1,9 +1,19 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/2/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_COMMAND_LIBRARY_HPP_ +#define _ESP_MODEM_COMMAND_LIBRARY_HPP_ #include "esp_modem_dte.hpp" #include "esp_modem_dce_module.hpp" @@ -13,6 +23,17 @@ namespace esp_modem { namespace dce_commands { +/** + * @defgroup ESP_MODEM_DCE_COMMAND ESP_MODEM DCE command library + * @brief Library of the most useful DCE commands + */ +/** @addtogroup ESP_MODEM_DCE_COMMAND + * @{ + */ + +/** + * @brief Declaration of all commands is generated from esp_modem_command_declare.inc + */ #define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \ return_type name(CommandableIf *t, ## __VA_ARGS__); @@ -20,8 +41,11 @@ namespace dce_commands { #undef ESP_MODEM_DECLARE_DCE_COMMAND +/** + * @} + */ + } // dce_commands } // esp_modem - -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP +#endif //_ESP_MODEM_COMMAND_LIBRARY_HPP_ diff --git a/esp_modem/include/cxx_include/esp_modem_dce.hpp b/esp_modem/include/cxx_include/esp_modem_dce.hpp index 345dd1487..bb644467a 100644 --- a/esp_modem/include/cxx_include/esp_modem_dce.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dce.hpp @@ -1,16 +1,30 @@ -#pragma once -#include +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_DCE_HPP_ +#define _ESP_MODEM_DCE_HPP_ + +#include #include "cxx_include/esp_modem_netif.hpp" #include "cxx_include/esp_modem_dce_module.hpp" -//#include "generate/esp_modem_command_declare.inc" -namespace esp_modem::dce_factory { +namespace esp_modem { -class Modes { +class DCE_Mode { public: - Modes(): mode(modem_mode::COMMAND_MODE) {} - ~Modes() = default; + DCE_Mode(): mode(modem_mode::COMMAND_MODE) {} + ~DCE_Mode() = default; bool set(DTE *dte, ModuleIf *module, Netif &netif, modem_mode m); modem_mode get(); @@ -18,7 +32,7 @@ private: modem_mode mode; }; -} + template class DCE_T { @@ -51,13 +65,10 @@ protected: std::shared_ptr dte; std::shared_ptr module; Netif netif; - esp_modem::dce_factory::Modes mode; + DCE_Mode mode; }; -//typedef DCE_T DCE; -// -//#if 0 class DCE: public DCE_T { public: @@ -74,4 +85,7 @@ public: #undef ESP_MODEM_DECLARE_DCE_COMMAND }; -//#endif \ No newline at end of file + +} // esp_modem + +#endif // _ESP_MODEM_DCE_HPP_ \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp b/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp index 631232ad1..6c63583ff 100644 --- a/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp @@ -1,194 +1,204 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/28/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP -#define AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_DCE_FACTORY_HPP_ +#define _ESP_MODEM_DCE_FACTORY_HPP_ namespace esp_modem::dce_factory { -using config = esp_modem_dce_config; +using config = ::esp_modem_dce_config; - class FactoryHelper { - public: - static std::unique_ptr create_pdp_context(std::string &apn); +class FactoryHelper { +public: + static std::unique_ptr create_pdp_context(std::string &apn); - template - static bool make(T* &t, Args&&... args) - { - t = new T(std::forward(args)...); - return true; - } + template + static bool make(T* &t, Args&&... args) + { + t = new T(std::forward(args)...); + return true; + } - template - static bool make(std::shared_ptr &t, Args&&... args) - { - t = std::make_shared(std::forward(args)...); - return true; - } + template + static bool make(std::shared_ptr &t, Args&&... args) + { + t = std::make_shared(std::forward(args)...); + return true; + } - template - static bool make(std::unique_ptr &t, Args&&... args) - { - t = std::unique_ptr(new T(std::forward(args)...)); - return true; - } + template + static bool make(std::unique_ptr &t, Args&&... args) + { + t = std::unique_ptr(new T(std::forward(args)...)); + return true; + } - }; +}; - template - class Builder { - public: - explicit Builder(std::shared_ptr dte): dte(std::move(dte)) - { - esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP(); - netif = esp_netif_new(&netif_config); - throw_if_false(netif != nullptr, "Cannot create default PPP netif"); - } +template +class Builder { +public: + explicit Builder(std::shared_ptr dte): dte(std::move(dte)) + { + esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP(); + netif = esp_netif_new(&netif_config); + throw_if_false(netif != nullptr, "Cannot create default PPP netif"); + } - Builder(std::shared_ptr x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif) - { - throw_if_false(netif != nullptr, "Null netif"); - } + Builder(std::shared_ptr x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif) + { + throw_if_false(netif != nullptr, "Null netif"); + } - Builder(std::shared_ptr dte, esp_netif_t* esp_netif, std::shared_ptr dev): dte(std::move(dte)), module(std::move(dev)), netif(esp_netif) - { - throw_if_false(netif != nullptr, "Null netif"); - } + Builder(std::shared_ptr dte, esp_netif_t* esp_netif, std::shared_ptr dev): dte(std::move(dte)), module(std::move(dev)), netif(esp_netif) + { + throw_if_false(netif != nullptr, "Null netif"); + } - ~Builder() - { - throw_if_false(module == nullptr, "module was captured or created but never used"); - } + ~Builder() + { + throw_if_false(module == nullptr, "module was captured or created but never used"); + } - template - bool create_module(Ptr &t, const esp_modem_dce_config *config) - { - return FactoryHelper::make(t, dte, config); - } + template + bool create_module(Ptr &t, const esp_modem_dce_config *config) + { + return FactoryHelper::make(t, dte, config); + } - template - bool create(Ptr &t, const esp_modem_dce_config *config) - { - if (dte == nullptr) + template + bool create(Ptr &t, const esp_modem_dce_config *config) + { + if (dte == nullptr) + return false; + if (module == nullptr) { + if (!create_module(module, config)) { return false; - if (module == nullptr) { - if (!create_module(module, config)) { - return false; - } } - return FactoryHelper::make(t, std::move(dte), std::move(module), netif); } + return FactoryHelper::make(t, std::move(dte), std::move(module), netif); + } - private: - std::shared_ptr dte; - std::shared_ptr module; - esp_netif_t *netif; - }; +private: + std::shared_ptr dte; + std::shared_ptr module; + esp_netif_t *netif; +}; - enum class Modem { - SIM800, - SIM7600, - BG96, - MinModule - }; +enum class Modem { + SIM800, + SIM7600, + BG96, + MinModule +}; - class Factory { - public: - explicit Factory(Modem modem): m(modem) {} +class Factory { +public: + explicit Factory(Modem modem): m(modem) {} - template - static std::unique_ptr build_unique(const config *cfg, Args&&... args) - { - return build_generic_DCE, T>(cfg, std::forward(args)...); + template + static std::unique_ptr build_unique(const config *cfg, Args&&... args) + { + return build_generic_DCE, T>(cfg, std::forward(args)...); + } + + template + static DCE* build(const config *cfg, Args&&... args) + { + return build_generic_DCE(cfg, std::forward(args)...); + } + + + template + static std::shared_ptr build_shared_module(const config *cfg, Args&&... args) + { + return build_module_T, T>(cfg, std::forward(args)...); + } + + + template + std::shared_ptr build_shared_module(const config *cfg, Args&&... args) + { + switch (m) { + case Modem::SIM800: + return build_shared_module(cfg, std::forward(args)...); + case Modem::SIM7600: + return build_shared_module(cfg, std::forward(args)...); + case Modem::BG96: + return build_shared_module(cfg, std::forward(args)...); + case Modem::MinModule: + break; } + return nullptr; + } - template - static DCE* build(const config *cfg, Args&&... args) - { - return build_generic_DCE(cfg, std::forward(args)...); + template + std::unique_ptr build_unique(const config *cfg, Args&&... args) + { + switch (m) { + case Modem::SIM800: + return build_unique(cfg, std::forward(args)...); + case Modem::SIM7600: + return build_unique(cfg, std::forward(args)...); + case Modem::BG96: + return build_unique(cfg, std::forward(args)...); + case Modem::MinModule: + break; } + return nullptr; + } - template - static std::shared_ptr build_shared_module(const config *cfg, Args&&... args) - { - return build_module_T, T>(cfg, std::forward(args)...); - } +private: + Modem m; + template + static bool build_module_T(U &t, const config *cfg, Args&&... args) + { + Builder b(std::forward(args)...); + return b.create_module(t, cfg); + } - template - std::shared_ptr build_shared_module(const config *cfg, Args&&... args) - { - switch (m) { - case Modem::SIM800: - return build_shared_module(cfg, std::forward(args)...); - case Modem::SIM7600: - return build_shared_module(cfg, std::forward(args)...); - case Modem::BG96: - return build_shared_module(cfg, std::forward(args)...); - case Modem::MinModule: - break; - } - return nullptr; - } + template + static T build_module_T(const config *cfg, Args&&... args) + { + T module; + if (build_module_T(module, cfg, std::forward(args)...)) + return module; + return nullptr; + } - template - std::unique_ptr build_unique(const config *cfg, Args&&... args) - { - switch (m) { - case Modem::SIM800: - return build_unique(cfg, std::forward(args)...); - case Modem::SIM7600: - return build_unique(cfg, std::forward(args)...); - case Modem::BG96: - return build_unique(cfg, std::forward(args)...); - case Modem::MinModule: - break; - } - return nullptr; - } + template + static bool build_generic_DCE(DcePtr &t, const config *cfg, Args&&... args) + { + Builder b(std::forward(args)...); + return b.template create(t, cfg); + } +protected: + template + static DcePtr build_generic_DCE(const config *cfg, Args&&... args) + { + DcePtr dce; + if (build_generic_DCE(dce, cfg, std::forward(args)...)) + return dce; + return nullptr; + } - private: - Modem m; +}; - template - static bool build_module_T(U &t, const config *cfg, Args&&... args) - { - Builder b(std::forward(args)...); - return b.create_module(t, cfg); - } +} // namespace esp_modem::dce_factory - template - static T build_module_T(const config *cfg, Args&&... args) - { - T module; - if (build_module_T(module, cfg, std::forward(args)...)) - return module; - return nullptr; - } - - template - static bool build_generic_DCE(DcePtr &t, const config *cfg, Args&&... args) - { - Builder b(std::forward(args)...); - return b.template create(t, cfg); - } - - protected: - template - static DcePtr build_generic_DCE(const config *cfg, Args&&... args) - { - DcePtr dce; - if (build_generic_DCE(dce, cfg, std::forward(args)...)) - return dce; - return nullptr; - } - - }; - -} // esp_modem - -#endif //AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP +#endif // _ESP_MODEM_DCE_FACTORY_HPP_ diff --git a/esp_modem/include/cxx_include/esp_modem_dce_module.hpp b/esp_modem/include/cxx_include/esp_modem_dce_module.hpp index 0dbe6477c..f3aafaa44 100644 --- a/esp_modem/include/cxx_include/esp_modem_dce_module.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dce_module.hpp @@ -1,4 +1,20 @@ -#pragma once +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_DCE_MODULE_ +#define _ESP_MODEM_DCE_MODULE_ + #include #include #include "generate/esp_modem_command_declare.inc" @@ -6,8 +22,11 @@ #include "cxx_include/esp_modem_types.hpp" #include "esp_modem_dce_config.h" +namespace esp_modem { + enum class command_result; class DTE; +struct PdpContext; class GenericModule: public ModuleIf { public: @@ -70,3 +89,8 @@ class BG96: public GenericModule { public: command_result get_module_name(std::string& name) override; }; + + +} // namespace esp_modem + +#endif // _ESP_MODEM_DCE_MODULE_ \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_dte.hpp b/esp_modem/include/cxx_include/esp_modem_dte.hpp index 5987e38fd..a8c941ad2 100644 --- a/esp_modem/include/cxx_include/esp_modem_dte.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dte.hpp @@ -1,81 +1,84 @@ -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_DTE_HPP_ +#define _ESP_MODEM_DTE_HPP_ + #include -#include -#include #include #include #include -#include "esp_err.h" #include "cxx_include/esp_modem_primitives.hpp" #include "cxx_include/esp_modem_terminal.hpp" #include "cxx_include/esp_modem_cmux.hpp" #include "cxx_include/esp_modem_types.hpp" +namespace esp_modem { -const int DTE_BUFFER_SIZE = 1024; - - -class DTE: public CommandableIf { +class DTE : public CommandableIf { public: - explicit DTE(std::unique_ptr t); - ~DTE() = default; + explicit DTE(std::unique_ptr t); -// std::unique_ptr detach() { return std::move(term); } -// void attach(std::unique_ptr t) { term = std::move(t); } -// void set_line_cb(got_line f) { on_line_cb = std::move(f); } - int write(uint8_t *data, size_t len) { return term->write(data, len); } - int read(uint8_t **d, size_t len) { + ~DTE() = default; + + int write(uint8_t *data, size_t len) { return term->write(data, len); } + + int read(uint8_t **d, size_t len) { auto data_to_read = std::min(len, buffer_size); auto data = buffer.get(); auto actual_len = term->read(data, data_to_read); *d = data; return actual_len; } - void set_data_cb(std::function f) - { -// on_data = std::move(f); - term->set_data_cb(std::move(f)); + void set_data_cb(std::function f) { + term->set_data_cb(std::move(f)); } -// std::shared_ptr get_buffer() { return buffer;} + void start() { term->start(); } + void data_mode_closed() { term->stop(); } - void set_mode(modem_mode m) { - term->start(); mode = m; + + void set_mode(modem_mode m) { + term->start(); + mode = m; if (m == modem_mode::DATA_MODE) { term->set_data_cb(on_data); } else if (m == modem_mode::CMUX_MODE) { setup_cmux(); } } - command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) override; -// std::shared_ptr buffer; - void send_cmux_command(uint8_t i, const std::string& command); + command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) override; + + void send_cmux_command(uint8_t i, const std::string &command); + private: Lock lock; -// std::unique_ptr cmux; + void setup_cmux(); -// void send_sabm(size_t dlci); -// CMUXHelper cmux; + static const size_t GOT_LINE = BIT0; size_t buffer_size; size_t consumed; -// std::shared_ptr> buffer; std::unique_ptr buffer; std::unique_ptr term; -// got_line_cb on_line; modem_mode mode; signal_group signal; std::function on_data; - - }; +} // namespace esp_modem - - - - -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP +#endif // _ESP_MODEM_DTE_HPP_ diff --git a/esp_modem/include/cxx_include/esp_modem_netif.hpp b/esp_modem/include/cxx_include/esp_modem_netif.hpp index 662e27be8..c0aa8e7cb 100644 --- a/esp_modem/include/cxx_include/esp_modem_netif.hpp +++ b/esp_modem/include/cxx_include/esp_modem_netif.hpp @@ -1,13 +1,25 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 2/26/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_NETIF_HPP +#define _ESP_MODEM_NETIF_HPP #include "esp_netif.h" #include "cxx_include/esp_modem_primitives.hpp" +namespace esp_modem { + class DTE; class Netif; @@ -16,19 +28,25 @@ struct ppp_netif_driver { Netif *ppp; }; - class Netif { public: explicit Netif(std::shared_ptr e, esp_netif_t *netif); + ~Netif(); + void start(); -// void notify_ppp_exit() { signal.set(PPP_EXIT); } - void wait_until_ppp_exits() { signal.wait(PPP_EXIT, 50000); } + + void wait_until_ppp_exits() { signal.wait(PPP_EXIT, 30000); } + void stop(); + private: void receive(uint8_t *data, size_t len); + static esp_err_t esp_modem_dte_transmit(void *h, void *buffer, size_t len); - static esp_err_t esp_modem_post_attach(esp_netif_t * esp_netif, void * args); + + static esp_err_t esp_modem_post_attach(esp_netif_t *esp_netif, void *args); + static void on_ppp_changed(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); std::shared_ptr ppp_dte; @@ -37,8 +55,8 @@ private: signal_group signal; static const size_t PPP_STARTED = BIT0; static const size_t PPP_EXIT = BIT1; - }; +} // namespace esp_modem -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP +#endif // _ESP_MODEM_NETIF_HPP diff --git a/esp_modem/include/cxx_include/esp_modem_primitives.hpp b/esp_modem/include/cxx_include/esp_modem_primitives.hpp index ba76e2a96..ec758ca17 100644 --- a/esp_modem/include/cxx_include/esp_modem_primitives.hpp +++ b/esp_modem/include/cxx_include/esp_modem_primitives.hpp @@ -1,13 +1,25 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 2/26/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_PRIMITIVES_HPP_ +#define _ESP_MODEM_PRIMITIVES_HPP_ #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "freertos/semphr.h" +namespace esp_modem { + #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS #define THROW(exception) throw(exception) class esp_err_exception: virtual public std::exception { @@ -114,4 +126,6 @@ struct signal_group { EventGroupHandle_t event_group; }; -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP +} // namespace esp_modem + +#endif // _ESP_MODEM_PRIMITIVES_HPP_ diff --git a/esp_modem/include/cxx_include/esp_modem_terminal.hpp b/esp_modem/include/cxx_include/esp_modem_terminal.hpp index 8cce80618..b725281bb 100644 --- a/esp_modem/include/cxx_include/esp_modem_terminal.hpp +++ b/esp_modem/include/cxx_include/esp_modem_terminal.hpp @@ -1,10 +1,19 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/3/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP - +#ifndef _ESP_MODEM_TERMINAL_HPP_ +#define _ESP_MODEM_TERMINAL_HPP_ #include #include @@ -15,6 +24,7 @@ #include "esp_err.h" #include "esp_modem_primitives.hpp" +namespace esp_modem { enum class terminal_error { BUFFER_OVERFLOW, @@ -25,12 +35,19 @@ enum class terminal_error { class Terminal { public: virtual ~Terminal() = default; + virtual void set_data_cb(std::function f) { on_data = std::move(f); } + void set_error_cb(std::function f) { on_error = std::move(f); } + virtual int write(uint8_t *data, size_t len) = 0; + virtual int read(uint8_t *data, size_t len) = 0; + virtual void start() = 0; + virtual void stop() = 0; + virtual size_t max_virtual_terms() { return 1; } protected: @@ -38,6 +55,6 @@ protected: std::function on_error; }; +} // namespace esp_modem - -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP +#endif // _ESP_MODEM_TERMINAL_HPP_ diff --git a/esp_modem/include/cxx_include/esp_modem_types.hpp b/esp_modem/include/cxx_include/esp_modem_types.hpp index c417854e5..43bcb4021 100644 --- a/esp_modem/include/cxx_include/esp_modem_types.hpp +++ b/esp_modem/include/cxx_include/esp_modem_types.hpp @@ -1,10 +1,21 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/8/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP +#ifndef _ESP_MODEM_TYPES_HPP_ +#define _ESP_MODEM_TYPES_HPP_ +namespace esp_modem { enum class modem_mode { UNDEF, @@ -22,8 +33,10 @@ enum class command_result { typedef std::function got_line_cb; struct PdpContext { - explicit PdpContext(std::string& apn): context_id(1), protocol_type("IP"), apn(apn) {} - explicit PdpContext(const char *apn): context_id(1), protocol_type("IP"), apn(apn) {} + explicit PdpContext(std::string &apn) : context_id(1), protocol_type("IP"), apn(apn) {} + + explicit PdpContext(const char *apn) : context_id(1), protocol_type("IP"), apn(apn) {} + size_t context_id; std::string protocol_type; std::string apn; @@ -31,13 +44,16 @@ struct PdpContext { class CommandableIf { public: - virtual command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) = 0; + virtual command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) = 0; }; class ModuleIf { public: virtual bool setup_data_mode() = 0; + virtual bool set_mode(modem_mode mode) = 0; }; -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP +} // namespace esp_modem + +#endif // _ESP_MODEM_TYPES_HPP_ diff --git a/esp_modem/include/esp_modem_api.h b/esp_modem/include/esp_modem_api.h index 9c61841ac..43ac4ac56 100644 --- a/esp_modem/include/esp_modem_api.h +++ b/esp_modem/include/esp_modem_api.h @@ -1,10 +1,20 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/8/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_API_H_ +#define _ESP_MODEM_API_H_ -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_API_H -#define SIMPLE_CXX_CLIENT_ESP_MODEM_API_H -#pragma once #include "generate/esp_modem_command_declare.inc" #ifdef __cplusplus @@ -13,6 +23,7 @@ extern "C" { typedef struct esp_modem_dce_wrap esp_modem_dce_t; typedef struct esp_modem_dte_config esp_modem_dte_config_t; +struct PdpContext; typedef enum esp_modem_dce_mode { ESP_MODEM_MODE_COMMAND, @@ -38,4 +49,4 @@ DECLARE_ALL_COMMAND_APIS(declares esp_modem_(esp_modem_t * dce, ...);) #endif -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_API_H +#endif // _CLIENT_ESP_MODEM_API_H_ diff --git a/esp_modem/include/esp_modem_config.h b/esp_modem/include/esp_modem_config.h index a57efadba..0729fd4a5 100644 --- a/esp_modem/include/esp_modem_config.h +++ b/esp_modem/include/esp_modem_config.h @@ -1,9 +1,19 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 2/24/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H -#define SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_CONFIG_H_ +#define _ESP_MODEM_CONFIG_H_ #include "driver/uart.h" #include "esp_modem_dce_config.h" @@ -64,4 +74,4 @@ struct esp_modem_dte_config { typedef struct esp_modem_dte_config esp_modem_dte_config_t; -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H +#endif // _ESP_MODEM_CONFIG_H_ diff --git a/esp_modem/include/esp_modem_dce_config.h b/esp_modem/include/esp_modem_dce_config.h index 15f96a53c..138fb0550 100644 --- a/esp_modem/include/esp_modem_dce_config.h +++ b/esp_modem/include/esp_modem_dce_config.h @@ -1,8 +1,19 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/28/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_DCE_CONFIG_H_ +#define _ESP_MODEM_DCE_CONFIG_H_ struct esp_modem_dce_config { const char* apn; @@ -15,7 +26,4 @@ struct esp_modem_dce_config { typedef struct esp_modem_dce_config esp_modem_dce_config_t; - -#define AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H - -#endif //AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H +#endif // _ESP_MODEM_DCE_CONFIG_H_ diff --git a/esp_modem/include/generate/esp_modem_command_declare.inc b/esp_modem/include/generate/esp_modem_command_declare.inc index df808a937..de799286e 100644 --- a/esp_modem/include/generate/esp_modem_command_declare.inc +++ b/esp_modem/include/generate/esp_modem_command_declare.inc @@ -1,9 +1,19 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/7/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC -#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_MODEM_COMMAND_DECLARE_INC_ +#define _ESP_MODEM_COMMAND_DECLARE_INC_ #ifdef __cplusplus #include @@ -13,8 +23,6 @@ #define BOOL_OUT(x) bool& x #define STRUCT_OUT(struct_name, x) struct_name& x #else -struct PdpContext; - #define STRING_IN(x) const char* x #define STRING_OUT(x) char* x #define BOOL_IN(x) const bool x @@ -22,7 +30,7 @@ struct PdpContext; #define STRUCT_OUT(struct_name, x) struct struct_name* x #endif - +#define _ESP_MODEM_COMMAND_DECLARE_INC_ #define ESP_MODEM_DEFINE_DCE_COMMAND(...) ESP_MODEM_DECLARE_DCE_COMMAND(##__VA_ARGS__) #define DEFINE_ALL_COMMAND_APIS() DECLARE_ALL_COMMAND_APIS() @@ -79,4 +87,4 @@ DECLARE_ALL_COMMAND_APIS() #endif -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC +#endif // _ESP_MODEM_COMMAND_DECLARE_INC_ diff --git a/esp_modem/private_include/exception_stub.hpp b/esp_modem/private_include/exception_stub.hpp index b8e231b88..ed41ad24a 100644 --- a/esp_modem/private_include/exception_stub.hpp +++ b/esp_modem/private_include/exception_stub.hpp @@ -1,9 +1,19 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/16/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef MODEM_CONSOLE_EXCEPTION_STUB_HPP -#define MODEM_CONSOLE_EXCEPTION_STUB_HPP +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _EXCEPTION_STUB_HPP_ +#define _EXCEPTION_STUB_HPP_ #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS #define TRY_CATCH_RET_NULL(block) \ @@ -23,4 +33,4 @@ #endif -#endif //MODEM_CONSOLE_EXCEPTION_STUB_HPP +#endif // _EXCEPTION_STUB_HPP_ diff --git a/esp_modem/private_include/uart_terminal.hpp b/esp_modem/private_include/uart_terminal.hpp index 8757a4592..b647188e3 100644 --- a/esp_modem/private_include/uart_terminal.hpp +++ b/esp_modem/private_include/uart_terminal.hpp @@ -1,16 +1,28 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 2/25/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at -#ifndef SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP -#define SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _UART_TERMINAL_HPP_ +#define _UART_TERMINAL_HPP_ #include "cxx_include/esp_modem_dte.hpp" struct esp_modem_dte_config; +namespace esp_modem { + std::unique_ptr create_uart_terminal(const esp_modem_dte_config *config); +} // namespace esp_modem - -#endif //SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP +#endif // _UART_TERMINAL_HPP_ diff --git a/esp_modem/src/esp_modem_api.cpp b/esp_modem/src/esp_modem_api.cpp index 447a72ce4..0412e49e3 100644 --- a/esp_modem/src/esp_modem_api.cpp +++ b/esp_modem/src/esp_modem_api.cpp @@ -1,6 +1,17 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/3/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "cxx_include/esp_modem_dte.hpp" #include "uart_terminal.hpp" #include "esp_log.h" @@ -10,69 +21,46 @@ #include "esp_modem_config.h" #include "exception_stub.hpp" + +namespace esp_modem { + struct PdpContext; -using namespace esp_modem; static const char *TAG = "modem_api"; -std::shared_ptr create_uart_dte(const esp_modem_dte_config *config) -{ +std::shared_ptr create_uart_dte(const dte_config *config) { TRY_CATCH_RET_NULL( - auto term = create_uart_terminal(config); - return std::make_shared(std::move(term)); + auto term = create_uart_terminal(config); + return std::make_shared(std::move(term)); ) } -template -std::unique_ptr> create_dce(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif) -{ - TRY_CATCH_RET_NULL( - return std::make_unique>(dte, dev, netif); - ) -} - -std::unique_ptr create_generic_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif) -{ - TRY_CATCH_RET_NULL( - return std::make_unique(dte, dev, netif); - ) -} - -//std::unique_ptr create_SIM7600_dce(const std::shared_ptr& dte, esp_netif_t *netif, std::string &apn) -//{ -// auto pdp = std::make_unique(apn); -// auto dev = std::make_shared(dte, std::move(pdp)); -// -// return create_generic_dce_from_module(dte, std::move(dev), netif); -//} - - -static inline std::unique_ptr create_modem_dce(dce_factory::Modem m, const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif) -{ +static inline std::unique_ptr +create_modem_dce(dce_factory::Modem m, const dce_config *config, std::shared_ptr dte, esp_netif_t *netif) { dce_factory::Factory f(m); TRY_CATCH_RET_NULL( - return f.build_unique(config, std::move(dte), netif); + return f.build_unique(config, std::move(dte), netif); ) } -std::unique_ptr create_SIM7600_dce(const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif) -{ +std::unique_ptr create_SIM7600_dce(const dce_config *config, std::shared_ptr dte, esp_netif_t *netif) { return create_modem_dce(dce_factory::Modem::SIM7600, config, std::move(dte), netif); } -std::unique_ptr create_SIM800_dce(const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif) -{ +std::unique_ptr create_SIM800_dce(const dce_config *config, std::shared_ptr dte, esp_netif_t *netif) { return create_modem_dce(dce_factory::Modem::SIM800, config, std::move(dte), netif); } -std::unique_ptr create_BG96_dce(const esp_modem_dce_config *config, std::shared_ptr dte, esp_netif_t *netif) -{ +std::unique_ptr create_BG96_dce(const dce_config *config, std::shared_ptr dte, esp_netif_t *netif) { return create_modem_dce(dce_factory::Modem::BG96, config, std::move(dte), netif); } +} // namespace esp_modem + // // C API definitions - +struct PdpContext; +using namespace esp_modem; struct esp_modem_dce_wrap // need to mimic the polymorphic dispatch as CPP uses templated dispatch { @@ -134,4 +122,3 @@ extern "C" esp_err_t esp_modem_read_pin(esp_modem_dce_t * dce, bool &x) return command_response_to_esp_err(dce_sim7600->read_pin(x)); } -// define all commands manually, as we have to convert char* strings to CPP strings and references to \ No newline at end of file diff --git a/esp_modem/src/esp_modem_cmux.cpp b/esp_modem/src/esp_modem_cmux.cpp index b61fbe9a2..da3fa9c73 100644 --- a/esp_modem/src/esp_modem_cmux.cpp +++ b/esp_modem/src/esp_modem_cmux.cpp @@ -1,9 +1,23 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/4/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include #include "cxx_include/esp_modem_dte.hpp" #include "esp_log.h" + +using namespace esp_modem; + /* CRC8 is the reflected CRC8/ROHC algorithm */ #define FCS_POLYNOMIAL 0xe0 /* reversed crc8 */ #define FCS_INIT_VALUE 0xFF diff --git a/esp_modem/src/esp_modem_command_library.cpp b/esp_modem/src/esp_modem_command_library.cpp index 2e442d6fe..aa9c2cccc 100644 --- a/esp_modem/src/esp_modem_command_library.cpp +++ b/esp_modem/src/esp_modem_command_library.cpp @@ -1,6 +1,16 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/8/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dce_module.hpp" diff --git a/esp_modem/src/esp_modem_dce.cpp b/esp_modem/src/esp_modem_dce.cpp index 966f7dbbb..44adb59f6 100644 --- a/esp_modem/src/esp_modem_dce.cpp +++ b/esp_modem/src/esp_modem_dce.cpp @@ -1,15 +1,25 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/24/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dce.hpp" #include "esp_log.h" -namespace esp_modem::dce_factory { +namespace esp_modem { -bool Modes::set(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m) +bool DCE_Mode::set(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m) { switch (m) { case modem_mode::UNDEF: @@ -50,11 +60,9 @@ bool Modes::set(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m) return false; } -modem_mode Modes::get() +modem_mode DCE_Mode::get() { return mode; } -} - - +} // esp_modem \ No newline at end of file diff --git a/esp_modem/src/esp_modem_dte.cpp b/esp_modem/src/esp_modem_dte.cpp index 2aa467df1..48929bfa7 100644 --- a/esp_modem/src/esp_modem_dte.cpp +++ b/esp_modem/src/esp_modem_dte.cpp @@ -1,7 +1,25 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "cxx_include/esp_modem_dte.hpp" #include #include "esp_log.h" +using namespace esp_modem; + +const int DTE_BUFFER_SIZE = 1024; + DTE::DTE(std::unique_ptr terminal): buffer_size(DTE_BUFFER_SIZE), consumed(0), buffer(std::make_unique(buffer_size)), diff --git a/esp_modem/src/esp_modem_factory.cpp b/esp_modem/src/esp_modem_factory.cpp index e521163b3..f51480294 100644 --- a/esp_modem/src/esp_modem_factory.cpp +++ b/esp_modem/src/esp_modem_factory.cpp @@ -1,8 +1,24 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include #include "cxx_include/esp_modem_types.hpp" #include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dce_module.hpp" +namespace esp_modem { + template std::shared_ptr create_device(const std::shared_ptr& dte, std::string &apn) { @@ -20,6 +36,7 @@ std::shared_ptr create_SIM7600_module(const std::shared_ptr& dte, return create_device(dte, apn); } +} #include "cxx_include/esp_modem_api.hpp" #include "cxx_include/esp_modem_dce_factory.hpp" diff --git a/esp_modem/src/esp_modem_modules.cpp b/esp_modem/src/esp_modem_modules.cpp index 4139b080d..76a70863d 100644 --- a/esp_modem/src/esp_modem_modules.cpp +++ b/esp_modem/src/esp_modem_modules.cpp @@ -1,11 +1,24 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 3/8/21. -// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cxx_include/esp_modem_api.hpp" #include "cxx_include/esp_modem_dce_module.hpp" #include "generate/esp_modem_command_declare.inc" -GenericModule::GenericModule(std::shared_ptr dte, const esp_modem_dce_config *config): +namespace esp_modem { + +GenericModule::GenericModule(std::shared_ptr dte, const dce_config *config) : dte(std::move(dte)), pdp(std::make_unique(config->apn)) {} @@ -17,26 +30,28 @@ GenericModule::GenericModule(std::shared_ptr dte, const esp_modem_dce_confi #define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, arg_nr, MUX_ARG, ...) \ return_type GenericModule::name(__VA_ARGS__) { return esp_modem::dce_commands::name(dte.get() ARGS(arg_nr) ); } -DECLARE_ALL_COMMAND_APIS(return_type name(...) { forwards to esp_modem::dce_commands::name(...) } ) +DECLARE_ALL_COMMAND_APIS(return_type name(...) { + forwards + to + esp_modem::dce_commands::name(...) +}) #undef ESP_MODEM_DECLARE_DCE_COMMAND - -command_result SIM7600::get_module_name(std::string& name) -{ +command_result SIM7600::get_module_name(std::string &name) { name = "7600"; return command_result::OK; } -command_result SIM800::get_module_name(std::string& name) -{ +command_result SIM800::get_module_name(std::string &name) { name = "800L"; return command_result::OK; } -command_result BG96::get_module_name(std::string& name) -{ +command_result BG96::get_module_name(std::string &name) { name = "BG96"; return command_result::OK; } + +} \ No newline at end of file diff --git a/esp_modem/src/esp_modem_netif.cpp b/esp_modem/src/esp_modem_netif.cpp index 59cc69f99..0a1a65db5 100644 --- a/esp_modem/src/esp_modem_netif.cpp +++ b/esp_modem/src/esp_modem_netif.cpp @@ -1,6 +1,17 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD // -// Created by david on 2/26/21. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 // +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include #include #include @@ -9,45 +20,35 @@ #include "cxx_include/esp_modem_dte.hpp" #include "esp_netif_ppp.h" -#include +namespace esp_modem { void Netif::on_ppp_changed(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - Netif *ppp = (Netif*)arg; -// DTE *e = (DTE*)arg; - std::cout << "on_ppp_changed " << std::endl; + int32_t event_id, void *event_data) { + auto *ppp = static_cast(arg); ESP_LOGW("TAG", "PPP state changed event %d", event_id); if (event_id < NETIF_PP_PHASE_OFFSET) { ESP_LOGI("TAG", "PPP state changed event %d", event_id); // only notify the modem on state/error events, ignoring phase transitions ppp->signal.set(PPP_EXIT); -// e->notify_ppp_exit(); -// e->data_mode_closed(); -// esp_modem_notify_ppp_netif_closed(dte); } } -esp_err_t Netif::esp_modem_dte_transmit(void *h, void *buffer, size_t len) -{ - Netif *ppp = (Netif*)h; +esp_err_t Netif::esp_modem_dte_transmit(void *h, void *buffer, size_t len) { + auto *ppp = static_cast(h); if (ppp->signal.is_any(PPP_STARTED)) { -// std::cout << "sending data " << len << std::endl; - if (ppp->ppp_dte->write((uint8_t*)buffer, len) > 0) { + if (ppp->ppp_dte->write((uint8_t *) buffer, len) > 0) { return ESP_OK; } } return ESP_FAIL; } -esp_err_t Netif::esp_modem_post_attach(esp_netif_t * esp_netif, void * args) -{ - auto d = (ppp_netif_driver*)args; - esp_netif_driver_ifconfig_t driver_ifconfig = { }; +esp_err_t Netif::esp_modem_post_attach(esp_netif_t *esp_netif, void *args) { + auto d = (ppp_netif_driver *) args; + esp_netif_driver_ifconfig_t driver_ifconfig = {}; driver_ifconfig.transmit = Netif::esp_modem_dte_transmit; - driver_ifconfig.handle = (void*)d->ppp; - std::cout << "esp_modem_post_attach " << std::endl; + driver_ifconfig.handle = (void *) d->ppp; d->base.netif = esp_netif; ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig)); // check if PPP error events are enabled, if not, do enable the error occurred/state changed @@ -59,32 +60,28 @@ esp_err_t Netif::esp_modem_post_attach(esp_netif_t * esp_netif, void * args) esp_netif_ppp_set_params(esp_netif, &ppp_config); } -// ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, 0)); return ESP_OK; } -void Netif::receive(uint8_t *data, size_t len) -{ +void Netif::receive(uint8_t *data, size_t len) { if (signal.is_any(PPP_STARTED)) { -// std::cout << "received data " << len << std::endl; esp_netif_receive(driver.base.netif, data, len, nullptr); } } -Netif::Netif(std::shared_ptr e, esp_netif_t *ppp_netif): - ppp_dte(std::move(e)), netif(ppp_netif) -{ +Netif::Netif(std::shared_ptr e, esp_netif_t *ppp_netif) : + ppp_dte(std::move(e)), netif(ppp_netif) { driver.base.netif = ppp_netif; driver.ppp = this; driver.base.post_attach = esp_modem_post_attach; - throw_if_esp_fail(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void*)this)); + throw_if_esp_fail(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void *) this)); throw_if_esp_fail(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, ppp_netif)); - throw_if_esp_fail(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected, ppp_netif)); + throw_if_esp_fail( + esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected, ppp_netif)); throw_if_esp_fail(esp_netif_attach(ppp_netif, &driver)); } -void Netif::start() -{ +void Netif::start() { ppp_dte->set_data_cb([this](size_t len) -> bool { uint8_t *data; auto actual_len = ppp_dte->read(&data, len); @@ -95,10 +92,15 @@ void Netif::start() signal.set(PPP_STARTED); } -void Netif::stop() -{ - std::cout << "esp_netif_action_stop " << std::endl; +void Netif::stop() { esp_netif_action_stop(driver.base.netif, nullptr, 0, nullptr); signal.clear(PPP_STARTED); +} -} \ No newline at end of file +Netif::~Netif() { + esp_event_handler_unregister(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected); +} + +} // namespace esp_modem \ No newline at end of file diff --git a/esp_modem/src/esp_modem_uart.cpp b/esp_modem/src/esp_modem_uart.cpp index 8d81a07b9..a90a700be 100644 --- a/esp_modem/src/esp_modem_uart.cpp +++ b/esp_modem/src/esp_modem_uart.cpp @@ -1,3 +1,16 @@ +// Copyright 2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include "cxx_include/esp_modem_dte.hpp" #include "freertos/FreeRTOS.h" @@ -13,17 +26,18 @@ static const char *TAG = "uart_terminal"; +namespace esp_modem { + struct uart_resource { explicit uart_resource(const esp_modem_dte_config *config); ~uart_resource(); - bool get_event(uart_event_t& event, uint32_t time_ms) - { + bool get_event(uart_event_t &event, uint32_t time_ms) { return xQueueReceive(event_queue, &event, pdMS_TO_TICKS(time_ms)); } - void reset_events() - { + + void reset_events() { uart_flush_input(port); xQueueReset(event_queue); } @@ -35,14 +49,13 @@ struct uart_resource { }; struct uart_task { - explicit uart_task(size_t stack_size, size_t priority, void* task_param, TaskFunction_t task_function): - task_handle(nullptr) - { + explicit uart_task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t task_function) : + task_handle(nullptr) { BaseType_t ret = xTaskCreate(task_function, "uart_task", 10000, task_param, priority, &task_handle); throw_if_false(ret == pdTRUE, "create uart event task failed"); } - ~uart_task() - { + + ~uart_task() { if (task_handle) vTaskDelete(task_handle); } @@ -51,13 +64,13 @@ struct uart_task { struct uart_event_loop { - explicit uart_event_loop(): event_loop_hdl(nullptr) - { + explicit uart_event_loop() : event_loop_hdl(nullptr) { esp_event_loop_args_t loop_args = {}; loop_args.queue_size = ESP_MODEM_EVENT_QUEUE_SIZE; loop_args.task_name = nullptr; throw_if_esp_fail(esp_event_loop_create(&loop_args, &event_loop_hdl), "create event loop failed"); } + void run() { esp_event_loop_run(event_loop_hdl, pdMS_TO_TICKS(0)); } ~uart_event_loop() { if (event_loop_hdl) esp_event_loop_delete(event_loop_hdl); } @@ -65,17 +78,15 @@ struct uart_event_loop { esp_event_loop_handle_t event_loop_hdl; }; -uart_resource::~uart_resource() -{ +uart_resource::~uart_resource() { if (port >= UART_NUM_0 && port < UART_NUM_MAX) { uart_driver_delete(port); } } -uart_resource::uart_resource(const esp_modem_dte_config *config): - port(-1) -{ +uart_resource::uart_resource(const esp_modem_dte_config *config) : + port(-1) { esp_err_t res; line_buffer_size = config->line_buffer_size; @@ -118,36 +129,38 @@ uart_resource::uart_resource(const esp_modem_dte_config *config): port = config->port_num; } -class uart_terminal: public Terminal { +class uart_terminal : public Terminal { public: - explicit uart_terminal(const esp_modem_dte_config *config): + explicit uart_terminal(const esp_modem_dte_config *config) : uart(config), event_loop(), signal(), task_handle(config->event_task_stack_size, config->event_task_priority, this, s_task) {} ~uart_terminal() override = default; - void start() override - { + + void start() override { signal.set(TASK_START); } - void stop() override - { + + void stop() override { signal.set(TASK_STOP); } int write(uint8_t *data, size_t len) override; + int read(uint8_t *data, size_t len) override; - void set_data_cb(std::function f) override - { + + void set_data_cb(std::function f) override { on_data = std::move(f); signal.set(TASK_PARAMS); } + private: - static void s_task(void * task_param) - { - auto t = static_cast(task_param); + static void s_task(void *task_param) { + auto t = static_cast(task_param); t->task(); vTaskDelete(NULL); } + void task(); static const size_t TASK_INIT = BIT0; @@ -163,17 +176,15 @@ private: }; -std::unique_ptr create_uart_terminal(const esp_modem_dte_config *config) -{ +std::unique_ptr create_uart_terminal(const esp_modem_dte_config *config) { TRY_CATCH_RET_NULL( - auto term = std::make_unique(config); - term->start(); - return term; + auto term = std::make_unique(config); + term->start(); + return term; ) } -void uart_terminal::task() -{ +void uart_terminal::task() { std::function on_data_priv = nullptr; uart_event_t event; size_t len; @@ -182,7 +193,7 @@ void uart_terminal::task() if (signal.is_any(TASK_STOP)) { return; // exits to the static method where the task gets deleted } - while(signal.is_any(TASK_START)) { + while (signal.is_any(TASK_START)) { event_loop.run(); if (uart.get_event(event, 100)) { if (signal.is_any(TASK_PARAMS)) { @@ -229,8 +240,7 @@ void uart_terminal::task() } } -int uart_terminal::read(uint8_t *data, size_t len) -{ +int uart_terminal::read(uint8_t *data, size_t len) { size_t length = 0; uart_get_buffered_data_len(uart.port, &length); if (length > 0) { @@ -239,10 +249,9 @@ int uart_terminal::read(uint8_t *data, size_t len) return 0; } -int uart_terminal::write(uint8_t *data, size_t len) -{ +int uart_terminal::write(uint8_t *data, size_t len) { return uart_write_bytes(uart.port, data, len); } - +} // namespace esp_modem