mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-20 14:02:21 +02:00
Implement perfect forwarding for command lib
This commit is contained in:
@ -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);
|
||||
|
45
esp_modem/include/cxx_include/esp_modem_cmux.hpp
Normal file
45
esp_modem/include/cxx_include/esp_modem_cmux.hpp
Normal 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
|
27
esp_modem/include/cxx_include/esp_modem_command_library.hpp
Normal file
27
esp_modem/include/cxx_include/esp_modem_command_library.hpp
Normal 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
|
@ -1,181 +0,0 @@
|
||||
//
|
||||
// Created by david on 3/2/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 <string.h>
|
||||
|
||||
enum class command_result;
|
||||
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace esp_modem {
|
||||
namespace dce_commands {
|
||||
|
||||
template <typename T> static inline command_result generic_command(T t, const std::string& command,
|
||||
const std::string& pass_phrase,
|
||||
const std::string& fail_phrase, uint32_t timeout_ms)
|
||||
{
|
||||
std::cout << command << std::endl;
|
||||
return t->command(command.c_str(), [&](uint8_t *data, size_t len) {
|
||||
std::string response((char*)data, len);
|
||||
std::cout << response << std::endl;
|
||||
|
||||
if (response.find(pass_phrase) != std::string::npos) {
|
||||
return command_result::OK;
|
||||
} else if (response.find(fail_phrase) != std::string::npos) {
|
||||
return command_result::FAIL;
|
||||
}
|
||||
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)
|
||||
{
|
||||
std::cout << command << std::endl;
|
||||
return t->command(command.c_str(), [&](uint8_t *data, size_t len) {
|
||||
size_t pos = 0;
|
||||
std::string response((char*)data, len);
|
||||
while ((pos = response.find('\n')) != std::string::npos) {
|
||||
std::string token = response.substr(0, pos);
|
||||
for (auto i = 0; i<2; ++i) // trip trailing \n\r of last two chars
|
||||
if (pos >= 1 && (token[pos-1] == '\r' || token[pos-1] == '\n'))
|
||||
token.pop_back();
|
||||
std::cout << "###" << token << "#### " << std::endl;
|
||||
|
||||
if (token.find("OK") != std::string::npos) {
|
||||
return command_result::OK;
|
||||
} 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;
|
||||
}
|
||||
response = response.substr(pos+1);
|
||||
}
|
||||
return command_result::TIMEOUT;
|
||||
}, timeout_ms);
|
||||
}
|
||||
|
||||
template <typename T> static inline command_result generic_command_common(T t, std::string command)
|
||||
{
|
||||
return generic_command(t, command, "OK", "ERROR", 500);
|
||||
}
|
||||
|
||||
|
||||
template <typename T> command_result sync(T t)
|
||||
{
|
||||
return generic_command_common(t, "AT\r");
|
||||
}
|
||||
|
||||
template <typename T> command_result set_echo(T 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000);
|
||||
}
|
||||
|
||||
template <typename T> command_result resume_data_mode(T t)
|
||||
{
|
||||
return generic_command(t, "ATO\r", "CONNECT", "ERROR", 5000);
|
||||
}
|
||||
|
||||
template <typename T> command_result set_command_mode(T t)
|
||||
{
|
||||
std::cout << "Sending +++" << std::endl;
|
||||
return t->command("+++", [&](uint8_t *data, size_t len) {
|
||||
std::string response((char*)data, len);
|
||||
std::cout << response << std::endl;
|
||||
if (response.find("OK") != std::string::npos) {
|
||||
return command_result::OK;
|
||||
} else if (response.find("NO CARRIER") != std::string::npos) {
|
||||
return command_result::OK;
|
||||
} else if (response.find("ERROR") != std::string::npos) {
|
||||
return command_result::FAIL;
|
||||
}
|
||||
return command_result::TIMEOUT;
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
template <typename T> command_result get_imsi(T 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)
|
||||
{
|
||||
return generic_get_string(t, "AT+CGSN\r", out, 5000);
|
||||
}
|
||||
|
||||
template <typename T> command_result get_module_name(T t, std::string& out)
|
||||
{
|
||||
return generic_get_string(t, "AT+CGMM\r", out, 5000);
|
||||
}
|
||||
|
||||
template <typename T> command_result set_cmux(T t)
|
||||
{
|
||||
return generic_command_common(t, "AT+CMUX=0\r");
|
||||
}
|
||||
|
||||
template <typename T> command_result read_pin(T t, bool& pin_ok)
|
||||
{
|
||||
std::cout << "Sending read_pin" << std::endl;
|
||||
return t->command("AT+CPIN?\r", [&](uint8_t *data, size_t len) {
|
||||
std::string response((char*)data, len);
|
||||
std::cout << response << std::endl;
|
||||
if (response.find("READY") != std::string::npos) {
|
||||
pin_ok = true;
|
||||
return command_result::OK;
|
||||
} else if (response.find("PIN") != std::string::npos || response.find("PUK") != std::string::npos ) {
|
||||
pin_ok = false;
|
||||
return command_result::OK;
|
||||
} else if (response.find("ERROR") != std::string::npos) {
|
||||
return command_result::FAIL;
|
||||
}
|
||||
return command_result::TIMEOUT;
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
template <typename T> command_result set_pin(T 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
|
@ -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;
|
||||
};
|
@ -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;
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
};
|
70
esp_modem/include/cxx_include/esp_modem_dce_module.hpp
Normal file
70
esp_modem/include/cxx_include/esp_modem_dce_module.hpp
Normal 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);
|
||||
};
|
@ -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);
|
@ -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;
|
||||
|
||||
|
@ -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
|
@ -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
|
43
esp_modem/include/cxx_include/esp_modem_terminal.hpp
Normal file
43
esp_modem/include/cxx_include/esp_modem_terminal.hpp
Normal 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
|
43
esp_modem/include/cxx_include/esp_modem_types.hpp
Normal file
43
esp_modem/include/cxx_include/esp_modem_types.hpp
Normal 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
|
@ -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
|
@ -1,17 +0,0 @@
|
||||
//
|
||||
// Created by david on 2/25/21.
|
||||
//
|
||||
|
||||
#ifndef SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP
|
||||
#define SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP
|
||||
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
|
||||
|
||||
struct dte_config;
|
||||
|
||||
std::unique_ptr<Terminal> create_uart_terminal(const dte_config *config);
|
||||
|
||||
|
||||
|
||||
#endif //SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP
|
Reference in New Issue
Block a user