Skip to content

Commit 24460de

Browse files
committed
BLE: Implement new scan API in GenericGap
1 parent 4835a1a commit 24460de

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

features/FEATURE_BLE/ble/generic/GenericGap.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ class GenericGap : public ::Gap,
373373
DisconnectionReason_t reason
374374
);
375375

376+
virtual ble_error_t setScanParameters(const GapScanParameters &params);
377+
378+
virtual ble_error_t startScan(
379+
scanning_filter_duplicates_t filtering,
380+
uint16_t duration_ms,
381+
uint16_t period_ms
382+
);
383+
376384
private:
377385
ble_error_t setAdvertisingData(AdvHandle_t handle, const AdvertisingData& payload, bool minimiseFragmentation, bool scan_reponse);
378386

features/FEATURE_BLE/source/generic/GenericGap.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,92 @@ void GenericGap::on_scan_request_received(
19121912

19131913
}
19141914

1915+
ble_error_t GenericGap::setScanParameters(const GapScanParameters &params)
1916+
{
1917+
use_non_deprecated_scan_api();
1918+
1919+
// FIXME: validate parameters
1920+
// FIXME: deal with random address rotation
1921+
1922+
if (is_extended_advertising_enabled()) {
1923+
bool active_scanning[] = {
1924+
params.get_1m_configuration().active_scanning,
1925+
params.get_coded_configuration().active_scanning
1926+
};
1927+
1928+
uint16_t scan_interval[] = {
1929+
params.get_1m_configuration().interval,
1930+
params.get_coded_configuration().interval
1931+
};
1932+
1933+
uint16_t scan_window[] = {
1934+
params.get_1m_configuration().window,
1935+
params.get_coded_configuration().window
1936+
};
1937+
1938+
return _pal_gap.set_extended_scan_parameters(
1939+
(pal::own_address_type_t::type) params.get_own_address_type(),
1940+
(pal::scanning_filter_policy_t::type) params.get_scanning_filter_policy(),
1941+
params.get_scanning_phys(),
1942+
active_scanning,
1943+
scan_interval,
1944+
scan_window
1945+
);
1946+
} else {
1947+
GapScanParameters::phy_configuration_t legacy_configuration =
1948+
params.get_1m_configuration();
1949+
1950+
return _pal_gap.set_scan_parameters(
1951+
legacy_configuration.active_scanning,
1952+
legacy_configuration.interval,
1953+
legacy_configuration.window,
1954+
(pal::own_address_type_t::type) params.get_own_address_type(),
1955+
(pal::scanning_filter_policy_t::type) params.get_scanning_filter_policy()
1956+
);
1957+
}
1958+
}
1959+
1960+
ble_error_t GenericGap::startScan(
1961+
scanning_filter_duplicates_t filtering,
1962+
uint16_t duration,
1963+
uint16_t period
1964+
)
1965+
{
1966+
use_non_deprecated_scan_api();
1967+
// FIXME: deal with random address rotation
1968+
1969+
if (is_extended_advertising_enabled()) {
1970+
return _pal_gap.extended_scan_enable(
1971+
/* enable */true,
1972+
(pal::duplicates_filter_t::type) filtering,
1973+
duration,
1974+
period
1975+
);
1976+
} else {
1977+
if (period != 0) {
1978+
return BLE_ERROR_INVALID_PARAM;
1979+
}
1980+
1981+
ble_error_t err = _pal_gap.scan_enable(
1982+
true,
1983+
filtering == SCAN_FILTER_DUPLICATES_DISABLED ? false : true
1984+
);
1985+
1986+
if (err) {
1987+
return err;
1988+
}
1989+
1990+
_scan_timeout.detach();
1991+
if (duration) {
1992+
_scan_timeout.attach_us(
1993+
mbed::callback(this, &GenericGap::on_scan_timeout),
1994+
duration * 10 /* ms */ * 1000 /* us */
1995+
);
1996+
}
1997+
1998+
return BLE_ERROR_NONE;
1999+
}
2000+
}
19152001

19162002
void GenericGap::use_deprecated_scan_api() const
19172003
{

0 commit comments

Comments
 (0)