Skip to content

Commit 4575ccb

Browse files
Copiloth2zero
andcommitted
Add onPassKeyEntry to NimBLEServerCallbacks and onPassKeyDisplay to NimBLEClientCallbacks
Co-authored-by: h2zero <32826625+h2zero@users.noreply.github.com>
1 parent 045344e commit 4575ccb

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/NimBLEClient.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,16 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
12101210
break;
12111211
}
12121212

1213-
if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
1213+
if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
1214+
struct ble_sm_io pkey = {0, 0};
1215+
pkey.action = event->passkey.params.action;
1216+
pkey.passkey = NimBLEDevice::getSecurityPasskey();
1217+
if (pkey.passkey == 123456) {
1218+
pkey.passkey = pClient->m_pClientCallbacks->onPassKeyDisplay(peerInfo);
1219+
}
1220+
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
1221+
NIMBLE_LOGD(LOG_TAG, "BLE_SM_IOACT_DISP; ble_sm_inject_io result: %d", rc);
1222+
} else if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
12141223
NIMBLE_LOGD(LOG_TAG, "Passkey on device's display: %" PRIu32, event->passkey.params.numcmp);
12151224
pClient->m_pClientCallbacks->onConfirmPasskey(peerInfo, event->passkey.params.numcmp);
12161225
} else if (event->passkey.params.action == BLE_SM_IOACT_OOB) {
@@ -1314,6 +1323,11 @@ void NimBLEClientCallbacks::onPassKeyEntry(NimBLEConnInfo& connInfo) {
13141323
NimBLEDevice::injectPassKey(connInfo, 123456);
13151324
} // onPassKeyEntry
13161325

1326+
uint32_t NimBLEClientCallbacks::onPassKeyDisplay(NimBLEConnInfo& connInfo) {
1327+
NIMBLE_LOGD(CB_TAG, "onPassKeyDisplay: default");
1328+
return NimBLEDevice::getSecurityPasskey();
1329+
} // onPassKeyDisplay
1330+
13171331
void NimBLEClientCallbacks::onAuthenticationComplete(NimBLEConnInfo& connInfo) {
13181332
NIMBLE_LOGD(CB_TAG, "onAuthenticationComplete: default");
13191333
} // onAuthenticationComplete

src/NimBLEClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ class NimBLEClientCallbacks {
187187
*/
188188
virtual void onPassKeyEntry(NimBLEConnInfo& connInfo);
189189

190+
/**
191+
* @brief Called when using passkey entry pairing and the passkey should be displayed.
192+
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.
193+
* @return The passkey to display to the user. The peer device must enter this passkey to complete the pairing.
194+
*/
195+
virtual uint32_t onPassKeyDisplay(NimBLEConnInfo& connInfo);
196+
190197
/**
191198
* @brief Called when the pairing procedure is complete.
192199
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.\n

src/NimBLEServer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,15 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) {
678678
// }
679679
// rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
680680
// NIMBLE_LOGD(LOG_TAG, "BLE_SM_IOACT_OOB; ble_sm_inject_io result: %d", rc);
681+
} else if (event->passkey.params.action == BLE_SM_IOACT_INPUT) {
682+
NIMBLE_LOGD(LOG_TAG, "Enter the passkey");
683+
684+
rc = ble_gap_conn_find(event->passkey.conn_handle, &peerInfo.m_desc);
685+
if (rc != 0) {
686+
return BLE_ATT_ERR_INVALID_HANDLE;
687+
}
688+
689+
pServer->m_pServerCallbacks->onPassKeyEntry(peerInfo);
681690
} else if (event->passkey.params.action == BLE_SM_IOACT_NONE) {
682691
NIMBLE_LOGD(LOG_TAG, "No passkey action required");
683692
}
@@ -1113,6 +1122,11 @@ uint32_t NimBLEServerCallbacks::onPassKeyDisplay() {
11131122
return 123456;
11141123
} // onPassKeyDisplay
11151124

1125+
void NimBLEServerCallbacks::onPassKeyEntry(NimBLEConnInfo& connInfo) {
1126+
NIMBLE_LOGD("NimBLEServerCallbacks", "onPassKeyEntry: default: 123456");
1127+
NimBLEDevice::injectPassKey(connInfo, 123456);
1128+
} // onPassKeyEntry
1129+
11161130
void NimBLEServerCallbacks::onConfirmPassKey(NimBLEConnInfo& connInfo, uint32_t pin) {
11171131
NIMBLE_LOGD("NimBLEServerCallbacks", "onConfirmPasskey: default: true");
11181132
NimBLEDevice::injectConfirmPasskey(connInfo, true);

src/NimBLEServer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ class NimBLEServerCallbacks {
181181
*/
182182
virtual uint32_t onPassKeyDisplay();
183183

184+
/**
185+
* @brief Called when using passkey entry pairing and the peer requires the passkey to be entered.
186+
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
187+
* about the peer connection parameters.
188+
* @details The application should call NimBLEDevice::injectPassKey with the passkey
189+
* displayed on the peer device to complete the pairing process.
190+
*/
191+
virtual void onPassKeyEntry(NimBLEConnInfo& connInfo);
192+
184193
/**
185194
* @brief Called when using numeric comparision for pairing.
186195
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information

0 commit comments

Comments
 (0)