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 auto call_id = next_call_id_;
184 next_call_id_ = (next_call_id_ + 1) % kMaxCallId;
185
186 // Skip call_id `0` to avoid confusion with legacy servers which use
187 // call_id `0` as `kOpenCallId` or which do not provide call_id at all.
188 if (next_call_id_ == 0) {
189 next_call_id_ = 1;
190 }
191
192 return call_id;
193 }
194
195 // Adds a call to the internal call registry. If a matching call already
196 // exists, it is cancelled. CleanUpCalls() must be called after RegisterCall.
197 void RegisterCall(Call& call) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
198
199 // Registers a call that is known to be unique. The calls list is NOT checked
200 // for existing calls.
RegisterUniqueCall(Call & call)201 void RegisterUniqueCall(Call& call) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
202 calls_.push_front(call);
203 }
204
CleanUpCall(Call & call)205 void CleanUpCall(Call& call) PW_UNLOCK_FUNCTION(rpc_lock()) {
206 const bool removed_call_to_cleanup = to_cleanup_.remove(call);
207 PW_DASSERT(removed_call_to_cleanup); // Should have been awaiting cleanup
208 call.CleanUpFromEndpoint();
209 }
210
211 // Removes the provided call from the call registry.
UnregisterCall(const Call & call)212 void UnregisterCall(const Call& call)
213 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
214 bool closed_call_was_in_list = calls_.remove(call);
215 PW_DASSERT(closed_call_was_in_list);
216 }
217
218 std::tuple<IntrusiveList<Call>::iterator, IntrusiveList<Call>::iterator>
219 FindIteratorsForCall(uint32_t channel_id,
220 uint32_t service_id,
221 uint32_t method_id,
222 uint32_t call_id)
223 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
224
225 std::tuple<IntrusiveList<Call>::iterator, IntrusiveList<Call>::iterator>
FindIteratorsForCall(const Call & call)226 FindIteratorsForCall(const Call& call)
227 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
228 return FindIteratorsForCall(call.channel_id_locked(),
229 call.service_id(),
230 call.method_id(),
231 call.id());
232 }
233
234 // Silently closes all calls. Called by the destructor. This is a
235 // non-destructor function so that Clang's lock safety analysis applies.
236 //
237 // Endpoints are not deleted in normal RPC use, and especially would not be
238 // deleted before the calls that use them. To handle this unusual case, all
239 // calls are closed without invoking on_error callbacks. If cleanup tasks are
240 // required, users should perform them before deleting the Endpoint. Cleanup
241 // could be done individually for each call or by closing channels with
242 // CloseChannel.
243 void RemoveAllCalls() PW_LOCKS_EXCLUDED(rpc_lock());
244
245 ChannelList channels_ PW_GUARDED_BY(rpc_lock());
246
247 // List of all active calls associated with this endpoint. Calls are added to
248 // this list when they start and removed from it when they finish.
249 IntrusiveList<Call> calls_ PW_GUARDED_BY(rpc_lock());
250
251 // List of all inactive calls that need to have their on_error callbacks
252 // called. Calling on_error requires releasing the RPC lock, so calls are
253 // added to this list in situations where releasing the mutex could be
254 // problematic.
255 IntrusiveList<Call> to_cleanup_ PW_GUARDED_BY(rpc_lock());
256
257 // Skip call_id `0` to avoid confusion with legacy servers which use
258 // call_id `0` as `kOpenCallId` or which do not provide call_id at all.
259 uint32_t next_call_id_ PW_GUARDED_BY(rpc_lock()) = 1;
260 };
261
262 // An `Endpoint` indicating that `rpc_lock()` is held.
263 //
264 // This is used as a constructor argument to supplement
265 // `PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock())`. Current compilers do not enforce
266 // lock annotations on constructors; no warnings or errors are produced when
267 // calling an annotated constructor without holding `rpc_lock()`.
268 class LockedEndpoint : public Endpoint {
269 public:
270 friend class Endpoint;
271 // No public constructor: this is created only via the `ClaimLocked` method on
272 // `Endpoint`.
273 constexpr LockedEndpoint() = delete;
274 };
275
ClaimLocked()276 inline LockedEndpoint& Endpoint::ClaimLocked() {
277 return *static_cast<LockedEndpoint*>(this);
278 }
279
280 } // namespace pw::rpc::internal
281