2021-03-29 19:34:45 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2022-10-11 16:31:57 +02:00
|
|
|
*
|
2021-03-29 19:34:45 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2021-05-26 15:57:25 +02:00
|
|
|
#pragma once
|
2021-05-21 16:05:37 +02:00
|
|
|
|
|
|
|
#include "esp_event.h"
|
2021-04-04 22:15:46 +02:00
|
|
|
#include "esp_modem_exception.hpp"
|
2021-03-06 16:56:50 +01:00
|
|
|
|
2021-04-04 22:15:46 +02:00
|
|
|
#if defined(CONFIG_IDF_TARGET_LINUX)
|
|
|
|
#include <mutex>
|
2021-05-13 07:28:05 +02:00
|
|
|
#include <thread>
|
|
|
|
|
2021-03-16 21:36:13 +01:00
|
|
|
#else
|
2021-11-17 09:50:10 +05:30
|
|
|
#include "freertos/event_groups.h"
|
2021-04-04 22:15:46 +02:00
|
|
|
// forward declarations of FreeRTOS primitives
|
|
|
|
struct QueueDefinition;
|
2021-03-16 21:36:13 +01:00
|
|
|
#endif
|
2021-02-26 18:32:15 +01:00
|
|
|
|
|
|
|
|
2021-04-04 22:15:46 +02:00
|
|
|
namespace esp_modem {
|
|
|
|
|
2021-05-13 07:28:05 +02:00
|
|
|
// Forward declaration for both linux/FreeRTOS targets
|
|
|
|
//
|
2021-06-01 10:21:51 +02:00
|
|
|
using TaskFunction_t = void (*)(void *);
|
2021-04-04 22:15:46 +02:00
|
|
|
#if !defined(CONFIG_IDF_TARGET_LINUX)
|
2021-03-06 16:56:50 +01:00
|
|
|
struct Lock {
|
2021-05-21 16:05:37 +02:00
|
|
|
using MutexT = QueueHandle_t;
|
2021-04-04 22:15:46 +02:00
|
|
|
explicit Lock();
|
|
|
|
~Lock();
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
private:
|
|
|
|
MutexT m{};
|
2021-03-06 16:56:50 +01:00
|
|
|
};
|
2021-11-08 10:46:53 +05:30
|
|
|
using TaskT = TaskHandle_t;
|
|
|
|
using SignalT = EventGroupHandle_t;
|
2021-04-04 22:15:46 +02:00
|
|
|
#else
|
2022-06-10 18:04:10 +02:00
|
|
|
using Lock = std::recursive_mutex;
|
2021-05-13 07:28:05 +02:00
|
|
|
struct SignalGroupInternal;
|
|
|
|
using SignalT = std::unique_ptr<SignalGroupInternal>;
|
|
|
|
using TaskT = std::thread;
|
|
|
|
static constexpr uint32_t portMAX_DELAY = UINT32_MAX;
|
2021-04-04 22:15:46 +02:00
|
|
|
#endif
|
|
|
|
|
2021-03-06 16:56:50 +01:00
|
|
|
template<class T>
|
|
|
|
class Scoped {
|
|
|
|
public:
|
2021-06-01 10:21:51 +02:00
|
|
|
explicit Scoped(T &l): lock(l)
|
|
|
|
{
|
|
|
|
lock.lock();
|
|
|
|
}
|
|
|
|
~Scoped()
|
|
|
|
{
|
|
|
|
lock.unlock();
|
|
|
|
}
|
2021-03-06 16:56:50 +01:00
|
|
|
|
|
|
|
private:
|
2021-06-01 10:21:51 +02:00
|
|
|
T &lock;
|
2021-03-06 16:56:50 +01:00
|
|
|
};
|
|
|
|
|
2021-05-13 07:28:05 +02:00
|
|
|
class Task {
|
|
|
|
public:
|
|
|
|
explicit Task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t task_function);
|
|
|
|
~Task();
|
|
|
|
|
|
|
|
static void Delete();
|
|
|
|
static void Relinquish();
|
2021-11-12 20:21:10 +01:00
|
|
|
static void Delay(uint32_t delay);
|
2021-05-13 07:28:05 +02:00
|
|
|
private:
|
|
|
|
TaskT task_handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SignalGroup {
|
|
|
|
public:
|
2021-04-04 22:15:46 +02:00
|
|
|
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;
|
|
|
|
|
2021-05-13 07:28:05 +02:00
|
|
|
explicit SignalGroup();
|
2021-04-04 22:15:46 +02:00
|
|
|
|
|
|
|
void set(uint32_t bits);
|
|
|
|
|
|
|
|
void clear(uint32_t bits);
|
|
|
|
|
|
|
|
// waiting for all and clearing if set
|
|
|
|
bool wait(uint32_t flags, uint32_t time_ms);
|
|
|
|
|
|
|
|
bool is_any(uint32_t flags);
|
|
|
|
|
|
|
|
// waiting for any bit, not clearing them
|
|
|
|
bool wait_any(uint32_t flags, uint32_t time_ms);
|
|
|
|
|
2021-05-13 07:28:05 +02:00
|
|
|
~SignalGroup();
|
2021-04-04 22:15:46 +02:00
|
|
|
|
|
|
|
private:
|
2021-05-13 07:28:05 +02:00
|
|
|
SignalT event_group;
|
2021-02-26 18:32:15 +01:00
|
|
|
};
|
|
|
|
|
2021-03-29 19:34:45 +02:00
|
|
|
} // namespace esp_modem
|