• 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 #include "pw_rpc/nanopb_client_call.h"
21 
22 namespace pw::rpc::internal {
23 
24 // Encodes a protobuf to a local span named by result from a list of nanopb
25 // struct initializers.
26 //
27 //  PW_ENCODE_PB(pw_rpc_TestProto, encoded, .value = 42);
28 //
29 #define PW_ENCODE_PB(proto, result, ...) \
30   _PW_ENCODE_PB_EXPAND(proto, result, __LINE__, __VA_ARGS__)
31 
32 #define _PW_ENCODE_PB_EXPAND(proto, result, unique, ...) \
33   _PW_ENCODE_PB_IMPL(proto, result, unique, __VA_ARGS__)
34 
35 #define _PW_ENCODE_PB_IMPL(proto, result, unique, ...)            \
36   std::array<pb_byte_t, 2 * sizeof(proto)> _pb_buffer_##unique{}; \
37   const std::span result =                                        \
38       ::pw::rpc::internal::EncodeProtobuf<proto, proto##_fields>( \
39           proto{__VA_ARGS__}, _pb_buffer_##unique)
40 
41 template <typename T, auto fields>
EncodeProtobuf(const T & protobuf,std::span<pb_byte_t> buffer)42 std::span<const std::byte> EncodeProtobuf(const T& protobuf,
43                                           std::span<pb_byte_t> buffer) {
44   auto output = pb_ostream_from_buffer(buffer.data(), buffer.size());
45   EXPECT_TRUE(pb_encode(&output, fields, &protobuf));
46   return std::as_bytes(buffer.first(output.bytes_written));
47 }
48 
49 // Decodes a protobuf to a nanopb struct named by result.
50 #define PW_DECODE_PB(proto, result, buffer)                        \
51   proto result;                                                    \
52   ::pw::rpc::internal::DecodeProtobuf<proto, proto##_fields>(      \
53       std::span(reinterpret_cast<const pb_byte_t*>(buffer.data()), \
54                 buffer.size()),                                    \
55       result);
56 
57 template <typename T, auto fields>
DecodeProtobuf(std::span<const pb_byte_t> buffer,T & protobuf)58 void DecodeProtobuf(std::span<const pb_byte_t> buffer, T& protobuf) {
59   auto input = pb_istream_from_buffer(buffer.data(), buffer.size());
60   EXPECT_TRUE(pb_decode(&input, fields, &protobuf));
61 }
62 
63 // Client response handler for a unary RPC invocation which captures the
64 // response it receives.
65 template <typename Response>
66 class TestUnaryResponseHandler : public UnaryResponseHandler<Response> {
67  public:
ReceivedResponse(Status status,const Response & response)68   void ReceivedResponse(Status status, const Response& response) override {
69     last_status_ = status;
70     last_response_ = response;
71     ++responses_received_;
72   }
73 
RpcError(Status status)74   void RpcError(Status status) override { rpc_error_ = status; }
75 
last_status()76   constexpr Status last_status() const { return last_status_; }
last_response()77   constexpr const Response& last_response() const& { return last_response_; }
responses_received()78   constexpr size_t responses_received() const { return responses_received_; }
rpc_error()79   constexpr Status rpc_error() const { return rpc_error_; }
80 
81  private:
82   Status last_status_;
83   Response last_response_;
84   size_t responses_received_ = 0;
85   Status rpc_error_;
86 };
87 
88 // Client response handler for a unary RPC invocation which stores information
89 // about the state of the stream.
90 template <typename Response>
91 class TestServerStreamingResponseHandler
92     : public ServerStreamingResponseHandler<Response> {
93  public:
ReceivedResponse(const Response & response)94   void ReceivedResponse(const Response& response) override {
95     last_response_ = response;
96     ++responses_received_;
97   }
98 
Complete(Status status)99   void Complete(Status status) override {
100     active_ = false;
101     status_ = status;
102   }
103 
RpcError(Status status)104   void RpcError(Status status) override { rpc_error_ = status; }
105 
active()106   constexpr bool active() const { return active_; }
status()107   constexpr Status status() const { return status_; }
last_response()108   constexpr const Response& last_response() const& { return last_response_; }
responses_received()109   constexpr size_t responses_received() const { return responses_received_; }
rpc_error()110   constexpr Status rpc_error() const { return rpc_error_; }
111 
112  private:
113   Status status_;
114   Response last_response_;
115   size_t responses_received_ = 0;
116   bool active_ = true;
117   Status rpc_error_;
118 };
119 
120 }  // namespace pw::rpc::internal
121