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 22 #include "common/contextual_callback.h" 23 #include "hci/address.h" 24 #include "module.h" 25 26 namespace bluetooth { 27 namespace hci { 28 namespace acl_manager { 29 30 // The AclScheduler is responsible for *scheduling* ACL connection-related operations (outgoing connections, 31 // incoming connections, and remote name requests). It maintains a queue of operations initiated by us, and tracks 32 // all incoming connections. We should never initiate a connection operation directly - instead, it should always 33 // pass through this class, so that we can be sure that it does not conflict with other operations. 34 // 35 // However, it does not perform any actual HCI operations itself - it simply takes in callbacks, and executes them 36 // at the appropriate time. 37 class AclScheduler : public bluetooth::Module { 38 public: 39 // Schedule an ACL Create Connection request 40 void EnqueueOutgoingAclConnection(Address address, common::ContextualOnceCallback<void()> start_connection); 41 42 // Inform the scheduler that we are handling an incoming connection. This will block all future outgoing ACL 43 // connection events until the incoming connection is deregistered. 44 void RegisterPendingIncomingConnection(Address address); 45 46 // Report that an ACL connection has completed, and dispatch to the appropriate callback based on the internal 47 // state. Then, start the next operation. 48 virtual void ReportAclConnectionCompletion( 49 Address address, 50 common::ContextualOnceCallback<void()> handle_outgoing_connection, 51 common::ContextualOnceCallback<void()> handle_incoming_connection, 52 common::ContextualOnceCallback<void(std::string)> handle_unknown_connection); 53 54 // Same as above, but for the outgoing ACL connection in particular (and no callbacks) 55 void ReportOutgoingAclConnectionFailure(); 56 57 // Cancel an ACL connection. If the request is already outgoing, we will invoke cancel_connection, without clearing 58 // the outgoing request. Otherwise, we will remove the request from the queue, invoke cancel_connection_completed, 59 // and execute the next request in the queue. 60 void CancelAclConnection( 61 Address address, 62 common::ContextualOnceCallback<void()> cancel_connection, 63 common::ContextualOnceCallback<void()> cancel_connection_completed); 64 65 // Schedule a Remote Name Request. When the request is started, start_request will be invoked. If the request is 66 // cancelled before it is dequeued, cancel_request_completed will be invoked. 67 void EnqueueRemoteNameRequest( 68 Address address, 69 common::ContextualOnceCallback<void()> start_request, 70 common::ContextualOnceCallback<void()> cancel_request_completed); 71 72 // Report that a Remote Name Request connection has completed, so we can resume popping from the queue. 73 void ReportRemoteNameRequestCompletion(Address address); 74 75 // Cancel an Remote Name Request. If the request is already outgoing, we will invoke cancel_request, without 76 // clearing the outgoing request. Otherwise, we will invoke the cancel_request_completed callback registered on 77 // the initial enqueue. 78 void CancelRemoteNameRequest(Address address, common::ContextualOnceCallback<void()> cancel_request); 79 80 private: 81 struct impl; 82 std::unique_ptr<impl> pimpl_; 83 84 protected: 85 void ListDependencies(ModuleList* list) const override; 86 void Start() override; 87 void Stop() override; ToString()88 std::string ToString() const override { 89 return std::string("AclSchedulerModule"); 90 } 91 92 public: 93 static const ModuleFactory Factory; 94 AclScheduler(); 95 virtual ~AclScheduler(); 96 }; 97 98 } // namespace acl_manager 99 } // namespace hci 100 } // namespace bluetooth 101