FastLED 3.10.6
Loading...
Searching...
No Matches
ble_notify_queue.h
Go to the documentation of this file.
1#pragma once
2
43
44#include "fl/stl/int.h"
45#include "fl/stl/vector.h"
46
47namespace fl {
48namespace net {
49namespace ble {
50
58 public:
66 static constexpr size_t kCapacity = 4;
67
68 private:
72 static constexpr size_t kSlots = kCapacity + 1;
73
74 static size_t next(size_t index) { return (index + 1) % kSlots; }
75
76 public:
81 bool push(const char *data, size_t size) {
82 if (data == nullptr || size == 0) {
83 return false;
84 }
85 const size_t tail = mTail;
86 const size_t advanced = next(tail);
87 if (advanced == mHead) {
88 return false; // full
89 }
90 fl::vector<u8> &slot = mSlots[tail];
91 slot.clear();
92 slot.reserve(size);
93 for (size_t i = 0; i < size; ++i) {
94 slot.push_back(static_cast<u8>(data[i]));
95 }
96 // Publish only after the slot is fully written.
97 mTail = advanced;
98 return true;
99 }
100
102 bool empty() const { return mHead == mTail; }
103
105 size_t depth() const {
106 const size_t head = mHead;
107 const size_t tail = mTail;
108 return (tail + kSlots - head) % kSlots;
109 }
110
112 size_t remaining() const {
113 if (empty()) {
114 return 0;
115 }
116 const size_t size = mSlots[mHead].size();
117 return mOffset >= size ? 0 : size - mOffset;
118 }
119
121 const u8 *chunkData() const {
122 if (remaining() == 0) {
123 return nullptr;
124 }
125 return mSlots[mHead].data() + mOffset;
126 }
127
132 size_t chunkSize(size_t max_payload) const {
133 if (max_payload == 0) {
134 return 0;
135 }
136 const size_t left = remaining();
137 return left < max_payload ? left : max_payload;
138 }
139
144 void advance(size_t sent) {
145 if (empty() || sent == 0) {
146 return;
147 }
148 const size_t head = mHead;
149 const size_t size = mSlots[head].size();
150 mOffset += sent;
151 if (mOffset >= size) {
152 mSlots[head].clear();
153 mOffset = 0;
154 // Release the slot last, so the producer cannot reuse it while
155 // it is still being cleared.
156 mHead = next(head);
157 }
158 }
159
164 void dropFront() {
165 if (empty()) {
166 return;
167 }
168 const size_t head = mHead;
169 mSlots[head].clear();
170 mOffset = 0;
171 mHead = next(head);
172 }
173
178 void clear() {
179 for (size_t i = 0; i < kSlots; ++i) {
180 mSlots[i].clear();
181 }
182 mHead = 0;
183 mTail = 0;
184 mOffset = 0;
185 }
186
187 private:
190 volatile size_t mHead = 0;
192 volatile size_t mTail = 0;
194 size_t mOffset = 0;
195};
196
197} // namespace ble
198} // namespace net
199} // namespace fl
bool push(const char *data, size_t size)
Append a complete payload.
void dropFront()
Drop the in-flight payload and move to the next one.
static constexpr size_t kCapacity
Maximum number of complete responses held at once.
size_t mOffset
Bytes of mSlots[mHead] already accepted. Consumer-only.
size_t chunkSize(size_t max_payload) const
Size of the next chunk, capped at max_payload.
const u8 * chunkData() const
Start of the next chunk, or nullptr when the queue is empty.
static constexpr size_t kSlots
One spare slot keeps "empty" and "full" distinguishable without a shared count, which is what makes t...
void advance(size_t sent)
Record that sent bytes were accepted by the stack.
fl::vector< u8 > mSlots[kSlots]
void clear()
Drop every queued payload — used when the link goes away.
size_t remaining() const
Bytes of the in-flight payload not yet accepted by the stack.
static size_t next(size_t index)
volatile size_t mHead
Owned by the consumer; read by the producer to detect "full".
size_t depth() const
Number of complete payloads queued, including the one in flight.
bool empty() const
True when nothing is waiting to be sent.
volatile size_t mTail
Owned by the producer; read by the consumer to detect "empty".
Bounded FIFO of outbound notification payloads, drained in MTU-sized chunks.
void reserve(fl::size n) FL_NO_EXCEPT
Definition vector.h:591
void clear() FL_NO_EXCEPT
Definition vector.h:634
void push_back(const T &value) FL_NO_EXCEPT
Definition vector.h:624
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179