BlueToe
an alternative GATT/BLE implementation
Loading...
Searching...
No Matches
buffer.hpp
1#ifndef BLUETOE_LINK_LAYER_BUFFER_HPP
2#define BLUETOE_LINK_LAYER_BUFFER_HPP
3
4#include <cstdlib>
5#include <cstdint>
6#include <cassert>
7#include <initializer_list>
8#include <algorithm>
9
10namespace bluetoe {
11namespace link_layer {
12
18 {
24 std::uint8_t* buffer;
25
30 std::size_t size;
31
35 bool empty() const
36 {
37 return buffer == nullptr && size == 0;
38 }
39
44 void fill( std::initializer_list< std::uint8_t > data )
45 {
46 assert( data.size() <= size );
47 std::copy( data.begin(), data.end(), buffer );
48 }
49 };
50
60 template < class Layout >
61 void fill( const read_buffer& buffer, std::initializer_list< std::uint8_t > data )
62 {
63 if ( data.size() >= 1 )
64 {
65 std::uint16_t header = *data.begin();
66
67 if ( data.size() >= 2 )
68 header |= *std::next( data.begin() ) << 8;
69
70 Layout::header( buffer, header );
71
72 if ( data.size() >= 3 )
73 {
74 std::uint8_t* body = Layout::body( buffer ).first;
75 std::copy( std::next( data.begin(), 2 ), std::end( data ), body );
76 }
77 }
78 }
79
85 {
87 const std::uint8_t* buffer;
88
90 std::size_t size;
91
95 bool empty() const
96 {
97 return buffer == nullptr && size == 0;
98 }
99
103 explicit write_buffer( const read_buffer& rhs )
104 : buffer( rhs.buffer )
105 , size( rhs.size )
106 {}
107
112 : buffer( nullptr )
113 , size( 0 )
114 {
115 }
116
120 write_buffer( const std::uint8_t* b, std::size_t s )
121 : buffer( b )
122 , size( s )
123 {
124 }
125 };
126}
127}
128
129#endif