diff --git a/src/BLECharacteristic.cpp b/src/BLECharacteristic.cpp index 9a07cb9d..6da1775f 100644 --- a/src/BLECharacteristic.cpp +++ b/src/BLECharacteristic.cpp @@ -72,11 +72,11 @@ BLECharacteristic::BLECharacteristic(const BLECharacteristic& other) BLECharacteristic::~BLECharacteristic() { - if (_local && _local->release() <= 0) { + if (_local && _local->release() == 0) { delete _local; } - if (_remote && _remote->release() <= 0) { + if (_remote && _remote->release() == 0) { delete _remote; } } diff --git a/src/BLEDescriptor.cpp b/src/BLEDescriptor.cpp index 7a6736b0..366b89aa 100644 --- a/src/BLEDescriptor.cpp +++ b/src/BLEDescriptor.cpp @@ -72,11 +72,11 @@ BLEDescriptor::BLEDescriptor(const BLEDescriptor& other) BLEDescriptor::~BLEDescriptor() { - if (_local && _local->release() <= 0) { + if (_local && _local->release() == 0) { delete _local; } - if (_remote && _remote->release() <= 0) { + if (_remote && _remote->release() == 0) { delete _remote; } } diff --git a/src/BLEService.cpp b/src/BLEService.cpp index 7b5df148..b3f33739 100644 --- a/src/BLEService.cpp +++ b/src/BLEService.cpp @@ -65,11 +65,11 @@ BLEService::BLEService(const BLEService& other) BLEService::~BLEService() { - if (_local && _local->release() <= 0) { + if (_local && _local->release() == 0) { delete _local; } - if (_remote && _remote->release() <= 0) { + if (_remote && _remote->release() == 0) { delete _remote; } } diff --git a/src/local/BLELocalAttribute.cpp b/src/local/BLELocalAttribute.cpp index ce3010a0..762d2457 100644 --- a/src/local/BLELocalAttribute.cpp +++ b/src/local/BLELocalAttribute.cpp @@ -19,9 +19,12 @@ #include "BLELocalAttribute.h" +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 +std::map BLELocalAttribute::_refCount; +#endif + BLELocalAttribute::BLELocalAttribute(const char* uuid) : - _uuid(uuid), - _refCount(0) + _uuid(uuid) { } @@ -51,14 +54,21 @@ enum BLEAttributeType BLELocalAttribute::type() const int BLELocalAttribute::retain() { - _refCount++; - - return _refCount; +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 + _refCount[this]++; + return _refCount[this]; +#else + return -1; +#endif } int BLELocalAttribute::release() { - _refCount--; - - return _refCount; +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 + _refCount[this]--; + if (_refCount[this] == 0) _refCount.erase(this); + return _refCount[this]; +#else + return -1; +#endif } diff --git a/src/local/BLELocalAttribute.h b/src/local/BLELocalAttribute.h index 2af948c3..add363c0 100644 --- a/src/local/BLELocalAttribute.h +++ b/src/local/BLELocalAttribute.h @@ -22,6 +22,10 @@ #include "utility/BLEUuid.h" +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 +#include +#endif + #define BLE_ATTRIBUTE_TYPE_SIZE 2 enum BLEAttributeType { @@ -54,7 +58,10 @@ class BLELocalAttribute private: BLEUuid _uuid; - int _refCount; + +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 + static std::map _refCount; +#endif }; #endif diff --git a/src/local/BLELocalCharacteristic.cpp b/src/local/BLELocalCharacteristic.cpp index 333d00b2..a53c4006 100644 --- a/src/local/BLELocalCharacteristic.cpp +++ b/src/local/BLELocalCharacteristic.cpp @@ -62,7 +62,7 @@ BLELocalCharacteristic::~BLELocalCharacteristic() for (unsigned int i = 0; i < descriptorCount(); i++) { BLELocalDescriptor* d = descriptor(i); - if (d->release() <= 0) { + if (d->release() == 0) { delete d; } } diff --git a/src/local/BLELocalService.cpp b/src/local/BLELocalService.cpp index 442c5422..58957342 100644 --- a/src/local/BLELocalService.cpp +++ b/src/local/BLELocalService.cpp @@ -33,7 +33,7 @@ BLELocalService::~BLELocalService() for (unsigned int i = 0; i < characteristicCount(); i++) { BLELocalCharacteristic* c = characteristic(i); - if (c->release() <= 0) { + if (c->release() == 0) { delete c; } } diff --git a/src/remote/BLERemoteAttribute.cpp b/src/remote/BLERemoteAttribute.cpp index 3018f161..60da2296 100644 --- a/src/remote/BLERemoteAttribute.cpp +++ b/src/remote/BLERemoteAttribute.cpp @@ -21,9 +21,12 @@ #include "BLERemoteAttribute.h" +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 +std::map BLERemoteAttribute::_refCount; +#endif + BLERemoteAttribute::BLERemoteAttribute(const uint8_t uuid[], uint8_t uuidLen) : - _uuid(BLEUuid::uuidToString(uuid, uuidLen)), - _refCount(0) + _uuid(BLEUuid::uuidToString(uuid, uuidLen)) { } @@ -38,14 +41,21 @@ const char* BLERemoteAttribute::uuid() const int BLERemoteAttribute::retain() { - _refCount++; - - return _refCount; +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 + _refCount[this]++; + return _refCount[this]; +#else + return -1; +#endif } int BLERemoteAttribute::release() { - _refCount--; - - return _refCount; +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 + _refCount[this]--; + if (_refCount[this] == 0) _refCount.erase(this); + return _refCount[this]; +#else + return -1; +#endif } diff --git a/src/remote/BLERemoteAttribute.h b/src/remote/BLERemoteAttribute.h index 2d10e5ba..225e9d5d 100644 --- a/src/remote/BLERemoteAttribute.h +++ b/src/remote/BLERemoteAttribute.h @@ -22,6 +22,10 @@ #include +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 +#include +#endif + class BLERemoteAttribute { public: @@ -35,7 +39,10 @@ class BLERemoteAttribute private: String _uuid; - int _refCount; + +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 + static std::map _refCount; +#endif }; #endif diff --git a/src/remote/BLERemoteCharacteristic.cpp b/src/remote/BLERemoteCharacteristic.cpp index bbd98ddf..dd74c61b 100644 --- a/src/remote/BLERemoteCharacteristic.cpp +++ b/src/remote/BLERemoteCharacteristic.cpp @@ -43,7 +43,7 @@ BLERemoteCharacteristic::~BLERemoteCharacteristic() for (unsigned int i = 0; i < descriptorCount(); i++) { BLERemoteDescriptor* d = descriptor(i); - if (d->release() <= 0) { + if (d->release() == 0) { delete d; } } diff --git a/src/remote/BLERemoteDevice.cpp b/src/remote/BLERemoteDevice.cpp index 5a49f26f..1a4a67ab 100644 --- a/src/remote/BLERemoteDevice.cpp +++ b/src/remote/BLERemoteDevice.cpp @@ -50,7 +50,7 @@ void BLERemoteDevice::clearServices() for (unsigned int i = 0; i < serviceCount(); i++) { BLERemoteService* s = service(i); - if (s->release() <= 0) { + if (s->release() == 0) { delete s; } } diff --git a/src/remote/BLERemoteService.cpp b/src/remote/BLERemoteService.cpp index fd5c0ba6..f7461290 100644 --- a/src/remote/BLERemoteService.cpp +++ b/src/remote/BLERemoteService.cpp @@ -31,7 +31,7 @@ BLERemoteService::~BLERemoteService() for (unsigned int i = 0; i < characteristicCount(); i++) { BLERemoteCharacteristic* c = characteristic(i); - if (c->release() <= 0) { + if (c->release() == 0) { delete c; } } diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 373213b9..3b42628b 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -164,7 +164,7 @@ void GATTClass::clearAttributes() for (unsigned int i = 0; i < attributeCount(); i++) { BLELocalAttribute* a = attribute(i); - if (a->release() <= 0) { + if (a->release() == 0) { delete a; } }