#ifndef BLUETOOTH_IMPL_H #define BLUETOOTH_IMPL_H #include "ble/Bluetooth.h" #include #include #include #include #define BLUETOOTH_UUID_INITIALIZER \ { \ 0x00, 0x00, 0x00, 0x00, \ 0x00, 0x00, \ 0x10, 0x00, \ 0x80, 0x00, \ 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb \ }; #define LOG_TRACE(body) LOG4CPLUS_TRACE(logger, body) #define LOG_DEBUG(body) LOG4CPLUS_DEBUG(logger, body) #define LOG_INFO(body) LOG4CPLUS_INFO(logger, body) #define LOG_WARN(body) LOG4CPLUS_WARN(logger, body) namespace trygvis { namespace bluetooth { // Utility typedefs using namespace log4cplus; // Logging class LogSetup { public: explicit LogSetup(const std::string &name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) { } protected: Logger logger; }; // Shared classes class DefaultBluetoothGattDescriptor : public BluetoothGattDescriptor { public: explicit DefaultBluetoothGattDescriptor(const BluetoothGattCharacteristicPtr& characteristic, uint16_t handle, const Uuid &uuid) : characteristic(characteristic), handle(handle), uuid(uuid), data() {} ~DefaultBluetoothGattDescriptor() override { delete data.underlying(); }; BluetoothGattCharacteristicPtr getCharacteristic() const { return characteristic; }; uint16_t getHandle() const override { return handle; } const Uuid getUuid() const override { return uuid; } void setValue(const ByteBuffer &buffer) override { delete[] data.underlying(); auto sz = buffer.getSize(); data = ByteBuffer(new uint8_t[sz], sz); data.copyFromEntire(buffer); }; ByteBuffer getValue() const override { return data; }; private: BluetoothGattCharacteristicPtr characteristic; uint16_t handle; Uuid uuid; ByteBuffer data; }; class DefaultBluetoothGattCharacteristic : protected LogSetup, public BluetoothGattCharacteristic { public: DefaultBluetoothGattCharacteristic(const BluetoothGattServicePtr &service, uint16_t handle, Uuid uuid, uint8_t properties, uint16_t valueHandle) : LogSetup("BluetoothGattCharacteristic"), service(service), handle(handle), uuid(uuid), properties(properties), valueHandle(valueHandle) { } ~DefaultBluetoothGattCharacteristic() override = default; BluetoothGattServicePtr getService() const override { return service; } uint16_t getHandle() const override { return handle; } const Uuid getUuid() const override { return uuid; } CharacteristicProperties getProperties() const override { return {properties}; } uint16_t getValueHandle() const override { return valueHandle; } BluetoothGattDescriptorPtr getDescriptor(Uuid uuid) const override { for (auto &d: descriptors) { if (d->getUuid() == uuid) { return d; } } return {}; } void addDescriptor(BluetoothGattDescriptorPtr &&d) { descriptors.push_back(std::move(d)); } protected: BluetoothGattServicePtr service; uint16_t handle; Uuid uuid; uint8_t properties; uint16_t valueHandle; vector descriptors; }; template class DefaultBluetoothGattService : public BluetoothGattService { public: DefaultBluetoothGattService(DeviceType &device, const Uuid& uuid, const uint16_t handle, const uint16_t endGroupHandle) : device(device), uuid(uuid), handle(handle), endGroupHandle(endGroupHandle) { } DefaultBluetoothGattService(const DefaultBluetoothGattService &o) = delete; DefaultBluetoothGattService(DefaultBluetoothGattService &&o) noexcept : device(std::move(o.device)), uuid(std::move(o.uuid)), handle(std::move(o.handle)), endGroupHandle(std::move(o.endGroupHandle)), characteristics(std::move(o.characteristics)) { } ~DefaultBluetoothGattService() override = default; BluetoothDevice &getDevice() const override { return device; } Uuid getUuid() const override { return uuid; } uint16_t getHandle() const override { return handle; } uint16_t getEndGroupHandle() const override { return endGroupHandle; } vector getCharacteristics() const override { vector cs(characteristics.size()); std::copy(begin(characteristics), end(characteristics), begin(cs)); return cs; } virtual void addCharacteristic(BluetoothGattCharacteristicPtr &&characteristic) { characteristics.emplace_back(characteristic); } o findCharacteristic(Uuid uuid) override { for (auto &c: characteristics) { if (c->getUuid() == uuid) { return {c}; } } return {}; } protected: DeviceType &device; const Uuid uuid; const uint16_t handle; const uint16_t endGroupHandle; vector characteristics; }; template class DefaultBluetoothGatt : protected LogSetup, public BluetoothGatt { public: _D &getDevice() const override { return device; } vector getServices() const override { vector ss(services.size()); std::copy(begin(services), end(services), begin(ss)); return ss; } o findService(Uuid uuid) override { for (auto &s: services) { if (s->getUuid() == uuid) { return o(s); } } return {}; } void addService(std::shared_ptr<_S> service) { services.emplace_back(service); } protected: explicit DefaultBluetoothGatt(_D &device, BluetoothCallback *callback) : LogSetup("BluetoothGatt"), device(device), callback(callback) {} ~DefaultBluetoothGatt() override = default; void removeServices() { services.clear(); } _D &device; vector > services; BluetoothCallback *callback; }; template class DefaultBluetoothDevice : protected LogSetup, public BluetoothDevice { public: Mac const &getMac() override { return mac; } A &getAdapter() override { return adapter; } protected: DefaultBluetoothDevice(A &adapter, Mac &mac) : LogSetup("BluetoothDevice"), adapter(adapter), mac(mac) { } DefaultBluetoothDevice(DefaultBluetoothDevice &&o) noexcept : LogSetup("BluetoothDevice"), adapter(std::move(o.adapter)), mac(std::move(o.mac)) { } ~DefaultBluetoothDevice() override { removeServices(); } void removeServices() { for (auto s: services) { delete s; } services.clear(); } A &adapter; Mac &mac; vector services; }; class DefaultBluetoothAdapter : protected LogSetup, public BluetoothAdapter { public: protected: explicit DefaultBluetoothAdapter(Mac &mac) : LogSetup("BluetoothAdapter"), mac(mac) { } Mac const &getMac() override { return mac; }; Mac &mac; }; BluetoothAdapterPtr getAdapterImpl(string name); } // namespace bluetooth } // namespace trygvis #endif