diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-11-17 22:38:32 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-11-17 22:38:32 +0100 |
commit | 52450ed3034b0ba058ea2c9f9baa2d5f78df6a94 (patch) | |
tree | 97fe35d5d7fe6fe354b2effe8c0ef4836787eb83 /apps/ble-read-characteristic.cpp | |
parent | a167d6e68e634a70af442cd86e43fd9223b1431c (diff) | |
download | ble-toys-52450ed3034b0ba058ea2c9f9baa2d5f78df6a94.tar.gz ble-toys-52450ed3034b0ba058ea2c9f9baa2d5f78df6a94.tar.bz2 ble-toys-52450ed3034b0ba058ea2c9f9baa2d5f78df6a94.tar.xz ble-toys-52450ed3034b0ba058ea2c9f9baa2d5f78df6a94.zip |
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.
Diffstat (limited to 'apps/ble-read-characteristic.cpp')
-rw-r--r-- | apps/ble-read-characteristic.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
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 <stdexcept> +#include <iostream> +#include <vector> +#include <boost/uuid/uuid_io.hpp> +#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<string>()->required(), "The MAC of the device to inspect"); + options("service", po::value<string>()->required(), "The UUID of the service to read"); + options("characteristic", po::value<string>()->required(), "The UUID of the characteristic to read"); + } + + int with_device(const shared_ptr<BluetoothDevice> &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>(); + string service_str = execution.vm["service"].as<string>(); + string characteristic_str = execution.vm["characteristic"].as<string>(); + + 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); +} |