Skip to content

Commit bbf68a4

Browse files
committed
comments updated
1 parent 47cfbce commit bbf68a4

File tree

8 files changed

+202
-107
lines changed

8 files changed

+202
-107
lines changed

components/ble/ble.c

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,57 @@
1414
#include "host/util/util.h"
1515
#include "nimble/nimble_port_freertos.h"
1616

17+
/**
18+
* This callback is executed when the host and controller become synced. This happens at startup and after a reset
19+
*/
1720
static void bleOnSync(void);
21+
22+
/**
23+
* This callback is executed when the host resets itself and the controller
24+
*
25+
* @param reason not used
26+
*/
1827
static void bleOnReset(int reason);
1928

20-
/* Define template prototype for store*/
21-
//located in nimble/host/store/config/src/ble_store_config.c
22-
//never definded in a .h file
29+
/**
30+
* Define template prototype for store. located in nimble/host/store/config/src/ble_store_config.c. Not defined in any .h nimble file.
31+
*/
2332
void ble_store_config_init(void);
2433

2534
static const char *tag_BLE = "SimLinkModule_BLE";
2635

2736
uint8_t bleAddressType = BLE_ADDR_RANDOM;
2837

2938
void initBLE(){
30-
//store for returncodes
31-
//mynewt.apache.org/latest/network/ble_hs/ble_hs_return_codes.html
39+
//list of returncodes: mynewt.apache.org/latest/network/ble_hs/ble_hs_return_codes.html
3240
int rc;
3341

34-
/* Initialize NVS — it is used to store PHY calibration data */
35-
//NVS = Non-volatile storage library is designed to store key-balue pairs in flash
36-
//Ablauf ist in esp idf beschrieben
37-
//docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble/index.html
42+
/*
43+
* Initialize NVS — it is used to store PHY calibration data
44+
* NVS = Non-volatile storage library is designed to store key-value pairs in flash
45+
* docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble/index.html
46+
*/
3847
esp_err_t ret = nvs_flash_init();
3948
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
4049
ESP_ERROR_CHECK(nvs_flash_erase());
4150
ret = nvs_flash_init();
4251
}
4352
ESP_ERROR_CHECK(ret);
4453

45-
//Controller initialization, enable and HCI initialization calls have been moved to nimble_port_init. This function can be deleted directly.
46-
//ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
54+
/*
55+
*Required in older ESP-IDF version.
56+
*Controller initialization, enable and HCI initialization calls have been moved to nimble_port_init. This function can be deleted directly.
57+
*ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
58+
*/
4759

4860
nimble_port_init();
4961

50-
/* Initialize the NimBLE host configuration */
62+
//Initialize the NimBLE host configuration
5163
ble_hs_cfg.sync_cb = bleOnSync;
5264
ble_hs_cfg.reset_cb = bleOnReset;
5365

54-
/**
55-
* Round-robin status callback. If a there is insufficient storage capacity
66+
/*
67+
* Round-robin status callback. If there is insufficient storage capacity
5668
* for a new record, delete the oldest bond and proceed with the persist
5769
* operation.
5870
*
@@ -62,32 +74,41 @@ void initBLE(){
6274
*/
6375
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
6476

65-
//optional wird ausgeführt wenn ein gatt resource (characteristic, descriptor, sevice) hinzugefügt wird. Also nicht benötigt
77+
//optional callback. Called when a GATT resource (characteristic, descriptor, sevice) is added.
6678
ble_hs_cfg.gatts_register_cb = gattSvrRegisterCb;
6779

68-
/*Security Manager local input output capabilities*/
69-
//io types zum aufbau einer sicheren verbindung
70-
//BLE_SM_IO_CAP_DISP_ONLY = Display only
71-
//BLE_SM_IO_CAP_DISP_YES_NO = Display & yes & no buttons
72-
//BLE_SM_IO_CAP_KEYBOARD_ONLY = Keyboard only
73-
//BLE_SM_IO_CAP_NO_IO = just work
74-
//BLE_SM_IO_CAP_KEYBOARD_DISP = Keyboard and display
75-
//BLE_SM_IO_CAP_KEYBOARD_ONLY && BLE_SM_IO_CAP_KEYBOARD_DISP not implemented
80+
/*Security Manager local input output capabilities
81+
*io types to establish a secure connection
82+
*BLE_SM_IO_CAP_DISP_ONLY = Display only
83+
*BLE_SM_IO_CAP_DISP_YES_NO = Display & yes & no buttons
84+
*BLE_SM_IO_CAP_KEYBOARD_ONLY = Keyboard only
85+
*BLE_SM_IO_CAP_NO_IO = just work
86+
*BLE_SM_IO_CAP_KEYBOARD_DISP = Keyboard and display
87+
*BLE_SM_IO_CAP_KEYBOARD_ONLY && BLE_SM_IO_CAP_KEYBOARD_DISP not implemented
88+
*/
7689
ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_YES_NO;
90+
7791
/*Security Manager secure connections flag
78-
if set proper flag in pairing request/response will be set. this results in using LE Secure Connections for pairing if also supported by remote device. Fallback to legacy pairing if not supported by remote.*/
92+
*if set proper flag in pairing request/response will be set. this results in using LE Secure Connections for pairing if also supported by remote device. Fallback to legacy pairing if not supported by remote.
93+
*/
7994
ble_hs_cfg.sm_sc = 1;
80-
/*security Manager bond flag
81-
if set proper flag in Pairing request/response will be set. This results in storing keys distributed during bonding.*/
95+
96+
/*Security Manager bond flag
97+
*if set proper flag in Pairing request/response will be set. This results in storing keys distributed during bonding.
98+
*/
8299
ble_hs_cfg.sm_bonding = 1;
83-
/*security manager mitm flag
84-
if set proper flag in pairing request/response will be set. This results in requiring man-in-the-middle protection when pairing.*/
100+
101+
/*Security manager MITM flag
102+
*if set proper flag in pairing request/response will be set. This results in requiring man-in-the-middle protection when pairing.
103+
*/
85104
ble_hs_cfg.sm_mitm = 1;
86-
/*Security Manager Local Key Distribution Mask*/
105+
106+
/*Security Manager Local Key Distribution Mask
107+
*Refer components/nimble/nimble/nimble/host/include/host/ble_sm.h for
108+
* more information
109+
*/
87110
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
88-
/* Refer components/nimble/nimble/nimble/host/include/host/ble_sm.h for
89-
* more information */
90-
/*Security Manager Remote Key Distribution Mask*/
111+
//Security Manager Remote Key Distribution Mask
91112
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
92113

93114
rc = gattSvrInit();
@@ -97,30 +118,31 @@ void initBLE(){
97118
rc = ble_svc_gap_device_name_set(CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME);
98119
assert(rc == 0);
99120

100-
//set the appearance of the device
101-
//0x03c3 = joystick; 0x03c4 = gamepad
121+
/*set the appearance of the device
122+
*0x03c3 = joystick; 0x03c4 = gamepad
123+
*/
102124
ble_svc_gap_device_appearance_set(0x03C4);
103125

104-
//https://github.com/espressif/esp-nimble/issues/33
105-
//KEINE AHNUNG WAS DAS MACHT ABER DADURCH KANN SICH DER ESP NACH EINEN NEUSTART WIEDER MIT DEM GERÄT VERBINDEN
106-
/* XXX Need to have template for store */
126+
/*store the identity resolving key (IRK) to establish a connection after restarting the esp.
127+
*https://github.com/espressif/esp-nimble/issues/33
128+
*/
107129
ble_store_config_init();
108130
}
109131

110-
//This callback is executed when the host and controller become synced. This happens at startup and after a reset
111132
static void bleOnSync(void){
112-
//int rc;
113-
114133
//rpa = resolvable private address; Address randomly generated from an identity address and an identity resolving key (IRK).
115134
ble_hs_pvcy_rpa_config(1);
116135

117-
/* Make sure we have proper identity address set (public preferred) */
118-
//rc = ble_hs_util_ensure_addr(0);
136+
/* Make sure we have proper identity address set (public preferred)
137+
* Works without that
138+
* rc = ble_hs_util_ensure_addr(0);
139+
*/
119140

120141

121-
/*use privacy*/
122-
//rc = ble_hs_id_infer_auto(bleAddressType, &bleAddressType);
123-
//assert(rc == 0);
142+
/* set the BLE address type
143+
* Works without that
144+
* rc = ble_hs_id_infer_auto(bleAddressType, &bleAddressType);
145+
*/
124146

125147
uint8_t addr_val[6] = {0};
126148
if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr_val, NULL)) {
@@ -134,22 +156,22 @@ static void bleOnSync(void){
134156
print_addr(addr_val);
135157
ESP_LOGI(tag_BLE, "\n");
136158

159+
//used to request a context switch to another task
137160
taskYIELD();
138161

139-
/*start advertising, when controller and host are in sync*/
162+
//start advertising, when controller and host are in sync
140163
bleAdvertise();
141164
}
142165

143-
//This callback is executed when the host resets itself and the controller
144166
static void bleOnReset(int reason){
145167
ESP_LOGI(tag_BLE, "Resetting state; reason=%d\n", reason);
146168
}
147169

148-
//start nimble in a task
149170
void bleHostTask(void *param)
150171
{
151172
ESP_LOGI(tag_BLE, "BLE Host Task Started");
152-
/* This function will return only when nimble_port_stop() is executed */
173+
174+
//This function will return only when nimble_port_stop() is executed
153175
nimble_port_run();
154176

155177
nimble_port_freertos_deinit();

0 commit comments

Comments
 (0)