mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-19 05:22:21 +02:00
CMUX: Experimental implementation
This commit is contained in:
@ -6,14 +6,11 @@
|
||||
#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP
|
||||
|
||||
#include "esp_modem_dte.hpp"
|
||||
//#include "esp_modem_dce_commands.hpp"
|
||||
#include "esp_modem_dce_commands.hpp"
|
||||
#include <string.h>
|
||||
|
||||
enum class command_result;
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#include <iostream>
|
||||
@ -38,6 +35,46 @@ 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -76,10 +113,66 @@ template <typename T> command_result resume_data_mode(T t)
|
||||
|
||||
template <typename T> command_result set_command_mode(T t)
|
||||
{
|
||||
return generic_command(t, "+++", "OK", "ERROR", 50000);
|
||||
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
|
||||
|
@ -16,17 +16,13 @@ public:
|
||||
dce_dte->set_mode(dte_mode::DATA_MODE);
|
||||
ppp_netif.start();
|
||||
}
|
||||
void exit_data() {
|
||||
ppp_netif.stop();
|
||||
device->set_mode(dte_mode::COMMAND_MODE);
|
||||
ppp_netif.wait_until_ppp_exits();
|
||||
dce_dte->set_mode(dte_mode::COMMAND_MODE);
|
||||
}
|
||||
void exit_data();
|
||||
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();
|
||||
private:
|
||||
std::shared_ptr<DTE> dce_dte;
|
||||
std::shared_ptr<DeviceIf> device;
|
||||
ppp ppp_netif;
|
||||
PPP ppp_netif;
|
||||
};
|
@ -1,9 +1,15 @@
|
||||
#pragma once
|
||||
#include "cxx_include/esp_modem_dce_commands_if.hpp"
|
||||
#include "cxx_include/esp_modem_commands.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;
|
||||
|
||||
@ -14,11 +20,17 @@ public:
|
||||
bool setup_data_mode() override;
|
||||
bool set_mode(dte_mode mode) override;
|
||||
|
||||
command_result set_echo(bool on) { return esp_modem::dce_commands::set_echo(dte, on); }
|
||||
command_result set_data_mode() { return esp_modem::dce_commands::set_data_mode(dte); }
|
||||
command_result resume_data_mode() { return esp_modem::dce_commands::resume_data_mode(dte); }
|
||||
command_result set_pdp_context(PdpContext& pdp_context) { return esp_modem::dce_commands::set_pdp_context(dte.get(), pdp_context); }
|
||||
command_result set_command_mode() { return esp_modem::dce_commands::set_command_mode(dte); }
|
||||
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;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
std::shared_ptr<DeviceIf> create_device(const std::shared_ptr<DTE>& dte, std::string &apn);
|
||||
#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);
|
@ -10,7 +10,7 @@
|
||||
#include "esp_err.h"
|
||||
#include "terminal_objects.hpp"
|
||||
#include "ppp_netif.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
enum class terminal_error {
|
||||
BUFFER_OVERFLOW,
|
||||
@ -18,10 +18,10 @@ enum class terminal_error {
|
||||
UNEXPECTED_CONTROL_FLOW,
|
||||
};
|
||||
|
||||
class terminal {
|
||||
class Terminal {
|
||||
public:
|
||||
virtual ~terminal() = default;
|
||||
void set_data_cb(std::function<void(size_t len)> f) { on_data = std::move(f); }
|
||||
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;
|
||||
@ -29,26 +29,36 @@ public:
|
||||
virtual void stop() = 0;
|
||||
|
||||
protected:
|
||||
std::function<void(size_t len)> on_data;
|
||||
std::function<bool(size_t len)> on_data;
|
||||
std::function<void(terminal_error)> on_error;
|
||||
};
|
||||
|
||||
class dte_adapter: public terminal {
|
||||
class CMUXedTerminal: public Terminal {
|
||||
public:
|
||||
dte_adapter(std::unique_ptr<terminal> terminal):
|
||||
original_dte(std::move(terminal)) {}
|
||||
~dte_adapter() override = default;
|
||||
int write(uint8_t *data, size_t len) override { return original_dte->write(data, len); }
|
||||
int read(uint8_t *data, size_t len) override { return original_dte->read(data, len); }
|
||||
explicit CMUXedTerminal(std::unique_ptr<Terminal> t, std::shared_ptr<uint8_t[]> b):
|
||||
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 { return term->write(data, len); }
|
||||
int read(uint8_t *data, size_t len) override { return term->read(data, len); }
|
||||
void start() override;
|
||||
void stop() override {}
|
||||
private:
|
||||
std::unique_ptr<terminal> original_dte;
|
||||
static bool process_cmux_recv(size_t len);
|
||||
void send_sabm(size_t i);
|
||||
std::unique_ptr<Terminal> term;
|
||||
std::shared_ptr<uint8_t[]> buffer;
|
||||
};
|
||||
|
||||
|
||||
enum class dte_mode {
|
||||
UNDEF,
|
||||
COMMAND_MODE,
|
||||
DATA_MODE
|
||||
DATA_MODE,
|
||||
CMUX_MODE
|
||||
};
|
||||
|
||||
enum class command_result {
|
||||
@ -60,12 +70,27 @@ enum class command_result {
|
||||
const int DTE_BUFFER_SIZE = 1024;
|
||||
|
||||
|
||||
struct CMUXHelper {
|
||||
void send_sabm(size_t dlci);
|
||||
};
|
||||
|
||||
enum class cmux_state {
|
||||
INIT,
|
||||
HEADER,
|
||||
PAYLOAD,
|
||||
FOOTER,
|
||||
|
||||
};
|
||||
|
||||
typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb;
|
||||
|
||||
class DTE {
|
||||
public:
|
||||
explicit DTE(std::unique_ptr<terminal> t);
|
||||
explicit DTE(std::unique_ptr<Terminal> t);
|
||||
~DTE() = default;
|
||||
|
||||
// std::unique_ptr<Terminal> detach() { return std::move(term); }
|
||||
// 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) {
|
||||
@ -75,28 +100,51 @@ public:
|
||||
*d = data;
|
||||
return actual_len;
|
||||
}
|
||||
void set_data_cb(std::function<void(size_t len)> f) { on_data = std::move(f); }
|
||||
void set_data_cb(std::function<bool(size_t len)> f)
|
||||
{
|
||||
// on_data = std::move(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(dte_mode m) {
|
||||
term->start(); mode = m;
|
||||
if (m == dte_mode::DATA_MODE) {
|
||||
term->set_data_cb(on_data);
|
||||
} else if (m == dte_mode::CMUX_MODE) {
|
||||
setup_cmux();
|
||||
}
|
||||
}
|
||||
command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms);
|
||||
// std::shared_ptr<uint8_t[]> buffer;
|
||||
|
||||
void send_cmux_command(uint8_t i, const std::string& command);
|
||||
private:
|
||||
const size_t GOT_LINE = BIT0;
|
||||
|
||||
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;
|
||||
std::unique_ptr<Terminal> term;
|
||||
got_line_cb on_line;
|
||||
dte_mode mode;
|
||||
signal_group signal;
|
||||
std::function<void(size_t len)> on_data;
|
||||
std::function<bool(size_t len)> on_data;
|
||||
|
||||
bool on_cmux(size_t len);
|
||||
static bool s_on_cmux(size_t len);
|
||||
cmux_state state;
|
||||
uint8_t dlci;
|
||||
uint8_t type;
|
||||
size_t payload_len;
|
||||
uint8_t frame_header[6];
|
||||
size_t frame_header_offset;
|
||||
};
|
||||
|
||||
|
||||
|
@ -9,29 +9,35 @@
|
||||
#include "cxx_include/terminal_objects.hpp"
|
||||
|
||||
class DTE;
|
||||
class PPP;
|
||||
|
||||
//struct ppp_netif_driver;
|
||||
struct ppp_netif_driver {
|
||||
esp_netif_driver_base_t base;
|
||||
DTE *e;
|
||||
PPP *ppp;
|
||||
};
|
||||
|
||||
|
||||
class ppp {
|
||||
class PPP {
|
||||
public:
|
||||
explicit ppp(std::shared_ptr<DTE> e, esp_netif_t *netif);
|
||||
explicit PPP(std::shared_ptr<DTE> e, esp_netif_t *netif);
|
||||
|
||||
void start();
|
||||
void notify_ppp_exit() { signal.set(PPP_EXIT); }
|
||||
// void notify_ppp_exit() { signal.set(PPP_EXIT); }
|
||||
void wait_until_ppp_exits() { signal.wait(PPP_EXIT, 50000); }
|
||||
void stop();
|
||||
private:
|
||||
void receive(uint8_t *data, size_t len) const;
|
||||
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 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;
|
||||
esp_netif_t *netif;
|
||||
struct ppp_netif_driver driver;
|
||||
struct ppp_netif_driver driver{};
|
||||
signal_group signal;
|
||||
const size_t PPP_EXIT = BIT0;
|
||||
static const size_t PPP_STARTED = BIT0;
|
||||
static const size_t PPP_EXIT = BIT1;
|
||||
|
||||
};
|
||||
|
||||
|
@ -56,6 +56,11 @@ struct signal_group {
|
||||
xEventGroupSetBits(event_group, bits);
|
||||
}
|
||||
|
||||
void clear(uint32_t bits)
|
||||
{
|
||||
xEventGroupClearBits(event_group, bits);
|
||||
}
|
||||
|
||||
bool wait(uint32_t flags, uint32_t time_ms) // waiting for all and clearing if set
|
||||
{
|
||||
EventBits_t bits = xEventGroupWaitBits(event_group, flags, pdTRUE, pdTRUE, pdMS_TO_TICKS(time_ms));
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
struct dte_config;
|
||||
|
||||
std::unique_ptr<terminal> create_uart_terminal(const dte_config *config);
|
||||
std::unique_ptr<Terminal> create_uart_terminal(const dte_config *config);
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user