22//https://macchina.io/blog/internet-of-things/communication-with-low-energy-bluetooth-devices-on-linux/
33
44
5- //TODO: alle geräte funktionieren außer ios; hier reinschauen wie notify gemacht wird; muss wahrscheinlich secure verbindung sein
6- //https://github.com/Xi-MingYu/Central/tree/394ccfb02f2c58d8ea7eb0e72b0abe927602f5ea/lib/NimBLE/src
7- //vielleicht eine secure verbindung aufbauen https://github.com/h2zero/NimBLE-Arduino/issues/222
8- //https://github.com/wakwak-koba/ESP32-NimBLE-Keyboard/blob/ec4ce707f00d260f30286191fe63dc23d65a5934/BleKeyboard.cpp#L141
9- // Diese verschlüsselung mal hinzufügen
5+ //TODO: Update connection parameter wieder hinzufügen (bleprh anschauen)
6+ //TODO: Schauen warum unter ios sich bei erneuter verbindung das gerät nicht verbinden kann
7+ //TODO: Bei einem erneuten verbindungsaufbau wird das gestoppte updaten der daten nicht wieder gestartet
8+ //TODO: vielleicht mal schauen ob a Resolvable Private Address schwierig sind hinzuzufügen statt publich adressen (bleprh anschauen)
9+ //Update connection parameters are not allowed during iPhone HID encryption, slave turns off the ability to automatically update connection parameters during encryption.
10+
11+ //https://github.com/espressif/esp-idf/issues/3532
1012
1113#include "esp_log.h"
1214#include "nvs_flash.h"
2123#include "services/gap/ble_svc_gap.h"
2224#include "blehr_sens.h"
2325#include "host/ble_uuid.h"
26+ #include "esp_peripheral.h"
2427
2528static const char * tag = "NimBLE_BLE_HeartRate" ;
2629static const ble_uuid16_t hid_service_uuid = BLE_UUID16_INIT (0x1812 );
@@ -183,6 +186,9 @@ blehr_tx_hrate(xTimerHandle ev)
183186static int
184187blehr_gap_event (struct ble_gap_event * event , void * arg )
185188{
189+ struct ble_gap_conn_desc desc ;
190+ int rc ;
191+
186192 switch (event -> type ) {
187193 case BLE_GAP_EVENT_CONNECT :
188194 /* A new connection was established or a connection attempt failed */
@@ -205,13 +211,15 @@ blehr_gap_event(struct ble_gap_event *event, void *arg)
205211 .supervision_timeout = 1860 /10 //10ms units, laut apple größer als itvl_max * (latency + 1) * 3
206212 };
207213
208- ESP_ERROR_CHECK (ble_gap_update_params (event -> connect .conn_handle , & connectionParameters ));
214+ // ESP_ERROR_CHECK(ble_gap_update_params(event->connect.conn_handle, &connectionParameters));
209215
210216 conn_handle = event -> connect .conn_handle ;
217+ ble_gap_security_initiate (conn_handle );
211218 break ;
212219
213220 case BLE_GAP_EVENT_DISCONNECT :
214221 ESP_LOGI ("ASDF" , "disconnect; reason=%d\n" , event -> disconnect .reason );
222+ //531 = Remote User Terminated Connection
215223
216224 /* Connection terminated; resume advertising */
217225 blehr_advertise ();
@@ -239,7 +247,73 @@ blehr_gap_event(struct ble_gap_event *event, void *arg)
239247 event -> mtu .conn_handle ,
240248 event -> mtu .value );
241249 break ;
250+ case BLE_GAP_EVENT_ENC_CHANGE :
251+ ESP_LOGI ("ASDF" , "encryption change event; status=%d " ,
252+ event -> enc_change .status );
253+ break ;
254+ case BLE_GAP_EVENT_REPEAT_PAIRING :
255+ /* We already have a bond with the peer, but it is attempting to
256+ * establish a new secure link. This app sacrifices security for
257+ * convenience: just throw away the old bond and accept the new link.
258+ */
259+ ESP_LOGI ("ASDF" , "establisch new secure link" );
260+
261+ /* Delete the old bond. */
262+ rc = ble_gap_conn_find (event -> repeat_pairing .conn_handle , & desc );
263+ assert (rc == 0 );
264+ ble_store_util_delete_peer (& desc .peer_id_addr );
265+
266+ /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
267+ * continue with the pairing operation.
268+ */
269+ return BLE_GAP_REPEAT_PAIRING_RETRY ;
270+ break ;
242271
272+ case BLE_GAP_EVENT_PASSKEY_ACTION :
273+ ESP_LOGI ("ASDF" , "PASSKEY_ACTION_EVENT started \n" );
274+
275+ struct ble_sm_io pkey = {0 };
276+ int key = 0 ;
277+
278+ if (event -> passkey .params .action == BLE_SM_IOACT_DISP ) {
279+ pkey .action = event -> passkey .params .action ;
280+ pkey .passkey = 123456 ; // This is the passkey to be entered on peer
281+ ESP_LOGI (tag , "Enter passkey %d on the peer side" , pkey .passkey );
282+ rc = ble_sm_inject_io (event -> passkey .conn_handle , & pkey );
283+ ESP_LOGI (tag , "ble_sm_inject_io result: %d\n" , rc );
284+ } else if (event -> passkey .params .action == BLE_SM_IOACT_NUMCMP ) {
285+ ESP_LOGI (tag , "Passkey on device's display: %d" , event -> passkey .params .numcmp );
286+ ESP_LOGI (tag , "Accept or reject the passkey through console in this format -> key Y or key N" );
287+ pkey .action = event -> passkey .params .action ;
288+ if (scli_receive_key (& key )) {
289+ pkey .numcmp_accept = key ;
290+ } else {
291+ pkey .numcmp_accept = 0 ;
292+ ESP_LOGE (tag , "Timeout! Rejecting the key" );
293+ }
294+ rc = ble_sm_inject_io (event -> passkey .conn_handle , & pkey );
295+ ESP_LOGI (tag , "ble_sm_inject_io result: %d\n" , rc );
296+ } else if (event -> passkey .params .action == BLE_SM_IOACT_OOB ) {
297+ static uint8_t tem_oob [16 ] = {0 };
298+ pkey .action = event -> passkey .params .action ;
299+ for (int i = 0 ; i < 16 ; i ++ ) {
300+ pkey .oob [i ] = tem_oob [i ];
301+ }
302+ rc = ble_sm_inject_io (event -> passkey .conn_handle , & pkey );
303+ ESP_LOGI (tag , "ble_sm_inject_io result: %d\n" , rc );
304+ } else if (event -> passkey .params .action == BLE_SM_IOACT_INPUT ) {
305+ ESP_LOGI (tag , "Enter the passkey through console in this format-> key 123456" );
306+ pkey .action = event -> passkey .params .action ;
307+ if (scli_receive_key (& key )) {
308+ pkey .passkey = key ;
309+ } else {
310+ pkey .passkey = 0 ;
311+ ESP_LOGE (tag , "Timeout! Passing 0 as the key" );
312+ }
313+ rc = ble_sm_inject_io (event -> passkey .conn_handle , & pkey );
314+ ESP_LOGI (tag , "ble_sm_inject_io result: %d\n" , rc );
315+ }
316+ return 0 ;
243317 }
244318
245319 return 0 ;
@@ -297,6 +371,10 @@ void app_main(void)
297371 /* Initialize the NimBLE host configuration */
298372 ble_hs_cfg .sync_cb = blehr_on_sync ;
299373 ble_hs_cfg .reset_cb = blehr_on_reset ;
374+ ble_hs_cfg .gatts_register_cb = gatt_svr_register_cb ;
375+ ble_hs_cfg .sm_io_cap = BLE_SM_IO_CAP_NO_IO ;
376+ ble_hs_cfg .sm_sc = 1 ;
377+ ble_hs_cfg .sm_mitm = 1 ;
300378
301379 /* name, period/time, auto reload, timer ID, callback */
302380 blehr_tx_timer = xTimerCreate ("blehr_tx_timer" , pdMS_TO_TICKS (2000 ), pdTRUE , (void * )0 , blehr_tx_hrate );
0 commit comments