Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_StackBuffer.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: May 27, 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__)) stack_buffer_t {
21 uint16_t length;
22 T array[1];
23};
24
28template <typename T, uint16_t S>
29struct __attribute__((__packed__)) stack_buffer_fixed_t {
30 uint16_t length;
31 T array[S];
32};
33
34template <typename T>
36public:
38
39 virtual ~StackBuffer() {}
40
41 uint16_t getMaxByteSize(uint16_t capacity) { return 2 + capacity * sizeof(T); }
42 uint16_t getMaxByteSize() { return getMaxByteSize(_capacity); }
43 uint16_t getMaxSize(uint16_t byteSize) { return (byteSize - 2) / sizeof(T); }
44
45 bool init() {
46 if (_buffer != NULL) {
47 return false;
48 }
49 // Allocate memory
50 _buffer = (stack_buffer_t<T>*)calloc(getMaxByteSize(), sizeof(uint8_t));
51 if (_buffer == NULL) {
52 LOGw("Could not allocate memory");
53 return false;
54 }
55 // LOGd("Allocated memory at %u", _buffer);
56 _allocatedSelf = true;
57 // Also call clear to make sure we start with a clean buffer
58 clear();
59 return true;
60 }
61
62 bool deinit() {
63 if (_buffer != NULL && _allocatedSelf) {
64 free(_buffer);
65 }
66 _allocatedSelf = false;
67 _buffer = NULL;
68 return true;
69 }
70
71 bool assign(buffer_ptr_t buffer, uint16_t bufferSize) {
72 if (getMaxSize(bufferSize) < _capacity || _allocatedSelf) {
73 LOGd("Could not assign at %u", buffer);
74 return false;
75 }
76 _buffer = (stack_buffer_t<T>*)buffer;
77 // LOGd("assign at %u array=%u", buffer, _buffer->array);
78 // Also call clear to make sure we start with a clean buffer
79 clear();
80 return true;
81 }
82
83 bool release() {
84 if (_allocatedSelf) {
85 return false;
86 }
87 _buffer = NULL;
88 return true;
89 }
90
92
93 void clear() { _buffer->length = 0; }
94
95 uint16_t size() const { return _buffer->length; }
96
97 uint16_t capacity() const { return _capacity; }
98
99 bool empty() const { return (_buffer->length == 0); }
100
101 bool full() const { return (size() >= _capacity); }
102
103 bool push(T value) {
104 if (full()) {
105 return false;
106 }
107 // LOGd("push %u at %u buffer=%u array=%u", value, _buffer->length, _buffer, _buffer->array);
108 _buffer->array[_buffer->length++] = value;
109 return true;
110 }
111
112 T pop() {
113 assert(!empty(), "Buffer is empty");
114 return _buffer->array[--(_buffer->length)];
115 }
116
117 T operator[](uint16_t idx) const {
118 assert(idx < size(), "Index too large");
119 // LOGd("get %u buffer=%u array=%u", idx, _buffer, _buffer->array);
120 return _buffer->array[idx];
121 }
122
123private:
125 uint16_t _capacity;
127};
Definition: cs_StackBuffer.h:35
stack_buffer_t< T > * _buffer
Definition: cs_StackBuffer.h:124
bool assign(buffer_ptr_t buffer, uint16_t bufferSize)
Definition: cs_StackBuffer.h:71
bool deinit()
Definition: cs_StackBuffer.h:62
uint16_t getMaxByteSize()
Definition: cs_StackBuffer.h:42
void clear()
Definition: cs_StackBuffer.h:93
uint16_t size() const
Definition: cs_StackBuffer.h:95
T operator[](uint16_t idx) const
Definition: cs_StackBuffer.h:117
bool empty() const
Definition: cs_StackBuffer.h:99
uint16_t capacity() const
Definition: cs_StackBuffer.h:97
virtual ~StackBuffer()
Definition: cs_StackBuffer.h:39
bool full() const
Definition: cs_StackBuffer.h:101
bool init()
Definition: cs_StackBuffer.h:45
bool release()
Definition: cs_StackBuffer.h:83
uint16_t _capacity
Definition: cs_StackBuffer.h:125
T pop()
Definition: cs_StackBuffer.h:112
StackBuffer(uint16_t capacity)
Definition: cs_StackBuffer.h:37
bool push(T value)
Definition: cs_StackBuffer.h:103
stack_buffer_t< T > * getBuffer()
Definition: cs_StackBuffer.h:91
uint16_t getMaxByteSize(uint16_t capacity)
Definition: cs_StackBuffer.h:41
uint16_t getMaxSize(uint16_t byteSize)
Definition: cs_StackBuffer.h:43
bool _allocatedSelf
Definition: cs_StackBuffer.h:126
#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_StackBuffer.h:29
uint16_t length
Definition: cs_StackBuffer.h:30
Struct with dynamic length, used by StackBuffer class.
Definition: cs_StackBuffer.h:20
uint16_t length
Definition: cs_StackBuffer.h:21