blob: f0f8181acc58ae40c18176c4d2791f1a8d0c575d [file] [log] [blame]
Alexei Frolov4d2adde2020-08-04 10:19:24 -07001// 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
Ewout van Bekkum7f5b3052021-11-11 17:35:23 -080015// clang-format off
16#include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first.
17
Alexei Frolov4d2adde2020-08-04 10:19:24 -070018#include "pw_rpc/client.h"
Ewout van Bekkum7f5b3052021-11-11 17:35:23 -080019// clang-format on
Alexei Frolov4d2adde2020-08-04 10:19:24 -070020
21#include "pw_log/log.h"
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070022#include "pw_rpc/internal/client_call.h"
Alexei Frolov4d2adde2020-08-04 10:19:24 -070023#include "pw_rpc/internal/packet.h"
Wyatt Hepler82db4b12021-09-23 09:10:12 -070024#include "pw_status/try.h"
Alexei Frolov4d2adde2020-08-04 10:19:24 -070025
26namespace pw::rpc {
27namespace {
28
Alexei Frolov4d2adde2020-08-04 10:19:24 -070029using internal::Packet;
30using internal::PacketType;
31
32} // namespace
33
34Status Client::ProcessPacket(ConstByteSpan data) {
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070035 PW_TRY_ASSIGN(Result<Packet> result,
36 Endpoint::ProcessPacket(data, Packet::kClient));
Wyatt Hepler82db4b12021-09-23 09:10:12 -070037 Packet& packet = *result;
Alexei Frolov4d2adde2020-08-04 10:19:24 -070038
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070039 // Find an existing call for this RPC, if any.
Wyatt Heplerc8bdc7d2021-10-20 09:29:01 -070040 internal::rpc_lock().lock();
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070041 internal::ClientCall* call =
42 static_cast<internal::ClientCall*>(FindCall(packet));
43
Wyatt Hepler82db4b12021-09-23 09:10:12 -070044 internal::Channel* channel = GetInternalChannel(packet.channel_id());
Wyatt Heplerc8bdc7d2021-10-20 09:29:01 -070045
Wyatt Hepler82db4b12021-09-23 09:10:12 -070046 if (channel == nullptr) {
Wyatt Heplerc8bdc7d2021-10-20 09:29:01 -070047 internal::rpc_lock().unlock();
Wyatt Heplerb1c47992022-01-06 11:05:50 -080048 PW_LOG_WARN("RPC client received a packet for an unregistered channel");
Wyatt Hepler82db4b12021-09-23 09:10:12 -070049 return Status::Unavailable();
Alexei Frolov062ed182020-09-28 16:23:06 -070050 }
51
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070052 if (call == nullptr || call->id() != packet.call_id()) {
Alexei Frolov86e05de2021-10-19 16:52:31 -070053 // The call for the packet does not exist. If the packet is a server stream
54 // message, notify the server so that it can kill the stream. Otherwise,
55 // silently drop the packet (as it would terminate the RPC anyway).
56 if (packet.type() == PacketType::SERVER_STREAM) {
Wyatt Hepler3be460d2021-09-09 16:28:12 -070057 channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()))
58 .IgnoreError();
Wyatt Heplerb1c47992022-01-06 11:05:50 -080059 PW_LOG_WARN("RPC client received stream message for an unknown call");
60 } else {
61 internal::rpc_lock().unlock();
Wyatt Hepler3be460d2021-09-09 16:28:12 -070062 }
Wyatt Hepler82db4b12021-09-23 09:10:12 -070063 return OkStatus(); // OK since the packet was handled
Alexei Frolov4d2adde2020-08-04 10:19:24 -070064 }
65
66 switch (packet.type()) {
67 case PacketType::RESPONSE:
Wyatt Hepler82db4b12021-09-23 09:10:12 -070068 // RPCs without a server stream include a payload with the final packet.
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070069 if (call->has_server_stream()) {
70 static_cast<internal::StreamResponseClientCall&>(*call).HandleCompleted(
Wyatt Hepler82db4b12021-09-23 09:10:12 -070071 packet.status());
72 } else {
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070073 static_cast<internal::UnaryResponseClientCall&>(*call).HandleCompleted(
Wyatt Hepler82db4b12021-09-23 09:10:12 -070074 packet.payload(), packet.status());
75 }
76 break;
Alexei Frolovd7276222020-10-01 12:41:59 -070077 case PacketType::SERVER_ERROR:
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070078 call->HandleError(packet.status());
Alexei Frolov4d2adde2020-08-04 10:19:24 -070079 break;
Wyatt Hepler5ba80642021-06-18 12:56:17 -070080 case PacketType::SERVER_STREAM:
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070081 if (call->has_server_stream()) {
82 call->HandlePayload(packet.payload());
Wyatt Hepler82db4b12021-09-23 09:10:12 -070083 } else {
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070084 call->HandleError(Status::InvalidArgument());
Wyatt Heplerb1c47992022-01-06 11:05:50 -080085 PW_LOG_DEBUG("Received SERVER_STREAM for RPC without a server stream");
86 internal::rpc_lock().lock();
Wyatt Hepler82db4b12021-09-23 09:10:12 -070087 // Report the error to the server so it can abort the RPC.
88 channel->Send(Packet::ClientError(packet, Status::InvalidArgument()))
89 .IgnoreError(); // Errors are logged in Channel::Send.
90 }
Alexei Frolov4d2adde2020-08-04 10:19:24 -070091 break;
Alexei Frolov4d2adde2020-08-04 10:19:24 -070092 default:
Wyatt Heplerc8bdc7d2021-10-20 09:29:01 -070093 internal::rpc_lock().unlock();
Wyatt Hepler1d000cc2021-10-20 11:05:06 -070094 PW_LOG_WARN("pw_rpc client unable to handle packet of type %u",
Wyatt Hepler82db4b12021-09-23 09:10:12 -070095 static_cast<unsigned>(packet.type()));
Alexei Frolov4d2adde2020-08-04 10:19:24 -070096 }
97
Wyatt Hepler82db4b12021-09-23 09:10:12 -070098 return OkStatus(); // OK since the packet was handled
Alexei Frolov4d2adde2020-08-04 10:19:24 -070099}
100
101} // namespace pw::rpc