diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-30 22:17:06 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-30 22:17:06 +0200 |
commit | 25d82b0c52120c81cfed5bc1f245408f08203b7b (patch) | |
tree | 325e95834cadf5d72003bb7f3b64558b4af6faaf /apps/ble-bts.cpp | |
parent | b99017a7938cb36e234025d9ae592bac06ec3ec6 (diff) | |
download | ble-toys-25d82b0c52120c81cfed5bc1f245408f08203b7b.tar.gz ble-toys-25d82b0c52120c81cfed5bc1f245408f08203b7b.tar.bz2 ble-toys-25d82b0c52120c81cfed5bc1f245408f08203b7b.tar.xz ble-toys-25d82b0c52120c81cfed5bc1f245408f08203b7b.zip |
Fixing lots of small nits:
o boost::uuid didn't give much, use our own and add new short uuid type.
o Fixing nits from clang-tidy.
Diffstat (limited to 'apps/ble-bts.cpp')
-rw-r--r-- | apps/ble-bts.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/apps/ble-bts.cpp b/apps/ble-bts.cpp new file mode 100644 index 0000000..1e76f60 --- /dev/null +++ b/apps/ble-bts.cpp @@ -0,0 +1,108 @@ +#include "ble/Bluetooth.h" +#include "apps.h" + +#include <csignal> +#include "apps.h" + +namespace trygvis { +namespace apps { + +using namespace std; +using namespace trygvis::bluetooth; +using namespace trygvis::apps; + +namespace ble_bts_utils { + +static std::function<void(int)> onSignal; + +static void signal_handler(int signal) { + onSignal(signal); +} +} + +class ble_bts : public app { +public: + ble_bts() : app("ble-scan") {} + + ~ble_bts() 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"); + } + + int main(app_execution &execution) override { + BluetoothSystem bluetoothSystem; + shared_ptr<BluetoothAdapter> adapter; + + struct sigaction sigIntHandler{}; + + ble_bts_utils::onSignal = [&](int signal) { adapter->stopScan(); }; + + sigIntHandler.sa_handler = &ble_bts_utils::signal_handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGINT, &sigIntHandler, nullptr); + + try { + adapter = bluetoothSystem.getAdapter(adapter_name); + + string mac_str = execution.vm["device"].as<string>(); + Mac mac = Mac::parseMac(mac_str); + + auto device = adapter->getDevice(mac); + + auto gatt = device->connectGatt(); + cout << "Connected" << endl; + + auto bts = gatt->findService(uuids::HealthTermometerService); + if (!bts) { + cout << "Device does not implement Health thermometer service" << endl; + return EXIT_FAILURE; + } + + auto tmc = bts->get()->findCharacteristic(uuids::TemperatureMeasurement); + + if (!tmc) { + cout << "The device does not have temperature measurement characteristic" << endl; + return EXIT_FAILURE; + } + + with_connection(gatt, tmc.value()); + + return EXIT_SUCCESS; + } 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; + } + } + + void with_connection(BluetoothGattPtr gatt, BluetoothGattCharacteristicPtr tmc) { + auto svc = tmc->getService(); + + cout << "Reading temp value" << endl; + + auto buf = gatt->readValue(tmc); + + cout << "bytes " << buf.getSize() << endl; + + for (int i = 0; i < buf.getSize(); i++) { + cout << "byte " << i << " = " << hex << buf.get8(i) << endl; + } + } +}; +} +} + +int main(int argc, const char *argv[]) { + using namespace trygvis::apps; + + return real_main(new ble_bts(), argc, argv); +} |