xref: /aosp_15_r20/external/pigweed/pw_rpc/public/pw_rpc/client.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 #pragma once
15 
16 #include <cstddef>
17 
18 #include "pw_bytes/span.h"
19 #include "pw_rpc/channel.h"
20 #include "pw_rpc/internal/endpoint.h"
21 #include "pw_rpc/internal/lock.h"
22 #include "pw_span/span.h"
23 
24 namespace pw::rpc {
25 
26 class Client : public internal::Endpoint {
27  public:
28   // If dynamic allocation is supported, it is not necessary to preallocate a
29   // channels list.
30 #if PW_RPC_DYNAMIC_ALLOCATION
31   _PW_RPC_CONSTEXPR Client() = default;
32 #endif  // PW_RPC_DYNAMIC_ALLOCATION
33 
34   // Creates a client that uses a set of RPC channels. Channels can be shared
35   // between multiple clients and servers.
Client(span<Channel> channels)36   _PW_RPC_CONSTEXPR Client(span<Channel> channels) : Endpoint(channels) {}
37 
38   // Processes an incoming RPC packet. The packet may be an RPC response or a
39   // control packet, the result of which is processed in this function. Returns
40   // whether the packet was able to be processed:
41   //
42   //   OK - The packet was processed by the client.
43   //   DATA_LOSS - Failed to decode the packet.
44   //   INVALID_ARGUMENT - The packet is intended for a server, not a client.
45   //   UNAVAILABLE - No RPC channel with the requested ID was found.
46   //
47   Status ProcessPacket(ConstByteSpan data)
48       PW_LOCKS_EXCLUDED(internal::rpc_lock());
49 
50  private:
51   // Remove these internal::Endpoint functions from the public interface.
52   using Endpoint::active_call_count;
53   using Endpoint::ClaimLocked;
54   using Endpoint::CleanUpCalls;
55   using Endpoint::GetInternalChannel;
56 };
57 
58 }  // namespace pw::rpc
59