|
| 1 | +/* |
| 2 | + * PacketBuffer.h |
| 3 | + * Copyright (C) 2022-2025 Levi Gillis - All Rights Reserved |
| 4 | + * You may use, distribute and modify this code under the |
| 5 | + * terms of the GNU Lesser General Public License v3.0 license. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef PACKET_BUFFER_H_ |
| 9 | +#define PACKET_BUFFER_H_ |
| 10 | + |
| 11 | +#if defined(USBCON) && defined(PLUGGABLE_USB_ENABLED) |
| 12 | + |
| 13 | +#include "USBOptions.h" |
| 14 | +#include <stddef.h> |
| 15 | +#include <stdint.h> |
| 16 | + |
| 17 | +#if PACKETBUFFER_COUNT < 2 |
| 18 | + #warning "PacketBuffer is likely too small, expect issues" |
| 19 | +#endif |
| 20 | + |
| 21 | +class PacketBuffer { |
| 22 | + public: |
| 23 | + virtual uint8_t *PrepareWrite(uint32_t &len) = 0; |
| 24 | + virtual void CommitWrite(uint32_t len) = 0; |
| 25 | + virtual uint8_t *PrepareRead(uint32_t &len) = 0; |
| 26 | + virtual void CommitRead(uint32_t len) = 0; |
| 27 | + virtual uint32_t Read(uint8_t *data, uint32_t len) = 0; |
| 28 | + virtual uint32_t Write(uint8_t *data, uint32_t len) = 0; |
| 29 | + virtual bool isEmpty() = 0; |
| 30 | + virtual bool isFull() = 0; |
| 31 | + virtual void clear() = 0; |
| 32 | + virtual uint32_t available() = 0; |
| 33 | + virtual uint32_t availableToWrite() = 0; |
| 34 | + virtual ~PacketBuffer() = default; |
| 35 | +}; |
| 36 | + |
| 37 | +template <uint32_t size> |
| 38 | +struct USBD_HID_BufferItem { |
| 39 | + uint32_t len = 0; |
| 40 | + uint32_t pos = 0; |
| 41 | + uint8_t buf[size]; |
| 42 | + |
| 43 | + uint32_t Read(uint8_t *data, uint32_t length) |
| 44 | + { |
| 45 | + uint8_t read = min(Remaining(), length); |
| 46 | + if (read) { |
| 47 | + memcpy(data, buf + pos, read); |
| 48 | + pos += read; |
| 49 | + } |
| 50 | + return read; |
| 51 | + } |
| 52 | + uint32_t Write(uint8_t *data, uint32_t length) |
| 53 | + { |
| 54 | + uint8_t write = min(size, length); |
| 55 | + if (write) { |
| 56 | + memcpy(buf, data, write); |
| 57 | + len = write; |
| 58 | + pos = 0; |
| 59 | + } |
| 60 | + return write; |
| 61 | + } |
| 62 | + uint32_t Remaining() |
| 63 | + { |
| 64 | + return len - pos; |
| 65 | + } |
| 66 | + bool Empty() |
| 67 | + { |
| 68 | + return Remaining() <= 0; |
| 69 | + } |
| 70 | + void Clear() |
| 71 | + { |
| 72 | + len = 0; |
| 73 | + pos = 0; |
| 74 | + } |
| 75 | + void WriteLength(uint32_t length) |
| 76 | + { |
| 77 | + len = length; |
| 78 | + pos = 0; |
| 79 | + } |
| 80 | + void ReadLength(uint32_t length) |
| 81 | + { |
| 82 | + pos = min(pos + length, size); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +template <uint32_t buffersize, uint32_t capacity> |
| 87 | +class SplitPacketBuffer : public PacketBuffer { |
| 88 | + public: |
| 89 | + virtual uint8_t *PrepareWrite(uint32_t &len) |
| 90 | + { |
| 91 | + isPrepared = true; |
| 92 | + if (writeHead == readHead) { |
| 93 | + // overwrite last |
| 94 | + writeHead = prevUnsafeHead(writeHead); |
| 95 | + } |
| 96 | + len = min(buffersize, len); |
| 97 | + return buffer[writeHead].buf; |
| 98 | + } |
| 99 | + virtual void CommitWrite(uint32_t len) |
| 100 | + { |
| 101 | + if (isPrepared) { |
| 102 | + buffer[writeHead].WriteLength(len); |
| 103 | + writeHead = newWriteHead(); |
| 104 | + } |
| 105 | + } |
| 106 | + virtual uint8_t *PrepareRead(uint32_t &len) |
| 107 | + { |
| 108 | + isPrepared = true; |
| 109 | + if (!buffer[readHead].Remaining()) { |
| 110 | + readHead = newReadHead(); |
| 111 | + } |
| 112 | + len = min(len, buffer[readHead].Remaining()); |
| 113 | + return buffer[readHead].buf; |
| 114 | + } |
| 115 | + virtual void CommitRead(uint32_t len) |
| 116 | + { |
| 117 | + if (isPrepared) { |
| 118 | + buffer[readHead].ReadLength(len); |
| 119 | + if (buffer[readHead].Empty()) { |
| 120 | + readHead = newReadHead(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + uint32_t Read(uint8_t *data, uint32_t len) |
| 125 | + { |
| 126 | + if (!buffer[readHead].Remaining()) { |
| 127 | + readHead = newReadHead(); |
| 128 | + } |
| 129 | + uint32_t read = 0; |
| 130 | + if (buffer[readHead].Remaining()) { |
| 131 | + read = buffer[readHead].Read(data, len); |
| 132 | + if (buffer[readHead].Empty()) { |
| 133 | + readHead = newReadHead(); |
| 134 | + } |
| 135 | + } |
| 136 | + return read; |
| 137 | + } |
| 138 | + virtual uint32_t Write(uint8_t *data, uint32_t len) |
| 139 | + { |
| 140 | + uint32_t write = 0; |
| 141 | + if (writeHead == readHead) { |
| 142 | + // overwrite last |
| 143 | + writeHead = prevUnsafeHead(writeHead); |
| 144 | + } |
| 145 | + write = buffer[writeHead].Write(data, len); |
| 146 | + writeHead = newWriteHead(); |
| 147 | + return write; |
| 148 | + } |
| 149 | + virtual bool isEmpty() |
| 150 | + { |
| 151 | + return available() == 0; |
| 152 | + } |
| 153 | + virtual bool isFull() |
| 154 | + { |
| 155 | +#if PACKETBUFFER_ALLOW_OVERWRITE |
| 156 | + return false; |
| 157 | +#else |
| 158 | + return readHead == writeHead; |
| 159 | +#endif |
| 160 | + } |
| 161 | + void clear() |
| 162 | + { |
| 163 | + readHead = 0; |
| 164 | + writeHead = 1; |
| 165 | + } |
| 166 | + virtual uint32_t available() |
| 167 | + { |
| 168 | + if (!buffer[readHead].Remaining()) { |
| 169 | + readHead = newReadHead(); |
| 170 | + } |
| 171 | + return getAvailable(readHead, writeHead); |
| 172 | + } |
| 173 | + virtual uint32_t availableToWrite() |
| 174 | + { |
| 175 | + return readHead == writeHead ? 0 : capacity; |
| 176 | + } |
| 177 | + |
| 178 | + uint32_t getAvailable(int head, int otherhead) |
| 179 | + { |
| 180 | +#if PACKETBUFFER_USE_FAST_AVAILABLE |
| 181 | + return buffer[head].Remaining(); |
| 182 | +#else |
| 183 | + uint32_t16_t total = 0; |
| 184 | + auto ptr = head; |
| 185 | + auto endptr = otherhead; |
| 186 | + for (size_t i = 0; i != endptr; i++) { |
| 187 | + total += buffer[i].Remaining(); |
| 188 | + i = newUnsafeHead(ptr); |
| 189 | + } |
| 190 | + return total; |
| 191 | +#endif |
| 192 | + } |
| 193 | + |
| 194 | + private: |
| 195 | + USBD_HID_BufferItem<buffersize> buffer[capacity]; |
| 196 | + int readHead = 0; |
| 197 | + int writeHead = 1; |
| 198 | + bool isPrepared = false; |
| 199 | + int newReadHead() |
| 200 | + { |
| 201 | + auto result = nextUnsafeHead(readHead); |
| 202 | + if (result == writeHead) { |
| 203 | + return readHead; |
| 204 | + } |
| 205 | + return result; |
| 206 | + } |
| 207 | + int newWriteHead() |
| 208 | + { |
| 209 | + return nextUnsafeHead(writeHead); |
| 210 | + } |
| 211 | + int nextUnsafeHead(int current) |
| 212 | + { |
| 213 | + auto result = current + 1; |
| 214 | + if (result >= capacity) { |
| 215 | + result = 0; |
| 216 | + } |
| 217 | + return result; |
| 218 | + } |
| 219 | + int prevUnsafeHead(int current) |
| 220 | + { |
| 221 | + auto result = current - 1; |
| 222 | + if (result < 0) { |
| 223 | + result = capacity - 1; |
| 224 | + } |
| 225 | + return result; |
| 226 | + } |
| 227 | +}; |
| 228 | + |
| 229 | +#endif |
| 230 | +#endif // PACKET_BUFFER_H_ |
0 commit comments