• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "common/contextual_callback.h"
24 #include "hci/hci_packets.h"
25 #include "module.h"
26 
27 namespace bluetooth {
28 namespace hci {
29 namespace acl_manager {
30 
31 // The AclScheduler is responsible for *scheduling* ACL connection-related operations (outgoing connections,
32 // incoming connections, and remote name requests). It maintains a queue of operations initiated by us, and tracks
33 // all incoming connections. We should never initiate a connection operation directly - instead, it should always
34 // pass through this class, so that we can be sure that it does not conflict with other operations.
35 //
36 // However, it does not perform any actual HCI operations itself - it simply takes in callbacks, and executes them
37 // at the appropriate time.
38 class AclScheduler : public bluetooth::Module {
39  public:
40   // Schedule an ACL Create Connection request
41   void EnqueueOutgoingAclConnection(Address address, common::ContextualOnceCallback<void()> start_connection);
42 
43   // Inform the scheduler that we are handling an incoming connection. This will block all future outgoing ACL
44   // connection events until the incoming connection is deregistered.
45   void RegisterPendingIncomingConnection(Address address);
46 
47   // Report that an ACL connection has completed, and dispatch to the appropriate callback based on the internal
48   // state. Then, start the next operation.
49   void ReportAclConnectionCompletion(
50       Address address,
51       common::ContextualOnceCallback<void()> handle_outgoing_connection,
52       common::ContextualOnceCallback<void()> handle_incoming_connection,
53       common::ContextualOnceCallback<void(std::string)> handle_unknown_connection);
54 
55   // Same as above, but for the outgoing ACL connection in particular (and no callbacks)
56   void ReportOutgoingAclConnectionFailure();
57 
58   // Cancel an ACL connection. If the request is already outgoing, we will invoke cancel_connection, without clearing
59   // the outgoing request. Otherwise, we will remove the request from the queue, invoke cancel_connection_completed,
60   // and execute the next request in the queue.
61   void CancelAclConnection(
62       Address address,
63       common::ContextualOnceCallback<void()> cancel_connection,
64       common::ContextualOnceCallback<void()> cancel_connection_completed);
65 
66   // Schedule a Remote Name Request. When the request is started, start_request will be invoked. If the request is
67   // cancelled before it is dequeued, cancel_request_completed will be invoked.
68   void EnqueueRemoteNameRequest(
69       Address address,
70       common::ContextualOnceCallback<void()> start_request,
71       common::ContextualOnceCallback<void()> cancel_request_completed);
72 
73   // Report that a Remote Name Request connection has completed, so we can resume popping from the queue.
74   void ReportRemoteNameRequestCompletion(Address address);
75 
76   // Cancel an Remote Name Request. If the request is already outgoing, we will invoke cancel_request, without
77   // clearing the outgoing request. Otherwise, we will invoke the cancel_request_completed callback registered on
78   // the initial enqueue.
79   void CancelRemoteNameRequest(Address address, common::ContextualOnceCallback<void()> cancel_request);
80 
81  private:
82   struct impl;
83   std::unique_ptr<impl> pimpl_;
84 
85  protected:
86   void ListDependencies(ModuleList* list) const override;
87   void Start() override;
88   void Stop() override;
ToString()89   std::string ToString() const override {
90     return std::string("AclSchedulerModule");
91   }
92 
93  public:
94   static const ModuleFactory Factory;
95   AclScheduler();
96   ~AclScheduler();
97 };
98 
99 }  // namespace acl_manager
100 }  // namespace hci
101 }  // namespace bluetooth