Pigweed
 
Loading...
Searching...
No Matches
encode_args.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <stdarg.h>
17#include <stddef.h>
18#include <stdint.h>
19
20#include "pw_polyfill/standard.h"
21#include "pw_preprocessor/util.h"
22#include "pw_tokenizer/internal/argument_types.h"
23#include "pw_varint/varint.h"
24
25#ifdef __cplusplus
26
27#include <cstring>
28
29#include "pw_polyfill/standard.h"
30#include "pw_span/span.h"
31#include "pw_tokenizer/config.h"
32#include "pw_tokenizer/tokenize.h"
33
34namespace pw::tokenizer {
35namespace internal {
36
37// Returns the maximum encoded size of an argument of the specified type.
38template <typename T>
39constexpr size_t ArgEncodedSizeBytes() {
40 constexpr pw_tokenizer_ArgTypes kType = VarargsType<T>();
41 if constexpr (kType == PW_TOKENIZER_ARG_TYPE_DOUBLE) {
42 return sizeof(float);
43 } else if constexpr (kType == PW_TOKENIZER_ARG_TYPE_STRING) {
44 return 1; // Size of the length byte only
45 } else if constexpr (kType == PW_TOKENIZER_ARG_TYPE_INT64) {
46 return 10; // Max size of a varint-encoded 64-bit integer
47 } else if constexpr (kType == PW_TOKENIZER_ARG_TYPE_INT) {
48 return sizeof(T) + 1; // Max size of zig-zag varint integer <= 32-bits
49 } else {
50 static_assert(sizeof(T) != sizeof(T), "Unsupported argument type");
51 }
52}
53
54} // namespace internal
55
70template <typename... ArgTypes>
71constexpr size_t MinEncodingBufferSizeBytes() {
72 return (sizeof(pw_tokenizer_Token) + ... +
73 internal::ArgEncodedSizeBytes<ArgTypes>());
74}
75
82size_t EncodeArgs(pw_tokenizer_ArgTypes types,
83 va_list args,
84 span<std::byte> output);
85
106template <size_t kMaxSizeBytes>
108 public:
109 // Encodes a tokenized message to an internal buffer.
110 EncodedMessage(pw_tokenizer_Token token,
111 pw_tokenizer_ArgTypes types,
112 va_list args) {
113 std::memcpy(data_, &token, sizeof(token));
114 size_ =
115 sizeof(token) +
116 EncodeArgs(types, args, span<std::byte>(data_).subspan(sizeof(token)));
117 }
118
120 const std::byte* data() const { return data_; }
121
123 const uint8_t* data_as_uint8() const {
124 return reinterpret_cast<const uint8_t*>(data());
125 }
126
128 size_t size() const { return size_; }
129
130 private:
131 static_assert(kMaxSizeBytes >= sizeof(pw_tokenizer_Token),
132 "The encoding buffer must be at least large enough for a token "
133 "(4 bytes)");
134
135 std::byte data_[kMaxSizeBytes];
136 size_t size_;
137};
138
139} // namespace pw::tokenizer
140
141#endif // __cplusplus
142
143PW_EXTERN_C_START
144
147size_t pw_tokenizer_EncodeArgs(pw_tokenizer_ArgTypes types,
148 va_list args,
149 void* output_buffer,
150 size_t output_buffer_size);
151
154static inline size_t pw_tokenizer_EncodeInt(int value,
155 void* output,
156 size_t output_size_bytes) {
157 return pw_varint_Encode32(
158 pw_varint_ZigZagEncode32(value), output, output_size_bytes);
159}
160
163static inline size_t pw_tokenizer_EncodeInt64(int64_t value,
164 void* output,
165 size_t output_size_bytes) {
166 return pw_varint_Encode64(
167 pw_varint_ZigZagEncode64(value), output, output_size_bytes);
168}
169
170PW_EXTERN_C_END
Definition: encode_args.h:107
const std::byte * data() const
The binary-encoded tokenized message.
Definition: encode_args.h:120
size_t size() const
The size of the encoded tokenized message in bytes.
Definition: encode_args.h:128
const uint8_t * data_as_uint8() const
Returns data() as a pointer to uint8_t instead of std::byte.
Definition: encode_args.h:123
size_t pw_varint_Encode64(uint64_t integer, void *output, size_t output_size_bytes)
static uint32_t pw_varint_ZigZagEncode32(int32_t n)
Zig-zag encodes an int32_t, returning it as a uint32_t.
Definition: varint.h:65
size_t pw_varint_Encode32(uint32_t integer, void *output, size_t output_size_bytes)
static uint64_t pw_varint_ZigZagEncode64(int64_t n)
Zig-zag encodes an int64_t, returning it as a uint64_t.
Definition: varint.h:70