Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / nanopb / nanopb_method.cc
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
26 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
38 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
49 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("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
62 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     if (channel.Send(response_buffer, response).ok()) {
77       return;
78     }
79
80     // Re-acquire the buffer to encode an error packet.
81     response_buffer = channel.AcquireBuffer();
82   }
83
84   PW_LOG_WARN("Failed to encode response packet for channel %u",
85               unsigned(channel.id()));
86   channel.Send(response_buffer,
87                Packet::ServerError(request, Status::Internal()));
88 }
89
90 }  // namespace pw::rpc::internal