added scope lock

This commit is contained in:
David Cermak
2021-03-06 16:56:50 +01:00
parent fdfca36919
commit 031bbc14ff
6 changed files with 47 additions and 44 deletions

View File

@ -294,10 +294,10 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
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!");
// }
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;
@ -315,17 +315,16 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
auto my_dce = create_dce(uart_dte, device, esp_netif);
// return;
my_dce->set_cmux();
// my_dce->set_cmux();
// my_dce->set_cmux();
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
// uart_dte->send_cmux_command(1, "AT+CPIN?\r");
uart_dte->write((uint8_t*)"AT+CPIN?\r", 9);
}
// while (1) {
// vTaskDelay(pdMS_TO_TICKS(1000));
// uart_dte->write((uint8_t*)"AT+CPIN?\r", 9);
//
// }
// uart_dte->send_cmux_command(2, "AT+CPIN?");
return;
// return;
my_dce->command("AT+CPIN?\r", [&](uint8_t *data, size_t len) {
std::string response((char*)data, len);

View File

@ -39,7 +39,7 @@ enum class cmux_state {
HEADER,
PAYLOAD,
FOOTER,
RECOVER,
};
class CMUXedTerminal: public Terminal {
@ -67,7 +67,6 @@ private:
size_t consumed;
std::unique_ptr<uint8_t[]> buffer;
bool on_cmux(size_t len);
static bool s_on_cmux(size_t len);
};
@ -96,26 +95,6 @@ struct CMUXHelper {
typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb;
//class CMUX {
//public:
// void setup_cmux();
// void send_sabm(size_t dlci);
//
// 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;
// size_t buffer_size;
// size_t consumed;
//// std::shared_ptr<std::vector<uint8_t>> buffer;
// std::unique_ptr<uint8_t[]> buffer;
//
//};
class DTE {
public:
explicit DTE(std::unique_ptr<Terminal> t);
@ -154,6 +133,7 @@ public:
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);

View File

@ -6,6 +6,8 @@
#define SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
class esp_err_exception: virtual public std::exception {
@ -44,6 +46,29 @@ static inline void throw_if_esp_fail(esp_err_t err)
}
}
struct Lock {
explicit Lock(): lock(nullptr)
{
lock = xSemaphoreCreateMutex();
throw_if_false(lock != nullptr, "create signal event group failed");
}
~Lock() { vSemaphoreDelete(lock); }
void take() { xSemaphoreTake(lock, portMAX_DELAY); }
void give() { xSemaphoreGive(lock); }
xSemaphoreHandle lock;
};
template<class T>
class Scoped {
public:
explicit Scoped(T &l):lock(l) { lock.take(); }
~Scoped() { lock.give(); }
private:
T& lock;
};
struct signal_group {
explicit signal_group(): event_group(nullptr)
{

View File

@ -94,13 +94,6 @@ bool CMUXedTerminal::process_cmux_recv(size_t len)
return false;
}
static CMUXedTerminal * s_cmux;
bool CMUXedTerminal::s_on_cmux(size_t len)
{
s_cmux->on_cmux(len);
return false;
}
bool output(uint8_t *data, size_t len, std::string message)
{
printf("OUTPUT: %s len=%d \n", message.c_str(), len);
@ -130,6 +123,9 @@ bool CMUXedTerminal::on_cmux(size_t len)
size_t footer_offset = 0;
while (available_len > 0) {
switch (state) {
case cmux_state::RECOVER:
// TODO: Implement recovery, looking for SOF_MARKER's
break;
case cmux_state::INIT:
if (frame[0] != SOF_MARKER) {
ESP_LOGW("CMUX", "TODO: Recover!");
@ -199,10 +195,12 @@ bool CMUXedTerminal::on_cmux(size_t len)
}
void CMUXedTerminal::setup_cmux()
{
s_cmux = this;
frame_header_offset = 0;
state = cmux_state::INIT;
term->set_data_cb(s_on_cmux);
term->set_data_cb([this](size_t len){
this->on_cmux(len);
return false;
});
for (size_t i = 0; i < 3; i++)
{

View File

@ -9,6 +9,7 @@ DTE::DTE(std::unique_ptr<Terminal> terminal):
command_result DTE::command(const std::string& command, got_line_cb got_line, uint32_t time_ms)
{
Scoped<Lock> l(lock);
command_result res = command_result::TIMEOUT;
term->write((uint8_t *)command.c_str(), command.length());
term->set_data_cb([&](size_t len){

View File

@ -88,7 +88,7 @@ PPP::PPP(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif):
void PPP::start()
{
ppp_dte->set_data_cb([&](size_t len) -> bool {
ppp_dte->set_data_cb([this](size_t len) -> bool {
uint8_t *data;
auto actual_len = ppp_dte->read(&data, len);
receive(data, actual_len);