Count received BLE advertisements over each neighbor lifetime

This commit is contained in:
evgeny committed 2026-10-30 12:45:18 +01:00
1 parent 694c5e021e
commit 77a58797fc
3 files changed
+19 -9

No files matched your search

+6 -2
View File
@@ -29,10 +29,14 @@ frontend передают instance. config.callbacks — наблюдатели;
штатного ETCP принадлежат драйверу. Сырые TX/RX нельзя читать параллельно ETCP.
Таблица в управляющей структуре драйвера, не в instance и не в UI:
64 записи {node_id, UTF-8 name, endpoint, RSSI, seen_us}; срок свежести 30 секунд.
Объявление обновляет адрес и RSSI. По истечении срока запись удаляется.
64 записи {node_id, UTF-8 name, endpoint, RSSI, seen_us, broadcast_received}; срок свежести 30 секунд.
Каждое принятое валидное объявление увеличивает broadcast_received, включая одинаковые повторы.
Обновление имени, адреса и RSSI сохраняет счётчик. По истечении срока запись удаляется;
повторное появление начинает счёт с 1, даже если таймер ещё не очистил просроченный слот.
Desktop формирует видимый список из snapshot общего драйвера. Android получает
тот же C API через android_ble_driver(). node_id/name в эфире не аутентифицированы.
Android показывает счётчик под ником, desktop — в строке соседа как RX.
В discovery TRACE каждый пакет содержит peer_received; DEBUG удаления показывает итоговый received.
Автоматическое подключение проверяется раз в секунду и при появлении соседа:
- только работающий CHAT-сервис, полная разрешённая запись мембера source=0/deleted=0;
+12 -7
View File
@@ -203,7 +203,7 @@ static void broadcast(void* arg, const struct ble_endpoint* endpoint, const uint
struct ble_identity peer = {0};
int result = ble_identity_decode(&peer, bytes, len);
const char* reason = ble_identity_result_name(result);
uint64_t count;
uint64_t count, peer_received = 0;
int rejected = result != BLE_IDENTITY_OK;
d->broadcast_received++;
if (rejected) count = ++d->broadcast_invalid[-result];
@@ -216,13 +216,16 @@ static void broadcast(void* arg, const struct ble_endpoint* endpoint, const uint
}
if (slot >= 0) {
struct ble_neighbor* n = &d->neighbors[slot];
if (n->identity.node_id != peer.node_id) {
int fresh = n->identity.node_id == peer.node_id && now - n->seen_us <= BLE_NEIGHBOR_TTL_US;
peer_received = fresh ? n->broadcast_received + 1 : 1;
if (!fresh) {
DEBUG_INFO(CAT, "BLE neighbor discovered peer=%016llx name=%s endpoint=%s rssi=%d",
(unsigned long long)peer.node_id, peer.name, endpoint->id, rssi);
d->retry[slot] = 0; d->check_groups = 1;
}
if (n->identity.node_id != peer.node_id || strcmp(n->identity.name, peer.name)) d->peers_changed = 1;
*n = (struct ble_neighbor){peer, *endpoint, rssi, now}; schedule(d);
if (!fresh || strcmp(n->identity.name, peer.name)) d->peers_changed = 1;
*n = (struct ble_neighbor){.identity = peer, .endpoint = *endpoint, .rssi = rssi,
.seen_us = now, .broadcast_received = peer_received}; schedule(d);
reason = "accepted"; count = ++d->broadcast_accepted;
} else { reason = "neighbor-table-full"; count = ++d->broadcast_full; rejected = 1; }
}
@@ -230,10 +233,11 @@ static void broadcast(void* arg, const struct ble_endpoint* endpoint, const uint
if (count <= 3 || debug_should_output(DEBUG_LEVEL_TRACE, CAT)) {
char detail[384];
snprintf(detail, sizeof(detail),
"BLE discovery packet endpoint=%s rssi=%d bytes=%zu header=%02x%02x version=%d expected=%u peer=%016llx reason=%s count=%llu",
"BLE discovery packet endpoint=%s rssi=%d bytes=%zu header=%02x%02x version=%d expected=%u "
"peer=%016llx reason=%s count=%llu peer_received=%llu",
endpoint->id, rssi, len, bytes && len ? bytes[0] : 0, bytes && len > 1 ? bytes[1] : 0,
bytes && len > 2 ? bytes[2] : -1, BLE_IDENTITY_VERSION, (unsigned long long)peer.node_id,
reason, (unsigned long long)count);
reason, (unsigned long long)count, (unsigned long long)peer_received);
if (count > 3) DEBUG_TRACE(CAT, "%s", detail);
else if (rejected) DEBUG_WARN(CAT, "%s", detail);
else DEBUG_DEBUG(CAT, "%s", detail);
@@ -542,7 +546,8 @@ static void tick(void* arg) {
for (int i = 0; i < BLE_NEIGHBORS_MAX; i++) {
struct ble_neighbor* n = &d->neighbors[i];
if (n->identity.node_id && now - n->seen_us > BLE_NEIGHBOR_TTL_US) {
DEBUG_DEBUG(CAT, "BLE neighbor expired peer=%016llx", (unsigned long long)n->identity.node_id);
DEBUG_DEBUG(CAT, "BLE neighbor expired peer=%016llx received=%llu age_us=%llu", (unsigned long long)n->identity.node_id,
(unsigned long long)n->broadcast_received, (unsigned long long)(now - n->seen_us));
memset(n, 0, sizeof(*n)); d->retry[i] = 0; d->peers_changed = 1;
}
}
+1
View File
@@ -22,6 +22,7 @@ struct ble_neighbor {
struct ble_endpoint endpoint; /* Адрес для исходящего GATT; обновляется каждым объявлением. */
int rssi; /* dBm, 127 — неизвестно. */
uint64_t seen_us; /* Монотонное время последнего объявления. */
uint64_t broadcast_received; /* Принятые объявления за жизнь свежей записи, включая одинаковые повторы. */
};
/* config копируется, instance заимствован. NULL при ошибке. TX для ETCP ограничен одним пакетом. */
struct ble_etcp* ble_etcp_create(struct UASYNC* ua, struct UTUN_INSTANCE* instance, const struct ble_config* config);