• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2016 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 <bluetooth/uuid.h>
20 
21 #include <atomic>
22 #include <functional>
23 #include <map>
24 #include <mutex>
25 
26 #include "service/bluetooth_instance.h"
27 #include "service/common/bluetooth/advertise_data.h"
28 #include "service/common/bluetooth/advertise_settings.h"
29 #include "service/common/bluetooth/low_energy_constants.h"
30 #include "service/common/bluetooth/scan_filter.h"
31 #include "service/common/bluetooth/scan_result.h"
32 #include "service/common/bluetooth/scan_settings.h"
33 #include "service/hal/bluetooth_gatt_interface.h"
34 #include "types/raw_address.h"
35 
36 namespace bluetooth {
37 
38 class Adapter;
39 
40 // A LowEnergyScanner represents an application's handle to perform various
41 // Bluetooth Low Energy GAP operations. Instances cannot be created directly and
42 // should be obtained through the factory.
43 class LowEnergyScanner : private hal::BluetoothGattInterface::ScannerObserver,
44                          public BluetoothInstance {
45  public:
46   // The Delegate interface is used to notify asynchronous events related to LE
47   // scan.
48   class Delegate {
49    public:
50     Delegate() = default;
51     Delegate(const Delegate&) = delete;
52     Delegate& operator=(const Delegate&) = delete;
53 
54     virtual ~Delegate() = default;
55 
56     // Called asynchronously to notify the delegate of nearby BLE advertisers
57     // found during a device scan.
58     virtual void OnScanResult(LowEnergyScanner* client,
59                               const ScanResult& scan_result) = 0;
60   };
61 
62   LowEnergyScanner(const LowEnergyScanner&) = delete;
63   LowEnergyScanner& operator=(const LowEnergyScanner&) = delete;
64 
65   // The destructor automatically unregisters this client instance from the
66   // stack.
67   ~LowEnergyScanner() override;
68 
69   // Assigns a delegate to this instance. |delegate| must out-live this
70   // LowEnergyClient instance.
71   void SetDelegate(Delegate* delegate);
72 
73   // Initiates a BLE device scan for this client using the given |settings| and
74   // |filters|. See the documentation for ScanSettings and ScanFilter for how
75   // these parameters can be configured. Return true on success, false
76   // otherwise. Please see logs for details in case of error.
77   bool StartScan(const ScanSettings& settings,
78                  const std::vector<ScanFilter>& filters);
79 
80   // Stops an ongoing BLE device scan for this client.
81   bool StopScan();
82 
83   // Returns the current scan settings.
scan_settings()84   const ScanSettings& scan_settings() const { return scan_settings_; }
85 
86   // BluetoothInstace overrides:
87   const Uuid& GetAppIdentifier() const override;
88   int GetInstanceId() const override;
89 
90   void ScanResultCallback(hal::BluetoothGattInterface* gatt_iface,
91                           const RawAddress& bda, int rssi,
92                           std::vector<uint8_t> adv_data) override;
93 
94  private:
95   friend class LowEnergyScannerFactory;
96 
97   // Constructor shouldn't be called directly as instances are meant to be
98   // obtained from the factory.
99   LowEnergyScanner(Adapter& adapter, const Uuid& uuid, int scanner_id);
100 
101   // Calls and clears the pending callbacks.
102   void InvokeAndClearStartCallback(BLEStatus status);
103   void InvokeAndClearStopCallback(BLEStatus status);
104 
105   // Raw pointer to the Bluetooth Adapter.
106   Adapter& adapter_;
107 
108   // See getters above for documentation.
109   Uuid app_identifier_;
110   int scanner_id_;
111 
112   // Protects device scan related members below.
113   std::mutex scan_fields_lock_;
114 
115   // Current scan settings.
116   ScanSettings scan_settings_;
117 
118   // If true, then this client have a BLE device scan in progress.
119   std::atomic_bool scan_started_;
120 
121   // Raw handle to the Delegate, which must outlive this LowEnergyScanner
122   // instance.
123   std::mutex delegate_mutex_;
124   Delegate* delegate_;
125 };
126 
127 // LowEnergyScannerFactory is used to register and obtain a per-application
128 // LowEnergyScanner instance. Users should call RegisterInstance to obtain their
129 // own unique LowEnergyScanner instance that has been registered with the
130 // Bluetooth stack.
131 class LowEnergyScannerFactory
132     : private hal::BluetoothGattInterface::ScannerObserver,
133       public BluetoothInstanceFactory {
134  public:
135   // Don't construct/destruct directly except in tests. Instead, obtain a handle
136   // from an Adapter instance.
137   explicit LowEnergyScannerFactory(Adapter& adapter);
138   LowEnergyScannerFactory(const LowEnergyScannerFactory&) = delete;
139   LowEnergyScannerFactory& operator=(const LowEnergyScannerFactory&) = delete;
140 
141   ~LowEnergyScannerFactory() override;
142 
143   // BluetoothInstanceFactory override:
144   bool RegisterInstance(const Uuid& app_uuid,
145                         const RegisterCallback& callback) override;
146 
147  private:
148   friend class LowEnergyScanner;
149 
150   // BluetoothGattInterface::ScannerObserver overrides:
151   void RegisterScannerCallback(const RegisterCallback& callback,
152                                const Uuid& app_uuid, uint8_t scanner_id,
153                                uint8_t status);
154 
155   // Map of pending calls to register.
156   std::mutex pending_calls_lock_;
157   std::unordered_set<Uuid> pending_calls_;
158 
159   // Raw pointer to the Adapter that owns this factory.
160   Adapter& adapter_;
161 };
162 
163 }  // namespace bluetooth
164