2014-02-03 05:09:12 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2014-02-12 02:42:56 +08:00
|
|
|
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
|
2014-02-03 05:09:12 +08:00
|
|
|
*
|
|
|
|
*
|
2014-02-12 02:42:56 +08:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-02-03 05:09:12 +08:00
|
|
|
*
|
2014-02-12 02:42:56 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2014-02-03 05:09:12 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2014-02-12 02:42:56 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2014-02-03 05:09:12 +08:00
|
|
|
*
|
2014-02-12 02:42:56 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2014-02-03 05:09:12 +08:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-03 05:09:18 +08:00
|
|
|
#include <stdlib.h>
|
2014-02-03 05:09:12 +08:00
|
|
|
#include <stdbool.h>
|
2014-02-03 05:09:18 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2014-02-03 05:09:14 +08:00
|
|
|
#include <glib.h>
|
2014-02-03 05:09:12 +08:00
|
|
|
|
|
|
|
#include "lib/bluetooth.h"
|
2014-02-03 05:09:17 +08:00
|
|
|
#include "lib/sdp.h"
|
|
|
|
#include "lib/sdp_lib.h"
|
2014-02-03 05:09:18 +08:00
|
|
|
#include "src/sdp-client.h"
|
|
|
|
#include "src/uuid-helper.h"
|
2014-02-03 05:09:19 +08:00
|
|
|
#include "src/shared/hfp.h"
|
2014-02-03 05:09:18 +08:00
|
|
|
#include "btio/btio.h"
|
2014-02-24 20:44:12 +08:00
|
|
|
#include "hal-msg.h"
|
2014-02-25 00:05:03 +08:00
|
|
|
#include "ipc-common.h"
|
2014-02-24 20:44:12 +08:00
|
|
|
#include "ipc.h"
|
2014-02-03 05:09:12 +08:00
|
|
|
#include "handsfree.h"
|
2014-02-03 05:09:17 +08:00
|
|
|
#include "bluetooth.h"
|
2014-02-03 05:09:14 +08:00
|
|
|
#include "src/log.h"
|
2014-02-03 05:09:18 +08:00
|
|
|
#include "utils.h"
|
2014-02-03 05:09:14 +08:00
|
|
|
|
2014-03-03 07:50:47 +08:00
|
|
|
#define HSP_AG_CHANNEL 12
|
2014-02-03 05:09:17 +08:00
|
|
|
#define HFP_AG_CHANNEL 13
|
2014-03-03 07:50:47 +08:00
|
|
|
|
2014-02-03 05:09:17 +08:00
|
|
|
#define HFP_AG_FEATURES 0
|
|
|
|
|
2014-03-04 23:32:04 +08:00
|
|
|
/* offsets in indicators table, should be incremented when sending CIEV */
|
|
|
|
#define IND_SERVICE 0
|
|
|
|
#define IND_CALL 1
|
|
|
|
#define IND_CALLSETUP 2
|
|
|
|
#define IND_CALLHELD 3
|
|
|
|
#define IND_SIGNAL 4
|
|
|
|
#define IND_ROAM 5
|
|
|
|
#define IND_BATTCHG 6
|
|
|
|
#define IND_COUNT (IND_BATTCHG + 1)
|
|
|
|
|
|
|
|
struct indicator {
|
|
|
|
const char *name;
|
|
|
|
int min;
|
|
|
|
int max;
|
|
|
|
int val;
|
|
|
|
bool always_active;
|
|
|
|
bool active;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct indicator inds_defaults[] = {
|
|
|
|
{ "service", 0, 1, 0, false, true },
|
|
|
|
{ "call", 0, 1, 0, true, true },
|
|
|
|
{ "callsetup", 0, 3, 0, true, true },
|
|
|
|
{ "callheld", 0, 2, 0, true, true },
|
|
|
|
{ "signal", 0, 5, 0, false, true },
|
|
|
|
{ "roam", 0, 1, 0, false, true },
|
|
|
|
{ "battchg", 0, 5, 0, false, true },
|
|
|
|
};
|
|
|
|
|
2014-02-03 05:09:18 +08:00
|
|
|
static struct {
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
uint8_t state;
|
2014-03-04 23:32:03 +08:00
|
|
|
uint32_t features;
|
2014-03-04 23:32:05 +08:00
|
|
|
bool indicators_enabled;
|
2014-03-04 23:32:04 +08:00
|
|
|
struct indicator inds[IND_COUNT];
|
2014-02-03 05:09:19 +08:00
|
|
|
struct hfp_gw *gw;
|
2014-02-03 05:09:18 +08:00
|
|
|
} device;
|
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
static bdaddr_t adapter_addr;
|
2014-02-24 20:44:12 +08:00
|
|
|
static struct ipc *hal_ipc = NULL;
|
2014-02-03 05:09:14 +08:00
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
static uint32_t hfp_record_id = 0;
|
|
|
|
static GIOChannel *hfp_server = NULL;
|
2014-02-03 05:09:18 +08:00
|
|
|
|
2014-03-03 07:50:47 +08:00
|
|
|
static uint32_t hsp_record_id = 0;
|
|
|
|
static GIOChannel *hsp_server = NULL;
|
|
|
|
|
2014-02-03 05:09:18 +08:00
|
|
|
static void device_set_state(uint8_t state)
|
|
|
|
{
|
|
|
|
struct hal_ev_handsfree_conn_state ev;
|
|
|
|
char address[18];
|
|
|
|
|
|
|
|
if (device.state == state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
device.state = state;
|
|
|
|
|
|
|
|
ba2str(&device.bdaddr, address);
|
|
|
|
DBG("device %s state %u", address, state);
|
|
|
|
|
|
|
|
bdaddr2android(&device.bdaddr, ev.bdaddr);
|
|
|
|
ev.state = state;
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_EV_HANDSFREE_CONN_STATE, sizeof(ev), &ev);
|
2014-02-03 05:09:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void device_init(const bdaddr_t *bdaddr)
|
|
|
|
{
|
|
|
|
bacpy(&device.bdaddr, bdaddr);
|
|
|
|
|
2014-03-04 23:32:04 +08:00
|
|
|
memcpy(device.inds, inds_defaults, sizeof(device.inds));
|
|
|
|
|
2014-02-03 05:09:18 +08:00
|
|
|
device_set_state(HAL_EV_HANDSFREE_CONNECTION_STATE_CONNECTING);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_cleanup(void)
|
|
|
|
{
|
2014-02-03 05:09:19 +08:00
|
|
|
if (device.gw) {
|
|
|
|
hfp_gw_unref(device.gw);
|
|
|
|
device.gw = NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:18 +08:00
|
|
|
device_set_state(HAL_EV_HANDSFREE_CONNECTION_STATE_DISCONNECTED);
|
|
|
|
|
|
|
|
memset(&device, 0, sizeof(device));
|
|
|
|
}
|
|
|
|
|
2014-02-23 05:09:25 +08:00
|
|
|
static void at_command_handler(const char *command, void *user_data)
|
2014-02-03 05:09:18 +08:00
|
|
|
{
|
2014-02-23 05:09:25 +08:00
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_ERROR);
|
2014-02-03 05:09:18 +08:00
|
|
|
|
2014-03-04 23:32:05 +08:00
|
|
|
if (device.state != HAL_EV_HANDSFREE_CONNECTION_STATE_SLC_CONNECTED)
|
|
|
|
hfp_gw_disconnect(device.gw);
|
2014-02-03 05:09:18 +08:00
|
|
|
}
|
|
|
|
|
2014-02-23 05:09:25 +08:00
|
|
|
static void disconnect_watch(void *user_data)
|
2014-02-03 05:09:19 +08:00
|
|
|
{
|
2014-02-23 05:09:25 +08:00
|
|
|
DBG("");
|
2014-02-03 05:09:19 +08:00
|
|
|
|
2014-02-23 05:09:25 +08:00
|
|
|
device_cleanup();
|
2014-02-03 05:09:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 23:32:05 +08:00
|
|
|
static void at_cmd_cmer(struct hfp_gw_result *result, enum hfp_gw_cmd_type type,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case HFP_GW_CMD_TYPE_SET:
|
|
|
|
/* mode must be =3 */
|
|
|
|
if (!hfp_gw_result_get_number(result, &val) || val != 3)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* keyp is don't care */
|
|
|
|
if (!hfp_gw_result_get_number(result, &val))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* disp is don't care */
|
|
|
|
if (!hfp_gw_result_get_number(result, &val))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* ind must be 0 or 1 */
|
|
|
|
if (!hfp_gw_result_get_number(result, &val) || val > 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (hfp_gw_result_has_next(result))
|
|
|
|
break;
|
|
|
|
|
|
|
|
device.indicators_enabled = val;
|
|
|
|
|
|
|
|
/* TODO Check for 3-way calling support */
|
|
|
|
device_set_state(HAL_EV_HANDSFREE_CONNECTION_STATE_SLC_CONNECTED);
|
|
|
|
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_OK);
|
|
|
|
|
|
|
|
return;
|
|
|
|
case HFP_GW_CMD_TYPE_TEST:
|
|
|
|
case HFP_GW_CMD_TYPE_READ:
|
|
|
|
case HFP_GW_CMD_TYPE_COMMAND:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-03-04 23:32:04 +08:00
|
|
|
static void at_cmd_cind(struct hfp_gw_result *result, enum hfp_gw_cmd_type type,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
char *buf, *ptr;
|
|
|
|
int len;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case HFP_GW_CMD_TYPE_TEST:
|
|
|
|
|
|
|
|
len = strlen("+CIND:") + 1;
|
|
|
|
|
|
|
|
for (i = 0; i < IND_COUNT; i++) {
|
|
|
|
len += strlen("(\"\",(X,X)),");
|
|
|
|
len += strlen(device.inds[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = g_malloc(len);
|
|
|
|
|
|
|
|
ptr = buf + sprintf(buf, "+CIND:");
|
|
|
|
|
|
|
|
for (i = 0; i < IND_COUNT; i++) {
|
|
|
|
ptr += sprintf(ptr, "(\"%s\",(%d%c%d)),",
|
|
|
|
device.inds[i].name,
|
|
|
|
device.inds[i].min,
|
|
|
|
device.inds[i].max == 1 ? ',' : '-',
|
|
|
|
device.inds[i].max);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr--;
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
hfp_gw_send_info(device.gw, "%s", buf);
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_OK);
|
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
|
|
|
|
return;
|
|
|
|
case HFP_GW_CMD_TYPE_READ:
|
|
|
|
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_EV_HANDSFREE_CIND, 0, NULL);
|
|
|
|
return;
|
|
|
|
case HFP_GW_CMD_TYPE_SET:
|
|
|
|
case HFP_GW_CMD_TYPE_COMMAND:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-03-04 23:32:03 +08:00
|
|
|
static void at_cmd_brsf(struct hfp_gw_result *result, enum hfp_gw_cmd_type type,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
unsigned int feat;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case HFP_GW_CMD_TYPE_SET:
|
|
|
|
if (!hfp_gw_result_get_number(result, &feat))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (hfp_gw_result_has_next(result))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* TODO verify features */
|
|
|
|
device.features = feat;
|
|
|
|
|
|
|
|
hfp_gw_send_info(device.gw, "+BRSF=%u", HFP_AG_FEATURES);
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_OK);
|
|
|
|
return;
|
|
|
|
case HFP_GW_CMD_TYPE_READ:
|
|
|
|
case HFP_GW_CMD_TYPE_TEST:
|
|
|
|
case HFP_GW_CMD_TYPE_COMMAND:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:18 +08:00
|
|
|
static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
error("handsfree: connect failed (%s)", err->message);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:19 +08:00
|
|
|
device.gw = hfp_gw_new(g_io_channel_unix_get_fd(chan));
|
|
|
|
if (!device.gw)
|
|
|
|
goto failed;
|
|
|
|
|
2014-02-23 05:09:25 +08:00
|
|
|
g_io_channel_set_close_on_unref(chan, FALSE);
|
|
|
|
|
2014-02-03 05:09:19 +08:00
|
|
|
hfp_gw_set_close_on_unref(device.gw, true);
|
|
|
|
hfp_gw_set_command_handler(device.gw, at_command_handler, NULL, NULL);
|
2014-02-23 05:09:25 +08:00
|
|
|
hfp_gw_set_disconnect_handler(device.gw, disconnect_watch, NULL, NULL);
|
2014-02-03 05:09:18 +08:00
|
|
|
|
2014-03-04 23:32:03 +08:00
|
|
|
|
|
|
|
hfp_gw_register(device.gw, at_cmd_brsf, "+BRSF", NULL, NULL);
|
2014-03-04 23:32:04 +08:00
|
|
|
hfp_gw_register(device.gw, at_cmd_cind, "+CIND", NULL, NULL);
|
2014-03-04 23:32:05 +08:00
|
|
|
hfp_gw_register(device.gw, at_cmd_cmer, "+CMER", NULL, NULL);
|
2014-02-03 05:09:18 +08:00
|
|
|
device_set_state(HAL_EV_HANDSFREE_CONNECTION_STATE_CONNECTED);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
g_io_channel_shutdown(chan, TRUE, NULL);
|
|
|
|
device_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void confirm_cb(GIOChannel *chan, gpointer data)
|
|
|
|
{
|
|
|
|
char address[18];
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
bt_io_get(chan, &err,
|
|
|
|
BT_IO_OPT_DEST, address,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &bdaddr,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (err) {
|
|
|
|
error("handsfree: confirm failed (%s)", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG("incoming connect from %s", address);
|
|
|
|
|
|
|
|
if (device.state != HAL_EV_HANDSFREE_CONNECTION_STATE_DISCONNECTED) {
|
|
|
|
info("handsfree: refusing connection from %s", address);
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_init(&bdaddr);
|
|
|
|
|
|
|
|
if (!bt_io_accept(chan, connect_cb, NULL, NULL, NULL)) {
|
|
|
|
error("handsfree: failed to accept connection");
|
|
|
|
device_cleanup();
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
drop:
|
|
|
|
g_io_channel_shutdown(chan, TRUE, NULL);
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:48 +08:00
|
|
|
static void sdp_hsp_search_cb(sdp_list_t *recs, int err, gpointer data)
|
2014-02-03 05:09:20 +08:00
|
|
|
{
|
|
|
|
sdp_list_t *protos, *classes;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
GIOChannel *io;
|
|
|
|
uuid_t uuid;
|
|
|
|
int channel;
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
if (err < 0) {
|
2014-03-03 07:50:48 +08:00
|
|
|
error("handsfree: unable to get SDP record: %s",
|
|
|
|
strerror(-err));
|
2014-02-03 05:09:20 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recs || !recs->data) {
|
2014-03-03 07:50:48 +08:00
|
|
|
info("handsfree: no HSP SDP records found");
|
2014-02-03 05:09:20 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_get_service_classes(recs->data, &classes) < 0) {
|
|
|
|
error("handsfree: unable to get service classes from record");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_get_access_protos(recs->data, &protos) < 0) {
|
|
|
|
error("handsfree: unable to get access protocols from record");
|
2014-02-05 18:50:59 +08:00
|
|
|
sdp_list_free(classes, free);
|
2014-02-03 05:09:20 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:48 +08:00
|
|
|
/* TODO read remote version? */
|
|
|
|
/* TODO read volume control support */
|
|
|
|
|
|
|
|
memcpy(&uuid, classes->data, sizeof(uuid));
|
|
|
|
sdp_list_free(classes, free);
|
|
|
|
|
|
|
|
if (!sdp_uuid128_to_uuid(&uuid) || uuid.type != SDP_UUID16 ||
|
|
|
|
uuid.value.uuid16 != HEADSET_SVCLASS_ID) {
|
|
|
|
sdp_list_free(protos, NULL);
|
|
|
|
error("handsfree: invalid service record or not HSP");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = sdp_get_proto_port(protos, RFCOMM_UUID);
|
|
|
|
sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
|
|
|
|
sdp_list_free(protos, NULL);
|
|
|
|
if (channel <= 0) {
|
|
|
|
error("handsfree: unable to get RFCOMM channel from record");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
io = bt_io_connect(connect_cb, NULL, NULL, &gerr,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &device.bdaddr,
|
|
|
|
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
|
|
|
|
BT_IO_OPT_CHANNEL, channel,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (!io) {
|
|
|
|
error("handsfree: unable to connect: %s", gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_io_channel_unref(io);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
device_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdp_search_hsp(void)
|
|
|
|
{
|
|
|
|
uuid_t uuid;
|
|
|
|
|
|
|
|
sdp_uuid16_create(&uuid, HEADSET_SVCLASS_ID);
|
|
|
|
|
|
|
|
return bt_search_service(&adapter_addr, &device.bdaddr, &uuid,
|
|
|
|
sdp_hsp_search_cb, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sdp_hfp_search_cb(sdp_list_t *recs, int err, gpointer data)
|
|
|
|
{
|
|
|
|
sdp_list_t *protos, *classes;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
GIOChannel *io;
|
|
|
|
uuid_t uuid;
|
|
|
|
int channel;
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
error("handsfree: unable to get SDP record: %s",
|
|
|
|
strerror(-err));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recs || !recs->data) {
|
|
|
|
info("handsfree: no HFP SDP records found, trying HSP");
|
|
|
|
|
|
|
|
if (sdp_search_hsp() < 0) {
|
|
|
|
error("handsfree: HSP SDP search failed");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_get_service_classes(recs->data, &classes) < 0) {
|
|
|
|
error("handsfree: unable to get service classes from record");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_get_access_protos(recs->data, &protos) < 0) {
|
|
|
|
error("handsfree: unable to get access protocols from record");
|
|
|
|
sdp_list_free(classes, free);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:20 +08:00
|
|
|
/* TODO read remote version? */
|
|
|
|
|
|
|
|
memcpy(&uuid, classes->data, sizeof(uuid));
|
|
|
|
sdp_list_free(classes, free);
|
|
|
|
|
|
|
|
if (!sdp_uuid128_to_uuid(&uuid) || uuid.type != SDP_UUID16 ||
|
|
|
|
uuid.value.uuid16 != HANDSFREE_SVCLASS_ID) {
|
|
|
|
sdp_list_free(protos, NULL);
|
|
|
|
error("handsfree: invalid service record or not HFP");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = sdp_get_proto_port(protos, RFCOMM_UUID);
|
|
|
|
sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
|
|
|
|
sdp_list_free(protos, NULL);
|
|
|
|
if (channel <= 0) {
|
|
|
|
error("handsfree: unable to get RFCOMM channel from record");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
io = bt_io_connect(connect_cb, NULL, NULL, &gerr,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &device.bdaddr,
|
|
|
|
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
|
|
|
|
BT_IO_OPT_CHANNEL, channel,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (!io) {
|
|
|
|
error("handsfree: unable to connect: %s", gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_io_channel_unref(io);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
device_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:48 +08:00
|
|
|
static int sdp_search_hfp(void)
|
|
|
|
{
|
|
|
|
uuid_t uuid;
|
|
|
|
|
|
|
|
sdp_uuid16_create(&uuid, HANDSFREE_SVCLASS_ID);
|
|
|
|
|
|
|
|
return bt_search_service(&adapter_addr, &device.bdaddr, &uuid,
|
|
|
|
sdp_hfp_search_cb, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
static void handle_connect(const void *buf, uint16_t len)
|
|
|
|
{
|
2014-02-03 05:09:20 +08:00
|
|
|
const struct hal_cmd_handsfree_connect *cmd = buf;
|
|
|
|
char addr[18];
|
|
|
|
uint8_t status;
|
|
|
|
bdaddr_t bdaddr;
|
2014-03-03 07:50:48 +08:00
|
|
|
int ret;
|
2014-02-03 05:09:20 +08:00
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
DBG("");
|
|
|
|
|
2014-02-03 05:09:20 +08:00
|
|
|
if (device.state != HAL_EV_HANDSFREE_CONNECTION_STATE_DISCONNECTED) {
|
|
|
|
status = HAL_STATUS_FAILED;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
android2bdaddr(&cmd->bdaddr, &bdaddr);
|
|
|
|
|
2014-02-05 18:15:33 +08:00
|
|
|
ba2str(&bdaddr, addr);
|
2014-02-03 05:09:20 +08:00
|
|
|
DBG("connecting to %s", addr);
|
|
|
|
|
|
|
|
device_init(&bdaddr);
|
|
|
|
|
2014-03-03 07:50:48 +08:00
|
|
|
/* prefer HFP over HSP */
|
|
|
|
ret = hfp_server ? sdp_search_hfp() : sdp_search_hsp();
|
|
|
|
if (ret < 0) {
|
2014-02-03 05:09:20 +08:00
|
|
|
error("handsfree: SDP search failed");
|
|
|
|
device_cleanup();
|
|
|
|
status = HAL_STATUS_FAILED;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = HAL_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
failed:
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_CONNECT, status);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_disconnect(const void *buf, uint16_t len)
|
|
|
|
{
|
2014-02-03 05:09:21 +08:00
|
|
|
const struct hal_cmd_handsfree_disconnect *cmd = buf;
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
uint8_t status;
|
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
DBG("");
|
|
|
|
|
2014-02-03 05:09:21 +08:00
|
|
|
android2bdaddr(cmd->bdaddr, &bdaddr);
|
|
|
|
|
|
|
|
if (device.state == HAL_EV_HANDSFREE_CONNECTION_STATE_DISCONNECTED ||
|
|
|
|
bacmp(&device.bdaddr, &bdaddr)) {
|
|
|
|
status = HAL_STATUS_FAILED;
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device.state == HAL_EV_HANDSFREE_CONNECTION_STATE_DISCONNECTING) {
|
|
|
|
status = HAL_STATUS_SUCCESS;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2014-02-23 05:09:25 +08:00
|
|
|
if (device.state == HAL_EV_HANDSFREE_CONNECTION_STATE_CONNECTING) {
|
2014-02-03 05:09:21 +08:00
|
|
|
device_cleanup();
|
2014-02-23 05:09:25 +08:00
|
|
|
} else {
|
|
|
|
device_set_state(HAL_EV_HANDSFREE_CONNECTION_STATE_DISCONNECTING);
|
|
|
|
hfp_gw_disconnect(device.gw);
|
2014-02-03 05:09:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
status = HAL_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
failed:
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_DISCONNECT, status);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_connect_audio(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_CONNECT_AUDIO, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_disconnect_audio(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
2014-02-03 05:09:14 +08:00
|
|
|
HAL_OP_HANDSFREE_DISCONNECT_AUDIO, HAL_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_start_vr(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_START_VR, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_stop_vr(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_STOP_VR, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_volume_control(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_VOLUME_CONTROL, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_device_status_notif(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
2014-02-03 05:09:14 +08:00
|
|
|
HAL_OP_HANDSFREE_DEVICE_STATUS_NOTIF,
|
|
|
|
HAL_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_cops(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_COPS_RESPONSE, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 23:32:04 +08:00
|
|
|
static unsigned int get_callsetup(uint8_t state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case HAL_HANDSFREE_CALL_STATE_INCOMING:
|
|
|
|
return 1;
|
|
|
|
case HAL_HANDSFREE_CALL_STATE_DIALING:
|
|
|
|
return 2;
|
|
|
|
case HAL_HANDSFREE_CALL_STATE_ALERTING:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
static void handle_cind(const void *buf, uint16_t len)
|
|
|
|
{
|
2014-03-04 23:32:04 +08:00
|
|
|
const struct hal_cmd_handsfree_cind_response *cmd = buf;
|
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
DBG("");
|
|
|
|
|
2014-03-04 23:32:04 +08:00
|
|
|
/* HAL doesn't provide indicators values so need to convert here */
|
|
|
|
device.inds[IND_SERVICE].val = cmd->svc;
|
|
|
|
device.inds[IND_CALL].val = !!(cmd->num_active + cmd->num_held);
|
|
|
|
device.inds[IND_CALLSETUP].val = get_callsetup(cmd->state);
|
|
|
|
device.inds[IND_CALLHELD].val = cmd->num_held ?
|
|
|
|
(cmd->num_active ? 1 : 2) : 0;
|
|
|
|
device.inds[IND_SIGNAL].val = cmd->signal;
|
|
|
|
device.inds[IND_ROAM].val = cmd->roam;
|
|
|
|
device.inds[IND_BATTCHG].val = cmd->batt_chg;
|
|
|
|
|
|
|
|
/* Order must match indicators_defaults table */
|
|
|
|
hfp_gw_send_info(device.gw, "+CIND: %u,%u,%u,%u,%u,%u,%u",
|
|
|
|
device.inds[IND_SERVICE].val,
|
|
|
|
device.inds[IND_CALL].val,
|
|
|
|
device.inds[IND_CALLSETUP].val,
|
|
|
|
device.inds[IND_CALLHELD].val,
|
|
|
|
device.inds[IND_SIGNAL].val,
|
|
|
|
device.inds[IND_ROAM].val,
|
|
|
|
device.inds[IND_BATTCHG].val);
|
|
|
|
|
|
|
|
hfp_gw_send_result(device.gw, HFP_RESULT_OK);
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
2014-03-04 23:32:04 +08:00
|
|
|
HAL_OP_HANDSFREE_CIND_RESPONSE, HAL_STATUS_SUCCESS);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_formatted_at_resp(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
2014-02-03 05:09:14 +08:00
|
|
|
HAL_OP_HANDSFREE_FORMATTED_AT_RESPONSE,
|
|
|
|
HAL_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_at_resp(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_AT_RESPONSE, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_clcc_resp(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
|
|
|
HAL_OP_HANDSFREE_CLCC_RESPONSE, HAL_STATUS_FAILED);
|
2014-02-03 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_phone_state_change(const void *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
DBG("");
|
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE,
|
2014-02-03 05:09:14 +08:00
|
|
|
HAL_OP_HANDSFREE_PHONE_STATE_CHANGE,
|
|
|
|
HAL_STATUS_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct ipc_handler cmd_handlers[] = {
|
|
|
|
/* HAL_OP_HANDSFREE_CONNECT */
|
|
|
|
{ handle_connect, false, sizeof(struct hal_cmd_handsfree_connect)},
|
|
|
|
/* HAL_OP_HANDSFREE_DISCONNECT */
|
|
|
|
{handle_disconnect, false, sizeof(struct hal_cmd_handsfree_disconnect)},
|
|
|
|
/*HAL_OP_HANDSFREE_CONNECT_AUDIO*/
|
|
|
|
{handle_connect_audio, false,
|
|
|
|
sizeof(struct hal_cmd_handsfree_connect_audio)},
|
|
|
|
/*HAL_OP_HANDSFREE_DISCONNECT_AUDIO*/
|
|
|
|
{handle_disconnect_audio, false,
|
|
|
|
sizeof(struct hal_cmd_handsfree_disconnect_audio)},
|
|
|
|
/* define HAL_OP_HANDSFREE_START_VR */
|
|
|
|
{handle_start_vr, false, 0 },
|
|
|
|
/* define HAL_OP_HANDSFREE_STOP_VR */
|
|
|
|
{handle_stop_vr, false, 0 },
|
|
|
|
/* HAL_OP_HANDSFREE_VOLUME_CONTROL */
|
|
|
|
{handle_volume_control, false,
|
|
|
|
sizeof(struct hal_cmd_handsfree_volume_control)},
|
|
|
|
/* HAL_OP_HANDSFREE_DEVICE_STATUS_NOTIF */
|
|
|
|
{handle_device_status_notif, false,
|
|
|
|
sizeof(struct hal_cmd_handsfree_device_status_notif)},
|
|
|
|
/* HAL_OP_HANDSFREE_COPS_RESPONSE */
|
|
|
|
{handle_cops, true, sizeof(struct hal_cmd_handsfree_cops_response)},
|
|
|
|
/* HAL_OP_HANDSFREE_CIND_RESPONSE */
|
|
|
|
{ handle_cind, false, sizeof(struct hal_cmd_handsfree_cind_response)},
|
|
|
|
/* HAL_OP_HANDSFREE_FORMATTED_AT_RESPONSE */
|
|
|
|
{handle_formatted_at_resp, true,
|
|
|
|
sizeof(struct hal_cmd_handsfree_formatted_at_response)},
|
|
|
|
/* HAL_OP_HANDSFREE_AT_RESPONSE */
|
|
|
|
{handle_at_resp, false, sizeof(struct hal_cmd_handsfree_at_response)},
|
|
|
|
/* HAL_OP_HANDSFREE_CLCC_RESPONSE */
|
|
|
|
{handle_clcc_resp, true,
|
|
|
|
sizeof(struct hal_cmd_handsfree_clcc_response)},
|
|
|
|
/* HAL_OP_HANDSFREE_PHONE_STATE_CHANGE */
|
|
|
|
{handle_phone_state_change, true,
|
|
|
|
sizeof(struct hal_cmd_handsfree_phone_state_change)},
|
|
|
|
};
|
2014-02-03 05:09:12 +08:00
|
|
|
|
2014-03-03 07:50:47 +08:00
|
|
|
static sdp_record_t *headset_ag_record(void)
|
|
|
|
{
|
|
|
|
sdp_list_t *svclass_id, *pfseq, *apseq, *root;
|
|
|
|
uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
|
|
|
|
uuid_t l2cap_uuid, rfcomm_uuid;
|
|
|
|
sdp_profile_desc_t profile;
|
|
|
|
sdp_list_t *aproto, *proto[2];
|
|
|
|
sdp_record_t *record;
|
|
|
|
sdp_data_t *channel;
|
|
|
|
uint8_t netid = 0x01;
|
|
|
|
sdp_data_t *network;
|
|
|
|
uint8_t ch = HSP_AG_CHANNEL;
|
|
|
|
|
|
|
|
record = sdp_record_alloc();
|
|
|
|
if (!record)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
network = sdp_data_alloc(SDP_UINT8, &netid);
|
|
|
|
if (!network) {
|
|
|
|
sdp_record_free(record);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
|
|
|
|
root = sdp_list_append(0, &root_uuid);
|
|
|
|
sdp_set_browse_groups(record, root);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&svclass_uuid, HEADSET_AGW_SVCLASS_ID);
|
|
|
|
svclass_id = sdp_list_append(0, &svclass_uuid);
|
|
|
|
sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
|
|
|
|
svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
|
|
|
|
sdp_set_service_classes(record, svclass_id);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&profile.uuid, HEADSET_PROFILE_ID);
|
|
|
|
profile.version = 0x0102;
|
|
|
|
pfseq = sdp_list_append(0, &profile);
|
|
|
|
sdp_set_profile_descs(record, pfseq);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
|
|
|
|
proto[0] = sdp_list_append(0, &l2cap_uuid);
|
|
|
|
apseq = sdp_list_append(0, proto[0]);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
|
|
|
|
proto[1] = sdp_list_append(0, &rfcomm_uuid);
|
|
|
|
channel = sdp_data_alloc(SDP_UINT8, &ch);
|
|
|
|
proto[1] = sdp_list_append(proto[1], channel);
|
|
|
|
apseq = sdp_list_append(apseq, proto[1]);
|
|
|
|
|
|
|
|
aproto = sdp_list_append(0, apseq);
|
|
|
|
sdp_set_access_protos(record, aproto);
|
|
|
|
|
|
|
|
sdp_set_info_attr(record, "Voice Gateway", 0, 0);
|
|
|
|
|
|
|
|
sdp_attr_add(record, SDP_ATTR_EXTERNAL_NETWORK, network);
|
|
|
|
|
|
|
|
sdp_data_free(channel);
|
|
|
|
sdp_list_free(proto[0], NULL);
|
|
|
|
sdp_list_free(proto[1], NULL);
|
|
|
|
sdp_list_free(apseq, NULL);
|
|
|
|
sdp_list_free(pfseq, NULL);
|
|
|
|
sdp_list_free(aproto, NULL);
|
|
|
|
sdp_list_free(root, NULL);
|
|
|
|
sdp_list_free(svclass_id, NULL);
|
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool enable_hsp_ag(void)
|
|
|
|
{
|
|
|
|
sdp_record_t *rec;
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
hsp_server = bt_io_listen(NULL, confirm_cb, NULL, NULL, &err,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
|
|
|
|
BT_IO_OPT_CHANNEL, HSP_AG_CHANNEL,
|
|
|
|
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (!hsp_server) {
|
|
|
|
error("Failed to listen on Headset rfcomm: %s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec = headset_ag_record();
|
|
|
|
if (!rec) {
|
|
|
|
error("Failed to allocate Headset record");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bt_adapter_add_record(rec, 0) < 0) {
|
|
|
|
error("Failed to register Headset record");
|
|
|
|
sdp_record_free(rec);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
hsp_record_id = rec->handle;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
g_io_channel_shutdown(hsp_server, TRUE, NULL);
|
|
|
|
g_io_channel_unref(hsp_server);
|
|
|
|
hsp_server = NULL;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_hsp_ag(void)
|
|
|
|
{
|
|
|
|
if (hsp_server) {
|
|
|
|
g_io_channel_shutdown(hsp_server, TRUE, NULL);
|
|
|
|
g_io_channel_unref(hsp_server);
|
|
|
|
hsp_server = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hsp_record_id > 0) {
|
|
|
|
bt_adapter_remove_record(hsp_record_id);
|
|
|
|
hsp_record_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
static sdp_record_t *hfp_ag_record(void)
|
2014-02-03 05:09:17 +08:00
|
|
|
{
|
|
|
|
sdp_list_t *svclass_id, *pfseq, *apseq, *root;
|
|
|
|
uuid_t root_uuid, svclass_uuid, ga_svclass_uuid;
|
|
|
|
uuid_t l2cap_uuid, rfcomm_uuid;
|
|
|
|
sdp_profile_desc_t profile;
|
|
|
|
sdp_list_t *aproto, *proto[2];
|
|
|
|
sdp_record_t *record;
|
|
|
|
sdp_data_t *channel, *features;
|
|
|
|
uint8_t netid = 0x01;
|
|
|
|
uint16_t sdpfeat;
|
|
|
|
sdp_data_t *network;
|
|
|
|
uint8_t ch = HFP_AG_CHANNEL;
|
|
|
|
|
|
|
|
record = sdp_record_alloc();
|
|
|
|
if (!record)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
network = sdp_data_alloc(SDP_UINT8, &netid);
|
|
|
|
if (!network) {
|
|
|
|
sdp_record_free(record);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
|
2014-02-05 17:00:14 +08:00
|
|
|
root = sdp_list_append(NULL, &root_uuid);
|
2014-02-03 05:09:17 +08:00
|
|
|
sdp_set_browse_groups(record, root);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&svclass_uuid, HANDSFREE_AGW_SVCLASS_ID);
|
2014-02-05 17:00:14 +08:00
|
|
|
svclass_id = sdp_list_append(NULL, &svclass_uuid);
|
2014-02-03 05:09:17 +08:00
|
|
|
sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
|
|
|
|
svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
|
|
|
|
sdp_set_service_classes(record, svclass_id);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&profile.uuid, HANDSFREE_PROFILE_ID);
|
|
|
|
profile.version = 0x0106;
|
2014-02-05 17:00:14 +08:00
|
|
|
pfseq = sdp_list_append(NULL, &profile);
|
2014-02-03 05:09:17 +08:00
|
|
|
sdp_set_profile_descs(record, pfseq);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
|
|
|
|
proto[0] = sdp_list_append(0, &l2cap_uuid);
|
2014-02-05 17:00:14 +08:00
|
|
|
apseq = sdp_list_append(NULL, proto[0]);
|
2014-02-03 05:09:17 +08:00
|
|
|
|
|
|
|
sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
|
2014-02-05 17:00:14 +08:00
|
|
|
proto[1] = sdp_list_append(NULL, &rfcomm_uuid);
|
2014-02-03 05:09:17 +08:00
|
|
|
channel = sdp_data_alloc(SDP_UINT8, &ch);
|
|
|
|
proto[1] = sdp_list_append(proto[1], channel);
|
|
|
|
apseq = sdp_list_append(apseq, proto[1]);
|
|
|
|
|
|
|
|
sdpfeat = HFP_AG_FEATURES;
|
|
|
|
features = sdp_data_alloc(SDP_UINT16, &sdpfeat);
|
|
|
|
sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
|
|
|
|
|
2014-02-05 17:00:14 +08:00
|
|
|
aproto = sdp_list_append(NULL, apseq);
|
2014-02-03 05:09:17 +08:00
|
|
|
sdp_set_access_protos(record, aproto);
|
|
|
|
|
2014-02-05 17:00:14 +08:00
|
|
|
sdp_set_info_attr(record, "Hands-Free Audio Gateway", NULL, NULL);
|
2014-02-03 05:09:17 +08:00
|
|
|
|
|
|
|
sdp_attr_add(record, SDP_ATTR_EXTERNAL_NETWORK, network);
|
|
|
|
|
|
|
|
sdp_data_free(channel);
|
2014-02-05 17:00:14 +08:00
|
|
|
sdp_list_free(proto[0], NULL);
|
|
|
|
sdp_list_free(proto[1], NULL);
|
|
|
|
sdp_list_free(apseq, NULL);
|
|
|
|
sdp_list_free(pfseq, NULL);
|
|
|
|
sdp_list_free(aproto, NULL);
|
|
|
|
sdp_list_free(root, NULL);
|
|
|
|
sdp_list_free(svclass_id, NULL);
|
2014-02-03 05:09:17 +08:00
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
static bool enable_hfp_ag(void)
|
2014-02-03 05:09:12 +08:00
|
|
|
{
|
2014-02-03 05:09:17 +08:00
|
|
|
sdp_record_t *rec;
|
2014-02-03 05:09:18 +08:00
|
|
|
GError *err = NULL;
|
2014-02-03 05:09:17 +08:00
|
|
|
|
2014-02-03 05:09:14 +08:00
|
|
|
DBG("");
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
if (hfp_server)
|
|
|
|
return false;
|
2014-02-03 05:09:14 +08:00
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
hfp_server = bt_io_listen(NULL, confirm_cb, NULL, NULL, &err,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
|
|
|
|
BT_IO_OPT_CHANNEL, HFP_AG_CHANNEL,
|
|
|
|
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (!hfp_server) {
|
2014-02-03 05:09:18 +08:00
|
|
|
error("Failed to listen on Handsfree rfcomm: %s", err->message);
|
|
|
|
g_error_free(err);
|
2014-03-03 07:50:45 +08:00
|
|
|
goto failed;
|
2014-02-03 05:09:18 +08:00
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
rec = hfp_ag_record();
|
2014-02-03 05:09:17 +08:00
|
|
|
if (!rec) {
|
|
|
|
error("Failed to allocate Handsfree record");
|
2014-02-03 05:09:18 +08:00
|
|
|
goto failed;
|
2014-02-03 05:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bt_adapter_add_record(rec, 0) < 0) {
|
|
|
|
error("Failed to register Handsfree record");
|
|
|
|
sdp_record_free(rec);
|
2014-02-03 05:09:18 +08:00
|
|
|
goto failed;
|
2014-02-03 05:09:17 +08:00
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
hfp_record_id = rec->handle;
|
2014-02-03 05:09:14 +08:00
|
|
|
|
|
|
|
return true;
|
2014-02-03 05:09:18 +08:00
|
|
|
|
|
|
|
failed:
|
2014-03-03 07:50:45 +08:00
|
|
|
g_io_channel_shutdown(hfp_server, TRUE, NULL);
|
|
|
|
g_io_channel_unref(hfp_server);
|
|
|
|
hfp_server = NULL;
|
2014-02-03 05:09:18 +08:00
|
|
|
|
|
|
|
return false;
|
2014-02-03 05:09:12 +08:00
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
static void cleanup_hfp_ag(void)
|
|
|
|
{
|
|
|
|
if (hfp_server) {
|
|
|
|
g_io_channel_shutdown(hfp_server, TRUE, NULL);
|
|
|
|
g_io_channel_unref(hfp_server);
|
|
|
|
hfp_server = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hfp_record_id > 0) {
|
|
|
|
bt_adapter_remove_record(hfp_record_id);
|
|
|
|
hfp_record_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_handsfree_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
|
|
|
|
{
|
2014-03-03 07:50:49 +08:00
|
|
|
DBG("mode 0x%x", mode);
|
2014-03-03 07:50:45 +08:00
|
|
|
|
|
|
|
bacpy(&adapter_addr, addr);
|
|
|
|
|
2014-03-03 07:50:47 +08:00
|
|
|
if (!enable_hsp_ag())
|
2014-03-03 07:50:45 +08:00
|
|
|
return false;
|
|
|
|
|
2014-03-03 07:50:49 +08:00
|
|
|
if (mode != HAL_MODE_HANDSFREE_HSP_ONLY && !enable_hfp_ag()) {
|
2014-03-03 07:50:47 +08:00
|
|
|
cleanup_hsp_ag();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
hal_ipc = ipc;
|
|
|
|
ipc_register(hal_ipc, HAL_SERVICE_ID_HANDSFREE, cmd_handlers,
|
|
|
|
G_N_ELEMENTS(cmd_handlers));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-03 05:09:12 +08:00
|
|
|
void bt_handsfree_unregister(void)
|
|
|
|
{
|
2014-02-03 05:09:14 +08:00
|
|
|
DBG("");
|
2014-02-03 05:09:12 +08:00
|
|
|
|
2014-02-24 20:44:12 +08:00
|
|
|
ipc_unregister(hal_ipc, HAL_SERVICE_ID_HANDSFREE);
|
|
|
|
hal_ipc = NULL;
|
2014-02-03 05:09:17 +08:00
|
|
|
|
2014-03-03 07:50:45 +08:00
|
|
|
cleanup_hfp_ag();
|
2014-03-03 07:50:47 +08:00
|
|
|
cleanup_hsp_ag();
|
2014-02-03 05:09:12 +08:00
|
|
|
}
|