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/channel.h"
22 #include "pw_rpc/internal/call.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::ChannelBase with this ID or nullptr if none matches.
GetInternalChannel(uint32_t channel_id)87 ChannelBase* 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<Channel> channels)110 _PW_RPC_CONSTEXPR Endpoint(span<Channel> channels) : channels_(channels) {}
111
112 // Parses an RPC packet and sets ongoing_call to the matching call, if any.
113 // Returns the parsed packet or an error.
114 Result<Packet> ProcessPacket(span<const std::byte> data,
115 Packet::Destination destination)
116 PW_LOCKS_EXCLUDED(rpc_lock());
117
118 // Finds a call object for an ongoing call associated with this packet, if
119 // any. The iterator will be calls_end() if no match was found.
FindCall(const Packet & packet)120 IntrusiveList<Call>::iterator FindCall(const Packet& packet)
121 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
122 return std::get<1>(FindIteratorsForCall(packet.channel_id(),
123 packet.service_id(),
124 packet.method_id(),
125 packet.call_id()));
126 }
127
128 // Used to check if a call iterator is valid or not.
calls_end()129 IntrusiveList<Call>::const_iterator calls_end() const
130 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
131 return calls_.end();
132 }
133
134 // Aborts calls associated with a particular service. Calls to
135 // AbortCallsForService() must be followed by a call to CleanUpCalls().
AbortCallsForService(const Service & service)136 void AbortCallsForService(const Service& service)
137 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
138 AbortCalls(AbortIdType::kService, UnwrapServiceId(service.service_id()));
139 }
140
141 // Marks an active call as awaiting cleanup, moving it from the active calls_
142 // list to the to_cleanup_ list.
143 //
144 // This method is protected so it can be exposed in tests.
CloseCallAndMarkForCleanup(Call & call,Status error)145 void CloseCallAndMarkForCleanup(Call& call, Status error)
146 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
147 call.CloseAndMarkForCleanupFromEndpoint(error);
148 calls_.remove(call);
149 to_cleanup_.push_front(call);
150 }
151
152 // Iterator version of CloseCallAndMarkForCleanup. Returns the iterator to the
153 // item after the closed call.
CloseCallAndMarkForCleanup(IntrusiveList<Call>::iterator before_call,IntrusiveList<Call>::iterator call_iterator,Status error)154 IntrusiveList<Call>::iterator CloseCallAndMarkForCleanup(
155 IntrusiveList<Call>::iterator before_call,
156 IntrusiveList<Call>::iterator call_iterator,
157 Status error) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
158 Call& call = *call_iterator;
159 call.CloseAndMarkForCleanupFromEndpoint(error);
160 auto next = calls_.erase_after(before_call);
161 to_cleanup_.push_front(call);
162 return next;
163 }
164
165 private:
166 // Give Call access to the register/unregister functions.
167 friend class Call;
168
169 enum class AbortIdType : bool { kChannel, kService };
170
171 // Aborts calls for a particular channel or service and enqueues them for
172 // cleanup. AbortCalls() must be followed by a call to CleanUpCalls().
173 void AbortCalls(AbortIdType type, uint32_t id)
174 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
175
176 // Returns an ID that can be assigned to a new call.
NewCallId()177 uint32_t NewCallId() PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
178 // Call IDs are varint encoded. Limit the varint size to 2 bytes (14 usable
179 // bits).
180 constexpr uint32_t kMaxCallId = 1 << 14;
181 auto call_id = next_call_id_;
182 next_call_id_ = (next_call_id_ + 1) % kMaxCallId;
183
184 // Skip call_id `0` to avoid confusion with legacy servers which use
185 // call_id `0` as `kOpenCallId` or which do not provide call_id at all.
186 if (next_call_id_ == 0) {
187 next_call_id_ = 1;
188 }
189
190 return call_id;
191 }
192
193 // Adds a call to the internal call registry. If a matching call already
194 // exists, it is cancelled. CleanUpCalls() must be called after RegisterCall.
195 void RegisterCall(Call& call) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
196
197 // Registers a call that is known to be unique. The calls list is NOT checked
198 // for existing calls.
RegisterUniqueCall(Call & call)199 void RegisterUniqueCall(Call& call) PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
200 calls_.push_front(call);
201 }
202
CleanUpCall(Call & call)203 void CleanUpCall(Call& call) PW_UNLOCK_FUNCTION(rpc_lock()) {
204 const bool removed_call_to_cleanup = to_cleanup_.remove(call);
205 PW_DASSERT(removed_call_to_cleanup); // Should have been awaiting cleanup
206 call.CleanUpFromEndpoint();
207 }
208
209 // Removes the provided call from the call registry.
UnregisterCall(const Call & call)210 void UnregisterCall(const Call& call)
211 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
212 bool closed_call_was_in_list = calls_.remove(call);
213 PW_DASSERT(closed_call_was_in_list);
214 }
215
216 std::tuple<IntrusiveList<Call>::iterator, IntrusiveList<Call>::iterator>
217 FindIteratorsForCall(uint32_t channel_id,
218 uint32_t service_id,
219 uint32_t method_id,
220 uint32_t call_id)
221 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock());
222
223 std::tuple<IntrusiveList<Call>::iterator, IntrusiveList<Call>::iterator>
FindIteratorsForCall(const Call & call)224 FindIteratorsForCall(const Call& call)
225 PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock()) {
226 return FindIteratorsForCall(call.channel_id_locked(),
227 call.service_id(),
228 call.method_id(),
229 call.id());
230 }
231
232 // Silently closes all calls. Called by the destructor. This is a
233 // non-destructor function so that Clang's lock safety analysis applies.
234 //
235 // Endpoints are not deleted in normal RPC use, and especially would not be
236 // deleted before the calls that use them. To handle this unusual case, all
237 // calls are closed without invoking on_error callbacks. If cleanup tasks are
238 // required, users should perform them before deleting the Endpoint. Cleanup
239 // could be done individually for each call or by closing channels with
240 // CloseChannel.
241 void RemoveAllCalls() PW_LOCKS_EXCLUDED(rpc_lock());
242
243 ChannelList channels_ PW_GUARDED_BY(rpc_lock());
244
245 // List of all active calls associated with this endpoint. Calls are added to
246 // this list when they start and removed from it when they finish.
247 IntrusiveList<Call> calls_ PW_GUARDED_BY(rpc_lock());
248
249 // List of all inactive calls that need to have their on_error callbacks
250 // called. Calling on_error requires releasing the RPC lock, so calls are
251 // added to this list in situations where releasing the mutex could be
252 // problematic.
253 IntrusiveList<Call> to_cleanup_ PW_GUARDED_BY(rpc_lock());
254
255 // Skip call_id `0` to avoid confusion with legacy servers which use
256 // call_id `0` as `kOpenCallId` or which do not provide call_id at all.
257 uint32_t next_call_id_ PW_GUARDED_BY(rpc_lock()) = 1;
258 };
259
260 // An `Endpoint` indicating that `rpc_lock()` is held.
261 //
262 // This is used as a constructor argument to supplement
263 // `PW_EXCLUSIVE_LOCKS_REQUIRED(rpc_lock())`. Current compilers do not enforce
264 // lock annotations on constructors; no warnings or errors are produced when
265 // calling an annotated constructor without holding `rpc_lock()`.
266 class LockedEndpoint : public Endpoint {
267 public:
268 friend class Endpoint;
269 // No public constructor: this is created only via the `ClaimLocked` method on
270 // `Endpoint`.
271 constexpr LockedEndpoint() = delete;
272 };
273
ClaimLocked()274 inline LockedEndpoint& Endpoint::ClaimLocked() {
275 return *static_cast<LockedEndpoint*>(this);
276 }
277
278 } // namespace pw::rpc::internal
279