Skip to content

Commit

Permalink
Fix ganglion native ble (#581)
Browse files Browse the repository at this point in the history
* update simpleble and fix discovery of ganglions on macos
  • Loading branch information
retiutut committed Dec 4, 2022
1 parent fc8c289 commit dcf3cb3
Show file tree
Hide file tree
Showing 81 changed files with 684 additions and 134 deletions.
64 changes: 64 additions & 0 deletions src/board_controller/ble_lib_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ size_t BLELibBoard::simpleble_adapter_get_count (void)
return func ();
}

bool BLELibBoard::simpleble_adapter_is_bluetooth_enabled (void)
{
std::lock_guard<std::mutex> lock (BLELibBoard::mutex);
if (BLELibBoard::dll_loader == NULL)
{
safe_logger (spdlog::level::err, "BLELibBoard::dll_loader is not initialized");
return false;
}
bool (*func) (void) = (bool (*) (void))BLELibBoard::dll_loader->get_address (
"simpleble_adapter_is_bluetooth_enabled");
if (func == NULL)
{
safe_logger (spdlog::level::err,
"failed to get function address for simpleble_adapter_is_bluetooth_enabled");
return false;
}

return func ();
}

simpleble_adapter_t BLELibBoard::simpleble_adapter_get_handle (size_t index)
{
std::lock_guard<std::mutex> lock (BLELibBoard::mutex);
Expand Down Expand Up @@ -232,6 +252,50 @@ simpleble_err_t BLELibBoard::simpleble_adapter_set_callback_on_scan_updated (
return func (handle, callback, userdata);
}

simpleble_err_t BLELibBoard::simpleble_adapter_set_callback_on_scan_start (
simpleble_adapter_t handle, void (*callback) (simpleble_adapter_t, void *), void *userdata)
{
std::lock_guard<std::mutex> lock (BLELibBoard::mutex);
if (BLELibBoard::dll_loader == NULL)
{
safe_logger (spdlog::level::err, "BLELibBoard::dll_loader is not initialized");
return SIMPLEBLE_FAILURE;
}
simpleble_err_t (*func) (simpleble_adapter_t, void (*) (simpleble_adapter_t, void *), void *) =
(simpleble_err_t (*) (simpleble_adapter_t, void (*) (simpleble_adapter_t, void *), void *))
BLELibBoard::dll_loader->get_address ("simpleble_adapter_set_callback_on_scan_start");
if (func == NULL)
{
safe_logger (spdlog::level::err,
"failed to get function address for simpleble_adapter_set_callback_on_scan_start");
return SIMPLEBLE_FAILURE;
}

return func (handle, callback, userdata);
}

simpleble_err_t BLELibBoard::simpleble_adapter_set_callback_on_scan_stop (
simpleble_adapter_t handle, void (*callback) (simpleble_adapter_t, void *), void *userdata)
{
std::lock_guard<std::mutex> lock (BLELibBoard::mutex);
if (BLELibBoard::dll_loader == NULL)
{
safe_logger (spdlog::level::err, "BLELibBoard::dll_loader is not initialized");
return SIMPLEBLE_FAILURE;
}
simpleble_err_t (*func) (simpleble_adapter_t, void (*) (simpleble_adapter_t, void *), void *) =
(simpleble_err_t (*) (simpleble_adapter_t, void (*) (simpleble_adapter_t, void *), void *))
BLELibBoard::dll_loader->get_address ("simpleble_adapter_set_callback_on_scan_stop");
if (func == NULL)
{
safe_logger (spdlog::level::err,
"failed to get function address for simpleble_adapter_set_callback_on_scan_stop");
return SIMPLEBLE_FAILURE;
}

return func (handle, callback, userdata);
}

simpleble_err_t BLELibBoard::simpleble_adapter_set_callback_on_scan_found (
simpleble_adapter_t handle,
void (*callback) (
Expand Down
7 changes: 7 additions & 0 deletions src/board_controller/brainalive/brainalive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ int BrainAlive::prepare_session ()
simpleble_adapter_set_callback_on_scan_found (
brainalive_adapter, ::brainalive_adapter_1_on_scan_found, (void *)this);

if (!simpleble_adapter_is_bluetooth_enabled ())
{
safe_logger (spdlog::level::warn, "Probably bluetooth is disabled.");
// dont throw an exception because of this
// https://github.com/OpenBluetoothToolbox/SimpleBLE/issues/115
}

simpleble_adapter_scan_start (brainalive_adapter);
int res = (int)BrainFlowExitCodes::STATUS_OK;
std::unique_lock<std::mutex> lk (m);
Expand Down
5 changes: 5 additions & 0 deletions src/board_controller/inc/ble_lib_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ class BLELibBoard : public Board
void simpleble_free (void *handle);
// adapter
size_t simpleble_adapter_get_count (void);
bool simpleble_adapter_is_bluetooth_enabled (void);
simpleble_adapter_t simpleble_adapter_get_handle (size_t index);
void simpleble_adapter_release_handle (simpleble_adapter_t handle);
simpleble_err_t simpleble_adapter_scan_for (simpleble_adapter_t handle, int timeout_ms);
simpleble_err_t simpleble_adapter_scan_start (simpleble_adapter_t handle);
simpleble_err_t simpleble_adapter_scan_stop (simpleble_adapter_t handle);
simpleble_err_t simpleble_adapter_set_callback_on_scan_start (
simpleble_adapter_t handle, void (*) (simpleble_adapter_t, void *), void *);
simpleble_err_t simpleble_adapter_set_callback_on_scan_stop (
simpleble_adapter_t handle, void (*) (simpleble_adapter_t, void *), void *);
simpleble_err_t simpleble_adapter_set_callback_on_scan_updated (simpleble_adapter_t handle,
void (*) (simpleble_adapter_t, simpleble_peripheral_t, void *), void *);
simpleble_err_t simpleble_adapter_set_callback_on_scan_found (simpleble_adapter_t handle,
Expand Down
7 changes: 7 additions & 0 deletions src/board_controller/muse/muse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ int Muse::prepare_session ()
simpleble_adapter_set_callback_on_scan_found (
muse_adapter, ::adapter_on_scan_found, (void *)this);

if (!simpleble_adapter_is_bluetooth_enabled ())
{
safe_logger (spdlog::level::warn, "Probably bluetooth is disabled.");
// dont throw an exception because of this
// https://github.com/OpenBluetoothToolbox/SimpleBLE/issues/115
}

simpleble_adapter_scan_start (muse_adapter);
int res = (int)BrainFlowExitCodes::STATUS_OK;
std::unique_lock<std::mutex> lk (m);
Expand Down
42 changes: 42 additions & 0 deletions src/board_controller/openbci/ganglion_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
#define GANGLION_NOTIFY_CHAR "2d30c082-f39f-4ce6-923f-3484ea480596"


static void ganglion_adapter_1_on_scan_start (simpleble_adapter_t adapter, void *board)
{
((GanglionNative *)(board))->adapter_1_on_scan_start (adapter);
}

static void ganglion_adapter_1_on_scan_stop (simpleble_adapter_t adapter, void *board)
{
((GanglionNative *)(board))->adapter_1_on_scan_stop (adapter);
}

static void ganglion_adapter_1_on_scan_found (
simpleble_adapter_t adapter, simpleble_peripheral_t peripheral, void *board)
{
Expand Down Expand Up @@ -70,9 +80,26 @@ int GanglionNative::prepare_session ()
return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR;
}

simpleble_adapter_set_callback_on_scan_start (
ganglion_adapter, ::ganglion_adapter_1_on_scan_start, (void *)this);
simpleble_adapter_set_callback_on_scan_stop (
ganglion_adapter, ::ganglion_adapter_1_on_scan_stop, (void *)this);
simpleble_adapter_set_callback_on_scan_found (
ganglion_adapter, ::ganglion_adapter_1_on_scan_found, (void *)this);

#ifdef _WIN32
Sleep (1000);
#else
usleep (1000000);
#endif

if (!simpleble_adapter_is_bluetooth_enabled ())
{
safe_logger (spdlog::level::warn, "Probably bluetooth is disabled.");
// dont throw an exception because of this
// https://github.com/OpenBluetoothToolbox/SimpleBLE/issues/115
}

simpleble_adapter_scan_start (ganglion_adapter);
int res = (int)BrainFlowExitCodes::STATUS_OK;
std::unique_lock<std::mutex> lk (m);
Expand Down Expand Up @@ -351,6 +378,16 @@ int GanglionNative::send_command (std::string config)
return (int)BrainFlowExitCodes::STATUS_OK;
}

void GanglionNative::adapter_1_on_scan_start (simpleble_adapter_t adapter)
{
safe_logger (spdlog::level::trace, "Scan started");
}

void GanglionNative::adapter_1_on_scan_stop (simpleble_adapter_t adapter)
{
safe_logger (spdlog::level::trace, "Scan stopped");
}

void GanglionNative::adapter_1_on_scan_found (
simpleble_adapter_t adapter, simpleble_peripheral_t peripheral)
{
Expand Down Expand Up @@ -379,6 +416,11 @@ void GanglionNative::adapter_1_on_scan_found (
{
found = true;
}
// for some reason device may send Simblee instead Ganglion name
else if (strncmp (peripheral_identified, "Simblee", 7) == 0)
{
found = true;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/board_controller/openbci/inc/ganglion_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class GanglionNative : public BLELibBoard
int config_board (std::string config);
int send_command (std::string config);

void adapter_1_on_scan_start (simpleble_adapter_t adapter);
void adapter_1_on_scan_stop (simpleble_adapter_t adapter);
void adapter_1_on_scan_found (simpleble_adapter_t adapter, simpleble_peripheral_t peripheral);
void read_data (
simpleble_uuid_t service, simpleble_uuid_t characteristic, uint8_t *data, size_t size);
Expand Down
2 changes: 1 addition & 1 deletion third_party/SimpleBLE/.github/workflows/ci_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
DEBIAN_FRONTEND: noninteractive
- name: Run Cppcheck
# TODO: Fix the suppressed file.
run: cppcheck --suppress=objectIndex:simpleble/src/windows/Utils.cpp --std=c++17 --error-exitcode=1 --xml --xml-version=2 --force . 2>cppcheck_res.xml
run: cppcheck --suppress=objectIndex:simpleble/src/backends/windows/Utils.cpp --std=c++17 --error-exitcode=1 --xml --xml-version=2 --force . 2>cppcheck_res.xml
- name: Generate Report
if: ${{ failure() }}
run: cppcheck-htmlreport --title=SimpleBLE --file=cppcheck_res.xml --report-dir=report
Expand Down
19 changes: 16 additions & 3 deletions third_party/SimpleBLE/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@ Changelog

All notable changes to this project will be documented in this file.

The format is based on `Keep a Changelog`_, and this project adheres to
`Semantic Versioning`_.
The format is based on `Keep a Changelog`_, and this project adheres to `Semantic Versioning`_.

[0.6.0] - 2022-XX-XX
--------------------

**Added**

- Option to build SimpleBLE plain-flavored (without any BLE code) for testing and debugging purposes.
- Support for advertized services.
- Support for GATT Characteristic properties.
- Retrieve the MTU value of an established connection. *(Thanks Marco Cruz!)*
- (Windows) Logging of WinRT initialization behavior.
- (SimpleBluez) Support for GATT characteristic flags.
- (SimpleBluez) Support for GATT characteristic MTUs. *(Thanks Marco Cruz!)*
- (SimpleBluez) Support for advertized services.

**Changed**

- (MacOS) More explicit exception messages.
- (MacOS) 16-bit UUIDs are now presented in their 128-bit form.

**Fixed**

- Fixed incorrect handling of services and characteristics in the Python examples. *(Thanks Carl-CWX!)*
- Incorrect handling of services and characteristics in the Python examples. *(Thanks Carl-CWX!)*
- (MacOS) Increased priority of the dispatch queue to prevent jitter in the incoming data.
- (Windows) Missing peripheral identifier data. *(Thanks eriklins!)*
- (Windows) Multiple initialization of the WinRT backend.


[0.5.0] - 2022-09-25
Expand Down
10 changes: 8 additions & 2 deletions third_party/SimpleBLE/docs/simpleble/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ to build and run SimpleBLE in your specific environment.
Building and Installing the Library (Source)
============================================

When building SimpleBLE from source on Linux, you will need to install the
following dependencies: ::
When building SimpleBLE from source, you will need some dependencies based on your
current operating system.

**Linux** ::

sudo apt install libdbus-1-dev

**Windows** ::

`Windows SDK <https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/>`_ (Version 10.0.19041.0 or higher)

The included CMake build script can be used to build SimpleBLE.
CMake is freely available for download from https://www.cmake.org/download/. ::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ int main() {
std::cout << "Connecting to " << peripheral.identifier() << " [" << peripheral.address() << "]" << std::endl;
peripheral.connect();

std::cout << "Successfully connected, listing services." << std::endl;
std::cout << "Successfully connected." << std::endl;
std::cout << "MTU: " << peripheral.mtu() << std::endl;
for (auto& service : peripheral.services()) {
std::cout << "Service: " << service.uuid() << std::endl;

for (auto& characteristic : service.characteristics()) {
std::cout << " Characteristic: " << characteristic.uuid() << std::endl;

std::cout << " Capabilities: ";
for (auto& capability : characteristic.capabilities()) {
std::cout << capability << " ";
}
std::cout << std::endl;

for (auto& descriptor : characteristic.descriptors()) {
std::cout << " Descriptor: " << descriptor.uuid() << std::endl;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <chrono>
#include <iomanip>
#include <iostream>
#include <thread>
#include <vector>

#include "../common/utils.hpp"

#include "simpleble/SimpleBLE.h"

using namespace std::chrono_literals;

int main() {
auto adapter_optional = Utils::getAdapter();

Expand Down Expand Up @@ -69,6 +73,8 @@ int main() {
Utils::print_byte_array(bytes);
});

std::this_thread::sleep_for(5s);

peripheral.unsubscribe(uuids[selection.value()].first, uuids[selection.value()].second);

peripheral.disconnect();
Expand Down
6 changes: 6 additions & 0 deletions third_party/SimpleBLE/examples/simpleble/cpp/scan/scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ int main() {
std::string peripheral_string = peripherals[i].identifier() + " [" + peripherals[i].address() + "]";

std::cout << "[" << i << "] " << peripheral_string << " " << connectable_string << std::endl;

std::vector<SimpleBLE::Service> services = peripherals[i].services();
for (auto& service : services) {
std::cout << " Service: " << service.uuid() << std::endl;
}

std::map<uint16_t, SimpleBLE::ByteArray> manufacturer_data = peripherals[i].manufacturer_data();
for (auto& [manufacturer_id, data] : manufacturer_data) {
std::cout << " Manufacturer ID: " << manufacturer_id << std::endl;
Expand Down

0 comments on commit dcf3cb3

Please sign in to comment.