Skip to content

Commit de392e0

Browse files
committed
Change allocation of default services and characteristics
Default local characteristics and services are now allocated in the HEAP. This change is needed for CI integration. Indeed, if these attributes are allocated in the STACK they cannot be freed by the 'delete' statement used in their destructors. Downside: their memory usage will not be counted in the estimated amount of RAM occupated by a resulting program that includes this library.
1 parent 524b3a6 commit de392e0

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/utility/GATT.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,12 @@
2828
#include "GATT.h"
2929

3030
GATTClass::GATTClass() :
31-
_genericAccessService("1800"),
32-
_deviceNameCharacteristic("2a00", BLERead, 20),
33-
_appearanceCharacteristic("2a01", BLERead, 2),
34-
_genericAttributeService("1801"),
35-
_servicesChangedCharacteristic("2a05", BLEIndicate, 4)
31+
_genericAccessService(NULL),
32+
_deviceNameCharacteristic(NULL),
33+
_appearanceCharacteristic(NULL),
34+
_genericAttributeService(NULL),
35+
_servicesChangedCharacteristic(NULL)
3636
{
37-
_genericAccessService.retain();
38-
_genericAttributeService.retain();
39-
40-
_genericAccessService.addCharacteristic(&_deviceNameCharacteristic);
41-
_genericAccessService.addCharacteristic(&_appearanceCharacteristic);
42-
43-
_genericAttributeService.addCharacteristic(&_servicesChangedCharacteristic);
4437
}
4538

4639
GATTClass::~GATTClass()
@@ -50,13 +43,29 @@ GATTClass::~GATTClass()
5043

5144
void GATTClass::begin()
5245
{
46+
_genericAccessService = new BLELocalService("1800");
47+
_deviceNameCharacteristic = new BLELocalCharacteristic("2a00", BLERead, 20);
48+
_appearanceCharacteristic = new BLELocalCharacteristic("2a01", BLERead, 2);
49+
_genericAttributeService = new BLELocalService("1801");
50+
_servicesChangedCharacteristic = new BLELocalCharacteristic("2a05", BLEIndicate, 4);
51+
52+
_genericAccessService->retain();
53+
_deviceNameCharacteristic->retain();
54+
_appearanceCharacteristic->retain();
55+
_genericAttributeService->retain();
56+
_servicesChangedCharacteristic->retain();
57+
58+
_genericAccessService->addCharacteristic(_deviceNameCharacteristic);
59+
_genericAccessService->addCharacteristic(_appearanceCharacteristic);
60+
_genericAttributeService->addCharacteristic(_servicesChangedCharacteristic);
61+
5362
setDeviceName("Arduino");
5463
setAppearance(0x000);
5564

5665
clearAttributes();
5766

58-
addService(&_genericAccessService);
59-
addService(&_genericAttributeService);
67+
addService(_genericAccessService);
68+
addService(_genericAttributeService);
6069
}
6170

6271
void GATTClass::end()
@@ -66,12 +75,12 @@ void GATTClass::end()
6675

6776
void GATTClass::setDeviceName(const char* deviceName)
6877
{
69-
_deviceNameCharacteristic.writeValue(deviceName);
78+
_deviceNameCharacteristic->writeValue(deviceName);
7079
}
7180

7281
void GATTClass::setAppearance(uint16_t appearance)
7382
{
74-
_appearanceCharacteristic.writeValue((uint8_t*)&appearance, sizeof(appearance));
83+
_appearanceCharacteristic->writeValue((uint8_t*)&appearance, sizeof(appearance));
7584
}
7685

7786
void GATTClass::addService(BLEService& service)

src/utility/GATT.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ class GATTClass {
6060
private:
6161
BLELinkedList<BLELocalAttribute*> _attributes;
6262

63-
BLELocalService _genericAccessService;
64-
BLELocalCharacteristic _deviceNameCharacteristic;
65-
BLELocalCharacteristic _appearanceCharacteristic;
66-
BLELocalService _genericAttributeService;
67-
BLELocalCharacteristic _servicesChangedCharacteristic;
63+
BLELocalService* _genericAccessService;
64+
BLELocalCharacteristic* _deviceNameCharacteristic;
65+
BLELocalCharacteristic* _appearanceCharacteristic;
66+
BLELocalService* _genericAttributeService;
67+
BLELocalCharacteristic* _servicesChangedCharacteristic;
6868
};
6969

7070
extern GATTClass GATT;

0 commit comments

Comments
 (0)