blob: 07c26ee3e3b9774a9a2f434ada49a869ec7b4d22 [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");
Alexei Frolov062ed182020-09-28 16:23:06 -070065 channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()));
Alexei Frolov4d2adde2020-08-04 10:19:24 -070066 return Status::NotFound();
67 }
68
69 switch (packet.type()) {
70 case PacketType::RESPONSE:
Alexei Frolovd7276222020-10-01 12:41:59 -070071 case PacketType::SERVER_ERROR:
Alexei Frolov4d2adde2020-08-04 10:19:24 -070072 call->HandleResponse(packet);
73 break;
74 case PacketType::SERVER_STREAM_END:
75 call->HandleResponse(packet);
76 RemoveCall(*call);
77 break;
Alexei Frolov4d2adde2020-08-04 10:19:24 -070078 default:
79 return Status::Unimplemented();
80 }
81
82 return Status::Ok();
83}
84
85Status Client::RegisterCall(BaseClientCall& call) {
86 auto existing_call = std::find_if(calls_.begin(), calls_.end(), [&](auto& c) {
87 return c.channel().id() == call.channel().id() &&
88 c.service_id() == call.service_id() &&
89 c.method_id() == call.method_id();
90 });
91 if (existing_call != calls_.end()) {
92 PW_LOG_WARN(
93 "RPC client tried to call same method multiple times; aborting.");
94 return Status::FailedPrecondition();
95 }
96
97 calls_.push_front(call);
98 return Status::Ok();
99}
100
101} // namespace pw::rpc