Skip to content

Commit b4fe046

Browse files
committed
Refactor 2904 descriptor
* General code cleanup. * Added method `NimBLECharacteristic::create2904` which will specifically create a CPF(0x2904) descriptor.
1 parent 47c3cd5 commit b4fe046

File tree

5 files changed

+59
-60
lines changed

5 files changed

+59
-60
lines changed

examples/Advanced/NimBLE_Server/main/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void app_main(void) {
195195
* and sizes. However we must cast the returned reference to the correct type as the method
196196
* only returns a pointer to the base NimBLEDescriptor class.
197197
*/
198-
NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor("2904");
198+
NimBLE2904* pBeef2904 = pBeefCharacteristic->create2904();
199199
pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
200200
pBeef2904->setCallbacks(&dscCallbacks);
201201

src/NimBLE2904.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,57 @@
1515
#include "nimconfig.h"
1616
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
1717

18-
#include "NimBLE2904.h"
19-
20-
21-
NimBLE2904::NimBLE2904(NimBLECharacteristic* pCharacteristic)
22-
: NimBLEDescriptor(NimBLEUUID((uint16_t) 0x2904),
23-
BLE_GATT_CHR_F_READ,
24-
sizeof(BLE2904_Data),
25-
pCharacteristic)
26-
{
27-
m_data.m_format = 0;
28-
m_data.m_exponent = 0;
29-
m_data.m_namespace = 1; // 1 = Bluetooth SIG Assigned Numbers
30-
m_data.m_unit = 0;
31-
m_data.m_description = 0;
32-
setValue((uint8_t*) &m_data, sizeof(m_data));
33-
} // BLE2904
18+
# include "NimBLE2904.h"
3419

20+
NimBLE2904::NimBLE2904(NimBLECharacteristic* pChr)
21+
: NimBLEDescriptor(NimBLEUUID((uint16_t)0x2904), BLE_GATT_CHR_F_READ, sizeof(NimBLE2904Data), pChr) {
22+
setValue(m_data);
23+
} // NimBLE2904
3524

3625
/**
3726
* @brief Set the description.
27+
* @param [in] description The description value to set.
3828
*/
3929
void NimBLE2904::setDescription(uint16_t description) {
4030
m_data.m_description = description;
41-
setValue((uint8_t*) &m_data, sizeof(m_data));
42-
}
43-
31+
setValue(m_data);
32+
} // setDescription
4433

4534
/**
4635
* @brief Set the exponent.
36+
* @param [in] exponent The exponent value to set.
4737
*/
4838
void NimBLE2904::setExponent(int8_t exponent) {
4939
m_data.m_exponent = exponent;
50-
setValue((uint8_t*) &m_data, sizeof(m_data));
40+
setValue(m_data);
5141
} // setExponent
5242

53-
5443
/**
5544
* @brief Set the format.
45+
* @param [in] format The format value to set.
5646
*/
5747
void NimBLE2904::setFormat(uint8_t format) {
5848
m_data.m_format = format;
59-
setValue((uint8_t*) &m_data, sizeof(m_data));
49+
setValue(m_data);
6050
} // setFormat
6151

62-
6352
/**
6453
* @brief Set the namespace.
54+
* @param [in] namespace_value The namespace value toset.
6555
*/
6656
void NimBLE2904::setNamespace(uint8_t namespace_value) {
6757
m_data.m_namespace = namespace_value;
68-
setValue((uint8_t*) &m_data, sizeof(m_data));
58+
setValue(m_data);
6959
} // setNamespace
7060

71-
7261
/**
73-
* @brief Set the units for this value. It should be one of the encoded values defined here:
74-
* https://www.bluetooth.com/specifications/assigned-numbers/units
62+
* @brief Set the units for this value.
7563
* @param [in] unit The type of units of this characteristic as defined by assigned numbers.
64+
* @details See https://www.bluetooth.com/specifications/assigned-numbers/units
7665
*/
7766
void NimBLE2904::setUnit(uint16_t unit) {
7867
m_data.m_unit = unit;
79-
setValue((uint8_t*) &m_data, sizeof(m_data));
68+
setValue(m_data);
8069
} // setUnit
8170

8271
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */

src/NimBLE2904.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,30 @@
1212
* Author: kolban
1313
*/
1414

15-
#ifndef MAIN_NIMBLE2904_H_
16-
#define MAIN_NIMBLE2904_H_
15+
#ifndef NIMBLE_CPP_2904_H_
16+
#define NIMBLE_CPP_2904_H_
17+
1718
#include "nimconfig.h"
1819
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
1920

20-
#include "NimBLEDescriptor.h"
21-
22-
struct BLE2904_Data {
23-
uint8_t m_format;
24-
int8_t m_exponent;
25-
uint16_t m_unit; // See https://www.bluetooth.com/specifications/assigned-numbers/units
26-
uint8_t m_namespace;
27-
uint16_t m_description;
21+
# include "NimBLEDescriptor.h"
2822

23+
struct NimBLE2904Data {
24+
uint8_t m_format{0};
25+
int8_t m_exponent{0};
26+
uint16_t m_unit{0x2700}; // Unitless; See https://www.bluetooth.com/specifications/assigned-numbers/units
27+
uint8_t m_namespace{1}; // 1 = Bluetooth SIG Assigned Numbers
28+
uint16_t m_description{0}; // unknown description
2929
} __attribute__((packed));
3030

31-
3231
/**
3332
* @brief Descriptor for Characteristic Presentation Format.
3433
*
3534
* This is a convenience descriptor for the Characteristic Presentation Format which has a UUID of 0x2904.
3635
*/
37-
class NimBLE2904: public NimBLEDescriptor {
38-
public:
39-
NimBLE2904(NimBLECharacteristic* pCharacterisitic = nullptr);
36+
class NimBLE2904 : public NimBLEDescriptor {
37+
public:
38+
NimBLE2904(NimBLECharacteristic* pChr = nullptr);
4039
static const uint8_t FORMAT_BOOLEAN = 1;
4140
static const uint8_t FORMAT_UINT2 = 2;
4241
static const uint8_t FORMAT_UINT4 = 3;
@@ -64,17 +63,18 @@ class NimBLE2904: public NimBLEDescriptor {
6463
static const uint8_t FORMAT_UTF8 = 25;
6564
static const uint8_t FORMAT_UTF16 = 26;
6665
static const uint8_t FORMAT_OPAQUE = 27;
66+
static const uint8_t FORMAT_MEDASN1 = 28;
6767

6868
void setDescription(uint16_t);
6969
void setExponent(int8_t exponent);
7070
void setFormat(uint8_t format);
7171
void setNamespace(uint8_t namespace_value);
7272
void setUnit(uint16_t unit);
7373

74-
private:
74+
private:
7575
friend class NimBLECharacteristic;
76-
BLE2904_Data m_data;
77-
}; // BLE2904
76+
NimBLE2904Data m_data{};
77+
}; // NimBLE2904
7878

79-
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
80-
#endif /* MAIN_NIMBLE2904_H_ */
79+
#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
80+
#endif // NIMBLE_CPP_2904_H_

src/NimBLECharacteristic.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const char* uuid, uint3
7272
*/
7373
NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const NimBLEUUID& uuid, uint32_t properties, uint16_t maxLen) {
7474
NimBLEDescriptor* pDescriptor = nullptr;
75-
if (uuid == NimBLEUUID(uint16_t(0x2904))) {
76-
pDescriptor = new NimBLE2904(this);
75+
if (uuid == NimBLEUUID(static_cast<uint16_t>(0x2904))) {
76+
NIMBLE_LOGW(LOG_TAG, "0x2904 descriptor should be created with create2904()");
77+
pDescriptor = create2904();
7778
} else {
7879
pDescriptor = new NimBLEDescriptor(uuid, properties, maxLen, this);
7980
}
@@ -82,6 +83,16 @@ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const NimBLEUUID& uuid,
8283
return pDescriptor;
8384
} // createDescriptor
8485

86+
/**
87+
* @brief Create a Characteristic Presentation Format Descriptor for this characteristic.
88+
* @return A pointer to a NimBLE2904 descriptor.
89+
*/
90+
NimBLE2904* NimBLECharacteristic::create2904() {
91+
NimBLE2904* pDescriptor = new NimBLE2904(this);
92+
addDescriptor(pDescriptor);
93+
return pDescriptor;
94+
} // create2904
95+
8596
/**
8697
* @brief Add a descriptor to the characteristic.
8798
* @param [in] pDescriptor A pointer to the descriptor to add.

src/NimBLECharacteristic.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
1818

1919
class NimBLECharacteristicCallbacks;
20+
class NimBLEService;
2021
class NimBLECharacteristic;
22+
class NimBLEDescriptor;
23+
class NimBLE2904;
2124

2225
# include "NimBLELocalValueAttribute.h"
23-
# include "NimBLEServer.h"
24-
# include "NimBLEService.h"
25-
# include "NimBLEDescriptor.h"
26-
# include "NimBLEAttValue.h"
27-
# include "NimBLEConnInfo.h"
2826

2927
# include <string>
3028
# include <vector>
@@ -39,11 +37,11 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
3937
public:
4038
NimBLECharacteristic(const char* uuid,
4139
uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
42-
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
40+
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
4341
NimBLEService* pService = nullptr);
4442
NimBLECharacteristic(const NimBLEUUID& uuid,
4543
uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
46-
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
44+
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN,
4745
NimBLEService* pService = nullptr);
4846

4947
~NimBLECharacteristic();
@@ -60,10 +58,11 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
6058

6159
NimBLEDescriptor* createDescriptor(const char* uuid,
6260
uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
63-
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
61+
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
6462
NimBLEDescriptor* createDescriptor(const NimBLEUUID& uuid,
6563
uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
66-
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
64+
uint16_t maxLen = BLE_ATT_ATTR_MAX_LEN);
65+
NimBLE2904* create2904();
6766
NimBLEDescriptor* getDescriptorByUUID(const char* uuid) const;
6867
NimBLEDescriptor* getDescriptorByUUID(const NimBLEUUID& uuid) const;
6968
NimBLEDescriptor* getDescriptorByHandle(uint16_t handle) const;
@@ -127,7 +126,7 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
127126
bool sendValue(const uint8_t* value,
128127
size_t length,
129128
bool is_notification = true,
130-
uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
129+
uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const;
131130

132131
NimBLECharacteristicCallbacks* m_pCallbacks{nullptr};
133132
NimBLEService* m_pService{nullptr};

0 commit comments

Comments
 (0)