aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2018-11-19 23:13:50 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2018-11-19 23:13:50 +0100
commite8aa2eadf309fbc0c0e1418c6bee482e505fa09b (patch)
treee93292292834b8d8959301cd8d02b27090e7e0ca
parentd6f16958eaa966332b911eb9257b8524c2efd772 (diff)
downloadble-toys-e8aa2eadf309fbc0c0e1418c6bee482e505fa09b.tar.gz
ble-toys-e8aa2eadf309fbc0c0e1418c6bee482e505fa09b.tar.bz2
ble-toys-e8aa2eadf309fbc0c0e1418c6bee482e505fa09b.tar.xz
ble-toys-e8aa2eadf309fbc0c0e1418c6bee482e505fa09b.zip
ble-read-characteristic: working READ mode, starting on NOTIFY.
-rw-r--r--apps/SoilMoisture.cpp10
-rw-r--r--apps/SoilMoisture.h4
-rw-r--r--apps/apps.h4
-rw-r--r--apps/ble-bts.cpp3
-rw-r--r--apps/ble-read-characteristic.cpp8
-rw-r--r--apps/mqtt_support.h2
-rw-r--r--ble/BluetoothImpl.h31
-rw-r--r--ble/LinuxBluetooth.cpp11
-rw-r--r--include/ble/Bluetooth.h38
-rw-r--r--include/ble/misc.h6
-rw-r--r--sensor/include/trygvis/sensor.h4
11 files changed, 69 insertions, 52 deletions
diff --git a/apps/SoilMoisture.cpp b/apps/SoilMoisture.cpp
index 79f168f..d7c03bb 100644
--- a/apps/SoilMoisture.cpp
+++ b/apps/SoilMoisture.cpp
@@ -4,23 +4,21 @@
namespace trygvis {
namespace sensor {
-#define BLUETOOTH_UUID_INITIALIZER \
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
+using namespace trygvis::bluetooth;
+using std::to_string;
+using std::bitset;
Uuid trygvis_io_base_uuid =
{0x32, 0xd0, 0x00, 0x00, 0x03, 0x5d, 0x59, 0xc5, 0x70, 0xd3, 0xbc, 0x8e, 0x4a, 0x1f, 0xd8, 0x3f};
Uuid bluetooth_base_uuid =
- {0x00, 0x00, 0x18, 0x0f, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
+ {0x00, 0x00, 0, 0, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
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;
-using std::to_string;
-
static
void createGetSensorCount(ByteBuffer &buffer) {
buffer
diff --git a/apps/SoilMoisture.h b/apps/SoilMoisture.h
index 3bfd0d8..4e6ae17 100644
--- a/apps/SoilMoisture.h
+++ b/apps/SoilMoisture.h
@@ -2,14 +2,14 @@
#include <ble/Bluetooth.h>
#include <boost/uuid/uuid.hpp>
-#include <experimental/optional>
+#include <optional>
namespace trygvis {
namespace sensor {
using namespace trygvis::bluetooth;
template<typename T>
-using o = std::experimental::optional<T>;
+using o = std::optional<T>;
enum class sm_cmd_code : uint8_t {
SM_CMD_GET_SENSOR_COUNT = 1,
diff --git a/apps/apps.h b/apps/apps.h
index 21a25a3..d750543 100644
--- a/apps/apps.h
+++ b/apps/apps.h
@@ -6,7 +6,7 @@
#include <boost/program_options/variables_map.hpp>
#include <stdexcept>
#include <iosfwd>
-#include <experimental/optional>
+#include <optional>
#include <json.hpp>
#include <mutex>
#include <condition_variable>
@@ -15,7 +15,7 @@ namespace trygvis {
namespace apps {
template<typename T>
-using o = std::experimental::optional<T>;
+using o = std::optional<T>;
namespace po = boost::program_options;
using namespace log4cplus;
diff --git a/apps/ble-bts.cpp b/apps/ble-bts.cpp
index 3f79f2c..7b888d4 100644
--- a/apps/ble-bts.cpp
+++ b/apps/ble-bts.cpp
@@ -91,7 +91,8 @@ public:
cout << "Reading temp value" << endl;
- auto buf = gatt->readValue(tmc);
+ StaticByteBuffer<100> response;
+ auto buf = gatt->readValue(tmc, response);
cout << "bytes " << buf.getSize() << endl;
diff --git a/apps/ble-read-characteristic.cpp b/apps/ble-read-characteristic.cpp
index 19bbfa3..8a0a7b6 100644
--- a/apps/ble-read-characteristic.cpp
+++ b/apps/ble-read-characteristic.cpp
@@ -67,7 +67,7 @@ public:
}
int with_device(const shared_ptr<BluetoothDevice> &device, const Uuid &service_uuid,
- const Uuid &characteristic_uuid) {
+ const Uuid &characteristic_uuid) {
cout << "Connecting to device: " << device->getMac().str() << endl;
auto gatt = device->connectGatt();
@@ -98,6 +98,12 @@ public:
auto response = gatt->readValue(characteristic, buf);
cout << "Got data, size=" << response.getSize() << endl;
+ } else if (op_mode == op::NOTIFY) {
+ auto cccd = characteristic.get()->getDescriptor(trygvis::bluetooth::uuids::CLIENT_CHARACTERISTIC_CONFIG);
+// cccd->setValue(BluetoothGattDescriptor::ENABLE_NOTIFICATION_VALUE);
+ gatt->setCharacteristicNotification(cccd, true);
+ } else {
+ cout << "Unsupported op mode." << endl;
}
return EXIT_SUCCESS;
diff --git a/apps/mqtt_support.h b/apps/mqtt_support.h
index ab319fd..0ba6b18 100644
--- a/apps/mqtt_support.h
+++ b/apps/mqtt_support.h
@@ -11,7 +11,7 @@
#include <atomic>
#include <condition_variable>
#include <limits.h>
-#include <experimental/optional>
+#include <optional>
#include "mosquitto.h"
#include "apps.h"
diff --git a/ble/BluetoothImpl.h b/ble/BluetoothImpl.h
index 802c844..975a011 100644
--- a/ble/BluetoothImpl.h
+++ b/ble/BluetoothImpl.h
@@ -40,33 +40,6 @@ protected:
// Shared classes
-namespace detail {
-
-template<typename T, typename U>
-struct constify2;
-
-template<typename T, typename U>
-struct constify2<T *, U *> {
- typedef T *type;
-};
-
-template<typename T, typename U>
-struct constify2<T *, U const *> {
- typedef T const *type;
-};
-
-}
-
-template<typename A, typename B>
-class CollectionImpl : public Collection<A> {
-public:
- explicit CollectionImpl(B &b) : b(b) {
- }
-
-private:
- B &b;
-};
-
class DefaultBluetoothGattCharacteristic : protected LogSetup, public BluetoothGattCharacteristic {
public:
DefaultBluetoothGattCharacteristic(const BluetoothGattServicePtr &service, uint16_t handle, Uuid uuid,
@@ -97,6 +70,10 @@ public:
return valueHandle;
}
+ shared_ptr<BluetoothGattDescriptor> getDescriptor(Uuid uuid) const override {
+ return {};
+ }
+
protected:
BluetoothGattServicePtr service;
uint16_t handle;
diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp
index d3fe80b..444e3a2 100644
--- a/ble/LinuxBluetooth.cpp
+++ b/ble/LinuxBluetooth.cpp
@@ -1,4 +1,5 @@
#include "BluetoothImpl.h"
+#include "ble/misc.h"
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
@@ -22,8 +23,9 @@ namespace trygvis {
namespace bluetooth {
namespace linux {
-using namespace uuids;
+//using namespace uuids;
using namespace std::chrono_literals;
+using namespace std;
using std::to_string;
class LinuxBluetoothGatt;
@@ -55,7 +57,7 @@ public:
void runScan(std::function<void(const shared_ptr<BluetoothDevice> &device)>) override;
- shared_ptr <BluetoothDevice> getDevice(Mac &mac) override;
+ shared_ptr<BluetoothDevice> getDevice(Mac &mac) override;
private:
void startScan() override;
@@ -105,6 +107,8 @@ public:
ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer& response) override;
+ void setCharacteristicNotification(const BluetoothGattDescriptorPtr &c, bool enable) override;
+
private:
void connect();
@@ -302,6 +306,9 @@ ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristicPtr &c
return view;
}
+void LinuxBluetoothGatt::setCharacteristicNotification(const BluetoothGattDescriptorPtr &c, bool enable) {
+}
+
void LinuxBluetoothGatt::discoverServices() {
uint16_t startHandle = 0x0001;
diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h
index d95d843..f97f3b8 100644
--- a/include/ble/Bluetooth.h
+++ b/include/ble/Bluetooth.h
@@ -1,7 +1,7 @@
#ifndef BLUETOOTH_H
#define BLUETOOTH_H
-#include <experimental/optional>
+#include <optional>
#include <iosfwd>
#include <iostream>
#include <stdexcept>
@@ -17,23 +17,30 @@
namespace trygvis {
namespace bluetooth {
-using namespace std;
+using std::shared_ptr;
+using std::string;
+using std::vector;
+using std::map;
+using std::runtime_error;
+
template<typename T>
-using o = std::experimental::optional<T>;
+using o = std::optional<T>;
class BluetoothAdapter;
class BluetoothDevice;
class BluetoothGatt;
+typedef shared_ptr<BluetoothGatt> BluetoothGattPtr;
class BluetoothGattService;
+typedef shared_ptr<BluetoothGattService> BluetoothGattServicePtr;
class BluetoothGattCharacteristic;
-
-typedef shared_ptr<BluetoothGatt> BluetoothGattPtr;
typedef shared_ptr<BluetoothGattCharacteristic> BluetoothGattCharacteristicPtr;
-typedef shared_ptr<BluetoothGattService> BluetoothGattServicePtr;
+
+class BluetoothGattDescriptor;
+typedef shared_ptr<BluetoothGattDescriptor> BluetoothGattDescriptorPtr;
class Mac {
public:
@@ -76,6 +83,17 @@ public:
Iterator<T> end();
};
+class BluetoothGattDescriptor {
+public:
+ static const uint8_t DISABLE_NOTIFICATION_VALUE[];
+ static const uint8_t ENABLE_INDICATION_VALUE[];
+ static const uint8_t ENABLE_NOTIFICATION_VALUE[];
+
+ virtual ~BluetoothGattDescriptor() = default;
+
+ virtual BluetoothGattCharacteristicPtr getCharacteristic() const = 0;
+};
+
class BluetoothGattCharacteristic {
public:
virtual ~BluetoothGattCharacteristic() = default;
@@ -89,6 +107,12 @@ public:
virtual uint8_t getProperties() const = 0;
virtual uint16_t getValueHandle() const = 0;
+
+ virtual shared_ptr<BluetoothGattDescriptor> getDescriptor(Uuid uuid) const = 0;
+
+ virtual shared_ptr<BluetoothGattDescriptor> getDescriptor(ShortUuid uuid) const {
+ return getDescriptor(uuid.toLong());
+ };
};
class BluetoothGattService {
@@ -131,6 +155,8 @@ public:
virtual ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer& response) = 0;
+ virtual void setCharacteristicNotification(const BluetoothGattDescriptorPtr &c, bool enable) = 0;
+
virtual void discoverServices() = 0;
virtual vector<shared_ptr<BluetoothGattService>> getServices() const = 0;
diff --git a/include/ble/misc.h b/include/ble/misc.h
index 4ba3309..2508d8e 100644
--- a/include/ble/misc.h
+++ b/include/ble/misc.h
@@ -1,6 +1,6 @@
#pragma once
-#include <experimental/optional>
+#include <optional>
#include <cstring>
#include <cctype>
@@ -10,7 +10,7 @@ namespace trygvis {
namespace bluetooth {
template<typename T>
-using o = std::experimental::optional<T>;
+using o = std::optional<T>;
class BluetoothAdapter;
class BluetoothDevice;
@@ -86,6 +86,8 @@ const ShortUuid PRIMARY_SERVICE{0x2800};
const ShortUuid SECONDARY_SERVICE{0x2801};
const ShortUuid CHARACTERISTIC{0x2803};
+const ShortUuid CLIENT_CHARACTERISTIC_CONFIG{0x2902};
+
const ShortUuid TemperatureMeasurement{0x2A1C};
}
diff --git a/sensor/include/trygvis/sensor.h b/sensor/include/trygvis/sensor.h
index 09372b9..8f76eca 100644
--- a/sensor/include/trygvis/sensor.h
+++ b/sensor/include/trygvis/sensor.h
@@ -6,7 +6,7 @@
#include <memory>
#include <vector>
#include <algorithm>
-#include <experimental/optional>
+#include <optional>
namespace trygvis {
namespace sensor {
@@ -14,7 +14,7 @@ namespace sensor {
//using namespace std;
template<typename T>
-using o = std::experimental::optional<T>;
+using o = std::optional<T>;
enum class sample_format_type {
AUTO,