From 9c095ce3c2165ae9fb976b007ba5c14602d06a0b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 20 Feb 2015 23:35:31 +0100 Subject: wip --- apps/sm-get-value.cpp | 92 +++++++++++++++++++++++--------------------------- ble/Bluetooth.cpp | 14 ++++++++ ble/Bluetooth.h | 11 ++++++ ble/LinuxBluetooth.cpp | 2 -- 4 files changed, 67 insertions(+), 52 deletions(-) diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 232c166..6d7c578 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -1,48 +1,30 @@ -#include #include -#include #include #include #include "Bluetooth.h" -#include "soil-moisture.h" using namespace std; using namespace trygvis::bluetooth; -Mac *targetMac; +typedef boost::uuids::uuid uuid_t; -void scan_callback(BluetoothDevice &device) { - device.adapter().stopScan(); +#define BLUETOOTH_UUID_INITIALIZER \ + { \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, \ + 0x10, 0x00, \ + 0x80, 0x00, \ + 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb \ + }; - if (device.mac() != *targetMac) { - cout << "found device: " << device.mac().str() << ", but not the one we want " << targetMac->str() << endl; - return; - } - - cout << "Connecting to device: " << device.mac().str() << endl; - - device.connect(); - - device.discoverServices(); - - vector services = device.getServices(); - cout << "Device has " << services.size() << " services" << endl; - - for (auto &s: services) { - const vector characteristics = s->getCharacteristics(); +uuid_t trygvis_io_base_uuid = { + 0x32, 0xd0, 0x00, 0x00, + 0x03, 0x5d, + 0x59, 0xc5, + 0x70, 0xd3, + 0xbc, 0x8e, 0x4a, 0x1f, 0xd8, 0x3f}; - cout << "Service: UUID: " << s->getUuid() << ", has " << characteristics.size() << " characteristics" << endl; - - for (auto &c: characteristics) { - cout << "Characteristic: UUID: " << c->getUuid() << ", properties: " << (int) c->getProperties() << endl; - } - } - - boost::uuids::uuid soil_moisture_service; - boost::optional service = device.findService(soil_moisture_service); - - device.disconnect(); -} +uuid_t soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10); int main(int argc, char *argv[]) { if (argc != 2) { @@ -51,25 +33,35 @@ int main(int argc, char *argv[]) { } int e; -// try { - Mac mac = Mac::parseMac(argv[1]); - targetMac = &mac; - BluetoothAdapter &adapter = getAdapter(0); + BluetoothSystem bluetoothSystem; + try { + Mac mac = Mac::parseMac(argv[1]); + + BluetoothAdapter &adapter = getAdapter(0); + + BluetoothDevice &device = adapter.getDevice(mac); - BluetoothDevice &device = adapter.getDevice(mac); + cout << "Connecting to device: " << device.mac().str() << endl; - scan_callback(device); + device.connect(); - e = EXIT_SUCCESS; -// } catch (std::runtime_error ex) { -// W << "std::runtime_error: " << ex.what(); -// e = EXIT_FAILURE; -// } catch (std::exception ex) { -// W << "std::exception: " << ex.what(); -// e = EXIT_FAILURE; -// } + device.discoverServices(); - shutdown(); - return e; + boost::optional service = device.findService(soil_moisture_service); + + if (!service) { + cout << "The device is missing the soil moisture service" << endl; + return EXIT_FAILURE; + } + + device.disconnect(); + return EXIT_SUCCESS; + } catch (std::runtime_error ex) { + W << "std::runtime_error: " << ex.what(); + return EXIT_FAILURE; + } catch (std::exception ex) { + W << "std::exception: " << ex.what(); + return EXIT_FAILURE; + } } diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp index b135282..4ceb3b6 100644 --- a/ble/Bluetooth.cpp +++ b/ble/Bluetooth.cpp @@ -165,6 +165,13 @@ BluetoothAdapter::BluetoothAdapter() { BluetoothAdapter::~BluetoothAdapter() { } +BluetoothSystem::BluetoothSystem() { +} + +BluetoothSystem::~BluetoothSystem() { + shutdown(); +} + /* map adapters; @@ -189,5 +196,12 @@ void shutdown() { shutdownImpl(); } +uuid_t makeUuid(const uuid_t base, uint8_t a, uint8_t b) { + uuid_t copy = base; + copy.data[2] = a; + copy.data[3] = b; + return copy; +} + } }; diff --git a/ble/Bluetooth.h b/ble/Bluetooth.h index e47ff3e..e7b163b 100644 --- a/ble/Bluetooth.h +++ b/ble/Bluetooth.h @@ -149,6 +149,15 @@ protected: virtual ~BluetoothAdapter(); }; +/** + * RAII support. + */ +class BluetoothSystem { +public: + BluetoothSystem(); + ~BluetoothSystem(); +}; + enum AttPduType { ERROR = 0x00, INVALID_HANDLE = 0x01, @@ -200,6 +209,8 @@ BluetoothAdapter &getAdapter(int hciDevice); void shutdown(); +boost::uuids::uuid makeUuid(const boost::uuids::uuid base, uint8_t a, uint8_t b); + } } diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index 4b7f0e0..b687807 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -19,8 +19,6 @@ namespace trygvis { namespace bluetooth { namespace linux { -typedef boost::uuids::uuid uuid_t; - using namespace uuids; class LinuxBluetoothDevice; -- cgit v1.2.3