aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/public/pw_rpc/payloads_view.h
blob: 3d4934f8839863e352dc3d72f9573f71aa9b89fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// The classes in this file facilitate testing RPC services. The main class is
// PayloadsView, which iterates over the payloads sent by an RPC service or
// client. This allows verifying that code that invokes RPCs or an RPC service
// implementation sends the expected requests or responses.
//
// This code is inteded for testing, not for deployment.
#pragma once

#include <tuple>

#include "pw_containers/filtered_view.h"
#include "pw_containers/vector.h"
#include "pw_containers/wrapped_iterator.h"
#include "pw_rpc/channel.h"
#include "pw_rpc/internal/method_info.h"
#include "pw_rpc/internal/packet.h"
#include "pw_rpc/method_type.h"

namespace pw::rpc {
namespace internal::test {

class FakeChannelOutput;

// Finds packets of a specified type for a particular method.
class PacketFilter {
 public:
  // Use Channel::kUnassignedChannelId to ignore the channel.
  constexpr PacketFilter(PacketType packet_type_1,
                         PacketType packet_type_2,
                         uint32_t channel_id,
                         uint32_t service_id,
                         uint32_t method_id)
      : packet_type_1_(packet_type_1),
        packet_type_2_(packet_type_2),
        channel_id_(channel_id),
        service_id_(service_id),
        method_id_(method_id) {}

  constexpr bool operator()(const Packet& packet) const {
    return (packet.type() == packet_type_1_ ||
            packet.type() == packet_type_2_) &&
           (channel_id_ == Channel::kUnassignedChannelId ||
            packet.channel_id() == channel_id_) &&
           packet.service_id() == service_id_ &&
           packet.method_id() == method_id_;
  }

 private:
  // Support filtering on two packet types to handle reading both client and
  // server streams for bidirectional streams.
  PacketType packet_type_1_;
  PacketType packet_type_2_;
  uint32_t channel_id_;
  uint32_t service_id_;
  uint32_t method_id_;
};

using PacketsView = containers::FilteredView<Vector<Packet>, PacketFilter>;

}  // namespace internal::test

// Returns the payloads for a particular RPC in a Vector of RPC packets.
//
// Adapts a FilteredView of packets to return payloads instead of packets.
class PayloadsView {
 public:
  class iterator : public containers::WrappedIterator<
                       iterator,
                       internal::test::PacketsView::iterator,
                       ConstByteSpan> {
   public:
    constexpr iterator() = default;

    // Access the payload (rather than packet) with operator* and operator->.
    const ConstByteSpan& operator*() const { return value().payload(); }
    const ConstByteSpan* operator->() const { return &value().payload(); }

   private:
    friend class PayloadsView;

    constexpr iterator(const internal::test::PacketsView::iterator& it)
        : containers::WrappedIterator<iterator,
                                      internal::test::PacketsView::iterator,
                                      ConstByteSpan>(it) {}
  };

  using const_iterator = iterator;

  const ConstByteSpan& operator[](size_t index) const {
    auto it = begin();
    std::advance(it, index);
    return *it;
  }

  // Number of payloads for the specified RPC.
  size_t size() const { return view_.size(); }

  bool empty() const { return begin() == end(); }

  // Returns the first/last payload for the RPC. size() must be > 0.
  const ConstByteSpan& front() const { return *begin(); }
  const ConstByteSpan& back() const { return *std::prev(end()); }

  iterator begin() const { return iterator(view_.begin()); }
  iterator end() const { return iterator(view_.end()); }

 private:
  friend class internal::test::FakeChannelOutput;

  template <typename>
  friend class NanopbPayloadsView;

  template <auto kMethod>
  using MethodInfo = internal::MethodInfo<kMethod>;

  using PacketType = internal::PacketType;

  template <auto kMethod>
  static constexpr PayloadsView For(const Vector<internal::Packet>& packets,
                                    uint32_t channel_id) {
    constexpr auto kTypes = PacketTypesWithPayload(MethodInfo<kMethod>::kType);
    return PayloadsView(packets,
                        std::get<0>(kTypes),
                        std::get<1>(kTypes),
                        channel_id,
                        MethodInfo<kMethod>::kServiceId,
                        MethodInfo<kMethod>::kMethodId);
  }

  constexpr PayloadsView(const Vector<internal::Packet>& packets,
                         MethodType method_type,
                         uint32_t channel_id,
                         uint32_t service_id,
                         uint32_t method_id)
      : PayloadsView(packets,
                     std::get<0>(PacketTypesWithPayload(method_type)),
                     std::get<1>(PacketTypesWithPayload(method_type)),
                     channel_id,
                     service_id,
                     method_id) {}

  constexpr PayloadsView(const Vector<internal::Packet>& packets,
                         PacketType packet_type_1,
                         PacketType packet_type_2,
                         uint32_t channel_id,
                         uint32_t service_id,
                         uint32_t method_id)
      : view_(packets,
              internal::test::PacketFilter(packet_type_1,
                                           packet_type_2,
                                           channel_id,
                                           service_id,
                                           method_id)) {}

  static constexpr std::tuple<PacketType, PacketType> PacketTypesWithPayload(
      MethodType method_type) {
    switch (method_type) {
      case MethodType::kUnary:
        return {PacketType::REQUEST, PacketType::RESPONSE};
      case MethodType::kServerStreaming:
        return {PacketType::REQUEST, PacketType::SERVER_STREAM};
      case MethodType::kClientStreaming:
        return {PacketType::CLIENT_STREAM, PacketType::RESPONSE};
      case MethodType::kBidirectionalStreaming:
        return {PacketType::CLIENT_STREAM, PacketType::SERVER_STREAM};
    }

// Workaround for GCC 8 bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86678
#if defined(__GNUC__) && __GNUC__ < 9
    return {};
#else
    PW_ASSERT(false);
#endif  // defined(__GNUC__) && __GNUC__ < 9
  }

  internal::test::PacketsView view_;
};

// Class for iterating over RPC statuses associated witha particular RPC. This
// is used to iterate over the user RPC statuses and or protocol errors for a
// particular RPC.
class StatusView {
 public:
  class iterator : public containers::WrappedIterator<
                       iterator,
                       internal::test::PacketsView::iterator,
                       Status> {
   public:
    constexpr iterator() = default;

    // Access the status (rather than packet) with operator* and operator->.
    const Status& operator*() const { return value().status(); }
    const Status* operator->() const { return &value().status(); }

   private:
    friend class StatusView;

    constexpr iterator(const internal::test::PacketsView::iterator& it)
        : containers::WrappedIterator<iterator,
                                      internal::test::PacketsView::iterator,
                                      Status>(it) {}
  };

  using const_iterator = iterator;

  const Status& operator[](size_t index) const {
    auto it = begin();
    std::advance(it, index);
    return *it;
  }

  // Number of statuses in this view.
  size_t size() const { return view_.size(); }
  bool empty() const { return begin() == end(); }

  // Returns the first/last payload for the RPC. size() must be > 0.
  const Status& front() const { return *begin(); }
  const Status& back() const { return *std::prev(end()); }

  iterator begin() const { return iterator(view_.begin()); }
  iterator end() const { return iterator(view_.end()); }

 private:
  friend class internal::test::FakeChannelOutput;

  template <auto kMethod>
  using MethodInfo = internal::MethodInfo<kMethod>;

  using PacketType = internal::PacketType;

  constexpr StatusView(const Vector<internal::Packet>& packets,
                       PacketType packet_type_1,
                       PacketType packet_type_2,
                       uint32_t channel_id,
                       uint32_t service_id,
                       uint32_t method_id)
      : view_(packets,
              internal::test::PacketFilter(packet_type_1,
                                           packet_type_2,
                                           channel_id,
                                           service_id,
                                           method_id)) {}

  internal::test::PacketsView view_;
};

}  // namespace pw::rpc