• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 <tuple>
17 
18 #include "pw_assert/assert.h"
19 #include "pw_containers/intrusive_list.h"
20 #include "pw_result/result.h"
21 #include "pw_rpc/internal/call.h"
22 #include "pw_rpc/internal/channel.h"
23 #include "pw_rpc/internal/channel_list.h"
24 #include "pw_rpc/internal/lock.h"
25 #include "pw_rpc/internal/packet.h"
26 #include "pw_span/span.h"
27 #include "pw_sync/lock_annotations.h"
28 
29 namespace pw::rpc::internal {
30 
31 class LockedEndpoint;
32 
33 // Manages a list of channels and a list of ongoing calls for either a server or
34 // client.
35 //
36 // For clients, calls start when they send a REQUEST packet to a server. For
37 // servers, calls start when the REQUEST packet is received. In either case,
38 // calls add themselves to the Endpoint's list when they're started and
39 // remove themselves when they complete. Calls do this through their associated
40 // Server or Client object, which derive from Endpoint.
41 class Endpoint {
42  public:
43   // If an endpoint is deleted, all calls using it are closed without notifying
44   // the other endpoint.
~Endpoint()45   ~Endpoint() PW_LOCKS_EXCLUDED(rpc_lock()) { RemoveAllCalls(); }
46 
47   // Public functions
48 
49   // Creates a channel with the provided ID and ChannelOutput, if a channel slot
50   // is available or can be allocated (if PW_RPC_DYNAMIC_ALLOCATION is enabled).
51   // Returns:
52   //
53   //   OK - the channel was opened successfully
54   //   ALREADY_EXISTS - a channel with this ID is already present; remove it
55   //       first
56   //   RESOURCE_EXHAUSTED - no unassigned channels are available and
57   //       PW_RPC_DYNAMIC_ALLOCATION is disabled
58   //
OpenChannel(uint32_t id,ChannelOutput & interface)59   Status OpenChannel(uint32_t id, ChannelOutput& interface)
60       PW_LOCKS_EXCLUDED(rpc_lock()) {
61     RpcLockGuard lock;
62     return channels_.Add(id, interface);
63   }
64 
65   // Closes a channel and terminates any pending calls on that channel.
66   // If the calls are client requests, their on_error callback will be
67   // called with the ABORTED status.
68   Status CloseChannel(uint32_t channel_id) PW_LOCKS_EXCLUDED(rpc_lock());
69 
70   // Internal functions, hidden by the Client and Server classes
71 
72   // Returns the number calls in the RPC calls list.
active_call_count()73   size_t active_call_count() const PW_LOCKS_EXCLUDED(rpc_lock()) {
74     RpcLockGuard lock;
75     return calls_.size();
76   }
77 
78   // Claims that `rpc_lock()` is held, returning a wrapped endpoint.
79   //
80   // This function should only be called in contexts in which it is clear that
81   // `rpc_lock()` is held. When calling this function from a constructor, the
82   // lock annotation will not result in errors, so care should be taken to
83   // ensure that `rpc_lock()` is held.
84   LockedEndpoint& ClaimLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
85 
86   // Finds an internal::Channel with this ID or nullptr if none matches.
GetInternalChannel(uint32_t channel_id)87   Channel* GetInternalChannel(uint32_t channel_id)
88       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
89     return channels_.Get(channel_id);
90   }
91 
92   // Loops until the list of calls to clean up is empty. Releases the RPC lock.
93   //
94   // This must be called after operations that potentially put calls in the
95   // awaiting cleanup state:
96   //
97   // - Creating a new call object, either from handling a request on the server
98   //   or starting a new call on the client.
99   // - Processing a stream message, since decoding to Nanopb or pwpb could fail,
100   //   and the RPC mutex should not be released yet.
101   // - Calls to CloseChannel() or UnregisterService(), which may need to cancel
102   //   multiple calls before the mutex is released.
103   //
104   void CleanUpCalls() PW_UNLOCK_FUNCTION(rpc_lock());
105 
106  protected:
107   _PW_RPC_CONSTEXPR Endpoint() = default;
108 
109   // Initializes the endpoint from a span of channels.
Endpoint(span<rpc::Channel> channels)110   _PW_RPC_CONSTEXPR Endpoint(span<rpc::Channel> channels)
111       : channels_(span(static_cast<internal::Channel*>(channels.data()),
112                        channels.size())) {}
113 
114   // Parses an RPC packet and sets ongoing_call to the matching call, if any.
115   // Returns the parsed packet or an error.
116   Result<Packet> ProcessPacket(span<const std::byte> data,
117                                Packet::Destination destination)
118       PW_LOCKS_EXCLUDED(rpc_lock());
119 
120   // Finds a call object for an ongoing call associated with this packet, if
121   // any. The iterator will be calls_end() if no match was found.
FindCall(const Packet & packet)122   IntrusiveList<Call>::iterator FindCall(const Packet& packet)
123       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
124     return std::get<1>(FindIteratorsForCall(packet.channel_id(),
125                                             packet.service_id(),
126                                             packet.method_id(),
127                                             packet.call_id()));
128   }
129 
130   // Used to check if a call iterator is valid or not.
calls_end()131   IntrusiveList<Call>::const_iterator calls_end() const
132       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
133     return calls_.end();
134   }
135 
136   // Aborts calls associated with a particular service. Calls to
137   // AbortCallsForService() must be followed by a call to CleanUpCalls().
AbortCallsForService(const Service & service)138   void AbortCallsForService(const Service& service)
139       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
140     AbortCalls(AbortIdType::kService, UnwrapServiceId(service.service_id()));
141   }
142 
143   // Marks an active call as awaiting cleanup, moving it from the active calls_
144   // list to the to_cleanup_ list.
145   //
146   // This method is protected so it can be exposed in tests.
CloseCallAndMarkForCleanup(Call & call,Status error)147   void CloseCallAndMarkForCleanup(Call& call, Status error)
148       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
149     call.CloseAndMarkForCleanupFromEndpoint(error);
150     calls_.remove(call);
151     to_cleanup_.push_front(call);
152   }
153 
154   // Iterator version of CloseCallAndMarkForCleanup. Returns the iterator to the
155   // item after the closed call.
CloseCallAndMarkForCleanup(IntrusiveList<Call>::iterator before_call,IntrusiveList<Call>::iterator call_iterator,Status error)156   IntrusiveList<Call>::iterator CloseCallAndMarkForCleanup(
157       IntrusiveList<Call>::iterator before_call,
158       IntrusiveList<Call>::iterator call_iterator,
159       Status error) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
160     Call& call = *call_iterator;
161     call.CloseAndMarkForCleanupFromEndpoint(error);
162     auto next = calls_.erase_after(before_call);
163     to_cleanup_.push_front(call);
164     return next;
165   }
166 
167  private:
168   // Give Call access to the register/unregister functions.
169   friend class Call;
170 
171   enum class AbortIdType : bool { kChannel, kService };
172 
173   // Aborts calls for a particular channel or service and enqueues them for
174   // cleanup. AbortCalls() must be followed by a call to CleanUpCalls().
175   void AbortCalls(AbortIdType type, uint32_t id)
176       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
177 
178   // Returns an ID that can be assigned to a new call.
NewCallId()179   uint32_t NewCallId() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
180     // Call IDs are varint encoded. Limit the varint size to 2 bytes (14 usable
181     // bits).
182     constexpr uint32_t kMaxCallId = 1 << 14;
183     return (++next_call_id_) % kMaxCallId;
184   }
185 
186   // Adds a call to the internal call registry. If a matching call already
187   // exists, it is cancelled. CleanUpCalls() must be called after RegisterCall.
188   void RegisterCall(Call& call) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
189 
190   // Registers a call that is known to be unique. The calls list is NOT checked
191   // for existing calls.
RegisterUniqueCall(Call & call)192   void RegisterUniqueCall(Call& call) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
193     calls_.push_front(call);
194   }
195 
CleanUpCall(Call & call)196   void CleanUpCall(Call& call) PW_UNLOCK_FUNCTION(rpc_lock()) {
197     const bool removed_call_to_cleanup = to_cleanup_.remove(call);
198     PW_DASSERT(removed_call_to_cleanup);  // Should have been awaiting cleanup
199     call.CleanUpFromEndpoint();
200   }
201 
202   // Removes the provided call from the call registry.
UnregisterCall(const Call & call)203   void UnregisterCall(const Call& call)
204       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
205     bool closed_call_was_in_list = calls_.remove(call);
206     PW_DASSERT(closed_call_was_in_list);
207   }
208 
209   std::tuple<IntrusiveList<Call>::iterator, IntrusiveList<Call>::iterator>
210   FindIteratorsForCall(uint32_t channel_id,
211                        uint32_t service_id,
212                        uint32_t method_id,
213                        uint32_t call_id)
214       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
215 
216   std::tuple<IntrusiveList<Call>::iterator, IntrusiveList<Call>::iterator>
FindIteratorsForCall(const Call & call)217   FindIteratorsForCall(const Call& call)
218       PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
219     return FindIteratorsForCall(call.channel_id_locked(),
220                                 call.service_id(),
221                                 call.method_id(),
222                                 call.id());
223   }
224 
225   // Silently closes all calls. Called by the destructor. This is a
226   // non-destructor function so that Clang's lock safety analysis applies.
227   //
228   // Endpoints are not deleted in normal RPC use, and especially would not be
229   // deleted before the calls that use them. To handle this unusual case, all
230   // calls are closed without invoking on_error callbacks. If cleanup tasks are
231   // required, users should perform them before deleting the Endpoint. Cleanup
232   // could be done individually for each call or by closing channels with
233   // CloseChannel.
234   void RemoveAllCalls() PW_LOCKS_EXCLUDED(rpc_lock());
235 
236   ChannelList channels_ PW_GUARDED_BY(rpc_lock());
237 
238   // List of all active calls associated with this endpoint. Calls are added to
239   // this list when they start and removed from it when they finish.
240   IntrusiveList<Call> calls_ PW_GUARDED_BY(rpc_lock());
241 
242   // List of all inactive calls that need to have their on_error callbacks
243   // called. Calling on_error requires releasing the RPC lock, so calls are
244   // added to this list in situations where releasing the mutex could be
245   // problematic.
246   IntrusiveList<Call> to_cleanup_ PW_GUARDED_BY(rpc_lock());
247 
248   uint32_t next_call_id_ PW_GUARDED_BY(rpc_lock()) = 0;
249 };
250 
251 // An `Endpoint` indicating that `rpc_lock()` is held.
252 //
253 // This is used as a constructor argument to supplement
254 // `PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock())`. Current compilers do not enforce
255 // lock annotations on constructors; no warnings or errors are produced when
256 // calling an annotated constructor without holding `rpc_lock()`.
257 class LockedEndpoint : public Endpoint {
258  public:
259   friend class Endpoint;
260   // No public constructor: this is created only via the `ClaimLocked` method on
261   // `Endpoint`.
262   constexpr LockedEndpoint() = delete;
263 };
264 
ClaimLocked()265 inline LockedEndpoint& Endpoint::ClaimLocked() {
266   return *static_cast<LockedEndpoint*>(this);
267 }
268 
269 }  // namespace pw::rpc::internal
270