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_threaded.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 PwpbWatchableChannelOutput final 28 : public WatchableChannelOutput< 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 = WatchableChannelOutput< 42 PwpbFakeChannelOutput<kMaxPackets, kPayloadsBufferSizeBytes>, 43 kOutputSize, 44 kMaxPackets, 45 kPayloadsBufferSizeBytes>; 46 47 public: 48 constexpr PwpbWatchableChannelOutput() = 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_LOCKS_EXCLUDED(Base::mutex_) { 53 std::lock_guard lock(Base::mutex_); 54 PW_ASSERT(Base::PacketCount() >= index); 55 return Base::output_.template responses<kMethod>(channel_id)[index]; 56 } 57 58 template <auto kMethod> request(uint32_t channel_id,uint32_t index)59 Request<kMethod> request(uint32_t channel_id, uint32_t index) 60 PW_LOCKS_EXCLUDED(Base::mutex_) { 61 std::lock_guard lock(Base::mutex_); 62 PW_ASSERT(Base::PacketCount() >= index); 63 return Base::output_.template requests<kMethod>(channel_id)[index]; 64 } 65 }; 66 67 } // namespace internal 68 69 template <size_t kOutputSize = 128, 70 size_t kMaxPackets = 16, 71 size_t kPayloadsBufferSizeBytes = 128> 72 class PwpbClientServerTestContextThreaded final 73 : public internal::ClientServerTestContextThreaded< 74 internal::PwpbWatchableChannelOutput<kOutputSize, 75 kMaxPackets, 76 kPayloadsBufferSizeBytes>, 77 kOutputSize, 78 kMaxPackets, 79 kPayloadsBufferSizeBytes> { 80 private: 81 template <auto kMethod> 82 using MethodInfo = internal::MethodInfo<kMethod>; 83 template <auto kMethod> 84 using Response = typename MethodInfo<kMethod>::Response; 85 template <auto kMethod> 86 using Request = typename MethodInfo<kMethod>::Request; 87 88 using Base = internal::ClientServerTestContextThreaded< 89 internal::PwpbWatchableChannelOutput<kOutputSize, 90 kMaxPackets, 91 kPayloadsBufferSizeBytes>, 92 kOutputSize, 93 kMaxPackets, 94 kPayloadsBufferSizeBytes>; 95 96 public: PwpbClientServerTestContextThreaded(const thread::Options & options)97 PwpbClientServerTestContextThreaded(const thread::Options& options) 98 : Base(options) {} 99 100 // Retrieve copy of request indexed by order of occurance 101 template <auto kMethod> request(uint32_t index)102 Request<kMethod> request(uint32_t index) { 103 return Base::channel_output_.template request<kMethod>(Base::channel().id(), 104 index); 105 } 106 107 // Retrieve copy of resonse indexed by order of occurance 108 template <auto kMethod> response(uint32_t index)109 Response<kMethod> response(uint32_t index) { 110 return Base::channel_output_.template response<kMethod>( 111 Base::channel().id(), index); 112 } 113 }; 114 115 } // namespace pw::rpc 116