aboutsummaryrefslogtreecommitdiff
path: root/ble/OsxBluetooth.mm
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/OsxBluetooth.mm
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/OsxBluetooth.mm')
-rw-r--r--ble/OsxBluetooth.mm181
1 files changed, 181 insertions, 0 deletions
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();
+}
+
+}
+}
+}