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 #include "pw_rpc/client.h"
16
17 #include "pw_log/log.h"
18 #include "pw_rpc/internal/packet.h"
19
20 namespace pw::rpc {
21 namespace {
22
23 using internal::BaseClientCall;
24 using internal::Packet;
25 using internal::PacketType;
26
27 } // namespace
28
ProcessPacket(ConstByteSpan data)29 Status Client::ProcessPacket(ConstByteSpan data) {
30 Result<Packet> result = Packet::FromBuffer(data);
31 if (!result.ok()) {
32 PW_LOG_WARN("RPC client failed to decode incoming packet");
33 return Status::DataLoss();
34 }
35
36 Packet& packet = result.value();
37
38 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
54 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
63 if (call == calls_.end()) {
64 PW_LOG_WARN("RPC client received a packet for a request it did not make");
65 channel->Send(Packet::ClientError(packet, Status::FailedPrecondition()));
66 return Status::NotFound();
67 }
68
69 switch (packet.type()) {
70 case PacketType::RESPONSE:
71 case PacketType::SERVER_ERROR:
72 call->HandleResponse(packet);
73 break;
74 case PacketType::SERVER_STREAM_END:
75 call->HandleResponse(packet);
76 RemoveCall(*call);
77 break;
78 default:
79 return Status::Unimplemented();
80 }
81
82 return OkStatus();
83 }
84
RegisterCall(BaseClientCall & call)85 Status 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 OkStatus();
99 }
100
101 } // namespace pw::rpc
102