Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_ResultPacketAccessor.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Oct 23, 2019
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
9#include <cfg/cs_StaticConfig.h>
10#include <logging/cs_Logger.h>
14#include <util/cs_Error.h>
15
16#include <cstdint>
17
22static const size_t CS_RESULT_PACKET_DEFAULT_PAYLOAD_SIZE = (g_CS_CHAR_READ_BUF_SIZE - sizeof(result_packet_header_t));
23
24template <cs_buffer_size_t PAYLOAD_SIZE = CS_RESULT_PACKET_DEFAULT_PAYLOAD_SIZE>
26public:
30 bool isInitialized() const { return _buffer != NULL; }
31
35 uint8_t getProtocolVersion() const {
37 return _buffer->header.protocolVersion;
38 }
39
43 void setProtocolVersion(uint8_t protocol) {
45 _buffer->header.protocolVersion = protocol;
46 }
47
53 return _buffer->header.commandType;
54 }
55
61 _buffer->header.commandType = type;
62 }
63
69 return _buffer->header.returnCode;
70 }
71
75 void setResult(cs_ret_code_t result) {
77 _buffer->header.returnCode = result;
78 }
79
87 cs_data_t data;
88 data.data = _buffer->payload;
89 data.len = getPayloadSize();
90 return data;
91 }
92
102 return std::min(_buffer->header.payloadSize, PAYLOAD_SIZE);
103 }
104
112 return _buffer->payload;
113 }
114
122 return PAYLOAD_SIZE;
123 }
124
127 if (size > getMaxPayloadSize()) {
129 }
130 _buffer->header.payloadSize = size;
131 return ERR_SUCCESS;
132 }
133
143 if (size > PAYLOAD_SIZE) {
146 }
147 _buffer->header.payloadSize = size;
148 memcpy(_buffer->payload, payload, size);
149 return ERR_SUCCESS;
150 }
151
154 assert(sizeof(result_packet_header_t) + PAYLOAD_SIZE <= size, STR_ERR_BUFFER_NOT_LARGE_ENOUGH);
155 LOGd(FMT_ASSIGN_BUFFER_LEN, buffer, size);
157 return ERR_SUCCESS;
158 }
159
163 return sizeof(result_packet_header_t) + _buffer->header.payloadSize;
164 }
165
169 return sizeof(result_packet_header_t) + PAYLOAD_SIZE;
170 }
171
175 cs_data_t data;
176 data.data = (buffer_ptr_t)_buffer;
177 data.len = getSerializedSize();
178 return data;
179 }
180
181protected:
186
187 void checkInitialized() const { assert(isInitialized(), "Buffer not initialized"); }
188
189private:
190};
Base class for a buffer accessor object.
Definition: cs_BufferAccessor.h:16
Definition: cs_ResultPacketAccessor.h:25
bool isInitialized() const
Only set or get fields when this instance is initialized.
Definition: cs_ResultPacketAccessor.h:30
cs_control_cmd_t getType() const
Get the command type.
Definition: cs_ResultPacketAccessor.h:51
cs_ret_code_t setPayloadSize(cs_buffer_size_t size)
Definition: cs_ResultPacketAccessor.h:125
cs_buffer_size_t getPayloadSize()
Get the size of the payload.
Definition: cs_ResultPacketAccessor.h:100
cs_data_t getPayload()
Get the payload.
Definition: cs_ResultPacketAccessor.h:85
cs_buffer_size_t getBufferSize() const
Get the capacity of the buffer.
Definition: cs_ResultPacketAccessor.h:167
cs_buffer_size_t getSerializedSize() const
Get the size of the serialized data in the buffer.
Definition: cs_ResultPacketAccessor.h:161
void setProtocolVersion(uint8_t protocol)
Set the protocol version.
Definition: cs_ResultPacketAccessor.h:43
buffer_ptr_t getPayloadBuffer()
Get the payload buffer.
Definition: cs_ResultPacketAccessor.h:110
void setResult(cs_ret_code_t result)
Set the result code.
Definition: cs_ResultPacketAccessor.h:75
result_packet_t< PAYLOAD_SIZE > * _buffer
Pointer to the packet to be sent.
Definition: cs_ResultPacketAccessor.h:185
cs_ret_code_t getResult() const
Get the result code.
Definition: cs_ResultPacketAccessor.h:67
void checkInitialized() const
Definition: cs_ResultPacketAccessor.h:187
cs_ret_code_t setPayload(buffer_ptr_t payload, cs_buffer_size_t size)
Copy payload into the packet.
Definition: cs_ResultPacketAccessor.h:141
void setType(cs_control_cmd_t type)
Set the command type.
Definition: cs_ResultPacketAccessor.h:59
cs_data_t getSerializedBuffer()
Get the pointer to the buffer.
Definition: cs_ResultPacketAccessor.h:173
uint8_t getProtocolVersion() const
Get the protocol version.
Definition: cs_ResultPacketAccessor.h:35
cs_buffer_size_t getMaxPayloadSize()
Get the capacity of the payload.
Definition: cs_ResultPacketAccessor.h:120
cs_ret_code_t assign(buffer_ptr_t buffer, cs_buffer_size_t size)
Assign the buffer used to hold the data.
Definition: cs_ResultPacketAccessor.h:153
@ ERR_SUCCESS
Definition: cs_ErrorCodes.h:10
@ ERR_BUFFER_TOO_SMALL
Definition: cs_ErrorCodes.h:16
#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 LOGd(fmt,...)
Definition: cs_Logger.h:90
#define LOGe(fmt,...)
Definition: cs_Logger.h:93
static const size_t CS_RESULT_PACKET_DEFAULT_PAYLOAD_SIZE
Default payload size for a result packet.
Definition: cs_ResultPacketAccessor.h:22
#define STR_ERR_BUFFER_NOT_LARGE_ENOUGH
Definition: cs_Strings.h:101
#define FMT_ASSIGN_BUFFER_LEN
Definition: cs_Strings.h:188
uint16_t cs_buffer_size_t
Definition: cs_Typedefs.h:20
uint16_t cs_control_cmd_t
Definition: cs_Typedefs.h:22
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
cs_buffer_size_t len
< Pointer to data.
Definition: cs_PacketsInternal.h:29
buffer_ptr_t data
Definition: cs_PacketsInternal.h:28
Header of a result packet.
Definition: cs_Packets.h:104
Result packet.
Definition: cs_Packets.h:120