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;
using namespace esp_modem;
//template<typename T, typename U> constexpr size_t offset_of(U T::*member)
//{
// 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 "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<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);
std::unique_ptr<DCE> create_BG96_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_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
#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<Terminal> t, std::unique_ptr<uint8_t[]> b, size_t buff_size):
@ -38,8 +68,12 @@ private:
size_t consumed;
std::unique_ptr<uint8_t[]> buffer;
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
#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_

View File

@ -1,16 +1,30 @@
#pragma once
#include <utility>
// 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 <utility>
#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 SpecificModule>
class DCE_T {
@ -51,13 +65,10 @@ protected:
std::shared_ptr<DTE> dte;
std::shared_ptr<SpecificModule> module;
Netif netif;
esp_modem::dce_factory::Modes mode;
DCE_Mode mode;
};
//typedef DCE_T<GenericModule> DCE;
//
//#if 0
class DCE: public DCE_T<GenericModule> {
public:
@ -74,4 +85,7 @@ public:
#undef ESP_MODEM_DECLARE_DCE_COMMAND
};
//#endif
} // esp_modem
#endif // _ESP_MODEM_DCE_HPP_

View File

@ -1,16 +1,26 @@
// 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:
class FactoryHelper {
public:
static std::unique_ptr<PdpContext> create_pdp_context(std::string &apn);
template <typename T, typename ...Args>
@ -34,11 +44,11 @@ using config = esp_modem_dce_config;
return true;
}
};
};
template<typename T>
class Builder {
public:
template<typename T>
class Builder {
public:
explicit Builder(std::shared_ptr<DTE> dte): dte(std::move(dte))
{
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP();
@ -81,21 +91,21 @@ using config = esp_modem_dce_config;
return FactoryHelper::make<DceT>(t, std::move(dte), std::move(module), netif);
}
private:
private:
std::shared_ptr<DTE> dte;
std::shared_ptr<T> module;
esp_netif_t *netif;
};
};
enum class Modem {
enum class Modem {
SIM800,
SIM7600,
BG96,
MinModule
};
};
class Factory {
public:
class Factory {
public:
explicit Factory(Modem modem): m(modem) {}
template <typename T, typename ...Args>
@ -151,7 +161,7 @@ using config = esp_modem_dce_config;
}
private:
private:
Modem m;
template <typename T, typename U, typename ...Args>
@ -177,7 +187,7 @@ using config = esp_modem_dce_config;
return b.template create<Dce>(t, cfg);
}
protected:
protected:
template <typename Dce, typename DcePtr, typename Module, typename ...Args>
static DcePtr build_generic_DCE(const config *cfg, Args&&... args)
{
@ -187,8 +197,8 @@ using config = esp_modem_dce_config;
return nullptr;
}
};
};
} // esp_modem
} // namespace esp_modem::dce_factory
#endif //AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP
#endif // _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 <utility>
#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_

View File

@ -1,30 +1,39 @@
#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 <memory>
#include <functional>
#include <exception>
#include <cstddef>
#include <cstdint>
#include <utility>
#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<Terminal> t);
~DTE() = default;
// std::unique_ptr<Terminal> detach() { return std::move(term); }
// 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 read(uint8_t **d, size_t len) {
auto data_to_read = std::min(len, buffer_size);
auto data = buffer.get();
@ -32,50 +41,44 @@ public:
*d = data;
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 data_mode_closed() { term->stop(); }
void set_mode(modem_mode m) {
term->start(); 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<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:
Lock lock;
// std::unique_ptr<CMUXedTerminal> 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<std::vector<uint8_t>> buffer;
std::unique_ptr<uint8_t[]> buffer;
std::unique_ptr<Terminal> term;
// got_line_cb on_line;
modem_mode mode;
signal_group signal;
std::function<bool(size_t len)> on_data;
};
} // namespace esp_modem
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
#endif // _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
#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<DTE> 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<DTE> 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

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
#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_

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
#define SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP
#ifndef _ESP_MODEM_TERMINAL_HPP_
#define _ESP_MODEM_TERMINAL_HPP_
#include <memory>
#include <functional>
@ -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<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); }
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<void(terminal_error)> on_error;
};
} // namespace esp_modem
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP
#endif // _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
#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<command_result(uint8_t *data, size_t len)> 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_

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"
#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_<API>(esp_modem_t * dce, ...);)
#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
#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_

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 {
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_

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
#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 <string>
@ -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_

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
#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_

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
#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<Terminal> create_uart_terminal(const esp_modem_dte_config *config);
} // namespace esp_modem
#endif //SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP
#endif // _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 "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<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(
auto term = create_uart_terminal(config);
return std::make_shared<DTE>(std::move(term));
)
}
template<typename SpecificModule>
std::unique_ptr<DCE_T<SpecificModule>> create_dce(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SpecificModule>& dev, 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)
{
static inline std::unique_ptr<DCE>
create_modem_dce(dce_factory::Modem m, const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) {
dce_factory::Factory f(m);
TRY_CATCH_RET_NULL(
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);
}
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);
}
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);
}
} // 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

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 "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

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_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_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

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 <string.h>
#include "esp_log.h"
using namespace esp_modem;
const int DTE_BUFFER_SIZE = 1024;
DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer_size(DTE_BUFFER_SIZE), consumed(0),
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 "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<typename T>
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);
}
}
#include "cxx_include/esp_modem_api.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 "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)) {}
@ -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, ...) \
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;
}
}

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 <utility>
#include <esp_log.h>
@ -9,45 +20,35 @@
#include "cxx_include/esp_modem_dte.hpp"
#include "esp_netif_ppp.h"
#include <iostream>
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<Netif *>(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<Netif *>(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<DTE> e, esp_netif_t *ppp_netif):
ppp_dte(std::move(e)), netif(ppp_netif)
{
Netif::Netif(std::shared_ptr<DTE> 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);
}
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 "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<bool(size_t len)> f) override
{
void set_data_cb(std::function<bool(size_t len)> f) override {
on_data = std::move(f);
signal.set(TASK_PARAMS);
}
private:
static void s_task(void * task_param)
{
auto t = static_cast<uart_terminal*>(task_param);
static void s_task(void *task_param) {
auto t = static_cast<uart_terminal *>(task_param);
t->task();
vTaskDelete(NULL);
}
void task();
static const size_t TASK_INIT = BIT0;
@ -163,8 +176,7 @@ 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(
auto term = std::make_unique<uart_terminal>(config);
term->start();
@ -172,8 +184,7 @@ std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *confi
)
}
void uart_terminal::task()
{
void uart_terminal::task() {
std::function<bool(size_t len)> 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