aboutsummaryrefslogtreecommitdiff
path: root/ble
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-06-30 14:37:06 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-06-30 14:37:06 +0200
commit441cd0b11186d66493798551e1102eb246f1af9f (patch)
tree3cdb5b1dea6b782cbf59e172807f9dc15e7bd418 /ble
parentaf63bd5688731b57551ac161c0dad26a0b4557d7 (diff)
downloadble-toys-441cd0b11186d66493798551e1102eb246f1af9f.tar.gz
ble-toys-441cd0b11186d66493798551e1102eb246f1af9f.tar.bz2
ble-toys-441cd0b11186d66493798551e1102eb246f1af9f.tar.xz
ble-toys-441cd0b11186d66493798551e1102eb246f1af9f.zip
Getting started on a port to OSX.
Diffstat (limited to 'ble')
-rw-r--r--ble/Bluetooth.cpp25
-rw-r--r--ble/BluetoothImpl.h14
-rw-r--r--ble/CMakeLists.txt21
-rw-r--r--ble/LinuxBluetooth.cpp6
-rw-r--r--ble/OsxBluetooth.h39
-rw-r--r--ble/OsxBluetooth.mm181
6 files changed, 268 insertions, 18 deletions
diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp
index 14a8cda..48fc87c 100644
--- a/ble/Bluetooth.cpp
+++ b/ble/Bluetooth.cpp
@@ -4,6 +4,12 @@
#include <sstream>
#include <iomanip>
+#if defined(IS_LINUX)
+#include "LinuxBluetooth.h"
+#elif defined(IS_APPLE)
+#include "OsxBluetooth.h"
+#endif
+
namespace trygvis {
namespace bluetooth {
using namespace std;
@@ -201,19 +207,26 @@ BluetoothAdapter::BluetoothAdapter() {
BluetoothAdapter::~BluetoothAdapter() {
}
+// -----------------------------------------------------------------------
+// BluetoothSystem
+// -----------------------------------------------------------------------
+
BluetoothSystem::BluetoothSystem() {
}
BluetoothSystem::~BluetoothSystem() {
- shutdown();
}
-shared_ptr<BluetoothAdapter> getAdapter(int hciDevice) {
- return getAdapterImpl(hciDevice);
-}
+shared_ptr<BluetoothAdapter> BluetoothSystem::getAdapter(string adapter_name) {
+#if defined(IS_LINUX)
+ typedef linux::LinuxBluetoothAdapter Impl;
+#elif defined(IS_APPLE)
+ typedef osx::OsxBluetoothAdapter Impl;
+
+ shared_ptr<Impl> adapter = osx::getAdapterImpl();
+#endif
-void shutdown() {
- shutdownImpl();
+ return std::static_pointer_cast<BluetoothAdapter>(std::move(adapter));
}
uuid_t makeUuid(const uuid_t base, uint8_t a, uint8_t b) {
diff --git a/ble/BluetoothImpl.h b/ble/BluetoothImpl.h
index 3f4615e..648961f 100644
--- a/ble/BluetoothImpl.h
+++ b/ble/BluetoothImpl.h
@@ -221,9 +221,14 @@ protected:
class DefaultBluetoothAdapter : protected LogSetup, public BluetoothAdapter {
public:
+
+ string getName() override {
+ return _name;
+ };
+
protected:
- DefaultBluetoothAdapter(Mac &mac) :
- LogSetup("BluetoothAdapter"), mac(mac) {
+ DefaultBluetoothAdapter(const string name, Mac &mac) :
+ LogSetup("BluetoothAdapter"), _name(name), mac(mac) {
}
Mac const &getMac() override {
@@ -231,12 +236,9 @@ protected:
};
Mac &mac;
+ const string _name;
};
-shared_ptr<BluetoothAdapter> getAdapterImpl(int hciDevice);
-
-void shutdownImpl();
-
}
};
diff --git a/ble/CMakeLists.txt b/ble/CMakeLists.txt
index 543e85d..5f20a21 100644
--- a/ble/CMakeLists.txt
+++ b/ble/CMakeLists.txt
@@ -1,4 +1,21 @@
-file(GLOB SOURCE_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.h)
+list(APPEND SOURCE_FILES Bluetooth.cpp)
+list(APPEND SOURCE_FILES BluetoothImpl.h)
+list(APPEND SOURCE_FILES ByteBuffer.cpp)
+list(APPEND SOURCE_FILES log.h)
+list(APPEND SOURCE_FILES "${PROJECT_SOURCE_DIR}/include/ble/Bluetooth.h")
+list(APPEND SOURCE_FILES "${PROJECT_SOURCE_DIR}/include/ble/ByteBuffer.h")
+
+if(IS_LINUX)
+ list(APPEND SOURCE_FILES LinuxBluetooth.cpp LinuxBluetooth.h)
+ list(APPEND COMPILE_OPTIONS -DIS_LINUX)
+elseif(IS_APPLE)
+ list(APPEND SOURCE_FILES OsxBluetooth.mm OsxBluetooth.h)
+ list(APPEND COMPILE_OPTIONS -DIS_APPLE)
+endif()
+
+find_package(Boost COMPONENTS regex system program_options REQUIRED)
add_library(ble ${SOURCE_FILES})
-include_directories("${PROJECT_SOURCE_DIR}/include")
+target_include_directories(ble PUBLIC "${PROJECT_SOURCE_DIR}/include")
+target_include_directories(ble PUBLIC "${Boost_INCLUDE_DIRS}")
+target_compile_options(ble PRIVATE -Wno-deprecated-register ${COMPILE_OPTIONS})
diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp
index d48bfcd..4450d4d 100644
--- a/ble/LinuxBluetooth.cpp
+++ b/ble/LinuxBluetooth.cpp
@@ -22,8 +22,6 @@ class LinuxBluetoothGatt;
class LinuxBluetoothDevice;
-class LinuxBluetoothAdapter;
-
class LinuxBluetoothManager;
class LinuxBluetoothAdapter : public DefaultBluetoothAdapter {
@@ -207,7 +205,7 @@ void LinuxBluetoothGatt::disconnect() {
close(l2cap);
}
-uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) {
+static uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) {
size_t bytesLeft = bytes.getBytesLeft();
uuid_t u;
@@ -437,7 +435,7 @@ LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId, Mac &mac) : Defaul
}
LinuxBluetoothAdapter::~LinuxBluetoothAdapter() {
- LOG_DEBUG("Stopping scan on device #" << hciDeviceId);
+ LOG_DEBUG("Closing adapter");
stopScan();
diff --git a/ble/OsxBluetooth.h b/ble/OsxBluetooth.h
new file mode 100644
index 0000000..8fa06a9
--- /dev/null
+++ b/ble/OsxBluetooth.h
@@ -0,0 +1,39 @@
+#ifndef OSX_BLUETOOTH_H
+#define OSX_BLUETOOTH_H
+
+#include <map>
+
+namespace trygvis {
+namespace bluetooth {
+namespace osx {
+
+class OsxBluetoothDevice;
+
+class OsxBluetoothAdapter : public DefaultBluetoothAdapter {
+public:
+ OsxBluetoothAdapter(const string name, Mac &mac);
+
+ ~OsxBluetoothAdapter();
+
+ void startScan() override;
+
+ void stopScan() override;
+
+ void runScan(std::function<void(BluetoothDevice &device)>) override;
+
+ BluetoothDevice &getDevice(Mac &mac) override;
+
+private:
+ bool scanning;
+
+ map<Mac, OsxBluetoothDevice *> devices;
+};
+
+shared_ptr<OsxBluetoothAdapter>&& getAdapterImpl();
+void shutdownImpl();
+
+}
+}
+}
+
+#endif
diff --git a/ble/OsxBluetooth.mm b/ble/OsxBluetooth.mm
new file mode 100644
index 0000000..e94edaf
--- /dev/null
+++ b/ble/OsxBluetooth.mm
@@ -0,0 +1,181 @@
+#import <CoreFoundation/CoreFoundation.h>
+#import <IOBluetooth/IOBluetoothUtilities.h>
+#import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
+#import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
+
+#import "BluetoothImpl.h"
+#import "OsxBluetooth.h"
+
+#include <map>
+using namespace std;
+
+namespace trygvis {
+namespace bluetooth {
+namespace osx {
+
+class OsxBluetoothDevice;
+class OsxBluetoothGatt;
+
+class OsxBluetoothDevice : public DefaultBluetoothDevice<OsxBluetoothAdapter> {
+public:
+ OsxBluetoothDevice(OsxBluetoothAdapter &adapter, Mac &mac);
+
+ ~OsxBluetoothDevice();
+
+ shared_ptr<BluetoothGatt> connectGatt() override;
+
+private:
+ weak_ptr<OsxBluetoothGatt> gatt;
+};
+
+class OsxBluetoothGatt : public DefaultBluetoothGatt<OsxBluetoothDevice> {
+public:
+ OsxBluetoothGatt(OsxBluetoothDevice &device);
+
+ ~OsxBluetoothGatt();
+
+ OsxBluetoothGatt(const OsxBluetoothGatt&) = delete;
+
+ OsxBluetoothGatt& operator=(const OsxBluetoothGatt&) = delete;
+
+ bool isConnected() const override;
+
+ void discoverServices() override;
+
+ void writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) override;
+
+ ByteBuffer readValue(const BluetoothGattCharacteristic &c) override;
+
+private:
+ void connect();
+
+ void disconnect();
+
+ vector<AttributeData> discoverServices(uint16_t startHandle);
+
+ vector<AttributeData> discoverCharacteristics(uint16_t startHandle, uint16_t endHandle);
+
+ ByteBuffer writeAndRead(ByteBuffer &out, shared_ptr<uint8_t> buffer, size_t size);
+
+ int l2cap;
+
+ bool connected;
+};
+
+OsxBluetoothAdapter::OsxBluetoothAdapter(const string name, Mac &mac) : DefaultBluetoothAdapter(name, mac) {
+}
+
+OsxBluetoothAdapter::~OsxBluetoothAdapter() {
+}
+
+void OsxBluetoothAdapter::startScan() {
+}
+
+void OsxBluetoothAdapter::stopScan() {
+}
+
+void OsxBluetoothAdapter::runScan(std::function<void(BluetoothDevice &device)>) {
+}
+
+BluetoothDevice &OsxBluetoothAdapter::getDevice(Mac &mac) {
+ map<Mac, OsxBluetoothDevice *>::iterator it = devices.find(mac);
+
+ if (it == devices.end()) {
+ OsxBluetoothDevice *device = new OsxBluetoothDevice(*this, mac);
+ devices[mac] = device;
+ return *device;
+ }
+
+ return *it->second;
+}
+
+OsxBluetoothDevice::OsxBluetoothDevice(OsxBluetoothAdapter &adapter, Mac &mac) : DefaultBluetoothDevice(adapter, mac) {
+}
+
+OsxBluetoothDevice::~OsxBluetoothDevice() {
+}
+
+shared_ptr<BluetoothGatt> OsxBluetoothDevice::connectGatt() {
+ if (auto p = gatt.lock()) {
+ return p;
+ }
+ auto ref = make_shared<OsxBluetoothGatt>(*this);
+
+ gatt = ref;
+
+ return ref;
+}
+
+// -----------------------------------------------------------------------
+// Gatt
+// -----------------------------------------------------------------------
+
+OsxBluetoothGatt::OsxBluetoothGatt(OsxBluetoothDevice &device) :
+ DefaultBluetoothGatt(device) {
+ connect();
+}
+
+OsxBluetoothGatt::~OsxBluetoothGatt() {
+ disconnect();
+}
+
+bool OsxBluetoothGatt::isConnected() const {
+ return connected;
+}
+
+void OsxBluetoothGatt::connect() {
+}
+
+void OsxBluetoothGatt::disconnect() {
+ if (!connected) {
+ return;
+ }
+}
+
+void OsxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) {
+}
+
+ByteBuffer OsxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) {
+ shared_ptr<uint8_t> buffer(new uint8_t[0]);
+
+ return ByteBuffer(buffer, 0);
+}
+
+void OsxBluetoothGatt::discoverServices() {
+}
+
+/*
+ByteBuffer OsxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr<uint8_t> buffer, size_t size) {
+}
+
+vector<AttributeData> OsxBluetoothGatt::discoverServices(uint16_t startHandle) {
+}
+
+vector<AttributeData> OsxBluetoothGatt::discoverCharacteristics(uint16_t startHandle, uint16_t endHandle) {
+}
+*/
+
+// -----------------------------------------------------------------------
+// OsxBluetoothSystem
+// -----------------------------------------------------------------------
+
+
+// We only have a single adapter on OSX.
+shared_ptr<OsxBluetoothAdapter> adapter;
+
+shared_ptr<OsxBluetoothAdapter>&& getAdapterImpl() {
+ if(!adapter) {
+ auto mac = Mac::parseMac("00:00:00:00:00:00");
+ adapter = make_shared<OsxBluetoothAdapter>("system", mac);
+ }
+
+ return std::move(adapter);
+}
+
+void shutdownImpl() {
+ adapter.reset();
+}
+
+}
+}
+}