Wait for resolved BlueZ services before opening cached GATT characteristics
This commit is contained in:
1 parent
7af631141b
commit
9b333b031e
2 files changed
+38
-12
No files matched your search
+21
-8
@@ -29,7 +29,7 @@ struct bluez_link {
|
||||
ble_link_id id;
|
||||
char device[BLE_ENDPOINT_MAX], rx[BLE_ENDPOINT_MAX], tx[BLE_ENDPOINT_MAX], invite[BLE_ENDPOINT_MAX];
|
||||
sd_bus_slot *connect, *query, *subscribe, *write;
|
||||
int fd[BLE_CHANNEL_COUNT], outgoing, opened, sending, buffered, subscribed, channel;
|
||||
int fd[BLE_CHANNEL_COUNT], outgoing, opened, sending, buffered, subscribed, channel, connected, resolved, mtu_known;
|
||||
void* io[BLE_CHANNEL_COUNT];
|
||||
unsigned int mtu;
|
||||
uint8_t value[BLE_VALUE_MAX];
|
||||
@@ -325,8 +325,8 @@ static int subscribed(sd_bus_message* m, void* arg, sd_bus_error* error) {
|
||||
}
|
||||
static int properties(struct ble_backend* b, const char* path, const char* interface, sd_bus_message* m) {
|
||||
const char *key, *type, *uuid = NULL;
|
||||
int r, resolved = 0, disconnected = 0, rssi = 127, have_data = 0;
|
||||
uint16_t mtu = 23;
|
||||
int r, resolved = -1, connected = -1, rssi = 127, have_data = 0;
|
||||
uint16_t mtu = 0;
|
||||
uint8_t data[BLE_BROADCAST_MAX]; size_t data_size = 0;
|
||||
if ((r = sd_bus_message_enter_container(m, 'a', "{sv}")) < 0) return r;
|
||||
while ((r = sd_bus_message_enter_container(m, 'e', "sv")) > 0) {
|
||||
@@ -351,7 +351,7 @@ static int properties(struct ble_backend* b, const char* path, const char* inter
|
||||
int16_t value; r = sd_bus_message_read(m, "n", &value); rssi = value;
|
||||
} else if (!strcmp(interface, DEVICE_IF) && !strcmp(type, "b") && (!strcmp(key, "Connected") || !strcmp(key, "ServicesResolved"))) {
|
||||
int value; r = sd_bus_message_read(m, "b", &value);
|
||||
if (!strcmp(key, "Connected")) disconnected = !value; else resolved = value;
|
||||
if (!strcmp(key, "Connected")) connected = value; else resolved = value;
|
||||
} else if (!strcmp(interface, CHAR_IF) && !strcmp(key, "UUID") && !strcmp(type, "s")) r = sd_bus_message_read(m, "s", &uuid);
|
||||
else if (!strcmp(interface, CHAR_IF) && !strcmp(key, "MTU") && !strcmp(type, "q")) r = sd_bus_message_read(m, "q", &mtu);
|
||||
else if (!strcmp(interface, CHAR_IF) && !strcmp(key, "Value") && !strcmp(type, "ay")) {
|
||||
@@ -373,8 +373,15 @@ static int properties(struct ble_backend* b, const char* path, const char* inter
|
||||
}
|
||||
if (!strcmp(interface, DEVICE_IF)) {
|
||||
struct bluez_link* l = find_device(b, path);
|
||||
if (l && disconnected) link_error(l, "device disconnected");
|
||||
else if (l && resolved && l->outgoing && !l->opened && !l->query) query_link(l);
|
||||
if (l && ((connected == 0 && l->connected) || (resolved == 0 && l->resolved)))
|
||||
link_error(l, connected == 0 ? "device disconnected" : "GATT services invalidated");
|
||||
else if (l && l->outgoing) {
|
||||
if (connected >= 0) l->connected = connected;
|
||||
if (resolved >= 0) l->resolved = resolved;
|
||||
DEBUG_DEBUG(CAT, "BLE BlueZ readiness link=%llu connected=%d resolved=%d opened=%d",
|
||||
(unsigned long long)l->id, l->connected, l->resolved, l->opened);
|
||||
if (l->connected && l->resolved && !l->opened && !l->query) query_link(l);
|
||||
}
|
||||
if (have_data && strlen(path) < BLE_ENDPOINT_MAX) {
|
||||
struct ble_endpoint ep = {0}; strcpy(ep.id, path); ble_backend_broadcast(b->owner, &ep, data, data_size, rssi);
|
||||
}
|
||||
@@ -382,7 +389,10 @@ static int properties(struct ble_backend* b, const char* path, const char* inter
|
||||
if (uuid) for (int i = 0; i < BLE_LINKS_MAX; i++) {
|
||||
struct bluez_link* l = &b->links[i]; size_t prefix = strlen(l->device);
|
||||
if (!l->id || !l->outgoing || l->opened || strncmp(path, l->device, prefix) || path[prefix] != '/' || strlen(path) >= BLE_ENDPOINT_MAX) continue;
|
||||
if (!strcmp(uuid, BLE_RX_UUID)) { strcpy(l->rx, path); if (mtu >= 23) l->mtu = mtu; }
|
||||
if (!strcmp(uuid, BLE_RX_UUID)) {
|
||||
strcpy(l->rx, path);
|
||||
if (mtu >= 23) { l->mtu = mtu; l->mtu_known = 1; }
|
||||
}
|
||||
if (!strcmp(uuid, BLE_TX_UUID)) strcpy(l->tx, path);
|
||||
if (!strcmp(uuid, BLE_INVITE_UUID)) strcpy(l->invite, path);
|
||||
}
|
||||
@@ -415,7 +425,10 @@ static int queried(sd_bus_message* m, void* arg, sd_bus_error* error) {
|
||||
l->query = sd_bus_slot_unref(l->query);
|
||||
if (!l->id) return 0;
|
||||
if (r < 0) { link_error(l, "service discovery failed"); return 0; }
|
||||
if (l->rx[0] && l->tx[0] && l->invite[0] && !l->subscribe && !l->opened) {
|
||||
DEBUG_DEBUG(CAT, "BLE BlueZ GATT snapshot link=%llu connected=%d resolved=%d RX=%d TX=%d INVITE=%d mtu=%u known=%d",
|
||||
(unsigned long long)l->id, l->connected, l->resolved, !!l->rx[0], !!l->tx[0], !!l->invite[0], l->mtu, l->mtu_known);
|
||||
if (l->connected && l->resolved && l->rx[0] && l->tx[0] && l->invite[0] && !l->subscribe && !l->opened) {
|
||||
if (!l->mtu_known) DEBUG_WARN(CAT, "BLE BlueZ MTU unavailable link=%llu: using ATT minimum 23", (unsigned long long)l->id);
|
||||
r = sd_bus_call_method_async(l->backend->bus, &l->subscribe, "org.bluez", l->tx, CHAR_IF, "StartNotify", subscribed, l, "");
|
||||
if (r < 0) link_error(l, "StartNotify request failed");
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ static char application[256];
|
||||
static int ready, broadcasts, ready_links, advertisement_count;
|
||||
static ble_link_id links[4];
|
||||
static unsigned commands, requests;
|
||||
static int remote_resolved;
|
||||
struct reply { sd_bus_slot* slot; int done, fd, failed; };
|
||||
static void poll_all(void) {
|
||||
for (int i = 0; i < 500; i++) { CHECK(sd_bus_process(bus, NULL) >= 0); uasync_poll(ua, 10); }
|
||||
@@ -53,7 +54,7 @@ static const sd_bus_vtable adv_vtable[] = {
|
||||
static int remote_property(sd_bus* b, const char* path, const char* i, const char* name,
|
||||
sd_bus_message* m, void* arg, sd_bus_error* e) {
|
||||
(void)b; (void)i; (void)arg; (void)e;
|
||||
if (!strcmp(name, "MTU")) return sd_bus_message_append(m, "q", 23);
|
||||
if (!strcmp(name, "MTU")) return sd_bus_message_append(m, "q", remote_resolved ? 517 : 23);
|
||||
const char* uuid = strstr(path, "/rx") ? BLE_RX_UUID : strstr(path, "/tx") ? BLE_TX_UUID : BLE_INVITE_UUID;
|
||||
return sd_bus_message_append(m, "s", uuid);
|
||||
}
|
||||
@@ -79,8 +80,15 @@ static int remote_write(sd_bus_message* m, void* arg, sd_bus_error* e) {
|
||||
}
|
||||
return r;
|
||||
}
|
||||
static int remote_state(sd_bus* b, const char* path, const char* i, const char* name,
|
||||
sd_bus_message* m, void* arg, sd_bus_error* e) {
|
||||
(void)b; (void)path; (void)i; (void)arg; (void)e;
|
||||
return sd_bus_message_append(m, "b", !strcmp(name, "Connected") || remote_resolved);
|
||||
}
|
||||
static const sd_bus_vtable device_vtable[] = {
|
||||
SD_BUS_VTABLE_START(0), SD_BUS_METHOD("Connect", "", "", simple, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END
|
||||
SD_BUS_VTABLE_START(0), SD_BUS_METHOD("Connect", "", "", simple, SD_BUS_VTABLE_UNPRIVILEGED),
|
||||
SD_BUS_PROPERTY("Connected", "b", remote_state, 0, 0), SD_BUS_PROPERTY("ServicesResolved", "b", remote_state, 0, 0),
|
||||
SD_BUS_VTABLE_END
|
||||
};
|
||||
static const sd_bus_vtable remote_vtable[] = {
|
||||
SD_BUS_VTABLE_START(0), SD_BUS_PROPERTY("UUID", "s", remote_property, 0, SD_BUS_VTABLE_PROPERTY_CONST),
|
||||
@@ -201,10 +209,15 @@ int main(void) {
|
||||
close(fd1); poll_all(); close(fd2); poll_all();
|
||||
/* Настоящий клиент backend: HELLO/DATA используют command, INVITE использует request. */
|
||||
struct ble_endpoint ep = {REMOTE};
|
||||
CHECK(ble_connect(transport, &ep, 3)); poll_all(); CHECK(ready_links == 3 && commands == 1);
|
||||
CHECK(ble_connect(transport, &ep, 3)); poll_all();
|
||||
/* Кеш UUID доступен сразу, но до ServicesResolved нельзя объявлять GATT готовым с прежним MTU23. */
|
||||
CHECK(ready_links == 2 && commands == 0);
|
||||
remote_resolved = 1;
|
||||
CHECK(sd_bus_emit_properties_changed(bus, REMOTE, "org.bluez.Device1", "ServicesResolved", NULL) >= 0);
|
||||
poll_all(); CHECK(ready_links == 3 && commands == 1);
|
||||
CHECK(!ble_tx_put(transport, links[3], frame(0xe5)));
|
||||
CHECK(!ble_channel_tx_put(transport, links[3], BLE_CHANNEL_INVITE, frame(0xe6)));
|
||||
poll_all(); CHECK(commands == 4 && requests == 3);
|
||||
poll_all(); CHECK(commands == 2 && requests == 1); /* После resolve используется актуальный MTU517. */
|
||||
ble_destroy(transport); uasync_destroy(ua, 0);
|
||||
for (int i = 0; i < 4; i++) sd_bus_slot_unref(remote[i]);
|
||||
sd_bus_slot_unref(manager); sd_bus_slot_unref(adapter); sd_bus_slot_unref(gatt); sd_bus_slot_unref(adv); sd_bus_close_unref(bus);
|
||||
|
||||
Reference in new issue
Block a user