#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 typedef boost::uuids::uuid uuid_t; template using o = boost::optional; using namespace log4cplus; // Logging class LogSetup { public: LogSetup(std::string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) { } protected: Logger logger; }; // Shared classes class DefaultBluetoothGattCharacteristic : protected LogSetup, public BluetoothGattCharacteristic { public: DefaultBluetoothGattCharacteristic(BluetoothGattService &service, uint16_t handle, uuid_t uuid, uint8_t properties, uint16_t valueHandle) : LogSetup("BluetoothGattCharacteristic"), service(service), handle(handle), uuid(uuid), properties(properties), valueHandle(valueHandle) { } virtual ~DefaultBluetoothGattCharacteristic() { } BluetoothGattService &getService() const { return service; } uint16_t getHandle() const { return handle; } const uuid_t getUuid() const { return uuid; } uint8_t getProperties() const { return properties; } uint16_t getValueHandle() const { return valueHandle; } protected: BluetoothGattService &service; uint16_t handle; uuid_t uuid; uint8_t properties; uint16_t valueHandle; }; class DefaultBluetoothGattService : public BluetoothGattService { public: DefaultBluetoothGattService(BluetoothDevice &device, const uuid_t uuid, const uint16_t handle, const uint16_t endGroupHandle) : device(device), uuid(uuid), handle(handle), endGroupHandle(endGroupHandle) { } virtual ~DefaultBluetoothGattService() { removeCharacteristics(); } virtual BluetoothDevice &getDevice() const { return device; } virtual uuid_t getUuid() const { return uuid; } virtual uint16_t getHandle() const { return handle; } virtual uint16_t getEndGroupHandle() const { return endGroupHandle; } virtual const vector getCharacteristics() const { return characteristics; } virtual void addCharacteristic(BluetoothGattCharacteristic *characteristic) { characteristics.push_back(characteristic); } virtual const o findCharacteristic(uuid_t uuid) const { for (auto c: characteristics) { if (memcmp(c->getUuid().data, uuid.data, 16) == 0) { return o(*c); } } return o(); } protected: BluetoothDevice &device; const uuid_t uuid; const uint16_t handle; const uint16_t endGroupHandle; vector characteristics; void removeCharacteristics() { for (auto &c: characteristics) { delete c; } characteristics.clear(); } }; template class DefaultBluetoothGatt : protected LogSetup, public BluetoothGatt { public: virtual _D &getDevice() const { return device; } virtual const vector getServices() const { return services; }; virtual const o findService(uuid_t uuid) const { for (auto s: services) { if (memcmp(s->getUuid().data, uuid.data, 16) == 0) { return o(*s); } } return o(); } virtual void addService(BluetoothGattService *service) { services.push_back(service); } protected: DefaultBluetoothGatt(_D &device) : LogSetup("BluetoothGatt"), device(device) { } virtual ~DefaultBluetoothGatt() { removeServices(); } void removeServices() { for (auto s: services) { delete s; } services.clear(); } _D &device; vector services; }; template class DefaultBluetoothDevice : protected LogSetup, public BluetoothDevice { public: virtual Mac const &getMac() override { return mac; } virtual A &getAdapter() override { return adapter; } protected: DefaultBluetoothDevice(A &adapter, Mac &mac) : LogSetup("BluetoothDevice"), adapter(adapter), mac(mac) { } virtual ~DefaultBluetoothDevice() { 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: DefaultBluetoothAdapter(Mac &mac) : LogSetup("BluetoothAdapter"), mac(mac) { } Mac const &getMac() override { return mac; }; Mac &mac; }; shared_ptr getAdapterImpl(string name); } }; #endif