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 20 #include "common/callback.h" 21 #include "hci/address_with_type.h" 22 #include "hci/hci_packets.h" 23 #include "hci/uuid.h" 24 #include "module.h" 25 26 namespace bluetooth { 27 namespace hci { 28 29 using ScannerId = uint8_t; 30 31 class AdvertisingFilterOnFoundOnLostInfo { 32 public: 33 uint8_t scanner_id; 34 uint8_t filter_index; 35 uint8_t advertiser_state; 36 AdvtInfoPresent advertiser_info_present; 37 Address advertiser_address; 38 uint8_t advertiser_address_type; 39 uint8_t tx_power; 40 int8_t rssi; 41 uint16_t time_stamp; 42 std::vector<uint8_t> adv_packet; 43 std::vector<uint8_t> scan_response; 44 }; 45 46 class ScanningCallback { 47 public: 48 enum ScanningStatus { 49 SUCCESS, 50 NO_RESOURCES = 0x80, 51 INTERNAL_ERROR = 0x85, 52 }; 53 54 virtual ~ScanningCallback() = default; 55 virtual void OnScannerRegistered( 56 const bluetooth::hci::Uuid app_uuid, ScannerId scanner_id, ScanningStatus status) = 0; 57 virtual void OnScanResult( 58 uint16_t event_type, 59 uint8_t address_type, 60 Address address, 61 uint8_t primary_phy, 62 uint8_t secondary_phy, 63 uint8_t advertising_sid, 64 int8_t tx_power, 65 int8_t rssi, 66 uint16_t periodic_advertising_interval, 67 std::vector<uint8_t> advertising_data) = 0; 68 virtual void OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info) = 0; 69 virtual void OnBatchScanReports( 70 int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data) = 0; 71 virtual void OnBatchScanThresholdCrossed(int client_if) = 0; 72 virtual void OnTimeout() = 0; 73 virtual void OnFilterEnable(Enable enable, uint8_t status) = 0; 74 virtual void OnFilterParamSetup(uint8_t available_spaces, ApcfAction action, uint8_t status) = 0; 75 virtual void OnFilterConfigCallback( 76 ApcfFilterType filter_type, uint8_t available_spaces, ApcfAction action, uint8_t status) = 0; 77 }; 78 79 class AdvertisingPacketContentFilterCommand { 80 public: 81 ApcfFilterType filter_type; 82 Address address; 83 ApcfApplicationAddressType application_address_type; 84 Uuid uuid; 85 Uuid uuid_mask; 86 std::vector<uint8_t> name; 87 uint16_t company; 88 uint16_t company_mask; 89 std::vector<uint8_t> data; 90 std::vector<uint8_t> data_mask; 91 }; 92 93 class AdvertisingFilterParameter { 94 public: 95 uint16_t feature_selection; 96 uint16_t list_logic_type; 97 uint8_t filter_logic_type; 98 uint8_t rssi_high_thresh; 99 DeliveryMode delivery_mode; 100 uint16_t onfound_timeout; 101 uint8_t onfound_timeout_cnt; 102 uint8_t rssi_low_thres; 103 uint16_t onlost_timeout; 104 uint16_t num_of_tracking_entries; 105 }; 106 107 enum class BatchScanMode : uint8_t { 108 DISABLE = 0, 109 TRUNCATED = 1, 110 FULL = 2, 111 TRUNCATED_AND_FULL = 3, 112 }; 113 114 class LeScanningManager : public bluetooth::Module { 115 public: 116 static constexpr uint8_t kMaxAppNum = 32; 117 static constexpr uint8_t kAdvertisingDataInfoNotPresent = 0xff; 118 static constexpr uint8_t kTxPowerInformationNotPresent = 0x7f; 119 static constexpr uint8_t kNotPeriodicAdvertisement = 0x00; 120 static constexpr ScannerId kInvalidScannerId = 0xFF; 121 LeScanningManager(); 122 123 void RegisterScanner(const Uuid app_uuid); 124 125 void Unregister(ScannerId scanner_id); 126 127 void Scan(bool start); 128 129 void SetScanParameters(LeScanType scan_type, uint16_t scan_interval, uint16_t scan_window); 130 131 /* Scan filter */ 132 void ScanFilterEnable(bool enable); 133 134 void ScanFilterParameterSetup( 135 ApcfAction action, uint8_t filter_index, AdvertisingFilterParameter advertising_filter_parameter); 136 137 void ScanFilterAdd(uint8_t filter_index, std::vector<AdvertisingPacketContentFilterCommand> filters); 138 139 /*Batch Scan*/ 140 void BatchScanConifgStorage( 141 uint8_t batch_scan_full_max, 142 uint8_t batch_scan_truncated_max, 143 uint8_t batch_scan_notify_threshold, 144 ScannerId scanner_id); 145 void BatchScanEnable( 146 BatchScanMode scan_mode, 147 uint32_t duty_cycle_scan_window_slots, 148 uint32_t duty_cycle_scan_interval_slots, 149 BatchScanDiscardRule batch_scan_discard_rule); 150 void BatchScanDisable(); 151 void BatchScanReadReport(ScannerId scanner_id, BatchScanMode scan_mode); 152 153 void TrackAdvertiser(ScannerId scanner_id); 154 155 void RegisterScanningCallback(ScanningCallback* scanning_callback); 156 157 static const ModuleFactory Factory; 158 159 protected: 160 void ListDependencies(ModuleList* list) override; 161 162 void Start() override; 163 164 void Stop() override; 165 166 std::string ToString() const override; 167 168 private: 169 struct impl; 170 std::unique_ptr<impl> pimpl_; 171 DISALLOW_COPY_AND_ASSIGN(LeScanningManager); 172 }; 173 174 } // namespace hci 175 } // namespace bluetooth 176