xref: /aosp_15_r20/external/pigweed/pw_rpc/client.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 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/client.h"
19 // clang-format on
20 
21 #include "pw_log/log.h"
22 #include "pw_rpc/internal/client_call.h"
23 #include "pw_rpc/internal/packet.h"
24 #include "pw_status/try.h"
25 
26 namespace pw::rpc {
27 namespace {
28 
29 using internal::Packet;
30 using internal::pwpb::PacketType;
31 
32 }  // namespace
33 
ProcessPacket(ConstByteSpan data)34 Status Client::ProcessPacket(ConstByteSpan data) {
35   PW_TRY_ASSIGN(Packet packet, Endpoint::ProcessPacket(data, Packet::kClient));
36 
37   // Find an existing call for this RPC, if any.
38   internal::rpc_lock().lock();
39   IntrusiveList<internal::Call>::iterator call = FindCall(packet);
40 
41   internal::ChannelBase* channel = GetInternalChannel(packet.channel_id());
42 
43   if (channel == nullptr) {
44     internal::rpc_lock().unlock();
45     PW_LOG_WARN("RPC client received a packet for an unregistered channel: %lu",
46                 static_cast<unsigned long>(packet.channel_id()));
47     return Status::Unavailable();
48   }
49 
50   if (call == calls_end()) {
51     // The call for the packet does not exist. If the packet is a server stream
52     // message, notify the server so that it can kill the stream. Otherwise,
53     // silently drop the packet (as it would terminate the RPC anyway).
54     if (packet.type() == PacketType::SERVER_STREAM) {
55       channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()))
56           .IgnoreError();
57       PW_LOG_WARN("RPC client received stream message for an unknown call");
58     }
59     internal::rpc_lock().unlock();
60     return OkStatus();  // OK since the packet was handled
61   }
62 
63   switch (packet.type()) {
64     case PacketType::RESPONSE:
65       PW_LOG_DEBUG("Client call %u for %u:%08x/%08x completed with status %s",
66                    static_cast<unsigned>(packet.call_id()),
67                    static_cast<unsigned>(packet.channel_id()),
68                    static_cast<unsigned>(packet.service_id()),
69                    static_cast<unsigned>(packet.method_id()),
70                    packet.status().str());
71       // RPCs without a server stream include a payload with the final packet.
72       if (call->has_server_stream()) {
73         static_cast<internal::StreamResponseClientCall&>(*call).HandleCompleted(
74             packet.status());
75       } else {
76         static_cast<internal::UnaryResponseClientCall&>(*call).HandleCompleted(
77             packet.payload(), packet.status());
78       }
79       break;
80     case PacketType::SERVER_ERROR:
81       PW_LOG_DEBUG("Client call %u for %u:%08x/%08x terminated with error %s",
82                    static_cast<unsigned>(packet.call_id()),
83                    static_cast<unsigned>(packet.channel_id()),
84                    static_cast<unsigned>(packet.service_id()),
85                    static_cast<unsigned>(packet.method_id()),
86                    packet.status().str());
87       call->HandleError(packet.status());
88       break;
89     case PacketType::SERVER_STREAM:
90       if (call->has_server_stream()) {
91         call->HandlePayload(packet.payload());
92       } else {
93         // Report the error to the server so it can abort the RPC.
94         channel->Send(Packet::ClientError(packet, Status::InvalidArgument()))
95             .IgnoreError();  // Errors are logged in Channel::Send.
96         call->HandleError(Status::InvalidArgument());
97         PW_LOG_DEBUG("Received SERVER_STREAM for RPC without a server stream");
98       }
99       break;
100 
101     case PacketType::REQUEST:
102     case PacketType::CLIENT_STREAM:
103     case PacketType::CLIENT_ERROR:
104     case PacketType::CLIENT_REQUEST_COMPLETION:
105     default:
106       internal::rpc_lock().unlock();
107       PW_LOG_WARN("pw_rpc client unable to handle packet of type %u",
108                   static_cast<unsigned>(packet.type()));
109   }
110 
111   return OkStatus();  // OK since the packet was handled
112 }
113 
114 }  // namespace pw::rpc
115