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 "pw_assert/check.h"
22 #include "pw_log/log.h"
23 #include "pw_rpc/client.h"
24
25 namespace pw::rpc {
26
CheckProcessPacket(internal::PacketType type,uint32_t service_id,uint32_t method_id,ConstByteSpan payload,Status status) const27 void FakeServer::CheckProcessPacket(internal::PacketType type,
28 uint32_t service_id,
29 uint32_t method_id,
30 ConstByteSpan payload,
31 Status status) const {
32 if (Status process_packet_status =
33 ProcessPacket(type, service_id, method_id, payload, status);
34 !process_packet_status.ok()) {
35 PW_LOG_CRITICAL("Failed to process packet in pw::rpc::FakeServer");
36 PW_LOG_CRITICAL(
37 "Packet contents\ntype: %u\nchannel_id: %u\nservice_id: %08x\n"
38 "method_id: %08x\npayload: %u bytes\nstatus: %s",
39 static_cast<unsigned>(type),
40 static_cast<unsigned>(channel_id_),
41 static_cast<unsigned>(service_id),
42 static_cast<unsigned>(method_id),
43 static_cast<unsigned>(payload.size()),
44 status.str());
45 PW_CHECK_OK(process_packet_status);
46 }
47 }
48
ProcessPacket(internal::PacketType type,uint32_t service_id,uint32_t method_id,ConstByteSpan payload,Status status) const49 Status FakeServer::ProcessPacket(internal::PacketType type,
50 uint32_t service_id,
51 uint32_t method_id,
52 ConstByteSpan payload,
53 Status status) const {
54 auto view = internal::test::PacketsView(
55 output_.packets(),
56 internal::test::PacketFilter(internal::PacketType::REQUEST,
57 internal::PacketType::RESPONSE,
58 channel_id_,
59 service_id,
60 method_id));
61
62 // Re-use the call ID of the most recent packet for this RPC.
63 uint32_t call_id = view.empty() ? 0 : view.back().call_id();
64
65 auto packet_encoding_result =
66 internal::Packet(
67 type, channel_id_, service_id, method_id, call_id, payload, status)
68 .Encode(packet_buffer_);
69 PW_CHECK_OK(packet_encoding_result.status());
70 return client_.ProcessPacket(*packet_encoding_result);
71 }
72
73 } // namespace pw::rpc
74