Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_ControlPacketAccessor.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
22//#define CS_CONTROL_PACKET_DEFAULT_PAYLOAD_SIZE (CS_CHAR_WRITE_BUF_SIZE - sizeof(control_packet_header_t))
24 (g_CS_CHAR_WRITE_BUF_SIZE - sizeof(control_packet_header_t));
25
26template <cs_buffer_size_t PAYLOAD_SIZE = CS_CONTROL_PACKET_DEFAULT_PAYLOAD_SIZE>
28public:
32 uint8_t getProtocolVersion() const {
34 return _buffer->header.protocolVersion;
35 }
36
40 void setProtocolVersion(uint8_t protocol) {
42 _buffer->header.protocolVersion = protocol;
43 }
44
50 return _buffer->header.commandType;
51 }
52
58 _buffer->header.commandType = type;
59 }
60
68 cs_data_t data;
69 data.data = _buffer->payload;
70 data.len = getPayloadSize();
71 return data;
72 }
73
83 return std::min(_buffer->header.payloadSize, PAYLOAD_SIZE);
84 }
85
93 return PAYLOAD_SIZE;
94 }
95
105 if (size > PAYLOAD_SIZE) {
108 }
109 if (size != 0 && payload == nullptr) {
110 LOGe("Null pointer");
112 }
113 _buffer->header.payloadSize = size;
114 if (size && _buffer->payload != payload) {
115 memmove(_buffer->payload, payload, size);
116 }
117 else {
118 LOGd("Skip copy");
119 }
120 return ERR_SUCCESS;
121 }
122
125 assert(sizeof(control_packet_header_t) + PAYLOAD_SIZE <= size, STR_ERR_BUFFER_NOT_LARGE_ENOUGH);
126 LOGd(FMT_ASSIGN_BUFFER_LEN, buffer, size);
128 return ERR_SUCCESS;
129 }
130
134 return sizeof(control_packet_header_t) + _buffer->header.payloadSize;
135 }
136
140 return sizeof(control_packet_header_t) + PAYLOAD_SIZE;
141 }
142
146 cs_data_t data;
147 data.data = (buffer_ptr_t)_buffer;
148 data.len = getSerializedSize();
149 return data;
150 }
151
152protected:
157
158 void checkInitialized() const { assert(_buffer != NULL, "Butter not initialized"); }
159
160private:
161};
Base class for a buffer accessor object.
Definition: cs_BufferAccessor.h:16
Definition: cs_ControlPacketAccessor.h:27
void checkInitialized() const
Definition: cs_ControlPacketAccessor.h:158
uint8_t getProtocolVersion() const
Get the protocol version.
Definition: cs_ControlPacketAccessor.h:32
void setProtocolVersion(uint8_t protocol)
Set the protocol version.
Definition: cs_ControlPacketAccessor.h:40
cs_ret_code_t assign(buffer_ptr_t buffer, cs_buffer_size_t size)
Assign the buffer used to hold the data.
Definition: cs_ControlPacketAccessor.h:124
void setType(cs_control_cmd_t type)
Set the command type.
Definition: cs_ControlPacketAccessor.h:56
cs_buffer_size_t getMaxPayloadSize()
Get the capacity of the payload.
Definition: cs_ControlPacketAccessor.h:91
cs_buffer_size_t getPayloadSize()
Get the size of the payload.
Definition: cs_ControlPacketAccessor.h:81
control_packet_t< PAYLOAD_SIZE > * _buffer
Pointer to the packet to be sent.
Definition: cs_ControlPacketAccessor.h:156
cs_control_cmd_t getType() const
Get the command type.
Definition: cs_ControlPacketAccessor.h:48
cs_buffer_size_t getBufferSize() const
Get the capacity of the buffer.
Definition: cs_ControlPacketAccessor.h:138
cs_ret_code_t setPayload(buffer_ptr_t payload, cs_buffer_size_t size)
Copy payload into the packet.
Definition: cs_ControlPacketAccessor.h:103
cs_data_t getPayload()
Get pointer to the payload.
Definition: cs_ControlPacketAccessor.h:66
cs_data_t getSerializedBuffer()
Get the pointer to the buffer.
Definition: cs_ControlPacketAccessor.h:144
cs_buffer_size_t getSerializedSize() const
Get the size of the serialized data in the buffer.
Definition: cs_ControlPacketAccessor.h:132
static constexpr size_t CS_CONTROL_PACKET_DEFAULT_PAYLOAD_SIZE
Default payload size for a control packet.
Definition: cs_ControlPacketAccessor.h:23
@ ERR_SUCCESS
Definition: cs_ErrorCodes.h:10
@ ERR_BUFFER_TOO_SMALL
Definition: cs_ErrorCodes.h:16
@ ERR_BUFFER_UNASSIGNED
Definition: cs_ErrorCodes.h:14
#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
#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
Header of a control packet.
Definition: cs_Packets.h:86
Control packet.
Definition: cs_Packets.h:96
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