From 441cd0b11186d66493798551e1102eb246f1af9f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 30 Jun 2015 14:37:06 +0200 Subject: Getting started on a port to OSX. --- ble/Bluetooth.cpp | 25 +++++-- ble/BluetoothImpl.h | 14 ++-- ble/CMakeLists.txt | 21 +++++- ble/LinuxBluetooth.cpp | 6 +- ble/OsxBluetooth.h | 39 +++++++++++ ble/OsxBluetooth.mm | 181 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 268 insertions(+), 18 deletions(-) create mode 100644 ble/OsxBluetooth.h create mode 100644 ble/OsxBluetooth.mm (limited to 'ble') 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 #include +#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 getAdapter(int hciDevice) { - return getAdapterImpl(hciDevice); -} +shared_ptr BluetoothSystem::getAdapter(string adapter_name) { +#if defined(IS_LINUX) + typedef linux::LinuxBluetoothAdapter Impl; +#elif defined(IS_APPLE) + typedef osx::OsxBluetoothAdapter Impl; + + shared_ptr adapter = osx::getAdapterImpl(); +#endif -void shutdown() { - shutdownImpl(); + return std::static_pointer_cast(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 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 + +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) override; + + BluetoothDevice &getDevice(Mac &mac) override; + +private: + bool scanning; + + map devices; +}; + +shared_ptr&& 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 +#import +#import +#import + +#import "BluetoothImpl.h" +#import "OsxBluetooth.h" + +#include +using namespace std; + +namespace trygvis { +namespace bluetooth { +namespace osx { + +class OsxBluetoothDevice; +class OsxBluetoothGatt; + +class OsxBluetoothDevice : public DefaultBluetoothDevice { +public: + OsxBluetoothDevice(OsxBluetoothAdapter &adapter, Mac &mac); + + ~OsxBluetoothDevice(); + + shared_ptr connectGatt() override; + +private: + weak_ptr gatt; +}; + +class OsxBluetoothGatt : public DefaultBluetoothGatt { +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 discoverServices(uint16_t startHandle); + + vector discoverCharacteristics(uint16_t startHandle, uint16_t endHandle); + + ByteBuffer writeAndRead(ByteBuffer &out, shared_ptr 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) { +} + +BluetoothDevice &OsxBluetoothAdapter::getDevice(Mac &mac) { + map::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 OsxBluetoothDevice::connectGatt() { + if (auto p = gatt.lock()) { + return p; + } + auto ref = make_shared(*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 buffer(new uint8_t[0]); + + return ByteBuffer(buffer, 0); +} + +void OsxBluetoothGatt::discoverServices() { +} + +/* +ByteBuffer OsxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr buffer, size_t size) { +} + +vector OsxBluetoothGatt::discoverServices(uint16_t startHandle) { +} + +vector OsxBluetoothGatt::discoverCharacteristics(uint16_t startHandle, uint16_t endHandle) { +} +*/ + +// ----------------------------------------------------------------------- +// OsxBluetoothSystem +// ----------------------------------------------------------------------- + + +// We only have a single adapter on OSX. +shared_ptr adapter; + +shared_ptr&& getAdapterImpl() { + if(!adapter) { + auto mac = Mac::parseMac("00:00:00:00:00:00"); + adapter = make_shared("system", mac); + } + + return std::move(adapter); +} + +void shutdownImpl() { + adapter.reset(); +} + +} +} +} -- cgit v1.2.3