From 52450ed3034b0ba058ea2c9f9baa2d5f78df6a94 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 17 Nov 2018 22:38:32 +0100 Subject: apps/ble-bts: o Adding start of health termometer service tool. apps/ble-read-characteristic: o Sart of new tool. apps/ble-inspect-device o Make adapter configurable. other: o UUID fixes and tests. --- apps/CMakeLists.txt | 3 +- apps/ble-bts.cpp | 14 +++--- apps/ble-inspect-device.cpp | 10 +++-- apps/ble-read-characteristic.cpp | 94 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 apps/ble-read-characteristic.cpp (limited to 'apps') diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 6491ef1..3b1235c 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -98,9 +98,10 @@ function(add_app) install(TARGETS ${app} RUNTIME DESTINATION bin) endfunction() +add_app(NAME ble-bts) add_app(NAME ble-inspect-device) +add_app(NAME ble-read-characteristic) add_app(NAME ble-scan) -add_app(NAME ble-bts) add_app(NAME sample-add-timestamp) add_app(NAME sample-convert) add_app(NAME sample-select) diff --git a/apps/ble-bts.cpp b/apps/ble-bts.cpp index 82d6d43..3f79f2c 100644 --- a/apps/ble-bts.cpp +++ b/apps/ble-bts.cpp @@ -59,9 +59,11 @@ public: auto gatt = device->connectGatt(); cout << "Connected" << endl; + gatt->discoverServices(); + auto bts = gatt->findService(uuids::HealthTermometerService); if (!bts) { - cout << "Device does not implement Health thermometer service" << endl; + cout << "Device does not implement Health thermometer service (" << uuids::HealthTermometerService.str() << ")" << endl; return EXIT_FAILURE; } @@ -72,7 +74,7 @@ public: return EXIT_FAILURE; } - with_connection(gatt, tmc.value()); + with_characteristic(gatt, tmc.value()); return EXIT_SUCCESS; } catch (std::runtime_error &ex) { @@ -84,7 +86,7 @@ public: } } - void with_connection(BluetoothGattPtr gatt, BluetoothGattCharacteristicPtr tmc) { + void with_characteristic(const BluetoothGattPtr &gatt, const BluetoothGattCharacteristicPtr &tmc) { auto svc = tmc->getService(); cout << "Reading temp value" << endl; @@ -93,13 +95,13 @@ public: cout << "bytes " << buf.getSize() << endl; - for (int i = 0; i < buf.getSize(); i++) { + for (size_t i = 0; i < buf.getSize(); i++) { cout << "byte " << i << " = " << hex << buf.peek8(i) << endl; } } }; -} -} +} // namespace apps +} // namespace trygvis int main(int argc, const char *argv[]) { using namespace trygvis::apps; diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp index 883faed..a06d86a 100644 --- a/apps/ble-inspect-device.cpp +++ b/apps/ble-inspect-device.cpp @@ -18,11 +18,15 @@ public: ~ble_inspect_device() override = default; + string adapter_name; + void add_options(po::options_description_easy_init &options) override { + auto adapter_value = po::value<>(&adapter_name)->default_value("0"); + options("adapter", adapter_value, "Which adapter to use."); options("device", po::value()->required(), "The MAC of the device to inspect"); } - void scan_callback(const shared_ptr &device) { + void with_device(const shared_ptr &device) { cout << "Inspecting device: " << device->getMac().str() << endl; auto gatt = device->connectGatt(); @@ -54,11 +58,11 @@ public: try { Mac mac = Mac::parseMac(mac_str); - auto adapter = bluetoothSystem.getAdapter("0"); + auto adapter = bluetoothSystem.getAdapter(adapter_name); auto device = adapter->getDevice(mac); - scan_callback(device); + with_device(device); return EXIT_SUCCESS; } catch (std::runtime_error &ex) { diff --git a/apps/ble-read-characteristic.cpp b/apps/ble-read-characteristic.cpp new file mode 100644 index 0000000..0aa5fdc --- /dev/null +++ b/apps/ble-read-characteristic.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include "ble/Bluetooth.h" +#include "apps.h" + +namespace trygvis { +namespace apps { + +using namespace std; +using namespace trygvis::bluetooth; +using namespace trygvis::apps; + +class ble_inspect_device : public app { +public: + ble_inspect_device() : app("ble-inspect-device") {} + + ~ble_inspect_device() override = default; + + string adapter_name; + + void add_options(po::options_description_easy_init &options) override { + auto adapter_value = po::value<>(&adapter_name)->default_value("0"); + options("adapter", adapter_value, "Which adapter to use."); + options("device", po::value()->required(), "The MAC of the device to inspect"); + options("service", po::value()->required(), "The UUID of the service to read"); + options("characteristic", po::value()->required(), "The UUID of the characteristic to read"); + } + + int with_device(const shared_ptr &device, const Uuid &service_uuid, const Uuid &characteristic_uuid) { + cout << "Inspecting device: " << device->getMac().str() << endl; + + auto gatt = device->connectGatt(); + cout << "Connected, discovering services" << endl; + + gatt->discoverServices(); + + auto serviceO = gatt->findService(service_uuid); + if (!serviceO) { + cout << "Device does not requested service (" << service_uuid.str() << ")" << endl; + return EXIT_FAILURE; + } + + auto service = serviceO->get(); + auto characteristicO = service->findCharacteristic(characteristic_uuid); + + if (!characteristicO) { + cout << "The device does not have requested characteristic (" << characteristic_uuid << ")" << endl; + return EXIT_FAILURE; + } + + auto characteristic = characteristicO.value(); + + gatt->readValue(characteristic); + + return EXIT_SUCCESS; + } + + int main(app_execution &execution) override { + string mac_str = execution.vm["device"].as(); + string service_str = execution.vm["service"].as(); + string characteristic_str = execution.vm["characteristic"].as(); + + auto service_uuid = Uuid::fromString(service_str); + auto characteristic_uuid = Uuid::fromString(characteristic_str); + + BluetoothSystem bluetoothSystem; + + try { + Mac mac = Mac::parseMac(mac_str); + + auto adapter = bluetoothSystem.getAdapter(adapter_name); + + auto device = adapter->getDevice(mac); + + return with_device(device, service_uuid.value(), characteristic_uuid.value()); + } catch (std::runtime_error &ex) { + cout << "std::runtime_error: " << ex.what() << endl; + return EXIT_FAILURE; + } catch (std::exception &ex) { + cout << "std::exception: " << ex.what() << endl; + return EXIT_FAILURE; + } + } +}; +} +} + +int main(int argc, const char *argv[]) { + using namespace trygvis::apps; + + return real_main(new ble_inspect_device(), argc, argv); +} -- cgit v1.2.3