• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/internal/nanopb_method.h"
16 
17 #include "pb_decode.h"
18 #include "pb_encode.h"
19 #include "pw_log/log.h"
20 #include "pw_rpc/internal/packet.h"
21 
22 namespace pw::rpc::internal {
23 
24 using std::byte;
25 
CallUnary(ServerCall & call,const Packet & request,void * request_struct,void * response_struct) const26 void NanopbMethod::CallUnary(ServerCall& call,
27                              const Packet& request,
28                              void* request_struct,
29                              void* response_struct) const {
30   if (!DecodeRequest(call.channel(), request, request_struct)) {
31     return;
32   }
33 
34   const Status status = function_.unary(call, request_struct, response_struct);
35   SendResponse(call.channel(), request, response_struct, status);
36 }
37 
CallServerStreaming(ServerCall & call,const Packet & request,void * request_struct) const38 void NanopbMethod::CallServerStreaming(ServerCall& call,
39                                        const Packet& request,
40                                        void* request_struct) const {
41   if (!DecodeRequest(call.channel(), request, request_struct)) {
42     return;
43   }
44 
45   internal::BaseServerWriter server_writer(call);
46   function_.server_streaming(call, request_struct, server_writer);
47 }
48 
DecodeRequest(Channel & channel,const Packet & request,void * proto_struct) const49 bool NanopbMethod::DecodeRequest(Channel& channel,
50                                  const Packet& request,
51                                  void* proto_struct) const {
52   if (serde_.DecodeRequest(proto_struct, request.payload())) {
53     return true;
54   }
55 
56   PW_LOG_WARN("Nanopb failed to decode request payload from channel %u",
57               unsigned(channel.id()));
58   channel.Send(Packet::ServerError(request, Status::DataLoss()));
59   return false;
60 }
61 
SendResponse(Channel & channel,const Packet & request,const void * response_struct,Status status) const62 void NanopbMethod::SendResponse(Channel& channel,
63                                 const Packet& request,
64                                 const void* response_struct,
65                                 Status status) const {
66   Channel::OutputBuffer response_buffer = channel.AcquireBuffer();
67   std::span payload_buffer = response_buffer.payload(request);
68 
69   StatusWithSize encoded = EncodeResponse(response_struct, payload_buffer);
70 
71   if (encoded.ok()) {
72     Packet response = Packet::Response(request);
73 
74     response.set_payload(payload_buffer.first(encoded.size()));
75     response.set_status(status);
76     pw::Status send_status = channel.Send(response_buffer, response);
77     if (send_status.ok()) {
78       return;
79     }
80 
81     PW_LOG_WARN("Failed to send response packet for channel %u, status %u",
82                 unsigned(channel.id()),
83                 send_status.code());
84 
85     // Re-acquire the buffer to encode an error packet.
86     response_buffer = channel.AcquireBuffer();
87   } else {
88     PW_LOG_WARN(
89         "Nanopb failed to encode response packet for channel %u, status %u",
90         unsigned(channel.id()),
91         encoded.status().code());
92   }
93   channel.Send(response_buffer,
94                Packet::ServerError(request, Status::Internal()));
95 }
96 
97 }  // namespace pw::rpc::internal
98