mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-19 05:22:21 +02:00
Test: Initial version of modem tests
This commit is contained in:
@ -58,9 +58,10 @@ public:
|
||||
return dte->command(command, std::move(got_line), time_ms);
|
||||
}
|
||||
|
||||
protected:
|
||||
bool set_mode(modem_mode m) { return mode.set(dte.get(), module.get(), netif, m); }
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
std::shared_ptr<DTE> dte;
|
||||
std::shared_ptr<SpecificModule> module;
|
||||
|
@ -50,12 +50,12 @@ template<typename Module>
|
||||
class Builder {
|
||||
static_assert(std::is_base_of<ModuleIf, Module>::value, "Builder must be used only for Module classes");
|
||||
public:
|
||||
explicit Builder(std::shared_ptr<DTE> dte): dte(std::move(dte))
|
||||
{
|
||||
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP();
|
||||
netif = esp_netif_new(&netif_config);
|
||||
throw_if_false(netif != nullptr, "Cannot create default PPP netif");
|
||||
}
|
||||
// explicit Builder(std::shared_ptr<DTE> dte): dte(std::move(dte))
|
||||
// {
|
||||
// esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_PPP();
|
||||
// netif = esp_netif_new(&netif_config);
|
||||
// throw_if_false(netif != nullptr, "Cannot create default PPP netif");
|
||||
// }
|
||||
|
||||
Builder(std::shared_ptr<DTE> x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif)
|
||||
{
|
||||
|
@ -69,7 +69,7 @@ private:
|
||||
|
||||
void setup_cmux();
|
||||
|
||||
static const size_t GOT_LINE = BIT0;
|
||||
static const size_t GOT_LINE = signal_group::bit0;
|
||||
size_t buffer_size;
|
||||
size_t consumed;
|
||||
std::unique_ptr<uint8_t[]> buffer;
|
||||
|
66
esp_modem/include/cxx_include/esp_modem_exception.hpp
Normal file
66
esp_modem/include/cxx_include/esp_modem_exception.hpp
Normal file
@ -0,0 +1,66 @@
|
||||
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _ESP_MODEM_EXCEPTION_HPP_
|
||||
#define _ESP_MODEM_EXCEPTION_HPP_
|
||||
|
||||
#include <string>
|
||||
#include "esp_err.h"
|
||||
|
||||
namespace esp_modem {
|
||||
|
||||
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
|
||||
#define THROW(exception) throw(exception)
|
||||
class esp_err_exception: virtual public std::exception {
|
||||
public:
|
||||
explicit esp_err_exception(esp_err_t err): esp_err(err) {}
|
||||
explicit esp_err_exception(std::string msg): esp_err(ESP_FAIL), message(std::move(msg)) {}
|
||||
explicit esp_err_exception(std::string msg, esp_err_t err): esp_err(err), message(std::move(msg)) {}
|
||||
virtual esp_err_t get_err_t() { return esp_err; }
|
||||
~esp_err_exception() noexcept override = default;
|
||||
virtual const char* what() const noexcept {
|
||||
return message.c_str();
|
||||
}
|
||||
private:
|
||||
esp_err_t esp_err;
|
||||
std::string message;
|
||||
};
|
||||
#else
|
||||
#define THROW(exception) abort()
|
||||
#endif
|
||||
|
||||
static inline void throw_if_false(bool condition, std::string message)
|
||||
{
|
||||
if (!condition) {
|
||||
THROW(esp_err_exception(std::move(message)));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void throw_if_esp_fail(esp_err_t err, std::string message)
|
||||
{
|
||||
if (err != ESP_OK) {
|
||||
THROW(esp_err_exception(std::move(message), err));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void throw_if_esp_fail(esp_err_t err)
|
||||
{
|
||||
if (err != ESP_OK) {
|
||||
THROW(esp_err_exception(err));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esp_modem
|
||||
|
||||
#endif //_ESP_MODEM_EXCEPTION_HPP_
|
@ -15,6 +15,8 @@
|
||||
#ifndef _ESP_MODEM_NETIF_HPP
|
||||
#define _ESP_MODEM_NETIF_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <cstddef>
|
||||
#include "esp_netif.h"
|
||||
#include "cxx_include/esp_modem_primitives.hpp"
|
||||
|
||||
@ -36,7 +38,7 @@ public:
|
||||
|
||||
void start();
|
||||
|
||||
void wait_until_ppp_exits() { signal.wait(PPP_EXIT, 30000); }
|
||||
void wait_until_ppp_exits();
|
||||
|
||||
void stop();
|
||||
|
||||
@ -53,8 +55,8 @@ private:
|
||||
esp_netif_t *netif;
|
||||
struct ppp_netif_driver driver{};
|
||||
signal_group signal;
|
||||
static const size_t PPP_STARTED = BIT0;
|
||||
static const size_t PPP_EXIT = BIT1;
|
||||
static const size_t PPP_STARTED = signal_group::bit0;
|
||||
static const size_t PPP_EXIT = signal_group::bit1;
|
||||
};
|
||||
|
||||
} // namespace esp_modem
|
||||
|
@ -14,116 +14,73 @@
|
||||
|
||||
#ifndef _ESP_MODEM_PRIMITIVES_HPP_
|
||||
#define _ESP_MODEM_PRIMITIVES_HPP_
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_modem_exception.hpp"
|
||||
|
||||
#if defined(CONFIG_IDF_TARGET_LINUX)
|
||||
#include <mutex>
|
||||
#else
|
||||
// forward declarations of FreeRTOS primitives
|
||||
struct QueueDefinition;
|
||||
typedef void * EventGroupHandle_t;
|
||||
#endif
|
||||
|
||||
|
||||
namespace esp_modem {
|
||||
|
||||
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
|
||||
#define THROW(exception) throw(exception)
|
||||
class esp_err_exception: virtual public std::exception {
|
||||
public:
|
||||
explicit esp_err_exception(esp_err_t err): esp_err(err) {}
|
||||
explicit esp_err_exception(std::string msg): esp_err(ESP_FAIL), message(std::move(msg)) {}
|
||||
explicit esp_err_exception(std::string msg, esp_err_t err): esp_err(err), message(std::move(msg)) {}
|
||||
virtual esp_err_t get_err_t() { return esp_err; }
|
||||
~esp_err_exception() noexcept override = default;
|
||||
virtual const char* what() const noexcept {
|
||||
return message.c_str();
|
||||
}
|
||||
private:
|
||||
esp_err_t esp_err;
|
||||
std::string message;
|
||||
};
|
||||
#else
|
||||
#define THROW(exception) abort()
|
||||
#endif
|
||||
|
||||
static inline void throw_if_false(bool condition, std::string message)
|
||||
{
|
||||
if (!condition) {
|
||||
THROW(esp_err_exception(std::move(message)));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void throw_if_esp_fail(esp_err_t err, std::string message)
|
||||
{
|
||||
if (err != ESP_OK) {
|
||||
THROW(esp_err_exception(std::move(message), err));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void throw_if_esp_fail(esp_err_t err)
|
||||
{
|
||||
if (err != ESP_OK) {
|
||||
THROW(esp_err_exception(err));
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(CONFIG_IDF_TARGET_LINUX)
|
||||
struct Lock {
|
||||
explicit Lock(): lock(nullptr)
|
||||
{
|
||||
lock = xSemaphoreCreateRecursiveMutex();
|
||||
throw_if_false(lock != nullptr, "create signal event group failed");
|
||||
}
|
||||
~Lock() { vSemaphoreDelete(lock); }
|
||||
void take() { xSemaphoreTakeRecursive(lock, portMAX_DELAY); }
|
||||
using MutexT = QueueDefinition*;
|
||||
|
||||
void give() { xSemaphoreGiveRecursive(lock); }
|
||||
xSemaphoreHandle lock;
|
||||
explicit Lock();
|
||||
~Lock();
|
||||
void lock();
|
||||
void unlock();
|
||||
private:
|
||||
MutexT m{};
|
||||
};
|
||||
|
||||
using Signal = void*;
|
||||
|
||||
#else
|
||||
using Lock = std::mutex;
|
||||
struct SignalGroup;
|
||||
using Signal = std::unique_ptr<SignalGroup>;
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
class Scoped {
|
||||
public:
|
||||
explicit Scoped(T &l):lock(l) { lock.take(); }
|
||||
~Scoped() { lock.give(); }
|
||||
explicit Scoped(T &l):lock(l) { lock.lock(); }
|
||||
~Scoped() { lock.unlock(); }
|
||||
|
||||
private:
|
||||
T& lock;
|
||||
};
|
||||
|
||||
struct signal_group {
|
||||
explicit signal_group(): event_group(nullptr)
|
||||
{
|
||||
event_group = xEventGroupCreate();
|
||||
throw_if_false(event_group != nullptr, "create signal event group failed");
|
||||
}
|
||||
static constexpr size_t bit0 = 1 << 0;
|
||||
static constexpr size_t bit1 = 1 << 1;
|
||||
static constexpr size_t bit2 = 1 << 2;
|
||||
static constexpr size_t bit3 = 1 << 3;
|
||||
|
||||
void set(uint32_t bits)
|
||||
{
|
||||
xEventGroupSetBits(event_group, bits);
|
||||
}
|
||||
explicit signal_group();
|
||||
|
||||
void clear(uint32_t bits)
|
||||
{
|
||||
xEventGroupClearBits(event_group, bits);
|
||||
}
|
||||
void set(uint32_t 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));
|
||||
return bits & flags;
|
||||
}
|
||||
void clear(uint32_t bits);
|
||||
|
||||
bool is_any(uint32_t flags)
|
||||
{
|
||||
return xEventGroupGetBits(event_group) & flags;
|
||||
}
|
||||
// waiting for all and clearing if set
|
||||
bool wait(uint32_t flags, uint32_t time_ms);
|
||||
|
||||
bool wait_any(uint32_t flags, uint32_t time_ms) // waiting for any bit, not clearing them
|
||||
{
|
||||
EventBits_t bits = xEventGroupWaitBits(event_group, flags, pdFALSE, pdFALSE, pdMS_TO_TICKS(time_ms));
|
||||
return bits & flags;
|
||||
}
|
||||
bool is_any(uint32_t flags);
|
||||
|
||||
~signal_group()
|
||||
{
|
||||
if (event_group) vEventGroupDelete(event_group);
|
||||
}
|
||||
// waiting for any bit, not clearing them
|
||||
bool wait_any(uint32_t flags, uint32_t time_ms);
|
||||
|
||||
EventGroupHandle_t event_group;
|
||||
~signal_group();
|
||||
|
||||
private:
|
||||
Signal event_group;
|
||||
};
|
||||
|
||||
} // namespace esp_modem
|
||||
|
Reference in New Issue
Block a user