mirror of
https://git.kernel.org/pub/scm/bluetooth/bluez.git
synced 2024-11-27 06:04:32 +08:00
e1debdf2d4
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.
245 lines
4.9 KiB
C
245 lines
4.9 KiB
C
/*
|
|
* 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 <pthread.h>
|
|
#include <errno.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <stdbool.h>
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <unistd.h>
|
|
#include <stdint.h>
|
|
|
|
#include <cutils/properties.h>
|
|
|
|
#include "hal-msg.h"
|
|
#include "hal-log.h"
|
|
#include "hal-ipc.h"
|
|
|
|
#define CONNECT_TIMEOUT (5 * 1000)
|
|
#define SERVICE_NAME "bluetoothd"
|
|
|
|
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;
|
|
struct pollfd pfd;
|
|
int new_sk;
|
|
|
|
memset(&pfd, 0 , sizeof(pfd));
|
|
pfd.fd = sk;
|
|
pfd.events = POLLIN;
|
|
|
|
err = poll(&pfd, 1, CONNECT_TIMEOUT);
|
|
if (err < 0) {
|
|
err = errno;
|
|
error("Failed to poll: %d (%s)", err, strerror(err));
|
|
return -1;
|
|
}
|
|
|
|
if (err == 0) {
|
|
error("bluetoothd connect timeout");
|
|
return -1;
|
|
}
|
|
|
|
new_sk = accept(sk, NULL, NULL);
|
|
if (new_sk < 0) {
|
|
err = errno;
|
|
error("Failed to accept socket: %d (%s)", err, strerror(err));
|
|
return -1;
|
|
}
|
|
|
|
return new_sk;
|
|
}
|
|
|
|
bool hal_ipc_init(void)
|
|
{
|
|
struct sockaddr_un addr;
|
|
int sk;
|
|
int err;
|
|
|
|
sk = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
|
|
if (sk < 0) {
|
|
err = errno;
|
|
error("Failed to create socket: %d (%s)", err,
|
|
strerror(err));
|
|
return false;
|
|
}
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH));
|
|
|
|
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
err = errno;
|
|
error("Failed to bind socket: %d (%s)", err, strerror(err));
|
|
close(sk);
|
|
return false;
|
|
}
|
|
|
|
if (listen(sk, 2) < 0) {
|
|
err = errno;
|
|
error("Failed to listen on socket: %d (%s)", err,
|
|
strerror(err));
|
|
close(sk);
|
|
return false;
|
|
}
|
|
|
|
/* Start Android Bluetooth daemon service */
|
|
property_set("ctl.start", SERVICE_NAME);
|
|
|
|
cmd_sk = accept_connection(sk);
|
|
if (cmd_sk < 0) {
|
|
close(sk);
|
|
return false;
|
|
}
|
|
|
|
notif_sk = accept_connection(sk);
|
|
if (notif_sk < 0) {
|
|
close(sk);
|
|
close(cmd_sk);
|
|
cmd_sk = -1;
|
|
return false;
|
|
}
|
|
|
|
info("bluetoothd connected");
|
|
|
|
close(sk);
|
|
|
|
return true;
|
|
}
|
|
|
|
void hal_ipc_cleanup(void)
|
|
{
|
|
close(cmd_sk);
|
|
cmd_sk = -1;
|
|
|
|
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;
|
|
}
|