@@ -48,20 +48,145 @@ const uint8_t UUID128_CHR_ADAFRUIT_VERSION[16] =
48
48
0xA8 , 0x42 , 0x32 , 0xC3 , 0x02 , 0x00 , 0xAF , 0xAD
49
49
};
50
50
51
+ // --------------------------------------------------------------------+
52
+ // Constructor
53
+ // --------------------------------------------------------------------+
54
+
55
+ BLEUuid::BLEUuid (void )
56
+ {
57
+ _uuid.type = BLE_UUID_TYPE_UNKNOWN;
58
+ _uuid.uuid = 0 ;
59
+
60
+ _uuid128 = NULL ;
61
+ _str = NULL ;
62
+ }
63
+
64
+ BLEUuid::BLEUuid (uint16_t uuid16)
65
+ {
66
+ set (uuid16);
67
+ }
68
+
69
+ BLEUuid::BLEUuid (const char *str)
70
+ {
71
+ set (str);
72
+ }
73
+
74
+ BLEUuid::BLEUuid (uint8_t const uuid128[16 ])
75
+ {
76
+ set (uuid128);
77
+ }
78
+
79
+ BLEUuid::BLEUuid (ble_uuid_t uuid)
80
+ {
81
+ _uuid = uuid;
82
+
83
+ _uuid128 = NULL ;
84
+ _str = NULL ;
85
+ }
86
+
87
+ BLEUuid::~BLEUuid ()
88
+ {
89
+ if (_str && _uuid128)
90
+ {
91
+ // don't even free _uuid128, it could be pointer copyied to other
92
+ // check again if it is a major memory leak
93
+ }
94
+ }
95
+
96
+ // Get size of uuid in bit length. return 16, 32 or 128
97
+ size_t BLEUuid::size (void ) const
98
+ {
99
+ // uuid 16
100
+ if (_uuid.type == BLE_UUID_TYPE_BLE ) return 16 ;
101
+ if (_uuid128 != NULL || _str != NULL || _uuid.type >= BLE_UUID_TYPE_VENDOR_BEGIN) return 128 ;
102
+
103
+ // unknown
104
+ return 0 ;
105
+ }
106
+
107
+ uint8_t * parse_str2uuid128 (const char * str)
108
+ {
109
+ uint8_t * u128 = (uint8_t *) rtos_malloc (16 );
110
+ uint8_t len = 0 ;
111
+
112
+ // str is input as big endian
113
+ for (int i = strlen (str)-1 ; i >= 0 && len < 16 ; i -= 2 )
114
+ {
115
+ if (str[i] == ' -' )
116
+ {
117
+ // skip dash
118
+ i++;
119
+ }else
120
+ {
121
+ char temp[3 ] = { 0 };
122
+ temp[0 ] = str[i-1 ];
123
+ temp[1 ] = str[i];
124
+
125
+ u128 [len++] = (uint8_t ) strtoul (temp, NULL , 16 );
126
+ }
127
+ }
128
+
129
+ return u128 ;
130
+ }
131
+
132
+ bool BLEUuid::begin (void )
133
+ {
134
+ // Add base uuid and decode to get uuid16
135
+ // This should cover the already added base uuid128 previously
136
+ if (_uuid.type == BLE_UUID_TYPE_UNKNOWN)
137
+ {
138
+ // allocate uuid128 and parse str (str for uuid16 already parsed at this point)
139
+ if (_str)
140
+ {
141
+ _uuid128 = parse_str2uuid128 (_str);
142
+ }
143
+
144
+ if (_uuid128 != NULL )
145
+ {
146
+ (void ) sd_ble_uuid_vs_add ( (ble_uuid128_t const *) _uuid128, &_uuid.type );
147
+ VERIFY_STATUS ( sd_ble_uuid_decode (16 , _uuid128, &_uuid), false );
148
+ }
149
+ }
150
+
151
+ return true ;
152
+ }
153
+
154
+ // --------------------------------------------------------------------+
155
+ // Set & Get
156
+ // --------------------------------------------------------------------+
51
157
void BLEUuid::set (uint16_t uuid16)
52
158
{
53
159
_uuid.type = BLE_UUID_TYPE_BLE;
54
160
_uuid.uuid = uuid16;
55
161
56
162
_uuid128 = NULL ;
163
+ _str = NULL ;
57
164
}
58
165
59
166
void BLEUuid::set (uint8_t const uuid128[16 ])
60
167
{
168
+ _uuid128 = uuid128;
169
+
61
170
_uuid.type = BLE_UUID_TYPE_UNKNOWN;
62
171
_uuid.uuid = 0 ;
172
+ _str = NULL ;
173
+ }
63
174
64
- _uuid128 = uuid128;
175
+ void BLEUuid::set (const char * str)
176
+ {
177
+ // Check if str is uuid16
178
+ if (strlen (str) == 4 )
179
+ {
180
+ uint16_t uuid16 = strtoul (str, NULL , 16 );
181
+ set (uuid16);
182
+ }else
183
+ {
184
+ _str = str;
185
+
186
+ _uuid.type = BLE_UUID_TYPE_UNKNOWN;
187
+ _uuid.uuid = 0 ;
188
+ _uuid128 = NULL ;
189
+ }
65
190
}
66
191
67
192
bool BLEUuid::get (uint16_t * uuid16 ) const
@@ -88,33 +213,9 @@ bool BLEUuid::get(uint8_t uuid128[16])
88
213
return true ;
89
214
}
90
215
91
- /* *
92
- * Get size of uuid in BIT
93
- * @return 16, 32 or 128
94
- */
95
- size_t BLEUuid::size (void ) const
96
- {
97
- // uuid 16
98
- if (_uuid.type == BLE_UUID_TYPE_BLE ) return 16 ;
99
- if (_uuid128 != NULL || _uuid.type >= BLE_UUID_TYPE_VENDOR_BEGIN) return 128 ;
100
-
101
- // unknown
102
- return 0 ;
103
- }
104
-
105
- bool BLEUuid::begin (void )
106
- {
107
- /* Add base uuid and decode to get uuid16
108
- * This should cover the already added base uuid128 previously
109
- */
110
- if (_uuid.type == BLE_UUID_TYPE_UNKNOWN && _uuid128 != NULL )
111
- {
112
- (void ) sd_ble_uuid_vs_add ( (ble_uuid128_t const *) _uuid128, &_uuid.type );
113
- VERIFY_STATUS ( sd_ble_uuid_decode (16 , _uuid128, &_uuid), false );
114
- }
115
-
116
- return true ;
117
- }
216
+ // --------------------------------------------------------------------+
217
+ // Comparison
218
+ // --------------------------------------------------------------------+
118
219
119
220
bool BLEUuid::operator == (const BLEUuid& uuid) const
120
221
{
@@ -136,6 +237,10 @@ bool BLEUuid::operator!= (const ble_uuid_t uuid) const
136
237
return !(*this == uuid);
137
238
}
138
239
240
+ // --------------------------------------------------------------------+
241
+ // Copy operator
242
+ // --------------------------------------------------------------------+
243
+
139
244
// Overload copy operator to allow initialization from other type
140
245
BLEUuid& BLEUuid::operator =(const uint16_t uuid)
141
246
{
@@ -149,6 +254,13 @@ BLEUuid& BLEUuid::operator=(uint8_t const uuid128[16])
149
254
return *this ;
150
255
}
151
256
257
+ BLEUuid& BLEUuid::operator =(const char * str)
258
+ {
259
+ set (str);
260
+ return *this ;
261
+ }
262
+
263
+
152
264
BLEUuid& BLEUuid::operator =(ble_uuid_t uuid)
153
265
{
154
266
_uuid = uuid;
0 commit comments