xref: /aosp_15_r20/external/pigweed/pw_rpc/raw/client_testing.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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 
15 // clang-format off
16 #include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first.
17 
18 #include "pw_rpc/raw/client_testing.h"
19 // clang-format on
20 
21 #include <mutex>
22 
23 #include "pw_assert/check.h"
24 #include "pw_log/log.h"
25 #include "pw_rpc/client.h"
26 #include "pw_rpc/internal/lock.h"
27 
28 namespace pw::rpc {
29 
CheckProcessPacket(internal::pwpb::PacketType type,uint32_t service_id,uint32_t method_id,std::optional<uint32_t> call_id,ConstByteSpan payload,Status status) const30 void FakeServer::CheckProcessPacket(internal::pwpb::PacketType type,
31                                     uint32_t service_id,
32                                     uint32_t method_id,
33                                     std::optional<uint32_t> call_id,
34                                     ConstByteSpan payload,
35                                     Status status) const {
36   if (Status process_packet_status =
37           ProcessPacket(type, service_id, method_id, call_id, payload, status);
38       !process_packet_status.ok()) {
39     PW_LOG_CRITICAL("Failed to process packet in pw::rpc::FakeServer");
40     PW_LOG_CRITICAL(
41         "Packet contents\ntype: %u\nchannel_id: %u\nservice_id: %08x\n"
42         "method_id: %08x\npayload: %u bytes\nstatus: %s",
43         static_cast<unsigned>(type),
44         static_cast<unsigned>(channel_id_),
45         static_cast<unsigned>(service_id),
46         static_cast<unsigned>(method_id),
47         static_cast<unsigned>(payload.size()),
48         status.str());
49     PW_CHECK_OK(process_packet_status);
50   }
51 }
52 
ProcessPacket(internal::pwpb::PacketType type,uint32_t service_id,uint32_t method_id,std::optional<uint32_t> call_id,ConstByteSpan payload,Status status) const53 Status FakeServer::ProcessPacket(internal::pwpb::PacketType type,
54                                  uint32_t service_id,
55                                  uint32_t method_id,
56                                  std::optional<uint32_t> call_id,
57                                  ConstByteSpan payload,
58                                  Status status) const {
59   if (!call_id.has_value()) {
60     std::lock_guard lock(output_.mutex_);
61     auto view = internal::test::PacketsView(
62         output_.packets(),
63         internal::test::PacketFilter(internal::pwpb::PacketType::REQUEST,
64                                      internal::pwpb::PacketType::RESPONSE,
65                                      channel_id_,
66                                      service_id,
67                                      method_id));
68 
69     // Re-use the call ID of the most recent packet for this RPC.
70     if (!view.empty()) {
71       call_id = view.back().call_id();
72     }
73   }
74 
75   auto packet_encoding_result =
76       internal::Packet(type,
77                        channel_id_,
78                        service_id,
79                        method_id,
80                        call_id.value_or(internal::Packet::kUnassignedId),
81                        payload,
82                        status)
83           .Encode(packet_buffer_);
84   PW_CHECK_OK(packet_encoding_result.status());
85   return client_.ProcessPacket(*packet_encoding_result);
86 }
87 
88 }  // namespace pw::rpc
89