From: Szymon Janc <szymon.janc@tieto.com>
To: linux-bluetooth@vger.kernel.org
Cc: Szymon Janc <szymon.janc@tieto.com>
Subject: [PATCH v3 02/11] android/hal: Add initial code for sending commands
Date: Mon, 21 Oct 2013 20:56:42 +0200 [thread overview]
Message-ID: <1382381811-28123-2-git-send-email-szymon.janc@tieto.com> (raw)
In-Reply-To: <1382381811-28123-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-ipc.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
android/hal-ipc.h | 3 ++
2 files changed, 110 insertions(+)
diff --git a/android/hal-ipc.c b/android/hal-ipc.c
index 6db67f2..5b77adf 100644
--- a/android/hal-ipc.c
+++ b/android/hal-ipc.c
@@ -37,6 +37,8 @@
static int cmd_sk = -1;
static int notif_sk = -1;
+static pthread_mutex_t cmd_sk_mutex = PTHREAD_MUTEX_INITIALIZER;
+
static int accept_connection(int sk)
{
int err;
@@ -135,3 +137,108 @@ void hal_ipc_cleanup(void)
close(notif_sk);
notif_sk = -1;
}
+
+int hal_ipc_cmd(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);
+ exit(EXIT_FAILURE);
+ }
+
+ 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);
+ exit(EXIT_FAILURE);
+ }
+
+ pthread_mutex_unlock(&cmd_sk_mutex);
+
+ if (ret < (ssize_t) sizeof(hal_msg)) {
+ error("Too small response received(%zd bytes), aborting", ret);
+ exit(EXIT_FAILURE);
+ }
+
+ if (ret != (ssize_t) (sizeof(hal_msg) + hal_msg.len)) {
+ error("Malformed response received(%zd bytes), aborting", ret);
+ exit(EXIT_FAILURE);
+ }
+
+ if (hal_msg.opcode != opcode && hal_msg.opcode != HAL_MSG_OP_ERROR) {
+ error("Invalid opcode received (%u vs %u), aborting",
+ hal_msg.opcode, opcode);
+ exit(EXIT_FAILURE);
+ }
+
+ 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;
+}
diff --git a/android/hal-ipc.h b/android/hal-ipc.h
index 2f71668..3834e7d 100644
--- a/android/hal-ipc.h
+++ b/android/hal-ipc.h
@@ -17,3 +17,6 @@
bool hal_ipc_init(void);
void hal_ipc_cleanup(void);
+
+int hal_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len, void *param,
+ size_t rsp_len, void *rsp, int *fd);
--
1.8.4
next prev parent reply other threads:[~2013-10-21 18:56 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-21 18:56 [PATCH v3 01/11] android/hal: Move IPC and sockets related code to separate file Szymon Janc
2013-10-21 18:56 ` Szymon Janc [this message]
2013-10-21 18:56 ` [PATCH v3 03/11] android/hal: Register adapter and socket interface on HAL init Szymon Janc
2013-10-21 18:56 ` [PATCH v3 04/11] android: Split command and notification socket watch Szymon Janc
2013-10-21 18:56 ` [PATCH v3 05/11] android: Define IPC MTU size Szymon Janc
2013-10-21 18:56 ` [PATCH v3 06/11] android: Add HAL_SERVICE_ID_MAX define Szymon Janc
2013-10-21 18:56 ` [PATCH v3 07/11] android: Add main_quit function Szymon Janc
2013-10-21 18:56 ` [PATCH v3 08/11] android: Add IPC helper for sending command responses and notification Szymon Janc
2013-10-21 18:56 ` [PATCH v3 09/11] android: Add IPC helper for convenient error sending Szymon Janc
2013-10-21 18:56 ` [PATCH v3 10/11] android: Add initial code for handling HAL commands Szymon Janc
2013-10-21 18:56 ` [PATCH v3 11/11] android: Implement core service functionality Szymon Janc
2013-10-21 19:58 ` [PATCH v3 01/11] android/hal: Move IPC and sockets related code to separate file Johan Hedberg
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=1382381811-28123-2-git-send-email-szymon.janc@tieto.com \
--to=szymon.janc@tieto.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