Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH_v5 2/6] profiles/network: Rename common.c|h to bnep.c|h
From: Ravi kumar Veeramally @ 2013-11-29 14:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ravi kumar Veeramally
In-Reply-To: <1385734845-9725-1-git-send-email-ravikumar.veeramally@linux.intel.com>

Files common.c|h contains only bnep related code, it makes
more sence with bnep.c|h.
---
 Makefile.plugins                      | 2 +-
 profiles/network/{common.c => bnep.c} | 2 +-
 profiles/network/{common.h => bnep.h} | 0
 profiles/network/connection.c         | 2 +-
 profiles/network/manager.c            | 2 +-
 profiles/network/server.c             | 2 +-
 6 files changed, 5 insertions(+), 5 deletions(-)
 rename profiles/network/{common.c => bnep.c} (99%)
 rename profiles/network/{common.h => bnep.h} (100%)

diff --git a/Makefile.plugins b/Makefile.plugins
index f5025e9..6a1ddbf 100644
--- a/Makefile.plugins
+++ b/Makefile.plugins
@@ -47,7 +47,7 @@ builtin_sources += profiles/audio/control.h profiles/audio/control.c \
 
 builtin_modules += network
 builtin_sources += profiles/network/manager.c \
-			profiles/network/common.h profiles/network/common.c \
+			profiles/network/bnep.h profiles/network/bnep.c \
 			profiles/network/server.h profiles/network/server.c \
 			profiles/network/connection.h \
 			profiles/network/connection.c
diff --git a/profiles/network/common.c b/profiles/network/bnep.c
similarity index 99%
rename from profiles/network/common.c
rename to profiles/network/bnep.c
index 71154c8..0a719a2 100644
--- a/profiles/network/common.c
+++ b/profiles/network/bnep.c
@@ -43,7 +43,7 @@
 #include <glib.h>
 
 #include "log.h"
-#include "common.h"
+#include "bnep.h"
 #include "lib/uuid.h"
 
 #define CON_SETUP_RETRIES      3
diff --git a/profiles/network/common.h b/profiles/network/bnep.h
similarity index 100%
rename from profiles/network/common.h
rename to profiles/network/bnep.h
diff --git a/profiles/network/connection.c b/profiles/network/connection.c
index d100580..9aff319 100644
--- a/profiles/network/connection.c
+++ b/profiles/network/connection.c
@@ -47,7 +47,7 @@
 #include "service.h"
 
 #include "error.h"
-#include "common.h"
+#include "bnep.h"
 #include "connection.h"
 
 #define NETWORK_PEER_INTERFACE "org.bluez.Network1"
diff --git a/profiles/network/manager.c b/profiles/network/manager.c
index ab4224d..8ac2dec 100644
--- a/profiles/network/manager.c
+++ b/profiles/network/manager.c
@@ -43,7 +43,7 @@
 #include "device.h"
 #include "profile.h"
 #include "service.h"
-#include "common.h"
+#include "bnep.h"
 #include "connection.h"
 #include "server.h"
 
diff --git a/profiles/network/server.c b/profiles/network/server.c
index 3a7e52a..b3aab11 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -48,7 +48,7 @@
 #include "error.h"
 #include "sdpd.h"
 
-#include "common.h"
+#include "bnep.h"
 #include "server.h"
 
 #define NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer1"
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH_v5 3/6] android/pan: Implement pan connect method in daemon
From: Ravi kumar Veeramally @ 2013-11-29 14:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ravi kumar Veeramally
In-Reply-To: <1385734845-9725-1-git-send-email-ravikumar.veeramally@linux.intel.com>

Implements the PAN connect method in android daemon with PANU role
only. Setting up the bnep environment, adds connection and makes
bnep interface up are part of bnep_connect call. Notifies bnep
interface on control state call back and connection status on
connection state call back.
---
 android/Android.mk  |   2 +
 android/Makefile.am |   3 +-
 android/pan.c       | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 216 insertions(+), 8 deletions(-)

diff --git a/android/Android.mk b/android/Android.mk
index c4d722d..549613c 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -42,6 +42,7 @@ LOCAL_SRC_FILES := \
 	../lib/hci.c \
 	../btio/btio.c \
 	../src/sdp-client.c \
+	../profiles/network/bnep.c \
 
 LOCAL_C_INCLUDES := \
 	$(call include-path-for, glib) \
@@ -66,6 +67,7 @@ lib_headers := \
 	sdp.h \
 	rfcomm.h \
 	sco.h \
+	bnep.h \
 
 $(shell mkdir -p $(LOCAL_PATH)/../lib/bluetooth)
 
diff --git a/android/Makefile.am b/android/Makefile.am
index 15ecf35..df04762 100644
--- a/android/Makefile.am
+++ b/android/Makefile.am
@@ -24,7 +24,8 @@ android_bluetoothd_SOURCES = android/main.c \
 				android/socket.h android/socket.c \
 				android/pan.h android/pan.c \
 				btio/btio.h btio/btio.c \
-				src/sdp-client.h src/sdp-client.c
+				src/sdp-client.h src/sdp-client.c \
+				profiles/network/bnep.h profiles/network/bnep.c
 
 android_bluetoothd_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@
 
diff --git a/android/pan.c b/android/pan.c
index ea15637..031dfff 100644
--- a/android/pan.c
+++ b/android/pan.c
@@ -29,35 +29,228 @@
 #include <fcntl.h>
 #include <glib.h>
 
+#include "btio/btio.h"
 #include "lib/bluetooth.h"
+#include "lib/bnep.h"
+#include "lib/sdp.h"
+#include "lib/sdp_lib.h"
+#include "src/glib-helper.h"
+#include "profiles/network/bnep.h"
+
 #include "log.h"
 #include "pan.h"
 #include "hal-msg.h"
 #include "ipc.h"
+#include "utils.h"
+#include "bluetooth.h"
 
-static uint8_t bt_pan_enable(struct hal_cmd_pan_enable *cmd, uint16_t len)
+static bdaddr_t adapter_addr;
+GSList *peers = NULL;
+uint8_t local_role = HAL_PAN_ROLE_NONE;
+
+struct pan_device {
+	char		dev[16];
+	bdaddr_t	dst;
+	uint8_t		conn_state;
+	uint8_t		role;
+	GIOChannel	*io;
+	guint		watch;
+};
+
+static int peer_cmp(gconstpointer s, gconstpointer user_data)
 {
-	DBG("Not Implemented");
+	const struct pan_device *dev = s;
+	const bdaddr_t *dst = user_data;
 
-	return HAL_STATUS_FAILED;
+	return bacmp(&dev->dst, dst);
 }
 
-static uint8_t bt_pan_get_role(void *cmd, uint16_t len)
+static void pan_device_free(struct pan_device *dev)
+{
+	local_role = HAL_PAN_ROLE_NONE;
+
+	if (dev->watch > 0) {
+		g_source_remove(dev->watch);
+		dev->watch = 0;
+	}
+
+	if (dev->io) {
+		g_io_channel_unref(dev->io);
+		dev->io = NULL;
+	}
+
+	peers = g_slist_remove(peers, dev);
+	g_free(dev);
+	dev = NULL;
+}
+
+static void bt_pan_notify_conn_state(struct pan_device *dev, uint8_t state)
+{
+	struct hal_ev_pan_conn_state ev;
+	char addr[18];
+
+	if (dev->conn_state == state)
+		return;
+
+	dev->conn_state = state;
+	ba2str(&dev->dst, addr);
+	DBG("device %s state %u", addr, state);
+
+	bdaddr2android(&dev->dst, ev.bdaddr);
+	ev.state = state;
+	ev.local_role = local_role;
+	ev.remote_role = dev->role;
+	ev.status = HAL_STATUS_SUCCESS;
+
+	ipc_send_notif(HAL_SERVICE_ID_PAN, HAL_EV_PAN_CONN_STATE, sizeof(ev),
+									&ev);
+}
+
+static void bt_pan_notify_ctrl_state(struct pan_device *dev, uint8_t state)
+{
+	struct hal_ev_pan_ctrl_state ev;
+
+	DBG("");
+
+	ev.state = state;
+	ev.local_role = local_role;
+	ev.status = HAL_STATUS_SUCCESS;
+	memcpy(ev.name, dev->dev, sizeof(dev->dev));
+
+	ipc_send_notif(HAL_SERVICE_ID_PAN, HAL_EV_PAN_CTRL_STATE, sizeof(ev),
+									&ev);
+}
+
+static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
+								gpointer data)
+{
+	struct pan_device *dev = data;
+
+	DBG("%s disconnected", dev->dev);
+
+	bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
+	pan_device_free(dev);
+
+	return FALSE;
+}
+
+static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data)
+{
+	struct pan_device *dev = data;
+
+	DBG("");
+
+	if (err < 0) {
+		error("bnep connect req failed: %s", strerror(-err));
+		bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
+		pan_device_free(dev);
+		return;
+	}
+
+	memcpy(dev->dev, iface, sizeof(dev->dev));
+
+	DBG("%s connected", dev->dev);
+
+	bt_pan_notify_ctrl_state(dev, HAL_PAN_CTRL_ENABLED);
+	bt_pan_notify_conn_state(dev, HAL_PAN_STATE_CONNECTED);
+
+	dev->watch = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+							bnep_watchdog_cb, dev);
+	g_io_channel_unref(dev->io);
+	dev->io = NULL;
+}
+
+static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
+{
+	struct pan_device *dev = data;
+	uint16_t src, dst;
+	int perr, sk;
+
+	DBG("");
+
+	if (err) {
+		error("%s", err->message);
+		bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
+		pan_device_free(dev);
+	}
+
+	src = (local_role == HAL_PAN_ROLE_NAP) ? BNEP_SVC_NAP : BNEP_SVC_PANU;
+	dst = (dev->role == HAL_PAN_ROLE_NAP) ? BNEP_SVC_NAP : BNEP_SVC_PANU;
+	sk = g_io_channel_unix_get_fd(dev->io);
+
+	perr = bnep_connect(sk, src, dst, bnep_conn_cb, dev);
+	if (perr < 0) {
+		error("bnep connect req failed: %s", strerror(-perr));
+		bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
+		pan_device_free(dev);
+		return;
+	}
+}
+
+static uint8_t bt_pan_connect(struct hal_cmd_pan_connect *cmd, uint16_t len)
+{
+	struct pan_device *dev;
+	bdaddr_t dst;
+	char addr[18];
+	GSList *l;
+	GError *gerr = NULL;
+
+	DBG("");
+
+	if (len < sizeof(*cmd))
+		return HAL_STATUS_INVALID;
+
+	android2bdaddr(&cmd->bdaddr, &dst);
+
+	l = g_slist_find_custom(peers, &dst, peer_cmp);
+	if (l)
+		return HAL_STATUS_FAILED;
+
+	dev = g_new0(struct pan_device, 1);
+	bacpy(&dev->dst, &dst);
+	local_role = cmd->local_role;
+	dev->role = cmd->remote_role;
+
+	ba2str(&dev->dst, addr);
+	DBG("connecting to %s %s", addr, dev->dev);
+
+	dev->io = bt_io_connect(connect_cb, dev, NULL, &gerr,
+					BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
+					BT_IO_OPT_DEST_BDADDR, &dev->dst,
+					BT_IO_OPT_PSM, BNEP_PSM,
+					BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
+					BT_IO_OPT_OMTU, BNEP_MTU,
+					BT_IO_OPT_IMTU, BNEP_MTU,
+					BT_IO_OPT_INVALID);
+	if (!dev->io) {
+		error("%s", gerr->message);
+		g_error_free(gerr);
+		g_free(dev);
+		return HAL_STATUS_FAILED;
+	}
+
+	peers = g_slist_append(peers, dev);
+	bt_pan_notify_conn_state(dev, HAL_PAN_STATE_CONNECTING);
+
+	return HAL_STATUS_SUCCESS;
+}
+
+static uint8_t bt_pan_disconnect(struct hal_cmd_pan_disconnect *cmd,
+								uint16_t len)
 {
 	DBG("Not Implemented");
 
 	return HAL_STATUS_FAILED;
 }
 
-static uint8_t bt_pan_connect(struct hal_cmd_pan_connect *cmd, uint16_t len)
+static uint8_t bt_pan_enable(struct hal_cmd_pan_enable *cmd, uint16_t len)
 {
 	DBG("Not Implemented");
 
 	return HAL_STATUS_FAILED;
 }
 
-static uint8_t bt_pan_disconnect(struct hal_cmd_pan_disconnect *cmd,
-								uint16_t len)
+static uint8_t bt_pan_get_role(void *cmd, uint16_t len)
 {
 	DBG("Not Implemented");
 
@@ -91,12 +284,24 @@ void bt_pan_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
 
 bool bt_pan_register(const bdaddr_t *addr)
 {
+	int err;
+
 	DBG("");
 
+	bacpy(&adapter_addr, addr);
+
+	err = bnep_init();
+	if (err) {
+		error("bnep init failed");
+		return false;
+	}
+
 	return true;
 }
 
 void bt_pan_unregister(void)
 {
 	DBG("");
+
+	bnep_cleanup();
 }
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH_v5 4/6] android/pan: Implement pan disconnect method in daemon
From: Ravi kumar Veeramally @ 2013-11-29 14:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ravi kumar Veeramally
In-Reply-To: <1385734845-9725-1-git-send-email-ravikumar.veeramally@linux.intel.com>

Disconnect ongoing PANU role connection betweek devices, free
the device and notify the connection state.
---
 android/pan.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/android/pan.c b/android/pan.c
index 031dfff..7093e4c 100644
--- a/android/pan.c
+++ b/android/pan.c
@@ -238,9 +238,35 @@ static uint8_t bt_pan_connect(struct hal_cmd_pan_connect *cmd, uint16_t len)
 static uint8_t bt_pan_disconnect(struct hal_cmd_pan_disconnect *cmd,
 								uint16_t len)
 {
-	DBG("Not Implemented");
+	struct pan_device *dev;
+	GSList *l;
+	bdaddr_t dst;
 
-	return HAL_STATUS_FAILED;
+	DBG("");
+
+	if (len < sizeof(*cmd))
+		return HAL_STATUS_INVALID;
+
+	android2bdaddr(&cmd->bdaddr, &dst);
+
+	l = g_slist_find_custom(peers, &dst, peer_cmp);
+	if (!l)
+		return HAL_STATUS_FAILED;
+
+	dev = l->data;
+
+	if (dev->watch) {
+		g_source_remove(dev->watch);
+		dev->watch = 0;
+	}
+
+	bnep_if_down(dev->dev);
+	bnep_kill_connection(&dst);
+
+	bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
+	pan_device_free(dev);
+
+	return HAL_STATUS_SUCCESS;
 }
 
 static uint8_t bt_pan_enable(struct hal_cmd_pan_enable *cmd, uint16_t len)
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH_v5 5/6] android/pan: Implement the get local role method in daemon
From: Ravi kumar Veeramally @ 2013-11-29 14:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ravi kumar Veeramally
In-Reply-To: <1385734845-9725-1-git-send-email-ravikumar.veeramally@linux.intel.com>

Returns local role of the device (NONE, PANU or NAP).
---
 android/pan.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/android/pan.c b/android/pan.c
index 7093e4c..9ff14f3 100644
--- a/android/pan.c
+++ b/android/pan.c
@@ -278,9 +278,15 @@ static uint8_t bt_pan_enable(struct hal_cmd_pan_enable *cmd, uint16_t len)
 
 static uint8_t bt_pan_get_role(void *cmd, uint16_t len)
 {
-	DBG("Not Implemented");
+	struct hal_rsp_pan_get_role rsp;
 
-	return HAL_STATUS_FAILED;
+	DBG("");
+
+	rsp.local_role = local_role;
+	ipc_send_rsp_full(HAL_SERVICE_ID_PAN, HAL_OP_PAN_GET_ROLE, sizeof(rsp),
+								&rsp, -1);
+
+	return HAL_STATUS_SUCCESS;
 }
 
 void bt_pan_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH_v5 6/6] android: Add reasons for adding capabilites to process
From: Ravi kumar Veeramally @ 2013-11-29 14:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Ravi kumar Veeramally
In-Reply-To: <1385734845-9725-1-git-send-email-ravikumar.veeramally@linux.intel.com>

CAP_NET_ADMIN: Allow use of MGMT interface
CAP_NET_BIND_SERVICE: Allow use of privileged PSM
CAP_NET_RAW: Allow use of bnep ioctl calls
---
 android/main.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/android/main.c b/android/main.c
index 3a14af5..eedca58 100644
--- a/android/main.c
+++ b/android/main.c
@@ -534,6 +534,9 @@ static bool set_capabilities(void)
 	header.version = _LINUX_CAPABILITY_VERSION;
 	header.pid = 0;
 
+	/* CAP_NET_ADMIN: Allow use of MGMT interface
+	 * CAP_NET_BIND_SERVICE: Allow use of privileged PSM
+	 * CAP_NET_RAW: Allow use of bnep ioctl calls */
 	cap.effective = cap.permitted =
 		CAP_TO_MASK(CAP_NET_RAW) |
 		CAP_TO_MASK(CAP_NET_ADMIN) |
-- 
1.8.3.2


^ permalink raw reply related

* Re: [PATCH_v5 0/6] Refactor bnep code and implement pan methods
From: Luiz Augusto von Dentz @ 2013-11-29 15:16 UTC (permalink / raw)
  To: Ravi kumar Veeramally; +Cc: linux-bluetooth@vger.kernel.org
In-Reply-To: <1385734845-9725-1-git-send-email-ravikumar.veeramally@linux.intel.com>

Hi Ravi,

On Fri, Nov 29, 2013 at 4:20 PM, Ravi kumar Veeramally
<ravikumar.veeramally@linux.intel.com> wrote:
> v5: Renamed strcut network_peer to pan_device as per Luiz comments.
>
> v4: Fixed Luiz comments (discussed in irc). Merge patch 1 and 2.
>     Pass fd in bnep_connect instead of GIOChannel.
>
> v3: Fixed Anderson and Luiz comments and solved rebased conflicts.
>
> v2: Refactored profiles/network/common.* as per Johan's comments
>     (renaming common.c|h to bnep.c|h and moving bnep related code to
>      bnep.c ro reduce redundancy in profiles/netowrk/connection.c and
>      android/pan.c)
>
> v1: This patch set supports PANU role with a minor fix in android. Added
>     CAP_NET_RAW capability for bnep services. Creates bnep connection and
>     up the inreface on connect call and free the device on disconnect call.
>     Interface name(bnepX) will be notified on control state cb. Android
>     environment will create IP address with dhcp calls.
>
> Ravi kumar Veeramally (6):
>   profiles/network: Refactor bnep connection setup functionality
>   profiles/network: Rename common.c|h to bnep.c|h
>   android/pan: Implement pan connect method in daemon
>   android/pan: Implement pan disconnect method in daemon
>   android/pan: Implement the get local role method in daemon
>   android: Add reasons for adding capabilites to process
>
>  Makefile.plugins                      |   2 +-
>  android/Android.mk                    |   2 +
>  android/Makefile.am                   |   3 +-
>  android/main.c                        |   3 +
>  android/pan.c                         | 253 ++++++++++++++++++++++++++++++++--
>  profiles/network/{common.c => bnep.c} | 197 +++++++++++++++++++++++++-
>  profiles/network/{common.h => bnep.h} |   5 +
>  profiles/network/connection.c         | 174 ++---------------------
>  profiles/network/manager.c            |   2 +-
>  profiles/network/server.c             |   5 +-
>  10 files changed, 468 insertions(+), 178 deletions(-)
>  rename profiles/network/{common.c => bnep.c} (58%)
>  rename profiles/network/{common.h => bnep.h} (87%)
>
> --
> 1.8.3.2

Applied, please have a look as I renamed a few other things and I
found that we were sending uninitialized data while testing.


-- 
Luiz Augusto von Dentz

^ permalink raw reply

* have to re-pair mouse every few hours
From: Brian J. Murrell @ 2013-11-30 20:37 UTC (permalink / raw)
  To: linux-bluetooth

[-- Attachment #1: Type: text/plain, Size: 1066 bytes --]

Hi,

I'm using a Microsoft Sculpt Touch Mouse on a Fedora 19 machine
(kernel-3.11.9-200.fc19.x86_64 and bluez-4.101-9.fc19.x86_64) with a:

Bus 002 Device 019: ID 0a5c:2148 Broadcom Corp. BCM92046DG-CL1ROM Bluetooth 2.1 Adapter

bluetooth adapter.  This exact same configuration worked for months just
fine with a Logitech bluetooth mouse.  But sadly that mouse was crap
(second one to fail within the warranty period of one of them) so
replaced it with this MS one.

The problem with this MS mouse is that it just goes AWOL and needs to be
delete and re-paired with the machine every few hours.  Typically it's
after I have gotten up from the computer and have come back to it.  But
I have also had it just happen while using it.

When this happens, the messages log reports messages such as:

Nov 30 12:42:03 pc kernel: [2500032.028982] Bluetooth: Unexpected continuation frame (len 0)
Nov 30 12:42:03 pc kernel: [2500032.115027] Bluetooth: Unexpected continuation frame (len 0)

Any ideas what the problem might be here?

Cheers,
b.



[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply

* [PATCH 0/4] more sixaxis goodies
From: Szymon Janc @ 2013-12-01 13:41 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

Hi,

This adds support for setting PS3 controller LEDs both over Bluetooth
and USB. More details in commits messages.

One thing I noticed is that setting LEDs over Bluetooth fails on kernel
3.10+ (write always returns 0). Could this be related to HIDP related
work that was merged in 3.10?

Comments and testing are welcome.

-- 
BR
Szymon Janc
Szymon Janc (4):
  sixaxis: Add support for setting PS3 controller LEDs
  Rename device_is_connected to btd_device_is_connected
  sixaxis: Skip controller setup if already connected over Bluetooth
  sixaxis: Add support for setting LEDs when connected over USB

 plugins/sixaxis.c       | 164 +++++++++++++++++++++++++++++++++++++++++++++---
 profiles/input/device.c |   4 +-
 src/adapter.c           |   6 +-
 src/device.c            |   6 +-
 src/device.h            |   2 +-
 5 files changed, 165 insertions(+), 17 deletions(-)

-- 
1.8.5


^ permalink raw reply

* [PATCH 1/4] sixaxis: Add support for setting PS3 controller LEDs
From: Szymon Janc @ 2013-12-01 13:41 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385905316-21800-1-git-send-email-szymon.janc@gmail.com>

This will set controller LEDs according to joystick device number
when controller is connected over Bluetooth. If joystick number is too
big (> 7) or falied to be read, set it to 0 to switch off all LEDs.
This will allow to disable LEDs blinking after connection.

Waiting for events is not really needed when connected over Bluetooth
but this is in preparation for supporting LEDs setup over USB.
---
 plugins/sixaxis.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 139 insertions(+), 2 deletions(-)

diff --git a/plugins/sixaxis.c b/plugins/sixaxis.c
index 070b463..c3ca267 100644
--- a/plugins/sixaxis.c
+++ b/plugins/sixaxis.c
@@ -31,6 +31,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <stdlib.h>
 #include <sys/ioctl.h>
 #include <linux/hidraw.h>
 #include <linux/input.h>
@@ -124,6 +125,62 @@ static int set_master_bdaddr(int fd, const bdaddr_t *bdaddr)
 	return ret;
 }
 
+static gboolean setup_leds(GIOChannel *channel, GIOCondition cond,
+							gpointer user_data)
+{
+	/*
+	 * the total time the led is active (0xff means forever)
+	 * |     duty_length: cycle time in deciseconds (0 - "blink very fast")
+	 * |     |     ??? (Maybe a phase shift or duty_length multiplier?)
+	 * |     |     |     % of duty_length led is off (0xff means 100%)
+	 * |     |     |     |     % of duty_length led is on (0xff means 100%)
+	 * |     |     |     |     |
+	 * 0xff, 0x27, 0x10, 0x00, 0x32,
+	 */
+	uint8_t leds_report[] = {
+		0x01,
+		0x00, 0x00, 0x00, 0x00, 0x00, /* rumble values TBD */
+		0x00, 0x00, 0x00, 0x00, 0x00, /* LED_1=0x02, LED_2=0x04 ... */
+		0xff, 0x27, 0x10, 0x00, 0x32, /* LED_4 */
+		0xff, 0x27, 0x10, 0x00, 0x32, /* LED_3 */
+		0xff, 0x27, 0x10, 0x00, 0x32, /* LED_2 */
+		0xff, 0x27, 0x10, 0x00, 0x32, /* LED_1 */
+		0x00, 0x00, 0x00, 0x00, 0x00,
+	};
+	int number = GPOINTER_TO_INT(user_data);
+	int ret;
+	int fd;
+
+	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
+		return FALSE;
+
+	DBG("number %d", number);
+
+	/* TODO we could support up to 10 (1 + 2 + 3 + 4) */
+	if (number > 7)
+		return FALSE;
+
+	if (number > 4) {
+		leds_report[10] |= 0x10;
+		number -= 4;
+	}
+
+	leds_report[10] |= 0x01 << number;
+
+	fd = g_io_channel_unix_get_fd(channel);
+
+	ret = write(fd, leds_report, sizeof(leds_report));
+	if (ret == sizeof(leds_report))
+		return FALSE;
+
+	if (ret < 0)
+		error("sixaxis: failed to set LEDS (%s)", strerror(errno));
+	else
+		error("sixaxis: failed to set LEDS (%d bytes written)", ret);
+
+	return FALSE;
+}
+
 static void setup_device(int fd, int index, struct btd_adapter *adapter)
 {
 	char device_addr[18], master_addr[18], adapter_addr[18];
@@ -167,6 +224,69 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 	btd_device_set_trusted(device, TRUE);
 }
 
+static int get_js_number(struct udev_device *udevice)
+{
+	struct udev_list_entry *devices, *dev_list_entry;
+	struct udev_enumerate *enumerate;
+	struct udev_device *hid_parent;
+	const char *hidraw_node;
+	const char *hid_phys;
+	int number = 0;
+
+	hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice,
+								"hid", NULL);
+
+	hid_phys = udev_device_get_property_value(hid_parent, "HID_PHYS");
+	hidraw_node = udev_device_get_devnode(udevice);
+	if (!hid_phys || !hidraw_node)
+		return 0;
+
+	enumerate = udev_enumerate_new(udev_device_get_udev(udevice));
+	udev_enumerate_add_match_sysname(enumerate, "js*");
+	udev_enumerate_scan_devices(enumerate);
+	devices = udev_enumerate_get_list_entry(enumerate);
+
+	udev_list_entry_foreach(dev_list_entry, devices) {
+		struct udev_device *input_parent;
+		struct udev_device *js_dev;
+		const char *input_phys;
+		const char *devname;
+
+		devname = udev_list_entry_get_name(dev_list_entry);
+		js_dev = udev_device_new_from_syspath(
+						udev_device_get_udev(udevice),
+						devname);
+
+		input_parent = udev_device_get_parent_with_subsystem_devtype(
+							js_dev, "input", NULL);
+		if (!input_parent)
+			goto next;
+
+		/* check if this is the joystick relative to the hidraw device
+		 * above */
+		input_phys = udev_device_get_sysattr_value(input_parent,
+									"phys");
+		if (!input_phys)
+			goto next;
+
+		if (!strcmp(input_phys, hid_phys)) {
+			number = atoi(udev_device_get_sysnum(js_dev));
+
+			/* joystick numbers start from 0, leds from 1 */
+			number++;
+
+			udev_device_unref(js_dev);
+			break;
+		}
+next:
+		udev_device_unref(js_dev);
+	}
+
+	udev_enumerate_unref(enumerate);
+
+	return number;
+}
+
 static int get_supported_device(struct udev_device *udevice, uint16_t *bus)
 {
 	struct udev_device *hid_parent;
@@ -195,6 +315,7 @@ static int get_supported_device(struct udev_device *udevice, uint16_t *bus)
 static void device_added(struct udev_device *udevice)
 {
 	struct btd_adapter *adapter;
+	GIOChannel *io;
 	uint16_t bus;
 	int index;
 	int fd;
@@ -215,10 +336,26 @@ static void device_added(struct udev_device *udevice)
 	if (fd < 0)
 		return;
 
-	if (bus == BUS_USB)
+	io = g_io_channel_unix_new(fd);
+
+	switch (bus) {
+	case BUS_USB:
 		setup_device(fd, index, adapter);
+		break;
+	case BUS_BLUETOOTH:
+		/* wait for events before setting leds */
+		g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+				setup_leds,
+				GINT_TO_POINTER(get_js_number(udevice)));
+
+		break;
+	default:
+		DBG("uknown bus type (%u)", bus);
+		break;
+	}
 
-	close(fd);
+	g_io_channel_set_close_on_unref(io, TRUE);
+	g_io_channel_unref(io);
 }
 
 static gboolean monitor_watch(GIOChannel *source, GIOCondition condition,
-- 
1.8.5


^ permalink raw reply related

* [PATCH 2/4] Rename device_is_connected to btd_device_is_connected
From: Szymon Janc @ 2013-12-01 13:41 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385905316-21800-1-git-send-email-szymon.janc@gmail.com>

Allow this symbol to be exported and usable from external plugins.
---
 profiles/input/device.c | 4 ++--
 src/adapter.c           | 6 +++---
 src/device.c            | 6 +++---
 src/device.h            | 2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/profiles/input/device.c b/profiles/input/device.c
index 0c2089b..521aca8 100644
--- a/profiles/input/device.c
+++ b/profiles/input/device.c
@@ -667,7 +667,7 @@ static gboolean input_device_auto_reconnect(gpointer user_data)
 	/* Stop the recurrent reconnection attempts if the device is reconnected
 	 * or is marked for removal. */
 	if (device_is_temporary(idev->device) ||
-					device_is_connected(idev->device))
+					btd_device_is_connected(idev->device))
 		return FALSE;
 
 	/* Only attempt an auto-reconnect for at most 3 minutes (6 * 30s). */
@@ -713,7 +713,7 @@ static void input_device_enter_reconnect_mode(struct input_device *idev)
 	/* If the device is temporary we are not required to reconnect with the
 	 * device. This is likely the case of a removing device. */
 	if (device_is_temporary(idev->device) ||
-					device_is_connected(idev->device))
+					btd_device_is_connected(idev->device))
 		return;
 
 	if (idev->reconnect_timer > 0)
diff --git a/src/adapter.c b/src/adapter.c
index 55a41db..41f7bd6 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2158,7 +2158,7 @@ static DBusMessage *remove_device(DBusConnection *conn,
 
 	btd_device_set_temporary(device, TRUE);
 
-	if (!device_is_connected(device)) {
+	if (!btd_device_is_connected(device)) {
 		adapter_remove_device(adapter, device);
 		return dbus_message_new_method_return(msg);
 	}
@@ -4195,7 +4195,7 @@ connect_le:
 	 * connect_list stop passive scanning so that a connection
 	 * attempt to it can be made
 	 */
-	if (device_is_le(dev) && !device_is_connected(dev) &&
+	if (device_is_le(dev) && !btd_device_is_connected(dev) &&
 				g_slist_find(adapter->connect_list, dev)) {
 		adapter->connect_le = dev;
 		stop_passive_scanning(adapter);
@@ -5865,7 +5865,7 @@ static void unpaired_callback(uint16_t index, uint16_t length,
 
 	btd_device_set_temporary(device, TRUE);
 
-	if (device_is_connected(device))
+	if (btd_device_is_connected(device))
 		device_request_disconnect(device, NULL);
 	else
 		adapter_remove_device(adapter, device);
diff --git a/src/device.c b/src/device.c
index 847ffad..5380c1a 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1355,7 +1355,7 @@ static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
 	if (device_is_le(dev)) {
 		int err;
 
-		if (device_is_connected(dev))
+		if (btd_device_is_connected(dev))
 			return dbus_message_new_method_return(msg);
 
 		btd_device_set_temporary(dev, FALSE);
@@ -1645,7 +1645,7 @@ static DBusMessage *pair_device(DBusConnection *conn, DBusMessage *msg,
 	 * channel first and only then start pairing (there's code for
 	 * this in the ATT connect callback)
 	 */
-	if (device_is_le(device) && !device_is_connected(device))
+	if (device_is_le(device) && !btd_device_is_connected(device))
 		err = device_connect_le(device);
 	else
 		err = adapter_create_bonding(adapter, &device->bdaddr,
@@ -1791,7 +1791,7 @@ static const GDBusPropertyTable device_properties[] = {
 	{ }
 };
 
-gboolean device_is_connected(struct btd_device *device)
+gboolean btd_device_is_connected(struct btd_device *device)
 {
 	return device->connected;
 }
diff --git a/src/device.h b/src/device.h
index c3fea4a..3a33cb2 100644
--- a/src/device.h
+++ b/src/device.h
@@ -74,7 +74,7 @@ void btd_device_set_trusted(struct btd_device *device, gboolean trusted);
 void device_set_bonded(struct btd_device *device, gboolean bonded);
 void device_set_legacy(struct btd_device *device, bool legacy);
 void device_set_rssi(struct btd_device *device, int8_t rssi);
-gboolean device_is_connected(struct btd_device *device);
+gboolean btd_device_is_connected(struct btd_device *device);
 bool device_is_retrying(struct btd_device *device);
 void device_bonding_complete(struct btd_device *device, uint8_t status);
 gboolean device_is_bonding(struct btd_device *device, const char *sender);
-- 
1.8.5


^ permalink raw reply related

* [PATCH 3/4] sixaxis: Skip controller setup if already connected over Bluetooth
From: Szymon Janc @ 2013-12-01 13:41 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385905316-21800-1-git-send-email-szymon.janc@gmail.com>

If controller is already connected over Bluetooth but was then
plugged-in via USB (eg. to charge battery) there is no need to do
any setup.
---
 plugins/sixaxis.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/plugins/sixaxis.c b/plugins/sixaxis.c
index c3ca267..b8fe287 100644
--- a/plugins/sixaxis.c
+++ b/plugins/sixaxis.c
@@ -194,6 +194,12 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 	if (get_master_bdaddr(fd, &master_bdaddr) < 0)
 		return;
 
+	/* This can happen if controller was plugged while already connected
+	 * eg. to charge up battery */
+	device = btd_adapter_find_device(adapter, &device_bdaddr);
+	if (device && btd_device_is_connected(device))
+		return;
+
 	adapter_bdaddr = btd_adapter_get_address(adapter);
 
 	if (bacmp(adapter_bdaddr, &master_bdaddr)) {
-- 
1.8.5


^ permalink raw reply related

* [PATCH 4/4] sixaxis: Add support for setting LEDs when connected over USB
From: Szymon Janc @ 2013-12-01 13:41 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385905316-21800-1-git-send-email-szymon.janc@gmail.com>

This allows to setup LEDs when device is connected over USB, not
Bluetooth. This coverts two scenarios:
- user plugged PS3 controller and pressed PS3 button before unplugging,
  in that case LEDs are set
- user plugged already BT connected PS3 controller to USB, this results
  in new /dev/input/jsX device being create but controller is still
  transmitting over BT and old jsX device exists. In that case don't
  set LEDs as they are already set.

This is not directly related to Bluetooth itself but change is really
small and provides much better and consistent user experience.
---
 plugins/sixaxis.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/plugins/sixaxis.c b/plugins/sixaxis.c
index b8fe287..45fa170 100644
--- a/plugins/sixaxis.c
+++ b/plugins/sixaxis.c
@@ -181,7 +181,7 @@ static gboolean setup_leds(GIOChannel *channel, GIOCondition cond,
 	return FALSE;
 }
 
-static void setup_device(int fd, int index, struct btd_adapter *adapter)
+static bool setup_device(int fd, int index, struct btd_adapter *adapter)
 {
 	char device_addr[18], master_addr[18], adapter_addr[18];
 	bdaddr_t device_bdaddr, master_bdaddr;
@@ -189,22 +189,23 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 	struct btd_device *device;
 
 	if (get_device_bdaddr(fd, &device_bdaddr) < 0)
-		return;
+		return false;
 
 	if (get_master_bdaddr(fd, &master_bdaddr) < 0)
-		return;
+		return false;
 
 	/* This can happen if controller was plugged while already connected
-	 * eg. to charge up battery */
+	 * eg. to charge up battery.
+	 * Don't set LEDs in that case, hence return false */
 	device = btd_adapter_find_device(adapter, &device_bdaddr);
 	if (device && btd_device_is_connected(device))
-		return;
+		return false;
 
 	adapter_bdaddr = btd_adapter_get_address(adapter);
 
 	if (bacmp(adapter_bdaddr, &master_bdaddr)) {
 		if (set_master_bdaddr(fd, adapter_bdaddr) < 0)
-			return;
+			return false;
 	}
 
 	ba2str(&device_bdaddr, device_addr);
@@ -218,7 +219,7 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 	if (g_slist_find_custom(btd_device_get_uuids(device), HID_UUID,
 						(GCompareFunc)strcasecmp)) {
 		DBG("device %s already known, skipping", device_addr);
-		return;
+		return true;
 	}
 
 	info("sixaxis: setting up new device");
@@ -228,6 +229,8 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 				devices[index].pid, devices[index].version);
 	btd_device_set_temporary(device, FALSE);
 	btd_device_set_trusted(device, TRUE);
+
+	return true;
 }
 
 static int get_js_number(struct udev_device *udevice)
@@ -346,8 +349,10 @@ static void device_added(struct udev_device *udevice)
 
 	switch (bus) {
 	case BUS_USB:
-		setup_device(fd, index, adapter);
-		break;
+		if (!setup_device(fd, index, adapter))
+			break;
+
+		/* fall through */
 	case BUS_BLUETOOTH:
 		/* wait for events before setting leds */
 		g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-- 
1.8.5


^ permalink raw reply related

* Add empty udev rule to disable hid2hci by default
From: Alexander Holler @ 2013-12-01 17:26 UTC (permalink / raw)
  To: linux-bluetooth

Hello.

Almost every distribution gets it wrong and enables hid2hci by default 
(besides Fedora where I already intervened twice).

This is a real problem, because it disables Bluetooth keyboards and/or 
mice which aren't paired with bluez, thus many Live-CDs and default 
installs aren't usable when only a Bluetooth keyboard is connect.

An easy solution to disable that behaviour would be to install an empty 
rule in /etc/udev/rules.d named the same as the one in 
/lib/udev/rules.d. It could just contain a comment like

# Delete this file in order to activate hid2hci.
#
# You might need to pair your Bluetooth keyboard and/or mouse
# in order to still use it when hid2hci is enabled.

This (empty) rule would then be used by udev instead of the one in 
/lib/udev/rules.d and thus would be an easy to use configuration switch.

I would appreciate it, if the default bluez install would install such 
an empty rule too, if configure was called with --enable-hid2hci.

I think otherwise that problem will never go away. It's really 
unbelievable how many distributions got this wrong and thus how many 
Live-CDs and default installations are unusable when only a Bluetooth 
keyboard is used with a hid-aware Bluetooth dongle.

Regards,

Alexander Holler

^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Bastien Nocera @ 2013-12-01 22:28 UTC (permalink / raw)
  To: Brian J. Murrell; +Cc: linux-bluetooth
In-Reply-To: <1385843856.12122.5.camel@pc.interlinx.bc.ca>

Hey Brian,

On Sat, 2013-11-30 at 15:37 -0500, Brian J. Murrell wrote:
> Hi,
> 
> I'm using a Microsoft Sculpt Touch Mouse on a Fedora 19 machine
> (kernel-3.11.9-200.fc19.x86_64 and bluez-4.101-9.fc19.x86_64) with a:
> 
> Bus 002 Device 019: ID 0a5c:2148 Broadcom Corp. BCM92046DG-CL1ROM Bluetooth 2.1 Adapter
> 
> bluetooth adapter.  This exact same configuration worked for months just
> fine with a Logitech bluetooth mouse.  But sadly that mouse was crap
> (second one to fail within the warranty period of one of them) so
> replaced it with this MS one.
> 
> The problem with this MS mouse is that it just goes AWOL and needs to be
> delete and re-paired with the machine every few hours.  Typically it's
> after I have gotten up from the computer and have come back to it.  But
> I have also had it just happen while using it.
> 
> When this happens, the messages log reports messages such as:
> 
> Nov 30 12:42:03 pc kernel: [2500032.028982] Bluetooth: Unexpected continuation frame (len 0)
> Nov 30 12:42:03 pc kernel: [2500032.115027] Bluetooth: Unexpected continuation frame (len 0)
> 
> Any ideas what the problem might be here?

Upstream developers don't work on BlueZ 4.x anymore. Any chance for you
to test using Fedora 20? It uses BlueZ 5.x which has a lot of fixes and
architectural changes compared to 4.x.

Cheers


^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Brian J. Murrell @ 2013-12-01 22:36 UTC (permalink / raw)
  To: Bastien Nocera; +Cc: linux-bluetooth
In-Reply-To: <1385936884.5405.3.camel@nuvo>

[-- Attachment #1: Type: text/plain, Size: 512 bytes --]

On Sun, 2013-12-01 at 23:28 +0100, Bastien Nocera wrote: 
> Hey Brian,

Hi Bastien,

> Upstream developers don't work on BlueZ 4.x anymore. Any chance for you
> to test using Fedora 20?

Hrm.  Scheduling an upgrade, complete with back-out path here is bit of
work.  I will see what I can do.  But for F19 I guess I am just stuck
with this frustrating behavior?  :-(

> It uses BlueZ 5.x which has a lot of fixes and
> architectural changes compared to 4.x.

Interesting to know.

Cheers,
b.


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Bastien Nocera @ 2013-12-01 22:38 UTC (permalink / raw)
  To: Brian J. Murrell; +Cc: linux-bluetooth
In-Reply-To: <1385937375.12122.118.camel@pc.interlinx.bc.ca>

On Sun, 2013-12-01 at 17:36 -0500, Brian J. Murrell wrote:
> On Sun, 2013-12-01 at 23:28 +0100, Bastien Nocera wrote: 
> > Hey Brian,
> 
> Hi Bastien,
> 
> > Upstream developers don't work on BlueZ 4.x anymore. Any chance for you
> > to test using Fedora 20?
> 
> Hrm.  Scheduling an upgrade, complete with back-out path here is bit of
> work.  I will see what I can do.  But for F19 I guess I am just stuck
> with this frustrating behavior?  :-(

You could try with a LiveCD.

> > It uses BlueZ 5.x which has a lot of fixes and
> > architectural changes compared to 4.x.
> 
> Interesting to know.
> 
> Cheers,
> b.
> 



^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Brian J. Murrell @ 2013-12-01 22:44 UTC (permalink / raw)
  To: Bastien Nocera; +Cc: linux-bluetooth
In-Reply-To: <1385937507.5405.5.camel@nuvo>

[-- Attachment #1: Type: text/plain, Size: 629 bytes --]

On Sun, 2013-12-01 at 23:38 +0100, Bastien Nocera wrote: 
> 
> You could try with a LiveCD.

I don't think a LiveCD would be a useful enough work environment for me
to use long enough to see the problem occur.  It has happened within
minutes once or twice but typically it can take several hours.  This
afternoon has actually been quite good a quite a few hours.

I don't know that there is enough useful stuff for me to do (without a
whole ton of configuring self preferences) in a livecd environment for
me to keep using it for hours.  Probably upgrading to F20 is the most
productive way to see.

Cheers,
b.


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply

* [PATCH 0/2] Bluetooth: A couple of SMP fixes
From: johan.hedberg @ 2013-12-02  8:49 UTC (permalink / raw)
  To: linux-bluetooth

From: Johan Hedberg <johan.hedberg@intel.com>

Hi,

Here are a couple of small tweaks to the SMP code that I've found while
browsing through and testing it (with the user space smp-tester).

Johan

Johan Hedberg (2):
  Bluetooth: Remove useless smp_rand function
  Bluetooth: Remove dead code from SMP encryption function

 net/bluetooth/smp.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)

-- 
1.8.4.2


^ permalink raw reply

* [PATCH 1/2] Bluetooth: Remove useless smp_rand function
From: johan.hedberg @ 2013-12-02  8:49 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1385974144-8836-1-git-send-email-johan.hedberg@gmail.com>

From: Johan Hedberg <johan.hedberg@intel.com>

This function was always just making a single get_random_bytes() call
and always returning the value 0. It's simpler to just call
get_random_bytes() directly where needed.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
 net/bluetooth/smp.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)

diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index f99352d1aa43..3bcb765b6a92 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -143,13 +143,6 @@ static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16],
 	return err;
 }
 
-static int smp_rand(u8 *buf)
-{
-	get_random_bytes(buf, 16);
-
-	return 0;
-}
-
 static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
 				     u16 dlen, void *data)
 {
@@ -606,9 +599,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (check_enc_key_size(conn, key_size))
 		return SMP_ENC_KEY_SIZE;
 
-	ret = smp_rand(smp->prnd);
-	if (ret)
-		return SMP_UNSPECIFIED;
+	get_random_bytes(smp->prnd, sizeof(smp->prnd));
 
 	smp->prsp[0] = SMP_CMD_PAIRING_RSP;
 	memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
@@ -644,9 +635,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (check_enc_key_size(conn, key_size))
 		return SMP_ENC_KEY_SIZE;
 
-	ret = smp_rand(smp->prnd);
-	if (ret)
-		return SMP_UNSPECIFIED;
+	get_random_bytes(smp->prnd, sizeof(smp->prnd));
 
 	smp->prsp[0] = SMP_CMD_PAIRING_RSP;
 	memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
-- 
1.8.4.2


^ permalink raw reply related

* [PATCH 2/2] Bluetooth: Remove dead code from SMP encryption function
From: johan.hedberg @ 2013-12-02  8:49 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1385974144-8836-1-git-send-email-johan.hedberg@gmail.com>

From: Johan Hedberg <johan.hedberg@intel.com>

The AES cipher is used in ECB mode by SMP and therefore doesn't use an
IV (crypto_blkcipher_ivsize returns 0) so the code trying to set the IV
was never getting called. Simply remove this code to avoid anyone from
thinking it actually makes some difference.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
 net/bluetooth/smp.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 3bcb765b6a92..e61e74a1aabb 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -53,8 +53,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
 {
 	struct blkcipher_desc desc;
 	struct scatterlist sg;
-	int err, iv_len;
-	unsigned char iv[128];
+	int err;
 
 	if (tfm == NULL) {
 		BT_ERR("tfm %p", tfm);
@@ -72,12 +71,6 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
 
 	sg_init_one(&sg, r, 16);
 
-	iv_len = crypto_blkcipher_ivsize(tfm);
-	if (iv_len) {
-		memset(&iv, 0xff, iv_len);
-		crypto_blkcipher_set_iv(tfm, iv, iv_len);
-	}
-
 	err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
 	if (err)
 		BT_ERR("Encrypt data error %d", err);
-- 
1.8.4.2


^ permalink raw reply related

* Re: [PATCH 0/2] Bluetooth: A couple of SMP fixes
From: Marcel Holtmann @ 2013-12-02 11:23 UTC (permalink / raw)
  To: Johan Hedberg; +Cc: linux-bluetooth@vger.kernel.org development
In-Reply-To: <1385974144-8836-1-git-send-email-johan.hedberg@gmail.com>

Hi Johan,

> Here are a couple of small tweaks to the SMP code that I've found while
> browsing through and testing it (with the user space smp-tester).
> 
> Johan
> 
> Johan Hedberg (2):
>  Bluetooth: Remove useless smp_rand function
>  Bluetooth: Remove dead code from SMP encryption function
> 
> net/bluetooth/smp.c | 24 +++---------------------
> 1 file changed, 3 insertions(+), 21 deletions(-)

both patches have been applied to bluetooth-next.

Regards

Marcel


^ permalink raw reply

* [PATCH v3 0/9] android: IPC improvements - daemon part
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

v3:
 - rebased againt latest pan changes

v2:
 - rebased against latest IPC helpers improvements
 - more compact command handlers table format
 - error handling path in command handlers improved according to Johan comments
 - randmon small fixes
 - patches not directly related to refactor removed from serie, will
   be send after this is merged

v1:
This serie implements IPC message handling iprovments in daemon similar
to what is already done in HAL part.

Szymon Janc (9):
  android: Add initial code for IPC message handlers
  android/main: Use generic IPC message handling for core service
  android/main: Use common exit path in core service functions
  android/bluetooth: Use generic IPC msg handling for commands
  android/bluetooth: Make property handling function return HAL status
  android/hidhost: Use generic IPC message handling for commands
  android/pan: Use generic IPC message handling for commands
  android/a2dp: Use generic IPC message handling for commands
  android/socket: Use generic IPC message handling for commands

 android/a2dp.c      |  69 ++++----
 android/a2dp.h      |   2 -
 android/bluetooth.c | 477 ++++++++++++++++++++++++++++++++++------------------
 android/hidhost.c   | 309 ++++++++++++++++++++--------------
 android/hidhost.h   |   2 -
 android/ipc.c       |  78 +++++++++
 android/ipc.h       |  10 ++
 android/main.c      | 123 +++++---------
 android/pan.c       |  87 +++++-----
 android/pan.h       |   2 -
 android/socket.c    | 102 ++++++-----
 11 files changed, 754 insertions(+), 507 deletions(-)

-- 
1.8.3.2


^ permalink raw reply

* [PATCH v3 1/9] android: Add initial code for IPC message handlers
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

This will allow to register and unregister handlers for IPC messages
Basic sanity check will be done in common code. Commands with variable
length will be verified against minimum size only.
---
 android/ipc.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 android/ipc.h | 10 ++++++++
 2 files changed, 88 insertions(+)

diff --git a/android/ipc.c b/android/ipc.c
index 64b0db5..56f328b 100644
--- a/android/ipc.c
+++ b/android/ipc.c
@@ -30,12 +30,20 @@
 #include <stdint.h>
 #include <string.h>
 #include <signal.h>
+#include <stdbool.h>
 #include <sys/socket.h>
 
 #include "hal-msg.h"
 #include "ipc.h"
 #include "log.h"
 
+struct service_handler {
+	const struct ipc_handler *handler;
+	uint8_t size;
+};
+
+static struct service_handler services[HAL_SERVICE_ID_MAX + 1];
+
 static int cmd_sk = -1;
 static int notif_sk = -1;
 
@@ -124,3 +132,73 @@ void ipc_send_notif(uint8_t service_id, uint8_t opcode,  uint16_t len,
 
 	ipc_send(notif_sk, service_id, opcode, len, param, -1);
 }
+
+void ipc_register(uint8_t service, const struct ipc_handler *handlers,
+								uint8_t size)
+{
+	services[service].handler = handlers;
+	services[service].size = size;
+}
+
+void ipc_unregister(uint8_t service)
+{
+	services[service].handler = NULL;
+	services[service].size = 0;
+}
+
+void ipc_handle_msg(const void *buf, ssize_t len)
+{
+	const struct hal_hdr *msg = buf;
+	const struct ipc_handler *handler;
+
+	if (len < (ssize_t) sizeof(*msg)) {
+		error("IPC: message too small (%zd bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
+		error("IPC: message malformed (%zd bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if service is valid */
+	if (msg->service_id > HAL_SERVICE_ID_MAX) {
+		error("IPC: unknown service (0x%x), terminating",
+							msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if service is registered */
+	if (!services[msg->service_id].handler) {
+		error("IPC: unregistered service (0x%x), terminating",
+							msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if opcode is valid */
+	if (msg->opcode == HAL_OP_STATUS ||
+			msg->opcode > services[msg->service_id].size) {
+		error("IPC: invalid opcode 0x%x for service 0x%x, terminating",
+						msg->opcode, msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* opcode is table offset + 1 */
+	handler = &services[msg->service_id].handler[msg->opcode - 1];
+
+	/* if payload size is valid */
+	if ((handler->var_len && handler->data_len > msg->len) ||
+			(!handler->var_len && handler->data_len != msg->len)) {
+		error("IPC: size invalid opcode 0x%x service 0x%x, terminating",
+						msg->service_id, msg->opcode);
+		raise(SIGTERM);
+		return;
+	}
+
+	handler->handler(msg->payload, msg->len);
+}
diff --git a/android/ipc.h b/android/ipc.h
index f66c9e0..9d0c5e1 100644
--- a/android/ipc.h
+++ b/android/ipc.h
@@ -21,6 +21,11 @@
  *
  */
 
+struct ipc_handler {
+	void (*handler) (const void *buf, uint16_t len);
+	bool var_len;
+	size_t data_len;
+};
 void ipc_init(int command_sk, int notification_sk);
 void ipc_cleanup(void);
 
@@ -29,3 +34,8 @@ void ipc_send_rsp_full(uint8_t service_id, uint8_t opcode, uint16_t len,
 							void *param, int fd);
 void ipc_send_notif(uint8_t service_id, uint8_t opcode,  uint16_t len,
 								void *param);
+void ipc_register(uint8_t service, const struct ipc_handler *handlers,
+								uint8_t size);
+void ipc_unregister(uint8_t service);
+
+void ipc_handle_msg(const void *buf, ssize_t len);
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 2/9] android/main: Use generic IPC message handling for core service
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on daemon start and unregistered on shutdown.
---
 android/main.c | 83 +++++++++++-----------------------------------------------
 1 file changed, 16 insertions(+), 67 deletions(-)

diff --git a/android/main.c b/android/main.c
index eedca58..0816ec7 100644
--- a/android/main.c
+++ b/android/main.c
@@ -74,9 +74,9 @@ static GIOChannel *hal_notif_io = NULL;
 
 static bool services[HAL_SERVICE_ID_MAX + 1] = { false };
 
-static void service_register(void *buf, uint16_t len)
+static void service_register(const void *buf, uint16_t len)
 {
-	struct hal_cmd_register_module *m = buf;
+	const struct hal_cmd_register_module *m = buf;
 
 	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id])
 		goto failed;
@@ -122,9 +122,9 @@ failed:
 							HAL_STATUS_FAILED);
 }
 
-static void service_unregister(void *buf, uint16_t len)
+static void service_unregister(const void *buf, uint16_t len)
 {
-	struct hal_cmd_unregister_module *m = buf;
+	const struct hal_cmd_unregister_module *m = buf;
 
 	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id])
 		goto failed;
@@ -164,20 +164,12 @@ failed:
 							HAL_STATUS_FAILED);
 }
 
-static void handle_service_core(uint8_t opcode, void *buf, uint16_t len)
-{
-	switch (opcode) {
-	case HAL_OP_REGISTER_MODULE:
-		service_register(buf, len);
-		break;
-	case HAL_OP_UNREGISTER_MODULE:
-		service_unregister(buf, len);
-		break;
-	default:
-		ipc_send_rsp(HAL_SERVICE_ID_CORE, opcode, HAL_STATUS_FAILED);
-		break;
-	}
-}
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_REGISTER_MODULE */
+	{ service_register, false, sizeof(struct hal_cmd_register_module) },
+	/* HAL_OP_UNREGISTER_MODULE */
+	{ service_unregister, false, sizeof(struct hal_cmd_unregister_module) },
+};
 
 static void bluetooth_stopped(void)
 {
@@ -211,7 +203,6 @@ static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
 							gpointer user_data)
 {
 	char buf[BLUEZ_HAL_MTU];
-	struct hal_hdr *msg = (void *) buf;
 	ssize_t ret;
 	int fd;
 
@@ -229,51 +220,7 @@ static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
 		goto fail;
 	}
 
-	if (ret < (ssize_t) sizeof(*msg)) {
-		error("HAL command too small, terminating (%zd)", ret);
-		goto fail;
-	}
-
-	if (ret != (ssize_t) (sizeof(*msg) + msg->len)) {
-		error("Malformed HAL command (%zd bytes), terminating", ret);
-		goto fail;
-	}
-
-	DBG("service_id %u opcode %u len %u", msg->service_id, msg->opcode,
-								msg->len);
-
-	if (msg->service_id > HAL_SERVICE_ID_MAX ||
-						!services[msg->service_id]) {
-		error("HAL command for unregistered service %u, terminating",
-							msg->service_id);
-		goto fail;
-	}
-
-	switch (msg->service_id) {
-	case HAL_SERVICE_ID_CORE:
-		handle_service_core(msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_BLUETOOTH:
-		bt_bluetooth_handle_cmd(fd, msg->opcode, msg->payload,
-								msg->len);
-		break;
-	case HAL_SERVICE_ID_HIDHOST:
-		bt_hid_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_SOCK:
-		bt_sock_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_A2DP:
-		bt_a2dp_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_PAN:
-		bt_pan_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	default:
-		ipc_send_rsp(msg->service_id, msg->opcode, HAL_STATUS_FAILED);
-		break;
-	}
-
+	ipc_handle_msg(buf, ret);
 	return TRUE;
 
 fail:
@@ -568,9 +515,6 @@ int main(int argc, char *argv[])
 	GError *err = NULL;
 	guint signal;
 
-	/* Core Service (ID=0) should always be considered registered */
-	services[0] = true;
-
 	context = g_option_context_new(NULL);
 	g_option_context_add_main_entries(context, options, NULL);
 
@@ -622,6 +566,9 @@ int main(int argc, char *argv[])
 	/* Use params: mtu = 0, flags = 0 */
 	start_sdp_server(0, 0);
 
+	ipc_register(HAL_SERVICE_ID_CORE, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+
 	DBG("Entering main loop");
 
 	event_loop = g_main_loop_new(NULL, FALSE);
@@ -640,6 +587,8 @@ int main(int argc, char *argv[])
 	bt_bluetooth_cleanup();
 	g_main_loop_unref(event_loop);
 
+	ipc_unregister(HAL_SERVICE_ID_CORE);
+
 	info("Exit");
 
 	__btd_log_cleanup();
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 3/9] android/main: Use common exit path in core service functions
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

This makes functions exit path simpler.
---
 android/main.c | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/android/main.c b/android/main.c
index 0816ec7..c0f8901 100644
--- a/android/main.c
+++ b/android/main.c
@@ -77,9 +77,12 @@ static bool services[HAL_SERVICE_ID_MAX + 1] = { false };
 static void service_register(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_register_module *m = buf;
+	uint8_t status;
 
-	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id])
+	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id]) {
+		status = HAL_STATUS_FAILED;
 		goto failed;
+	}
 
 	switch (m->service_id) {
 	case HAL_SERVICE_ID_BLUETOOTH:
@@ -91,43 +94,51 @@ static void service_register(const void *buf, uint16_t len)
 
 		break;
 	case HAL_SERVICE_ID_HIDHOST:
-		if (!bt_hid_register(&adapter_bdaddr))
+		if (!bt_hid_register(&adapter_bdaddr)) {
+			status = HAL_STATUS_FAILED;
 			goto failed;
+		}
 
 		break;
 	case HAL_SERVICE_ID_A2DP:
-		if (!bt_a2dp_register(&adapter_bdaddr))
+		if (!bt_a2dp_register(&adapter_bdaddr)) {
+			status = HAL_STATUS_FAILED;
 			goto failed;
+		}
 
 		break;
 	case HAL_SERVICE_ID_PAN:
-		if (!bt_pan_register(&adapter_bdaddr))
+		if (!bt_pan_register(&adapter_bdaddr)) {
+			status = HAL_STATUS_FAILED;
 			goto failed;
+		}
 
 		break;
 	default:
 		DBG("service %u not supported", m->service_id);
+		status = HAL_STATUS_FAILED;
 		goto failed;
 	}
 
 	services[m->service_id] = true;
 
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
-							HAL_STATUS_SUCCESS);
+	status = HAL_STATUS_SUCCESS;
 
 	info("Service ID=%u registered", m->service_id);
-	return;
+
 failed:
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
-							HAL_STATUS_FAILED);
+	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE, status);
 }
 
 static void service_unregister(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_unregister_module *m = buf;
+	uint8_t status;
 
-	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id])
+	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id]) {
+		status = HAL_STATUS_FAILED;
 		goto failed;
+	}
 
 	switch (m->service_id) {
 	case HAL_SERVICE_ID_BLUETOOTH:
@@ -149,19 +160,18 @@ static void service_unregister(const void *buf, uint16_t len)
 		/* This would indicate bug in HAL, as unregister should not be
 		 * called in init failed */
 		DBG("service %u not supported", m->service_id);
+		status = HAL_STATUS_FAILED;
 		goto failed;
 	}
 
 	services[m->service_id] = false;
 
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
-							HAL_STATUS_SUCCESS);
+	status = HAL_STATUS_SUCCESS;
 
 	info("Service ID=%u unregistered", m->service_id);
-	return;
+
 failed:
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
-							HAL_STATUS_FAILED);
+	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE, status);
 }
 
 static const struct ipc_handler cmd_handlers[] = {
-- 
1.8.3.2


^ permalink raw reply related


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