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 // clang-format off
16 #include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first.
17
18 #include "pw_rpc/nanopb/internal/method.h"
19 // clang-format on
20
21 #include "pb_decode.h"
22 #include "pb_encode.h"
23 #include "pw_log/log.h"
24 #include "pw_rpc/internal/packet.h"
25
26 namespace pw::rpc {
27
28 namespace internal {
29
CallSynchronousUnary(const CallContext & context,const Packet & request,void * request_struct,void * response_struct) const30 void NanopbMethod::CallSynchronousUnary(const CallContext& context,
31 const Packet& request,
32 void* request_struct,
33 void* response_struct) const {
34 if (!DecodeRequest(context, request, request_struct)) {
35 rpc_lock().unlock();
36 return;
37 }
38
39 NanopbServerCall responder(context.ClaimLocked(), MethodType::kUnary);
40 rpc_lock().unlock();
41 const Status status = function_.synchronous_unary(
42 context.service(), request_struct, response_struct);
43 responder.SendUnaryResponse(response_struct, status).IgnoreError();
44 }
45
CallUnaryRequest(const CallContext & context,MethodType type,const Packet & request,void * request_struct) const46 void NanopbMethod::CallUnaryRequest(const CallContext& context,
47 MethodType type,
48 const Packet& request,
49 void* request_struct) const {
50 if (!DecodeRequest(context, request, request_struct)) {
51 rpc_lock().unlock();
52 return;
53 }
54
55 NanopbServerCall server_writer(context.ClaimLocked(), type);
56 rpc_lock().unlock();
57 function_.unary_request(context.service(), request_struct, server_writer);
58 }
59
DecodeRequest(const CallContext & context,const Packet & request,void * proto_struct) const60 bool NanopbMethod::DecodeRequest(const CallContext& context,
61 const Packet& request,
62 void* proto_struct) const {
63 if (serde_.request().Decode(request.payload(), proto_struct).ok()) {
64 return true;
65 }
66
67 // The channel is known to exist. It was found when the request was processed
68 // and the lock has been held since, so GetInternalChannel cannot fail.
69 static_cast<internal::Channel*>(
70 context.server().GetInternalChannel(context.channel_id()))
71 ->Send(Packet::ServerError(request, Status::DataLoss()))
72 .IgnoreError();
73 PW_LOG_WARN("Nanopb failed to decode request payload from channel %u",
74 unsigned(context.channel_id()));
75 return false;
76 }
77
78 } // namespace internal
79 } // namespace pw::rpc
80