Linux bluetooth development
 help / color / mirror / Atom feed
From: Bernie Innocenti <bernie@codewiz.org>
To: linux-bluetooth@vger.kernel.org
Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	Bernie Innocenti <bernie@codewiz.org>
Subject: [PATCH BlueZ v2 1/2] cups: Read the IEEE 1284 device ID over SDP
Date: Mon, 16 Nov 2026 20:29:30 +0900	[thread overview]
Message-ID: <20261116112931.1598858-2-bernie@codewiz.org> (raw)
In-Reply-To: <20261116112931.1598858-1-bernie@codewiz.org>

Device1 has no DiscoverServices method, so the device ID was never
found. Read the 1284ID attribute of the HCRP record directly, as the
print path already does for the channel and PSMs.

Assisted-by: Claude:claude-fable-5
---
 profiles/cups/cups.h |   1 +
 profiles/cups/main.c | 217 ++++---------------------------------------
 profiles/cups/sdp.c  |  55 +++++++++++
 3 files changed, 72 insertions(+), 201 deletions(-)

diff --git a/profiles/cups/cups.h b/profiles/cups/cups.h
index bdaa89498..f2ec8aec7 100644
--- a/profiles/cups/cups.h
+++ b/profiles/cups/cups.h
@@ -20,6 +20,7 @@ enum {					/**** Backend exit codes ****/
 
 int sdp_search_spp(sdp_session_t *sdp, uint8_t *channel);
 int sdp_search_hcrp(sdp_session_t *sdp, unsigned short *ctrl_psm, unsigned short *data_psm);
+char *sdp_search_ieee1284_id(sdp_session_t *sdp);
 
 int spp_print(bdaddr_t *src, bdaddr_t *dst, uint8_t channel, int fd, int copies, const char *cups_class);
 int hcrp_print(bdaddr_t *src, bdaddr_t *dst, unsigned short ctrl_psm, unsigned short data_psm, int fd, int copies, const char *cups_class);
diff --git a/profiles/cups/main.c b/profiles/cups/main.c
index d56c942b9..bcd6d2f9f 100644
--- a/profiles/cups/main.c
+++ b/profiles/cups/main.c
@@ -44,136 +44,21 @@ static GMainLoop *loop = NULL;
 static DBusConnection *conn = NULL;
 static gboolean doing_disco = FALSE;
 
-#define ATTRID_1284ID 0x0300
-
-struct context_data {
-	gboolean found;
-	char *id;
-};
-
-static void element_start(GMarkupParseContext *context,
-				const char *element_name,
-				const char **attribute_names,
-				const char **attribute_values,
-				gpointer user_data, GError **err)
+static char *device_get_ieee1284_id(const char *address)
 {
-	struct context_data *ctx_data = user_data;
-
-	if (!strcmp(element_name, "record"))
-		return;
-
-	if (!strcmp(element_name, "attribute")) {
-		int i;
-		for (i = 0; attribute_names[i]; i++) {
-			if (strcmp(attribute_names[i], "id") != 0)
-				continue;
-			if (strtol(attribute_values[i], 0, 0) == ATTRID_1284ID)
-				ctx_data->found = TRUE;
-			break;
-		}
-		return;
-	}
-
-	if (ctx_data->found  && !strcmp(element_name, "text")) {
-		int i;
-		for (i = 0; attribute_names[i]; i++) {
-			if (!strcmp(attribute_names[i], "value")) {
-				ctx_data->id = g_strdup(attribute_values[i] + 2);
-				ctx_data->found = FALSE;
-			}
-		}
-	}
-}
-
-static GMarkupParser parser = {
-	element_start, NULL, NULL, NULL, NULL
-};
-
-static char *sdp_xml_parse_record(const char *data)
-{
-	GMarkupParseContext *ctx;
-	struct context_data ctx_data;
-	int size;
-
-	size = strlen(data);
-	ctx_data.found = FALSE;
-	ctx_data.id = NULL;
-	ctx = g_markup_parse_context_new(&parser, 0, &ctx_data, NULL);
-
-	if (g_markup_parse_context_parse(ctx, data, size, NULL) == FALSE) {
-		g_markup_parse_context_free(ctx);
-		g_free(ctx_data.id);
-		return NULL;
-	}
-
-	g_markup_parse_context_free(ctx);
-
-	return ctx_data.id;
-}
-
-static char *device_get_ieee1284_id(const char *adapter, const char *device)
-{
-	DBusMessage *message, *reply;
-	DBusMessageIter iter, reply_iter;
-	DBusMessageIter reply_iter_entry;
-	const char *hcr_print = "00001126-0000-1000-8000-00805f9b34fb";
-	const char *xml;
-	char *id = NULL;
-
-	/* Look for the service handle of the HCRP service */
-	message = dbus_message_new_method_call("org.bluez", device,
-						"org.bluez.Device1",
-						"DiscoverServices");
-	dbus_message_iter_init_append(message, &iter);
-	dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &hcr_print);
-
-	reply = dbus_connection_send_with_reply_and_block(conn,
-							message, -1, NULL);
+	sdp_session_t *sdp;
+	bdaddr_t bdaddr;
+	char *id;
 
-	dbus_message_unref(message);
+	str2ba(address, &bdaddr);
 
-	if (!reply)
+	sdp = sdp_connect(BDADDR_ANY, &bdaddr, SDP_RETRY_IF_BUSY);
+	if (!sdp)
 		return NULL;
 
-	dbus_message_iter_init(reply, &reply_iter);
-
-	if (dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_ARRAY) {
-		dbus_message_unref(reply);
-		return NULL;
-	}
+	id = sdp_search_ieee1284_id(sdp);
 
-	dbus_message_iter_recurse(&reply_iter, &reply_iter_entry);
-
-	/* Hopefully we only get one handle, or take a punt */
-	while (dbus_message_iter_get_arg_type(&reply_iter_entry) ==
-							DBUS_TYPE_DICT_ENTRY) {
-		guint32 key;
-		DBusMessageIter dict_entry;
-
-		dbus_message_iter_recurse(&reply_iter_entry, &dict_entry);
-
-		/* Key ? */
-		dbus_message_iter_get_basic(&dict_entry, &key);
-		if (!key) {
-			dbus_message_iter_next(&reply_iter_entry);
-			continue;
-		}
-
-		/* Try to get the value */
-		if (!dbus_message_iter_next(&dict_entry)) {
-			dbus_message_iter_next(&reply_iter_entry);
-			continue;
-		}
-
-		dbus_message_iter_get_basic(&dict_entry, &xml);
-
-		id = sdp_xml_parse_record(xml);
-		if (id != NULL)
-			break;
-		dbus_message_iter_next(&reply_iter_entry);
-	}
-
-	dbus_message_unref(reply);
+	sdp_close(sdp);
 
 	return id;
 }
@@ -362,9 +247,9 @@ static void remote_device_found(const char *adapter, const char *bdaddr,
 		return;
 	}
 
-	id = device_get_ieee1284_id(adapter, object_path);
+	id = device_get_ieee1284_id(bdaddr);
 	add_device_to_list(name, bdaddr, id);
-	g_free(id);
+	free(id);
 
 	dbus_message_unref(reply);
 }
@@ -434,9 +319,9 @@ static gboolean list_known_printers(const char *adapter)
 		if (device_is_printer(adapter, object_path, &name, &bdaddr)) {
 			char *id;
 
-			id = device_get_ieee1284_id(adapter, object_path);
+			id = device_get_ieee1284_id(bdaddr);
 			add_device_to_list(name, bdaddr, id);
-			g_free(id);
+			free(id);
 		}
 		g_free(name);
 		g_free(bdaddr);
@@ -601,84 +486,14 @@ static gboolean list_printers(void)
 
 static gboolean print_ieee1284(const char *bdaddr)
 {
-	DBusMessage *message, *reply, *adapter_reply;
-	DBusMessageIter iter;
-	char *object_path = NULL;
-	char *adapter;
 	char *id;
 
-	adapter_reply = NULL;
-
-	conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
-	if (conn == NULL)
+	id = device_get_ieee1284_id(bdaddr);
+	if (id == NULL)
 		return FALSE;
 
-	message = dbus_message_new_method_call("org.bluez", "/",
-			"org.bluez.Manager",
-			"DefaultAdapter");
-
-	adapter_reply = dbus_connection_send_with_reply_and_block(conn,
-			message, -1, NULL);
-
-	dbus_message_unref(message);
-
-	if (!adapter_reply)
-		return FALSE;
-
-	if (dbus_message_get_args(adapter_reply, NULL,
-			DBUS_TYPE_OBJECT_PATH, &adapter,
-			DBUS_TYPE_INVALID) == FALSE) {
-		dbus_message_unref(adapter_reply);
-		return FALSE;
-	}
-
-	message = dbus_message_new_method_call("org.bluez", adapter,
-			"org.bluez.Adapter1",
-			"FindDevice");
-	dbus_message_iter_init_append(message, &iter);
-	dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &bdaddr);
-
-	if (adapter_reply != NULL)
-		dbus_message_unref(adapter_reply);
-
-	reply = dbus_connection_send_with_reply_and_block(conn,
-			message, -1, NULL);
-
-	dbus_message_unref(message);
-
-	if (!reply) {
-		message = dbus_message_new_method_call("org.bluez", adapter,
-				"org.bluez.Adapter1",
-				"CreateDevice");
-		dbus_message_iter_init_append(message, &iter);
-		dbus_message_iter_append_basic(&iter,
-				DBUS_TYPE_STRING, &bdaddr);
-
-		reply = dbus_connection_send_with_reply_and_block(conn,
-				message, -1, NULL);
-
-		dbus_message_unref(message);
-
-		if (!reply)
-			return FALSE;
-	}
-
-	if (dbus_message_get_args(reply, NULL,
-					DBUS_TYPE_OBJECT_PATH, &object_path,
-					DBUS_TYPE_INVALID) == FALSE) {
-		dbus_message_unref(reply);
-		return FALSE;
-	}
-
-	id = device_get_ieee1284_id(adapter, object_path);
-	if (id == NULL) {
-		dbus_message_unref(reply);
-		return FALSE;
-	}
 	printf("%s", id);
-	g_free(id);
-
-	dbus_message_unref(reply);
+	free(id);
 
 	return TRUE;
 }
diff --git a/profiles/cups/sdp.c b/profiles/cups/sdp.c
index 5d83b8ea9..e83196df6 100644
--- a/profiles/cups/sdp.c
+++ b/profiles/cups/sdp.c
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <unistd.h>
+#include <string.h>
 #include <signal.h>
 #include <sys/socket.h>
 
@@ -24,6 +25,8 @@
 
 #include "cups.h"
 
+#define ATTRID_1284ID 0x0300
+
 int sdp_search_hcrp(sdp_session_t *sdp, unsigned short *ctrl_psm, unsigned short *data_psm)
 {
 	sdp_list_t *srch, *attrs, *rsp;
@@ -104,3 +107,55 @@ int sdp_search_spp(sdp_session_t *sdp, uint8_t *channel)
 
 	return -1;
 }
+
+static char *record_get_ieee1284_id(sdp_record_t *rec)
+{
+	sdp_data_t *data = sdp_data_get(rec, ATTRID_1284ID);
+	int len;
+
+	if (!data || !SDP_IS_TEXT_STR(data->dtd))
+		return NULL;
+
+	/* The device ID is preceded by its two length bytes */
+	len = data->unitSize - sizeof(uint8_t);
+	if (len <= 2)
+		return NULL;
+
+	return strndup(data->val.str + 2, len - 2);
+}
+
+char *sdp_search_ieee1284_id(sdp_session_t *sdp)
+{
+	sdp_list_t *srch, *attrs, *rsp, *l;
+	uuid_t svclass;
+	uint16_t attr;
+	char *id = NULL;
+	int err;
+
+	if (!sdp)
+		return NULL;
+
+	sdp_uuid16_create(&svclass, HCR_PRINT_SVCLASS_ID);
+	srch = sdp_list_append(NULL, &svclass);
+
+	attr = ATTRID_1284ID;
+	attrs = sdp_list_append(NULL, &attr);
+
+	err = sdp_service_search_attr_req(sdp, srch, SDP_ATTR_REQ_INDIVIDUAL,
+								attrs, &rsp);
+	sdp_list_free(srch, NULL);
+	sdp_list_free(attrs, NULL);
+	if (err)
+		return NULL;
+
+	for (l = rsp; l; l = l->next) {
+		if (!id)
+			id = record_get_ieee1284_id(l->data);
+
+		sdp_record_free(l->data);
+	}
+
+	sdp_list_free(rsp, NULL);
+
+	return id;
+}
-- 
2.56.1


  reply	other threads:[~2026-11-16 11:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13  9:34 [PATCH BlueZ 0/2] cups: Make printer discovery work again Bernie Innocenti
2026-11-13  9:34 ` [PATCH BlueZ 1/2] cups: Read the IEEE 1284 device ID over SDP Bernie Innocenti
2026-11-13 16:35   ` Luiz Augusto von Dentz
2026-11-16 11:29     ` [PATCH BlueZ v2 0/2] cups: Make printer discovery work again Bernie Innocenti
2026-11-16 11:29       ` Bernie Innocenti [this message]
2026-11-16 11:29       ` [PATCH BlueZ v2 2/2] cups: Port printer discovery to the ObjectManager API Bernie Innocenti
2026-11-17  8:43     ` [PATCH BlueZ 1/2] cups: Read the IEEE 1284 device ID over SDP Bernie Innocenti
2026-11-13  9:34 ` [PATCH BlueZ 2/2] cups: Port printer discovery to the ObjectManager API Bernie Innocenti

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116112931.1598858-2-bernie@codewiz.org \
    --to=bernie@codewiz.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox