• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 <cinttypes>
17 
18 #include "pw_rpc/internal/client_server_testing.h"
19 #include "pw_rpc/pwpb/fake_channel_output.h"
20 
21 namespace pw::rpc {
22 namespace internal {
23 
24 template <size_t kOutputSize,
25           size_t kMaxPackets,
26           size_t kPayloadsBufferSizeBytes>
27 class PwpbForwardingChannelOutput final
28     : public ForwardingChannelOutput<
29           PwpbFakeChannelOutput<kMaxPackets, kPayloadsBufferSizeBytes>,
30           kOutputSize,
31           kMaxPackets,
32           kPayloadsBufferSizeBytes> {
33  private:
34   template <auto kMethod>
35   using MethodInfo = internal::MethodInfo<kMethod>;
36   template <auto kMethod>
37   using Response = typename MethodInfo<kMethod>::Response;
38   template <auto kMethod>
39   using Request = typename MethodInfo<kMethod>::Request;
40 
41   using Base = ForwardingChannelOutput<
42       PwpbFakeChannelOutput<kMaxPackets, kPayloadsBufferSizeBytes>,
43       kOutputSize,
44       kMaxPackets,
45       kPayloadsBufferSizeBytes>;
46 
47  public:
48   constexpr PwpbForwardingChannelOutput() = default;
49 
50   template <auto kMethod>
response(uint32_t channel_id,uint32_t index)51   Response<kMethod> response(uint32_t channel_id, uint32_t index) {
52     PW_ASSERT(Base::PacketCount() >= index);
53     return Base::output_.template responses<kMethod>(channel_id)[index];
54   }
55 
56   template <auto kMethod>
request(uint32_t channel_id,uint32_t index)57   Request<kMethod> request(uint32_t channel_id, uint32_t index) {
58     PW_ASSERT(Base::PacketCount() >= index);
59     return Base::output_.template requests<kMethod>(channel_id)[index];
60   }
61 };
62 
63 }  // namespace internal
64 
65 template <size_t kOutputSize = 128,
66           size_t kMaxPackets = 16,
67           size_t kPayloadsBufferSizeBytes = 128>
68 class PwpbClientServerTestContext final
69     : public internal::ClientServerTestContext<
70           internal::PwpbForwardingChannelOutput<kOutputSize,
71                                                 kMaxPackets,
72                                                 kPayloadsBufferSizeBytes>,
73           kOutputSize,
74           kMaxPackets,
75           kPayloadsBufferSizeBytes> {
76  private:
77   template <auto kMethod>
78   using MethodInfo = internal::MethodInfo<kMethod>;
79   template <auto kMethod>
80   using Response = typename MethodInfo<kMethod>::Response;
81   template <auto kMethod>
82   using Request = typename MethodInfo<kMethod>::Request;
83 
84   using Base = internal::ClientServerTestContext<
85       internal::PwpbForwardingChannelOutput<kOutputSize,
86                                             kMaxPackets,
87                                             kPayloadsBufferSizeBytes>,
88       kOutputSize,
89       kMaxPackets,
90       kPayloadsBufferSizeBytes>;
91 
92  public:
93   PwpbClientServerTestContext() = default;
94 
95   // Retrieve copy of request indexed by order of occurance
96   template <auto kMethod>
request(uint32_t index)97   Request<kMethod> request(uint32_t index) {
98     return Base::channel_output_.template request<kMethod>(Base::channel().id(),
99                                                            index);
100   }
101 
102   // Retrieve copy of resonse indexed by order of occurance
103   template <auto kMethod>
response(uint32_t index)104   Response<kMethod> response(uint32_t index) {
105     return Base::channel_output_.template response<kMethod>(
106         Base::channel().id(), index);
107   }
108 };
109 
110 }  // namespace pw::rpc
111