Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1072731ybb; Wed, 1 Apr 2020 15:15:28 -0700 (PDT) X-Google-Smtp-Source: APiQypLWczIZGpTC469FApAiQyjYM2kdylFUznOOdv3X4ubX0HEqNiBOPHr75Vsqfs6ZcSjmjD3z X-Received: by 2002:a9d:7590:: with SMTP id s16mr55505otk.250.1585779328747; Wed, 01 Apr 2020 15:15:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585779328; cv=none; d=google.com; s=arc-20160816; b=PldDa8C9ouG8qRjge/88sisUQvaetEi4kjNvLE8k9gVUQ4He469tlU6YUCSjLrNpwp XCLpKNsM/W/WP7QOOYxVWW9tqi/ASK+t1abAGj15a9wkUwU8l/qhzQAr8oelUXL1w1qX GbjaRL4OuFzO2fxcXhSO67M4UcQy6/5jv/oEWL0UKbQG1h0d18fdgUuRPekNj17z0ypK G7LRJFvPhIgif+PMf7cNKrhpS2RacrwQ54XiNhCWcDHP8uLV0kqfc85WxBfVcuK+D0D0 oL6sFQ51F9a9t2Vn2T7lN9vD5Q9Kvxa17S6SntGaVKMKRouPqu3x7wCHXiwkNAdrEFvR TJQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4ImDv9zhDYlZCVg2ESXGVGfLitxeb2BWAs2xQefNJC4=; b=ZE7KgmB/VzT5mRHrP4ElSg07KUumnvWlJP08UxgQJktqHD6+b2gJD+zT1Qqt3i9mMA DmvnRyS2gLm16P46e1915raFv9yJF9g0NyDzrX2X2zns9/kq9YxFF03GTMqkidYZ8x73 CLpUSFNgbSKRFWgMP7Tru4JegfUDYYJR5vMmWH2JVg+5BS+WRgDS+5Ooy//YPy+QLQbO Bl11D34/CHm/EhrqPTFg9DIdXxTH4PA7+upOVfBpO03M8GENBGpU3u1O4U6H0HzjNY21 mpjhN880vUNJv6j5y73EGaaSfkW6rsSrZ59/Mu2459O2DG8GMlg9j1jKoDgbVrvnK95F zPNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Vlowylyf; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 15si1412347oix.96.2020.04.01.15.15.16; Wed, 01 Apr 2020 15:15:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Vlowylyf; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733150AbgDAWOu (ORCPT + 99 others); Wed, 1 Apr 2020 18:14:50 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:40256 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733101AbgDAWOt (ORCPT ); Wed, 1 Apr 2020 18:14:49 -0400 Received: by mail-pl1-f194.google.com with SMTP id h11so537396plk.7 for ; Wed, 01 Apr 2020 15:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4ImDv9zhDYlZCVg2ESXGVGfLitxeb2BWAs2xQefNJC4=; b=VlowylyfuOJGXMvS8/IFhM4NVn1RLE8M3NPWhsXKvPhE8XjHGszd6P81gFwtRwhO3G SfZdUUUm1R3JB3mCN30DXO+soaunEgptYDsJ3fuL9WL3vjZ+sBc6fxNk/5bz+jqS7LDu di0Qq1fbqCOycztRO8sKXxgpGOY5AoVTQgNjg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4ImDv9zhDYlZCVg2ESXGVGfLitxeb2BWAs2xQefNJC4=; b=kQMModYXdUP6VoeW7SNqxFYxYElAAJ5iP7LcpmJWMw/kQ7VwdscHLtE6/NXHCJFuVk /1llcI4w3bSyyKSx/n3i7i2DWqlL9Ja6tCal2xn987ylKfBS2Vxanp+JLn85tVKtqtzV TY8GO9/FPi6cIUlpOZsRL6Mc6e6UyjsCzadWI12D6JB1ed0qYVIx8RJxURdtQIfHqlXc u2hgZyRFwiYGpJ23+PK5jutC30be8iwlKGnEVeuA6XCwfprrCUvJ0pobjK8xpictXZ8G SeMvPzi9ONXRCPemIg04+mDK1vom6PgqZfT6aW9T91ShHoFXAw7CNVkN6Hr3m/Cm13zK hChw== X-Gm-Message-State: AGi0PuawyUxULrVPfJl2UF96fRlDIqagpMYZ3Jeb0tVpwJqrJtGKcS5p dqub6ROGkvlBdRiztFnkjj7vqd9pJ+o= X-Received: by 2002:a17:902:c595:: with SMTP id p21mr20674plx.17.1585779287054; Wed, 01 Apr 2020 15:14:47 -0700 (PDT) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:b7e2:9910:bd78:608d]) by smtp.gmail.com with ESMTPSA id z6sm2394666pjt.42.2020.04.01.15.14.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Apr 2020 15:14:46 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Miao Chou Subject: [PATCH 5/5] Add SDP records retrieval for a unpaired/unconnected device Date: Wed, 1 Apr 2020 15:14:34 -0700 Message-Id: <20200401221434.12185-5-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200401221434.12185-1-sonnysasaka@chromium.org> References: <20200401221434.12185-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Miao Chou This add the support of SDP record inquiry to a device which is not paired or connected previously. --- src/device.c | 130 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 21 deletions(-) diff --git a/src/device.c b/src/device.c index 1b5afc226..dd7c32625 100644 --- a/src/device.c +++ b/src/device.c @@ -270,9 +270,35 @@ static const uint16_t uuid_list[] = { 0 }; +struct pending_sdp_query { + bdaddr_t dev_bdaddr; + DBusConnection *conn; + DBusMessage *msg; +}; + static int device_browse_gatt(struct btd_device *device, DBusMessage *msg); static int device_browse_sdp(struct btd_device *device, DBusMessage *msg); +static struct pending_sdp_query *pending_sdp_query_new(DBusConnection *conn, + DBusMessage *msg, const struct btd_device *dev) +{ + struct pending_sdp_query *query; + + query = g_new0(struct pending_sdp_query, 1); + query->dev_bdaddr = dev->bdaddr; + query->conn = dbus_connection_ref(conn); + query->msg = dbus_message_ref(msg); + + return query; +} + +static void pending_sdp_query_free(struct pending_sdp_query *query) +{ + dbus_connection_unref(query->conn); + dbus_message_unref(query->msg); + g_free(query); +} + static struct bearer_state *get_state(struct btd_device *dev, uint8_t bdaddr_type) { @@ -3008,28 +3034,13 @@ static dbus_bool_t append_record(const sdp_record_t *rec, return TRUE; } -static DBusMessage *get_service_records(DBusConnection *conn, - DBusMessage *msg, void *data) +static dbus_bool_t append_records(DBusMessage *reply, sdp_list_t *seq) { - struct btd_device *dev = data; - sdp_list_t *seq; - DBusMessage *reply; DBusMessageIter rec_array; DBusMessageIter attr_array; - if (!btd_adapter_get_powered(dev->adapter)) - return btd_error_not_ready(msg); - - if (!btd_device_is_connected(dev)) - return btd_error_not_connected(msg); - - reply = dbus_message_new_method_return(msg); - if (!reply) - return btd_error_failed(msg, "Failed to create method reply"); - - /* Load records from storage if there is nothing in cache */ - if (!dev->tmp_records) - return btd_error_failed(msg, "SDP record not found"); + if (!seq) + return FALSE; dbus_message_iter_init_append(reply, &rec_array); @@ -3037,18 +3048,95 @@ static DBusMessage *get_service_records(DBusConnection *conn, "a{q(yuv)}", &attr_array)) return FALSE; - for (seq = dev->tmp_records; seq; seq = seq->next) { + for (; seq; seq = seq->next) { sdp_record_t *rec = (sdp_record_t *) seq->data; if (!rec) continue; if (!append_record(rec, &attr_array)) - return btd_error_failed(msg, - "SDP record attachment failed"); + return FALSE; } if (!dbus_message_iter_close_container(&rec_array, &attr_array)) return FALSE; + return TRUE; +} + +static void get_service_records_cb(struct btd_device *dev, int err, + void *user_data) +{ + struct pending_sdp_query *query = user_data; + DBusMessage *reply; + sdp_list_t *seq = dev->tmp_records; + + if (memcmp(&query->dev_bdaddr, &dev->bdaddr, sizeof(bdaddr_t))) { + reply = btd_error_failed(query->msg, "Device mismatched"); + goto send_reply; + } + + if (!dev->bredr_state.svc_resolved) { + reply = btd_error_not_ready(query->msg); + goto send_reply; + } + + /* Load records from storage if there is nothing in cache */ + if (!seq) { + reply = btd_error_failed(query->msg, "SDP record not found"); + goto send_reply; + } + + reply = dbus_message_new_method_return(query->msg); + if (!reply) { + reply = btd_error_failed(query->msg, "Failed to create method reply"); + goto send_reply; + } + + if (!append_records(reply, dev->tmp_records)) + reply = btd_error_failed(query->msg, "SDP record attachment failed"); + +send_reply: + g_dbus_send_message(query->conn, reply); + pending_sdp_query_free(query); +} + +static DBusMessage *get_service_records(DBusConnection *conn, + DBusMessage *msg, void *data) +{ + struct btd_device *dev = data; + DBusMessage *reply; + struct pending_sdp_query *query = NULL; + sdp_list_t *seq = dev->tmp_records; + + if (!btd_adapter_get_powered(dev->adapter)) + return btd_error_not_ready(msg); + + if (!btd_device_is_connected(dev)) { + /* + * Do a SDP service discovery on the device if the device is not + * previously connected. SDP records will be returned later in + * get_service_records_cb. + */ + query = pending_sdp_query_new(conn, msg, dev); + device_wait_for_svc_complete(dev, get_service_records_cb, + query); + device_browse_sdp(dev, NULL); + return NULL; + } + + if (!dev->bredr_state.svc_resolved) + return btd_error_not_ready(msg); + + /* Load records from storage if there is nothing in cache */ + if (!seq) + return btd_error_failed(msg, "SDP record not found"); + + reply = dbus_message_new_method_return(msg); + if (!reply) + return btd_error_failed(msg, "Failed to create method reply"); + + if (!append_records(reply, seq)) + return btd_error_failed(msg, "SDP record attachment failed"); + return reply; } -- 2.24.1