blob: f8c1b293aba6be7667343ebb3fffaa9cfdf7515f [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
15#include "pw_rpc/client.h"
16
17#include "pw_log/log.h"
18#include "pw_rpc/internal/packet.h"
19
20namespace pw::rpc {
21namespace {
22
23using internal::BaseClientCall;
24using internal::Packet;
25using internal::PacketType;
26
27} // namespace
28
29Status Client::ProcessPacket(ConstByteSpan data) {
Alexei Frolov3f2d0082020-10-04 22:30:39 -070030 Result<Packet> result = Packet::FromBuffer(data);
31 if (!result.ok()) {
Alexei Frolov4d2adde2020-08-04 10:19:24 -070032 PW_LOG_WARN("RPC client failed to decode incoming packet");
33 return Status::DataLoss();
34 }
35
Alexei Frolov3f2d0082020-10-04 22:30:39 -070036 Packet& packet = result.value();
37
Alexei Frolov4d2adde2020-08-04 10:19:24 -070038 if (packet.destination() != Packet::kClient) {
39 return Status::InvalidArgument();
40 }
41
42 if (packet.channel_id() == Channel::kUnassignedChannelId ||
43 packet.service_id() == 0 || packet.method_id() == 0) {
44 PW_LOG_WARN("RPC client received a malformed packet");
45 return Status::DataLoss();
46 }
47
48 auto call = std::find_if(calls_.begin(), calls_.end(), [&](auto& c) {
49 return c.channel().id() == packet.channel_id() &&
50 c.service_id() == packet.service_id() &&
51 c.method_id() == packet.method_id();
52 });
53
Alexei Frolov062ed182020-09-28 16:23:06 -070054 auto channel = std::find_if(channels_.begin(), channels_.end(), [&](auto& c) {
55 return c.id() == packet.channel_id();
56 });
57
58 if (channel == channels_.end()) {
59 PW_LOG_WARN("RPC client received a packet for an unregistered channel");
60 return Status::NotFound();
61 }
62
Alexei Frolov4d2adde2020-08-04 10:19:24 -070063 if (call == calls_.end()) {
64 PW_LOG_WARN("RPC client received a packet for a request it did not make");
Wyatt Hepler3be460d2021-09-09 16:28:12 -070065 // Don't send responses to errors to avoid infinite error cycles.
66 if (packet.type() != PacketType::SERVER_ERROR) {
67 channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()))
68 .IgnoreError();
69 }
Alexei Frolov4d2adde2020-08-04 10:19:24 -070070 return Status::NotFound();
71 }
72
73 switch (packet.type()) {
74 case PacketType::RESPONSE:
Alexei Frolovd7276222020-10-01 12:41:59 -070075 case PacketType::SERVER_ERROR:
Alexei Frolov4d2adde2020-08-04 10:19:24 -070076 call->HandleResponse(packet);
Wyatt Hepler5ba80642021-06-18 12:56:17 -070077 call->Unregister();
Alexei Frolov4d2adde2020-08-04 10:19:24 -070078 break;
Wyatt Hepler5ba80642021-06-18 12:56:17 -070079 case PacketType::SERVER_STREAM:
Alexei Frolov4d2adde2020-08-04 10:19:24 -070080 call->HandleResponse(packet);
Alexei Frolov4d2adde2020-08-04 10:19:24 -070081 break;
Alexei Frolov4d2adde2020-08-04 10:19:24 -070082 default:
83 return Status::Unimplemented();
84 }
85
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080086 return OkStatus();
Alexei Frolov4d2adde2020-08-04 10:19:24 -070087}
88
89Status Client::RegisterCall(BaseClientCall& call) {
90 auto existing_call = std::find_if(calls_.begin(), calls_.end(), [&](auto& c) {
91 return c.channel().id() == call.channel().id() &&
92 c.service_id() == call.service_id() &&
93 c.method_id() == call.method_id();
94 });
95 if (existing_call != calls_.end()) {
96 PW_LOG_WARN(
97 "RPC client tried to call same method multiple times; aborting.");
98 return Status::FailedPrecondition();
99 }
100
101 calls_.push_front(call);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800102 return OkStatus();
Alexei Frolov4d2adde2020-08-04 10:19:24 -0700103}
104
105} // namespace pw::rpc