summaryrefslogtreecommitdiff
path: root/system/stack/btm/btm_ble_scanner.cc
blob: df2a8087d0207e006cfd8b5d29d2da3dda6f9a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <base/logging.h>
#include <string.h>

#include <queue>
#include <vector>

#include "ble_scanner.h"
#include "ble_scanner_hci_interface.h"
#include "bt_target.h"
#include "btm_int.h"
#include "btm_int_types.h"
#include "device/include/controller.h"
#include "osi/include/alarm.h"
#include "stack/btm/btm_ble_int.h"
#include "stack_config.h"

std::mutex lock1;

namespace {

class BleScanningManagerImpl;

BleScanningManager* instance;
base::WeakPtr<BleScanningManagerImpl> instance_weakptr;

static void status_callback(uint8_t status) {
  VLOG(1) << __func__ << " Received status_cb with status:" << status;
}

class BleScanningManagerImpl
    : public BleScanningManager,
      public BleScannerHciInterface::ScanEventObserver {
 public:
  BleScanningManagerImpl(BleScannerHciInterface* interface)
      : hci_interface(interface), weak_factory_(this) {}

  ~BleScanningManagerImpl() {}

  void PeriodicScanStart(uint8_t options, uint8_t set_id, uint8_t adv_addr_type,
                         const RawAddress& adv_addr, uint16_t skip_num,
                         uint16_t sync_timeout,
                         uint8_t sync_cte_type) override {
    GetHciInterface()->PeriodicScanStart(options, set_id, adv_addr_type,
                                         adv_addr, skip_num, sync_timeout,
                                         sync_cte_type);
  }

  void PeriodicScanCancelStart() override {
    GetHciInterface()->PeriodicScanCancelStart(base::Bind(&status_callback));
  }

  void PeriodicScanTerminate(uint16_t sync_handle) override {
    GetHciInterface()->PeriodicScanTerminate(sync_handle,
                                             base::Bind(&status_callback));
  }

  void PeriodicAdvSyncTransfer(
      const RawAddress& bd_addr, uint16_t service_data, uint16_t sync_handle,
      BleScannerHciInterface::handle_cb command_complete) override {
    GetHciInterface()->PeriodicAdvSyncTransfer(bd_addr, service_data,
                                               sync_handle, command_complete);
  }

  void PeriodicAdvSetInfoTransfer(const RawAddress& bd_addr,
                                  uint16_t service_data, uint8_t adv_handle,
                                  handle_cb command_complete) override {
    GetHciInterface()->PeriodicAdvSetInfoTransfer(bd_addr, service_data,
                                                  adv_handle, command_complete);
  }

  void SetPeriodicAdvSyncTransferParams(const RawAddress& bd_addr, uint8_t mode,
                                        uint16_t skip, uint16_t sync_timeout,
                                        uint8_t cte_type, bool set_defaults,
                                        status_cb command_complete) override {
    GetHciInterface()->SetPeriodicAdvSyncTransferParams(
        bd_addr, mode, skip, sync_timeout, cte_type, set_defaults,
        command_complete);
  }

  void OnPeriodicScanResult(uint16_t sync_handle, uint8_t tx_power, int8_t rssi,
                            uint8_t cte_type, uint8_t pkt_data_status,
                            uint8_t pkt_data_len,
                            const uint8_t* pkt_data) override {
    btm_ble_periodic_adv_report(sync_handle, tx_power, rssi, cte_type,
                                pkt_data_status, pkt_data_len, pkt_data);
  }

  void OnPeriodicScanEstablished(uint8_t status, uint16_t sync_handle,
                                 uint8_t set_id, uint8_t adv_addr_type,
                                 const RawAddress& adv_addr, uint8_t adv_phy,
                                 uint16_t adv_interval,
                                 uint8_t adv_clock_accuracy) override {
    btm_ble_periodic_adv_sync_established(status, sync_handle, set_id,
                                          adv_addr_type, adv_addr, adv_phy,
                                          adv_interval, adv_clock_accuracy);
  }

  void OnPeriodicScanLost(uint16_t sync_handle) override {
    btm_ble_periodic_adv_sync_lost(sync_handle);
  }

  base::WeakPtr<BleScanningManagerImpl> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

 private:
  BleScannerHciInterface* GetHciInterface() { return hci_interface; }
  BleScannerHciInterface* hci_interface = nullptr;

  // Member variables should appear before the WeakPtrFactory, to ensure
  // that any WeakPtrs are invalidated before its members
  // variable's destructors are executed, rendering them invalid.
  base::WeakPtrFactory<BleScanningManagerImpl> weak_factory_;
};

}  // namespace

void BleScanningManager::Initialize(BleScannerHciInterface* interface) {
  instance = new BleScanningManagerImpl(interface);
  instance_weakptr = ((BleScanningManagerImpl*)instance)->GetWeakPtr();
}

bool BleScanningManager::IsInitialized() { return instance; }

base::WeakPtr<BleScanningManager> BleScanningManager::Get() {
  return instance_weakptr;
};

void BleScanningManager::CleanUp() {
  delete instance;
  instance = nullptr;
};

/**
 * This function initializes the scanning manager.
 **/
void btm_ble_scanner_init() {
  BleScannerHciInterface::Initialize();
  if (BleScannerHciInterface::Get()) {
    BleScanningManager::Initialize(BleScannerHciInterface::Get());
  } else {
    VLOG(1) << __func__ << " BleScannerHciInterface::Get() returns null";
  }
  if ((BleScannerHciInterface::Get()) && (BleScanningManager::Get())) {
    BleScannerHciInterface::Get()->SetScanEventObserver(
        (BleScanningManagerImpl*)BleScanningManager::Get().get());
  } else {
    VLOG(1) << __func__ << " BleScannerHciInterface or BleScanningManager is null";
  }
}

/*******************************************************************************
 *
 * Function         btm_ble_scanner_cleanup
 *
 * Description      This function cleans up scanner control block.
 *
 * Parameters
 * Returns          void
 *
 ******************************************************************************/
void btm_ble_scanner_cleanup(void) {
  std::lock_guard<std::mutex> lock(lock1);
  BleScanningManager::CleanUp();
  BleScannerHciInterface::CleanUp();
}