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 
30 // The RemoteNameRequestModule handles Remote Name Requests, which produce both Remote Name Request
31 // Completed events, and Remote Host Supported Features Notification events.
32 
33 using CompletionCallback = common::ContextualOnceCallback<void(ErrorCode)>;
34 using RemoteHostSupportedFeaturesCallback = common::ContextualOnceCallback<void(uint64_t)>;
35 using RemoteNameCallback =
36         common::ContextualOnceCallback<void(ErrorCode, std::array<uint8_t, 248>)>;
37 
38 // Historical note: This class is intended to provide a shim at the *HCI* layer, so legacy Remote
39 // Name Requests can interoperate with the GD ACL scheduler. Thus, we intentionally do not merge
40 // identical requests, cache responses, or handle request timeouts - we leave this to our callers.
41 // When GD clients start to use this module, richer functionality should be added.
42 class RemoteNameRequestModule : public bluetooth::Module {
43 public:
44   // Dispatch a Remote Name Request
45   void StartRemoteNameRequest(
46           Address address, std::unique_ptr<RemoteNameRequestBuilder> request,
47           CompletionCallback on_completion,
48           RemoteHostSupportedFeaturesCallback on_remote_host_supported_features_notification,
49           RemoteNameCallback on_remote_name_complete);
50 
51   // Cancel a Remote Name Request
52   void CancelRemoteNameRequest(Address address);
53 
54   // Due to controller bugs (b/184239841), an ACL connection completion is sometimes reported in
55   // place of an RNR completion This method lets the ACL manager inform the RNR module if this
56   // happens, since we don't get the appropriate HCI event.
57   void ReportRemoteNameRequestCancellation(Address address);
58 
59 private:
60   struct impl;
61   std::unique_ptr<impl> pimpl_;
62 
63 protected:
64   void ListDependencies(ModuleList* list) const override;
65   void Start() override;
66   void Stop() override;
ToString()67   std::string ToString() const override { return std::string("RemoteNameRequestModule"); }
68 
69 public:
70   static const ModuleFactory Factory;
71   RemoteNameRequestModule();
72   ~RemoteNameRequestModule();
73 };
74 
75 }  // namespace hci
76 }  // namespace bluetooth
77