2020-03-29 17:44:20 -06:00
|
|
|
/*
|
2024-12-12 19:21:03 -07:00
|
|
|
* Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
|
|
|
|
* esp-nimble-cpp, NimBLE-Arduino contributors.
|
2020-03-29 17:44:20 -06:00
|
|
|
*
|
2024-12-12 19:21:03 -07:00
|
|
|
* 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
|
2020-05-13 22:03:56 -06:00
|
|
|
*
|
2024-12-12 19:21:03 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-03-29 17:44:20 -06:00
|
|
|
*
|
2024-12-12 19:21:03 -07:00
|
|
|
* 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.
|
2020-03-29 17:44:20 -06:00
|
|
|
*/
|
2024-12-12 19:21:03 -07:00
|
|
|
|
2024-11-28 09:17:42 -07:00
|
|
|
#ifndef NIMBLE_CPP_SCAN_H_
|
|
|
|
#define NIMBLE_CPP_SCAN_H_
|
2020-03-29 17:44:20 -06:00
|
|
|
|
2020-05-13 22:03:56 -06:00
|
|
|
#include "nimconfig.h"
|
2021-09-06 21:14:43 -06:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-05-13 22:03:56 -06:00
|
|
|
|
2024-11-18 09:37:46 -07:00
|
|
|
# include "NimBLEAdvertisedDevice.h"
|
|
|
|
# include "NimBLEUtils.h"
|
2020-03-29 17:44:20 -06:00
|
|
|
|
2024-11-18 09:37:46 -07:00
|
|
|
# if defined(CONFIG_NIMBLE_CPP_IDF)
|
|
|
|
# include "host/ble_gap.h"
|
|
|
|
# else
|
|
|
|
# include "nimble/nimble/host/include/host/ble_gap.h"
|
|
|
|
# endif
|
2020-03-29 17:44:20 -06:00
|
|
|
|
2024-11-18 09:37:46 -07:00
|
|
|
# include <vector>
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
class NimBLEDevice;
|
|
|
|
class NimBLEScan;
|
|
|
|
class NimBLEAdvertisedDevice;
|
2022-08-27 13:13:27 -06:00
|
|
|
class NimBLEScanCallbacks;
|
2020-05-17 20:21:35 -06:00
|
|
|
class NimBLEAddress;
|
2020-03-29 17:44:20 -06:00
|
|
|
|
|
|
|
/**
|
2020-07-08 19:27:26 -06:00
|
|
|
* @brief A class that contains and operates on the results of a BLE scan.
|
|
|
|
* @details When a scan completes, we have a set of found devices. Each device is described
|
|
|
|
* by a NimBLEAdvertisedDevice object. The number of items in the set is given by
|
2020-03-29 17:44:20 -06:00
|
|
|
* getCount(). We can retrieve a device by calling getDevice() passing in the
|
|
|
|
* index (starting at 0) of the desired device.
|
|
|
|
*/
|
|
|
|
class NimBLEScanResults {
|
2024-11-18 09:37:46 -07:00
|
|
|
public:
|
|
|
|
void dump() const;
|
|
|
|
int getCount() const;
|
|
|
|
const NimBLEAdvertisedDevice* getDevice(uint32_t idx) const;
|
|
|
|
const NimBLEAdvertisedDevice* getDevice(const NimBLEAddress& address) const;
|
|
|
|
std::vector<NimBLEAdvertisedDevice*>::const_iterator begin() const;
|
|
|
|
std::vector<NimBLEAdvertisedDevice*>::const_iterator end() const;
|
|
|
|
|
|
|
|
private:
|
2020-03-29 17:44:20 -06:00
|
|
|
friend NimBLEScan;
|
2024-11-18 09:37:46 -07:00
|
|
|
std::vector<NimBLEAdvertisedDevice*> m_deviceVec;
|
2020-03-29 17:44:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform and manage %BLE scans.
|
|
|
|
*
|
|
|
|
* Scanning is associated with a %BLE client that is attempting to locate BLE servers.
|
|
|
|
*/
|
|
|
|
class NimBLEScan {
|
2024-11-18 09:37:46 -07:00
|
|
|
public:
|
|
|
|
bool start(uint32_t duration, bool isContinue = false, bool restart = true);
|
|
|
|
bool isScanning();
|
|
|
|
void setScanCallbacks(NimBLEScanCallbacks* pScanCallbacks, bool wantDuplicates = false);
|
|
|
|
void setActiveScan(bool active);
|
2024-11-28 09:17:42 -07:00
|
|
|
void setInterval(uint16_t intervalMs);
|
|
|
|
void setWindow(uint16_t windowMs);
|
|
|
|
void setDuplicateFilter(uint8_t enabled);
|
2024-11-18 09:37:46 -07:00
|
|
|
void setLimitedOnly(bool enabled);
|
|
|
|
void setFilterPolicy(uint8_t filter);
|
|
|
|
bool stop();
|
|
|
|
void clearResults();
|
|
|
|
NimBLEScanResults getResults();
|
|
|
|
NimBLEScanResults getResults(uint32_t duration, bool is_continue = false);
|
|
|
|
void setMaxResults(uint8_t maxResults);
|
|
|
|
void erase(const NimBLEAddress& address);
|
|
|
|
void erase(const NimBLEAdvertisedDevice* device);
|
|
|
|
|
2024-11-28 09:17:42 -07:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
enum Phy { SCAN_1M = 0x01, SCAN_CODED = 0x02, SCAN_ALL = 0x03 };
|
|
|
|
void setPhy(Phy phyMask);
|
|
|
|
void setPeriod(uint32_t periodMs);
|
|
|
|
# endif
|
|
|
|
|
2024-11-18 09:37:46 -07:00
|
|
|
private:
|
2020-03-29 17:44:20 -06:00
|
|
|
friend class NimBLEDevice;
|
2020-07-28 20:57:33 -06:00
|
|
|
|
|
|
|
NimBLEScan();
|
|
|
|
~NimBLEScan();
|
2024-11-18 09:37:46 -07:00
|
|
|
static int handleGapEvent(ble_gap_event* event, void* arg);
|
|
|
|
void onHostSync();
|
|
|
|
|
|
|
|
NimBLEScanCallbacks* m_pScanCallbacks;
|
|
|
|
ble_gap_disc_params m_scanParams;
|
|
|
|
NimBLEScanResults m_scanResults;
|
|
|
|
NimBLETaskData* m_pTaskData;
|
|
|
|
uint8_t m_maxResults;
|
2024-11-28 09:17:42 -07:00
|
|
|
|
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
uint8_t m_phy{SCAN_ALL};
|
|
|
|
uint16_t m_period{0};
|
|
|
|
# endif
|
2022-08-27 13:13:27 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A callback handler for callbacks associated device scanning.
|
|
|
|
*/
|
|
|
|
class NimBLEScanCallbacks {
|
2024-11-18 09:37:46 -07:00
|
|
|
public:
|
2022-08-27 13:13:27 -06:00
|
|
|
virtual ~NimBLEScanCallbacks() {}
|
2023-04-06 15:56:53 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Called when a new device is discovered, before the scan result is received (if applicable).
|
|
|
|
* @param [in] advertisedDevice The device which was discovered.
|
|
|
|
*/
|
2024-11-18 09:37:46 -07:00
|
|
|
virtual void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice);
|
2023-04-06 15:56:53 -06:00
|
|
|
|
2022-08-27 13:13:27 -06:00
|
|
|
/**
|
2023-04-06 15:56:53 -06:00
|
|
|
* @brief Called when a new scan result is complete, including scan response data (if applicable).
|
|
|
|
* @param [in] advertisedDevice The device for which the complete result is available.
|
2022-08-27 13:13:27 -06:00
|
|
|
*/
|
2024-11-18 09:37:46 -07:00
|
|
|
virtual void onResult(const NimBLEAdvertisedDevice* advertisedDevice);
|
2023-04-06 15:56:53 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Called when a scan operation ends.
|
|
|
|
* @param [in] scanResults The results of the scan that ended.
|
2024-11-18 09:37:46 -07:00
|
|
|
* @param [in] reason The reason code for why the scan ended.
|
2023-04-06 15:56:53 -06:00
|
|
|
*/
|
2024-11-18 09:37:46 -07:00
|
|
|
virtual void onScanEnd(const NimBLEScanResults& scanResults, int reason);
|
2020-03-29 17:44:20 -06:00
|
|
|
};
|
|
|
|
|
2024-11-28 09:17:42 -07:00
|
|
|
#endif // CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER
|
|
|
|
#endif // NIMBLE_CPP_SCAN_H_
|