From e62749bf0361647c613df218e5384d5a03d7fc5c Mon Sep 17 00:00:00 2001 From: evgeny Date: Fri, 30 Oct 2026 17:54:14 +0100 Subject: [PATCH] Cover initial BLE publication and preserve radio diagnostics --- lib/ble/tests/ble_advertising_probe.cpp | 22 +++++++------- lib/ble/tests/test_ble_bluez.c | 2 ++ tools/chatgui/tests/test_desktop_ble.cpp | 38 ++++++++++++++++++++---- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/lib/ble/tests/ble_advertising_probe.cpp b/lib/ble/tests/ble_advertising_probe.cpp index e4c67654a..bbf662a67 100644 --- a/lib/ble/tests/ble_advertising_probe.cpp +++ b/lib/ble/tests/ble_advertising_probe.cpp @@ -116,15 +116,18 @@ int main(int argc, char** argv) { std::shared_ptr early; bool completed = false; try { - if (publisher_first) { - /* Тот же publish(), но GATT ещё даже не создан; отдельное состояние живёт до конца проверки. */ + /* Изолированный publisher исключает автоматическую перестановку GATT в рабочем backend. */ + auto start_publisher = [&] { early = std::make_shared(); early->ua = ua; early->owner = &owner; early->company = config.company_id; init_apartment(apartment_type::multi_threaded); early->apartment = true; int size = ble_identity_encode(early->broadcast.data(), config.node_id, "probe18"); if (size < 0) throw std::runtime_error("cannot encode manufacturer identity"); early->broadcast_size = size; early->broadcast_enabled = true; - DEBUG_INFO(CAT, "BLE advertising probe mode=publisher-first; starting publisher before creating GATT/scan"); early->publish(); + }; + if (publisher_first) { + DEBUG_INFO(CAT, "BLE advertising probe mode=publisher-first; starting publisher before creating GATT/scan"); + start_publisher(); auto start = get_time_us(), deadline = start + 20000000; while (!owner.failed && early->publisher_status != int(BluetoothLEAdvertisementPublisherStatus::Started) && get_time_us() < deadline) uasync_poll(ua, 100); @@ -157,22 +160,21 @@ int main(int argc, char** argv) { if (!observe(s, *early, owner, *gatt, "publisher_first_gatt_on", seconds)) throw std::runtime_error("publisher-first observation incomplete"); } else { - uint8_t payload[BLE_BROADCAST_MAX]; - int size = ble_identity_encode(payload, config.node_id, "probe18"); - if (size < 0 || ble_backend_advertise(backend, payload, size) != BLE_OK) - throw std::runtime_error("manufacturer publisher startup failed"); - if (!observe(s, s, owner, *gatt, "gatt_on", seconds)) + start_publisher(); + if (!observe(s, *early, owner, *gatt, "gatt_on", seconds)) throw std::runtime_error("baseline incomplete"); DEBUG_INFO(CAT, "BLE advertising probe stopping GATT advertisement; publisher and scan remain unchanged"); gatt->requested = false; gatt->stop_confirmed = false; + s.service_requested = false; s.provider.StopAdvertising(); - if (!observe(s, s, owner, *gatt, "gatt_off", seconds)) + if (!observe(s, *early, owner, *gatt, "gatt_off", seconds)) throw std::runtime_error("GATT-off observation incomplete"); DEBUG_INFO(CAT, "BLE advertising probe restoring GATT advertisement connectable=1 discoverable=1"); GattServiceProviderAdvertisingParameters parameters; parameters.IsConnectable(true); parameters.IsDiscoverable(true); gatt->requested = true; gatt->stop_confirmed = false; + s.service_requested = true; s.provider.StartAdvertising(parameters); - if (!observe(s, s, owner, *gatt, "gatt_restored", seconds)) + if (!observe(s, *early, owner, *gatt, "gatt_restored", seconds)) throw std::runtime_error("GATT-restored observation incomplete"); } completed = true; diff --git a/lib/ble/tests/test_ble_bluez.c b/lib/ble/tests/test_ble_bluez.c index 22e8e18e7..cb16909e7 100644 --- a/lib/ble/tests/test_ble_bluez.c +++ b/lib/ble/tests/test_ble_bluez.c @@ -178,8 +178,10 @@ int main(void) { CHECK(sd_bus_add_object_vtable(bus, &remote[i+1], paths[i], "org.bluez.GattCharacteristic1", remote_vtable, NULL) >= 0); ua = uasync_create(); CHECK(ua); struct ble_config config = {0}; config.node_id = 200; config.company_id = 0xffff; + memcpy(config.initial_advertisement, "abc", 3); config.initial_advertisement_size = 3; config.callbacks.status = status; config.callbacks.connection = connection; config.callbacks.broadcast = broadcast; transport = ble_create(ua, &config); CHECK(transport && !ble_start(transport)); poll_all(); CHECK(ready == 1 && broadcasts == 0); + CHECK(advertisement_count == 2); /* Сервис и начальное manufacturer-объявление. */ CHECK(!ble_advertise(transport, "abc", 3)); poll_all(); CHECK(advertisement_count == 2); CHECK(!ble_advertise(transport, "def", 3)); poll_all(); CHECK(advertisement_count == 3); announce(); diff --git a/tools/chatgui/tests/test_desktop_ble.cpp b/tools/chatgui/tests/test_desktop_ble.cpp index 8fb70c2cd..e62b965f1 100644 --- a/tools/chatgui/tests/test_desktop_ble.cpp +++ b/tools/chatgui/tests/test_desktop_ble.cpp @@ -10,12 +10,15 @@ struct ble_backend { ble_transport* owner = nullptr; }; static ble_backend backend; static int creations, destructions, advertisements; -static QByteArray advertised; -extern "C" ble_backend* ble_backend_create(ble_transport* owner, UASYNC*, const ble_config*) { +static QByteArray advertised, initial; +static bool rejectAdvertisement; +extern "C" ble_backend* ble_backend_create(ble_transport* owner, UASYNC*, const ble_config* config) { + initial = QByteArray(reinterpret_cast(config->initial_advertisement), int(config->initial_advertisement_size)); backend.owner = owner; ++creations; return &backend; } extern "C" void ble_backend_destroy(ble_backend*) { backend.owner = nullptr; ++destructions; } extern "C" int ble_backend_advertise(ble_backend*, const uint8_t* data, size_t len) { + if (rejectAdvertisement) return BLE_ERROR; advertised = QByteArray(reinterpret_cast(data), int(len)); ++advertisements; return 0; } extern "C" int ble_backend_connect(ble_backend*, ble_link_id, const ble_endpoint*) { return BLE_ERROR; } @@ -33,14 +36,17 @@ private slots: radio.apply(0, "Desktop"); QVERIFY(!radio.transport()); QCOMPARE(creations, 0); radio.apply(3, "Desktop"); QVERIFY(radio.transport()); QCOMPARE(creations, 1); QCOMPARE(radio.snapshot().state, BLE_STARTING); QCOMPARE(advertisements, 0); + ble_identity startup{}; + QCOMPARE(ble_identity_decode(&startup, reinterpret_cast(initial.constData()), initial.size()), 0); + QCOMPARE(startup.node_id, uint64_t(1)); QCOMPARE(QByteArray(startup.name), QByteArray("Desktop")); ble_endpoint endpoint{}; strcpy(endpoint.id, "peer"); QCOMPARE(ble_backend_incoming(backend.owner, &endpoint), ble_link_id(0)); - ble_backend_status(backend.owner, 1, "ready"); QCOMPARE(advertisements, 1); + ble_backend_status(backend.owner, 1, "ready"); QCOMPARE(advertisements, 2); ble_identity identity{}; QCOMPARE(ble_identity_decode(&identity, reinterpret_cast(advertised.constData()), advertised.size()), 0); QCOMPARE(identity.node_id, uint64_t(1)); QCOMPARE(QByteArray(identity.name), QByteArray("Desktop")); - radio.apply(3, "Desktop"); QCOMPARE(advertisements, 1); - radio.apply(7, "Renamed"); QCOMPARE(advertisements, 2); + radio.apply(3, "Desktop"); QCOMPARE(advertisements, 2); + radio.apply(7, "Renamed"); QCOMPARE(advertisements, 3); QVERIFY(ble_backend_incoming(backend.owner, &endpoint) != 0); uint8_t bytes[24]; int length = ble_identity_encode(bytes, 2, "Android"); ble_backend_broadcast(backend.owner, &endpoint, bytes, length, -42); @@ -67,6 +73,28 @@ private slots: QCOMPARE(radio.snapshot().reason, QString("adapter is off")); radio.apply(0, "Renamed"); QCOMPARE(destructions, 2); } + void startupUpdatesAndFailure() { + auto destroy = [](UASYNC* ua) { uasync_destroy(ua, 0); }; + std::unique_ptr ua(uasync_create(), destroy); QVERIFY(ua); + DesktopBle radio(ua.get(), 1); + radio.apply(7, "Initial"); QVERIFY(!initial.isEmpty()); + radio.apply(7, "Latest"); QCOMPARE(radio.snapshot().state, BLE_STARTING); + ble_backend_status(backend.owner, 1, "ready"); + ble_identity identity{}; + QCOMPARE(ble_identity_decode(&identity, reinterpret_cast(advertised.constData()), advertised.size()), 0); + QCOMPARE(QByteArray(identity.name), QByteArray("Latest")); + radio.apply(0, "Latest"); + radio.apply(7, "Initial"); radio.apply(5, "Initial"); + ble_backend_status(backend.owner, 1, "ready"); QVERIFY(advertised.isEmpty()); + radio.apply(0, "Initial"); + radio.apply(7, "Initial"); rejectAdvertisement = true; + ble_backend_status(backend.owner, 1, "ready"); rejectAdvertisement = false; + QCOMPARE(radio.snapshot().state, BLE_CLOSED); + QCOMPARE(radio.snapshot().reason, QString("initial BLE advertisement failed")); + radio.apply(0, "Initial"); + radio.apply(5, "Initial"); QVERIFY(initial.isEmpty()); + ble_backend_status(backend.owner, 1, "ready"); QCOMPARE(radio.snapshot().state, BLE_READY); + } }; QTEST_GUILESS_MAIN(DesktopBleTest) #include "test_desktop_ble.moc"