static void register_notify(void *data, void *user_data) { struct notify_client *notify_client = data; struct btd_gatt_client *client = user_data; struct async_dbus_op *op; DBG("Re-register subscribed notification client"); op = new0(struct async_dbus_op, 1); op->data = notify_client; notify_client->notify_id = bt_gatt_client_register_notify(client->gatt, notify_client->chrc->value_handle, register_notify_cb, notify_cb, op, async_dbus_op_free); if (notify_client->notify_id) return; async_dbus_op_free(op); DBG("Failed to re-register notification client"); queue_remove(notify_client->chrc->notify_clients, client); queue_remove(client->all_notify_clients, client); notify_client_free(notify_client); }
static void cmd_register_notify(struct client *cli, char *cmd_str) { char *argv[2]; int argc = 0; uint16_t value_handle; char *endptr = NULL; if (!bt_gatt_client_is_ready(cli->gatt)) { printf("GATT client not initialized\n"); return; } if (!parse_args(cmd_str, 1, argv, &argc) || argc != 1) { register_notify_usage(); return; } value_handle = strtol(argv[0], &endptr, 16); if (!endptr || *endptr != '\0' || !value_handle) { printf("Invalid value handle: %s\n", argv[0]); return; } if (!bt_gatt_client_register_notify(cli->gatt, value_handle, register_notify_cb, notify_cb, NULL, NULL)) printf("Failed to register notify handler\n"); printf("\n"); }
static DBusMessage *characteristic_start_notify(DBusConnection *conn, DBusMessage *msg, void *user_data) { struct characteristic *chrc = user_data; struct bt_gatt_client *gatt = chrc->service->client->gatt; const char *sender = dbus_message_get_sender(msg); struct async_dbus_op *op; struct notify_client *client; if (!(chrc->props & BT_GATT_CHRC_PROP_NOTIFY || chrc->props & BT_GATT_CHRC_PROP_INDICATE)) return btd_error_not_supported(msg); /* Each client can only have one active notify session. */ client = queue_find(chrc->notify_clients, match_notify_sender, sender); if (client) return client->notify_id ? btd_error_failed(msg, "Already notifying") : btd_error_in_progress(msg); client = notify_client_create(chrc, sender); if (!client) return btd_error_failed(msg, "Failed allocate notify session"); queue_push_tail(chrc->notify_clients, client); queue_push_tail(chrc->service->client->all_notify_clients, client); /* * If the device is currently not connected, return success. We will * automatically try and register all clients when a GATT client becomes * ready. */ if (!gatt) { DBusMessage *reply; reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID); if (reply) return reply; /* * Clean up and respond with an error instead of timing out to * avoid any ambiguities. */ error("Failed to construct D-Bus message reply"); goto fail; } op = new0(struct async_dbus_op, 1); op->data = client; op->msg = dbus_message_ref(msg); client->notify_id = bt_gatt_client_register_notify(gatt, chrc->value_handle, register_notify_cb, notify_cb, op, async_dbus_op_free); if (client->notify_id) return NULL; async_dbus_op_free(op); fail: queue_remove(chrc->notify_clients, client); queue_remove(chrc->service->client->all_notify_clients, client); /* Directly free the client */ notify_client_free(client); return btd_error_failed(msg, "Failed to register notify session"); }