aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-21 10:28:00 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-21 10:28:00 +0100
commit7c9f266cdb37e215279208198dfe009dc66c5daa (patch)
tree131334cd6e14c5d14e2a07c06aff9920744fb246 /apps
parent9c095ce3c2165ae9fb976b007ba5c14602d06a0b (diff)
downloadble-toys-7c9f266cdb37e215279208198dfe009dc66c5daa.tar.gz
ble-toys-7c9f266cdb37e215279208198dfe009dc66c5daa.tar.bz2
ble-toys-7c9f266cdb37e215279208198dfe009dc66c5daa.tar.xz
ble-toys-7c9f266cdb37e215279208198dfe009dc66c5daa.zip
o Creating a class to access GATT.
Diffstat (limited to 'apps')
-rw-r--r--apps/ble-inspect-device.cpp56
-rw-r--r--apps/sm-get-value.cpp27
2 files changed, 39 insertions, 44 deletions
diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp
index 361311c..e50f6ae 100644
--- a/apps/ble-inspect-device.cpp
+++ b/apps/ble-inspect-device.cpp
@@ -7,25 +7,16 @@
using namespace std;
using namespace trygvis::bluetooth;
-Mac *targetMac;
+void scan_callback(BluetoothDevice & device) {
+ cout << "Inspecting device: " << device.getMac().str() << endl;
-void scan_callback(BluetoothDevice &device) {
- device.adapter().stopScan();
+ auto &gatt = device.connectGatt();
- 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();
+ gatt.discoverServices();
- device.discoverServices();
-
- vector<BluetoothGattService *> services = device.getServices();
+ vector < BluetoothGattService * > services = gatt.getServices();
cout << "Device has " << services.size() << " services" << endl;
-
+
for (auto &s: services) {
const vector<BluetoothGattCharacteristic *> characteristics = s->getCharacteristics();
@@ -36,7 +27,7 @@ void scan_callback(BluetoothDevice &device) {
}
}
- device.disconnect();
+ gatt.disconnect();
}
int main(int argc, char *argv[]) {
@@ -45,28 +36,23 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
- int e;
-// try {
- Mac mac = Mac::parseMac(argv[1]);
- targetMac = &mac;
+ BluetoothSystem bluetoothSystem;
- BluetoothAdapter &adapter = getAdapter(0);
+ try {
+ Mac mac = Mac::parseMac(argv[1]);
- BluetoothDevice &device = adapter.getDevice(mac);
+ BluetoothAdapter &adapter = getAdapter(0);
- scan_callback(device);
+ BluetoothDevice &device = adapter.getDevice(mac);
-// adapter->runScan(scan_callback);
+ scan_callback(device);
- 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;
-// }
-
- shutdown();
- return e;
+ 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/apps/sm-get-value.cpp b/apps/sm-get-value.cpp
index 6d7c578..9e6ae2c 100644
--- a/apps/sm-get-value.cpp
+++ b/apps/sm-get-value.cpp
@@ -2,11 +2,14 @@
#include <boost/uuid/uuid_io.hpp>
#include <boost/optional.hpp>
#include "Bluetooth.h"
+#include "log.h"
using namespace std;
using namespace trygvis::bluetooth;
typedef boost::uuids::uuid uuid_t;
+template<class T>
+using o = boost::optional<T>;
#define BLUETOOTH_UUID_INITIALIZER \
{ \
@@ -25,6 +28,7 @@ uuid_t trygvis_io_base_uuid = {
0xbc, 0x8e, 0x4a, 0x1f, 0xd8, 0x3f};
uuid_t soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10);
+uuid_t soil_moisture_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x11);
int main(int argc, char *argv[]) {
if (argc != 2) {
@@ -32,30 +36,35 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
- int e;
-
BluetoothSystem bluetoothSystem;
try {
Mac mac = Mac::parseMac(argv[1]);
- BluetoothAdapter &adapter = getAdapter(0);
+ auto &adapter = getAdapter(0);
- BluetoothDevice &device = adapter.getDevice(mac);
+ auto &device = adapter.getDevice(mac);
- cout << "Connecting to device: " << device.mac().str() << endl;
+ cout << "Connecting to device: " << device.getMac().str() << endl;
- device.connect();
+ auto &gatt = device.connectGatt();
- device.discoverServices();
+ gatt.discoverServices();
- boost::optional<BluetoothGattService *> service = device.findService(soil_moisture_service);
+ auto service = gatt.findService(soil_moisture_service);
if (!service) {
cout << "The device is missing the soil moisture service" << endl;
return EXIT_FAILURE;
}
- device.disconnect();
+ auto c = (*service)->findCharacteristic(soil_moisture_characteristic);
+
+ if (!c) {
+ cout << "The device is missing the soil moisture characteristic" << endl;
+ return EXIT_FAILURE;
+ }
+
+ gatt.disconnect();
return EXIT_SUCCESS;
} catch (std::runtime_error ex) {
W << "std::runtime_error: " << ex.what();