Add license headers, update namespaces

This commit is contained in:
David Cermak
2021-03-29 19:34:45 +02:00
parent 30bde53d46
commit 246572d286
27 changed files with 813 additions and 437 deletions

View File

@ -318,6 +318,8 @@ TEMPLATE(2)
static esp_console_repl_t *s_repl = NULL; static esp_console_repl_t *s_repl = NULL;
using namespace esp_modem;
//template<typename T, typename U> constexpr size_t offset_of(U T::*member) //template<typename T, typename U> constexpr size_t offset_of(U T::*member)
//{ //{
// return (char*)&((T*)nullptr->*member) - (char*)nullptr; // return (char*)&((T*)nullptr->*member) - (char*)nullptr;

View File

@ -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 <memory> #include <memory>
#include "cxx_include/esp_modem_dce.hpp" #include "cxx_include/esp_modem_dce.hpp"
#include "cxx_include/esp_modem_dce_module.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_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; typedef struct esp_netif_obj esp_netif_t;
std::shared_ptr<DTE> 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<GenericModule> create_generic_module(const std::shared_ptr<DTE>& dte, std::string &apn); /**
std::shared_ptr<SIM7600> create_SIM7600_module(const std::shared_ptr<DTE>& dte, std::string &apn); * @brief Create UART DTE
* @param config DTE configuration
* @return shared ptr to DTE
*/
std::shared_ptr<DTE> create_uart_dte(const dte_config *config);
/**
* @}
*/
std::unique_ptr<DCE> create_generic_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<GenericModule>& dev, esp_netif_t *netif); /**
std::unique_ptr<DCE> create_SIM7600_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SIM7600>& 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<DCE> create_SIM7600_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
std::unique_ptr<DCE> create_SIM7600_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif); /**
std::unique_ptr<DCE> create_SIM800_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif); * @brief Create DCE based on SIM800 module
std::unique_ptr<DCE> create_BG96_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif); */
std::unique_ptr<DCE> create_SIM800_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
/**
* @brief Create DCE based on BG96 module
*/
std::unique_ptr<DCE> create_BG96_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
/**
* @}
*/
} // namespace esp_modem
#endif // _ESP_MODEM_API_HPP_

View File

@ -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 #ifndef _ESP_MODEM_CMUX_HPP_
#define SIMPLE_CXX_CLIENT_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 { enum class cmux_state {
INIT, INIT,
HEADER, HEADER,
@ -13,6 +35,14 @@ enum class cmux_state {
RECOVER, 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 { class CMUXedTerminal: public Terminal {
public: public:
explicit CMUXedTerminal(std::unique_ptr<Terminal> t, std::unique_ptr<uint8_t[]> b, size_t buff_size): explicit CMUXedTerminal(std::unique_ptr<Terminal> t, std::unique_ptr<uint8_t[]> b, size_t buff_size):
@ -38,8 +68,12 @@ private:
size_t consumed; size_t consumed;
std::unique_ptr<uint8_t[]> buffer; std::unique_ptr<uint8_t[]> buffer;
bool on_cmux(size_t len); bool on_cmux(size_t len);
}; };
/**
* @}
*/
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP } // namespace esp_modem
#endif // _ESP_MODEM_CMUX_HPP_

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP //
// 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_dte.hpp"
#include "esp_modem_dce_module.hpp" #include "esp_modem_dce_module.hpp"
@ -13,6 +23,17 @@
namespace esp_modem { namespace esp_modem {
namespace dce_commands { 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, ...) \ #define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
return_type name(CommandableIf *t, ## __VA_ARGS__); return_type name(CommandableIf *t, ## __VA_ARGS__);
@ -20,8 +41,11 @@ namespace dce_commands {
#undef ESP_MODEM_DECLARE_DCE_COMMAND #undef ESP_MODEM_DECLARE_DCE_COMMAND
/**
* @}
*/
} // dce_commands } // dce_commands
} // esp_modem } // esp_modem
#endif //_ESP_MODEM_COMMAND_LIBRARY_HPP_
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP

View File

@ -1,16 +1,30 @@
#pragma once // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
#include <utility> //
// 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 <utility>
#include "cxx_include/esp_modem_netif.hpp" #include "cxx_include/esp_modem_netif.hpp"
#include "cxx_include/esp_modem_dce_module.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: public:
Modes(): mode(modem_mode::COMMAND_MODE) {} DCE_Mode(): mode(modem_mode::COMMAND_MODE) {}
~Modes() = default; ~DCE_Mode() = default;
bool set(DTE *dte, ModuleIf *module, Netif &netif, modem_mode m); bool set(DTE *dte, ModuleIf *module, Netif &netif, modem_mode m);
modem_mode get(); modem_mode get();
@ -18,7 +32,7 @@ private:
modem_mode mode; modem_mode mode;
}; };
}
template<class SpecificModule> template<class SpecificModule>
class DCE_T { class DCE_T {
@ -51,13 +65,10 @@ protected:
std::shared_ptr<DTE> dte; std::shared_ptr<DTE> dte;
std::shared_ptr<SpecificModule> module; std::shared_ptr<SpecificModule> module;
Netif netif; Netif netif;
esp_modem::dce_factory::Modes mode; DCE_Mode mode;
}; };
//typedef DCE_T<GenericModule> DCE;
//
//#if 0
class DCE: public DCE_T<GenericModule> { class DCE: public DCE_T<GenericModule> {
public: public:
@ -74,4 +85,7 @@ public:
#undef ESP_MODEM_DECLARE_DCE_COMMAND #undef ESP_MODEM_DECLARE_DCE_COMMAND
}; };
//#endif
} // esp_modem
#endif // _ESP_MODEM_DCE_HPP_

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP //
// 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 { namespace esp_modem::dce_factory {
using config = esp_modem_dce_config; using config = ::esp_modem_dce_config;
class FactoryHelper { class FactoryHelper {
public: public:
static std::unique_ptr<PdpContext> create_pdp_context(std::string &apn); static std::unique_ptr<PdpContext> create_pdp_context(std::string &apn);
template <typename T, typename ...Args> template <typename T, typename ...Args>
static bool make(T* &t, Args&&... args) static bool make(T* &t, Args&&... args)
{ {
t = new T(std::forward<Args>(args)...); t = new T(std::forward<Args>(args)...);
return true; return true;
} }
template <typename T, typename ...Args> template <typename T, typename ...Args>
static bool make(std::shared_ptr<T> &t, Args&&... args) static bool make(std::shared_ptr<T> &t, Args&&... args)
{ {
t = std::make_shared<T>(std::forward<Args>(args)...); t = std::make_shared<T>(std::forward<Args>(args)...);
return true; return true;
} }
template <typename T, typename ...Args> template <typename T, typename ...Args>
static bool make(std::unique_ptr<T> &t, Args&&... args) static bool make(std::unique_ptr<T> &t, Args&&... args)
{ {
t = std::unique_ptr<T>(new T(std::forward<Args>(args)...)); t = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
return true; return true;
} }
}; };
template<typename T> template<typename T>
class Builder { class Builder {
public: public:
explicit Builder(std::shared_ptr<DTE> dte): dte(std::move(dte)) explicit Builder(std::shared_ptr<DTE> dte): dte(std::move(dte))
{ {
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP(); esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP();
netif = esp_netif_new(&netif_config); netif = esp_netif_new(&netif_config);
throw_if_false(netif != nullptr, "Cannot create default PPP netif"); throw_if_false(netif != nullptr, "Cannot create default PPP netif");
} }
Builder(std::shared_ptr<DTE> x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif) Builder(std::shared_ptr<DTE> x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif)
{ {
throw_if_false(netif != nullptr, "Null netif"); throw_if_false(netif != nullptr, "Null netif");
} }
Builder(std::shared_ptr<DTE> dte, esp_netif_t* esp_netif, std::shared_ptr<T> dev): dte(std::move(dte)), module(std::move(dev)), netif(esp_netif) Builder(std::shared_ptr<DTE> dte, esp_netif_t* esp_netif, std::shared_ptr<T> dev): dte(std::move(dte)), module(std::move(dev)), netif(esp_netif)
{ {
throw_if_false(netif != nullptr, "Null netif"); throw_if_false(netif != nullptr, "Null netif");
} }
~Builder() ~Builder()
{ {
throw_if_false(module == nullptr, "module was captured or created but never used"); throw_if_false(module == nullptr, "module was captured or created but never used");
} }
template<typename Ptr> template<typename Ptr>
bool create_module(Ptr &t, const esp_modem_dce_config *config) bool create_module(Ptr &t, const esp_modem_dce_config *config)
{ {
return FactoryHelper::make<T>(t, dte, config); return FactoryHelper::make<T>(t, dte, config);
} }
template<typename DceT, typename Ptr> template<typename DceT, typename Ptr>
bool create(Ptr &t, const esp_modem_dce_config *config) bool create(Ptr &t, const esp_modem_dce_config *config)
{ {
if (dte == nullptr) if (dte == nullptr)
return false;
if (module == nullptr) {
if (!create_module(module, config)) {
return false; return false;
if (module == nullptr) {
if (!create_module(module, config)) {
return false;
}
} }
return FactoryHelper::make<DceT>(t, std::move(dte), std::move(module), netif);
} }
return FactoryHelper::make<DceT>(t, std::move(dte), std::move(module), netif);
}
private: private:
std::shared_ptr<DTE> dte; std::shared_ptr<DTE> dte;
std::shared_ptr<T> module; std::shared_ptr<T> module;
esp_netif_t *netif; esp_netif_t *netif;
}; };
enum class Modem { enum class Modem {
SIM800, SIM800,
SIM7600, SIM7600,
BG96, BG96,
MinModule MinModule
}; };
class Factory { class Factory {
public: public:
explicit Factory(Modem modem): m(modem) {} explicit Factory(Modem modem): m(modem) {}
template <typename T, typename ...Args> template <typename T, typename ...Args>
static std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args) static std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args)
{ {
return build_generic_DCE<DCE, std::unique_ptr<DCE>, T>(cfg, std::forward<Args>(args)...); return build_generic_DCE<DCE, std::unique_ptr<DCE>, T>(cfg, std::forward<Args>(args)...);
}
template <typename T, typename ...Args>
static DCE* build(const config *cfg, Args&&... args)
{
return build_generic_DCE<DCE, DCE*, T>(cfg, std::forward<Args>(args)...);
}
template <typename T, typename ...Args>
static std::shared_ptr<T> build_shared_module(const config *cfg, Args&&... args)
{
return build_module_T<std::shared_ptr<T>, T>(cfg, std::forward<Args>(args)...);
}
template <typename ...Args>
std::shared_ptr<GenericModule> build_shared_module(const config *cfg, Args&&... args)
{
switch (m) {
case Modem::SIM800:
return build_shared_module<SIM800>(cfg, std::forward<Args>(args)...);
case Modem::SIM7600:
return build_shared_module<SIM7600>(cfg, std::forward<Args>(args)...);
case Modem::BG96:
return build_shared_module<BG96>(cfg, std::forward<Args>(args)...);
case Modem::MinModule:
break;
} }
return nullptr;
}
template <typename T, typename ...Args> template <typename ...Args>
static DCE* build(const config *cfg, Args&&... args) std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args)
{ {
return build_generic_DCE<DCE, DCE*, T>(cfg, std::forward<Args>(args)...); switch (m) {
case Modem::SIM800:
return build_unique<SIM800>(cfg, std::forward<Args>(args)...);
case Modem::SIM7600:
return build_unique<SIM7600>(cfg, std::forward<Args>(args)...);
case Modem::BG96:
return build_unique<BG96>(cfg, std::forward<Args>(args)...);
case Modem::MinModule:
break;
} }
return nullptr;
}
template <typename T, typename ...Args> private:
static std::shared_ptr<T> build_shared_module(const config *cfg, Args&&... args) Modem m;
{
return build_module_T<std::shared_ptr<T>, T>(cfg, std::forward<Args>(args)...);
}
template <typename T, typename U, typename ...Args>
static bool build_module_T(U &t, const config *cfg, Args&&... args)
{
Builder<T> b(std::forward<Args>(args)...);
return b.create_module(t, cfg);
}
template <typename ...Args> template <typename T, typename U, typename ...Args>
std::shared_ptr<GenericModule> build_shared_module(const config *cfg, Args&&... args) static T build_module_T(const config *cfg, Args&&... args)
{ {
switch (m) { T module;
case Modem::SIM800: if (build_module_T<U>(module, cfg, std::forward<Args>(args)...))
return build_shared_module<SIM800>(cfg, std::forward<Args>(args)...); return module;
case Modem::SIM7600: return nullptr;
return build_shared_module<SIM7600>(cfg, std::forward<Args>(args)...); }
case Modem::BG96:
return build_shared_module<BG96>(cfg, std::forward<Args>(args)...);
case Modem::MinModule:
break;
}
return nullptr;
}
template <typename ...Args> template <typename Dce, typename DcePtr, typename Module, typename ...Args>
std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args) static bool build_generic_DCE(DcePtr &t, const config *cfg, Args&&... args)
{ {
switch (m) { Builder<Module> b(std::forward<Args>(args)...);
case Modem::SIM800: return b.template create<Dce>(t, cfg);
return build_unique<SIM800>(cfg, std::forward<Args>(args)...); }
case Modem::SIM7600:
return build_unique<SIM7600>(cfg, std::forward<Args>(args)...);
case Modem::BG96:
return build_unique<BG96>(cfg, std::forward<Args>(args)...);
case Modem::MinModule:
break;
}
return nullptr;
}
protected:
template <typename Dce, typename DcePtr, typename Module, typename ...Args>
static DcePtr build_generic_DCE(const config *cfg, Args&&... args)
{
DcePtr dce;
if (build_generic_DCE<Dce, DcePtr, Module>(dce, cfg, std::forward<Args>(args)...))
return dce;
return nullptr;
}
private: };
Modem m;
template <typename T, typename U, typename ...Args> } // namespace esp_modem::dce_factory
static bool build_module_T(U &t, const config *cfg, Args&&... args)
{
Builder<T> b(std::forward<Args>(args)...);
return b.create_module(t, cfg);
}
template <typename T, typename U, typename ...Args> #endif // _ESP_MODEM_DCE_FACTORY_HPP_
static T build_module_T(const config *cfg, Args&&... args)
{
T module;
if (build_module_T<U>(module, cfg, std::forward<Args>(args)...))
return module;
return nullptr;
}
template <typename Dce, typename DcePtr, typename Module, typename ...Args>
static bool build_generic_DCE(DcePtr &t, const config *cfg, Args&&... args)
{
Builder<Module> b(std::forward<Args>(args)...);
return b.template create<Dce>(t, cfg);
}
protected:
template <typename Dce, typename DcePtr, typename Module, typename ...Args>
static DcePtr build_generic_DCE(const config *cfg, Args&&... args)
{
DcePtr dce;
if (build_generic_DCE<Dce, DcePtr, Module>(dce, cfg, std::forward<Args>(args)...))
return dce;
return nullptr;
}
};
} // esp_modem
#endif //AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP

View File

@ -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 <memory> #include <memory>
#include <utility> #include <utility>
#include "generate/esp_modem_command_declare.inc" #include "generate/esp_modem_command_declare.inc"
@ -6,8 +22,11 @@
#include "cxx_include/esp_modem_types.hpp" #include "cxx_include/esp_modem_types.hpp"
#include "esp_modem_dce_config.h" #include "esp_modem_dce_config.h"
namespace esp_modem {
enum class command_result; enum class command_result;
class DTE; class DTE;
struct PdpContext;
class GenericModule: public ModuleIf { class GenericModule: public ModuleIf {
public: public:
@ -70,3 +89,8 @@ class BG96: public GenericModule {
public: public:
command_result get_module_name(std::string& name) override; command_result get_module_name(std::string& name) override;
}; };
} // namespace esp_modem
#endif // _ESP_MODEM_DCE_MODULE_

View File

@ -1,81 +1,84 @@
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
#define SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP //
// 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 <memory> #include <memory>
#include <functional>
#include <exception>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <utility> #include <utility>
#include "esp_err.h"
#include "cxx_include/esp_modem_primitives.hpp" #include "cxx_include/esp_modem_primitives.hpp"
#include "cxx_include/esp_modem_terminal.hpp" #include "cxx_include/esp_modem_terminal.hpp"
#include "cxx_include/esp_modem_cmux.hpp" #include "cxx_include/esp_modem_cmux.hpp"
#include "cxx_include/esp_modem_types.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: public:
explicit DTE(std::unique_ptr<Terminal> t); explicit DTE(std::unique_ptr<Terminal> t);
~DTE() = default;
// std::unique_ptr<Terminal> detach() { return std::move(term); } ~DTE() = default;
// void attach(std::unique_ptr<Terminal> 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 write(uint8_t *data, size_t len) { return term->write(data, len); }
int read(uint8_t **d, size_t len) { int read(uint8_t **d, size_t len) {
auto data_to_read = std::min(len, buffer_size); auto data_to_read = std::min(len, buffer_size);
auto data = buffer.get(); auto data = buffer.get();
auto actual_len = term->read(data, data_to_read); auto actual_len = term->read(data, data_to_read);
*d = data; *d = data;
return actual_len; return actual_len;
} }
void set_data_cb(std::function<bool(size_t len)> f)
{
// on_data = std::move(f);
term->set_data_cb(std::move(f));
void set_data_cb(std::function<bool(size_t len)> f) {
term->set_data_cb(std::move(f));
} }
// std::shared_ptr<uint8_t[]> get_buffer() { return buffer;}
void start() { term->start(); } void start() { term->start(); }
void data_mode_closed() { term->stop(); } 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) { if (m == modem_mode::DATA_MODE) {
term->set_data_cb(on_data); term->set_data_cb(on_data);
} else if (m == modem_mode::CMUX_MODE) { } else if (m == modem_mode::CMUX_MODE) {
setup_cmux(); setup_cmux();
} }
} }
command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) override;
// std::shared_ptr<uint8_t[]> 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: private:
Lock lock; Lock lock;
// std::unique_ptr<CMUXedTerminal> cmux;
void setup_cmux(); void setup_cmux();
// void send_sabm(size_t dlci);
// CMUXHelper cmux;
static const size_t GOT_LINE = BIT0; static const size_t GOT_LINE = BIT0;
size_t buffer_size; size_t buffer_size;
size_t consumed; size_t consumed;
// std::shared_ptr<std::vector<uint8_t>> buffer;
std::unique_ptr<uint8_t[]> buffer; std::unique_ptr<uint8_t[]> buffer;
std::unique_ptr<Terminal> term; std::unique_ptr<Terminal> term;
// got_line_cb on_line;
modem_mode mode; modem_mode mode;
signal_group signal; signal_group signal;
std::function<bool(size_t len)> on_data; std::function<bool(size_t len)> on_data;
}; };
} // namespace esp_modem
#endif // _ESP_MODEM_DTE_HPP_
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP //
// 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 "esp_netif.h"
#include "cxx_include/esp_modem_primitives.hpp" #include "cxx_include/esp_modem_primitives.hpp"
namespace esp_modem {
class DTE; class DTE;
class Netif; class Netif;
@ -16,19 +28,25 @@ struct ppp_netif_driver {
Netif *ppp; Netif *ppp;
}; };
class Netif { class Netif {
public: public:
explicit Netif(std::shared_ptr<DTE> e, esp_netif_t *netif); explicit Netif(std::shared_ptr<DTE> e, esp_netif_t *netif);
~Netif();
void start(); 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(); void stop();
private: private:
void receive(uint8_t *data, size_t len); 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_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); static void on_ppp_changed(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
std::shared_ptr<DTE> ppp_dte; std::shared_ptr<DTE> ppp_dte;
@ -37,8 +55,8 @@ private:
signal_group signal; signal_group signal;
static const size_t PPP_STARTED = BIT0; static const size_t PPP_STARTED = BIT0;
static const size_t PPP_EXIT = BIT1; static const size_t PPP_EXIT = BIT1;
}; };
} // namespace esp_modem
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP #endif // _ESP_MODEM_NETIF_HPP

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP //
// 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/FreeRTOS.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
namespace esp_modem {
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
#define THROW(exception) throw(exception) #define THROW(exception) throw(exception)
class esp_err_exception: virtual public std::exception { class esp_err_exception: virtual public std::exception {
@ -114,4 +126,6 @@ struct signal_group {
EventGroupHandle_t event_group; EventGroupHandle_t event_group;
}; };
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP } // namespace esp_modem
#endif // _ESP_MODEM_PRIMITIVES_HPP_

View File

@ -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 #ifndef _ESP_MODEM_TERMINAL_HPP_
#define SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP #define _ESP_MODEM_TERMINAL_HPP_
#include <memory> #include <memory>
#include <functional> #include <functional>
@ -15,6 +24,7 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_modem_primitives.hpp" #include "esp_modem_primitives.hpp"
namespace esp_modem {
enum class terminal_error { enum class terminal_error {
BUFFER_OVERFLOW, BUFFER_OVERFLOW,
@ -25,12 +35,19 @@ enum class terminal_error {
class Terminal { class Terminal {
public: public:
virtual ~Terminal() = default; virtual ~Terminal() = default;
virtual void set_data_cb(std::function<bool(size_t len)> f) { on_data = std::move(f); } virtual void set_data_cb(std::function<bool(size_t len)> f) { on_data = std::move(f); }
void set_error_cb(std::function<void(terminal_error)> f) { on_error = std::move(f); } void set_error_cb(std::function<void(terminal_error)> f) { on_error = std::move(f); }
virtual int write(uint8_t *data, size_t len) = 0; virtual int write(uint8_t *data, size_t len) = 0;
virtual int read(uint8_t *data, size_t len) = 0; virtual int read(uint8_t *data, size_t len) = 0;
virtual void start() = 0; virtual void start() = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual size_t max_virtual_terms() { return 1; } virtual size_t max_virtual_terms() { return 1; }
protected: protected:
@ -38,6 +55,6 @@ protected:
std::function<void(terminal_error)> on_error; std::function<void(terminal_error)> on_error;
}; };
} // namespace esp_modem
#endif // _ESP_MODEM_TERMINAL_HPP_
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP

View File

@ -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 #ifndef _ESP_MODEM_TYPES_HPP_
#define SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP #define _ESP_MODEM_TYPES_HPP_
namespace esp_modem {
enum class modem_mode { enum class modem_mode {
UNDEF, UNDEF,
@ -22,8 +33,10 @@ enum class command_result {
typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb; typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb;
struct PdpContext { struct PdpContext {
explicit PdpContext(std::string& 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) {}
explicit PdpContext(const char *apn) : context_id(1), protocol_type("IP"), apn(apn) {}
size_t context_id; size_t context_id;
std::string protocol_type; std::string protocol_type;
std::string apn; std::string apn;
@ -31,13 +44,16 @@ struct PdpContext {
class CommandableIf { class CommandableIf {
public: 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 { class ModuleIf {
public: public:
virtual bool setup_data_mode() = 0; virtual bool setup_data_mode() = 0;
virtual bool set_mode(modem_mode 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_

View File

@ -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" #include "generate/esp_modem_command_declare.inc"
#ifdef __cplusplus #ifdef __cplusplus
@ -13,6 +23,7 @@ extern "C" {
typedef struct esp_modem_dce_wrap esp_modem_dce_t; typedef struct esp_modem_dce_wrap esp_modem_dce_t;
typedef struct esp_modem_dte_config esp_modem_dte_config_t; typedef struct esp_modem_dte_config esp_modem_dte_config_t;
struct PdpContext;
typedef enum esp_modem_dce_mode typedef enum esp_modem_dce_mode
{ {
ESP_MODEM_MODE_COMMAND, ESP_MODEM_MODE_COMMAND,
@ -38,4 +49,4 @@ DECLARE_ALL_COMMAND_APIS(declares esp_modem_<API>(esp_modem_t * dce, ...);)
#endif #endif
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_API_H #endif // _CLIENT_ESP_MODEM_API_H_

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H //
// 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 "driver/uart.h"
#include "esp_modem_dce_config.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; typedef struct esp_modem_dte_config esp_modem_dte_config_t;
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H #endif // _ESP_MODEM_CONFIG_H_

View File

@ -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 { struct esp_modem_dce_config {
const char* apn; const char* apn;
@ -15,7 +26,4 @@ struct esp_modem_dce_config {
typedef struct esp_modem_dce_config esp_modem_dce_config_t; typedef struct esp_modem_dce_config esp_modem_dce_config_t;
#endif // _ESP_MODEM_DCE_CONFIG_H_
#define AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H
#endif //AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC //
// 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 #ifdef __cplusplus
#include <string> #include <string>
@ -13,8 +23,6 @@
#define BOOL_OUT(x) bool& x #define BOOL_OUT(x) bool& x
#define STRUCT_OUT(struct_name, x) struct_name& x #define STRUCT_OUT(struct_name, x) struct_name& x
#else #else
struct PdpContext;
#define STRING_IN(x) const char* x #define STRING_IN(x) const char* x
#define STRING_OUT(x) char* x #define STRING_OUT(x) char* x
#define BOOL_IN(x) const bool x #define BOOL_IN(x) const bool x
@ -22,7 +30,7 @@ struct PdpContext;
#define STRUCT_OUT(struct_name, x) struct struct_name* x #define STRUCT_OUT(struct_name, x) struct struct_name* x
#endif #endif
#define _ESP_MODEM_COMMAND_DECLARE_INC_
#define ESP_MODEM_DEFINE_DCE_COMMAND(...) ESP_MODEM_DECLARE_DCE_COMMAND(##__VA_ARGS__) #define ESP_MODEM_DEFINE_DCE_COMMAND(...) ESP_MODEM_DECLARE_DCE_COMMAND(##__VA_ARGS__)
#define DEFINE_ALL_COMMAND_APIS() DECLARE_ALL_COMMAND_APIS() #define DEFINE_ALL_COMMAND_APIS() DECLARE_ALL_COMMAND_APIS()
@ -79,4 +87,4 @@ DECLARE_ALL_COMMAND_APIS()
#endif #endif
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC #endif // _ESP_MODEM_COMMAND_DECLARE_INC_

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define MODEM_CONSOLE_EXCEPTION_STUB_HPP //
// 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 #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
#define TRY_CATCH_RET_NULL(block) \ #define TRY_CATCH_RET_NULL(block) \
@ -23,4 +33,4 @@
#endif #endif
#endif //MODEM_CONSOLE_EXCEPTION_STUB_HPP #endif // _EXCEPTION_STUB_HPP_

View File

@ -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 // http://www.apache.org/licenses/LICENSE-2.0
#define SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP //
// 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" #include "cxx_include/esp_modem_dte.hpp"
struct esp_modem_dte_config; struct esp_modem_dte_config;
namespace esp_modem {
std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *config); std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *config);
} // namespace esp_modem
#endif // _UART_TERMINAL_HPP_
#endif //SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP

View File

@ -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 "cxx_include/esp_modem_dte.hpp"
#include "uart_terminal.hpp" #include "uart_terminal.hpp"
#include "esp_log.h" #include "esp_log.h"
@ -10,69 +21,46 @@
#include "esp_modem_config.h" #include "esp_modem_config.h"
#include "exception_stub.hpp" #include "exception_stub.hpp"
namespace esp_modem {
struct PdpContext; struct PdpContext;
using namespace esp_modem;
static const char *TAG = "modem_api"; static const char *TAG = "modem_api";
std::shared_ptr<DTE> create_uart_dte(const esp_modem_dte_config *config) std::shared_ptr<DTE> create_uart_dte(const dte_config *config) {
{
TRY_CATCH_RET_NULL( TRY_CATCH_RET_NULL(
auto term = create_uart_terminal(config); auto term = create_uart_terminal(config);
return std::make_shared<DTE>(std::move(term)); return std::make_shared<DTE>(std::move(term));
) )
} }
template<typename SpecificModule> static inline std::unique_ptr<DCE>
std::unique_ptr<DCE_T<SpecificModule>> create_dce(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SpecificModule>& dev, esp_netif_t *netif) create_modem_dce(dce_factory::Modem m, const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) {
{
TRY_CATCH_RET_NULL(
return std::make_unique<DCE_T<SpecificModule>>(dte, dev, netif);
)
}
std::unique_ptr<DCE> create_generic_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<GenericModule>& dev, esp_netif_t *netif)
{
TRY_CATCH_RET_NULL(
return std::make_unique<DCE>(dte, dev, netif);
)
}
//std::unique_ptr<DCE> create_SIM7600_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
//{
// auto pdp = std::make_unique<PdpContext>(apn);
// auto dev = std::make_shared<SIM7600>(dte, std::move(pdp));
//
// return create_generic_dce_from_module(dte, std::move(dev), netif);
//}
static inline std::unique_ptr<DCE> create_modem_dce(dce_factory::Modem m, const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif)
{
dce_factory::Factory f(m); dce_factory::Factory f(m);
TRY_CATCH_RET_NULL( 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<DCE> create_SIM7600_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) std::unique_ptr<DCE> create_SIM7600_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) {
{
return create_modem_dce(dce_factory::Modem::SIM7600, config, std::move(dte), netif); return create_modem_dce(dce_factory::Modem::SIM7600, config, std::move(dte), netif);
} }
std::unique_ptr<DCE> create_SIM800_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) std::unique_ptr<DCE> create_SIM800_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) {
{
return create_modem_dce(dce_factory::Modem::SIM800, config, std::move(dte), netif); return create_modem_dce(dce_factory::Modem::SIM800, config, std::move(dte), netif);
} }
std::unique_ptr<DCE> create_BG96_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) std::unique_ptr<DCE> create_BG96_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) {
{
return create_modem_dce(dce_factory::Modem::BG96, config, std::move(dte), netif); return create_modem_dce(dce_factory::Modem::BG96, config, std::move(dte), netif);
} }
} // namespace esp_modem
// //
// C API definitions // 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 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)); 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

View File

@ -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 <cstring> #include <cstring>
#include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dte.hpp"
#include "esp_log.h" #include "esp_log.h"
using namespace esp_modem;
/* CRC8 is the reflected CRC8/ROHC algorithm */ /* CRC8 is the reflected CRC8/ROHC algorithm */
#define FCS_POLYNOMIAL 0xe0 /* reversed crc8 */ #define FCS_POLYNOMIAL 0xe0 /* reversed crc8 */
#define FCS_INIT_VALUE 0xFF #define FCS_INIT_VALUE 0xFF

View File

@ -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_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp" #include "cxx_include/esp_modem_dce_module.hpp"

View File

@ -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_dte.hpp"
#include "cxx_include/esp_modem_dce.hpp" #include "cxx_include/esp_modem_dce.hpp"
#include "esp_log.h" #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) { switch (m) {
case modem_mode::UNDEF: case modem_mode::UNDEF:
@ -50,11 +60,9 @@ bool Modes::set(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m)
return false; return false;
} }
modem_mode Modes::get() modem_mode DCE_Mode::get()
{ {
return mode; return mode;
} }
} } // esp_modem

View File

@ -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 "cxx_include/esp_modem_dte.hpp"
#include <string.h> #include <string.h>
#include "esp_log.h" #include "esp_log.h"
using namespace esp_modem;
const int DTE_BUFFER_SIZE = 1024;
DTE::DTE(std::unique_ptr<Terminal> terminal): DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer_size(DTE_BUFFER_SIZE), consumed(0), buffer_size(DTE_BUFFER_SIZE), consumed(0),
buffer(std::make_unique<uint8_t[]>(buffer_size)), buffer(std::make_unique<uint8_t[]>(buffer_size)),

View File

@ -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 <functional> #include <functional>
#include "cxx_include/esp_modem_types.hpp" #include "cxx_include/esp_modem_types.hpp"
#include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp" #include "cxx_include/esp_modem_dce_module.hpp"
namespace esp_modem {
template<typename T> template<typename T>
std::shared_ptr<T> create_device(const std::shared_ptr<DTE>& dte, std::string &apn) std::shared_ptr<T> create_device(const std::shared_ptr<DTE>& dte, std::string &apn)
{ {
@ -20,6 +36,7 @@ std::shared_ptr<SIM7600> create_SIM7600_module(const std::shared_ptr<DTE>& dte,
return create_device<SIM7600>(dte, apn); return create_device<SIM7600>(dte, apn);
} }
}
#include "cxx_include/esp_modem_api.hpp" #include "cxx_include/esp_modem_api.hpp"
#include "cxx_include/esp_modem_dce_factory.hpp" #include "cxx_include/esp_modem_dce_factory.hpp"

View File

@ -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 "cxx_include/esp_modem_dce_module.hpp"
#include "generate/esp_modem_command_declare.inc" #include "generate/esp_modem_command_declare.inc"
GenericModule::GenericModule(std::shared_ptr<DTE> dte, const esp_modem_dce_config *config): namespace esp_modem {
GenericModule::GenericModule(std::shared_ptr<DTE> dte, const dce_config *config) :
dte(std::move(dte)), pdp(std::make_unique<PdpContext>(config->apn)) {} dte(std::move(dte)), pdp(std::make_unique<PdpContext>(config->apn)) {}
@ -17,26 +30,28 @@ GenericModule::GenericModule(std::shared_ptr<DTE> dte, const esp_modem_dce_confi
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, arg_nr, MUX_ARG, ...) \ #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) ); } 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 #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"; name = "7600";
return command_result::OK; return command_result::OK;
} }
command_result SIM800::get_module_name(std::string& name) command_result SIM800::get_module_name(std::string &name) {
{
name = "800L"; name = "800L";
return command_result::OK; return command_result::OK;
} }
command_result BG96::get_module_name(std::string& name) command_result BG96::get_module_name(std::string &name) {
{
name = "BG96"; name = "BG96";
return command_result::OK; return command_result::OK;
} }
}

View File

@ -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 <memory> #include <memory>
#include <utility> #include <utility>
#include <esp_log.h> #include <esp_log.h>
@ -9,45 +20,35 @@
#include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dte.hpp"
#include "esp_netif_ppp.h" #include "esp_netif_ppp.h"
#include <iostream>
namespace esp_modem {
void Netif::on_ppp_changed(void *arg, esp_event_base_t event_base, void Netif::on_ppp_changed(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) int32_t event_id, void *event_data) {
{ auto *ppp = static_cast<Netif *>(arg);
Netif *ppp = (Netif*)arg;
// DTE *e = (DTE*)arg;
std::cout << "on_ppp_changed " << std::endl;
ESP_LOGW("TAG", "PPP state changed event %d", event_id); ESP_LOGW("TAG", "PPP state changed event %d", event_id);
if (event_id < NETIF_PP_PHASE_OFFSET) { if (event_id < NETIF_PP_PHASE_OFFSET) {
ESP_LOGI("TAG", "PPP state changed event %d", event_id); ESP_LOGI("TAG", "PPP state changed event %d", event_id);
// only notify the modem on state/error events, ignoring phase transitions // only notify the modem on state/error events, ignoring phase transitions
ppp->signal.set(PPP_EXIT); 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) esp_err_t Netif::esp_modem_dte_transmit(void *h, void *buffer, size_t len) {
{ auto *ppp = static_cast<Netif *>(h);
Netif *ppp = (Netif*)h;
if (ppp->signal.is_any(PPP_STARTED)) { 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_OK;
} }
} }
return ESP_FAIL; return ESP_FAIL;
} }
esp_err_t Netif::esp_modem_post_attach(esp_netif_t * esp_netif, void * args) esp_err_t Netif::esp_modem_post_attach(esp_netif_t *esp_netif, void *args) {
{ auto d = (ppp_netif_driver *) args;
auto d = (ppp_netif_driver*)args; esp_netif_driver_ifconfig_t driver_ifconfig = {};
esp_netif_driver_ifconfig_t driver_ifconfig = { };
driver_ifconfig.transmit = Netif::esp_modem_dte_transmit; driver_ifconfig.transmit = Netif::esp_modem_dte_transmit;
driver_ifconfig.handle = (void*)d->ppp; driver_ifconfig.handle = (void *) d->ppp;
std::cout << "esp_modem_post_attach " << std::endl;
d->base.netif = esp_netif; d->base.netif = esp_netif;
ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig)); 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 // 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_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; 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)) { if (signal.is_any(PPP_STARTED)) {
// std::cout << "received data " << len << std::endl;
esp_netif_receive(driver.base.netif, data, len, nullptr); esp_netif_receive(driver.base.netif, data, len, nullptr);
} }
} }
Netif::Netif(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif): Netif::Netif(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif) :
ppp_dte(std::move(e)), netif(ppp_netif) ppp_dte(std::move(e)), netif(ppp_netif) {
{
driver.base.netif = ppp_netif; driver.base.netif = ppp_netif;
driver.ppp = this; driver.ppp = this;
driver.base.post_attach = esp_modem_post_attach; 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_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)); 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 { ppp_dte->set_data_cb([this](size_t len) -> bool {
uint8_t *data; uint8_t *data;
auto actual_len = ppp_dte->read(&data, len); auto actual_len = ppp_dte->read(&data, len);
@ -95,10 +92,15 @@ void Netif::start()
signal.set(PPP_STARTED); signal.set(PPP_STARTED);
} }
void Netif::stop() void Netif::stop() {
{
std::cout << "esp_netif_action_stop " << std::endl;
esp_netif_action_stop(driver.base.netif, nullptr, 0, nullptr); esp_netif_action_stop(driver.base.netif, nullptr, 0, nullptr);
signal.clear(PPP_STARTED); signal.clear(PPP_STARTED);
}
} 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

View File

@ -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 "cxx_include/esp_modem_dte.hpp"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
@ -13,17 +26,18 @@
static const char *TAG = "uart_terminal"; static const char *TAG = "uart_terminal";
namespace esp_modem {
struct uart_resource { struct uart_resource {
explicit uart_resource(const esp_modem_dte_config *config); explicit uart_resource(const esp_modem_dte_config *config);
~uart_resource(); ~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)); return xQueueReceive(event_queue, &event, pdMS_TO_TICKS(time_ms));
} }
void reset_events()
{ void reset_events() {
uart_flush_input(port); uart_flush_input(port);
xQueueReset(event_queue); xQueueReset(event_queue);
} }
@ -35,14 +49,13 @@ struct uart_resource {
}; };
struct uart_task { struct uart_task {
explicit uart_task(size_t stack_size, size_t priority, void* task_param, TaskFunction_t task_function): explicit uart_task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t task_function) :
task_handle(nullptr) task_handle(nullptr) {
{
BaseType_t ret = xTaskCreate(task_function, "uart_task", 10000, task_param, priority, &task_handle); BaseType_t ret = xTaskCreate(task_function, "uart_task", 10000, task_param, priority, &task_handle);
throw_if_false(ret == pdTRUE, "create uart event task failed"); throw_if_false(ret == pdTRUE, "create uart event task failed");
} }
~uart_task()
{ ~uart_task() {
if (task_handle) vTaskDelete(task_handle); if (task_handle) vTaskDelete(task_handle);
} }
@ -51,13 +64,13 @@ struct uart_task {
struct uart_event_loop { 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 = {}; esp_event_loop_args_t loop_args = {};
loop_args.queue_size = ESP_MODEM_EVENT_QUEUE_SIZE; loop_args.queue_size = ESP_MODEM_EVENT_QUEUE_SIZE;
loop_args.task_name = nullptr; loop_args.task_name = nullptr;
throw_if_esp_fail(esp_event_loop_create(&loop_args, &event_loop_hdl), "create event loop failed"); 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)); } 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); } ~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; 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) { if (port >= UART_NUM_0 && port < UART_NUM_MAX) {
uart_driver_delete(port); uart_driver_delete(port);
} }
} }
uart_resource::uart_resource(const esp_modem_dte_config *config): uart_resource::uart_resource(const esp_modem_dte_config *config) :
port(-1) port(-1) {
{
esp_err_t res; esp_err_t res;
line_buffer_size = config->line_buffer_size; 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; port = config->port_num;
} }
class uart_terminal: public Terminal { class uart_terminal : public Terminal {
public: public:
explicit uart_terminal(const esp_modem_dte_config *config): explicit uart_terminal(const esp_modem_dte_config *config) :
uart(config), event_loop(), signal(), uart(config), event_loop(), signal(),
task_handle(config->event_task_stack_size, config->event_task_priority, this, s_task) {} task_handle(config->event_task_stack_size, config->event_task_priority, this, s_task) {}
~uart_terminal() override = default; ~uart_terminal() override = default;
void start() override
{ void start() override {
signal.set(TASK_START); signal.set(TASK_START);
} }
void stop() override
{ void stop() override {
signal.set(TASK_STOP); signal.set(TASK_STOP);
} }
int write(uint8_t *data, size_t len) override; int write(uint8_t *data, size_t len) override;
int read(uint8_t *data, size_t len) override; int read(uint8_t *data, size_t len) override;
void set_data_cb(std::function<bool(size_t len)> f) override
{ void set_data_cb(std::function<bool(size_t len)> f) override {
on_data = std::move(f); on_data = std::move(f);
signal.set(TASK_PARAMS); signal.set(TASK_PARAMS);
} }
private: private:
static void s_task(void * task_param) static void s_task(void *task_param) {
{ auto t = static_cast<uart_terminal *>(task_param);
auto t = static_cast<uart_terminal*>(task_param);
t->task(); t->task();
vTaskDelete(NULL); vTaskDelete(NULL);
} }
void task(); void task();
static const size_t TASK_INIT = BIT0; static const size_t TASK_INIT = BIT0;
@ -163,17 +176,15 @@ private:
}; };
std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *config) std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *config) {
{
TRY_CATCH_RET_NULL( TRY_CATCH_RET_NULL(
auto term = std::make_unique<uart_terminal>(config); auto term = std::make_unique<uart_terminal>(config);
term->start(); term->start();
return term; return term;
) )
} }
void uart_terminal::task() void uart_terminal::task() {
{
std::function<bool(size_t len)> on_data_priv = nullptr; std::function<bool(size_t len)> on_data_priv = nullptr;
uart_event_t event; uart_event_t event;
size_t len; size_t len;
@ -182,7 +193,7 @@ void uart_terminal::task()
if (signal.is_any(TASK_STOP)) { if (signal.is_any(TASK_STOP)) {
return; // exits to the static method where the task gets deleted 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(); event_loop.run();
if (uart.get_event(event, 100)) { if (uart.get_event(event, 100)) {
if (signal.is_any(TASK_PARAMS)) { 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; size_t length = 0;
uart_get_buffered_data_len(uart.port, &length); uart_get_buffered_data_len(uart.port, &length);
if (length > 0) { if (length > 0) {
@ -239,10 +249,9 @@ int uart_terminal::read(uint8_t *data, size_t len)
return 0; 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); return uart_write_bytes(uart.port, data, len);
} }
} // namespace esp_modem