Linux bluetooth development
 help / color / mirror / Atom feed
* [RFC 3/6] android: Use hal-log.h for logging in hidhost HAL
From: Szymon Janc @ 2013-10-20 17:45 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382291146-23518-1-git-send-email-szymon.janc@tieto.com>

This will allow to log both on Android and Linux (in haltest)
---
 android/hal-hidhost.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/android/hal-hidhost.c b/android/hal-hidhost.c
index 7436864..8c47e27 100644
--- a/android/hal-hidhost.c
+++ b/android/hal-hidhost.c
@@ -21,9 +21,7 @@
 #include <hardware/bluetooth.h>
 #include <hardware/bt_hh.h>
 
-#define LOG_TAG "BlueZ"
-#include <cutils/log.h>
-
+#include "hal-log.h"
 #include "hal.h"
 
 bthh_callbacks_t *bt_hh_cbacks;
@@ -35,7 +33,7 @@ static bool interface_ready(void)
 
 static bt_status_t bt_hidhost_connect(bt_bdaddr_t *bd_addr)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -48,7 +46,7 @@ static bt_status_t bt_hidhost_connect(bt_bdaddr_t *bd_addr)
 
 static bt_status_t bt_hidhost_disconnect(bt_bdaddr_t *bd_addr)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -61,7 +59,7 @@ static bt_status_t bt_hidhost_disconnect(bt_bdaddr_t *bd_addr)
 
 static bt_status_t bt_hidhost_virtual_unplug(bt_bdaddr_t *bd_addr)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -75,7 +73,7 @@ static bt_status_t bt_hidhost_virtual_unplug(bt_bdaddr_t *bd_addr)
 static bt_status_t bt_hidhost_set_info(bt_bdaddr_t *bd_addr,
 						bthh_hid_info_t hid_info)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -89,7 +87,7 @@ static bt_status_t bt_hidhost_set_info(bt_bdaddr_t *bd_addr,
 static bt_status_t bt_hidhost_get_protocol(bt_bdaddr_t *bd_addr,
 					bthh_protocol_mode_t protocolMode)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -103,7 +101,7 @@ static bt_status_t bt_hidhost_get_protocol(bt_bdaddr_t *bd_addr,
 static bt_status_t bt_hidhost_set_protocol(bt_bdaddr_t *bd_addr,
 					bthh_protocol_mode_t protocolMode)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -119,7 +117,7 @@ static bt_status_t bt_hidhost_get_report(bt_bdaddr_t *bd_addr,
 						uint8_t reportId,
 						int bufferSize)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -134,7 +132,7 @@ static bt_status_t bt_hidhost_set_report(bt_bdaddr_t *bd_addr,
 						bthh_report_type_t reportType,
 						char *report)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -147,7 +145,7 @@ static bt_status_t bt_hidhost_set_report(bt_bdaddr_t *bd_addr,
 
 static bt_status_t bt_hidhost_send_data(bt_bdaddr_t *bd_addr, char *data)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -160,7 +158,7 @@ static bt_status_t bt_hidhost_send_data(bt_bdaddr_t *bd_addr, char *data)
 
 static bt_status_t bt_hidhost_init(bthh_callbacks_t *callbacks)
 {
-	ALOGI(__func__);
+	DBG("");
 
 	/* store reference to user callbacks */
 	bt_hh_cbacks = callbacks;
@@ -174,7 +172,7 @@ static bt_status_t bt_hidhost_init(bthh_callbacks_t *callbacks)
 
 static void bt_hidhost_cleanup(void)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return;
-- 
1.8.4


^ permalink raw reply related

* [RFC 4/6] android: Use hal-log.h for logging in pan HAL
From: Szymon Janc @ 2013-10-20 17:45 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382291146-23518-1-git-send-email-szymon.janc@tieto.com>

This will allow to log both on Android and Linux (in haltest)
---
 android/hal-pan.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/android/hal-pan.c b/android/hal-pan.c
index f63e83d..645fe8c 100644
--- a/android/hal-pan.c
+++ b/android/hal-pan.c
@@ -21,9 +21,7 @@
 #include <hardware/bluetooth.h>
 #include <hardware/bt_pan.h>
 
-#define LOG_TAG "BlueZ"
-#include <cutils/log.h>
-
+#include "hal-log.h"
 #include "hal.h"
 
 const btpan_callbacks_t *bt_pan_cbacks = NULL;
@@ -35,7 +33,7 @@ static bool interface_ready(void)
 
 static bt_status_t bt_pan_enable(int local_role)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -45,7 +43,7 @@ static bt_status_t bt_pan_enable(int local_role)
 
 static int bt_pan_get_local_role(void)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BTPAN_ROLE_NONE;
@@ -56,7 +54,7 @@ static int bt_pan_get_local_role(void)
 static bt_status_t bt_pan_connect(const bt_bdaddr_t *bd_addr, int local_role,
 					int remote_role)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -69,7 +67,7 @@ static bt_status_t bt_pan_connect(const bt_bdaddr_t *bd_addr, int local_role,
 
 static bt_status_t bt_pan_disconnect(const bt_bdaddr_t *bd_addr)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
@@ -82,7 +80,7 @@ static bt_status_t bt_pan_disconnect(const bt_bdaddr_t *bd_addr)
 
 static bt_status_t bt_pan_init(const btpan_callbacks_t *callbacks)
 {
-	ALOGI(__func__);
+	DBG("");
 
 	bt_pan_cbacks = callbacks;
 
@@ -95,7 +93,7 @@ static bt_status_t bt_pan_init(const btpan_callbacks_t *callbacks)
 
 static void bt_pan_cleanup()
 {
-	ALOGD(__func__);
+	DBG("");
 
 	if (!interface_ready())
 		return;
-- 
1.8.4


^ permalink raw reply related

* [RFC 5/6] android: Use hal-log.h for logging in socket HAL
From: Szymon Janc @ 2013-10-20 17:45 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382291146-23518-1-git-send-email-szymon.janc@tieto.com>

This will allow to log both on Android and Linux (in haltest)
---
 android/hal-sock.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/android/hal-sock.c b/android/hal-sock.c
index 31e6b59..dab3756 100644
--- a/android/hal-sock.c
+++ b/android/hal-sock.c
@@ -20,16 +20,14 @@
 #include <hardware/bluetooth.h>
 #include <hardware/bt_sock.h>
 
-#define LOG_TAG "BlueZ"
-#include <cutils/log.h>
-
+#include "hal-log.h"
 #include "hal.h"
 
 static bt_status_t btsock_listen_rfcomm(const char *service_name,
 					const uint8_t *uuid, int chan,
 					int *sock, int flags)
 {
-	ALOGD(__func__);
+	DBG("");
 
 	return BT_STATUS_UNSUPPORTED;
 }
@@ -39,20 +37,20 @@ static bt_status_t listen(btsock_type_t type, const char *service_name,
 					int *sock, int flags)
 {
 	if ((!uuid && chan <= 0) || !sock) {
-		ALOGE("%s: invalid params: uuid %p, chan %d, sock %p",
+		error("%s: invalid params: uuid %p, chan %d, sock %p",
 						__func__, uuid, chan, sock);
 		return BT_STATUS_PARM_INVALID;
 	}
 
-	ALOGD("%s: uuid %p chan %d sock %p type %d service_name %s",
-			__func__, uuid, chan, sock, type, service_name);
+	DBG("uuid %p chan %d sock %p type %d service_name %s",
+					uuid, chan, sock, type, service_name);
 
 	switch (type) {
 	case BTSOCK_RFCOMM:
 		return btsock_listen_rfcomm(service_name, uuid, chan,
 								sock, flags);
 	default:
-		ALOGE("%s: Socket type %d not supported", __func__, type);
+		error("%s: Socket type %d not supported", __func__, type);
 		break;
 	}
 
@@ -64,13 +62,12 @@ static bt_status_t connect(const bt_bdaddr_t *bdaddr, btsock_type_t type,
 					int *sock, int flags)
 {
 	if ((!uuid && chan <= 0) || !bdaddr || !sock) {
-		ALOGE("invalid params: bd_addr %p, uuid %p, chan %d, sock %p",
+		error("invalid params: bd_addr %p, uuid %p, chan %d, sock %p",
 					bdaddr, uuid, chan, sock);
 		return BT_STATUS_PARM_INVALID;
 	}
 
-	ALOGD("%s: uuid %p chan %d sock %p type %d", __func__, uuid, chan,
-								sock, type);
+	DBG("uuid %p chan %d sock %p type %d", uuid, chan, sock, type);
 
 	return BT_STATUS_UNSUPPORTED;
 }
-- 
1.8.4


^ permalink raw reply related

* [RFC 6/6] android: Remove not needed cutils/log.h stub
From: Szymon Janc @ 2013-10-20 17:45 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382291146-23518-1-git-send-email-szymon.janc@tieto.com>

---
 Makefile.android     |  2 --
 android/cutils/log.h | 29 -----------------------------
 2 files changed, 31 deletions(-)
 delete mode 100644 android/cutils/log.h

diff --git a/Makefile.android b/Makefile.android
index e9992d9..8196583 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -31,7 +31,6 @@ android_libhal_internal_la_SOURCES = android/hal.h android/hal-bluetooth.c \
 					android/hardware/bt_rc.h \
 					android/hardware/bt_sock.h \
 					android/hardware/hardware.h \
-					android/cutils/log.h \
 					android/cutils/properties.h \
 					android/hal-log.h
 
@@ -93,5 +92,4 @@ EXTRA_DIST += android/hardware/bluetooth.h \
 		android/hardware/bt_rc.h \
 		android/hardware/bt_sock.h \
 		android/hardware/hardware.h \
-		android/cutils/log.h \
 		android/cutils/properties.h
diff --git a/android/cutils/log.h b/android/cutils/log.h
deleted file mode 100644
index 389542b..0000000
--- a/android/cutils/log.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _CUTILS_LOG_H
-#define _CUTILS_LOG_H
-
-static inline void ALOG() {};
-
-#define ALOGV(...) ALOG("V", __VA_ARGS__)
-#define ALOGD(...) ALOG("D", __VA_ARGS__)
-#define ALOGI(...) ALOG("I", __VA_ARGS__)
-#define ALOGW(...) ALOG("W", __VA_ARGS__)
-#define ALOGE(...) ALOG("E", __VA_ARGS__)
-#define LOG_ALWAYS_FATAL(...)   do { ALOGE(__VA_ARGS__); exit(1); } while (0)
-
-#endif // _CUTILS_LOG_H
-- 
1.8.4


^ permalink raw reply related

* Re: [PATCH v2 0/6] Initial Android IPC
From: Johan Hedberg @ 2013-10-20 19:37 UTC (permalink / raw)
  To: Szymon Janc; +Cc: linux-bluetooth
In-Reply-To: <1382262838-25725-1-git-send-email-szymon.janc@tieto.com>

Hi Szymon,

On Sun, Oct 20, 2013, Szymon Janc wrote:
> V2:
> - simplify daemon code
> - sockets handling cleanup
> - other minor fixes
> 
> -- 
> BR
> Szymon Janc
> 
> Szymon Janc (6):
>   android: Fix missing log cleanup on daemon exit
>   android: Improve cutils/log.h stubs
>   android: Define path for HAL communication socket
>   android: Connect daemon to HAL library
>   android: Make HAL library wait for daemon to connect on init
>   android: Remove not needed property_get function form cutils stubs
> 
>  android/cutils/log.h        |  10 ++--
>  android/cutils/properties.h |  27 ----------
>  android/hal-bluetooth.c     | 126 ++++++++++++++++++++++++++++++++++++--------
>  android/hal-msg.h           |   2 +
>  android/main.c              | 124 +++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 234 insertions(+), 55 deletions(-)

All six patches have been applied. Thanks.

Johan

^ permalink raw reply

* Re: [RFC 0/6] Logging from Android HAL library
From: Johan Hedberg @ 2013-10-20 19:40 UTC (permalink / raw)
  To: Szymon Janc; +Cc: linux-bluetooth
In-Reply-To: <1382291146-23518-1-git-send-email-szymon.janc@tieto.com>

Hi Szymon,

On Sun, Oct 20, 2013, Szymon Janc wrote:
> This is an attempt to make library logging API similar to other parts of code.
> It maps log levels used in BlueZ to Android levels and provides log printing
> when HAL library is used by haltest on Linux. Since cutils/log.h is not used
> directly there is no need to stub it on Linux.
> 
> Another advantage is that LOG_TAG and message format is configured in single
> file (eg. modules tags could be easily added to improve readibility of
> non-debug messages)
> 
> Comments are welcome.
> 
> Szymon Janc (6):
>   android: Add wrapper for HAL logging
>   android: Use hal-log.h for logging in bluetooth HAL
>   android: Use hal-log.h for logging in hidhost HAL
>   android: Use hal-log.h for logging in pan HAL
>   android: Use hal-log.h for logging in socket HAL
>   android: Remove not needed cutils/log.h stub
> 
>  Makefile.android        |  9 ++++---
>  android/cutils/log.h    | 29 ---------------------
>  android/hal-bluetooth.c | 68 ++++++++++++++++++++++++-------------------------
>  android/hal-hidhost.c   | 26 +++++++++----------
>  android/hal-log.h       | 35 +++++++++++++++++++++++++
>  android/hal-pan.c       | 16 +++++-------
>  android/hal-sock.c      | 19 ++++++--------
>  7 files changed, 100 insertions(+), 102 deletions(-)
>  delete mode 100644 android/cutils/log.h
>  create mode 100644 android/hal-log.h

Even though you flagged this as RFC it looked fine to me and I like the
idea that we don't need to wrap our brains to different logging
conventions depending on which side of the IPC we are. So all patches
have been applied. Thanks.

Johan

^ permalink raw reply

* [PATCH BlueZ] android: Add audio plugin protocol to API doc
From: Luiz Augusto von Dentz @ 2013-10-21  7:59 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds audio plugin IPC is used to communicate Android BlueZ daemon
and audio plugin.
---
 android/hal-ipc-api.txt | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/android/hal-ipc-api.txt b/android/hal-ipc-api.txt
index 9a8b770..bc8a128 100644
--- a/android/hal-ipc-api.txt
+++ b/android/hal-ipc-api.txt
@@ -1245,3 +1245,79 @@ Android HAL name: "gatt" (BT_PROFILE_GATT_ID)
 	Opcode 0x9c - Request Write notification
 	Opcode 0x9d - Request Execute Write notification
 	Opcode 0x9e - Response Confirmation notification
+
+Bluetooth Audio Plugin (ID = 10)
+================================
+
+The audio plugin happen to be in a different socket but all the rules for
+HAL socket apply here as well.
+
+	.--Android--.                             .---Audio---.
+	|  daemon   |                             |   Plugin  |
+	|           |          Command            |           |
+	|           | <-------------------------- |           |
+	|           | --------------------------> |           |
+	|           |          Response           |           |
+	|           |                             |           |
+	|           |                             |           |
+	|           |        Notification         |           |
+	|           | --------------------------> |           |
+	|           |                             |           |
+	'-----------'                             '-----------'
+
+Android HAL name: "audio" (BT_AUDIO_ID)
+
+	Opcode 0x00 - Error response
+
+		Response parameters: Status (1 octet)
+
+		Valid status values: 0x01 = Fail
+		                     0x02 = Not ready
+		                     0x03 = No memory
+		                     0x04 = Busy
+		                     0x05 = Done (already completed)
+		                     0x06 = Unsupported
+		                     0x07 = Parameter invalid
+		                     0x08 = Unhandled
+		                     0x09 = Authentication failure
+		                     0x0a = Remote device down
+
+	Opcode 0x01 - Register Audio Endpoint commmand
+
+		Command parameters: Service UUID (16 octets)
+		                    Codec ID (1 octets)
+		                    Codec capabilities length (1 octets)
+		                    Codec capabilities (variable)
+		                    Codec configuration length (1 octets)
+		                    Codec configuration (variable)
+		Response parameters: Endpoint ID (1 octets)
+
+	Opcode 0x02 - Unregister Audio Endpoint command
+
+		Command parameters: Endpoint ID (1 octets)
+
+	Opcode 0x03 - Resume Audio command
+
+		Command parameters: Endpoint ID (1 octets)
+
+	Opcode 0x04 - Suspend Audio command
+
+		Command parameters: Endpoint ID (1 octets)
+
+Notifications:
+
+	Opcode 0x80 - Audio State Changed notification
+
+		Notification paremters: Endpoint ID (1 octets)
+		                        State (1 octets)
+
+		Valid state values: 0x01 = Idle
+		                    0x02 = Suspended
+		                    0x03 = Running
+
+	Opcode 0x81 - New Audio Connection notification
+
+		Notification parameters: Endpoint ID (1 octets)
+		                         Codec configuration length (1 octets)
+		                         Codec configuration (variable)
+		                         File descriptor (inline)
-- 
1.8.3.1


^ permalink raw reply related

* [PATCH 1/4] android: Add missing error status description for Core Service
From: Szymon Janc @ 2013-10-21  8:08 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

We need at least one failed status description.
---
 android/hal-ipc-api.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/android/hal-ipc-api.txt b/android/hal-ipc-api.txt
index 9a8b770..5d8e085 100644
--- a/android/hal-ipc-api.txt
+++ b/android/hal-ipc-api.txt
@@ -117,7 +117,9 @@ Core Service (ID 0)
 
 	Opcode 0x00 - Error response
 
-		Response parameters: Error (1 octet)
+		Response parameters: Status (1 octet)
+
+		Valid status values: 0x01 = Fail
 
 	Opcode 0x01 - Register module command/response
 
-- 
1.8.4


^ permalink raw reply related

* [PATCH 2/4] android:hal: Add initial code for sending commands
From: Szymon Janc @ 2013-10-21  8:08 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382342936-10331-1-git-send-email-szymon.janc@tieto.com>

This will be used by all HAL modules to send commands and get response
from daemon. In case of any protocol error negative value is returned.
---
 android/hal-bluetooth.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
 android/hal.h           |  3 +++
 2 files changed, 57 insertions(+)

diff --git a/android/hal-bluetooth.c b/android/hal-bluetooth.c
index 589cb1b..de07108 100644
--- a/android/hal-bluetooth.c
+++ b/android/hal-bluetooth.c
@@ -23,6 +23,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <poll.h>
+#include <pthread.h>
 
 #include <hardware/bluetooth.h>
 #include <hardware/bt_sock.h>
@@ -43,6 +44,59 @@ bt_callbacks_t *bt_hal_cbacks = NULL;
 static int cmd_sk = -1;
 static int notif_sk = -1;
 
+static pthread_mutex_t cmd_sk_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len,
+							const void *param)
+{
+	int ret;
+	int msg_len = sizeof(struct hal_msg_hdr) + len;
+	char buf[msg_len];
+	struct hal_msg_hdr *msg = (struct hal_msg_hdr *)buf;
+	struct {
+		struct hal_msg_hdr hdr;
+		struct hal_msg_rsp_error error;
+	} __attribute__((packed)) rsp;
+
+	if (cmd_sk < 0)
+		return -EBADF;
+
+	msg->service_id = service_id;
+	msg->opcode = opcode;
+	msg->len = len;
+
+	if (len > 0)
+		memcpy(msg + sizeof(msg), param, len);
+
+	memset(&rsp, 0, sizeof(rsp));
+
+	pthread_mutex_lock(&cmd_sk_mutex );
+
+	if (write(cmd_sk, msg, msg_len) != msg_len) {
+		pthread_mutex_unlock(&cmd_sk_mutex );
+		return -EIO;
+	}
+
+	ret = read(cmd_sk, &rsp, sizeof(rsp));
+
+	pthread_mutex_unlock(&cmd_sk_mutex );
+
+	if (ret < (int)sizeof(rsp.hdr))
+		return -EIO;
+
+	ret -= sizeof(rsp.hdr);
+
+	if (rsp.hdr.opcode == opcode && ret == 0)
+		return 0;
+
+	if (ret != sizeof(rsp.error))
+		return -EIO;
+
+	ret = rsp.error.status;
+
+	return ret;
+}
+
 static bool interface_ready(void)
 {
 	return bt_hal_cbacks != NULL;
diff --git a/android/hal.h b/android/hal.h
index a548e48..cf6e1f7 100644
--- a/android/hal.h
+++ b/android/hal.h
@@ -22,3 +22,6 @@
 btsock_interface_t *bt_get_sock_interface(void);
 bthh_interface_t *bt_get_hidhost_interface(void);
 btpan_interface_t *bt_get_pan_interface(void);
+
+int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len,
+							const void *param);
-- 
1.8.4


^ permalink raw reply related

* [PATCH 3/4] android:hal: Add support for receiving fd in command response
From: Szymon Janc @ 2013-10-21  8:08 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382342936-10331-1-git-send-email-szymon.janc@tieto.com>

If fd is expected together with response it will be stored into passed
pointer.
---
 android/hal-bluetooth.c | 42 ++++++++++++++++++++++++++++++++++++++++--
 android/hal.h           |  2 +-
 2 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/android/hal-bluetooth.c b/android/hal-bluetooth.c
index de07108..4174fcf 100644
--- a/android/hal-bluetooth.c
+++ b/android/hal-bluetooth.c
@@ -46,8 +46,43 @@ static int notif_sk = -1;
 
 static pthread_mutex_t cmd_sk_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+static int read_fd(int sk, void *buf, int size, int *fd)
+{
+	struct msghdr msg;
+	struct iovec iv;
+	struct cmsghdr *cmsg;
+	char cmsgbuf[CMSG_SPACE(sizeof(int))];
+	int ret;
+
+	*fd = -1;
+
+	memset(&msg, 0, sizeof(msg));
+	memset(cmsgbuf, 0, sizeof(cmsgbuf));
+
+	iv.iov_base = buf;
+	iv.iov_len = size;
+
+	msg.msg_iov = &iv;
+	msg.msg_iovlen = 1;
+
+	msg.msg_control = cmsgbuf;
+	msg.msg_controllen = sizeof(cmsgbuf);
+
+	ret = recvmsg(sk, &msg, 0);
+	if(ret < 0)
+		return -EIO;
+
+	cmsg = CMSG_FIRSTHDR(&msg);
+	if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
+			cmsg->cmsg_type == SCM_RIGHTS &&
+			cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
+		memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
+
+	return ret;
+}
+
 int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len,
-							const void *param)
+						const void *param, int *fd)
 {
 	int ret;
 	int msg_len = sizeof(struct hal_msg_hdr) + len;
@@ -77,7 +112,10 @@ int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len,
 		return -EIO;
 	}
 
-	ret = read(cmd_sk, &rsp, sizeof(rsp));
+	if (fd)
+		ret = read_fd(cmd_sk, &rsp, sizeof(rsp), fd);
+	else
+		ret = read(cmd_sk, &rsp, sizeof(rsp));
 
 	pthread_mutex_unlock(&cmd_sk_mutex );
 
diff --git a/android/hal.h b/android/hal.h
index cf6e1f7..8062971 100644
--- a/android/hal.h
+++ b/android/hal.h
@@ -24,4 +24,4 @@ bthh_interface_t *bt_get_hidhost_interface(void);
 btpan_interface_t *bt_get_pan_interface(void);
 
 int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len,
-							const void *param);
+						const void *param, int *fd);
-- 
1.8.4


^ permalink raw reply related

* [PATCH 4/4] android:hal: Register adapter and socket interface on HAL init
From: Szymon Janc @ 2013-10-21  8:08 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382342936-10331-1-git-send-email-szymon.janc@tieto.com>

After daemon is started HAL needs to register those two interfaces
to performe initialization procedure as described in IPC documentation.
---
 android/hal-bluetooth.c | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/android/hal-bluetooth.c b/android/hal-bluetooth.c
index 4174fcf..6af6a6b 100644
--- a/android/hal-bluetooth.c
+++ b/android/hal-bluetooth.c
@@ -46,7 +46,7 @@ static int notif_sk = -1;
 
 static pthread_mutex_t cmd_sk_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-static int read_fd(int sk, void *buf, int size, int *fd)
+static int read_with_fd(int sk, void *buf, int size, int *fd)
 {
 	struct msghdr msg;
 	struct iovec iv;
@@ -72,11 +72,13 @@ static int read_fd(int sk, void *buf, int size, int *fd)
 	if(ret < 0)
 		return -EIO;
 
-	cmsg = CMSG_FIRSTHDR(&msg);
-	if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
-			cmsg->cmsg_type == SCM_RIGHTS &&
-			cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
-		memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
+	/* Receive auxiliary data in msg */
+	for (cmsg = CMSG_FIRSTHDR(&msg); !cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
+		if (cmsg->cmsg_level == SOL_SOCKET
+				&& cmsg->cmsg_type == SCM_RIGHTS) {
+			memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
+			break;
+		}
 
 	return ret;
 }
@@ -113,7 +115,7 @@ int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len,
 	}
 
 	if (fd)
-		ret = read_fd(cmd_sk, &rsp, sizeof(rsp), fd);
+		ret = read_with_fd(cmd_sk, &rsp, sizeof(rsp), fd);
 	else
 		ret = read(cmd_sk, &rsp, sizeof(rsp));
 
@@ -241,6 +243,8 @@ static void stop_daemon(void)
 
 static int init(bt_callbacks_t *callbacks)
 {
+	struct hal_msg_cmd_register_module cmd;
+
 	DBG("");
 
 	if (interface_ready())
@@ -251,7 +255,25 @@ static int init(bt_callbacks_t *callbacks)
 
 	bt_hal_cbacks = callbacks;
 
+	cmd.service_id = HAL_SERVICE_ID_BLUETOOTH;
+
+	if (hal_send_msg(HAL_SERVICE_ID_CORE, HAL_MSG_OP_REGISTER_MODULE,
+						sizeof(cmd), &cmd, NULL) != 0 )
+		goto fail;
+
+	cmd.service_id = HAL_SERVICE_ID_SOCK;
+
+	if (hal_send_msg(HAL_SERVICE_ID_CORE, HAL_MSG_OP_REGISTER_MODULE,
+						sizeof(cmd), &cmd, NULL) != 0)
+		goto fail;
+
 	return BT_STATUS_SUCCESS;
+
+fail:
+	stop_daemon();
+	bt_hal_cbacks = NULL;
+	return BT_STATUS_FAIL;
+
 }
 
 static int enable(void)
-- 
1.8.4


^ permalink raw reply related

* [PATCH BlueZ] android: Add A2DP skeleton
From: Luiz Augusto von Dentz @ 2013-10-21  8:39 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds initial skeleton for A2DP Android HAL.
---
 Makefile.android |  1 +
 android/hal-av.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 android/hal.h    |  2 ++
 3 files changed, 97 insertions(+)
 create mode 100644 android/hal-av.c

diff --git a/Makefile.android b/Makefile.android
index 8196583..ec9e4be 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -18,6 +18,7 @@ android_libhal_internal_la_SOURCES = android/hal.h android/hal-bluetooth.c \
 					android/hal-sock.c \
 					android/hal-hidhost.c \
 					android/hal-pan.c \
+					android/hal-av.c \
 					android/hardware/bluetooth.h \
 					android/hardware/bt_av.h \
 					android/hardware/bt_gatt.h \
diff --git a/android/hal-av.c b/android/hal-av.c
new file mode 100644
index 0000000..ef45066
--- /dev/null
+++ b/android/hal-av.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+
+#include <hardware/bluetooth.h>
+#include <hardware/bt_av.h>
+
+#include "hal-log.h"
+#include "hal.h"
+
+const btav_callbacks_t *cbs = NULL;
+
+static bool interface_ready(void)
+{
+	return cbs != NULL;
+}
+
+static bt_status_t av_connect(bt_bdaddr_t *bd_addr)
+{
+	DBG("");
+
+	if (!interface_ready())
+		return BT_STATUS_NOT_READY;
+
+	if (!bd_addr)
+		return BT_STATUS_PARM_INVALID;
+
+	return BT_STATUS_UNSUPPORTED;
+}
+
+static bt_status_t av_disconnect(bt_bdaddr_t *bd_addr)
+{
+	DBG("");
+
+	if (!interface_ready())
+		return BT_STATUS_NOT_READY;
+
+	if (!bd_addr)
+		return BT_STATUS_PARM_INVALID;
+
+	return BT_STATUS_UNSUPPORTED;
+}
+
+static bt_status_t av_init(btav_callbacks_t *callbacks)
+{
+	DBG("");
+
+	cbs = callbacks;
+
+	/* TODO: enable service */
+
+	return BT_STATUS_SUCCESS;
+}
+
+static void av_cleanup()
+{
+	DBG("");
+
+	if (!interface_ready())
+		return;
+
+	/* TODO: disable service */
+
+	cbs = NULL;
+}
+
+static btav_interface_t iface = {
+	.size = sizeof(iface),
+	.init = av_init,
+	.connect = av_connect,
+	.disconnect = av_disconnect,
+	.cleanup = av_cleanup
+};
+
+btav_interface_t *bt_get_av_interface()
+{
+	return &iface;
+}
diff --git a/android/hal.h b/android/hal.h
index a548e48..d984336 100644
--- a/android/hal.h
+++ b/android/hal.h
@@ -18,7 +18,9 @@
 #include <hardware/bt_sock.h>
 #include <hardware/bt_hh.h>
 #include <hardware/bt_pan.h>
+#include <hardware/bt_av.h>
 
 btsock_interface_t *bt_get_sock_interface(void);
 bthh_interface_t *bt_get_hidhost_interface(void);
 btpan_interface_t *bt_get_pan_interface(void);
+btav_interface_t *bt_get_av_interface(void);
-- 
1.8.3.1


^ permalink raw reply related

* [PATCH BlueZ v2] android: Add A2DP skeleton
From: Luiz Augusto von Dentz @ 2013-10-21 10:27 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds initial skeleton for A2DP Android HAL.
---
v2: Adds hal-av.c to Android.mk

 Makefile.android   |  2 ++
 android/Android.mk |  1 +
 android/hal-av.c   | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 android/hal.h      |  2 ++
 4 files changed, 99 insertions(+)
 create mode 100644 android/hal-av.c

diff --git a/Makefile.android b/Makefile.android
index 8196583..f5a5834 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -18,6 +18,7 @@ android_libhal_internal_la_SOURCES = android/hal.h android/hal-bluetooth.c \
 					android/hal-sock.c \
 					android/hal-hidhost.c \
 					android/hal-pan.c \
+					android/hal-av.c \
 					android/hardware/bluetooth.h \
 					android/hardware/bt_av.h \
 					android/hardware/bt_gatt.h \
@@ -61,6 +62,7 @@ EXTRA_DIST += android/hal-bluetooth.c \
 		android/hal-sock.c \
 		android/hal-hidhost.c \
 		android/hal-pan.c \
+		android/hal-av.c \
 		android/hal-log.h
 
 EXTRA_DIST += android/client/terminal.c \
diff --git a/android/Android.mk b/android/Android.mk
index 30b2169..87445ed 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -55,6 +55,7 @@ LOCAL_SRC_FILES := \
 	hal-sock.c \
 	hal-hidhost.c \
 	hal-pan.c \
+	hal-av.c \
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
diff --git a/android/hal-av.c b/android/hal-av.c
new file mode 100644
index 0000000..ef45066
--- /dev/null
+++ b/android/hal-av.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+
+#include <hardware/bluetooth.h>
+#include <hardware/bt_av.h>
+
+#include "hal-log.h"
+#include "hal.h"
+
+const btav_callbacks_t *cbs = NULL;
+
+static bool interface_ready(void)
+{
+	return cbs != NULL;
+}
+
+static bt_status_t av_connect(bt_bdaddr_t *bd_addr)
+{
+	DBG("");
+
+	if (!interface_ready())
+		return BT_STATUS_NOT_READY;
+
+	if (!bd_addr)
+		return BT_STATUS_PARM_INVALID;
+
+	return BT_STATUS_UNSUPPORTED;
+}
+
+static bt_status_t av_disconnect(bt_bdaddr_t *bd_addr)
+{
+	DBG("");
+
+	if (!interface_ready())
+		return BT_STATUS_NOT_READY;
+
+	if (!bd_addr)
+		return BT_STATUS_PARM_INVALID;
+
+	return BT_STATUS_UNSUPPORTED;
+}
+
+static bt_status_t av_init(btav_callbacks_t *callbacks)
+{
+	DBG("");
+
+	cbs = callbacks;
+
+	/* TODO: enable service */
+
+	return BT_STATUS_SUCCESS;
+}
+
+static void av_cleanup()
+{
+	DBG("");
+
+	if (!interface_ready())
+		return;
+
+	/* TODO: disable service */
+
+	cbs = NULL;
+}
+
+static btav_interface_t iface = {
+	.size = sizeof(iface),
+	.init = av_init,
+	.connect = av_connect,
+	.disconnect = av_disconnect,
+	.cleanup = av_cleanup
+};
+
+btav_interface_t *bt_get_av_interface()
+{
+	return &iface;
+}
diff --git a/android/hal.h b/android/hal.h
index a548e48..d984336 100644
--- a/android/hal.h
+++ b/android/hal.h
@@ -18,7 +18,9 @@
 #include <hardware/bt_sock.h>
 #include <hardware/bt_hh.h>
 #include <hardware/bt_pan.h>
+#include <hardware/bt_av.h>
 
 btsock_interface_t *bt_get_sock_interface(void);
 bthh_interface_t *bt_get_hidhost_interface(void);
 btpan_interface_t *bt_get_pan_interface(void);
+btav_interface_t *bt_get_av_interface(void);
-- 
1.8.3.1


^ permalink raw reply related

* Re: [PATCH BlueZ v2] android: Add A2DP skeleton
From: Johan Hedberg @ 2013-10-21 10:44 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth
In-Reply-To: <1382351241-17513-1-git-send-email-luiz.dentz@gmail.com>

Hi Luiz,

On Mon, Oct 21, 2013, Luiz Augusto von Dentz wrote:
> This adds initial skeleton for A2DP Android HAL.
> ---
> v2: Adds hal-av.c to Android.mk
> 
>  Makefile.android   |  2 ++
>  android/Android.mk |  1 +
>  android/hal-av.c   | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  android/hal.h      |  2 ++
>  4 files changed, 99 insertions(+)
>  create mode 100644 android/hal-av.c

Applied. Thanks.

Johan

^ permalink raw reply

* [RFCv1 0/6] Set of patches implementing missing IPC functionality
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

This is my set of patches rebased against very recent bluez.git
(most probably there might be some issues related to style used).

These patches add some missing capability to the process, add basic IPC server
and client. Daemon listens for incoming connections from HALs so it acts
as a server, while HALs connect and act as client. Please advise what
are better names for hal-msg and hal-msg-client, should some code be moved
to adapter.c?

Andrei Emeltchenko (6):
  android: Add capabilities and set userid
  android: Handle mgmt changed events
  android: Implement basic HAL server
  android: Add HAL message helpers
  android: Add helper to send fd using SCM_RIGHTS
  android: Add Android HAL callback task

 Makefile.android         |    2 +-
 android/Android.mk       |    9 ++
 android/adapter.c        |  129 ++++++++++++++++++++++
 android/bt-sock.c        |   64 +++++++++++
 android/hal-cb-thread.c  |   86 +++++++++++++++
 android/hal-msg-client.c |  123 +++++++++++++++++++++
 android/hal-msg-client.h |   25 +++++
 android/hal-msg.c        |  267 ++++++++++++++++++++++++++++++++++++++++++++++
 android/hal-msg.h        |    5 +
 android/main.c           |   69 ++++++++++++
 configure.ac             |    4 +
 11 files changed, 782 insertions(+), 1 deletion(-)
 create mode 100644 android/bt-sock.c
 create mode 100644 android/hal-cb-thread.c
 create mode 100644 android/hal-msg-client.c
 create mode 100644 android/hal-msg-client.h
 create mode 100644 android/hal-msg.c

-- 
1.7.10.4


^ permalink raw reply

* [RFCv1 1/6] android: Add capabilities and set userid
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1382354917-8632-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

The patch set UID as standard Bluetooth user for Android (AID_BLUETOOTH).
For SDP server we need to bind to lower port, acquire this capability.
Other capabilities are required to access management interface.
---
 android/main.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure.ac   |    4 ++++
 2 files changed, 62 insertions(+)

diff --git a/android/main.c b/android/main.c
index fac1003..4a716be 100644
--- a/android/main.c
+++ b/android/main.c
@@ -33,10 +33,17 @@
 #include <stdbool.h>
 #include <string.h>
 #include <errno.h>
+#include <unistd.h>
+
 #include <sys/signalfd.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#if defined(ANDROID)
+#include <sys/prctl.h>
+#include <private/android_filesystem_config.h>
+#endif
+
 #include <glib.h>
 
 #include "log.h"
@@ -409,6 +416,54 @@ static void cleanup_hal_connection(void)
 	}
 }
 
+static bool set_capabilities(void)
+{
+#if defined(ANDROID)
+	struct __user_cap_header_struct header;
+	struct __user_cap_data_struct cap;
+	gid_t groups[] = {AID_NET_BT, AID_NET_BT_ADMIN, AID_NET_ADMIN};
+
+	DBG("pid %d uid %d gid %d", getpid(), getuid(), getgid());
+
+	header.version = _LINUX_CAPABILITY_VERSION;
+
+	prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
+
+	if (setgid(AID_BLUETOOTH) < 0)
+		warn("%s: setgid(): %s", __func__, strerror(errno));
+
+	if (setuid(AID_BLUETOOTH) < 0)
+		warn("%s: setuid(): %s", __func__, strerror(errno));
+
+	header.version = _LINUX_CAPABILITY_VERSION;
+	header.pid = 0;
+
+	cap.effective = cap.permitted =
+		CAP_TO_MASK(CAP_SETGID) |
+		CAP_TO_MASK(CAP_NET_RAW) |
+		CAP_TO_MASK(CAP_NET_ADMIN) |
+		CAP_TO_MASK(CAP_NET_BIND_SERVICE);
+	cap.inheritable = 0;
+
+	if (capset(&header, &cap) < 0) {
+		error("%s: capset(): %s", __func__, strerror(errno));
+		return false;
+	}
+
+	if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0)
+		warn("%s: setgroups: %s", __func__, strerror(errno));
+
+	if (capget(&header, &cap) < 0)
+		error("%s: capget(): %s", __func__, strerror(errno));
+	else
+		DBG("Caps: eff: 0x%x, perm: 0x%x, inh: 0x%x", cap.effective,
+					cap.permitted, cap.inheritable);
+
+	DBG("pid %d uid %d gid %d", getpid(), getuid(), getgid());
+#endif
+	return true;
+}
+
 int main(int argc, char *argv[])
 {
 	GOptionContext *context;
@@ -442,6 +497,9 @@ int main(int argc, char *argv[])
 
 	__btd_log_init("*", 0);
 
+	if (!set_capabilities())
+		return EXIT_FAILURE;
+
 	if (!init_mgmt_interface())
 		return EXIT_FAILURE;
 
diff --git a/configure.ac b/configure.ac
index b4d3998..e3b5220 100644
--- a/configure.ac
+++ b/configure.ac
@@ -247,4 +247,8 @@ AC_ARG_ENABLE(android, AC_HELP_STRING([--enable-android],
 					[enable_android=${enableval}])
 AM_CONDITIONAL(ANDROID, test "${enable_android}" = "yes")
 
+if (test "${enable_android}" = "yes"); then
+	AC_CHECK_LIB(cap, capget, dummy=yes, AC_MSG_ERROR(libcap is required))
+fi
+
 AC_OUTPUT(Makefile src/bluetoothd.8 lib/bluez.pc)
-- 
1.7.10.4


^ permalink raw reply related

* [RFCv1 2/6] android: Handle mgmt changed events
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1382354917-8632-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

Add code handling changing adapter settings.
---
 android/adapter.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/android/adapter.c b/android/adapter.c
index 4009f4d..cae3187 100644
--- a/android/adapter.c
+++ b/android/adapter.c
@@ -69,6 +69,134 @@ failed:
 	adapter->ready(NULL, err);
 }
 
+static void mgmt_local_name_changed_event(uint16_t index, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct bt_adapter *adapter = user_data;
+	const struct mgmt_cp_set_local_name *rp = param;
+
+	if (length < sizeof(*rp)) {
+		error("Wrong size of local name changed parameters");
+		return;
+	}
+
+	if (!g_strcmp0(adapter->name, (const char *) rp->name))
+		return;
+
+	DBG("name: %s", rp->name);
+
+	g_free(adapter->name);
+	adapter->name = g_strdup((const char *) rp->name);
+
+	/* TODO Update services if needed */
+}
+
+static void settings_changed_connectable(struct bt_adapter *adapter)
+{
+	/* TODO */
+}
+
+static void settings_changed_discoverable(struct bt_adapter *adapter)
+{
+	/* TODO */
+}
+
+static void settings_changed(struct bt_adapter *adapter, uint32_t settings)
+{
+	uint32_t changed_mask;
+
+	changed_mask = adapter->current_settings ^ settings;
+
+	adapter->current_settings = settings;
+
+	DBG("0x%08x", changed_mask);
+
+	if (changed_mask & MGMT_SETTING_POWERED) {
+		info("Powered");
+
+		/*
+		if (adapter->current_settings & MGMT_SETTING_POWERED)
+			start_adapter()
+		else
+			stop_adapter()
+		*/
+	}
+
+	if (changed_mask & MGMT_SETTING_CONNECTABLE) {
+		DBG("Connectable");
+
+		settings_changed_connectable(adapter);
+	}
+
+	if (changed_mask & MGMT_SETTING_DISCOVERABLE) {
+		DBG("Discoverable");
+
+		settings_changed_discoverable(adapter);
+	}
+}
+
+static void new_settings_callback(uint16_t index, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct bt_adapter *adapter = user_data;
+	uint32_t settings;
+
+	if (length < sizeof(settings)) {
+		error("Wrong size of new settings parameters");
+		return;
+	}
+
+	settings = bt_get_le32(param);
+
+	DBG("settings: 0x%8.8x -> 0x%8.8x", adapter->current_settings,
+								settings);
+
+	if (settings == adapter->current_settings)
+		return;
+
+	settings_changed(adapter, settings);
+}
+
+static void mgmt_dev_class_changed_event(uint16_t index, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct bt_adapter *adapter = user_data;
+	const struct mgmt_cod *rp = param;
+	uint32_t dev_class;
+
+	if (length < sizeof(*rp)) {
+		error("Wrong size of class of device changed parameters");
+		return;
+	}
+
+	dev_class = rp->val[0] | (rp->val[1] << 8) | (rp->val[2] << 16);
+
+	if (dev_class == adapter->dev_class)
+		return;
+
+	DBG("Class: 0x%06x", dev_class);
+
+	adapter->dev_class = dev_class;
+
+	/* TODO: Inform prop change: Class */
+
+	/* TODO: Gatt attrib set*/
+}
+
+static void register_mgmt_handlers(struct bt_adapter *adapter)
+{
+	mgmt_register(adapter->mgmt, MGMT_EV_NEW_SETTINGS, 0,
+			new_settings_callback, adapter, NULL);
+
+	mgmt_register(adapter->mgmt, MGMT_EV_CLASS_OF_DEV_CHANGED,
+			0, mgmt_dev_class_changed_event,
+			adapter, NULL);
+
+	mgmt_register(adapter->mgmt, MGMT_EV_LOCAL_NAME_CHANGED,
+			0, mgmt_local_name_changed_event,
+			adapter, NULL);
+}
+
 static void load_link_keys(struct bt_adapter *adapter, GSList *keys)
 {
 	struct mgmt_cp_load_link_keys *cp;
@@ -128,6 +256,7 @@ static void read_info_complete(uint8_t status, uint16_t length, const void *para
 	adapter->current_settings = btohs(rp->current_settings);
 
 	/* TODO: Register all event notification handlers */
+	register_mgmt_handlers(adapter);
 
 	load_link_keys(adapter, NULL);
 
-- 
1.7.10.4


^ permalink raw reply related

* [RFCv1 3/6] android: Implement basic HAL server
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1382354917-8632-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

Add basic HAL server on BlueZ daemon side. It will listen for messages
from Android HAL threads.
---
 Makefile.android   |    2 +-
 android/Android.mk |    1 +
 android/hal-msg.c  |  267 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 android/hal-msg.h  |    5 +
 android/main.c     |   11 +++
 5 files changed, 285 insertions(+), 1 deletion(-)
 create mode 100644 android/hal-msg.c

diff --git a/Makefile.android b/Makefile.android
index 8196583..96f3f22 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -3,7 +3,7 @@ noinst_PROGRAMS += android/bluetoothd
 
 android_bluetoothd_SOURCES =	android/main.c \
 				src/log.c \
-				android/hal-msg.h \
+				android/hal-msg.h android/hal-msg.c \
 				src/sdpd-database.c src/sdpd-server.c \
 				src/sdpd-service.c src/sdpd-request.c \
 				src/shared/util.h src/shared/util.c \
diff --git a/android/Android.mk b/android/Android.mk
index 30b2169..d5dfde7 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -16,6 +16,7 @@ LOCAL_SRC_FILES := \
 	main.c \
 	log.c \
 	adapter.c \
+	hal-msg.c \
 	../src/shared/mgmt.c \
 	../src/shared/util.c \
 	../src/sdpd-database.c \
diff --git a/android/hal-msg.c b/android/hal-msg.c
new file mode 100644
index 0000000..aa02236
--- /dev/null
+++ b/android/hal-msg.c
@@ -0,0 +1,267 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+
+#include <glib.h>
+
+#include "log.h"
+#include "hal-msg.h"
+
+static guint watch_id = 0;
+
+static uint8_t hal_register_module(struct hal_msg_hdr *msg)
+{
+	DBG("");
+
+	return 0;
+}
+
+static uint8_t hal_unregister_module(struct hal_msg_hdr *msg)
+{
+	DBG("");
+
+	return 0;
+}
+
+static uint8_t process_hal_service_chan(struct hal_msg_hdr *msg)
+{
+	uint8_t status = -1;
+
+	DBG("");
+
+	switch (msg->opcode) {
+	case HAL_MSG_OP_REGISTER_MODULE:
+		status = hal_register_module(msg);
+		break;
+	case HAL_MSG_OP_UNREGISTER_MODULE:
+		status = hal_unregister_module(msg);
+		break;
+	default:
+		error("%s: unrecognized command on service channel", __func__);
+		break;
+	}
+
+	return status;
+}
+
+static uint8_t sanity_check(struct hal_msg_hdr *msg)
+{
+	/* TODO: Add sanity check here */
+
+	return 0;
+}
+
+static uint8_t process_hal_msg(uint8_t *buf, int len)
+{
+	struct hal_msg_hdr *msg = (struct hal_msg_hdr *) buf;
+	uint8_t status;
+
+	DBG("");
+
+	status = sanity_check(msg);
+	if (status != 0)
+		return status;
+
+	if (msg->service_id == 0)
+		status = process_hal_service_chan(msg);
+
+	return status;
+}
+
+static gboolean io_session_event(GIOChannel *chan, GIOCondition cond,
+								gpointer data)
+{
+	struct hal_msg_hdr *hdr;
+	uint8_t buf[MAX_HAL_BUF_SIZE];
+	int sock, len;
+	uint8_t status;
+
+	if (cond & G_IO_NVAL)
+		return FALSE;
+
+	sock = g_io_channel_unix_get_fd(chan);
+
+	if (cond & (G_IO_HUP | G_IO_ERR)) {
+		error("%s: error condition %d", __func__, cond);
+		/* TODO: handle */
+		return FALSE;
+	}
+
+	len = recv(sock, buf, sizeof(buf), 0);
+	if (len <= 0) {
+		error("%s: recv(): %s", __func__, strerror(errno));
+		/* TODO: handle */
+		return FALSE;
+	}
+
+	if (len < (int) sizeof(struct hal_msg_hdr))
+		return FALSE;
+
+	status = process_hal_msg(buf, len);
+
+	hdr = (struct hal_msg_hdr *) buf;
+
+	if (status == 0) {
+		/* Success reply */
+		len =  send(sock, hdr, sizeof(*hdr), 0);
+		if (len != sizeof(hdr)) {
+			error("%s: send() rsp: %s", __func__, strerror(errno));
+			/* TODO: handle */
+			return FALSE;
+		}
+	} else {
+		struct hal_msg_rsp {
+			struct hal_msg_hdr hdr;
+			uint8_t status;
+		} rsp;
+
+		rsp.hdr.service_id = hdr->service_id;
+		rsp.hdr.opcode = HAL_MSG_OP_ERROR;
+		rsp.hdr.len = sizeof(rsp.status);
+		rsp.status = status;
+
+		len = send(sock, &rsp, sizeof(rsp), 0);
+		if (len != sizeof(rsp)) {
+			error("%s: send() rsp: %s", __func__, strerror(errno));
+			/* TODO: handle */
+			return FALSE;
+		}
+	}
+
+	return TRUE;
+}
+
+static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond,
+								gpointer data)
+{
+	GIOChannel *io;
+	int sock, nsk;
+	struct sockaddr_un addr;
+	socklen_t len = sizeof(addr);
+
+	DBG("");
+
+	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
+		return FALSE;
+
+	sock = g_io_channel_unix_get_fd(chan);
+
+	nsk = accept(sock, (struct sockaddr *) &addr, &len);
+	if (nsk < 0) {
+		error("%s: accept(): %s", __func__, strerror(errno));
+		return TRUE;
+	}
+
+	io = g_io_channel_unix_new(nsk);
+	g_io_channel_set_close_on_unref(io, TRUE);
+
+	g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+						io_session_event, data);
+
+	g_io_channel_unref(io);
+
+	return TRUE;
+}
+
+static int init_hal_socket(const char *sock_path)
+{
+	struct sockaddr_un addr;
+	int sock;
+	size_t len;
+
+	DBG("");
+
+	sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+	if (sock < 0) {
+		error("%s: socket(): %s", __func__, strerror(errno));
+		return sock;
+	}
+
+	len = strlen(sock_path);
+	if (len > sizeof(addr.sun_path) - 1) {
+		error("%s: too big socket name", __func__);
+		return -1;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strcpy(addr.sun_path, sock_path);
+
+	/* let fail for android */
+	if (unlink(addr.sun_path) < 0)
+		warn("%s: unlink() %s failed: %s", __func__, sock_path,
+							strerror(errno));
+
+	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		error("%s: bind(): %s", __func__, strerror(errno));
+		return -1;
+	}
+
+	if (listen(sock, 5) < 0) {
+		error("%s: listen(): %s", __func__, strerror(errno));
+		return -1;
+	}
+
+	chmod(sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+
+	return sock;
+}
+
+void start_hal_srv(const char *sock_path)
+{
+	GIOChannel *io;
+	int sock;
+
+	DBG("");
+
+	sock = init_hal_socket(sock_path);
+	if (sock < 0)
+		return;
+
+	io = g_io_channel_unix_new(sock);
+	g_io_channel_set_close_on_unref(io, TRUE);
+
+	watch_id = g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+							io_accept_event, &sock);
+}
+
+void stop_hal_srv(void)
+{
+	DBG("");
+
+	if (watch_id > 0)
+		g_source_remove(watch_id);
+
+	watch_id = 0;
+}
diff --git a/android/hal-msg.h b/android/hal-msg.h
index 4440fc8..f27662a 100644
--- a/android/hal-msg.h
+++ b/android/hal-msg.h
@@ -23,6 +23,9 @@
 
 static const char BLUEZ_HAL_SK_PATH[] = "\0bluez_hal_socket";
 
+void start_hal_srv(const char *sock_path);
+void stop_hal_srv(void);
+
 struct hal_msg_hdr {
 	uint8_t service_id;
 	uint8_t opcode;
@@ -40,6 +43,8 @@ struct hal_msg_hdr {
 #define HAL_SERVICE_ID_AVRCP		8
 #define HAL_SERVICE_ID_GATT		9
 
+#define MAX_HAL_BUF_SIZE	1024
+
 /* Core Service */
 
 #define HAL_MSG_OP_ERROR		0x00
diff --git a/android/main.c b/android/main.c
index 4a716be..99db09b 100644
--- a/android/main.c
+++ b/android/main.c
@@ -56,6 +56,14 @@
 #include "adapter.h"
 #include "hal-msg.h"
 
+#if defined(ANDROID)
+#define BT_HAL_SOCKET "/tmp/bt_hal"
+#else
+#define BT_HAL_SOCKET "/var/run/bt_hal"
+#endif
+
+#define SHUTDOWN_GRACE_SECONDS 10
+
 static GMainLoop *event_loop;
 static struct mgmt *mgmt_if = NULL;
 
@@ -505,6 +513,7 @@ int main(int argc, char *argv[])
 
 	/* Use params: mtu = 0, flags = 0 */
 	start_sdp_server(0, 0);
+	start_hal_srv(BT_HAL_SOCKET);
 
 	DBG("Entering main loop");
 
@@ -513,6 +522,8 @@ int main(int argc, char *argv[])
 	g_source_remove(signal);
 
 	cleanup_hal_connection();
+
+	stop_hal_srv();
 	stop_sdp_server();
 	cleanup_mgmt_interface();
 	g_main_loop_unref(event_loop);
-- 
1.7.10.4


^ permalink raw reply related

* [RFCv1 4/6] android: Add HAL message helpers
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1382354917-8632-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

Add helper to ease opening channel and sending commands.
---
 android/Android.mk       |    1 +
 android/hal-msg-client.c |  123 ++++++++++++++++++++++++++++++++++++++++++++++
 android/hal-msg-client.h |   25 ++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 android/hal-msg-client.c
 create mode 100644 android/hal-msg-client.h

diff --git a/android/Android.mk b/android/Android.mk
index d5dfde7..4bd4116 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -56,6 +56,7 @@ LOCAL_SRC_FILES := \
 	hal-sock.c \
 	hal-hidhost.c \
 	hal-pan.c \
+	hal-msg-client.c \
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
diff --git a/android/hal-msg-client.c b/android/hal-msg-client.c
new file mode 100644
index 0000000..43af9a1
--- /dev/null
+++ b/android/hal-msg-client.c
@@ -0,0 +1,123 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include <hardware/bluetooth.h>
+#include <cutils/sockets.h>
+#include <cutils/properties.h>
+
+#define LOG_TAG "BlueZ"
+#include <cutils/log.h>
+
+#include "hal-msg.h"
+#include "hal-msg-client.h"
+
+#define BLUEZ_SOCKET "/tmp/bt_hal"
+
+int open_hal_chan(void)
+{
+	int tries = 10;
+	int sock;
+
+	while (tries-- > 0) {
+		sock = socket_local_client(BLUEZ_SOCKET,
+					ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
+					SOCK_SEQPACKET);
+		if (sock < 0)
+			ALOGE("%s: Cannot open chan: %s", __func__,
+							strerror(errno));
+		else
+			return sock;
+
+		usleep(100000);
+	}
+
+	return sock;
+}
+
+static int hal_send(int sock, struct hal_msg_hdr *msg)
+{
+	int len, size;
+	uint8_t buf[MAX_HAL_BUF_SIZE];
+	struct hal_msg_rsp {
+		struct hal_msg_hdr hdr;
+		uint8_t status;
+	} *rsp;
+
+	if (sock < 0) {
+		ALOGE("%s: socket not ready", __func__);
+		return BT_STATUS_NOT_READY;
+	}
+
+	size = sizeof(*msg) + msg->len;
+	len = send(sock, msg, size, 0);
+	if (len != size) {
+		ALOGE("%s: send(): %s", __func__, strerror(errno));
+		close(sock);
+		return BT_STATUS_FAIL;
+	}
+
+	ALOGD("%s: Sent %d bytes", __func__, len);
+
+	len = recv(sock, &buf, sizeof(buf), 0);
+	if (len <= 0) {
+		ALOGE("%s: recv(): %s", __func__, strerror(errno));
+		return BT_STATUS_FAIL;
+	}
+
+	if (len == sizeof(struct hal_msg_hdr))
+		return BT_STATUS_SUCCESS;
+
+	rsp = (struct hal_msg_rsp *) buf;
+
+	ALOGE("%s: error returned: %u", __func__, rsp->status);
+
+	return rsp->status;
+}
+
+int hal_register_module(int sock, uint8_t service_id)
+{
+	struct hal_msg_hdr *hdr;
+	struct hal_msg_cmd_register_module *msg;
+	int err;
+
+	hdr = malloc(sizeof(*hdr) + sizeof(*msg));
+	if (hdr == NULL)
+		return BT_STATUS_NOMEM;
+
+	hdr->service_id = 0;
+	hdr->opcode = HAL_MSG_OP_REGISTER_MODULE;
+	hdr->len = sizeof(*msg);
+
+	msg = (struct  hal_msg_cmd_register_module *) hdr + sizeof(*hdr);
+	msg->service_id = service_id;
+
+	err = hal_send(sock, hdr);
+
+	free(hdr);
+	return err;
+}
diff --git a/android/hal-msg-client.h b/android/hal-msg-client.h
new file mode 100644
index 0000000..c620dea
--- /dev/null
+++ b/android/hal-msg-client.h
@@ -0,0 +1,25 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+int hal_register_module(int sock, uint8_t service_id);
+int open_hal_chan(void);
-- 
1.7.10.4


^ permalink raw reply related

* [RFCv1 5/6] android: Add helper to send fd using SCM_RIGHTS
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1382354917-8632-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

This is used in accept message to Android framework.
---
 android/Android.mk |    1 +
 android/bt-sock.c  |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
 create mode 100644 android/bt-sock.c

diff --git a/android/Android.mk b/android/Android.mk
index 4bd4116..786c9fb 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -57,6 +57,7 @@ LOCAL_SRC_FILES := \
 	hal-hidhost.c \
 	hal-pan.c \
 	hal-msg-client.c \
+	bt-sock.c \
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
diff --git a/android/bt-sock.c b/android/bt-sock.c
new file mode 100644
index 0000000..68dbe32
--- /dev/null
+++ b/android/bt-sock.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <errno.h>
+
+#include <cutils/log.h>
+
+int bt_sock_send_acc_fd(int sock_fd, const unsigned char *buf, int len,
+								int send_fd)
+{
+	ssize_t ret;
+	struct msghdr msg;
+	struct cmsghdr *cmsg;
+	struct iovec iv;
+	char msgbuf[CMSG_SPACE(1)];
+
+	ALOGD("%s: len %d sock_fd %d send_fd %d", __func__, len, sock_fd,
+								send_fd);
+
+	if (sock_fd == -1 || send_fd == -1)
+		return -1;
+
+	memset(&msg, 0, sizeof(msg));
+
+	msg.msg_control = msgbuf;
+	msg.msg_controllen = sizeof(msgbuf);
+	cmsg = CMSG_FIRSTHDR(&msg);
+	cmsg->cmsg_level = SOL_SOCKET;
+	cmsg->cmsg_type = SCM_RIGHTS;
+	cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
+	memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
+
+	iv.iov_base = (unsigned char *) buf;
+	iv.iov_len = len;
+
+	msg.msg_iov = &iv;
+	msg.msg_iovlen = 1;
+
+	ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
+	if (ret < 0) {
+		ALOGE("%s: sock_fd %d send_fd %d sendmsg ret %d errno %d %s",
+				__func__, sock_fd, send_fd, (int)ret, errno,
+				strerror(errno));
+		return ret;
+	}
+
+	return ret;
+}
-- 
1.7.10.4


^ permalink raw reply related

* [RFCv1 6/6] android: Add Android HAL callback task
From: Andrei Emeltchenko @ 2013-10-21 11:28 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1382354917-8632-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

Android HAL callback task listens for messages from BlueZ GPL daemon.
---
 android/Android.mk      |    6 ++++
 android/hal-cb-thread.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
 create mode 100644 android/hal-cb-thread.c

diff --git a/android/Android.mk b/android/Android.mk
index 786c9fb..c5c6e7b 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -58,9 +58,15 @@ LOCAL_SRC_FILES := \
 	hal-pan.c \
 	hal-msg-client.c \
 	bt-sock.c \
+	hal-cb-thread.c \
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
+	libglib \
+
+LOCAL_C_INCLUDES := \
+	$(call include-path-for, glib) \
+	$(call include-path-for, glib)/glib \
 
 LOCAL_MODULE := bluetooth.default
 LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
diff --git a/android/hal-cb-thread.c b/android/hal-cb-thread.c
new file mode 100644
index 0000000..1b1e228
--- /dev/null
+++ b/android/hal-cb-thread.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <errno.h>
+#include <sys/epoll.h>
+#include <hardware/bluetooth.h>
+#include <fcntl.h>
+
+#define LOG_TAG "BlueZ"
+#include <cutils/log.h>
+
+#include "hal-msg.h"
+
+#define HAL_CBACK(base, cb_function, ...)				\
+	do {								\
+		if (base && base->cb_function) {			\
+			ALOGI("CB %s->%s", #base, #cb_function);	\
+			base->cb_function(__VA_ARGS__);			\
+		} else {						\
+			ALOGE("%s: Callback is NULL", __func__);	\
+		}							\
+	} while (0)
+
+extern bt_callbacks_t *bt_hal_cbacks;
+
+static int epfd;
+
+static void bthal_thread_associate(void)
+{
+	ALOGI(__func__);
+
+	HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
+}
+
+static void bthal_thread_disassociate(void)
+{
+	ALOGI(__func__);
+
+	HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
+
+	bt_hal_cbacks = NULL;
+}
+
+void process_msg(struct hal_msg_hdr *msg)
+{
+	ALOGI(__func__);
+
+	/* TODO: Process messages from daemon */
+}
+
+int mainloop(void)
+{
+	return EXIT_SUCCESS;
+}
+
+void *hal_cb_task(void *arg)
+{
+	int sock;
+
+	sock = open_hal_chan();
+
+	bthal_thread_associate();
+
+	bthal_thread_disassociate();
+
+	return NULL;
+}
-- 
1.7.10.4


^ permalink raw reply related

* [PATCH v2 1/3] android: Add missing error status description for Core Service
From: Szymon Janc @ 2013-10-21 12:56 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

We need at least one failed status description.
---
 android/hal-ipc-api.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/android/hal-ipc-api.txt b/android/hal-ipc-api.txt
index 9a8b770..5d8e085 100644
--- a/android/hal-ipc-api.txt
+++ b/android/hal-ipc-api.txt
@@ -117,7 +117,9 @@ Core Service (ID 0)
 
 	Opcode 0x00 - Error response
 
-		Response parameters: Error (1 octet)
+		Response parameters: Status (1 octet)
+
+		Valid status values: 0x01 = Fail
 
 	Opcode 0x01 - Register module command/response
 
-- 
1.8.4


^ permalink raw reply related

* [PATCH v2 2/3] android/hal: Add initial code for sending commands
From: Szymon Janc @ 2013-10-21 12:56 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382360211-3565-1-git-send-email-szymon.janc@tieto.com>

This will be used by all HAL modules to send commands and get response
from daemon. In case of any protocol error abort.

If non-null fd pointer is passed auxiliary data with passed FD will be
received as part of response.
---
 android/hal-bluetooth.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++
 android/hal-msg.h       |   3 ++
 2 files changed, 111 insertions(+)

diff --git a/android/hal-bluetooth.c b/android/hal-bluetooth.c
index 589cb1b..3d3023f 100644
--- a/android/hal-bluetooth.c
+++ b/android/hal-bluetooth.c
@@ -23,6 +23,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <poll.h>
+#include <pthread.h>
 
 #include <hardware/bluetooth.h>
 #include <hardware/bt_sock.h>
@@ -43,6 +44,113 @@ bt_callbacks_t *bt_hal_cbacks = NULL;
 static int cmd_sk = -1;
 static int notif_sk = -1;
 
+static pthread_mutex_t cmd_sk_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len, void *param,
+					size_t rsp_len, void *rsp, int *fd)
+{
+	ssize_t ret;
+	struct msghdr msg;
+	struct iovec iv[2];
+	struct hal_msg_hdr hal_msg;
+	char cmsgbuf[CMSG_SPACE(sizeof(int))];
+
+	if (cmd_sk < 0)
+		return -EBADF;
+
+	memset(&msg, 0, sizeof(msg));
+	memset(&hal_msg, 0, sizeof(hal_msg));
+
+	hal_msg.service_id = service_id;
+	hal_msg.opcode = opcode;
+	hal_msg.len = len;
+
+	iv[0].iov_base = &hal_msg;
+	iv[0].iov_len = sizeof(hal_msg);
+
+	iv[1].iov_base = param;
+	iv[1].iov_len = len;
+
+	msg.msg_iov = iv;
+	msg.msg_iovlen = 2;
+
+	pthread_mutex_lock(&cmd_sk_mutex);
+
+	ret = sendmsg(cmd_sk, &msg, 0);
+	if (ret < 0) {
+		error("Sending command failed, aborting :%s", strerror(errno));
+		pthread_mutex_unlock(&cmd_sk_mutex);
+		abort();
+	}
+
+	memset(&msg, 0, sizeof(msg));
+	memset(&hal_msg, 0, sizeof(hal_msg));
+
+	iv[0].iov_base = &hal_msg;
+	iv[0].iov_len = sizeof(hal_msg);
+
+	iv[1].iov_base = rsp;
+	iv[1].iov_len = rsp_len;
+
+	msg.msg_iov = iv;
+	msg.msg_iovlen = 2;
+
+	if (fd) {
+		memset(cmsgbuf, 0, sizeof(cmsgbuf));
+		msg.msg_control = cmsgbuf;
+		msg.msg_controllen = sizeof(cmsgbuf);
+	}
+
+	ret = recvmsg(cmd_sk, &msg, 0);
+	if (ret < 0) {
+		error("Receiving command response failed, aborting :%s",
+							strerror(errno));
+		pthread_mutex_unlock(&cmd_sk_mutex);
+		abort();
+	}
+
+	pthread_mutex_unlock(&cmd_sk_mutex);
+
+	if (ret < (ssize_t)sizeof(hal_msg)) {
+		error("Too small response received(%zd bytes), aborting", ret);
+		abort();
+	}
+
+	if (ret != (ssize_t)(sizeof(hal_msg) + hal_msg.len)) {
+		error("Malformed response received(%zd bytes), aborting", ret);
+		abort();
+	}
+
+	if (hal_msg.opcode != opcode && hal_msg.opcode != HAL_MSG_OP_ERROR) {
+		error("Invalid opcode received (%u vs %u), aborting",
+						hal_msg.opcode, opcode);
+		abort();
+	}
+
+	if (hal_msg.opcode == HAL_MSG_OP_ERROR) {
+		struct hal_msg_rsp_error *err = rsp;
+		return -err->status;
+	}
+
+	/* Receive auxiliary data in msg */
+	if (fd) {
+		struct cmsghdr *cmsg;
+
+		*fd = -1;
+
+		for (cmsg = CMSG_FIRSTHDR(&msg); !cmsg;
+					cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+			if (cmsg->cmsg_level == SOL_SOCKET
+					&& cmsg->cmsg_type == SCM_RIGHTS) {
+				memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
+				break;
+			}
+		}
+	}
+
+	return hal_msg.len;
+}
+
 static bool interface_ready(void)
 {
 	return bt_hal_cbacks != NULL;
diff --git a/android/hal-msg.h b/android/hal-msg.h
index 4440fc8..a33df96 100644
--- a/android/hal-msg.h
+++ b/android/hal-msg.h
@@ -246,3 +246,6 @@ struct hal_msg_ev_bt_le_test_mode {
 	uint8_t  status;
 	uint16_t num_packets;
 } __attribute__((packed));
+
+int hal_send_msg(uint8_t service_id, uint8_t opcode, uint16_t len, void *param,
+					size_t rsp_len, void *rsp, int *fd);
-- 
1.8.4


^ permalink raw reply related

* [PATCH v2 3/3] android/hal: Register adapter and socket interface on HAL init
From: Szymon Janc @ 2013-10-21 12:56 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1382360211-3565-1-git-send-email-szymon.janc@tieto.com>

After daemon is started HAL needs to register those two interfaces
to performe initialization procedure as described in IPC documentation.
---
 android/hal-bluetooth.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/android/hal-bluetooth.c b/android/hal-bluetooth.c
index 3d3023f..c1e0261 100644
--- a/android/hal-bluetooth.c
+++ b/android/hal-bluetooth.c
@@ -257,6 +257,9 @@ static void stop_daemon(void)
 
 static int init(bt_callbacks_t *callbacks)
 {
+	struct hal_msg_cmd_register_module cmd;
+	struct hal_msg_rsp_error rsp;
+
 	DBG("");
 
 	if (interface_ready())
@@ -267,7 +270,27 @@ static int init(bt_callbacks_t *callbacks)
 
 	bt_hal_cbacks = callbacks;
 
+	cmd.service_id = HAL_SERVICE_ID_BLUETOOTH;
+
+	if (hal_send_msg(HAL_SERVICE_ID_CORE, HAL_MSG_OP_REGISTER_MODULE,
+						sizeof(cmd), &cmd,
+						sizeof(rsp), &rsp, NULL) < 0)
+		 goto fail;
+
+	cmd.service_id = HAL_SERVICE_ID_SOCK;
+
+	if (hal_send_msg(HAL_SERVICE_ID_CORE, HAL_MSG_OP_REGISTER_MODULE,
+						sizeof(cmd), &cmd,
+						sizeof(rsp), &rsp, NULL) < 0)
+		 goto fail;
+
 	return BT_STATUS_SUCCESS;
+
+fail:
+	stop_daemon();
+	bt_hal_cbacks = NULL;
+	return BT_STATUS_FAIL;
+
 }
 
 static int enable(void)
-- 
1.8.4


^ permalink raw reply related

* [PATCH BlueZ] core: Fix not forwarding errors caused by SDP search properly
From: Luiz Augusto von Dentz @ 2013-10-21 13:04 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

In case bt_search_service fails while processing (e.g. connection is
dropped) the error forward is a positive value leading
btd_service_connecting_complete to not change any state at all.

Futhermore the error from sdp_process was completely ignored which may
cause problems as well.
---
 src/sdp-client.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/sdp-client.c b/src/sdp-client.c
index 2789db6..1221f5e 100644
--- a/src/sdp-client.c
+++ b/src/sdp-client.c
@@ -196,14 +196,15 @@ static gboolean search_process_cb(GIOChannel *chan, GIOCondition cond,
 							gpointer user_data)
 {
 	struct search_context *ctxt = user_data;
-	int err = 0;
+	int err;
 
 	if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
-		err = EIO;
+		err = -EIO;
 		goto failed;
 	}
 
-	if (sdp_process(ctxt->session) < 0)
+	err = sdp_process(ctxt->session);
+	if (err < 0)
 		goto failed;
 
 	return TRUE;
-- 
1.8.3.1


^ 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