229 lines
5.5 KiB
C
229 lines
5.5 KiB
C
/****************************************************************************
|
|
* Copyright (C) 2022 Xiaomi Corporation
|
|
*
|
|
* 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.
|
|
***************************************************************************/
|
|
#ifndef __BT_LE_SCAN_H_
|
|
#define __BT_LE_SCAN_H_
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "bluetooth.h"
|
|
#include "bt_le_advertiser.h"
|
|
#ifndef BTSYMBOLS
|
|
#define BTSYMBOLS(s) s
|
|
#endif
|
|
|
|
#define BLE_SCAN_FILTER_UUID_MAX_NUM 2
|
|
|
|
/**
|
|
* @brief Scan start status code
|
|
*
|
|
*/
|
|
enum {
|
|
BT_SCAN_STATUS_SUCCESS = 0,
|
|
BT_SCAN_STATUS_START_FAIL,
|
|
BT_SCAN_STATUS_NO_PERMISSION,
|
|
BT_SCAN_STATUS_SCANNER_REG_NOMEM,
|
|
BT_SCAN_STATUS_SCANNER_EXISTED,
|
|
BT_SCAN_STATUS_SCANNER_NOT_FOUND,
|
|
BT_SCAN_STATUS_SCANNER_REMOVED
|
|
};
|
|
|
|
/**
|
|
* @brief Scan mode
|
|
*
|
|
*/
|
|
enum {
|
|
BT_SCAN_MODE_LOW_POWER = 0,
|
|
BT_SCAN_MODE_BALANCED,
|
|
BT_SCAN_MODE_LOW_LATENCY,
|
|
};
|
|
|
|
#define SCAN_MODE_LOW_POWER_INTERVAL 0x1000
|
|
#define SCAN_MODE_LOW_POWER_WINDOW 0x100
|
|
#define SCAN_MODE_BALANCED_INTERVAL 0x500
|
|
#define SCAN_MODE_BALANCED_WINDOW 0x140
|
|
#define SCAN_MODE_LOW_LATENCY_INTERVAL 0xA0
|
|
#define SCAN_MODE_LOW_LATENCY_WINDOW 0xA0
|
|
|
|
typedef void bt_scanner_t;
|
|
|
|
typedef enum {
|
|
BT_LE_SCAN_TYPE_PASSIVE = 0,
|
|
BT_LE_SCAN_TYPE_ACTIVE
|
|
} ble_scan_type_t;
|
|
|
|
/**
|
|
* @brief Scan filter policy
|
|
*
|
|
*/
|
|
typedef enum {
|
|
BT_LE_SCAN_POLICY_ACCEPT_ALL = 0,
|
|
BT_LE_SCAN_POLICY_ONLY_WHITE_LIST,
|
|
BT_LE_SCAN_POLICY_ACCEPT_ALL_AND_RPA,
|
|
BT_LE_SCAN_POLICY_ONLY_WHITE_LIST_AND_RPA,
|
|
} ble_scan_filter_type_t;
|
|
|
|
/**
|
|
* @brief Scan result structure
|
|
*
|
|
*/
|
|
typedef struct {
|
|
bt_address_t addr;
|
|
uint8_t dev_type; /* bt_device_type_t */
|
|
int8_t rssi;
|
|
uint8_t addr_type; /* ble_addr_type_t */
|
|
uint8_t adv_type; /* ble_adv_type_t */
|
|
uint8_t length;
|
|
uint8_t pad[1];
|
|
uint8_t adv_data[1];
|
|
} ble_scan_result_t;
|
|
|
|
/**
|
|
* @brief Scan filter policy structure
|
|
*
|
|
*/
|
|
typedef struct {
|
|
uint8_t policy;
|
|
} ble_scan_filter_policy_t;
|
|
|
|
/**
|
|
* @brief Scan settings structure
|
|
*
|
|
*/
|
|
typedef struct {
|
|
uint8_t scan_mode;
|
|
uint8_t legacy;
|
|
uint8_t scan_type; /* ble_scan_type_t */
|
|
uint8_t scan_phy; /* ble_phy_type_t */
|
|
ble_scan_filter_policy_t policy;
|
|
} ble_scan_settings_t;
|
|
|
|
/**
|
|
* @brief Useless
|
|
*
|
|
*/
|
|
typedef struct {
|
|
int scan_interval;
|
|
int scan_window;
|
|
ble_scan_type_t scan_type;
|
|
ble_phy_type_t scan_phy;
|
|
ble_scan_filter_type_t filter_type;
|
|
} ble_scan_params_t;
|
|
|
|
typedef struct {
|
|
uint32_t duration;
|
|
uint32_t period;
|
|
uint16_t uuids[BLE_SCAN_FILTER_UUID_MAX_NUM];
|
|
uint8_t active;
|
|
uint8_t duplicated;
|
|
} ble_scan_filter_t;
|
|
|
|
/**
|
|
* @brief scan result callback
|
|
*
|
|
* @param scanner - scanner handle.
|
|
* @param result - scan result.
|
|
*/
|
|
typedef void (*on_scan_result_cb_t)(bt_scanner_t* scanner, ble_scan_result_t* result);
|
|
|
|
/**
|
|
* @brief Scan start status callback
|
|
*
|
|
* @param scanner - scanner handle.
|
|
* @param result - scan start status, BT_SCAN_STATUS_SUCCESS on success.
|
|
*/
|
|
typedef void (*on_scan_status_cb_t)(bt_scanner_t* scanner, uint8_t status);
|
|
|
|
/**
|
|
* @brief Scan stopped callback
|
|
*
|
|
* @param scanner - scanner handle.
|
|
*/
|
|
typedef void (*on_scan_stopped_cb_t)(bt_scanner_t* scanner);
|
|
|
|
/**
|
|
* @brief Scanner callback structure
|
|
*
|
|
*/
|
|
typedef struct {
|
|
uint32_t size;
|
|
on_scan_result_cb_t on_scan_result;
|
|
on_scan_status_cb_t on_scan_start_status;
|
|
on_scan_stopped_cb_t on_scan_stopped;
|
|
} scanner_callbacks_t;
|
|
|
|
/**
|
|
* @brief Start LE scan
|
|
*
|
|
* @param ins - bluetooth client instance.
|
|
* @param cbs - scan callback function.
|
|
* @return bt_scanner_t* - scanner handle generated by scan manager.
|
|
*/
|
|
bt_scanner_t* BTSYMBOLS(bt_le_start_scan)(bt_instance_t* ins, const scanner_callbacks_t* cbs);
|
|
|
|
/**
|
|
* @brief Start LE scan with scan settings
|
|
*
|
|
* @param ins - bluetooth client instance.
|
|
* @param settings - scan settings with scan mode and scan phy.
|
|
* @param cbs - scan callback function.
|
|
* @return bt_scanner_t* - scanner handle generated by scan manager.
|
|
*/
|
|
bt_scanner_t* BTSYMBOLS(bt_le_start_scan_settings)(bt_instance_t* ins,
|
|
ble_scan_settings_t* settings,
|
|
const scanner_callbacks_t* cbs);
|
|
|
|
/**
|
|
* @brief Start LE scan with scan filters
|
|
*
|
|
* @param ins - bluetooth client instance.
|
|
* @param settings - scan settings with scan mode and scan phy.
|
|
* @param filter_data - filter data.
|
|
* @param filter_length - filter data length.
|
|
* @param cbs - scan callback function.
|
|
* @return bt_scanner_t* - scanner handle generated by scan manager.
|
|
*/
|
|
bt_scanner_t* BTSYMBOLS(bt_le_start_scan_with_filters)(bt_instance_t* ins,
|
|
ble_scan_settings_t* settings,
|
|
ble_scan_filter_t* filter_data,
|
|
const scanner_callbacks_t* cbs);
|
|
|
|
/**
|
|
* @brief Stop LE scan
|
|
*
|
|
* @param ins - bluetooth client instance.
|
|
* @param scanner - scanner handle generated by scan manager.
|
|
*/
|
|
void BTSYMBOLS(bt_le_stop_scan)(bt_instance_t* ins, bt_scanner_t* scanner);
|
|
|
|
/**
|
|
* @brief Check LE scan is support
|
|
*
|
|
* @param ins - bluetooth client instance.
|
|
* @return true - support.
|
|
* @return false - not support.
|
|
*/
|
|
bool BTSYMBOLS(bt_le_scan_is_supported)(bt_instance_t* ins);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* __BT_LE_SCAN_H_ */
|