public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [Bluez-devel] [PATCH] Attempt bonding with keyboards.
@ 2007-09-22 17:49 David Woodhouse
  2007-09-22 18:57 ` Marcel Holtmann
  0 siblings, 1 reply; 4+ messages in thread
From: David Woodhouse @ 2007-09-22 17:49 UTC (permalink / raw)
  To: bluez-devel; +Cc: Claudio Takahasi, marcel

My Nokia SU-8W keyboard will never attempt to reconnect to the computer
after a power cycle unless we actually pair with it. With the input
service as it currently stands, we don't -- so it works once, but then
next time I want to use it I need to go through the whole search and
'add new input device' process again (and the bluetooth-properties
applet crashes when you try to add a device which already exists, which
is not so cute).

This patch makes the input service attempt bonding with new keyboards.
It will ignore errors though -- if bonding fails, it still goes ahead
and connects as before.

I've tested this with the two input devices I have to hand -- both with
successful pairing and when I allow pairing to fail. It would be
interesting to test it on the keyboards which have problems with
pairing.

A critical pair of eyes over my first attempt at using dbus probably
wouldn't go amiss. :)

diff --git a/input/manager.c b/input/manager.c
index 5dfb064..f1d25dc 100644
--- a/input/manager.c
+++ b/input/manager.c
@@ -415,6 +415,29 @@ failed:
 	return FALSE;
 }
 
+static void create_bonding_reply(DBusPendingCall *call, void *data)
+{
+	DBusMessage *reply = dbus_pending_call_steal_reply(call);
+	struct pending_req *pr = data;
+	DBusError derr;
+
+	dbus_error_init(&derr);
+	if (dbus_set_error_from_message(&derr, reply)) {
+		error("CreateBonding failed: %s(%s)",
+					derr.name, derr.message);
+		/* But ignore the error */
+	}
+
+	dbus_message_unref(reply);
+
+	if (l2cap_connect(&pr->src, &pr->dst, L2CAP_PSM_HIDP_CTRL,
+				(GIOFunc) control_connect_cb, pr) < 0) {
+		int err = errno;
+		error("L2CAP connect failed:%s (%d)", strerror(err), err);
+		err_connection_failed(pr->conn, pr->msg, strerror(err));
+	}
+}
+
 static void finish_sdp_transaction(bdaddr_t *dba) 
 {
 	char address[18], *addr_ptr = address;
@@ -450,6 +473,34 @@ static void finish_sdp_transaction(bdaddr_t *dba)
 	dbus_message_unref(reply);
 }
 
+static int start_bonding_attempt(DBusPendingCall *call, struct pending_req *pr)
+{
+	DBusPendingCall *pending;
+	DBusMessage *msg;
+	char address[18], *addr_ptr = address;
+
+	msg = dbus_message_new_method_call("org.bluez", pr->adapter_path,
+					   "org.bluez.Adapter", "CreateBonding");
+	if (!msg) {
+		error("Unable to allocate new method call");
+		err_not_supported(pr->conn, pr->msg);
+		return -1;
+	}
+
+	ba2str(&pr->dst, address);
+	dbus_message_append_args(msg, DBUS_TYPE_STRING, &addr_ptr, DBUS_TYPE_INVALID);
+	if (dbus_connection_send_with_reply(pr->conn, msg, &pending, -1) == FALSE) {
+		error("Can't send D-Bus message.");
+		err_not_supported(pr->conn, pr->msg);
+		dbus_message_unref(msg);
+		return -1;
+	}
+	dbus_pending_call_set_notify(pending, create_bonding_reply, pr, NULL);
+	dbus_pending_call_unref(pending);
+	dbus_message_unref(msg);
+	return 0;
+}
+
 static void hid_record_reply(DBusPendingCall *call, void *data)
 {
 	DBusMessage *reply = dbus_pending_call_steal_reply(call);
@@ -457,6 +508,7 @@ static void hid_record_reply(DBusPendingCall *call, void *data)
 	DBusError derr;
 	uint8_t *rec_bin;
 	int len, scanned;
+	sdp_data_t *pdlist;
 
 	dbus_error_init(&derr);
 	if (dbus_set_error_from_message(&derr, reply)) {
@@ -493,13 +545,19 @@ static void hid_record_reply(DBusPendingCall *call, void *data)
 		goto fail;
 	}
 
-	if (l2cap_connect(&pr->src, &pr->dst, L2CAP_PSM_HIDP_CTRL,
-				(GIOFunc) control_connect_cb, pr) < 0) {
-		int err = errno;
-		error("L2CAP connect failed:%s (%d)", strerror(err), err);
-		err_connection_failed(pr->conn, pr->msg, strerror(err));
-		goto fail;
-
+	pdlist = sdp_data_get(pr->hid_rec, SDP_ATTR_HID_DEVICE_SUBCLASS);
+	if (pdlist && (pdlist->val.uint8 & 0x40)) {
+		if (start_bonding_attempt(call, pr))
+			goto fail;
+	} else {
+		/* No encryption -- connect control channel */
+		if (l2cap_connect(&pr->src, &pr->dst, L2CAP_PSM_HIDP_CTRL,
+				  (GIOFunc) control_connect_cb, pr) < 0) {
+			int err = errno;
+			error("L2CAP connect failed:%s (%d)", strerror(err), err);
+			err_connection_failed(pr->conn, pr->msg, strerror(err));
+			goto fail;
+		}
 	}
 	dbus_message_unref(reply);
 


-- 
dwmw2


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Bluez-devel] [PATCH] Attempt bonding with keyboards.
  2007-09-22 17:49 [Bluez-devel] [PATCH] Attempt bonding with keyboards David Woodhouse
@ 2007-09-22 18:57 ` Marcel Holtmann
  2007-09-22 21:00   ` David Woodhouse
  2007-09-22 23:10   ` David Woodhouse
  0 siblings, 2 replies; 4+ messages in thread
From: Marcel Holtmann @ 2007-09-22 18:57 UTC (permalink / raw)
  To: BlueZ development; +Cc: Claudio Takahasi

Hi David,

> My Nokia SU-8W keyboard will never attempt to reconnect to the computer
> after a power cycle unless we actually pair with it. With the input
> service as it currently stands, we don't -- so it works once, but then
> next time I want to use it I need to go through the whole search and
> 'add new input device' process again (and the bluetooth-properties
> applet crashes when you try to add a device which already exists, which
> is not so cute).

the main questions is if we wanna make that policy of pairing a device
first part of the input service or the the UI creating the device.

I think it should be part of the UI and it should create the pairing
before connecting the input device.

With the move to CreateDevice and CreatePairedDevice this all will
become obsolete anyway.

Regards

Marcel



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Bluez-devel] [PATCH] Attempt bonding with keyboards.
  2007-09-22 18:57 ` Marcel Holtmann
@ 2007-09-22 21:00   ` David Woodhouse
  2007-09-22 23:10   ` David Woodhouse
  1 sibling, 0 replies; 4+ messages in thread
From: David Woodhouse @ 2007-09-22 21:00 UTC (permalink / raw)
  To: BlueZ development; +Cc: Claudio Takahasi

On Sat, 2007-09-22 at 20:57 +0200, Marcel Holtmann wrote:
> the main questions is if we wanna make that policy of pairing a device
> first part of the input service or the the UI creating the device.
> 
> I think it should be part of the UI and it should create the pairing
> before connecting the input device.

I'm not convinced. Should the UI really have to know things like 'if
it's a keyboard, attempt pairing. If it's a mouse, don't bother'?

I would have thought that this was something we should leave to the
input service. The UI just tells the service to make it work.

-- 
dwmw2


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Bluez-devel] [PATCH] Attempt bonding with keyboards.
  2007-09-22 18:57 ` Marcel Holtmann
  2007-09-22 21:00   ` David Woodhouse
@ 2007-09-22 23:10   ` David Woodhouse
  1 sibling, 0 replies; 4+ messages in thread
From: David Woodhouse @ 2007-09-22 23:10 UTC (permalink / raw)
  To: BlueZ development; +Cc: Claudio Takahasi

On Sat, 2007-09-22 at 20:57 +0200, Marcel Holtmann wrote:
> I think it should be part of the UI and it should create the pairing
> before connecting the input device.

In the meantime, this would at least let the user initiate bonding
'manually' through the GUI, without having to use hcitool.

diff --git a/properties/adapter.c b/properties/adapter.c
index b9109cb..17b3aba 100644
--- a/properties/adapter.c
+++ b/properties/adapter.c
@@ -71,7 +71,7 @@ struct adapter_data {
 	GtkWidget *timeout_scale;
 	GtkWidget *entry;
 	GtkWidget *combo;
-	GtkWidget *button_delete;
+	GtkWidget *button_bonding;
 	GtkWidget *button_trusted;
 	GtkWidget *button_disconnect;
 	GtkTreeSelection *selection;
@@ -248,7 +248,11 @@ static void update_buttons(struct adapter_data *adapter, gboolean bonded,
 		gtk_button_set_label(GTK_BUTTON(adapter->button_trusted),
 							_("Remove _Trust"));
 
-	gtk_widget_set_sensitive(adapter->button_delete, bonded);
+	gtk_widget_set_sensitive(adapter->button_bonding, 1);
+	if (bonded)
+		gtk_button_set_label(GTK_BUTTON(adapter->button_bonding), _("Delete _Bonding"));
+	else
+		gtk_button_set_label(GTK_BUTTON(adapter->button_bonding), _("Create _Bonding"));
 
 	gtk_widget_set_sensitive(adapter->button_disconnect, connected);
 }
@@ -270,10 +274,13 @@ static void select_callback(GtkTreeSelection *selection, gpointer user_data)
 
 	update_buttons(adapter, bonded, trusted, connected);
 
-	if (selected == TRUE)
+	if (selected == TRUE) {
 		gtk_widget_show(adapter->button_trusted);
-	else
+		gtk_widget_show(adapter->button_bonding);
+	} else {
 		gtk_widget_hide(adapter->button_trusted);
+		gtk_widget_hide(adapter->button_bonding);
+	}
 }
 
 static void row_callback(GtkTreeModel *model, GtkTreePath  *path,
@@ -293,21 +300,28 @@ static void row_callback(GtkTreeModel *model, GtkTreePath  *path,
 	update_buttons(adapter, bonded, trusted, connected);
 }
 
-static void delete_callback(GtkWidget *button, gpointer user_data)
+static void bonding_callback(GtkWidget *button, gpointer user_data)
 {
 	struct adapter_data *adapter = user_data;
 	GtkTreeModel *model;
 	GtkTreeIter iter;
 	gchar *address;
+	gboolean bonded;
 
 	if (gtk_tree_selection_get_selected(adapter->selection,
 						&model, &iter) == FALSE)
 		return;
 
-	gtk_tree_model_get(model, &iter, COLUMN_ADDRESS, &address, -1);
+	gtk_tree_model_get(model, &iter, COLUMN_ADDRESS, &address, 
+			   COLUMN_BONDED, &bonded, -1);
 
-	if (show_confirm_dialog() == TRUE)
-		bluetooth_client_remove_bonding(client, adapter->path, address);
+	if (bonded) {
+		if (show_confirm_dialog() == TRUE)
+			bluetooth_client_remove_bonding(client, adapter->path, address);
+	} else {
+		bluetooth_client_create_bonding(client, adapter->path, address);
+	}
+		
 
 	g_free(address);
 }
@@ -762,14 +776,14 @@ static void create_adapter(struct adapter_data *adapter)
 	gtk_box_set_homogeneous(GTK_BOX(buttonbox), FALSE);
 	gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
 
-	button = gtk_button_new_from_stock(GTK_STOCK_DELETE);
-	gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
+	button = gtk_button_new_with_mnemonic(NULL);
+	gtk_widget_set_no_show_all(button, TRUE);
 	gtk_container_add(GTK_CONTAINER(buttonbox), button);
 
 	g_signal_connect(G_OBJECT(button), "clicked",
-				G_CALLBACK(delete_callback), adapter);
+				G_CALLBACK(bonding_callback), adapter);
 
-	adapter->button_delete = button;
+	adapter->button_bonding = button;
 
 	button = gtk_button_new_with_mnemonic(NULL);
 	gtk_widget_set_no_show_all(button, TRUE);

-- 
dwmw2


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-09-22 23:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-09-22 17:49 [Bluez-devel] [PATCH] Attempt bonding with keyboards David Woodhouse
2007-09-22 18:57 ` Marcel Holtmann
2007-09-22 21:00   ` David Woodhouse
2007-09-22 23:10   ` David Woodhouse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox