// Feather nRF52840 Sense: BLE active-scan detector for crbug.com/562546391. // // Advertises a connectable, scannable peripheral named "BT562546391" and // enables SoftDevice scan request notifications, so every SCAN_REQ sent by a // nearby active scanner raises BLE_GAP_EVT_SCAN_REQ_REPORT. A host running a // BlueZ discovery session scans actively, so the scan request rate is a // radio-level signal of whether the host still holds that session -- visible // on the red LED and on the serial log without trusting any host-side API. // // Advertised manufacturer data (company 0xFFFF): seq, scan requests in the // last second, total scan requests; all little-endian uint16. #include namespace { constexpr uint16_t kCompanyId = 0xFFFF; constexpr char kDeviceName[] = "BT562546391"; constexpr uint32_t kReportIntervalMs = 1000; // 0.625 ms units; fast enough that a 1 s window sees many scan requests. constexpr uint16_t kAdvIntervalUnits = 160; struct ManufacturerPayload { uint16_t company_id; uint16_t seq; uint16_t scan_reqs_last_second; uint16_t scan_reqs_total; } __attribute__((packed)); uint8_t g_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; uint8_t g_adv_buf[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; uint8_t g_adv_len = 0; uint8_t g_scan_rsp_buf[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; uint8_t g_scan_rsp_len = 0; // Offset of ManufacturerPayload inside g_adv_buf, patched in place while // advertising; the SoftDevice reads this buffer directly. uint8_t g_mfg_offset = 0; volatile uint32_t g_scan_req_total = 0; volatile uint32_t g_scan_req_window = 0; volatile uint8_t g_last_scanner[BLE_GAP_ADDR_LEN] = {}; bool AppendField(uint8_t* buf, uint8_t& len, uint8_t type, const void* data, uint8_t data_len) { if (len + 2 + data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX) { return false; } buf[len++] = data_len + 1; buf[len++] = type; memcpy(buf + len, data, data_len); len += data_len; return true; } void BuildAdvData() { const uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED | BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE; AppendField(g_adv_buf, g_adv_len, BLE_GAP_AD_TYPE_FLAGS, &flags, 1); AppendField(g_adv_buf, g_adv_len, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, kDeviceName, strlen(kDeviceName)); const ManufacturerPayload payload = {kCompanyId, 0, 0, 0}; g_mfg_offset = g_adv_len + 2; AppendField(g_adv_buf, g_adv_len, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, &payload, sizeof(payload)); const int8_t tx_power = 4; AppendField(g_scan_rsp_buf, g_scan_rsp_len, BLE_GAP_AD_TYPE_TX_POWER_LEVEL, &tx_power, 1); } void UpdateManufacturerPayload(uint16_t seq, uint16_t last_second, uint16_t total) { const ManufacturerPayload payload = {kCompanyId, seq, last_second, total}; memcpy(g_adv_buf + g_mfg_offset, &payload, sizeof(payload)); } ble_gap_adv_data_t GapAdvData() { ble_gap_adv_data_t data = {}; data.adv_data.p_data = g_adv_buf; data.adv_data.len = g_adv_len; data.scan_rsp_data.p_data = g_scan_rsp_buf; data.scan_rsp_data.len = g_scan_rsp_len; return data; } void OnBleEvent(ble_evt_t* evt) { if (evt->header.evt_id != BLE_GAP_EVT_SCAN_REQ_REPORT) { return; } g_scan_req_total++; g_scan_req_window++; const uint8_t* peer = evt->evt.gap_evt.params.scan_req_report.peer_addr.addr; for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++) { g_last_scanner[i] = peer[i]; } } void StartAdvertising() { ble_gap_adv_params_t params = {}; params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED; params.interval = kAdvIntervalUnits; params.duration = 0; params.filter_policy = BLE_GAP_ADV_FP_ANY; params.primary_phy = BLE_GAP_PHY_1MBPS; params.scan_req_notification = 1; ble_gap_adv_data_t data = GapAdvData(); uint32_t err = sd_ble_gap_adv_set_configure(&g_adv_handle, &data, ¶ms); if (err == NRF_SUCCESS) { err = sd_ble_gap_adv_start(g_adv_handle, CONN_CFG_PERIPHERAL); } Serial.printf("advertising start: err=0x%04lX handle=%u\n", err, g_adv_handle); } String FormatLastScanner() { char out[18]; snprintf(out, sizeof(out), "%02X:%02X:%02X:%02X:%02X:%02X", g_last_scanner[5], g_last_scanner[4], g_last_scanner[3], g_last_scanner[2], g_last_scanner[1], g_last_scanner[0]); return String(out); } } // namespace void setup() { Serial.begin(115200); pinMode(LED_RED, OUTPUT); digitalWrite(LED_RED, LOW); Bluefruit.begin(); Bluefruit.setTxPower(4); Bluefruit.setName(kDeviceName); Bluefruit.autoConnLed(false); Bluefruit.setEventCallback(OnBleEvent); BuildAdvData(); StartAdvertising(); Serial.println("feather scan detector for crbug.com/562546391"); Serial.println("red LED on == some host is actively scanning"); } void loop() { static uint32_t next_report_ms = 0; static uint16_t seq = 0; static bool was_scanned = false; if (millis() < next_report_ms) { delay(10); return; } next_report_ms = millis() + kReportIntervalMs; const uint32_t window = g_scan_req_window; g_scan_req_window = 0; const uint32_t total = g_scan_req_total; const bool scanned = window > 0; digitalWrite(LED_RED, scanned ? HIGH : LOW); UpdateManufacturerPayload(++seq, window, total); ble_gap_adv_data_t data = GapAdvData(); sd_ble_gap_adv_set_configure(&g_adv_handle, &data, nullptr); Serial.printf("t=%lus scan_reqs/s=%lu total=%lu scanning=%s", millis() / 1000, window, total, scanned ? "YES" : "no"); if (scanned) { Serial.printf(" scanner=%s", FormatLastScanner().c_str()); } Serial.println(); if (scanned != was_scanned) { Serial.printf(">>> host scanning %s\n", scanned ? "STARTED" : "STOPPED"); was_scanned = scanned; } }