@@ -30,6 +30,34 @@ typedef struct {
30
30
static unsigned uuidTableEntries = 0 ; /* current usage of the table */
31
31
converted_uuid_table_entry_t convertedUUIDTable[UUID_TABLE_MAX_ENTRIES];
32
32
33
+ namespace {
34
+
35
+ static void set_perm (ble_gap_conn_sec_mode_t & dest, GattAttribute::Security_t src) {
36
+ switch (src.value ()) {
37
+ case GattAttribute::Security_t::NONE:
38
+ BLE_GAP_CONN_SEC_MODE_SET_OPEN (&dest);
39
+ break ;
40
+
41
+ case GattAttribute::Security_t::UNAUTHENTICATED:
42
+ BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM (&dest);
43
+ break ;
44
+
45
+ case GattAttribute::Security_t::AUTHENTICATED:
46
+ BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM (&dest);
47
+ break ;
48
+
49
+ case GattAttribute::Security_t::SC_AUTHENTICATED:
50
+ BLE_GAP_CONN_SEC_MODE_SET_LESC_ENC_WITH_MITM (&dest);
51
+ break ;
52
+
53
+ default :
54
+ break ;
55
+ }
56
+ }
57
+
58
+ }
59
+
60
+
33
61
void custom_reset_128bits_uuid_table () {
34
62
uuidTableEntries = 0 ;
35
63
}
@@ -203,7 +231,9 @@ error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base,
203
231
error_t custom_add_in_characteristic (uint16_t service_handle,
204
232
ble_uuid_t *p_uuid,
205
233
uint8_t properties,
206
- SecurityManager::SecurityMode_t requiredSecurity,
234
+ GattAttribute::Security_t read_security,
235
+ GattAttribute::Security_t write_security,
236
+ GattAttribute::Security_t update_security,
207
237
uint8_t *p_data,
208
238
uint16_t length,
209
239
uint16_t max_length,
@@ -226,8 +256,8 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
226
256
/* Notification requires cccd */
227
257
memclr_ ( &cccd_md, sizeof (ble_gatts_attr_md_t ));
228
258
cccd_md.vloc = BLE_GATTS_VLOC_STACK;
229
- BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cccd_md.read_perm );
230
- BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cccd_md.write_perm );
259
+ set_perm ( cccd_md.read_perm , GattAttribute::Security_t::NONE );
260
+ set_perm ( cccd_md.write_perm , update_security );
231
261
}
232
262
233
263
ble_gatts_char_md_t char_md = {0 };
@@ -256,49 +286,8 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
256
286
/* Always set variable size */
257
287
attr_md.vlen = has_variable_len;
258
288
259
- if (char_props.read || char_props.notify || char_props.indicate ) {
260
- switch (requiredSecurity) {
261
- case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
262
- BLE_GAP_CONN_SEC_MODE_SET_OPEN (&attr_md.read_perm );
263
- break ;
264
- case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
265
- BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM (&attr_md.read_perm );
266
- break ;
267
- case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
268
- BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM (&attr_md.read_perm );
269
- break ;
270
- case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
271
- BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM (&attr_md.read_perm );
272
- break ;
273
- case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
274
- BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM (&attr_md.read_perm );
275
- break ;
276
- default :
277
- break ;
278
- };
279
- }
280
-
281
- if (char_props.write || char_props.write_wo_resp ) {
282
- switch (requiredSecurity) {
283
- case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
284
- BLE_GAP_CONN_SEC_MODE_SET_OPEN (&attr_md.write_perm );
285
- break ;
286
- case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
287
- BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM (&attr_md.write_perm );
288
- break ;
289
- case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
290
- BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM (&attr_md.write_perm );
291
- break ;
292
- case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
293
- BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM (&attr_md.write_perm );
294
- break ;
295
- case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
296
- BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM (&attr_md.write_perm );
297
- break ;
298
- default :
299
- break ;
300
- };
301
- }
289
+ set_perm (attr_md.read_perm , read_security);
290
+ set_perm (attr_md.write_perm , write_security);
302
291
303
292
ble_gatts_attr_t attr_char_value = {0 };
304
293
@@ -342,7 +331,9 @@ error_t custom_add_in_descriptor(uint16_t char_handle,
342
331
uint16_t length,
343
332
uint16_t max_length,
344
333
bool has_variable_len,
345
- uint16_t *p_desc_handle)
334
+ uint16_t *p_desc_handle,
335
+ GattAttribute::Security_t read_security,
336
+ GattAttribute::Security_t write_security)
346
337
{
347
338
/* Descriptor metadata */
348
339
ble_gatts_attr_md_t desc_md = {0 };
@@ -352,8 +343,8 @@ error_t custom_add_in_descriptor(uint16_t char_handle,
352
343
desc_md.vlen = has_variable_len;
353
344
354
345
/* Make it readable and writable */
355
- BLE_GAP_CONN_SEC_MODE_SET_OPEN (& desc_md.read_perm );
356
- BLE_GAP_CONN_SEC_MODE_SET_OPEN (& desc_md.write_perm );
346
+ set_perm ( desc_md.read_perm , read_security );
347
+ set_perm ( desc_md.write_perm , write_security );
357
348
358
349
ble_gatts_attr_t attr_desc = {0 };
359
350
0 commit comments