diff --git a/btle/custom/custom_helper.cpp b/btle/custom/custom_helper.cpp index f337ff6..db18c61 100644 --- a/btle/custom/custom_helper.cpp +++ b/btle/custom/custom_helper.cpp @@ -200,6 +200,7 @@ error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t *p_uuid, uint8_t properties, + GattCharacteristic::ble_gatt_char_required_security_t requiredSecurity, uint8_t *p_data, uint16_t min_length, uint16_t max_length, @@ -244,11 +245,43 @@ error_t custom_add_in_characteristic(uint16_t service_handle, attr_md.vlen = (min_length == max_length) ? 0 : 1; if (char_props.read || char_props.notify || char_props.indicate) { - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); + switch (requiredSecurity) { + case GattCharacteristic::SECURITY_MODE_ENCRYPTION_OPEN_LINK : + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); + break; + case GattCharacteristic::SECURITY_MODE_ENCRYPTION_NO_MITM : + BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.read_perm); + break; + case GattCharacteristic::SECURITY_MODE_ENCRYPTION_WITH_MITM : + BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm); + break; + case GattCharacteristic::SECURITY_MODE_SIGNED_NO_MITM : + BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.read_perm); + break; + case GattCharacteristic::SECURITY_MODE_SIGNED_WITH_MITM : + BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm); + break; + }; } if (char_props.write || char_props.write_wo_resp) { - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); + switch (requiredSecurity) { + case GattCharacteristic::SECURITY_MODE_ENCRYPTION_OPEN_LINK : + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); + break; + case GattCharacteristic::SECURITY_MODE_ENCRYPTION_NO_MITM : + BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm); + break; + case GattCharacteristic::SECURITY_MODE_ENCRYPTION_WITH_MITM : + BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm); + break; + case GattCharacteristic::SECURITY_MODE_SIGNED_NO_MITM : + BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.write_perm); + break; + case GattCharacteristic::SECURITY_MODE_SIGNED_WITH_MITM : + BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.write_perm); + break; + }; } ble_gatts_attr_t attr_char_value = {0}; diff --git a/btle/custom/custom_helper.h b/btle/custom/custom_helper.h index e5c54d5..c04ed4b 100644 --- a/btle/custom/custom_helper.h +++ b/btle/custom/custom_helper.h @@ -20,6 +20,7 @@ #include "common/common.h" #include "ble.h" #include "UUID.h" +#include "GattCharacteristic.h" #ifdef __cplusplus extern "C" { @@ -33,6 +34,7 @@ ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid); error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t *p_uuid, uint8_t properties, + GattCharacteristic::ble_gatt_char_required_security_t requiredSecurity, uint8_t *p_data, uint16_t min_length, uint16_t max_length, diff --git a/nRF51GattServer.cpp b/nRF51GattServer.cpp index 18ff4b0..464456d 100644 --- a/nRF51GattServer.cpp +++ b/nRF51GattServer.cpp @@ -86,6 +86,7 @@ ble_error_t nRF51GattServer::addService(GattService &service) custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID, &nordicUUID, p_char->getProperties(), + p_char->getRequiredSecurity(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), p_char->getValueAttribute().getMaxLength(),