emlabcpp
modern opinionated embedded C++ library
packet.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./sequencer.h"
27 #include "./serializer.h"
28 #include "./tuple.h"
29 
30 namespace emlabcpp::protocol
31 {
32 
33 template < typename T >
34 concept packet_def = requires( T t ) {
35  { T::endianess } -> std::convertible_to< std::endian >;
36  is_std_array_v< std::decay_t< decltype( T::prefix ) > >;
37  requires convertible< typename T::size_type >;
38  requires convertible< typename T::checksum_type >;
39 };
40 
41 template < typename Def, typename Payload >
43  Def::endianess,
44  std::decay_t< decltype( Def::prefix ) >,
46  typename Def::checksum_type >;
47 
48 template < packet_def Def, typename Payload >
49 struct packet : packet_base< Def, Payload >
50 {
51  using payload_type = Payload;
53 
54  static constexpr auto prefix = Def::prefix;
55  static constexpr auto endianess = Def::endianess;
56 
57  using prefix_type = std::decay_t< decltype( prefix ) >;
59  static constexpr std::size_t prefix_size = prefix_traits::max_size;
60 
61  using size_type = typename Def::size_type;
63  static constexpr std::size_t size_size = size_traits::max_size;
64 
67 
68  using checksum_type = typename Def::checksum_type;
70 
71  static_assert( fixedly_sized< size_type > );
72 
74  {
75  using message_type = typename base::message_type;
77 
78  static constexpr auto prefix = Def::prefix;
79  static constexpr std::size_t fixed_size = prefix_size + size_size;
80 
81  static constexpr std::size_t get_size( auto const& buffer )
82  {
83  std::array< std::byte, size_size > tmp;
84 
85  auto iter = std::begin( buffer );
86  std::advance( iter, prefix_size );
87  std::copy_n( iter, size_size, tmp.begin() );
90  }
91  };
92 
94 
95  static constexpr checksum_type get_checksum( view< std::byte const* > const mview )
96  {
97  return Def::get_checksum( mview );
98  }
99 };
100 
101 } // namespace emlabcpp::protocol
Definition: sequencer.h:42
Generic class to represent view of some container.
Definition: view.h:41
MIT License.
Definition: multiplexer.h:33
concept packet_def
Definition: packet.h:34
requires(std::is_enum_v< T >) struct serializer< T
bounded< std::size_t, max_size, max_size > size_type
Definition: serializer.h:74
static constexpr std::size_t max_size
Definition: serializer.h:73
Each definition of item provided to protocol library should have specialization of 'proto_traits' str...
Definition: traits.h:44
Creates a segment starting with counter defined by CounterDef, this counter limits how many bytes are...
Definition: base.h:104
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
constexpr bool is_std_array_v
Definition: base.h:160
typename base::message_type message_type
Definition: packet.h:75
static constexpr std::size_t get_size(auto const &buffer)
Definition: packet.h:81
static constexpr std::size_t fixed_size
Definition: packet.h:79
static constexpr auto prefix
Definition: packet.h:78
Definition: packet.h:50
typename payload_traits::value_type value_type
Definition: packet.h:66
typename Def::size_type size_type
Definition: packet.h:61
static constexpr checksum_type get_checksum(view< std::byte const * > const mview)
Definition: packet.h:95
static constexpr std::size_t prefix_size
Definition: packet.h:59
static constexpr auto prefix
Definition: packet.h:54
static constexpr std::size_t size_size
Definition: packet.h:63
std::decay_t< decltype(prefix) > prefix_type
Definition: packet.h:57
Payload payload_type
Definition: packet.h:51
typename Def::checksum_type checksum_type
Definition: packet.h:68
static constexpr auto endianess
Definition: packet.h:55
Definition: serializer.h:38
static constexpr T deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:56
tuple is high levle alternative to use just 'std::tuple' that is more friendly for standalone protoco...
Definition: tuple.h:43
message< max_size > message_type
Definition: tuple.h:58