feat(nimble): Authorize or deauthorize a BLE device for a connection

This commit is contained in:
Darshan Dobariya
2024-03-11 10:23:45 +05:30
committed by Rahul Tank
parent 91141f2588
commit 9da05fac77
3 changed files with 49 additions and 0 deletions
+17
View File
@@ -63,6 +63,23 @@ int ble_gap_clear_legacy_adv(void);
int ble_gap_duplicate_exception_list(uint8_t subcode, uint8_t type, uint8_t *value, void *cb);
#endif
/**
* Authorizes or deauthorizes a BLE device for a connection.
*
* This function updates the security flags of a BLE connection to authorize or
* deauthorize a device for the specified connection.
*
* @param conn_handle The handle corresponding to the connection to
* authorize.
* @param authorized Authorized the device or not.
*
* @return 0 on success;
* BLE_HS_ENOTCONN if the connection handle is not found.
* BLE_HS_EAUTHOR if the device is not authenticated before authorization.
*/
int
ble_gap_dev_authorization(uint16_t conn_handle, bool authorized);
void ble_gap_rx_test_evt(const void *buf, uint8_t len);
void ble_gap_tx_test_evt(const void *buf, uint8_t len);
void ble_gap_end_test_evt(const void *buf, uint8_t len);
+4
View File
@@ -202,6 +202,10 @@ struct ble_gap_sec_state {
/** Size of a key used for encryption */
unsigned key_size:5;
/** Current device security state*/
unsigned authorize:1;
};
/** Advertising parameters */
+28
View File
@@ -6247,6 +6247,34 @@ done:
#endif
}
int
ble_gap_dev_authorization(uint16_t conn_handle, bool authorized)
{
struct ble_hs_conn *conn = ble_hs_conn_find(conn_handle);
if (conn != NULL) {
if (!(conn->bhc_sec_state.authenticated)) {
// Device should be authenticated before authorization
BLE_HS_LOG(ERROR, "Authorized should occur after successful Authentication(MITM protection) \n");
return BLE_HS_EAUTHOR;
}
// Update connection security flags
if (authorized) {
conn->bhc_sec_state.authorize = 1;
} else {
conn->bhc_sec_state.authorize = 0;
}
} else {
// Connection handle not found
BLE_HS_LOG(ERROR, "Can't find connection \n");
return BLE_HS_ENOTCONN;
}
return 0;
}
int
ble_gap_pair_initiate(uint16_t conn_handle)
{