From 25d82b0c52120c81cfed5bc1f245408f08203b7b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 30 Aug 2018 22:17:06 +0200 Subject: 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. --- apps/CMakeLists.txt | 1 + apps/SoilMoisture.cpp | 16 +++---- apps/SoilMoisture.h | 4 +- apps/ble-bts.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++ apps/ble-inspect-device.cpp | 2 +- apps/sm-get-value.cpp | 4 +- 6 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 apps/ble-bts.cpp (limited to 'apps') diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 8f8feb3..6491ef1 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -100,6 +100,7 @@ endfunction() add_app(NAME ble-inspect-device) 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/SoilMoisture.cpp b/apps/SoilMoisture.cpp index 4b9dfe5..d92752d 100644 --- a/apps/SoilMoisture.cpp +++ b/apps/SoilMoisture.cpp @@ -7,16 +7,16 @@ namespace sensor { #define BLUETOOTH_UUID_INITIALIZER \ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb }; -auto trygvis_io_base_uuid = - boost::uuids::uuid{0x32, 0xd0, 0x00, 0x00, 0x03, 0x5d, 0x59, 0xc5, 0x70, 0xd3, 0xbc, 0x8e, 0x4a, 0x1f, 0xd8, 0x3f}; +Uuid trygvis_io_base_uuid = + {0x32, 0xd0, 0x00, 0x00, 0x03, 0x5d, 0x59, 0xc5, 0x70, 0xd3, 0xbc, 0x8e, 0x4a, 0x1f, 0xd8, 0x3f}; -auto bluetooth_base_uuid = - boost::uuids::uuid{0x00, 0x00, 0x18, 0x0f, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; +Uuid bluetooth_base_uuid = + {0x00, 0x00, 0x18, 0x0f, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; -const boost::uuids::uuid soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10); -const boost::uuids::uuid soil_moisture_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x11); -const boost::uuids::uuid temperature_characteristic = makeUuid(bluetooth_base_uuid, 0x2a, 0x1e); -const boost::uuids::uuid light_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x12); +const Uuid soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10); +const Uuid soil_moisture_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x11); +const Uuid temperature_characteristic = makeUuid(bluetooth_base_uuid, 0x2a, 0x1e); +const Uuid light_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x12); using namespace trygvis::bluetooth; diff --git a/apps/SoilMoisture.h b/apps/SoilMoisture.h index 0ad1d15..499c9f6 100644 --- a/apps/SoilMoisture.h +++ b/apps/SoilMoisture.h @@ -24,8 +24,8 @@ enum class sm_cmd_code : uint8_t { SM_CMD_FAIL = 255, }; -extern const boost::uuids::uuid soil_moisture_service; -extern const boost::uuids::uuid soil_moisture_characteristic; +extern const Uuid soil_moisture_service; +extern const Uuid soil_moisture_characteristic; class SoilMoisture { public: 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 +#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 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()->required(), "The MAC of the device to inspect"); + } + + int main(app_execution &execution) override { + BluetoothSystem bluetoothSystem; + shared_ptr 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(); + 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); +} diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp index baee93a..8f85e5c 100644 --- a/apps/ble-inspect-device.cpp +++ b/apps/ble-inspect-device.cpp @@ -16,7 +16,7 @@ class ble_inspect_device : public app { public: ble_inspect_device() : app("ble-inspect-device") {} - ~ble_inspect_device() = default; + ~ble_inspect_device() override = default; void add_options(po::options_description_easy_init &options) override { options("device", po::value()->required(), "The MAC of the device to inspect"); diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 483df70..eb7a141 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -153,7 +153,7 @@ public: } // If the user didn't specify any sensors, add all. - if (sensors.size() == 0) { + if (sensors.empty()) { for (unsigned int i = 0; i < sensorCount; i++) { sensorIndexes.push_back(i); } @@ -165,7 +165,7 @@ public: return; } - sensors.push_back(make_pair(i, soilMoisture.getName(i))); + sensors.emplace_back(make_pair(i, soilMoisture.getName(i))); }); } auto mac = gatt->getDevice().getMac().str(); -- cgit v1.2.3