diff --git a/android/socket.c b/android/socket.c
index b505836..872ed6a 100644
--- a/android/socket.c
+++ b/android/socket.c
GList *connections = NULL;
struct rfcomm_sock {
- int fd; /* descriptor for communication with Java framework */
- int real_sock; /* real RFCOMM socket */
int channel; /* RFCOMM channel */
- guint rfcomm_watch;
- guint stack_watch;
+ /* for socket to BT */
+ int bt_sock;
+ guint bt_watch;
+
+ /* for socket to HAL */
+ int jv_sock;
+ guint jv_watch;
bdaddr_t dst;
uint32_t service_handle;
socklen_t len = sizeof(int);
int rcv, snd, size, err;
- err = getsockopt(rfsock->real_sock, SOL_SOCKET, SO_RCVBUF, &rcv, &len);
+ err = getsockopt(rfsock->bt_sock, SOL_SOCKET, SO_RCVBUF, &rcv, &len);
if (err < 0) {
error("getsockopt(SO_RCVBUF): %s", strerror(errno));
return -errno;
}
- err = getsockopt(rfsock->real_sock, SOL_SOCKET, SO_SNDBUF, &snd, &len);
+ err = getsockopt(rfsock->bt_sock, SOL_SOCKET, SO_SNDBUF, &snd, &len);
if (err < 0) {
error("getsockopt(SO_SNDBUF): %s", strerror(errno));
return -errno;
{
struct rfcomm_sock *rfsock = data;
- DBG("rfsock: %p fd %d real_sock %d chan %u",
- rfsock, rfsock->fd, rfsock->real_sock, rfsock->channel);
+ DBG("rfsock: %p jv_sock %d bt_sock %d chan %u",
+ rfsock, rfsock->jv_sock, rfsock->bt_sock, rfsock->channel);
- if (rfsock->fd >= 0)
- if (close(rfsock->fd) < 0)
- error("close() fd %d failed: %s", rfsock->fd,
+ if (rfsock->jv_sock >= 0)
+ if (close(rfsock->jv_sock) < 0)
+ error("close() fd %d failed: %s", rfsock->jv_sock,
strerror(errno));
- if (rfsock->real_sock >= 0)
- if (close(rfsock->real_sock) < 0)
- error("close() fd %d: failed: %s", rfsock->real_sock,
+ if (rfsock->bt_sock >= 0)
+ if (close(rfsock->bt_sock) < 0)
+ error("close() fd %d: failed: %s", rfsock->bt_sock,
strerror(errno));
- if (rfsock->rfcomm_watch > 0)
- if (!g_source_remove(rfsock->rfcomm_watch))
- error("rfcomm_watch source was not found");
+ if (rfsock->bt_watch > 0)
+ if (!g_source_remove(rfsock->bt_watch))
+ error("bt_watch source was not found");
- if (rfsock->stack_watch > 0)
- if (!g_source_remove(rfsock->stack_watch))
+ if (rfsock->jv_watch > 0)
+ if (!g_source_remove(rfsock->jv_watch))
error("stack_watch source was not found");
if (rfsock->service_handle)
g_free(rfsock);
}
-static struct rfcomm_sock *create_rfsock(int sock, int *hal_fd)
+static struct rfcomm_sock *create_rfsock(int bt_sock, int *hal_sock)
{
int fds[2] = {-1, -1};
struct rfcomm_sock *rfsock;
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
error("socketpair(): %s", strerror(errno));
- *hal_fd = -1;
+ *hal_sock = -1;
return NULL;
}
rfsock = g_new0(struct rfcomm_sock, 1);
- rfsock->fd = fds[0];
- *hal_fd = fds[1];
- rfsock->real_sock = sock;
+ rfsock->jv_sock = fds[0];
+ *hal_sock = fds[1];
+ rfsock->bt_sock = bt_sock;
- if (sock < 0)
+ if (bt_sock < 0)
return rfsock;
if (rfsock_set_buffer(rfsock) < 0) {
return sent;
}
-static gboolean sock_stack_event_cb(GIOChannel *io, GIOCondition cond,
+static gboolean jv_sock_client_event_cb(GIOChannel *io, GIOCondition cond,
gpointer data)
{
struct rfcomm_sock *rfsock = data;
goto fail;
}
- len = read(rfsock->fd, rfsock->buf, rfsock->buf_size);
+ len = read(rfsock->jv_sock, rfsock->buf, rfsock->buf_size);
if (len <= 0) {
error("read(): %s", strerror(errno));
/* Read again */
return TRUE;
}
- sent = try_write_all(rfsock->real_sock, rfsock->buf, len);
+ sent = try_write_all(rfsock->bt_sock, rfsock->buf, len);
if (sent < 0) {
error("write(): %s", strerror(errno));
goto fail;
return FALSE;
}
-static gboolean sock_rfcomm_event_cb(GIOChannel *io, GIOCondition cond,
+static gboolean bt_sock_event_cb(GIOChannel *io, GIOCondition cond,
gpointer data)
{
struct rfcomm_sock *rfsock = data;
goto fail;
}
- len = read(rfsock->real_sock, rfsock->buf, rfsock->buf_size);
+ len = read(rfsock->bt_sock, rfsock->buf, rfsock->buf_size);
if (len <= 0) {
error("read(): %s", strerror(errno));
/* Read again */
return TRUE;
}
- sent = try_write_all(rfsock->fd, rfsock->buf, len);
+ sent = try_write_all(rfsock->jv_sock, rfsock->buf, len);
if (sent < 0) {
error("write(): %s", strerror(errno));
goto fail;
cmd.channel = rfsock->channel;
cmd.status = 0;
- len = bt_sock_send_fd(rfsock->fd, &cmd, sizeof(cmd), fd_accepted);
+ len = bt_sock_send_fd(rfsock->jv_sock, &cmd, sizeof(cmd), fd_accepted);
if (len != sizeof(cmd)) {
error("Error sending accept signal");
return false;
return true;
}
-static gboolean sock_server_stack_event_cb(GIOChannel *io, GIOCondition cond,
+static gboolean jv_sock_server_event_cb(GIOChannel *io, GIOCondition cond,
gpointer data)
{
struct rfcomm_sock *rfsock = data;
static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
{
struct rfcomm_sock *rfsock = user_data;
- struct rfcomm_sock *rfsock_acc;
- GIOChannel *io_stack;
+ struct rfcomm_sock *new_rfsock;
+ GIOChannel *jv_io;
GError *gerr = NULL;
bdaddr_t dst;
char address[18];
- int sock_acc;
- int hal_fd;
+ int new_sock;
+ int hal_sock;
guint id;
GIOCondition cond;
ba2str(&dst, address);
DBG("Incoming connection from %s rfsock %p", address, rfsock);
- sock_acc = g_io_channel_unix_get_fd(io);
- rfsock_acc = create_rfsock(sock_acc, &hal_fd);
- if (!rfsock_acc) {
+ new_sock = g_io_channel_unix_get_fd(io);
+ new_rfsock = create_rfsock(new_sock, &hal_sock);
+ if (!new_rfsock) {
g_io_channel_shutdown(io, TRUE, NULL);
return;
}
- DBG("rfsock: fd %d real_sock %d chan %u sock %d",
- rfsock->fd, rfsock->real_sock, rfsock->channel,
- sock_acc);
+ DBG("rfsock: jv_sock %d bt_sock %d chan %u new_sock %d",
+ rfsock->jv_sock, rfsock->bt_sock, rfsock->channel,
+ new_sock);
- if (!sock_send_accept(rfsock, &dst, hal_fd)) {
- cleanup_rfsock(rfsock_acc);
+ if (!sock_send_accept(rfsock, &dst, hal_sock)) {
+ cleanup_rfsock(new_rfsock);
return;
}
- connections = g_list_append(connections, rfsock_acc);
+ connections = g_list_append(connections, new_rfsock);
/* Handle events from Android */
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
- io_stack = g_io_channel_unix_new(rfsock_acc->fd);
- id = g_io_add_watch(io_stack, cond, sock_stack_event_cb, rfsock_acc);
- g_io_channel_unref(io_stack);
+ jv_io = g_io_channel_unix_new(new_rfsock->jv_sock);
+ id = g_io_add_watch(jv_io, cond, jv_sock_client_event_cb, new_rfsock);
+ g_io_channel_unref(jv_io);
- rfsock_acc->stack_watch = id;
+ new_rfsock->jv_watch = id;
/* Handle rfcomm events */
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
- id = g_io_add_watch(io, cond, sock_rfcomm_event_cb, rfsock_acc);
+ id = g_io_add_watch(io, cond, bt_sock_event_cb, new_rfsock);
g_io_channel_set_close_on_unref(io, FALSE);
- rfsock_acc->rfcomm_watch = id;
+ new_rfsock->bt_watch = id;
- DBG("rfsock %p rfsock_acc %p stack_watch %d rfcomm_watch %d",
- rfsock, rfsock_acc, rfsock_acc->stack_watch,
- rfsock_acc->rfcomm_watch);
+ DBG("rfsock %p rfsock_acc %p jv_watch %d bt_watch %d",
+ rfsock, new_rfsock, new_rfsock->jv_watch,
+ new_rfsock->bt_watch);
}
static uint8_t rfcomm_listen(int chan, const uint8_t *name, const uint8_t *uuid,
- uint8_t flags, int *hal_fd)
+ uint8_t flags, int *hal_sock)
{
const struct profile_info *profile;
struct rfcomm_sock *rfsock = NULL;
BtIOSecLevel sec_level;
- GIOChannel *io, *io_stack;
+ GIOChannel *io, *jv_io;
GIOCondition cond;
GError *err = NULL;
guint id;
DBG("rfcomm channel %d svc_name %s", chan, name);
- rfsock = create_rfsock(-1, hal_fd);
+ rfsock = create_rfsock(-1, hal_sock);
if (!rfsock)
return HAL_STATUS_FAILED;
goto failed;
}
- rfsock->real_sock = g_io_channel_unix_get_fd(io);
+ rfsock->bt_sock = g_io_channel_unix_get_fd(io);
g_io_channel_set_close_on_unref(io, FALSE);
g_io_channel_unref(io);
/* Handle events from Android */
cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
- io_stack = g_io_channel_unix_new(rfsock->fd);
- id = g_io_add_watch_full(io_stack, G_PRIORITY_HIGH, cond,
- sock_server_stack_event_cb, rfsock,
+ jv_io = g_io_channel_unix_new(rfsock->jv_sock);
+ id = g_io_add_watch_full(jv_io, G_PRIORITY_HIGH, cond,
+ jv_sock_server_event_cb, rfsock,
NULL);
- g_io_channel_unref(io_stack);
+ g_io_channel_unref(jv_io);
- rfsock->stack_watch = id;
+ rfsock->jv_watch = id;
- DBG("real_sock %d fd %d hal_fd %d", rfsock->real_sock, rfsock->fd,
- *hal_fd);
+ DBG("bt_sock %d jv_sock %d hal_sock %d", rfsock->bt_sock,
+ rfsock->jv_sock, *hal_sock);
- if (write(rfsock->fd, &chan, sizeof(chan)) != sizeof(chan)) {
+ if (write(rfsock->jv_sock, &chan, sizeof(chan)) != sizeof(chan)) {
error("Error sending RFCOMM channel");
goto failed;
}
failed:
cleanup_rfsock(rfsock);
- close(*hal_fd);
+ close(*hal_sock);
return HAL_STATUS_FAILED;
}
{
const struct hal_cmd_sock_listen *cmd = buf;
uint8_t status;
- int hal_fd;
+ int hal_sock;
switch (cmd->type) {
case HAL_SOCK_RFCOMM:
status = rfcomm_listen(cmd->channel, cmd->name, cmd->uuid,
- cmd->flags, &hal_fd);
+ cmd->flags, &hal_sock);
break;
case HAL_SOCK_SCO:
case HAL_SOCK_L2CAP:
goto failed;
ipc_send_rsp_full(HAL_SERVICE_ID_SOCK, HAL_OP_SOCK_LISTEN, 0, NULL,
- hal_fd);
- close(hal_fd);
+ hal_sock);
+ close(hal_sock);
return ;
failed:
cmd.channel = rfsock->channel;
cmd.status = 0;
- len = write(rfsock->fd, &cmd, sizeof(cmd));
+ len = write(rfsock->jv_sock, &cmd, sizeof(cmd));
if (len < 0) {
error("%s", strerror(errno));
return false;
{
struct rfcomm_sock *rfsock = user_data;
bdaddr_t *dst = &rfsock->dst;
- GIOChannel *io_stack;
+ GIOChannel *jv_io;
char address[18];
guint id;
GIOCondition cond;
ba2str(dst, address);
DBG("Connected to %s", address);
- DBG("rfsock: fd %d real_sock %d chan %u sock %d",
- rfsock->fd, rfsock->real_sock, rfsock->channel,
+ DBG("rfsock: jv_sock %d bt_sock %d chan %u sock %d",
+ rfsock->jv_sock, rfsock->bt_sock, rfsock->channel,
g_io_channel_unix_get_fd(io));
if (!sock_send_connect(rfsock, dst))
/* Handle events from Android */
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
- io_stack = g_io_channel_unix_new(rfsock->fd);
- id = g_io_add_watch(io_stack, cond, sock_stack_event_cb, rfsock);
- g_io_channel_unref(io_stack);
+ jv_io = g_io_channel_unix_new(rfsock->jv_sock);
+ id = g_io_add_watch(jv_io, cond, jv_sock_client_event_cb, rfsock);
+ g_io_channel_unref(jv_io);
- rfsock->stack_watch = id;
+ rfsock->jv_watch = id;
/* Handle rfcomm events */
cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
- id = g_io_add_watch(io, cond, sock_rfcomm_event_cb, rfsock);
+ id = g_io_add_watch(io, cond, bt_sock_event_cb, rfsock);
g_io_channel_set_close_on_unref(io, FALSE);
- rfsock->rfcomm_watch = id;
+ rfsock->bt_watch = id;
return;
fail:
g_io_channel_set_close_on_unref(io, FALSE);
g_io_channel_unref(io);
- if (write(rfsock->fd, &chan, sizeof(chan)) != sizeof(chan)) {
+ if (write(rfsock->jv_sock, &chan, sizeof(chan)) != sizeof(chan)) {
error("Error sending RFCOMM channel");
return false;
}
- rfsock->real_sock = g_io_channel_unix_get_fd(io);
+ rfsock->bt_sock = g_io_channel_unix_get_fd(io);
rfsock_set_buffer(rfsock);
rfsock->channel = chan;
connections = g_list_append(connections, rfsock);
}
static uint8_t connect_rfcomm(const bdaddr_t *addr, int chan,
- const uint8_t *uuid, uint8_t flags, int *hal_fd)
+ const uint8_t *uuid, uint8_t flags,
+ int *hal_sock)
{
struct rfcomm_sock *rfsock;
uuid_t uu;
return HAL_STATUS_INVALID;
}
- rfsock = create_rfsock(-1, hal_fd);
+ rfsock = create_rfsock(-1, hal_sock);
if (!rfsock)
return HAL_STATUS_FAILED;
failed:
cleanup_rfsock(rfsock);
- close(*hal_fd);
+ close(*hal_sock);
return HAL_STATUS_FAILED;
}
const struct hal_cmd_sock_connect *cmd = buf;
bdaddr_t bdaddr;
uint8_t status;
- int hal_fd;
+ int hal_sock;
DBG("");
switch (cmd->type) {
case HAL_SOCK_RFCOMM:
status = connect_rfcomm(&bdaddr, cmd->channel, cmd->uuid,
- cmd->flags, &hal_fd);
+ cmd->flags, &hal_sock);
break;
case HAL_SOCK_SCO:
case HAL_SOCK_L2CAP:
goto failed;
ipc_send_rsp_full(HAL_SERVICE_ID_SOCK, HAL_OP_SOCK_CONNECT, 0, NULL,
- hal_fd);
- close(hal_fd);
+ hal_sock);
+ close(hal_sock);
return;
failed: