xref: /aosp_15_r20/external/pigweed/pw_rpc/pwpb/public/pw_rpc/pwpb/internal/common.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 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 #pragma once
15 
16 #include "pw_assert/assert.h"
17 #include "pw_bytes/span.h"
18 #include "pw_protobuf/encoder.h"
19 #include "pw_protobuf/internal/codegen.h"
20 #include "pw_protobuf/stream_decoder.h"
21 #include "pw_rpc/internal/client_call.h"
22 #include "pw_rpc/internal/encoding_buffer.h"
23 #include "pw_rpc/internal/server_call.h"
24 #include "pw_rpc/pwpb/serde.h"
25 #include "pw_span/span.h"
26 #include "pw_status/status.h"
27 #include "pw_status/status_with_size.h"
28 
29 namespace pw::rpc::internal {
30 
31 // Forward declaration to avoid circular include.
32 class PwpbServerCall;
33 
34 // Defines per-message struct type instance of the serializer/deserializer.
35 template <PwpbMessageDescriptor kRequest, PwpbMessageDescriptor kResponse>
36 constexpr PwpbMethodSerde kPwpbMethodSerde(kRequest, kResponse);
37 
38 // [Client] Encodes and sends the initial request message for the call.
39 // active() must be true.
40 template <typename Request>
PwpbSendInitialRequest(ClientCall & call,PwpbSerde serde,const Request & request)41 void PwpbSendInitialRequest(ClientCall& call,
42                             PwpbSerde serde,
43                             const Request& request)
44     PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
45   PW_ASSERT(call.active_locked());
46 
47   Result<ByteSpan> buffer = EncodeToPayloadBuffer(request, serde);
48   if (buffer.ok()) {
49     call.SendInitialClientRequest(*buffer);
50   } else {
51     call.CloseAndMarkForCleanup(buffer.status());
52   }
53 }
54 
55 // [Client/Server] Encodes and sends a client or server stream message.
56 // Returns FAILED_PRECONDITION if active() is false.
57 template <typename Payload>
PwpbSendStream(Call & call,const Payload & payload,const PwpbMethodSerde * serde)58 Status PwpbSendStream(Call& call,
59                       const Payload& payload,
60                       const PwpbMethodSerde* serde)
61     PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
62   if (!call.active_locked()) {
63     return Status::FailedPrecondition();
64   }
65 
66   Result<ByteSpan> buffer = EncodeToPayloadBuffer(
67       payload,
68       call.type() == kClientCall ? serde->request() : serde->response());
69   PW_TRY(buffer);
70 
71   return call.WriteLocked(*buffer);
72 }
73 
74 }  // namespace pw::rpc::internal
75