1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
27
60
61
62
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
86
87
88
89
90
91
92
93
94
98
99
103
104
108
109
113
114
118
119
123
124
128
129
130
131
132
133
134
135
136
137
138
142
143
150
151
152
156
157
158
159
160
170
171
181
182
194
195
201
202
208
209
213
214
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
236
237
238
251
255
267
268
269
270
271
272
273
274
275
276
277
283
284
285
286
287
288
289
290
291
292
293
298
299
300
304
305
309
310
314
315
316
323
324
329
330
334
335
339
340
344
345
353
354
361
362
370
371
375
376
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* ... */
#include <algorithm>
#include <cstddef>
#include <exception>
#include <deque>
#include <cstdint>
#include <memory>
#include <ranges>
#include <utility>
#include <vector>
#include <string>
#include <memory_resource>
#include "esp_log.h"
#include "mqtt_outbox.h"13 includes
constexpr auto TAG = "custom_outbox";
/* ... */
class trace_resource : public std::pmr::memory_resource {
public:
explicit trace_resource(std::string resource_name, std::pmr::memory_resource *upstream_resource = std::pmr::get_default_resource()) : upstream{upstream_resource}, name{std::move(resource_name)} {}
[[nodiscard]] std::string_view get_name() const noexcept
{
return std::string_view(name);
}{...}
[[nodiscard]] auto upstream_resource() const
{
return upstream;
}{...}
private...:
void *do_allocate(std::size_t bytes, std::size_t alignment) override
{
auto *allocated = upstream->allocate(bytes, alignment);
allocated_total += bytes;
ESP_LOGI(name.c_str(), "%s: %zu bytes allocated, %zu total bytes in use", name.c_str(), bytes, allocated_total);
return allocated;
}{...}
void do_deallocate(void *ptr, std::size_t bytes, std::size_t alignment) override
{
upstream->deallocate(ptr, bytes, alignment);
ESP_LOGI(name.c_str(), "%s: %zu bytes deallocated, %zu total bytes in use", name.c_str(), bytes, allocated_total);
}{...}
[[nodiscard]] bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override
{
return this == &other;
}{...}
size_t allocated_total{};
std::pmr::memory_resource *upstream;
std::string name;...
}{...};
struct outbox_item {
/* ... */
using allocator_type = std::pmr::polymorphic_allocator<>;
enum class id_t : int {};
enum class type_t : int {};
enum class qos_t : int {};
outbox_item(
std::pmr::vector<uint8_t> message,
id_t msg_id,
type_t msg_type,
qos_t msg_qos,
outbox_tick_t tick,
pending_state_t pending_state,
allocator_type alloc = {}
) : message(std::move(message), alloc), id(msg_id), type(msg_type), qos(msg_qos), tick(tick), pending_state(pending_state) {}
outbox_item(const outbox_item &other, allocator_type alloc = {}) : message(other.message, alloc), id(other.id), type(other.type), qos(other.qos), tick(other.tick), pending_state(other.pending_state) {}
outbox_item(outbox_item &&other, allocator_type alloc) noexcept : message(std::move(other.message), alloc), id(other.id), type(other.type), qos(other.qos), tick(other.tick), pending_state(other.pending_state)
{}{ ... }
outbox_item(const outbox_item &) = default;
outbox_item(outbox_item &&other) = default;
outbox_item &operator=(const outbox_item &rhs) = default;
outbox_item &operator=(outbox_item &&other) = default;
~outbox_item() = default;
[[nodiscard]] auto state() const noexcept
{
return pending_state;
}{...}
[[nodiscard]] allocator_type get_allocator() const
{
return message.get_allocator();
}{...}
void set(pending_state state) noexcept
{
pending_state = state;
}{...}
void set(outbox_tick_t n_tick) noexcept
{
tick = n_tick;
}{...}
[[nodiscard]] auto get_id() const noexcept
{
return id;
}{...}
[[nodiscard]] auto get_type() const noexcept
{
return type;
}{...}
[[nodiscard]] auto get_tick() const noexcept
{
return tick;
}{...}
[[nodiscard]] auto get_data(size_t *len, uint16_t *msg_id, int *msg_type, int *msg_qos)
{
*len = message.size();
*msg_id = static_cast<uint16_t>(id);
*msg_type = static_cast<int>(type);
*msg_qos = static_cast<int>(qos);
return message.data();
}{ ... }
[[nodiscard]] auto get_size() const noexcept
{
return message.size();
}{...}
private:
std::pmr::vector<uint8_t> message;
id_t id;
type_t type;
qos_t qos;
outbox_tick_t tick;
pending_state_t pending_state;...
}{ ... };
/* ... */
struct outbox_t {
using allocator_type = std::pmr::polymorphic_allocator<>;
explicit outbox_t(allocator_type alloc = {}) : queue(alloc) {}
outbox_item_handle_t get(outbox_item::id_t msg_id)
{
if (auto item = std::ranges::find_if(queue, [msg_id](auto & item) {
return item.get_id() == msg_id;
}{...});
item != std::end(queue)) {
return &(*item);
}{...}
return nullptr;
}{ ... }
int delete_expired(outbox_tick_t current_tick, outbox_tick_t timeout)
{
return std::erase_if(queue, [current_tick, timeout, this](const outbox_item & item) {
if (current_tick - item.get_tick() > timeout) {
total_size -= item.get_size();
return true;
}{...}
return false;
}{...});
}{ ... }
outbox_item::id_t delete_single_expired(outbox_tick_t current_tick, outbox_tick_t timeout)
{
if (auto erase = std::ranges::find_if(queue, [current_tick, timeout](auto & item) {
return (current_tick - item.get_tick() > timeout);
}{...}); erase != std::end(queue)) {
auto msg_id = erase->get_id();
total_size -= erase->get_size();
queue.erase(erase);
return msg_id;
}{...}
return outbox_item::id_t{-1};
}{ ... }
auto erase(outbox_item_handle_t to_erase)
{
return erase_if([to_erase](auto & item) {
return &item == to_erase;
}{...});
}{ ... }
auto erase(outbox_item::id_t msg_id, outbox_item::type_t msg_type)
{
return erase_if([msg_id, msg_type](auto & item) {
return (item.get_id() == msg_id && (item.get_type() == msg_type));
}{...});
}{ ... }
[[nodiscard]] auto size() const noexcept
{
return total_size;
}{...}
void clear()
{
queue.clear();
}{ ... }
outbox_item_handle_t enqueue(outbox_message_handle_t message, outbox_tick_t tick) noexcept
{
try {
auto &item =
queue.emplace_back(std::pmr::vector<uint8_t> {message->data, message->data + message->len},
outbox_item::id_t{message->msg_id},
outbox_item::type_t{message->msg_type},
outbox_item::qos_t{message->msg_qos},
tick,
QUEUED
);
total_size += item.get_size();
ESP_LOGD(TAG, "ENQUEUE msgid=%d, msg_type=%d, len=%d, size=%" PRIu64, message->msg_id, message->msg_type, message->len + message->remaining_len, outbox_get_size(this));
return &item;
}{...} catch (const std::exception &e) {
return nullptr;
}{...}
}{...}
outbox_item_handle_t dequeue(pending_state_t state, outbox_tick_t *tick)
{
if (auto item = std::ranges::find_if(queue, [state](auto & item) {
return item.state() == state;
}{...});
item != std::end(queue)) {
if (tick != nullptr) {
*tick = item->get_tick();
}{...}
return &(*item);
}{...}
return nullptr;
}{ ... }
[[nodiscard]] allocator_type get_allocator() const
{
return queue.get_allocator();
}{...}
private:
[[nodiscard]] esp_err_t erase_if(std::predicate<outbox_item &> auto &&predicate)
{
if (auto to_erase = std::ranges::find_if(queue, predicate); to_erase != std::end(queue)) {
total_size -= to_erase->get_size();
queue.erase(to_erase);
return ESP_OK;
}{...}
return ESP_FAIL;
}{ ... }
std::size_t total_size{};
std::pmr::deque<outbox_item> queue ;...
}{ ... };
extern "C" {
outbox_handle_t outbox_init()
{
static constexpr auto work_memory_size = 16 * 1024;
static std::array<std::byte, work_memory_size> resource_buffer{};
try {
/* ... */
auto *monotonic_resource = new std::pmr::monotonic_buffer_resource{resource_buffer.data(), resource_buffer.size(), std::pmr::null_memory_resource()};
auto *trace_monotonic = new trace_resource("Monotonic", monotonic_resource);
auto *pool_resource = new std::pmr::unsynchronized_pool_resource{trace_monotonic};
auto *trace_pool = new trace_resource("Pool", pool_resource);
auto *outbox = new outbox_t{trace_pool};
return outbox;
}{...} catch (const std::exception &e) {
ESP_LOGD(TAG, "Not enough memory to construct the outbox, review the resource_buffer size");
return nullptr;
}{...}
}{ ... }
outbox_item_handle_t outbox_enqueue(outbox_handle_t outbox, outbox_message_handle_t message, outbox_tick_t tick)
{
return outbox->enqueue(message, tick);
}{ ... }
outbox_item_handle_t outbox_get(outbox_handle_t outbox, int msg_id)
{
return outbox->get(outbox_item::id_t{msg_id});
}{ ... }
outbox_item_handle_t outbox_dequeue(outbox_handle_t outbox, pending_state_t pending, outbox_tick_t *tick)
{
return outbox->dequeue(pending, tick);
}{ ... }
}{...}
uint8_t *outbox_item_get_data(outbox_item_handle_t item, size_t *len, uint16_t *msg_id, int *msg_type, int *qos)
{
if (item == nullptr) {
return nullptr;
}{...}
return item->get_data(len, msg_id, msg_type, qos);
}{ ... }
esp_err_t outbox_delete_item(outbox_handle_t outbox, outbox_item_handle_t item_to_delete)
{
return outbox->erase(item_to_delete);
}{ ... }
esp_err_t outbox_delete(outbox_handle_t outbox, int msg_id, int msg_type)
{
return outbox->erase(outbox_item::id_t{msg_id}, outbox_item::type_t{msg_type});
}{ ... }
int outbox_delete_single_expired(outbox_handle_t outbox, outbox_tick_t current_tick, outbox_tick_t timeout)
{
return static_cast<int>(outbox->delete_single_expired(current_tick, timeout));
}{ ... }
int outbox_delete_expired(outbox_handle_t outbox, outbox_tick_t current_tick, outbox_tick_t timeout)
{
return outbox->delete_expired(current_tick, timeout);
}{ ... }
esp_err_t outbox_set_pending(outbox_handle_t outbox, int msg_id, pending_state_t pending)
{
if (auto *item = outbox->get(outbox_item::id_t{msg_id}); item != nullptr) {
item->set(pending);
return ESP_OK;
}{...}
return ESP_FAIL;
}{ ... }
pending_state_t outbox_item_get_pending(outbox_item_handle_t item)
{
if (item != nullptr) {
return item->state();
}{...}
return QUEUED;
}{ ... }
esp_err_t outbox_set_tick(outbox_handle_t outbox, int msg_id, outbox_tick_t tick)
{
if (auto *item = outbox->get(outbox_item::id_t{msg_id}); item != nullptr) {
item->set(tick);
return ESP_OK;
}{...}
return ESP_FAIL;
}{ ... }
uint64_t outbox_get_size(outbox_handle_t outbox)
{
return outbox->size();
}{ ... }
void outbox_delete_all_items(outbox_handle_t outbox)
{
outbox->clear();
}{ ... }
void outbox_destroy(outbox_handle_t outbox)
{
auto *trace_pool = static_cast<trace_resource *>(outbox->get_allocator().resource());
auto *pool_resource = static_cast<std::pmr::unsynchronized_pool_resource *>(trace_pool->upstream_resource());
auto *trace_monotonic = static_cast<trace_resource *>(pool_resource->upstream_resource());
auto *monotonic_resource = static_cast<std::pmr::monotonic_buffer_resource *>(trace_monotonic->upstream_resource());
delete monotonic_resource;
delete trace_monotonic;
delete pool_resource;
delete trace_pool;
delete outbox;
}{ ... }