Implement perfect forwarding for command lib

This commit is contained in:
David Cermak
2021-03-07 19:43:45 +01:00
parent 031bbc14ff
commit 904cf94648
29 changed files with 586 additions and 338 deletions

View File

@ -9,12 +9,12 @@ set(srcs "src/esp_modem.c"
"src/esp_sim7600.c"
"src/esp_bg96.c"
"src/esp_modem_dte.cpp"
"src/ppp_netif.cpp"
"src/esp_modem_netif.cpp"
"src/esp_modem_api.cpp"
"src/esp_modem_dce.cpp"
"src/esp_modem_device.cpp"
"src/esp_modem_device_factory.cpp"
"src/esp_modem_factory.cpp"
"src/esp_modem_cmux.cpp"
"src/esp_modem_command_library.cpp"
"src/esp_modem_modules.cpp"
"src/esp_modem_uart.cpp")
set(include_dirs "include")

View File

@ -16,7 +16,7 @@
#include "esp_modem_netif.h"
#include "esp_log.h"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/uart_terminal.hpp"
#include "cxx_include/esp_modem_api.hpp"
#define BROKER_URL "mqtt://mqtt.eclipseprojects.io"
@ -266,7 +266,7 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
uint8_t data[32] = {};
int actual_len = 0;
auto uart_dte = create_uart_dte(dte_config);
uart_dte->set_mode(dte_mode::UNDEF);
uart_dte->set_mode(modem_mode::UNDEF);
uart_dte->command("+++", [&](uint8_t *data, size_t len) {
std::string response((char*)data, len);
ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data);
@ -293,14 +293,15 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
assert(esp_netif);
std::string apn = "internet";
auto device = create_device(uart_dte, apn);
bool pin_ok = true;
if (device->read_pin(pin_ok) == command_result::OK && !pin_ok) {
throw_if_false(device->set_pin("1234") == command_result::OK, "Cannot set PIN!");
}
// auto device = create_generic_module(uart_dte, apn);
auto device = create_SIM7600_module(uart_dte, apn);
// bool pin_ok = true;
// if (device->read_pin(pin_ok) == command_result::OK && !pin_ok) {
// throw_if_false(device->set_pin("1234") == command_result::OK, "Cannot set PIN!");
// }
//
// std::string number;
std::string number;
// std::cout << "----" << std::endl;
// device->get_imsi(number);
// std::cout << "----" << std::endl;
@ -312,7 +313,14 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
// device->get_module_name(number);
// std::cout << "|" << number << "|" << std::endl;
// std::cout << "----" << std::endl;
auto my_dce = create_dce(uart_dte, device, esp_netif);
// auto my_dce = create_generic_module_dce(uart_dte, device, esp_netif);
auto my_dce = create_SIM7600_dce_from_module(uart_dte, device, esp_netif);
my_dce->get_module_name(number);
std::cout << "|" << number << "|" << std::endl;
bool pin_ok = true;
if (my_dce->read_pin(pin_ok) == command_result::OK && !pin_ok) {
throw_if_false(my_dce->set_pin("1234") == command_result::OK, "Cannot set PIN!");
}
// return;
// my_dce->set_cmux();
@ -326,6 +334,9 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
// uart_dte->send_cmux_command(2, "AT+CPIN?");
// return;
my_dce->get_module_name(number);
my_dce->set_data_mode();
my_dce->command("AT+CPIN?\r", [&](uint8_t *data, size_t len) {
std::string response((char*)data, len);
ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data);

View File

@ -2,13 +2,24 @@
#include <memory>
#include "cxx_include/esp_modem_dce.hpp"
#include "cxx_include/esp_modem_device_factory.hpp"
#include "cxx_include/esp_modem_dce_module.hpp"
class DTE;
class GenericModule;
class SIM7600;
class SIM800;
class BG96;
struct dte_config;
typedef struct esp_netif_obj esp_netif_t;
std::shared_ptr<DTE> create_uart_dte(const dte_config *config);
std::unique_ptr<DCE> create_dce(const std::shared_ptr<DTE>& dte, const std::shared_ptr<DeviceIf>& dev, esp_netif_t *netif);
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);
std::unique_ptr<DCE<GenericModule>> 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<SIM7600>> create_SIM7600_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SIM7600>& dev, esp_netif_t *netif);
std::unique_ptr<DCE<SIM7600>> create_SIM7600_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn);

View File

@ -0,0 +1,45 @@
//
// Created by david on 3/8/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP
enum class cmux_state {
INIT,
HEADER,
PAYLOAD,
FOOTER,
RECOVER,
};
class CMUXedTerminal: public Terminal {
public:
explicit CMUXedTerminal(std::unique_ptr<Terminal> t, std::unique_ptr<uint8_t[]> b, size_t buff_size):
term(std::move(t)), buffer(std::move(b)) {}
~CMUXedTerminal() override = default;
void setup_cmux();
void set_data_cb(std::function<bool(size_t len)> f) override {}
int write(uint8_t *data, size_t len) override;
int read(uint8_t *data, size_t len) override { return term->read(data, len); }
void start() override;
void stop() override {}
private:
static bool process_cmux_recv(size_t len);
void send_sabm(size_t i);
std::unique_ptr<Terminal> term;
cmux_state state;
uint8_t dlci;
uint8_t type;
size_t payload_len;
uint8_t frame_header[6];
size_t frame_header_offset;
size_t buffer_size;
size_t consumed;
std::unique_ptr<uint8_t[]> buffer;
bool on_cmux(size_t len);
};
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP

View File

@ -0,0 +1,27 @@
//
// Created by david on 3/2/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP
#include "esp_modem_dte.hpp"
#include "esp_modem_dce_module.hpp"
#include "esp_modem_types.hpp"
#include "generate/esp_modem_command_declare.inc"
namespace esp_modem {
namespace dce_commands {
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
return_type name(CommandableIf *t, ## __VA_ARGS__);
DECLARE_ALL_COMMAND_APIS(forwards name(...) { device->name(...); } )
#undef ESP_MODEM_DECLARE_DCE_COMMAND
} // dce_commands
} // esp_modem
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP

View File

@ -1,28 +1,65 @@
#pragma once
#include "cxx_include/esp_modem_dce_commands_if.hpp"
#include "cxx_include/esp_modem_netif.hpp"
#include "generate/esp_modem_command_declare.inc"
template<class SpecificModule>
class DCE {
static_assert(std::is_base_of<ModuleIf, SpecificModule>::value, "DCE must be instantiated with Module class only");
public:
explicit DCE(const std::shared_ptr<DTE>& d, std::shared_ptr<DeviceIf> device, esp_netif_t * netif);
explicit DCE(const std::shared_ptr<DTE>& dte, std::shared_ptr<SpecificModule> device, esp_netif_t * netif):
dce_dte(dte), device(std::move(device)), ppp_netif(dte, netif)
{ }
void set_data() {
// command("AT+CGDCONT=1,\"IP\",\"internet\"\r", [&](uint8_t *data, size_t len) {
// return command_result::OK;
// }, 1000);
// command("ATD*99***1#\r", [&](uint8_t *data, size_t len) {
// return command_result::OK;
// }, 1000);
device->setup_data_mode();
device->set_mode(dte_mode::DATA_MODE);
dce_dte->set_mode(dte_mode::DATA_MODE);
device->set_mode(modem_mode::DATA_MODE);
dce_dte->set_mode(modem_mode::DATA_MODE);
ppp_netif.start();
}
void exit_data();
void exit_data() {
ppp_netif.stop();
device->set_mode(modem_mode::COMMAND_MODE);
// uint8_t* data;
dce_dte->set_data_cb([&](size_t len) -> bool {
// auto actual_len = dce_dte->read(&data, 64);
// ESP_LOG_BUFFER_HEXDUMP("esp-modem: debug_data", data, actual_len, ESP_LOG_INFO);
return false;
});
ppp_netif.wait_until_ppp_exits();
dce_dte->set_data_cb(nullptr);
dce_dte->set_mode(modem_mode::COMMAND_MODE);
}
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
template <typename ...Agrs> \
return_type name(Agrs&&... args) \
{ \
return device->name(std::forward<Agrs>(args)...); \
}
DECLARE_ALL_COMMAND_APIS(forwards name(...) { device->name(...); } )
#undef ESP_MODEM_DECLARE_DCE_COMMAND
// template <typename ...Params>
// command_result get_module_name(Params&&... params)
// {
// return device->get_module_name(std::forward<Params>(params)...);
// }
// template <typename ...Params>
// command_result set_data_mode(Params&&... params)
// {
// return device->set_data_mode(std::forward<Params>(params)...);
// }
command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) {
return dce_dte->command(command, got_line, time_ms);
}
void set_cmux();
void set_cmux() { device->set_mode(modem_mode::CMUX_MODE);
dce_dte->set_mode(modem_mode::CMUX_MODE); }
private:
std::shared_ptr<DTE> dce_dte;
std::shared_ptr<DeviceIf> device;
std::shared_ptr<SpecificModule> device;
PPP ppp_netif;
};

View File

@ -1,40 +0,0 @@
#pragma once
#include "cxx_include/esp_modem_dce_commands_if.hpp"
#include <memory>
#include <utility>
struct PdpContext {
PdpContext(std::string& apn): context_id(1), protocol_type("IP"), apn(apn) {}
size_t context_id;
std::string protocol_type;
std::string apn;
};
enum class command_result;
class DTE;
class Device: public DeviceIf {
public:
explicit Device(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp):
dte(std::move(dte)), pdp(std::move(pdp)) {}
bool setup_data_mode() override;
bool set_mode(dte_mode mode) override;
command_result set_echo(bool on);
command_result set_data_mode();
command_result resume_data_mode();
command_result set_pdp_context(PdpContext& pdp_context);
command_result set_command_mode();
command_result set_cmux();
command_result get_imsi(std::string& imsi_number);
command_result set_pin(const std::string& pin);
command_result read_pin(bool& pin_ok);
command_result get_imei(std::string& imei);
command_result get_module_name(std::string& imei);
private:
std::shared_ptr<DTE> dte;
std::unique_ptr<PdpContext> pdp;
};

View File

@ -1,9 +0,0 @@
#pragma once
enum class dte_mode;
class DeviceIf {
public:
virtual bool setup_data_mode() = 0;
virtual bool set_mode(dte_mode mode) = 0;
};

View File

@ -0,0 +1,70 @@
#pragma once
#include <memory>
#include <utility>
#include "generate/esp_modem_command_declare.inc"
#include "cxx_include/esp_modem_command_library.hpp"
#include "cxx_include/esp_modem_types.hpp"
enum class command_result;
class DTE;
class GenericModule: public ModuleIf {
public:
explicit GenericModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp):
dte(std::move(dte)), pdp(std::move(pdp)) {}
bool setup_data_mode() override
{
if (set_echo(false) != command_result::OK)
return false;
if (set_pdp_context(*pdp) != command_result::OK)
return false;
return true;
}
bool set_mode(modem_mode mode) override
{
if (mode == modem_mode::DATA_MODE) {
if (set_data_mode() != command_result::OK)
return resume_data_mode() == command_result::OK;
return true;
} else if (mode == modem_mode::COMMAND_MODE) {
return set_command_mode() == command_result::OK;
} else if (mode == modem_mode::CMUX_MODE) {
return set_cmux() == command_result::OK;
}
return true;
}
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
template <typename ...Agrs> \
return_type name(Agrs&&... args) { return esp_modem::dce_commands::name(dte.get(), std::forward<Agrs>(args)...); }
DECLARE_ALL_COMMAND_APIS(return_type name(...); )
#undef ESP_MODEM_DECLARE_DCE_COMMAND
protected:
std::shared_ptr<DTE> dte;
std::unique_ptr<PdpContext> pdp;
};
// Definitions of other modules
class SIM7600: public GenericModule {
using GenericModule::GenericModule;
public:
command_result get_module_name(std::string& name);
};
class SIM800: public GenericModule {
using GenericModule::GenericModule;
public:
command_result get_module_name(std::string& name);
};
class BG96: public GenericModule {
public:
command_result get_module_name(std::string& name);
};

View File

@ -1,5 +0,0 @@
#pragma once
#include "cxx_include/esp_modem_dce_commands.hpp"
//std::shared_ptr<DeviceIf> create_device(const std::shared_ptr<DTE>& dte, std::string &apn);
std::shared_ptr<Device> create_device(const std::shared_ptr<DTE>& dte, std::string &apn);

View File

@ -1,6 +1,5 @@
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
#include <memory>
#include <functional>
#include <exception>
@ -8,94 +7,16 @@
#include <cstdint>
#include <utility>
#include "esp_err.h"
#include "terminal_objects.hpp"
#include "ppp_netif.hpp"
#include <array>
#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"
enum class terminal_error {
BUFFER_OVERFLOW,
CHECKSUM_ERROR,
UNEXPECTED_CONTROL_FLOW,
};
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;
protected:
std::function<bool(size_t len)> on_data;
std::function<void(terminal_error)> on_error;
};
enum class cmux_state {
INIT,
HEADER,
PAYLOAD,
FOOTER,
RECOVER,
};
class CMUXedTerminal: public Terminal {
public:
explicit CMUXedTerminal(std::unique_ptr<Terminal> t, std::unique_ptr<uint8_t[]> b, size_t buff_size):
term(std::move(t)), buffer(std::move(b)) {}
~CMUXedTerminal() override = default;
void setup_cmux();
void set_data_cb(std::function<bool(size_t len)> f) override {}
int write(uint8_t *data, size_t len) override;
int read(uint8_t *data, size_t len) override { return term->read(data, len); }
void start() override;
void stop() override {}
private:
static bool process_cmux_recv(size_t len);
void send_sabm(size_t i);
std::unique_ptr<Terminal> term;
cmux_state state;
uint8_t dlci;
uint8_t type;
size_t payload_len;
uint8_t frame_header[6];
size_t frame_header_offset;
size_t buffer_size;
size_t consumed;
std::unique_ptr<uint8_t[]> buffer;
bool on_cmux(size_t len);
};
enum class dte_mode {
UNDEF,
COMMAND_MODE,
DATA_MODE,
CMUX_MODE
};
enum class command_result {
OK,
FAIL,
TIMEOUT
};
const int DTE_BUFFER_SIZE = 1024;
struct CMUXHelper {
void send_sabm(size_t dlci);
};
typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb;
class DTE {
class DTE: public CommandableIf {
public:
explicit DTE(std::unique_ptr<Terminal> t);
~DTE() = default;
@ -120,15 +41,15 @@ public:
// std::shared_ptr<uint8_t[]> get_buffer() { return buffer;}
void start() { term->start(); }
void data_mode_closed() { term->stop(); }
void set_mode(dte_mode m) {
void set_mode(modem_mode m) {
term->start(); mode = m;
if (m == dte_mode::DATA_MODE) {
if (m == modem_mode::DATA_MODE) {
term->set_data_cb(on_data);
} else if (m == dte_mode::CMUX_MODE) {
} 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);
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);
@ -145,7 +66,7 @@ private:
std::unique_ptr<uint8_t[]> buffer;
std::unique_ptr<Terminal> term;
// got_line_cb on_line;
dte_mode mode;
modem_mode mode;
signal_group signal;
std::function<bool(size_t len)> on_data;

View File

@ -2,11 +2,11 @@
// Created by david on 2/26/21.
//
#ifndef SIMPLE_CXX_CLIENT_PPP_NETIF_HPP
#define SIMPLE_CXX_CLIENT_PPP_NETIF_HPP
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP
#include "esp_netif.h"
#include "cxx_include/terminal_objects.hpp"
#include "cxx_include/esp_modem_primitives.hpp"
class DTE;
class PPP;
@ -42,4 +42,4 @@ private:
};
#endif //SIMPLE_CXX_CLIENT_PPP_NETIF_HPP
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP

View File

@ -2,8 +2,8 @@
// Created by david on 2/26/21.
//
#ifndef SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP
#define SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
@ -49,7 +49,7 @@ static inline void throw_if_esp_fail(esp_err_t err)
struct Lock {
explicit Lock(): lock(nullptr)
{
lock = xSemaphoreCreateMutex();
lock = xSemaphoreCreateRecursiveMutex();
throw_if_false(lock != nullptr, "create signal event group failed");
}
~Lock() { vSemaphoreDelete(lock); }
@ -111,4 +111,4 @@ struct signal_group {
EventGroupHandle_t event_group;
};
#endif //SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP

View File

@ -0,0 +1,43 @@
//
// Created by david on 3/3/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP
#include <memory>
#include <functional>
#include <exception>
#include <cstddef>
#include <cstdint>
#include <utility>
#include "esp_err.h"
#include "esp_modem_primitives.hpp"
enum class terminal_error {
BUFFER_OVERFLOW,
CHECKSUM_ERROR,
UNEXPECTED_CONTROL_FLOW,
};
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:
std::function<bool(size_t len)> on_data;
std::function<void(terminal_error)> on_error;
};
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP

View File

@ -0,0 +1,43 @@
//
// Created by david on 3/8/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP
enum class modem_mode {
UNDEF,
COMMAND_MODE,
DATA_MODE,
CMUX_MODE
};
enum class command_result {
OK,
FAIL,
TIMEOUT
};
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) {}
size_t context_id;
std::string protocol_type;
std::string apn;
};
class CommandableIf {
public:
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

View File

@ -1,10 +0,0 @@
//
// Created by david on 3/3/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_UART_TERMINAL_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_UART_TERMINAL_HPP
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_UART_TERMINAL_HPP

View File

@ -0,0 +1,33 @@
//
// Created by david on 3/8/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_API_H
#define SIMPLE_CXX_CLIENT_ESP_MODEM_API_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct esp_modem_dce_wrap esp_modem_t;
esp_modem_t *esp_modem_new(const dte_config *config, esp_netif_t *netif, const char* apn);
void esp_modem_destroy(esp_modem_t * dce);
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
esp_err_t esp_modem_ ## name(esp_modem_t *dce, ##__VA_ARGS__);
DECLARE_ALL_COMMAND_APIS(declares esp_modem_<API>(esp_modem_t * dce, ...);)
#undef ESP_MODEM_DECLARE_DCE_COMMAND
#ifdef __cplusplus
}
#endif
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_API_H

View File

@ -0,0 +1,73 @@
//
// Created by david on 3/7/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC
#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC
#ifdef __cplusplus
#define STRING_IN const std::string&
#define STRING_OUT std::string&
#else
#define STRING_IN const char*
#define STRING_OUT char*
#endif
#define ESP_MODEM_DEFINE_DCE_COMMAND(...) ESP_MODEM_DECLARE_DCE_COMMAND(##__VA_ARGS__)
#define DEFINE_ALL_COMMAND_APIS() DECLARE_ALL_COMMAND_APIS()
#define DECLARE_ALL_COMMAND_APIS(...) \
/**
* @brief Sends the supplied PIN code
*
* @param pin Pin
*/ \
\
ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, TEMPLATE_ARG, MUX_ARG, STRING_IN) \
/**
* @brief Checks if the SIM needs a PIN
*
* @param[out] pin_ok Pin
*/ \
ESP_MODEM_DECLARE_DCE_COMMAND(read_pin, command_result, TEMPLATE_ARG, MUX_ARG, bool& pin_ok) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(set_echo, command_result, TEMPLATE_ARG, MUX_ARG, bool on) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(resume_data_mode, command_result, TEMPLATE_ARG, MUX_ARG) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(set_pdp_context, command_result, TEMPLATE_ARG, MUX_ARG, PdpContext& pdp_context) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(set_command_mode, command_result, TEMPLATE_ARG, MUX_ARG) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(set_cmux, command_result, TEMPLATE_ARG, MUX_ARG) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(get_imsi, command_result, TEMPLATE_ARG, MUX_ARG, std::string& imsi_number) \
\
ESP_MODEM_DECLARE_DCE_COMMAND(get_imei, command_result, TEMPLATE_ARG, MUX_ARG, std::string& imsi_number) \
\
/**
* @brief Reads the module name
*
* @param[out] module name
*/ \
ESP_MODEM_DECLARE_DCE_COMMAND(get_module_name, command_result, TEMPLATE_ARG, MUX_ARG, std::string& name) \
\
/**
* @brief Sets the modem to data mode
*
*/ \
ESP_MODEM_DECLARE_DCE_COMMAND(set_data_mode, command_result, TEMPLATE_ARG, MUX_ARG)
// --- DCE command documentation starts here ---
#ifdef GENERATE_DOCS
// gcc -E -CC -P -DGENERATE_DOCS esp_modem_command_declare.inc | sed -n '/DCE command documentation/,//p'
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
return_type name (__VA_ARGS__);
DECLARE_ALL_COMMAND_APIS()
#endif
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC

View File

@ -2,9 +2,10 @@
// Created by david on 3/3/21.
//
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/uart_terminal.hpp"
#include "uart_terminal.hpp"
#include "esp_log.h"
#include "cxx_include/esp_modem_api.hpp"
#include "esp_modem_api.h"
static const char *TAG = "dce_factory";
struct PdpContext;
@ -12,7 +13,6 @@ struct PdpContext;
std::shared_ptr<DTE> create_uart_dte(const dte_config *config)
{
try {
// auto term = std::make_unique<dte>(std::make_unique<uart_terminal>(config));
auto term = create_uart_terminal(config);
return std::make_shared<DTE>(std::move(term));
} catch (std::bad_alloc& e) {
@ -27,10 +27,11 @@ std::shared_ptr<DTE> create_uart_dte(const dte_config *config)
}
std::unique_ptr<DCE> create_dce(const std::shared_ptr<DTE>& dte, const std::shared_ptr<DeviceIf>& dev, esp_netif_t *netif)
template<typename SpecificModule>
std::unique_ptr<DCE<SpecificModule>> create_dce(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SpecificModule>& dev, esp_netif_t *netif)
{
try {
return std::make_unique<DCE>(dte, dev, netif);
return std::make_unique<DCE<SpecificModule>>(dte, dev, netif);
} catch (std::bad_alloc& e) {
ESP_LOGE(TAG, "Out of memory");
return nullptr;
@ -42,3 +43,74 @@ std::unique_ptr<DCE> create_dce(const std::shared_ptr<DTE>& dte, const std::shar
}
}
std::unique_ptr<DCE<GenericModule>> create_generic_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<GenericModule>& dev, esp_netif_t *netif)
{
return create_dce<GenericModule>(dte, dev, netif);
}
std::unique_ptr<DCE<SIM7600>> create_SIM7600_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SIM7600>& dev, esp_netif_t *netif)
{
return create_dce<SIM7600>(dte, dev, netif);
}
std::unique_ptr<DCE<SIM7600>> 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_dce<SIM7600>(dte, std::move(dev), netif);
}
//
// C API definitions
struct esp_modem_dce_wrap // need to mimic the polymorphic dispatch as CPP uses templated dispatch
{
enum esp_modem_t { MODEM_SIM7600, MODEM_SIM800, MODEM_BG96 } modem_type;
void * dce_ptr;
};
static inline esp_err_t command_response_to_esp_err(command_result res)
{
switch (res) {
case command_result::OK:
return ESP_OK;
case command_result::FAIL:
return ESP_FAIL;
case command_result::TIMEOUT:
return ESP_ERR_TIMEOUT;
}
return ESP_ERR_INVALID_ARG;
}
extern "C" esp_modem_t *esp_modem_new(const dte_config *config, esp_netif_t *netif, const char* apn)
{
auto dce_wrap = new (std::nothrow) esp_modem_dce_wrap;
if (dce_wrap == nullptr)
return nullptr;
std::string apn_str(apn);
auto dte = create_uart_dte(config);
auto dce = create_SIM7600_dce(dte, netif, apn_str);
dce_wrap->modem_type = esp_modem_dce_wrap::MODEM_SIM7600;
dce_wrap->dce_ptr = dce.release();
return dce_wrap;
}
extern "C" void esp_modem_destroy(esp_modem_t * dce)
{
assert(dce->modem_type == esp_modem_dce_wrap::MODEM_SIM7600);
auto dce_sim7600 = static_cast<DCE<SIM7600>*>(dce->dce_ptr);
delete dce_sim7600;
delete dce;
}
extern "C" esp_err_t esp_modem_read_pin(esp_modem_t * dce, bool &x)
{
assert(dce->modem_type == esp_modem_dce_wrap::MODEM_SIM7600);
auto dce_sim7600 = static_cast<DCE<SIM7600>*>(dce->dce_ptr);
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,24 +1,18 @@
//
// Created by david on 3/2/21.
// Created by david on 3/8/21.
//
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP
#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP
#include "esp_modem_dte.hpp"
#include "esp_modem_dce_commands.hpp"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp"
#include "cxx_include/esp_modem_command_library.hpp"
#include <string.h>
enum class command_result;
// TODO: Remove iostream
#include <iostream>
namespace esp_modem {
namespace dce_commands {
namespace esp_modem::dce_commands {
template <typename T> static inline command_result generic_command(T t, const std::string& command,
static inline command_result generic_command(CommandableIf* t, const std::string& command,
const std::string& pass_phrase,
const std::string& fail_phrase, uint32_t timeout_ms)
{
@ -35,16 +29,8 @@ template <typename T> static inline command_result generic_command(T t, const st
return command_result::TIMEOUT;
}, timeout_ms);
}
static inline void strip_cr_lf_tail(char *str, uint32_t len)
{
if (str[len - 2] == '\r') {
str[len - 2] = '\0';
} else if (str[len - 1] == '\r') {
str[len - 1] = '\0';
}
}
template <typename T> static inline command_result generic_get_string(T t, const std::string& command, std::string& output, uint32_t timeout_ms)
static inline command_result generic_get_string(CommandableIf* t, const std::string& command, std::string& output, uint32_t timeout_ms)
{
std::cout << command << std::endl;
return t->command(command.c_str(), [&](uint8_t *data, size_t len) {
@ -62,13 +48,7 @@ template <typename T> static inline command_result generic_get_string(T t, const
} else if (token.find("ERROR") != std::string::npos) {
return command_result::FAIL;
} else if (token.length() > 2) {
// response.resize(response.find('\r'));
// response.erase(std::find(response.begin(), response.end(), '\r'), response.end());
// std::cout << "|" << output << "|" << std::endl;
// const std::string& out(response);
output = token; //.substr(0,-1);
std::cout << "|" << token << "|" << std::endl;
// std::cout << output << std::endl;
output = token;
}
response = response.substr(pos+1);
}
@ -76,42 +56,42 @@ template <typename T> static inline command_result generic_get_string(T t, const
}, timeout_ms);
}
template <typename T> static inline command_result generic_command_common(T t, std::string command)
static inline command_result generic_command_common(CommandableIf* t, std::string command)
{
return generic_command(t, command, "OK", "ERROR", 500);
}
template <typename T> command_result sync(T t)
command_result sync(CommandableIf* t)
{
return generic_command_common(t, "AT\r");
}
template <typename T> command_result set_echo(T t, bool on)
command_result set_echo(CommandableIf* t, bool on)
{
if (on)
return generic_command_common(t, "ATE1\r");
return generic_command_common(t, "ATE0\r");
}
template <typename T> command_result set_pdp_context(T t, PdpContext& pdp)
command_result set_pdp_context(CommandableIf* t, PdpContext& pdp)
{
std::string pdp_command = "AT+CGDCONT=" + std::to_string(pdp.context_id) +
",\"" + pdp.protocol_type + "\",\"" + pdp.apn + "\"\r";
return generic_command_common(t, pdp_command);
}
template <typename T> command_result set_data_mode(T t)
command_result set_data_mode(CommandableIf* t)
{
return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000);
}
template <typename T> command_result resume_data_mode(T t)
command_result resume_data_mode(CommandableIf* t)
{
return generic_command(t, "ATO\r", "CONNECT", "ERROR", 5000);
}
template <typename T> command_result set_command_mode(T t)
command_result set_command_mode(CommandableIf* t)
{
std::cout << "Sending +++" << std::endl;
return t->command("+++", [&](uint8_t *data, size_t len) {
@ -128,27 +108,27 @@ template <typename T> command_result set_command_mode(T t)
}, 5000);
}
template <typename T> command_result get_imsi(T t, std::string& imsi_number)
command_result get_imsi(CommandableIf* t, std::string& imsi_number)
{
return generic_get_string(t, "AT+CIMI\r", imsi_number, 5000);
}
template <typename T> command_result get_imei(T t, std::string& out)
command_result get_imei(CommandableIf* t, std::string& out)
{
return generic_get_string(t, "AT+CGSN\r", out, 5000);
}
template <typename T> command_result get_module_name(T t, std::string& out)
command_result get_module_name(CommandableIf* t, std::string& out)
{
return generic_get_string(t, "AT+CGMM\r", out, 5000);
}
template <typename T> command_result set_cmux(T t)
command_result set_cmux(CommandableIf* t)
{
return generic_command_common(t, "AT+CMUX=0\r");
}
template <typename T> command_result read_pin(T t, bool& pin_ok)
command_result read_pin(CommandableIf* t, bool& pin_ok)
{
std::cout << "Sending read_pin" << std::endl;
return t->command("AT+CPIN?\r", [&](uint8_t *data, size_t len) {
@ -167,15 +147,11 @@ template <typename T> command_result read_pin(T t, bool& pin_ok)
}, 5000);
}
template <typename T> command_result set_pin(T t, const std::string& pin)
command_result set_pin(CommandableIf* t, const std::string& pin)
{
std::cout << "Sending set_pin" << std::endl;
std::string set_pin_command = "AT+CPIN=" + pin + "\r";
return generic_command_common(t, set_pin_command);
}
} // dce_commands
} // esp_modem
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP
} // esp_modem::dce_commands

View File

@ -1,33 +0,0 @@
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce.hpp"
#include <cstring>
#include <utility>
#include "esp_log.h"
DCE::DCE(const std::shared_ptr<DTE>& dte, std::shared_ptr<DeviceIf> device, esp_netif_t * netif):
dce_dte(dte), device(std::move(device)), ppp_netif(dte, netif)
{ }
void DCE::exit_data() {
ppp_netif.stop();
device->set_mode(dte_mode::COMMAND_MODE);
uint8_t* data;
dce_dte->set_data_cb([&](size_t len) -> bool {
auto actual_len = dce_dte->read(&data, 64);
ESP_LOG_BUFFER_HEXDUMP("esp-modem: debug_data", data, actual_len, ESP_LOG_INFO);
return false;
});
ppp_netif.wait_until_ppp_exits();
dce_dte->set_data_cb(nullptr);
dce_dte->set_mode(dte_mode::COMMAND_MODE);
}
void DCE::set_cmux()
{
device->set_mode(dte_mode::CMUX_MODE);
dce_dte->set_mode(dte_mode::CMUX_MODE);
// auto t = dce_dte->detach();
// auto cmux = std::make_unique<CMUXedTerminal>(std::move(t), dce_dte->get_buffer());
// dce_dte->attach(std::move(cmux));
}

View File

@ -1,46 +0,0 @@
//
// Created by david on 3/3/21.
//
#include "cxx_include/esp_modem_dce_commands.hpp"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_commands.hpp"
bool Device::setup_data_mode() {
if (set_echo(false) != command_result::OK)
return false;
if (set_pdp_context(*pdp) != command_result::OK)
return false;
// if (set_data_mode() != command_result::OK)
// return false;
return true;
}
bool Device::set_mode(dte_mode mode) {
if (mode == dte_mode::DATA_MODE) {
if (set_data_mode() != command_result::OK)
return resume_data_mode() == command_result::OK;
return true;
} else if (mode == dte_mode::COMMAND_MODE) {
return set_command_mode() == command_result::OK;
} else if (mode == dte_mode::CMUX_MODE) {
return set_cmux() == command_result::OK;
}
return true;
}
command_result Device::set_echo(bool on) { return esp_modem::dce_commands::set_echo(dte, on); }
command_result Device::set_data_mode() { return esp_modem::dce_commands::set_data_mode(dte); }
command_result Device::resume_data_mode() { return esp_modem::dce_commands::resume_data_mode(dte); }
command_result Device::set_pdp_context(PdpContext& pdp_context) { return esp_modem::dce_commands::set_pdp_context(dte.get(), pdp_context); }
command_result Device::set_command_mode() { return esp_modem::dce_commands::set_command_mode(dte); }
command_result Device::set_cmux() { return esp_modem::dce_commands::set_cmux(dte); }
command_result Device::get_imsi(std::string& imsi_number) { return esp_modem::dce_commands::get_imsi(dte, imsi_number); }
command_result Device::set_pin(const std::string& pin) { return esp_modem::dce_commands::set_pin(dte, pin); }
command_result Device::read_pin(bool& pin_ok) { return esp_modem::dce_commands::read_pin(dte, pin_ok); }
command_result Device::get_imei(std::string &imei) { return esp_modem::dce_commands::get_imei(dte, imei); }
command_result Device::get_module_name(std::string &name) { return esp_modem::dce_commands::get_module_name(dte, name); }

View File

@ -1,10 +0,0 @@
#include "cxx_include/esp_modem_dce_commands.hpp"
#include "cxx_include/esp_modem_dte.hpp"
//std::shared_ptr<DeviceIf> create_device(const std::shared_ptr<DTE>& dte, std::string &apn)
std::shared_ptr<Device> create_device(const std::shared_ptr<DTE>& dte, std::string &apn)
{
auto pdp = std::make_unique<PdpContext>(apn);
return std::make_shared<Device>(dte, std::move(pdp));
}

View File

@ -5,10 +5,11 @@
DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer_size(DTE_BUFFER_SIZE), consumed(0),
buffer(std::make_unique<uint8_t[]>(buffer_size)),
term(std::move(terminal)), mode(dte_mode::UNDEF) {}
term(std::move(terminal)), mode(modem_mode::UNDEF) {}
command_result DTE::command(const std::string& command, got_line_cb got_line, uint32_t time_ms)
command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms)
{
// assert(term_id < term->max_virtual_terms());
Scoped<Lock> l(lock);
command_result res = command_result::TIMEOUT;
term->write((uint8_t *)command.c_str(), command.length());

View File

@ -0,0 +1,21 @@
#include <functional>
#include "cxx_include/esp_modem_types.hpp"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp"
template<typename T>
std::shared_ptr<T> create_device(const std::shared_ptr<DTE>& dte, std::string &apn)
{
auto pdp = std::make_unique<PdpContext>(apn);
return std::make_shared<T>(dte, std::move(pdp));
}
std::shared_ptr<GenericModule> create_generic_module(const std::shared_ptr<DTE>& dte, std::string &apn)
{
return create_device<GenericModule>(dte, apn);
}
std::shared_ptr<SIM7600> create_SIM7600_module(const std::shared_ptr<DTE>& dte, std::string &apn)
{
return create_device<SIM7600>(dte, apn);
}

View File

@ -0,0 +1,23 @@
//
// Created by david on 3/8/21.
//
#include "cxx_include/esp_modem_dce_module.hpp"
command_result SIM7600::get_module_name(std::string& name)
{
name = "7600";
return command_result::OK;
}
command_result SIM800::get_module_name(std::string& name)
{
name = "800L";
return command_result::OK;
}
command_result BG96::get_module_name(std::string& name)
{
name = "BG96";
return command_result::OK;
}

View File

@ -5,15 +5,12 @@
#include <utility>
#include <esp_log.h>
#include <esp_event.h>
#include "cxx_include/ppp_netif.hpp"
#include "cxx_include/esp_modem_netif.hpp"
#include "cxx_include/esp_modem_dte.hpp"
#include "esp_netif_ppp.h"
#include <iostream>
//struct ppp_netif_driver {
// esp_netif_driver_base_t base;
//};
void PPP::on_ppp_changed(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)

View File

@ -177,11 +177,8 @@ std::unique_ptr<Terminal> create_uart_terminal(const dte_config *config)
ESP_LOGE(TAG, "%s", e.what());
return nullptr;
}
}
void uart_terminal::task()
{
std::function<bool(size_t len)> on_data_priv = nullptr;