Skip to content

Commit 1c32e6a

Browse files
committed
bond works on some devices
1 parent 310a1f8 commit 1c32e6a

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

docs/infos

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
Linux reicht es wenn irgendwie viel bytes im report sind, welches zum gerät geschickt wird.
22
Windows und Android muss die report größe mit der größe des report deskriptors übereinstimmen.
3-
IOS benötigt eine sichere Verbindung und dann noch die erlaubnis auf das report zuzugreifen wenn es eine sichere verbindung ist. Zusätzlich darf während dem aufbau einer sicheren verbindung keine connection updates durchgeführt werden.
3+
IOS benötigt eine sichere Verbindung und dann noch die erlaubnis auf das report zuzugreifen wenn es eine sichere verbindung ist. Zusätzlich darf während dem aufbau einer sicheren verbindung keine connection updates durchgeführt werden.
4+
5+
Zusätzlich muss für ios die irk key von ios geräten zwischengespeichert werden, da nach einen neustart des ios geräts oder alle x minuten die bluetooth adresse erneurt wird und sonst das gerät sich nicht mehr mit dem esp verbinden kann.
6+
7+
Resolvable private (RPA) --> Address randomly generated from an identity address and an identity resolving key (IRK).

docs/todo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
- Update connection parameter wieder hinzufügen (bleprh anschauen)
2-
- Schauen warum unter ios sich bei erneuter verbindung das gerät nicht verbinden kann
32
- Bei einem erneuten verbindungsaufbau wird das gestoppte updaten der daten nicht wieder gestartet
4-
- vielleicht mal schauen ob a Resolvable Private Address schwierig sind hinzuzufügen statt publich adressen (bleprh anschauen)
3+
- IOS kann den bond nicht wiederherstellen, wenn ESP neugestartet wird (muss neu hinzugefügt werden)
4+
- Android kann schon beim enkoppeln und erneuten koppeln nicht mehr bonden ohne nachfrage
5+
- Linux geht immer
56
- Code cleanup
67
- Kommentare von main nehmen und in diesen branch hinzufügen
78
- Mergen in main

main/gatt_svr.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ static int gatt_svr_chr_hid(uint16_t conn_handle, uint16_t attr_handle,
200200
int *test = OS_MBUF_DATA(ctxt->om,int *);
201201
//00 == hid host is entering the suspend state
202202
//01 == hid host is exiting the suspend state
203-
ESP_LOGW("ASDF", "WRITE TO CONTROL POINT %d",*test);
203+
//nur das erste bit betrachten
204+
//unter ios wird der suspend state schon geändert wenn man das gerät nur umdreht und das display noch nicht eingeschalten hat :)
205+
int wakeupInfo = *test & 0b01;
206+
ESP_LOGW("ASDF", "WRITE TO CONTROL POINT %d",wakeupInfo);
204207
return 0;
205208
}
206209

main/main.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "blehr_sens.h"
1616
#include "host/ble_uuid.h"
1717
#include "esp_peripheral.h"
18+
#include "host/ble_hs_pvcy.h"
1819

1920
static const char *tag = "NimBLE_BLE_HeartRate";
2021
static const ble_uuid16_t hid_service_uuid = BLE_UUID16_INIT(0x1812);
@@ -29,10 +30,26 @@ static const char *device_name = "HID Device";
2930

3031
static int blehr_gap_event(struct ble_gap_event *event, void *arg);
3132

32-
static uint8_t blehr_addr_type;
33+
static uint8_t blehr_addr_type = BLE_ADDR_RANDOM;
3334

3435
static bool volumeUp = true;
3536

37+
static void
38+
ble_app_set_addr(void)
39+
{
40+
ble_addr_t addr;
41+
int rc;
42+
43+
/* generate new non-resolvable private address */
44+
rc = ble_hs_id_gen_rnd(0, &addr);
45+
assert(rc == 0);
46+
47+
/* set generated address */
48+
rc = ble_hs_id_set_rnd(addr.val);
49+
50+
assert(rc == 0);
51+
}
52+
3653
/**
3754
* Utility function to log an array of bytes.
3855
*/
@@ -305,6 +322,13 @@ blehr_gap_event(struct ble_gap_event *event, void *arg)
305322
ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc);
306323
}
307324
return 0;
325+
case BLE_GAP_EVENT_NOTIFY_TX:
326+
//Represents a transmitted ATT notification or indication, or a
327+
//completed indication transaction.
328+
ESP_LOGI("asdf", "notify tx event occured");
329+
return 0;
330+
default:
331+
ESP_LOGI("ASDF", "GAP EVENT ID: %d\n",event->type);
308332
}
309333

310334
return 0;
@@ -315,6 +339,15 @@ blehr_on_sync(void)
315339
{
316340
int rc;
317341

342+
/* Generate a non-resolvable private address. */
343+
//ble_app_set_addr();
344+
345+
ble_hs_pvcy_rpa_config(1);
346+
347+
/* Make sure we have proper identity address set (public preferred) */
348+
//rc = ble_hs_util_ensure_addr(1);
349+
350+
318351
rc = ble_hs_id_infer_auto(0, &blehr_addr_type);
319352
assert(rc == 0);
320353

@@ -362,10 +395,33 @@ void app_main(void)
362395
/* Initialize the NimBLE host configuration */
363396
ble_hs_cfg.sync_cb = blehr_on_sync;
364397
ble_hs_cfg.reset_cb = blehr_on_reset;
398+
/**
399+
* Round-robin status callback. If a there is insufficient storage capacity
400+
* for a new record, delete the oldest bond and proceed with the persist
401+
* operation.
402+
*
403+
* Note: This is not the best behavior for an actual product because
404+
* uninteresting peers could cause important bonds to be deleted. This is
405+
* useful for demonstrations and sample apps.
406+
*/
407+
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
408+
//optional wird ausgeführt wenn ein gatt resource (characteristic, descriptor, sevice) hinzugefügt wird. Also nicht benötigt
365409
ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
410+
411+
//io types zum aufbau einer sicheren verbindung
412+
//BLE_SM_IO_CAP_DISP_ONLY = Display only
413+
//BLE_SM_IO_CAP_DISP_YES_NO = Display & yes & no buttons
414+
//BLE_SM_IO_CAP_KEYBOARD_ONLY = Keyboard only
415+
//BLE_SM_IO_CAP_NO_IO = just work
416+
//BLE_SM_IO_CAP_KEYBOARD_DISP = Keyboard and display
366417
ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO;
367418
ble_hs_cfg.sm_sc = 1;
419+
ble_hs_cfg.sm_bonding = 1;
368420
ble_hs_cfg.sm_mitm = 1;
421+
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
422+
/* Refer components/nimble/nimble/nimble/host/include/host/ble_sm.h for
423+
* more information */
424+
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
369425

370426
/* name, period/time, auto reload, timer ID, callback */
371427
blehr_tx_timer = xTimerCreate("blehr_tx_timer", pdMS_TO_TICKS(2000), pdTRUE, (void *)0, blehr_tx_hrate);

0 commit comments

Comments
 (0)