Skip to content

Commit 177a539

Browse files
committed
comments updated
1 parent bbf68a4 commit 177a539

File tree

4 files changed

+96
-117
lines changed

4 files changed

+96
-117
lines changed

components/ble/gap.c

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include "ssd1306.h"
1414
#include "button.h"
1515

16+
/**
17+
* The callback to associate with this advertising procedure. If advertising ends, the event is reported through this callback. If advertising results in a connection, the connection inherits this callback as its event-reporting mechanism.
18+
*
19+
* @param event data about the GAP event
20+
* @param arg not used
21+
* @return always 0
22+
*/
1623
static int bleGAPEevent(struct ble_gap_event *event, void *arg);
1724

1825
static const char *tag_GAP = "SimLinkModule_GAP";
@@ -22,11 +29,6 @@ uint16_t conn_handle = 0;
2229
bool notify_state_report_data = false;
2330
bool notify_state_battery_status = false;
2431

25-
/*
26-
* Enables advertising with parameters:
27-
* o General discoverable mode
28-
* o Undirected connectable mode
29-
*/
3032
void bleAdvertise(void){
3133
ssd1306_clear();
3234
ssd1306_setString("Connecting",10,9);
@@ -37,27 +39,14 @@ void bleAdvertise(void){
3739
int rc;
3840
const char* name;
3941

40-
/*
41-
* Set the advertisement data included in our advertisements:
42-
* o Flags (indicates advertisement type and other general info)
43-
* o Advertising tx power
44-
* o Device name
45-
*/
42+
//set the advertisement data included in the advertisement
4643
memset(&fields, 0, sizeof(fields));
4744

48-
/*
49-
* Advertise two flags:
50-
* o Discoverability in forthcoming advertisement (general)
51-
* o BLE-only (BR/EDR unsupported)
52-
*/
45+
//discoverability in forhtcoming advertisement and BLE only (BR/EDR in nimlbe not supported)
5346
fields.flags = BLE_HS_ADV_F_DISC_GEN |
5447
BLE_HS_ADV_F_BREDR_UNSUP;
5548

56-
/*
57-
* Indicate that the TX power level field should be included; have the
58-
* stack fill this value automatically. This is done by assigning the
59-
* special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
60-
*/
49+
//tx power level field ist filled automatically via the BLE stack
6150
fields.tx_pwr_lvl_is_present = 1;
6251
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
6352

@@ -69,25 +58,26 @@ void bleAdvertise(void){
6958
fields.appearance = ble_svc_gap_device_appearance();
7059
fields.appearance_is_present = 1;
7160

61+
//only one service can be added. ADV packet size to small for more services
7262
fields.num_uuids16 = 1;
7363
fields.uuids16_is_complete = 0;
7464
fields.uuids16 = (ble_uuid16_t[]){
7565
hid_service_uuid
7666
};
77-
//geht nur eins weil sonst die adv packetgröße nicht reicht
7867

7968
rc = ble_gap_adv_set_fields(&fields);
8069
if (rc != 0) {
8170
ESP_LOGI(tag_GAP, "error setting advertisement data; rc=%d\n", rc);
8271
return;
8372
}
8473

85-
/* Begin advertising */
8674
memset(&adv_params, 0, sizeof(adv_params));
8775
//undirected-connectable
8876
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
8977
//general-discoverable
9078
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
79+
80+
//Begin advertising
9181
rc = ble_gap_adv_start(bleAddressType, NULL, BLE_HS_FOREVER,
9282
&adv_params, bleGAPEevent, NULL);
9383
if (rc != 0) {
@@ -96,33 +86,33 @@ void bleAdvertise(void){
9686
}
9787
}
9888

99-
//The callback to associate with this advertising procedure. If advertising ends, the event is reported through this callback. If advertising results in a connection, the connection inherits this callback as its event-reporting mechanism.
89+
//
10090
static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
10191
struct ble_gap_conn_desc desc;
10292
int rc;
10393

10494
switch (event->type) {
10595
case BLE_GAP_EVENT_CONNECT:
106-
/* A new connection was established or a connection attempt failed */
96+
//A new connection was established or a connection attempt failed
10797
ESP_LOGI(tag_GAP, "connection %s; status=%d\n",
10898
event->connect.status == 0 ? "established" : "failed",
10999
event->connect.status);
110100

111101
if (event->connect.status != 0) {
112-
/* Connection failed; resume advertising */
102+
//Connection failed; resume advertising
113103
bleAdvertise();
114104
} else {
115105
ssd1306_clear();
116106
ssd1306_display();
117107

118108
struct ble_gap_upd_params connectionParameters = {
119109
//itvl: These determine how often the devices will "ping-pong" each other and also when they will send any data required. So if you set the value to something like 20, that would mean packets are sent every 25ms, which will obviously consume more power than say a value of 80 (100ms). The reason for the min max values is so the devices can negotiate a compromise for the best possible communication, you can set these to the same value if you prefer.
120-
.itvl_min = (int)(11.25/1.25), //1.25ms units; laut apple 11.25 minimum fuer hid
121-
.itvl_max = (int)(20/1.25), //minimum ist laut apple eigentlich 15ms deswegen etwas höher setzen
110+
.itvl_min = (int)(11.25/1.25), //1.25ms units; according to apple 11.25 minimum for HID
111+
.itvl_max = (int)(20/1.25), //minimum according to apple actually 15ms therefore slightly higher
122112
//latency: This is how many "ping-pong" (connection interval) events the slave(server) device is allowed to skip without the client device considering the connection terminated. So if you had a 25ms connection interval and you wanted to sleep for 1 second you could set this value to 40 and the client would consider the connection active for up to 40 skipped intervals.
123113
.latency = 30, //up to 30 connection intervals
124114
//timeout: This is the absolute (disconnection) timeout, if no packets are received by either device within this time the connection is considered terminated.
125-
.supervision_timeout = 1860/10 //10ms units, laut apple größer als itvl_max * (latency + 1) * 3
115+
.supervision_timeout = 1860/10 //10ms units, according to apple greater than itvl_max * (latency + 1) * 3
126116
};
127117

128118
//Update connection parameters are not allowed during iPhone HID encryption, slave turns off the ability to automatically update connection parameters during encryption.
@@ -145,7 +135,7 @@ static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
145135
notify_state_report_data = false;
146136
notify_state_battery_status = false;
147137

148-
/* Connection terminated; resume advertising */
138+
//Connection terminated; resume advertising
149139
bleAdvertise();
150140
break;
151141

@@ -164,7 +154,7 @@ static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
164154
break;
165155
}
166156

167-
//muss eigentlich nur gemacht werden bei den attr_handle wo es auf encrypted nur gelesen wird --> input report
157+
//actually only needs to be done on the attr_handle where it is only read if encrypted --> input report
168158
if(!desc.sec_state.encrypted) {
169159
ble_gap_security_initiate(event->subscribe.conn_handle);
170160
}
@@ -195,26 +185,25 @@ static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
195185
}
196186
break;
197187
case BLE_GAP_EVENT_REPEAT_PAIRING:
198-
//https://ubuntu.com/core/docs/bluez/reference/pairing/introduction
199-
/* We already have a bond with the peer, but it is attempting to
188+
/*
189+
* https://ubuntu.com/core/docs/bluez/reference/pairing/introduction
190+
* We already have a bond with the peer, but it is attempting to
200191
* establish a new secure link. This app sacrifices security for
201192
* convenience: just throw away the old bond and accept the new link.
202193
*/
203194
ESP_LOGI(tag_GAP, "establish new secure link");
204195

205-
/* Delete the old bond. */
196+
//Delete the old bond.
206197
rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
207198
assert(rc == 0);
208199
ble_store_util_delete_peer(&desc.peer_id_addr);
209200

210-
/* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
211-
* continue with the pairing operation.
212-
*/
201+
//Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should continue with the pairing operation.
213202
return BLE_GAP_REPEAT_PAIRING_RETRY;
214203
break;
215204

216205
case BLE_GAP_EVENT_PASSKEY_ACTION:
217-
//https://community.nxp.com/t5/Wireless-Connectivity-Knowledge/KW36-Enabling-Out-Of-Band-Pairing-on-a-Bluetooth-LE-Central-and/tac-p/1238698
206+
// info about BLE pairing: https://community.nxp.com/t5/Wireless-Connectivity-Knowledge/KW36-Enabling-Out-Of-Band-Pairing-on-a-Bluetooth-LE-Central-and/tac-p/1238698
218207
ESP_LOGI(tag_GAP, "PASSKEY_ACTION_EVENT started \n");
219208

220209
struct ble_sm_io pkey = {0};
@@ -225,25 +214,26 @@ static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
225214
pkey.action = event->passkey.params.action;
226215

227216
pkey.passkey = esp_random();
228-
/* Max value is 999999 */
217+
//Max value is 999999
229218
pkey.passkey %= 1000000;
230219

231220
char pinStr[12];
232221
snprintf(pinStr, 12,"%06d", (int)pkey.passkey);
233222

223+
//display passkey for the other side
234224
ssd1306_clear();
235225
ssd1306_setString("PIN",48,0);
236226
ssd1306_setString(pinStr,31,18);
237227
ssd1306_display();
238-
//display passkey for the other side
239228

240229
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
241230
ESP_LOGI(tag_GAP, "ble_sm_inject_io result: %d\n", rc);
242231
} else if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
243-
//BLE_SM_IO_CAP_DISP_YES_NO option
244-
245-
//gegenseite kann auch mit yes oder no bestätigen den pin
246-
//verify with buttons Yes or No --> add timeout after x seconds
232+
/*
233+
*BLE_SM_IO_CAP_DISP_YES_NO option
234+
*both sides must answer yes or no
235+
*verify with buttons Yes or No --> add timeout after x seconds
236+
*/
247237
char pinStr[12];
248238
snprintf(pinStr, 12,"%06d", (int)event->passkey.params.numcmp);
249239
ssd1306_clear();
@@ -270,16 +260,18 @@ static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
270260
ESP_LOGI(tag_GAP, "NUMCP_ACCEPT: %d", pkey.numcmp_accept);
271261
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
272262
if(rc != 0){
273-
//https://github.com/espressif/esp-idf/issues/8555#issuecomment-1066591071
274-
// 0x05 = Authentication Failure error code
263+
/*
264+
*Cancel connection if not agreed
265+
*https://github.com/espressif/esp-idf/issues/8555#issuecomment-1066591071
266+
*0x05 = Authentication Failure error code
267+
*/
275268
ble_gap_terminate(conn_handle, 0x05);
276269
}
277270
ESP_LOGI(tag_GAP, "ble_sm_inject_io result: %d\n", rc);
278271
}
279272
return 0;
280273
case BLE_GAP_EVENT_NOTIFY_TX:
281-
//Represents a transmitted ATT notification or indication, or a
282-
//completed indication transaction.
274+
//Represents a transmitted ATT notification or indication, or a completed indication transaction.
283275
return 0;
284276
case BLE_GAP_EVENT_IDENTITY_RESOLVED:
285277
ssd1306_setConnectedImage();

docs/BLE-Debugging.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# iOS
2-
- Unter iOS kann die App [LightBlue](https://punchthrough.com/lightblue/) verwendet werden. Zu beachten ist das Apple bei einigen Diensten nicht die rohen Daten an 3rd party Apps weitergeben [S. 197](https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf). Dadurch können manche Daten nicht gedebugged werden. Ein Service ist beispielsweise HID.
3-
- Eine weitere Möglichkeit für das Debugging für iOS ist mit einem Macbook auf dem XCode läuft. Dafür gibt es [PacketLogger](https://developer.apple.com/bluetooth/) womit Bluetooth-Pakete live gelogged werden können. Zusätzlich muss auf dem iOS-Gerät ein Logging-Profil heruntergeladen werden und installiert werden.
2+
- On iOS, the [LightBlue](https://punchthrough.com/lightblue/) app can be used. Note that Apple does not pass the raw data to 3rd party apps for some services [S. 197](https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf). This means that some data cannot be debugged. One service, for example, is HID.
3+
- Another possibility for debugging for iOS is with a Mac running XCode. For this there is [PacketLogger](https://developer.apple.com/bluetooth/) which can log Bluetooth packets live. Additionally, a logging profile must be downloaded and installed on the iOS device.
44

55
# Android
6-
- Unter Android können die Apps [LightBlue](https://punchthrough.com/lightblue/) und [nRF Connect](https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en&gl=US) verwendet werden. Dort können mehr Daten / Dienste wie unter iOS ausgelesen werden.
7-
- In den Entwickleroptionen kann "Bluetooth HCI-Snoop-Protokoll aktiviert" werden und die Daten gelogged werden und später mittels Wirekshark ausgelesen werden.
6+
- For Android, the apps [LightBlue](https://punchthrough.com/lightblue/) and [nRF Connect](https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en&gl=US) can be used. There you can read more data / services as on iOS.
7+
- In the developer options "Bluetooth HCI snoop protocol can be enabled" and the data can be logged and later inspected using Wirekshark.
88

99
# Linux
1010
- Tools: hcitool, gatttool, bluetoothctl, hidraw-dump, Bluepy
1111

12-
## Allgemein
12+
## General
1313
Edit `/lib/systemd/system/bluetooth.service` and change to:
1414

1515
`ExecStart=/usr/libexec/bluetooth/bluetoothd --noplugin=input,hog`
@@ -18,52 +18,52 @@ Then Bluetooth daemon won't grab input or hog devices and won't create system in
1818

1919
## hcitool
2020

21-
### Verfügbares Bluetoothgerät am Linuxrechner
21+
### Available Bluetooth device at the Linux computer
2222
`hcitool dev`
2323

2424
### Scan for BLE-Devices
2525
`sudo hcitool lescan`
2626

2727
## gatttool
2828

29-
### Verbindung aufbauen
29+
### Establish connection
3030
1. `sudo gatttool [-t random] -b <BLE ADDRESS> -I`
3131
2. `connect`
3232

33-
### Liste aller vorhandenen Dienste aufzeigen
33+
### Show list of all existing services
3434
`primary`
3535

36-
### Liste aller vorhandenen Handles
37-
Jeder Handle ist ein Verbindungspunkt, wo Daten gelesen oder geschrieben werden können.
36+
### List of all existing handles
37+
Each handle is a connection point where data can be read or written.
3838

3939
`char-desc`
4040

41-
### Ein Handle auslesen
41+
### Read a handle
4242
`char-read-hnd <handle>`
4343

44-
### Schreiben eines Werts in ein Handle
44+
### Write a value to the handle
4545
`char-write-req <handle> <data>`
4646

4747
## bluetoothctl
4848
`sudo bluetoothctl`
4949

50-
### Geräte suchen
50+
### Search devices
5151
`scan le`
5252

53-
### Gerät verbinden
53+
### Connect device
5454
`connect <BLE ADDRESS>`
5555

56-
### Info des Geräts
56+
### Device info
5757
`info`
5858

59-
### Menü wechseln, damit mit GATT gearbeitet werden kann
59+
### Change menu to work with GATT
6060
`menu gatt`
6161

62-
#### Attributliste
62+
#### list all attributes
6363
`list-attributes`
6464

65-
#### Attribut auslesen
66-
- `select-attribute <Pfad>`
65+
#### read attribute
66+
- `select-attribute <path>`
6767
- `read`
6868

6969
## hidraw-dump
@@ -74,11 +74,11 @@ Jeder Handle ist ein Verbindungspunkt, wo Daten gelesen oder geschrieben werden
7474
- `cd hidraw-dump`
7575
- `make`
7676

77-
### Auslesen aller HID-Deskriptoren
77+
### GET all HID descriptors
7878
`sudo ./hidraw-dump`
7979

80-
### HID Deskriptor menschen lesbar machen
81-
Auf dieser [Website](https://eleccelerator.com/usbdescreqparser/) umwandeln.
80+
### Make HID descriptor human readable
81+
Use the [website](https://eleccelerator.com/usbdescreqparser/).
8282

8383
## Bluepy
8484

@@ -88,16 +88,16 @@ Auf dieser [Website](https://eleccelerator.com/usbdescreqparser/) umwandeln.
8888
3. `cd bluepy/bluepy`
8989
4. `make`
9090

91-
### Verwendung
91+
### Usage
9292

93-
#### Starten
93+
#### start
9494
`./bluepy-helper`
9595

96-
#### Verbinden
96+
#### connect
9797
`conn <BT ADDRESS>`
9898

99-
#### Liste von Diensten
99+
#### List of services
100100
`svcs`
101101

102-
## Hinweis:
103-
- Debugging des XBox controllers hat nur verbunden, wenn man ihn nicht in den Bluetooth-Einstellungen verbunden hatte und wenn mann ihn erst connecting modus bringt wenn man gatttools offen hat und danach connect aufruft. Beim gatttool darf auch nicht die Option random aktiviert sein.
102+
## Note:
103+
- Debugging the XBox controller only worked if it was not connected in the Bluetooth settings and if the XBox controller was only switched to connecting mode when gatttools was open and `connect` was called. The option random shouldn't be activated in the gatttool.

0 commit comments

Comments
 (0)