#include #include "BluetoothImpl.h" #include "OsxBluetooth.h" #include "OsxBluetoothImpl.h" #include using namespace std; @implementation InquiryDelegateWrapper - (id) initWithCPPInstance:(trygvis::bluetooth::osx::InquiryDelegate*)_adapter { self = [super init]; if (self) { adapter = _adapter; } return self; } - (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry *)sender; { if (adapter) adapter->deviceInquiryStarted(); } @end 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 callback) { auto y = new InquiryDelegate(*this, callback); id x = [InquiryDelegateWrapper ::initWithCPPInstance:y]; inq = [IOBluetoothDeviceInquiry inquiryWithDelegate:x]; if(!inq) { throw BluetoothException(&adapter, "Could not initialize inquery"); } } void InquiryDelegate::deviceInquiryStarted() { } 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(); } } } }