Skip to content

Commit 8dfb6dc

Browse files
committed
Some hardening for misbehaving apps (e.g. calling connect while scanning)
1 parent a8462ae commit 8dfb6dc

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ set(CPACK_PACKAGE_INSTALL_DIRECTORY /usr CACHE STRING "Install directory (defaul
4545
if (ENV{TRAVIS_TAG})
4646
set(CPACK_PACKAGE_VERSION $ENV{TRAVIS_TAG})
4747
else()
48-
set(CPACK_PACKAGE_VERSION 1.0.2)
48+
set(CPACK_PACKAGE_VERSION 1.0.3)
4949
endif()
5050
set(CPACK_PACKAGE_CONTACT "Pat Deegan, https://psychogenic.com")
5151
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "C++ Wrapper for gattlib, to access GATT information from Bluetooth Low Energy (BLE) devices")

gattlibpp/BLECentral.cpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,42 @@ bool BLECentral::scan(SecondsValue runSeconds,
306306
BLECENTRAL_ENSURE_ADAPTER_AVAIL(failure);
307307

308308
if (is_scanning) {
309+
/*
310+
AsyncAction::Details asyncAlreadyRunningAct("scanrunning", scanCompleted, failure);
311+
asyncAlreadyRunningAct.trigger = [](AsyncAction::Details * thisAction) {
312+
thisAction->completedWithSuccess();
313+
};
309314
310-
BLECENTRAL_TRIGGER_CB_IFSET(failure, "already scanning");
311-
return false;
315+
queueAsyncAction(asyncAlreadyRunningAct);
316+
*/
317+
return true;
312318
}
313319

320+
if (connected_to.size() && ! numAsyncActionsQueued()) {
321+
BLECNTL_DEBUGLN("asked to scan but we're connected and there's nothing queued to disconn... do that first");
322+
323+
Device::Details * dets = deviceDetails(connected_to);
324+
if (dets && dets->connection) {
325+
// we really seem to be still connected to this thing
326+
AsyncAction::Details autoDisconnAction("autodisconn", [](){
327+
BLECNTL_DEBUGLN("auto disconn completed");
328+
}, [](){
329+
BLECNTL_DEBUGLN("hm, auto disconn failed?");
330+
});
331+
332+
UUID devId = dets->id;
333+
334+
autoDisconnAction.trigger = [this, devId](AsyncAction::Details * thisAction){
335+
336+
this->disconnect(devId, [](){
337+
}, [](){
338+
});
339+
};
340+
341+
}
342+
}
343+
344+
314345

315346
devDiscoveredCb = deviceDiscoveredCb;
316347

@@ -320,7 +351,7 @@ bool BLECentral::scan(SecondsValue runSeconds,
320351
if (gattlib_adapter_scan_enable_async(adapter, clib_device_discovered_callback,
321352
runSeconds, clib_scan_complete_callback) != 0)
322353
{
323-
354+
this->is_scanning = false;
324355
thisAction->completedWithFailure();
325356
}
326357

@@ -329,27 +360,6 @@ bool BLECentral::scan(SecondsValue runSeconds,
329360

330361
queueAsyncAction(asyncAct);
331362

332-
333-
334-
#if 0
335-
// let's push this onto the queue, because bluez has
336-
// a tendency to spit out all the cached deviced immediately,
337-
// and we want to ensure that
338-
// a) we're well into 'scanning mode' when the discovered callback triggers, and
339-
// b) we want the discovered cb to alway be called in the same context -- meaning
340-
// if the async processing happens is some thread, then every cb should happen
341-
// in that thread.
342-
reportQueue.push_back([runSeconds, this, failure](){
343-
344-
if (gattlib_adapter_scan_enable_async(adapter, clib_device_discovered_callback,
345-
runSeconds, clib_scan_complete_callback) != 0)
346-
{
347-
BLECENTRAL_TRIGGER_CB_IFSET(failure, "scan enable call failed");
348-
}
349-
350-
});
351-
#endif
352-
353363
is_scanning = true;
354364
return true;
355365
}
@@ -381,7 +391,13 @@ bool BLECentral::stopScan(Callbacks::SuccessNotification succ,
381391
Callbacks::Error failure) {
382392
BLECENTRAL_ENSURE_ADAPTER_AVAIL(failure);
383393
if (! is_scanning) {
384-
BLECENTRAL_TRIGGER_CB_IFSET(failure, "not currently scanning");
394+
395+
AsyncAction::Details alreadyDoneAct("stopScanDone", succ, failure);
396+
alreadyDoneAct.trigger = [](AsyncAction::Details * thisAction) {
397+
thisAction->completedWithSuccess();
398+
};
399+
queueAsyncAction(alreadyDoneAct);
400+
return true;
385401
}
386402

387403
// we are currently scanning, which means we have an async op running
@@ -962,7 +978,7 @@ void BLECentral::deviceDiscovered(const Discovery::Device & dev) {
962978

963979
void BLECentral::scanCompleted() {
964980

965-
// is_scanning = false;
981+
is_scanning = false;
966982
currentOpCompleted();
967983

968984
}

include/gattlibpp/BLECentral.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class BLECentral {
442442
void autoDiscoverServicesStep(Device::Details * onDevice);
443443
void autoDiscoverCharacteristicsStep(Device::Details * onDevice);
444444
void queueAsyncAction(const AsyncAction::Details & dets);
445-
445+
inline AsyncActionQueue::size_type numAsyncActionsQueued() { return asyncActions.size(); }
446446

447447
AdapterPtr adapter;
448448
AdapterName adapter_name;

0 commit comments

Comments
 (0)