2021-02-26 18:32:15 +01:00
|
|
|
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
|
|
|
|
#define SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
#include <exception>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
|
|
|
#include "esp_err.h"
|
2021-03-07 19:43:45 +01:00
|
|
|
#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"
|
2021-03-05 21:18:38 +01:00
|
|
|
|
2021-03-03 20:35:08 +01:00
|
|
|
|
2021-02-26 18:32:15 +01:00
|
|
|
const int DTE_BUFFER_SIZE = 1024;
|
|
|
|
|
|
|
|
|
2021-03-07 19:43:45 +01:00
|
|
|
class DTE: public CommandableIf {
|
2021-02-26 18:32:15 +01:00
|
|
|
public:
|
2021-03-04 20:19:18 +01:00
|
|
|
explicit DTE(std::unique_ptr<Terminal> t);
|
2021-03-03 20:35:08 +01:00
|
|
|
~DTE() = default;
|
2021-03-04 20:19:18 +01:00
|
|
|
|
|
|
|
// std::unique_ptr<Terminal> detach() { return std::move(term); }
|
|
|
|
// void attach(std::unique_ptr<Terminal> t) { term = std::move(t); }
|
2021-02-26 18:32:15 +01:00
|
|
|
// void set_line_cb(got_line f) { on_line_cb = std::move(f); }
|
2021-02-26 20:23:29 +01:00
|
|
|
int write(uint8_t *data, size_t len) { return term->write(data, len); }
|
2021-02-27 09:32:14 +01:00
|
|
|
int read(uint8_t **d, size_t len) {
|
|
|
|
auto data_to_read = std::min(len, buffer_size);
|
|
|
|
auto data = buffer.get();
|
|
|
|
auto actual_len = term->read(data, data_to_read);
|
|
|
|
*d = data;
|
|
|
|
return actual_len;
|
|
|
|
}
|
2021-03-04 20:19:18 +01:00
|
|
|
void set_data_cb(std::function<bool(size_t len)> f)
|
|
|
|
{
|
|
|
|
// on_data = std::move(f);
|
|
|
|
term->set_data_cb(std::move(f));
|
2021-02-26 18:32:15 +01:00
|
|
|
|
2021-03-04 20:19:18 +01:00
|
|
|
}
|
|
|
|
// std::shared_ptr<uint8_t[]> get_buffer() { return buffer;}
|
2021-02-26 20:23:29 +01:00
|
|
|
void start() { term->start(); }
|
|
|
|
void data_mode_closed() { term->stop(); }
|
2021-03-07 19:43:45 +01:00
|
|
|
void set_mode(modem_mode m) {
|
2021-02-27 09:32:14 +01:00
|
|
|
term->start(); mode = m;
|
2021-03-07 19:43:45 +01:00
|
|
|
if (m == modem_mode::DATA_MODE) {
|
2021-02-27 09:32:14 +01:00
|
|
|
term->set_data_cb(on_data);
|
2021-03-07 19:43:45 +01:00
|
|
|
} else if (m == modem_mode::CMUX_MODE) {
|
2021-03-04 20:19:18 +01:00
|
|
|
setup_cmux();
|
2021-02-27 09:32:14 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-07 19:43:45 +01:00
|
|
|
command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) override;
|
2021-03-04 20:19:18 +01:00
|
|
|
// std::shared_ptr<uint8_t[]> buffer;
|
2021-02-26 18:32:15 +01:00
|
|
|
|
2021-03-04 20:19:18 +01:00
|
|
|
void send_cmux_command(uint8_t i, const std::string& command);
|
2021-02-26 18:32:15 +01:00
|
|
|
private:
|
2021-03-06 16:56:50 +01:00
|
|
|
Lock lock;
|
2021-03-05 21:18:38 +01:00
|
|
|
// std::unique_ptr<CMUXedTerminal> cmux;
|
2021-03-04 20:19:18 +01:00
|
|
|
void setup_cmux();
|
2021-03-05 21:18:38 +01:00
|
|
|
// void send_sabm(size_t dlci);
|
2021-03-04 20:19:18 +01:00
|
|
|
// CMUXHelper cmux;
|
|
|
|
static const size_t GOT_LINE = BIT0;
|
2021-02-26 18:32:15 +01:00
|
|
|
size_t buffer_size;
|
|
|
|
size_t consumed;
|
2021-03-04 20:19:18 +01:00
|
|
|
// std::shared_ptr<std::vector<uint8_t>> buffer;
|
2021-02-26 18:32:15 +01:00
|
|
|
std::unique_ptr<uint8_t[]> buffer;
|
2021-03-04 20:19:18 +01:00
|
|
|
std::unique_ptr<Terminal> term;
|
2021-03-05 21:18:38 +01:00
|
|
|
// got_line_cb on_line;
|
2021-03-07 19:43:45 +01:00
|
|
|
modem_mode mode;
|
2021-02-26 18:32:15 +01:00
|
|
|
signal_group signal;
|
2021-03-04 20:19:18 +01:00
|
|
|
std::function<bool(size_t len)> on_data;
|
|
|
|
|
2021-03-05 21:18:38 +01:00
|
|
|
|
2021-02-26 18:32:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-03-03 20:35:08 +01:00
|
|
|
|
2021-02-26 20:23:29 +01:00
|
|
|
|
2021-02-26 18:32:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
|