1 /*
2  * Copyright 2019 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 #pragma once
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "common/callback.h"
22 #include "hci/address_with_type.h"
23 #include "hci/hci_packets.h"
24 #include "hci/le_scanning_callback.h"
25 #include "hci/uuid.h"
26 #include "module.h"
27 
28 namespace bluetooth {
29 namespace hci {
30 
31 enum class BatchScanMode : uint8_t {
32   DISABLE = 0,
33   TRUNCATED = 1,
34   FULL = 2,
35   TRUNCATED_AND_FULL = 3,
36 };
37 
38 class LeScanningManager : public bluetooth::Module {
39 public:
40   static constexpr uint8_t kMaxAppNum = 32;
41   static constexpr uint8_t kAdvertisingDataInfoNotPresent = 0xff;
42   static constexpr uint8_t kTxPowerInformationNotPresent = 0x7f;
43   static constexpr uint8_t kNotPeriodicAdvertisement = 0x00;
44   static constexpr ScannerId kInvalidScannerId = 0xFF;
45   LeScanningManager();
46   LeScanningManager(const LeScanningManager&) = delete;
47   LeScanningManager& operator=(const LeScanningManager&) = delete;
48 
49   virtual void RegisterScanner(const Uuid app_uuid);
50 
51   virtual void Unregister(ScannerId scanner_id);
52 
53   virtual void Scan(bool start);
54 
55   virtual void SetScanParameters(ScannerId scanner_id, LeScanType scan_type, uint16_t scan_interval,
56                                  uint16_t scan_window, uint8_t scan_phy);
57 
58   virtual void SetScanFilterPolicy(LeScanningFilterPolicy filter_policy);
59 
60   /* Scan filter */
61   virtual void ScanFilterEnable(bool enable);
62 
63   virtual void ScanFilterParameterSetup(ApcfAction action, uint8_t filter_index,
64                                         AdvertisingFilterParameter advertising_filter_parameter);
65 
66   virtual void ScanFilterAdd(uint8_t filter_index,
67                              std::vector<AdvertisingPacketContentFilterCommand> filters);
68 
69   /*Batch Scan*/
70   virtual void BatchScanConifgStorage(uint8_t batch_scan_full_max, uint8_t batch_scan_truncated_max,
71                                       uint8_t batch_scan_notify_threshold, ScannerId scanner_id);
72   virtual void BatchScanEnable(BatchScanMode scan_mode, uint32_t duty_cycle_scan_window_slots,
73                                uint32_t duty_cycle_scan_interval_slots,
74                                BatchScanDiscardRule batch_scan_discard_rule);
75   virtual void BatchScanDisable();
76   virtual void BatchScanReadReport(ScannerId scanner_id, BatchScanMode scan_mode);
77 
78   virtual void StartSync(uint8_t sid, const AddressWithType& address, uint16_t skip,
79                          uint16_t timeout, int reg_id);
80 
81   virtual void StopSync(uint16_t handle);
82 
83   virtual void CancelCreateSync(uint8_t sid, const Address& address);
84 
85   virtual void TransferSync(const Address& address, uint16_t handle, uint16_t service_data,
86                             uint16_t sync_handle, int pa_source);
87 
88   virtual void TransferSetInfo(const Address& address, uint16_t handle, uint16_t service_data,
89                                uint8_t adv_handle, int pa_source);
90 
91   virtual void SyncTxParameters(const Address& addr, uint8_t mode, uint16_t skip, uint16_t timeout,
92                                 int reg_id);
93 
94   virtual void TrackAdvertiser(uint8_t filter_index, ScannerId scanner_id);
95 
96   virtual void RegisterScanningCallback(ScanningCallback* scanning_callback);
97 
98   virtual bool IsAdTypeFilterSupported() const;
99 
100   static const ModuleFactory Factory;
101 
102 protected:
103   void ListDependencies(ModuleList* list) const override;
104 
105   void Start() override;
106 
107   void Stop() override;
108 
109   std::string ToString() const override;
110 
111 private:
112   struct impl;
113   std::unique_ptr<impl> pimpl_;
114 };
115 
116 }  // namespace hci
117 }  // namespace bluetooth
118