我要使用 ESP32-C3 来做一个 BLE Long Range 广播封包,我使用 mul ti_adv_demo.c 来修改,程序档案如下,
Code: [Select all] [Expand/Collapse]
- #define FUNC_SEND_WAIT_SEM(func, sem) do {
- esp_err_t __err_rc = (func);
- if (__err_rc != ESP_OK) {
- ESP_LOGE(LOG_TAG, "%s, message send fail, error = %d", __func__, __err_rc);
- }
- xSemaphoreTake(sem, portMAX_DELAY);
- } while(0);
- static SemaphoreHandle_t test_sem = NULL;
- esp_ble_gap_ext_adv_params_t legacy_adv_params = {
- .type = ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY_IND,
- .interval_min = 320,
- .interval_max = 640,
- .channel_map = ADV_CHNL_ALL,
- .filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
- .primary_phy = ESP_BLE_GAP_PHY_1M,
- .max_skip = 0,
- .secondary_phy = ESP_BLE_GAP_PHY_1M,
- .sid = 0,
- .scan_req_notif = false,
- .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
- };
- esp_ble_gap_ext_adv_params_t ext_adv_params_coded = {
- .type = ESP_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE,
- .interval_min = 640,
- .interval_max = 960,
- .channel_map = ADV_CHNL_ALL,
- .filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
- .primary_phy = ESP_BLE_GAP_PHY_CODED,
- .max_skip = 0,
- .secondary_phy = ESP_BLE_GAP_PHY_CODED,
- .sid = 1,
- .scan_req_notif = false,
- .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
- };
- static uint8_t legacy_adv_data[ = {
- 0x02, 0x01, 0x06,
- 0x02, 0x0a, 0xeb,
- 0x14, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
- 'D', 'V', '_', 'C', 'O', 'D', 'E', 'D'
- };
- static uint8_t legacy_scan_rsp_data[ = {
- 0x02, 0x01, 0x06,
- 0x02, 0x0a, 0xeb,
- 0x15, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
- 'D', 'V', '_', 'L', 'E', 'G', 'A', 'C', 'Y'
- };
- static uint8_t raw_scan_rsp_data_coded[ = {
- 0x02, 0x01, 0x06,
- 0x02, 0x0a, 0xeb,
- 0x14, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
- 'D', 'V', '_', 'C', 'O', 'D', 'E', 'D'
- };
- static esp_ble_gap_ext_adv_t ext_adv[4 = {
- // instance, duration, peroid
- [0 = {0, 0, 0},
- [1 = {1, 0, 0},
- };
- static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
- {
- switch (event) {
- case ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT:
- xSemaphoreGive(test_sem);
- ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status %d", param->ext_adv_set_rand_addr.status);
- break;
- case ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT:
- xSemaphoreGive(test_sem);
- ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status %d", param->ext_adv_set_params.status);
- break;
- case ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT:
- xSemaphoreGive(test_sem);
- ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status %d", param->ext_adv_data_set.status);
- break;
- case ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT:
- xSemaphoreGive(test_sem);
- ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status %d", param->scan_rsp_set.status);
- break;
- case ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT:
- xSemaphoreGive(test_sem);
- ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT, status %d", param->ext_adv_start.status);
- break;
- case ESP_GAP_BLE_EXT_ADV_STOP_COMPLETE_EVT:
- xSemaphoreGive(test_sem);
- ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_STOP_COMPLETE_EVT, status %d", param->ext_adv_stop.status);
- break;
- default:
- break;
- }
- }
- void app_main(void)
- {
- esp_err_t ret;
- // Initialize NVS.
- ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK( ret );
- ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
- ret = esp_bt_controller_init(&bt_cfg);
- if (ret) {
- ESP_LOGE(LOG_TAG, "%s initialize controller failed: %sn", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
- if (ret) {
- ESP_LOGE(LOG_TAG, "%s enable controller failed: %sn", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_init();
- if (ret) {
- ESP_LOGE(LOG_TAG, "%s init bluetooth failed: %sn", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_enable();
- if (ret) {
- ESP_LOGE(LOG_TAG, "%s enable bluetooth failed: %sn", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_ble_gap_register_callback(gap_event_handler);
- if (ret){
- ESP_LOGE(LOG_TAG, "gap register error, error code = %x", ret);
- return;
- }
- vTaskDelay(200 / portTICK_PERIOD_MS);
- test_sem = xSemaphoreCreateBinary();
- // 1M phy legacy adv, ADV_IND
- FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(0, &legacy_adv_params), test_sem);
- FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(0, sizeof(legacy_adv_data), &legacy_adv_data[0), test_sem);
- FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_scan_rsp_data_raw(0, sizeof(legacy_scan_rsp_data), &legacy_scan_rsp_data[0), test_sem);
- // coded phy extend adv, Connectable advertising
- FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(1, &ext_adv_params_coded), test_sem);
- // FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(1, sizeof(legacy_adv_data), &legacy_adv_data[0]), test_sem);
- FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_scan_rsp_data_raw(1, sizeof(raw_scan_rsp_data_coded), &raw_scan_rsp_data_coded[0), test_sem);
- // start all adv
- FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_start(2, &ext_adv[0), test_sem);
- return;
- }
执行时 log 显示程序正确执行,我也于 Sniffer 上看见了 Long Range 格式的 SCAN RSP 封包。
I (641) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (641) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status 0
I (641) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (651) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (661) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (671) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT, status 0
我现在想要将 Long Range 广播封包改成 ADV 封包,但于
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(1, sizeof(legacy_adv_data), &legacy_adv_data[0]), test_sem);
却出现了问题,LOG 如下
I (641) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (641) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status 0
I (641) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (651) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
E (661) BT_BTM: LE EA SetAdvData: cmd err=0x12
I (661) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status 7
I (671) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (681) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT, status 0
请问我该如何修改呢?
0
|
|
|
|