-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketBuffer.h
199 lines (182 loc) · 5.11 KB
/
PacketBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Arduino.h"
#include <memory>
class PacketBuffer {
public:
PacketBuffer(){};
PacketBuffer(const PacketBuffer &obj)
: _bytesTransferred(obj._bytesTransferred),
_bytesToTransfer(obj._bytesToTransfer),
_validityTs(obj._validityTs) {
allocate(obj._size);
memcpy(_buffer.get(), obj._buffer.get(), obj._size);
};
virtual ~PacketBuffer() = default;
void setValidityTs(uint32_t ts) {
_validityTs = ts;
};
uint32_t getValidityTs() {
return _validityTs;
};
//Return the pointer to the internal buffer at specified index. Do not use this method to modify the buffer, only for reading
const uint8_t *get_ptrAt(size_t idx){
return idx < _size ? &get_ptr()[idx] : get_ptr();
}
//Return the pointer to the internal buffer. Do not use this method to modify the buffer, only for reading
const uint8_t *get_ptr(){
return _buffer.get();
}
//Operator overloaded for reading and updating the value a single byte of the buffer
uint8_t &operator[](size_t idx) {
return idx < _size ? _buffer[idx] : _buffer[0];
};
virtual PacketBuffer &operator+=(uint8_t newChar) = 0;
//Append srcBuf to the internal buffer
virtual bool copyArray(const uint8_t *srcBuf, size_t len) = 0;
//Copy the srcBuf at the indicated position of the internal buffer
virtual bool copyArray(int positionFrom, const uint8_t *srcBuf, size_t len) = 0;
PacketBuffer &operator=(const PacketBuffer &msg) {
_bytesTransferred = msg._bytesTransferred;
_bytesToTransfer = msg._bytesToTransfer;
_validityTs = msg._validityTs;
allocate(msg._size);
memcpy(_buffer.get(), msg._buffer.get(), msg._size);
}
void allocate(size_t n) {
_buffer = std::unique_ptr<uint8_t[]>(new uint8_t[n]);
_size = n;
}
void reset() {
if (_buffer) {
_buffer.reset();
}
_size = 0;
_bytesTransferred = 0;
_bytesToTransfer = 0;
_validityTs = 0;
}
protected:
bool hasPendingBytes() {
return _bytesToTransfer - _bytesTransferred > 0;
};
uint32_t transferredBytes() {
return _bytesTransferred;
};
uint32_t pendingBytes() {
return _bytesToTransfer - _bytesTransferred;
};
uint32_t bytesToTransfer() {
return _bytesToTransfer;
};
void setBytesTransferred(int n) {
_bytesTransferred = n;
};
void setBytesToTransfer(int n) {
_bytesToTransfer = n;
};
size_t size() {
return _size;
};
bool setBytes(int position, uint8_t *newChar, size_t len) {
if (position + len <= _size) {
memcpy(&_buffer[position], newChar, len);
return true;
}
return false;
}
private:
std::unique_ptr<uint8_t[]> _buffer;
size_t _size = 0;
uint32_t _bytesTransferred = 0;
uint32_t _bytesToTransfer = 0;
uint32_t _validityTs = 0;
};
class OutputPacketBuffer : public PacketBuffer {
public:
OutputPacketBuffer(){};
uint32_t len() {
return bytesToTransfer();
};
uint32_t bytesSent() {
return transferredBytes();
};
uint32_t bytesToSend() {
return pendingBytes();
};
bool hasBytesToSend() {
return hasPendingBytes();
};
void bytesSent(int n) {
setBytesTransferred(n);
};
void incrementBytesSent(int n) {
setBytesTransferred(transferredBytes() + n);
};
void startProgress() {
setBytesTransferred(0);
};
void clear() {
setBytesTransferred(0);
};
OutputPacketBuffer &operator+=(uint8_t newChar) {
copyArray(bytesToTransfer(), &newChar, sizeof(newChar));
}
bool copyArray(const uint8_t *srcBuf, size_t len) {
copyArray(bytesToTransfer(), srcBuf, len);
}
bool copyArray(int positionFrom, const uint8_t *srcBuf, size_t len) override {
int nextOccupation = bytesToTransfer() + len;
bool success = false;
if (nextOccupation <= size()) {
setBytes(positionFrom, const_cast<uint8_t *>(srcBuf), len);
setBytesToTransfer(nextOccupation);
success = true;
}
return success;
}
};
class InputPacketBuffer : public PacketBuffer {
public:
InputPacketBuffer(){};
InputPacketBuffer(int n) {
allocate(n);
setPayloadLen(n);
};
bool receivedAll() {
return !hasPendingBytes() && bytesToTransfer() > 0;
};
uint32_t missingBytes() {
return pendingBytes();
};
uint32_t len() {
return transferredBytes();
};
void setPayloadLen(uint32_t len) {
setBytesToTransfer(len);
};
void clear() {
setBytesTransferred(0);
};
InputPacketBuffer &operator+=(uint8_t newChar) {
copyArray(transferredBytes(), &newChar, sizeof(newChar));
}
bool copyArray(const uint8_t *srcBuf, size_t len) {
copyArray(transferredBytes(), srcBuf, len);
}
bool copyArray(int positionFrom, const uint8_t *srcBuf, size_t len) override {
int nextOccupation = transferredBytes() + len;
bool success = false;
if (nextOccupation <= size()) {
setBytes(positionFrom, const_cast<uint8_t *>(srcBuf), len);
setBytesTransferred(nextOccupation);
success = true;
}
return success;
}
};