blob: 7424288c60f3e994bba27cb5bc60c7d11b3d420e [file] [log] [blame]
Alexei Frolov5d6d3922020-05-08 13:57:02 -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/server.h"
16
Wyatt Hepler3e2d7192020-06-11 08:28:21 -070017#include <algorithm>
18
Alexei Frolov5d6d3922020-05-08 13:57:02 -070019#include "pw_log/log.h"
Wyatt Heplerddfc0772021-09-03 16:46:25 -070020#include "pw_rpc/internal/endpoint.h"
Alexei Frolov5d6d3922020-05-08 13:57:02 -070021#include "pw_rpc/internal/packet.h"
Wyatt Hepler80f26ff2020-06-01 09:30:17 -070022#include "pw_rpc/server_context.h"
Alexei Frolov5d6d3922020-05-08 13:57:02 -070023
24namespace pw::rpc {
Wyatt Hepler712d3672020-07-13 15:52:11 -070025namespace {
Alexei Frolov5d6d3922020-05-08 13:57:02 -070026
Wyatt Hepler80f26ff2020-06-01 09:30:17 -070027using std::byte;
28
Alexei Frolov5d6d3922020-05-08 13:57:02 -070029using internal::Packet;
Alexei Frolov33a1e8f2020-05-26 08:39:32 -070030using internal::PacketType;
Alexei Frolov5d6d3922020-05-08 13:57:02 -070031
Wyatt Hepler712d3672020-07-13 15:52:11 -070032} // namespace
33
Wyatt Hepler0f262352020-07-29 09:51:27 -070034Status Server::ProcessPacket(std::span<const byte> data,
35 ChannelOutput& interface) {
Wyatt Heplerddfc0772021-09-03 16:46:25 -070036 internal::Call* call;
37 Result<Packet> result = Endpoint::ProcessPacket(data, Packet::kServer, call);
38
39 if (!result.ok()) {
40 return result.status();
Wyatt Hepler0f262352020-07-29 09:51:27 -070041 }
42
Wyatt Heplerddfc0772021-09-03 16:46:25 -070043 Packet& packet = *result;
Alexei Frolov5d6d3922020-05-08 13:57:02 -070044
Wyatt Heplerddfc0772021-09-03 16:46:25 -070045 internal::Channel* channel = GetInternalChannel(packet.channel_id());
Alexei Frolov5d6d3922020-05-08 13:57:02 -070046 if (channel == nullptr) {
Alexei Frolov33a1e8f2020-05-26 08:39:32 -070047 // If the requested channel doesn't exist, try to dynamically assign one.
48 channel = AssignChannel(packet.channel_id(), interface);
49 if (channel == nullptr) {
Wyatt Hepler3be460d2021-09-09 16:28:12 -070050 // If a channel can't be assigned, send a RESOURCE_EXHAUSTED error. Never
51 // send responses to error messages, though, to avoid infinite cycles.
52 if (packet.type() != PacketType::CLIENT_ERROR) {
53 internal::Channel temp_channel(packet.channel_id(), &interface);
54 temp_channel
55 .Send(Packet::ServerError(packet, Status::ResourceExhausted()))
56 .IgnoreError();
57 }
58 return OkStatus(); // OK since the packet was handled
Alexei Frolov33a1e8f2020-05-26 08:39:32 -070059 }
Alexei Frolov5d6d3922020-05-08 13:57:02 -070060 }
61
Wyatt Hepler0f262352020-07-29 09:51:27 -070062 const auto [service, method] = FindMethod(packet);
63
64 if (method == nullptr) {
Wyatt Hepler3be460d2021-09-09 16:28:12 -070065 // Don't send responses to errors to avoid infinite error cycles.
66 if (packet.type() != PacketType::CLIENT_ERROR) {
67 channel->Send(Packet::ServerError(packet, Status::NotFound()))
68 .IgnoreError();
69 }
70 return OkStatus(); // OK since the packet was handled.
Wyatt Hepler0f262352020-07-29 09:51:27 -070071 }
72
73 switch (packet.type()) {
74 case PacketType::REQUEST: {
Wyatt Hepler85e91402021-08-05 12:48:03 -070075 // If the REQUEST is for an ongoing RPC, cancel it, then call it again.
Wyatt Heplerddfc0772021-09-03 16:46:25 -070076 if (call != nullptr) {
77 call->HandleError(Status::Cancelled());
Wyatt Hepler85e91402021-08-05 12:48:03 -070078 }
79
Wyatt Heplerddfc0772021-09-03 16:46:25 -070080 internal::CallContext context(*this, *channel, *service, *method);
81 method->Invoke(context, packet);
Wyatt Hepler0f262352020-07-29 09:51:27 -070082 break;
83 }
Wyatt Heplera9211162021-06-12 15:40:11 -070084 case PacketType::CLIENT_STREAM:
Wyatt Heplerddfc0772021-09-03 16:46:25 -070085 HandleClientStreamPacket(packet, *channel, call);
Wyatt Hepler0f262352020-07-29 09:51:27 -070086 break;
87 case PacketType::CLIENT_ERROR:
Wyatt Heplerddfc0772021-09-03 16:46:25 -070088 if (call != nullptr) {
89 call->HandleError(packet.status());
Wyatt Hepler01fc15b2021-06-10 18:15:59 -070090 }
Wyatt Hepler0f262352020-07-29 09:51:27 -070091 break;
Wyatt Heplera9211162021-06-12 15:40:11 -070092 case PacketType::CANCEL:
Wyatt Heplerddfc0772021-09-03 16:46:25 -070093 HandleCancelPacket(packet, *channel, call);
Wyatt Hepler0f262352020-07-29 09:51:27 -070094 break;
Wyatt Heplera9211162021-06-12 15:40:11 -070095 case PacketType::CLIENT_STREAM_END:
Wyatt Heplerddfc0772021-09-03 16:46:25 -070096 HandleClientStreamPacket(packet, *channel, call);
Wyatt Heplera9211162021-06-12 15:40:11 -070097 break;
Wyatt Hepler0f262352020-07-29 09:51:27 -070098 default:
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000099 channel->Send(Packet::ServerError(packet, Status::Unimplemented()))
100 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Wyatt Hepler0f262352020-07-29 09:51:27 -0700101 PW_LOG_WARN("Unable to handle packet of type %u",
102 unsigned(packet.type()));
103 }
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800104 return OkStatus();
Wyatt Hepler0f262352020-07-29 09:51:27 -0700105}
106
Wyatt Heplercbd09c22020-09-15 11:17:24 -0700107std::tuple<Service*, const internal::Method*> Server::FindMethod(
Wyatt Hepler0f262352020-07-29 09:51:27 -0700108 const internal::Packet& packet) {
Wyatt Hepler712d3672020-07-13 15:52:11 -0700109 // Packets always include service and method IDs.
Wyatt Hepler3e2d7192020-06-11 08:28:21 -0700110 auto service = std::find_if(services_.begin(), services_.end(), [&](auto& s) {
Wyatt Hepler712d3672020-07-13 15:52:11 -0700111 return s.id() == packet.service_id();
Wyatt Hepler3e2d7192020-06-11 08:28:21 -0700112 });
113
114 if (service == services_.end()) {
Wyatt Hepler0f262352020-07-29 09:51:27 -0700115 return {};
Alexei Frolov5d6d3922020-05-08 13:57:02 -0700116 }
117
Wyatt Hepler0f262352020-07-29 09:51:27 -0700118 return {&(*service), service->FindMethod(packet.method_id())};
Wyatt Hepler712d3672020-07-13 15:52:11 -0700119}
Wyatt Hepler80f26ff2020-06-01 09:30:17 -0700120
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700121void Server::HandleClientStreamPacket(const internal::Packet& packet,
122 internal::Channel& channel,
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700123 internal::Call* call) const {
124 if (call == nullptr) {
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700125 PW_LOG_DEBUG(
126 "Received client stream packet for method that is not pending");
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +0000127 channel.Send(Packet::ServerError(packet, Status::FailedPrecondition()))
128 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700129 return;
130 }
131
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700132 if (!call->has_client_stream()) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +0000133 channel.Send(Packet::ServerError(packet, Status::InvalidArgument()))
134 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700135 return;
136 }
137
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700138 if (!call->client_stream_open()) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +0000139 channel.Send(Packet::ServerError(packet, Status::FailedPrecondition()))
140 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700141 return;
142 }
143
144 if (packet.type() == PacketType::CLIENT_STREAM) {
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700145 call->HandleClientStream(packet.payload());
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700146 } else { // Handle PacketType::CLIENT_STREAM_END.
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700147 call->EndClientStream();
Wyatt Hepler712d3672020-07-13 15:52:11 -0700148 }
Alexei Frolov5d6d3922020-05-08 13:57:02 -0700149}
150
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700151void Server::HandleCancelPacket(const Packet& packet,
152 internal::Channel& channel,
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700153 internal::Call* call) const {
154 if (call == nullptr) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +0000155 channel.Send(Packet::ServerError(packet, Status::FailedPrecondition()))
156 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Wyatt Hepler01fc15b2021-06-10 18:15:59 -0700157 PW_LOG_DEBUG("Received CANCEL packet for method that is not pending");
158 } else {
Wyatt Heplerddfc0772021-09-03 16:46:25 -0700159 call->HandleError(Status::Cancelled());
Alexei Frolov062ed182020-09-28 16:23:06 -0700160 }
161}
162
Alexei Frolov5d6d3922020-05-08 13:57:02 -0700163} // namespace pw::rpc