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/OsxBluetooth.mm | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 ble/OsxBluetooth.mm (limited to 'ble/OsxBluetooth.mm') 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