#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; using namespace log4cplus; // Logging class LogSetup { public: LogSetup(std::string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) { } protected: Logger logger; }; // Shared classes namespace detail { template struct constify2; template struct constify2 { typedef T *type; }; template struct constify2 { typedef T const *type; }; } template class CollectionImpl : public Collection { public: CollectionImpl(B &b) : b(b) { } private: B &b; }; class DefaultBluetoothGattCharacteristic : protected LogSetup, public BluetoothGattCharacteristic { public: DefaultBluetoothGattCharacteristic(const BluetoothGattServicePtr &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() { } BluetoothGattServicePtr 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: BluetoothGattServicePtr service; uint16_t handle; uuid_t uuid; uint8_t properties; uint16_t valueHandle; }; template class DefaultBluetoothGattService : public BluetoothGattService { public: DefaultBluetoothGattService(DeviceType &device, const uuid_t 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)) { } virtual ~DefaultBluetoothGattService() { } 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 vector> getCharacteristics() const { vector> cs(characteristics.size()); std::copy(begin(characteristics), end(characteristics), begin(cs)); return cs; } virtual void addCharacteristic(shared_ptr &&characteristic) { characteristics.emplace_back(characteristic); } virtual o> findCharacteristic(uuid_t uuid) { for (auto &c: characteristics) { if (memcmp(c->getUuid().data, uuid.data, 16) == 0) { return o>(c); } } return o>(); } protected: DeviceType &device; const uuid_t uuid; const uint16_t handle; const uint16_t endGroupHandle; vector> characteristics; }; template class DefaultBluetoothGatt : protected LogSetup, public BluetoothGatt { public: virtual _D &getDevice() const { return device; } // virtual Collection getServices() const { // return CollectionImpl>(services); // } virtual vector> getServices() const { vector> ss(services.size()); std::copy(begin(services), end(services), begin(ss)); return ss; } virtual o findService(uuid_t uuid) { for (auto &s: services) { if (memcmp(s->getUuid().data, uuid.data, 16) == 0) { return o>(s); } } return o>(); } void addService(shared_ptr<_S> service) { services.emplace_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) { } DefaultBluetoothDevice(DefaultBluetoothDevice &&o) noexcept : LogSetup("BluetoothDevice"), adapter(std::move(o.adapter)), mac(std::move(o.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