I am wondering about a few lines in the gatts_server example:
https://github.com/espressif/esp-idf/bl ... tts_demo.c
Code: Select all
static uint8_t adv_config_done = 0;
#define adv_config_flag (1 << 0)
#define scan_rsp_config_flag (1 << 1)
Code: Select all
esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data));
[…] // error handling
adv_config_done |= adv_config_flag;
esp_err_t raw_scan_ret = esp_ble_gap_config_scan_rsp_data_raw(raw_scan_rsp_data, sizeof(raw_scan_rsp_data));
[…] // error handling
adv_config_done |= scan_rsp_config_flag;
Code: Select all
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
adv_config_done &= (~adv_config_flag);
if (adv_config_done==0){
esp_ble_gap_start_advertising(&adv_params);
}
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
adv_config_done &= (~scan_rsp_config_flag);
if (adv_config_done==0){
esp_ble_gap_start_advertising(&adv_params);
}
break;
What happens:
- from underlying ble: the switch cases get only executed if respective config function was called
- if only one config was done the case gets executed.
- if both configs are done the first case statement's if statement returns false but deletes its own flag. Then the next if statement is true so esp_ble_gap_start_advertising is called only once.
Can we replace the entire logic with one variable is_advertising?
Code: Select all
// global variable
static uint8_t is_advertising = 0;
// in gap_event_handler(…) function:
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
if (is_advertising==0){
esp_ble_gap_start_advertising(&adv_params);
is_advertising = 1;
}
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
if (is_advertising==0){
esp_ble_gap_start_advertising(&adv_params);
is_advertising = 1;
}
break;
In my opinion this would be way clearer and easier to understand.
Furthermore the logic is also dependent on ble generating the right events. If you take the logic to somewhere else you might get some surprises:
The logic returns always true except that both flags are set simultaneously. Even it no flag is set at all it evaluates as true (does not happen here as mentioned above because ble only raises proper events).
I attach a c file where I tested the logic
But I am no expert on ble so I am asking if the changes would have unintended side effects I did not see?
(like with multiple gatt profiles, or whatever else).
Otherwise I would like to change the example.
I am looking forward to feedback so I can stop thinking about this :-)
Another quick question: would a simple ble scanner example be worth an own example?
Just for scanning available devices you don't need a gatt layer and on my first attempt it took me a bit to strip the gatt example down to a simple scanner.
However I think that that is a common think to do. Should I suggest it as an example?
Best regards!