diff --git a/lib/ble/tests/test_ble_transport.c b/lib/ble/tests/test_ble_transport.c index e34d4894e..3e266a856 100644 --- a/lib/ble/tests/test_ble_transport.c +++ b/lib/ble/tests/test_ble_transport.c @@ -9,13 +9,14 @@ #define CHECK(v) do { if (!(v)) { fprintf(stderr, "FAIL line=%d: %s\n", __LINE__, #v); exit(1); } } while (0) struct fake_link { ble_link_id id, other; struct ble_backend* peer; uint8_t data[512]; size_t len; int pending; - enum ble_wire_channel channel; uint64_t ack_at; uint16_t ack_id, ack_delivered; int ack_held; + enum ble_wire_channel channel; uint64_t ack_at, complete_at; uint16_t ack_id, ack_delivered; int ack_held, delivered; }; struct ble_backend { struct ble_transport* owner; struct UASYNC* ua; void* task; int index, started; struct fake_link links[16]; }; static struct ble_backend* backends[3]; static unsigned int mtu = 23; static int hold, drop, reject_peer, ready_count, writable_count; static int hold_ack; +static unsigned ack_delay_us; static unsigned ack_timeouts; static unsigned closed_count; static uint64_t data_wire_bytes[3]; @@ -58,7 +59,9 @@ int ble_backend_send(struct ble_backend* b, ble_link_id id, enum ble_wire_channe CHECK(!f->ack_at || now - f->ack_at >= BLE_ACK_INTERVAL_US); f->ack_at = now; f->ack_id = (uint16_t)((data[0] << 8) | data[1]); } - f->channel = channel; memcpy(f->data, data, len); f->len = len; f->pending = 1; schedule(b); return 0; + f->channel = channel; memcpy(f->data, data, len); f->len = len; f->pending = 1; f->delivered = 0; + f->complete_at = channel == BLE_WIRE_ACK ? get_time_us() + ack_delay_us : 0; + schedule(b); return 0; } static void run(void* arg) { struct ble_backend* b = arg; b->task = NULL; @@ -75,14 +78,19 @@ static void run(void* arg) { if (target && !target->len) { schedule(b); continue; } /* Удалённая подписка должна завершиться до доставки HELLO. */ ble_link_id id = f->id, other = f->other; struct ble_backend* peer = f->peer; - uint8_t data[512]; size_t len = f->len; memcpy(data, f->data, len); f->pending = 0; - int dropped = f->channel != BLE_WIRE_ACK && data[f->channel == BLE_WIRE_DATA ? 2 : 0] == 3 && drop && --drop == 0; - if (hold_ack && f->channel == BLE_WIRE_ACK) f->ack_held = 1; - else if (!dropped && peer && slot(peer, other)) { - if (f->channel == BLE_WIRE_ACK) f->ack_delivered = f->ack_id; - ble_backend_received(peer->owner, other, f->channel, data, len); + if (!f->delivered) { + uint8_t data[512]; size_t len = f->len; memcpy(data, f->data, len); f->delivered = 1; + int dropped = f->channel != BLE_WIRE_ACK && data[f->channel == BLE_WIRE_DATA ? 2 : 0] == 3 && drop && --drop == 0; + if (hold_ack && f->channel == BLE_WIRE_ACK) f->ack_held = 1; + else if (!dropped && peer && slot(peer, other)) { + if (f->channel == BLE_WIRE_ACK) f->ack_delivered = f->ack_id; + ble_backend_received(peer->owner, other, f->channel, data, len); + } + } + if (slot(b, id)) { + if (get_time_us() < f->complete_at) { schedule(b); continue; } + f->pending = 0; ble_backend_sent(b->owner, id, 1); } - if (slot(b, id)) ble_backend_sent(b->owner, id, 1); } } static int validate(void* arg, ble_link_id id, uint64_t node) { (void)arg; (void)id; (void)node; return !reject_peer; } @@ -100,6 +108,32 @@ static struct ll_entry* frame(unsigned int size, uint8_t value) { struct ll_entry* e = ll_alloc_lldgram(size); CHECK(e); e->len = size; memset(e->dgram, value, size); return e; } static void release(struct ll_entry* e) { queue_dgram_free(e); queue_entry_free(e); } +/* Непрерывный RX при ACK completion >15ms не должен вытеснять встречный DATA. */ +static void exercise_ack_fairness(void) { + mtu = 517; hold = drop = reject_peer = hold_ack = 0; ack_delay_us = 0; + struct UASYNC* ua = uasync_create(); CHECK(ua); + struct ble_config cfg = {.node_id = 1}; struct ble_transport* a = ble_create(ua, &cfg); + cfg.node_id = 2; struct ble_transport* b = ble_create(ua, &cfg); CHECK(a && b && !ble_start(a) && !ble_start(b)); drain(ua); + struct ble_endpoint ep = {"1"}; ble_link_id id = ble_connect(a, &ep, 2); CHECK(id); drain(ua); + ble_link_id other = incoming_ids[1]; ack_delay_us = 20000; + int queued = 0, replied = 0; unsigned received = 0; + uint64_t started = get_time_us(); + while (!replied && get_time_us() - started < 250000) { + while (ble_tx_ready(a, id)) CHECK(!ble_tx_put(a, id, frame(500, 0x31))); + uasync_poll(ua, 0); + struct fake_link* receiver = slot(backends[1], other); CHECK(receiver); + if (!queued && receiver->pending && receiver->channel == BLE_WIRE_ACK) { + CHECK(!ble_tx_put(b, other, frame(1, 0xa7))); queued = 1; + } + struct ll_entry* e; + while ((e = ble_rx_get(b, other))) { received++; release(e); } + while ((e = ble_rx_get(a, id))) { CHECK(e->len == 1 && e->dgram[0] == 0xa7); replied++; release(e); } + } + CHECK(queued && replied == 1 && received > 1); + printf("BLE ACK fairness PASS (completion=20ms, continuous RX, DATA returned in %lluus)\n", + (unsigned long long)(get_time_us() - started)); + ble_destroy(a); ble_destroy(b); uasync_destroy(ua, 0); ack_delay_us = 0; +} static void exercise(unsigned int test_mtu) { mtu = test_mtu; hold = drop = reject_peer = ready_count = writable_count = 0; struct UASYNC* ua = uasync_create(); CHECK(ua); @@ -231,6 +265,7 @@ static void exercise_flow(void) { int main(void) { debug_config_init(); debug_set_level(DEBUG_LEVEL_WARN); size_t before = u_get_allocated_count(); + exercise_ack_fairness(); CHECK(u_get_allocated_count() == before); exercise(23); exercise(247); exercise(517); exercise_flow(); CHECK(u_get_allocated_count() == before); puts("BLE transport tests PASS (MTU 23/247/517, duplex, loss, backpressure, peers, teardown)"); return 0;