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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2014 Intel Corporation. All rights reserved.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "btio/btio.h"
#include "src/log.h"
#include "src/shared/util.h"
#include "sco.h"
struct bt_sco {
int ref_count;
GIOChannel *server_io;
GIOChannel *io;
guint watch;
bdaddr_t local_addr;
bdaddr_t remote_addr;
bt_sco_confirm_func_t confirm_cb;
bt_sco_conn_func_t connect_cb;
bt_sco_disconn_func_t disconnect_cb;
};
/* We support only one sco for the moment */
static bool sco_in_use = false;
static void clear_remote_address(struct bt_sco *sco)
{
memset(&sco->remote_addr, 0, sizeof(bdaddr_t));
}
static gboolean disconnect_watch(GIOChannel *chan, GIOCondition cond,
gpointer user_data)
{
struct bt_sco *sco = user_data;
g_io_channel_shutdown(sco->io, TRUE, NULL);
g_io_channel_unref(sco->io);
sco->io = NULL;
DBG("");
sco->watch = 0;
if (sco->disconnect_cb)
sco->disconnect_cb(&sco->remote_addr);
clear_remote_address(sco);
return FALSE;
}
static void connect_sco_cb(GIOChannel *chan, GError *err, gpointer user_data)
{
struct bt_sco *sco = user_data;
DBG("");
/* Lets unref connecting io */
if (sco->io) {
g_io_channel_unref(sco->io);
sco->io = NULL;
}
if (err) {
error("sco: Audio connect failed (%s)", err->message);
/*
* Connect_sco_cb is called only when connect_cb is in place
* Therefore it is safe to call it
*/
sco->connect_cb(SCO_STATUS_ERROR, &sco->remote_addr);
clear_remote_address(sco);
return;
}
g_io_channel_set_close_on_unref(chan, TRUE);
sco->io = g_io_channel_ref(chan);
sco->watch = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
disconnect_watch, sco);
/* It is safe to call it here */
sco->connect_cb(SCO_STATUS_OK, &sco->remote_addr);
}
static void confirm_sco_cb(GIOChannel *chan, gpointer user_data)
{
char address[18];
bdaddr_t bdaddr;
GError *err = NULL;
struct bt_sco *sco = user_data;
uint16_t voice_settings;
DBG("");
bt_io_get(chan, &err,
BT_IO_OPT_DEST, address,
BT_IO_OPT_DEST_BDADDR, &bdaddr,
BT_IO_OPT_INVALID);
if (err) {
error("sco: audio confirm failed (%s)", err->message);
g_error_free(err);
goto drop;
}
if (!sco->confirm_cb || !sco->connect_cb) {
error("sco: Connect and/or confirm callback not registered ");
goto drop;
}
/* Check if there is SCO */
if (sco->io) {
error("sco: SCO is in progress");
goto drop;
}
if (!sco->confirm_cb(&bdaddr, &voice_settings)) {
error("sco: Audio connection from %s rejected", address);
goto drop;
}
bacpy(&sco->remote_addr, &bdaddr);
DBG("Incoming SCO connection from %s, voice settings 0x%x", address,
voice_settings);
err = NULL;
bt_io_set(chan, &err, BT_IO_OPT_VOICE, voice_settings,
BT_IO_OPT_INVALID);
if (err) {
error("sco: Could not set voice settings (%s)", err->message);
g_error_free(err);
goto drop;
}
if (!bt_io_accept(chan, connect_sco_cb, sco, NULL, NULL)) {
error("sco: Failed to accept audio connection");
goto drop;
}
sco->io = g_io_channel_ref(chan);
return;
drop:
g_io_channel_shutdown(chan, TRUE, NULL);
}
static bool sco_listen(struct bt_sco *sco)
{
GError *err = NULL;
if (!sco)
return false;
sco->server_io = bt_io_listen(NULL, confirm_sco_cb, sco, NULL, &err,
BT_IO_OPT_SOURCE_BDADDR,
&sco->local_addr,
BT_IO_OPT_INVALID);
if (!sco->server_io) {
error("sco: Failed to listen on SCO: %s", err->message);
g_error_free(err);
return false;
}
return true;
}
struct bt_sco *bt_sco_new(const bdaddr_t *local_bdaddr)
{
struct bt_sco *sco;
if (!local_bdaddr)
return NULL;
/* For now we support only one SCO connection per time */
if (sco_in_use)
return NULL;
sco = new0(struct bt_sco, 1);
if (!sco)
return NULL;
bacpy(&sco->local_addr, local_bdaddr);
if (!sco_listen(sco)) {
free(sco);
return NULL;
}
sco_in_use = true;
return bt_sco_ref(sco);
}
struct bt_sco *bt_sco_ref(struct bt_sco *sco)
{
if (!sco)
return NULL;
__sync_fetch_and_add(&sco->ref_count, 1);
return sco;
}
static void sco_free(struct bt_sco *sco)
{
if (sco->server_io) {
g_io_channel_shutdown(sco->server_io, TRUE, NULL);
g_io_channel_unref(sco->server_io);
}
if (sco->io) {
g_io_channel_shutdown(sco->io, TRUE, NULL);
g_io_channel_unref(sco->io);
}
g_free(sco);
sco_in_use = false;
}
void bt_sco_unref(struct bt_sco *sco)
{
DBG("");
if (!sco)
return;
if (__sync_sub_and_fetch(&sco->ref_count, 1))
return;
sco_free(sco);
}
bool bt_sco_connect(struct bt_sco *sco, const bdaddr_t *addr,
uint16_t voice_settings)
{
GIOChannel *io;
GError *gerr = NULL;
DBG("");
if (!sco || !sco->connect_cb || !addr) {
error("sco: Incorrect parameters or missing connect_cb");
return false;
}
/* Check if we have connection in progress */
if (sco->io) {
error("sco: Connection already in progress");
return false;
}
io = bt_io_connect(connect_sco_cb, sco, NULL, &gerr,
BT_IO_OPT_SOURCE_BDADDR, &sco->local_addr,
BT_IO_OPT_DEST_BDADDR, addr,
BT_IO_OPT_VOICE, voice_settings,
BT_IO_OPT_INVALID);
if (!io) {
error("sco: unable to connect audio: %s", gerr->message);
g_error_free(gerr);
return false;
}
sco->io = io;
bacpy(&sco->remote_addr, addr);
return true;
}
void bt_sco_disconnect(struct bt_sco *sco)
{
if (!sco)
return;
if (sco->io)
g_io_channel_shutdown(sco->io, TRUE, NULL);
}
bool bt_sco_get_fd_and_mtu(struct bt_sco *sco, int *fd, uint16_t *mtu)
{
GError *err;
if (!sco->io || !fd || !mtu)
return false;
err = NULL;
if (!bt_io_get(sco->io, &err, BT_IO_OPT_MTU, mtu, BT_IO_OPT_INVALID)) {
error("Unable to get MTU: %s\n", err->message);
g_clear_error(&err);
return false;
}
*fd = g_io_channel_unix_get_fd(sco->io);
return true;
}
void bt_sco_set_confirm_cb(struct bt_sco *sco,
bt_sco_confirm_func_t func)
{
sco->confirm_cb = func;
}
void bt_sco_set_connect_cb(struct bt_sco *sco, bt_sco_conn_func_t func)
{
sco->connect_cb = func;
}
void bt_sco_set_disconnect_cb(struct bt_sco *sco,
bt_sco_disconn_func_t func)
{
sco->disconnect_cb = func;
}
|