From 7bdbe8291407ed22802b63707fbfe49d09651bf9 Mon Sep 17 00:00:00 2001 From: Lurkars Date: Sat, 11 Jul 2020 17:33:47 +0200 Subject: [PATCH] fixed scanning (forgot flag, dueto google not sending it) --- README.md | 6 ++++++ main/ena-bluetooth-scan.c | 22 ++++++++++++++++++---- main/ena.c | 17 ++++++++--------- main/main.c | 15 ++++++++------- partitions.csv | 2 +- 5 files changed, 41 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 0ee4ff4..84a1461 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,12 @@ recommended ### Build and Flash +May flash partition table: + +``` +idf.py partition_table-flash +``` + Build the project and flash it to the board, then run monitor tool to view serial output: ``` diff --git a/main/ena-bluetooth-scan.c b/main/ena-bluetooth-scan.c index 35cf167..ff8f2e8 100644 --- a/main/ena-bluetooth-scan.c +++ b/main/ena-bluetooth-scan.c @@ -34,19 +34,33 @@ void ena_bluetooth_scan_event_callback(esp_gap_ble_cb_event_t event, esp_ble_gap case ESP_GAP_BLE_SCAN_RESULT_EVT: if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) { - // check for ENA Service UUID: - if (p->scan_rst.ble_adv[0] == 0x3 && p->scan_rst.ble_adv[1] == 0x3 && p->scan_rst.ble_adv[2] == 0x6f && p->scan_rst.ble_adv[3] == 0xfd) + if (p->scan_rst.adv_data_len < 28) + { + // adv_data too short for exposure notification + break; + } + + int flag_offset = 0; + // received payload from Google does not contain flag specified in Bluetooth Specification!? So check for length and then add offset + if (p->scan_rst.adv_data_len == 31) + { + // data contains flag + flag_offset = 3; + } + + // check for ENA Service UUID: (after flag 0x03 0x03 0x6f 0xfd for ENA service) + if (p->scan_rst.ble_adv[0 + flag_offset] == 0x3 && p->scan_rst.ble_adv[1 + flag_offset] == 0x3 && p->scan_rst.ble_adv[2 + flag_offset] == 0x6f && p->scan_rst.ble_adv[3 + flag_offset] == 0xfd) { uint8_t rpi[ENA_KEY_LENGTH] = {0}; for (int i = 0; i < ENA_KEY_LENGTH; i++) { - rpi[i] = p->scan_rst.ble_adv[8 + i]; + rpi[i] = p->scan_rst.ble_adv[i + 8 + flag_offset]; } uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0}; for (int i = 0; i < ENA_AEM_METADATA_LENGTH; i++) { - aem[i] = p->scan_rst.ble_adv[8 + ENA_KEY_LENGTH + i]; + aem[i] = p->scan_rst.ble_adv[i + ENA_KEY_LENGTH + 8 + flag_offset]; } ena_detection((uint32_t)time(NULL), rpi, aem, p->scan_rst.rssi); diff --git a/main/ena.c b/main/ena.c index 113e160..65ba6e9 100644 --- a/main/ena.c +++ b/main/ena.c @@ -30,27 +30,26 @@ void ena_init(void) ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK(nvs_flash_init()); } - // init BLE esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg)); ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE)); + ESP_ERROR_CHECK(esp_bluedroid_init()); ESP_ERROR_CHECK(esp_bluedroid_enable()); + // new bluetooth address nesseccary? + uint8_t bt_address[ESP_BD_ADDR_LEN]; + esp_fill_random(bt_address, ESP_BD_ADDR_LEN); + bt_address[0] |= 0xC0; + + ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(bt_address)); + ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9)); ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9)); ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P9)); ESP_ERROR_CHECK(esp_ble_gap_config_local_privacy(true)); - /* nesseccary? - // new bluetooth address - uint8_t bt_address[ESP_BD_ADDR_LEN]; - esp_fill_random(bt_address, ESP_BD_ADDR_LEN); - bt_address[0] |= 0xC0; - - ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(bt_address)); -*/ // init ENA ena_crypto_init(); diff --git a/main/main.c b/main/main.c index 4c57fac..2abf6be 100644 --- a/main/main.c +++ b/main/main.c @@ -1,14 +1,14 @@ /** * provide bluetooth part of Exposure Notification API v1.2 as defined by Apple/Google - * + * * Source documents: - * + * * https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf - * + * * https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf - * - * - * + * + * + * */ #include @@ -31,7 +31,7 @@ void app_main(void) settimeofday(&tv, NULL); esp_log_level_set(ENA_STORAGE_LOG, ESP_LOG_INFO); - // ena_storage_erase(); + ena_storage_erase(); // only needed on first start! TODO automatically check ena_init(); @@ -42,3 +42,4 @@ void app_main(void) vTaskDelay(1000 / portTICK_PERIOD_MS); } } + diff --git a/partitions.csv b/partitions.csv index fc77773..b2d0479 100644 --- a/partitions.csv +++ b/partitions.csv @@ -3,4 +3,4 @@ nvs, data, nvs, 0x9000, 0x6000, phy_init, data, phy, 0xf000, 0x1000, factory, app, factory, 0x10000, 1M, -ena, data, nvs, 0x110000,0x2C0000, \ No newline at end of file +ena, data, 0xFF, 0x110000,0x2C0000, \ No newline at end of file