• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <span>
17 
18 #include "pb_decode.h"
19 #include "pb_encode.h"
20 
21 namespace pw::rpc::internal {
22 
23 // Encodes a protobuf to a local span named by result from a list of nanopb
24 // struct initializers.
25 //
26 //  PW_ENCODE_PB(pw_rpc_TestProto, encoded, .value = 42);
27 //
28 #define PW_ENCODE_PB(proto, result, ...) \
29   _PW_ENCODE_PB_EXPAND(proto, result, __LINE__, __VA_ARGS__)
30 
31 #define _PW_ENCODE_PB_EXPAND(proto, result, unique, ...) \
32   _PW_ENCODE_PB_IMPL(proto, result, unique, __VA_ARGS__)
33 
34 #define _PW_ENCODE_PB_IMPL(proto, result, unique, ...)            \
35   std::array<pb_byte_t, 2 * sizeof(proto)> _pb_buffer_##unique{}; \
36   const std::span result =                                        \
37       ::pw::rpc::internal::EncodeProtobuf<proto, proto##_fields>( \
38           proto{__VA_ARGS__}, _pb_buffer_##unique)
39 
40 template <typename T, auto kFields>
EncodeProtobuf(const T & protobuf,std::span<pb_byte_t> buffer)41 std::span<const std::byte> EncodeProtobuf(const T& protobuf,
42                                           std::span<pb_byte_t> buffer) {
43   auto output = pb_ostream_from_buffer(buffer.data(), buffer.size());
44   EXPECT_TRUE(pb_encode(&output, kFields, &protobuf));
45   return std::as_bytes(buffer.first(output.bytes_written));
46 }
47 
48 // Decodes a protobuf to a nanopb struct named by result.
49 #define PW_DECODE_PB(proto, result, buffer)                        \
50   proto result;                                                    \
51   ::pw::rpc::internal::DecodeProtobuf<proto, proto##_fields>(      \
52       std::span(reinterpret_cast<const pb_byte_t*>(buffer.data()), \
53                 buffer.size()),                                    \
54       result);
55 
56 template <typename T, auto kFields>
DecodeProtobuf(std::span<const pb_byte_t> buffer,T & protobuf)57 void DecodeProtobuf(std::span<const pb_byte_t> buffer, T& protobuf) {
58   auto input = pb_istream_from_buffer(buffer.data(), buffer.size());
59   EXPECT_TRUE(pb_decode(&input, kFields, &protobuf));
60 }
61 
62 }  // namespace pw::rpc::internal
63