blob: ead04c117459193716fdbbe6520de2fc2691f9af [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");
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000065 channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()))
66 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Alexei Frolov4d2adde2020-08-04 10:19:24 -070067 return Status::NotFound();
68 }
69
70 switch (packet.type()) {
71 case PacketType::RESPONSE:
Alexei Frolovd7276222020-10-01 12:41:59 -070072 case PacketType::SERVER_ERROR:
Alexei Frolov4d2adde2020-08-04 10:19:24 -070073 call->HandleResponse(packet);
Wyatt Hepler5ba80642021-06-18 12:56:17 -070074 call->Unregister();
Alexei Frolov4d2adde2020-08-04 10:19:24 -070075 break;
Wyatt Hepler5ba80642021-06-18 12:56:17 -070076 case PacketType::SERVER_STREAM:
Alexei Frolov4d2adde2020-08-04 10:19:24 -070077 call->HandleResponse(packet);
Alexei Frolov4d2adde2020-08-04 10:19:24 -070078 break;
Alexei Frolov4d2adde2020-08-04 10:19:24 -070079 default:
80 return Status::Unimplemented();
81 }
82
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080083 return OkStatus();
Alexei Frolov4d2adde2020-08-04 10:19:24 -070084}
85
86Status Client::RegisterCall(BaseClientCall& call) {
87 auto existing_call = std::find_if(calls_.begin(), calls_.end(), [&](auto& c) {
88 return c.channel().id() == call.channel().id() &&
89 c.service_id() == call.service_id() &&
90 c.method_id() == call.method_id();
91 });
92 if (existing_call != calls_.end()) {
93 PW_LOG_WARN(
94 "RPC client tried to call same method multiple times; aborting.");
95 return Status::FailedPrecondition();
96 }
97
98 calls_.push_front(call);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080099 return OkStatus();
Alexei Frolov4d2adde2020-08-04 10:19:24 -0700100}
101
102} // namespace pw::rpc