Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_StreamBufferAccessor.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Apr 3, 2020
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
11
52public:
54 StreamBufferAccessor(cs_data_t data) : StreamBufferAccessor(data.data, data.len) {}
55
61 void reset() {
64 }
65
72 buff_begin = buffer;
73 buff_end = buffer + size;
74 reset();
75
76 return ERR_SUCCESS;
77 }
78
81
84
87
90
91 // ============ Get ============
92
100 template <class T>
101 StreamBufferAccessor& get(T*& packet_ptr) {
102 if (!status_check()) {
103 // failed status check
104 // ignore get request when failure
105 return *this;
106 }
107
108 if (!size_check<T>()) {
109 // failed size check
110 packet_ptr = nullptr;
112 }
113 else {
114 // write to the out-parameter packet_ptr
115 packet_ptr = reinterpret_cast<T*>(buff_curr);
116 increment_buff<T>();
117 }
118
119 return *this;
120 }
121
128 template <class T>
129 T get() {
130 if (!size_check<T>() && !status_check()) {
131 increment_buff<T>();
132 return *reinterpret_cast<T*>(buff_curr);
133 }
134
135 // try to return a default when possible
137 return T();
138 }
139
140 // ============ Set ============
141
146 template <class T>
147 void put(T packet) {
148 if (size_check<T>() && status_check()) {
149 // copy into the buffer, after that, increment
150 *reinterpret_cast<T*>(buff_curr) = packet;
151 increment_buff<T>();
152 }
153 else {
154 // failed size_check
156 }
157 }
158
159protected:
160 // returns false on failure
161 template <class T>
162 inline bool size_check() {
163 return buff_curr + sizeof(T) <= buff_end;
164 }
165
166 template <class T>
167 inline void increment_buff() {
168 buff_curr += sizeof(T);
169 }
170
171 // returns false on failure
172 inline bool status_check() { return internal_status == ERR_SUCCESS; }
173
174private:
178
180};
A wrapper around a raw uint8_t array (buffer) to easily read, write from it.
Definition: cs_StreamBufferAccessor.h:51
cs_buffer_size_t getSerializedSize() const
Definition: cs_StreamBufferAccessor.h:80
StreamBufferAccessor & get(T *&packet_ptr)
If the stream buffer is in state OK, and remaining packet size is available, assign the packet_ptr to...
Definition: cs_StreamBufferAccessor.h:101
StreamBufferAccessor(cs_data_t data)
Definition: cs_StreamBufferAccessor.h:54
cs_buffer_size_t getBufferSize() const
Definition: cs_StreamBufferAccessor.h:83
buffer_ptr_t buff_curr
Definition: cs_StreamBufferAccessor.h:176
void reset()
Resets the stream buffer accessor to construction state, such that the next operation will read/write...
Definition: cs_StreamBufferAccessor.h:61
cs_ret_code_t assign(buffer_ptr_t buffer, cs_buffer_size_t size)
Binds this stream buffer accessor to the given buffer pointer and tell it that there are size bytes a...
Definition: cs_StreamBufferAccessor.h:71
cs_buffer_size_t getRemainingCapacity() const
Definition: cs_StreamBufferAccessor.h:86
buffer_ptr_t buff_end
Definition: cs_StreamBufferAccessor.h:177
buffer_ptr_t buff_begin
Definition: cs_StreamBufferAccessor.h:175
cs_ret_code_t internal_status
Definition: cs_StreamBufferAccessor.h:179
StreamBufferAccessor(buffer_ptr_t buffer, cs_buffer_size_t size)
Definition: cs_StreamBufferAccessor.h:53
bool size_check()
Definition: cs_StreamBufferAccessor.h:162
void put(T packet)
Put packet into the stream, if possible.
Definition: cs_StreamBufferAccessor.h:147
void increment_buff()
Definition: cs_StreamBufferAccessor.h:167
T get()
Same as the pointer version of get, but this will copy its value into the lvalue its return value is ...
Definition: cs_StreamBufferAccessor.h:129
cs_data_t getSerializedBuffer()
Definition: cs_StreamBufferAccessor.h:89
bool status_check()
Definition: cs_StreamBufferAccessor.h:172
@ ERR_SUCCESS
Definition: cs_ErrorCodes.h:10
@ ERR_NO_SPACE
Definition: cs_ErrorCodes.h:25
uint16_t cs_buffer_size_t
Definition: cs_Typedefs.h:20
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
uint8_t * buffer_ptr_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 10 May....
Definition: cs_Typedefs.h:19
Packets (structs) that are used internally in the firmware, and can be changed freely.
Definition: cs_PacketsInternal.h:27