summaryrefslogtreecommitdiff
path: root/android/audio-ipc.c
blob: f4b55e36fdf3129768b668abd9f0c8c3d3200f24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2014  Intel Corporation. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; 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 <stddef.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include <glib.h>

#include "ipc.h"
#include "log.h"
#include "audio-msg.h"
#include "audio-ipc.h"

static GIOChannel *audio_io = NULL;

static struct service_handler service;

static gboolean audio_watch_cb(GIOChannel *io, GIOCondition cond,
							gpointer user_data)
{
	char buf[BLUEZ_AUDIO_MTU];
	ssize_t ret;
	int fd, err;

	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
		info("Audio IPC: command socket closed");
		goto fail;
	}

	fd = g_io_channel_unix_get_fd(io);

	ret = read(fd, buf, sizeof(buf));
	if (ret < 0) {
		error("Audio IPC: command read failed (%s)", strerror(errno));
		goto fail;
	}

	err = ipc_handle_msg(&service, AUDIO_SERVICE_ID, buf, ret);
	if (err < 0) {
		error("Audio IPC: failed to handle message (%s)",
							strerror(-err));
		goto fail;
	}

	return TRUE;

fail:
	audio_ipc_cleanup();
	return FALSE;
}

static gboolean audio_connect_cb(GIOChannel *io, GIOCondition cond,
							gpointer user_data)
{
	DBG("");

	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
		error("Audio IPC: socket connect failed");
		audio_ipc_cleanup();
		return FALSE;
	}

	cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;

	g_io_add_watch(audio_io, cond, audio_watch_cb, NULL);

	info("Audio IPC: successfully connected");

	return FALSE;
}

void audio_ipc_init(void)
{
	audio_io = ipc_connect(BLUEZ_AUDIO_SK_PATH, sizeof(BLUEZ_AUDIO_SK_PATH),
							audio_connect_cb);
}

void audio_ipc_cleanup(void)
{
	if (audio_io) {
		g_io_channel_shutdown(audio_io, TRUE, NULL);
		g_io_channel_unref(audio_io);
		audio_io = NULL;
	}
}

void audio_ipc_register(const struct ipc_handler *handlers, uint8_t size)
{
	service.handler = handlers;
	service.size = size;
}

void audio_ipc_unregister(void)
{
	service.handler = NULL;
	service.size = 0;
}

void audio_ipc_send_rsp(uint8_t opcode, uint8_t status)
{
	struct audio_status s;
	int sk;

	sk = g_io_channel_unix_get_fd(audio_io);

	if (status == AUDIO_STATUS_SUCCESS) {
		ipc_send(sk, AUDIO_SERVICE_ID, opcode, 0, NULL, -1);
		return;
	}

	s.code = status;

	ipc_send(sk, AUDIO_SERVICE_ID, AUDIO_OP_STATUS, sizeof(s), &s, -1);
}

void audio_ipc_send_rsp_full(uint8_t opcode, uint16_t len, void *param, int fd)
{
	ipc_send(g_io_channel_unix_get_fd(audio_io), AUDIO_SERVICE_ID, opcode,
							len, param, fd);
}