Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_DifferentialBuffer.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: May 25, 2016
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
10#include <logging/cs_Logger.h>
11
12#include <cstdlib>
13
14#include "common/cs_Types.h"
15#include "util/cs_BleError.h"
16
19template <typename T>
20struct __attribute__((__packed__)) differential_buffer_t {
22 uint16_t length;
23
26
29
31 int8_t array[1]; // Dummy length
32};
33
37template <typename T, uint16_t S>
38struct __attribute__((__packed__)) differential_buffer_fixed_t {
40 uint16_t length;
41
44
47
49 int8_t array[S - 1];
50};
51
58template <typename T>
60public:
62
64
65 uint16_t getMaxByteSize(uint16_t capacity) { return 2 + 2 * sizeof(T) + capacity - 1; }
66 uint16_t getMaxByteSize() { return getMaxByteSize(_capacity); }
67 uint16_t getMaxSize(uint16_t byteSize) { return byteSize - 2 - 2 * sizeof(T) + 1; }
68
69 bool init() {
70 if (_buffer != NULL) {
71 return false;
72 }
73 // Allocate memory
74 _buffer = (differential_buffer_t<T>*)calloc(getMaxByteSize(), sizeof(uint8_t));
75 if (_buffer == NULL) {
76 LOGw("Could not allocate memory");
77 return false;
78 }
79 // LOGd("Allocated memory at %u", _buffer);
80 _allocatedSelf = true;
81 // Also call clear to make sure we start with a clean buffer
82 clear();
83 return true;
84 }
85
86 bool deinit() {
87 if (_buffer != NULL && _allocatedSelf) {
88 free(_buffer);
89 }
90 _allocatedSelf = false;
91 _buffer = NULL;
92 return true;
93 }
94
95 bool assign(buffer_ptr_t buffer, uint16_t bufferSize) {
96 if (getMaxSize(bufferSize) < _capacity || _allocatedSelf) {
97 LOGd("Could not assign at %u", buffer);
98 LOGd("%u < %u", getMaxSize(bufferSize), _capacity);
99 return false;
100 }
101 // LOGd("assign at %u", buffer);
103 // Also call clear to make sure we start with a clean buffer
104 clear();
105 return true;
106 }
107
108 bool release() {
109 if (_allocatedSelf) {
110 return false;
111 }
112 _buffer = NULL;
113 return true;
114 }
115
117
118 void clear() { _buffer->length = 0; }
119
120 uint16_t size() const { return _buffer->length; }
121
122 uint16_t capacity() const { return _capacity; }
123
124 bool empty() const { return _buffer->length == 0; }
125
126 bool full() const { return size() >= _capacity; }
127
128 bool push(T value) {
129 if (full()) {
130 return false;
131 }
132 // LOGd("push %u at %u buffer=%u", value, _buffer->length, _buffer);
133 if (empty()) {
134 _buffer->firstVal = value;
135 _buffer->lastVal = value;
136 _buffer->length = 1;
137 return true;
138 }
139
140 int32_t diff = (int32_t)value - _buffer->lastVal;
141 // LOGd("diff=%i", diff);
142 if (diff > 127 || diff < -127) {
143 LOGw("diff too large! %u - %u", value, _buffer->lastVal);
144 clear();
145 return false;
146 }
147 _buffer->array[_buffer->length - 1] = diff;
148 _buffer->length++;
149 _buffer->lastVal = value;
150 return true;
151 }
152
153 T pop() {
154 assert(!empty(), "Buffer is empty");
155 T value = _buffer->lastVal;
156 if (size() == 1) {
157 clear();
158 }
159 else {
160 _buffer->lastVal -= _buffer->array[_buffer->length-- - 1];
161 }
162 return value;
163 }
164
165 T peekBack() const {
166 assert(!empty(), "Buffer is empty");
167 return _buffer->lastVal;
168 }
169
170 T peekFront() const {
171 assert(!empty(), "Buffer is empty");
172 return _buffer->firstVal;
173 }
174
175 bool getValue(T& value, const uint16_t index) const {
176 if (index >= size()) {
177 return false;
178 }
179 if (index == 0) {
180 // LOGd("getVal %u", index);
181 value = _buffer->firstVal;
182 return true;
183 }
184 // LOGd("getVal %u diff=%i buffer=%u array=%u", index, _buffer->array[index - 1], _buffer, _buffer->array);
185 value += _buffer->array[index - 1];
186 return true;
187 }
188
189private:
191 uint16_t _capacity;
193};
Differential Buffer implementation.
Definition: cs_DifferentialBuffer.h:59
uint16_t getMaxByteSize(uint16_t capacity)
Definition: cs_DifferentialBuffer.h:65
DifferentialBuffer(uint16_t capacity)
Definition: cs_DifferentialBuffer.h:61
bool deinit()
Definition: cs_DifferentialBuffer.h:86
uint16_t size() const
Definition: cs_DifferentialBuffer.h:120
bool push(T value)
Definition: cs_DifferentialBuffer.h:128
bool full() const
Definition: cs_DifferentialBuffer.h:126
bool release()
Definition: cs_DifferentialBuffer.h:108
T peekFront() const
Definition: cs_DifferentialBuffer.h:170
bool _allocatedSelf
Definition: cs_DifferentialBuffer.h:192
uint16_t _capacity
Definition: cs_DifferentialBuffer.h:191
bool getValue(T &value, const uint16_t index) const
Definition: cs_DifferentialBuffer.h:175
virtual ~DifferentialBuffer()
Definition: cs_DifferentialBuffer.h:63
bool empty() const
Definition: cs_DifferentialBuffer.h:124
T pop()
Definition: cs_DifferentialBuffer.h:153
void clear()
Definition: cs_DifferentialBuffer.h:118
uint16_t capacity() const
Definition: cs_DifferentialBuffer.h:122
T peekBack() const
Definition: cs_DifferentialBuffer.h:165
uint16_t getMaxByteSize()
Definition: cs_DifferentialBuffer.h:66
differential_buffer_t< T > * _buffer
Definition: cs_DifferentialBuffer.h:190
bool assign(buffer_ptr_t buffer, uint16_t bufferSize)
Definition: cs_DifferentialBuffer.h:95
uint16_t getMaxSize(uint16_t byteSize)
Definition: cs_DifferentialBuffer.h:67
differential_buffer_t< T > * getBuffer()
Definition: cs_DifferentialBuffer.h:116
bool init()
Definition: cs_DifferentialBuffer.h:69
#define assert(expr, message)
Author: Crownstone Team Date: 21 Sep., 2013 License: LGPLv3+, Apache License 2.0, and/or MIT (triple-...
Definition: cs_Error.h:22
#define LOGw(fmt,...)
Definition: cs_Logger.h:92
#define LOGd(fmt,...)
Definition: cs_Logger.h:90
uint8_t * buffer_ptr_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 10 May....
Definition: cs_Typedefs.h:19
Struct with fixed length, useful when sending as payload.
Definition: cs_DifferentialBuffer.h:38
T firstVal
Value of the oldest element in the buffer.
Definition: cs_DifferentialBuffer.h:43
T lastVal
Value of the newest element in the buffer.
Definition: cs_DifferentialBuffer.h:46
uint16_t length
Number of elements stored in this struct.
Definition: cs_DifferentialBuffer.h:40
Struct with dynamic length, used by StackBuffer class.
Definition: cs_DifferentialBuffer.h:20
uint16_t length
Number of elements stored in this struct.
Definition: cs_DifferentialBuffer.h:22
T firstVal
Value of the oldest element in the buffer.
Definition: cs_DifferentialBuffer.h:25
T lastVal
Value of the newest element in the buffer.
Definition: cs_DifferentialBuffer.h:28