• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 
15 #pragma once
16 
17 #include <cstdint>
18 #include <optional>
19 
20 #include "pw_bluetooth_proxy/l2cap_status_delegate.h"
21 #include "pw_containers/vector.h"
22 #include "pw_sync/lock_annotations.h"
23 #include "pw_sync/mutex.h"
24 
25 namespace pw::bluetooth::proxy {
26 
27 /// Thread safe collection of service delegates and l2cap channel connections
28 /// currently being tracked.
29 class L2capStatusTracker {
30  public:
31   struct DisconnectParams {
32     uint16_t connection_handle;
33     uint16_t remote_cid;
34     uint16_t local_cid;
35   };
36 
37   void RegisterDelegate(L2capStatusDelegate& delegate)
38       PW_LOCKS_EXCLUDED(mutex_);
39 
40   void UnregisterDelegate(L2capStatusDelegate& delegate)
41       PW_LOCKS_EXCLUDED(mutex_);
42 
43   // Event handlers
44   //
45   // For each event type, this class will hold onto one pending event to be
46   // delivered via `DeliverPendingEvents` to all registered delegates.
47   void HandleConnectionComplete(const L2capChannelConnectionInfo& info)
48       PW_LOCKS_EXCLUDED(mutex_);
49 
50   void HandleAclDisconnectionComplete(uint16_t connection_handle)
51       PW_LOCKS_EXCLUDED(mutex_);
52 
53   void HandleDisconnectionComplete(const DisconnectParams& params)
54       PW_LOCKS_EXCLUDED(mutex_);
55 
56   // Notify any registered delegates of any pending events.
57   //
58   // Call when not holding any locks that would prevent re-entry into ProxyHost
59   // API's. Specifically we want to allow for acquiring new channels within
60   // delegate functions..
61   void DeliverPendingEvents() PW_LOCKS_EXCLUDED(mutex_);
62 
63  private:
64   void DeliverPendingConnectionComplete(const L2capChannelConnectionInfo& info)
65       PW_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
66 
67   void DeliverPendingAclDisconnectionComplete(uint16_t connection_handle)
68       PW_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
69 
70   void DeliverPendingDisconnectionComplete(const DisconnectParams& params)
71       PW_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
72 
73   IntrusiveForwardList<L2capStatusDelegate> delegates_ PW_GUARDED_BY(mutex_);
74 
75   // TODO: https://pwbug.dev/369849508 - Use allocator to let client control max
76   // here.
77   static constexpr size_t kMaxTrackedConnections = 10;
78 
79   // Contains an entry for each channel connection open that has a delegate
80   // registered for it's PSM.
81   Vector<L2capChannelConnectionInfo, kMaxTrackedConnections>
PW_GUARDED_BY(mutex_)82       connected_channel_infos_ PW_GUARDED_BY(mutex_){};
83 
84   std::optional<L2capChannelConnectionInfo> pending_connection_complete_
85       PW_GUARDED_BY(mutex_);
86   std::optional<uint16_t> pending_acl_disconnection_complete_
87       PW_GUARDED_BY(mutex_);
88   std::optional<DisconnectParams> pending_disconnection_complete_
89       PW_GUARDED_BY(mutex_);
90 
91   sync::Mutex mutex_;
92 };
93 
94 }  // namespace pw::bluetooth::proxy
95