Linux bluetooth development
 help / color / mirror / Atom feed
From: Andrei Emeltchenko <Andrei.Emeltchenko.news@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [RFCv1 4/6] android: Add HAL message helpers
Date: Mon, 21 Oct 2013 14:28:35 +0300	[thread overview]
Message-ID: <1382354917-8632-5-git-send-email-Andrei.Emeltchenko.news@gmail.com> (raw)
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


  parent reply	other threads:[~2013-10-21 11:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-21 11:28 [RFCv1 0/6] Set of patches implementing missing IPC functionality Andrei Emeltchenko
2013-10-21 11:28 ` [RFCv1 1/6] android: Add capabilities and set userid Andrei Emeltchenko
2013-10-21 11:28 ` [RFCv1 2/6] android: Handle mgmt changed events Andrei Emeltchenko
2013-10-21 11:28 ` [RFCv1 3/6] android: Implement basic HAL server Andrei Emeltchenko
2013-10-21 11:28 ` Andrei Emeltchenko [this message]
2013-10-21 11:28 ` [RFCv1 5/6] android: Add helper to send fd using SCM_RIGHTS Andrei Emeltchenko
2013-10-21 11:28 ` [RFCv1 6/6] android: Add Android HAL callback task Andrei Emeltchenko

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=1382354917-8632-5-git-send-email-Andrei.Emeltchenko.news@gmail.com \
    --to=andrei.emeltchenko.news@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

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

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