blob: 88d7bac388f98e5f26cf1f9e0b0ff5c8335c1b21 [file] [log] [blame]
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011 Nokia Corporation
* Copyright (C) 2011 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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 <errno.h>
#include <fcntl.h>
#include <gdbus/gdbus.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <glib.h>
#include <bluetooth/bluetooth.h>
#include "lib/uuid.h"
#include "src/dbus-common.h"
#include "lib/sdp.h"
#include "src/adapter.h"
#include "src/device.h"
#include "src/error.h"
#include "src/log.h"
#include "attrib/att.h"
#include "attrib/gattrib.h"
#include "attrib/gatt.h"
#include "src/attio.h"
#include "btvoice.h"
#include "monitor.h"
#include "src/textfile.h"
#define AUD_START_UUID 0xFFF1
#define AUD_CONFIG_UUID 0xFFF2
#define AUD_STREAM_UUID 0xFFF4
static pthread_mutex_t audio_mutex = PTHREAD_MUTEX_INITIALIZER;
struct disc_desc_cb_data {
uint16_t end;
gpointer data;
};
struct audiostream {
uint8_t id;
uint8_t type;
guint notifyid;
struct gatt_char *decl;
struct monitor *monitor;
};
struct monitor {
struct btd_device *device;
GAttrib *attrib;
struct att_range *audioOverBle;
struct gatt_primary *audioPrim;
struct enabled enabled;
guint attioid;
uint16_t aud_start_handle;
uint16_t aud_config_handle;
uint16_t aud_stream_handle;
struct audiostream *audiostream;
};
struct report {
uint8_t chartype;
uint8_t id;
uint8_t type;
guint notifyid;
struct gatt_char *decl;
struct monitor *monitor;
};
static GSList *monitors = NULL;
static struct monitor *find_monitor(struct btd_device *device)
{
GSList *l;
for (l = monitors; l; l = l->next) {
struct monitor *monitor = l->data;
if (monitor->device == device)
return monitor;
}
return NULL;
}
// AUDIO STUFF
static void aud_cfg_written_cb(guint8 status, const guint8 *pdu,
guint16 plen, gpointer user_data)
{
if (status != 0) {
error("Write output report failed: %s", att_ecode2str(status));
return;
}
}
static void audio_value_cb(const uint8_t *pdu, uint16_t len,
gpointer user_data)
{
//Send it to pipe...
pthread_mutex_lock(&audio_mutex);
int fp = open(FIFO_BTD2AUDIO_FILE, O_WRONLY | O_NONBLOCK);
if (fp != -1) {
uint8_t writebuf[256];
writebuf[0] = len; //This byte in not included in the length
memcpy(&writebuf[1],pdu, len);
write(fp, writebuf, len+1);
} else {
perror("fopen");
}
close(fp);
pthread_mutex_unlock(&audio_mutex);
}
static void audio_ccc_written_cb(guint8 status, const guint8 *pdu,
guint16 plen, gpointer user_data)
{
struct audiostream *audiostream = user_data;
struct monitor *monitor = audiostream->monitor;
if (status != 0) {
error("Write audio characteristic descriptor failed: %s",
att_ecode2str(status));
return;
}
audiostream->notifyid = g_attrib_register(monitor->attrib,
ATT_OP_HANDLE_NOTIFY,
audiostream->decl->value_handle,
audio_value_cb, audiostream, NULL);
DBG("Audio characteristic descriptor written: notifications enabled, handle %d", audiostream->decl->value_handle);
}
static void audio_write_ccc(uint16_t handle, gpointer user_data)
{
struct audiostream *audiostream = user_data;
struct monitor *monitor = audiostream->monitor;
uint8_t value[] = { 0x01, 0x00 };
gatt_write_char(monitor->attrib, handle, value, sizeof(value),
audio_ccc_written_cb, audiostream);
}
static void external_service_char_cb(GSList *chars, guint8 status,
gpointer user_data)
{
DBG("Not Manageed");
}
static void external_report_reference_cb(guint8 status, const guint8 *pdu,
guint16 plen, gpointer user_data)
{
struct monitor *monitor = user_data;
uint16_t uuid16;
bt_uuid_t uuid;
if (status != 0) {
error("Read External Report Reference descriptor failed: %s",
att_ecode2str(status));
return;
}
if (plen != 3) {
error("Malformed ATT read response");
return;
}
uuid16 = att_get_u16(&pdu[1]);
DBG("External report reference read, external report characteristic "
"UUID: 0x%04x", uuid16);
bt_uuid16_create(&uuid, uuid16);
gatt_discover_char(monitor->attrib, 0x00, 0xff, &uuid,
external_service_char_cb, monitor);
}
static void report_reference_cb(guint8 status, const guint8 *pdu,
guint16 plen, gpointer user_data)
{
struct report *report = user_data;
if (status != 0) {
error("Read Report Reference descriptor failed: %s",
att_ecode2str(status));
return;
}
if (plen != 3) {
error("Malformed ATT read response");
return;
}
report->id = pdu[1];
report->type = pdu[2];
DBG("Report ID: 0x%02x Report type: 0x%02x", pdu[1], pdu[2]);
}
static void discover_descriptor_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
struct disc_desc_cb_data *ddcb_data = user_data;
struct report *report;
struct monitor *monitor;
struct att_data_list *list = NULL;
GAttrib *attrib = NULL;
uint8_t format;
uint16_t handle = 0xffff;
uint16_t end = ddcb_data->end;
int i;
if (status == ATT_ECODE_ATTR_NOT_FOUND) {
DBG("Discover all characteristic descriptors finished");
goto done;
}
if (status != 0) {
error("Discover all characteristic descriptors failed: %s",
att_ecode2str(status));
goto done;
}
list = dec_find_info_resp(pdu, len, &format);
if (list == NULL)
return;
if (format != ATT_FIND_INFO_RESP_FMT_16BIT)
goto done;
for (i = 0; i < list->num; i++) {
uint16_t uuid16;
uint8_t *value;
value = list->data[i];
handle = att_get_u16(value);
uuid16 = att_get_u16(&value[2]);
switch (uuid16) {
case GATT_CLIENT_CHARAC_CFG_UUID:
report = ddcb_data->data;
attrib = report->monitor->attrib;
audio_write_ccc(handle, report);
break;
case GATT_REPORT_REFERENCE:
report = ddcb_data->data;
attrib = report->monitor->attrib;
gatt_read_char(attrib, handle,
report_reference_cb, report);
break;
case GATT_EXTERNAL_REPORT_REFERENCE:
monitor = ddcb_data->data;
attrib = monitor->attrib;
gatt_read_char(attrib, handle,
external_report_reference_cb, monitor);
break;
}
}
done:
att_data_list_free(list);
if (handle != 0xffff && handle < end)
gatt_discover_char_desc(attrib, handle + 1, end, discover_descriptor_cb,
ddcb_data);
else
g_free(ddcb_data);
}
static void discover_descriptor(GAttrib *attrib, uint16_t start, uint16_t end,
gpointer user_data)
{
struct disc_desc_cb_data *ddcb_data;
if (start > end)
return;
ddcb_data = g_new0(struct disc_desc_cb_data, 1);
ddcb_data->end = end;
ddcb_data->data = user_data;
gatt_discover_char_desc(attrib, start, end, discover_descriptor_cb, ddcb_data);
}
static void char_discovered_cb(GSList *chars, guint8 status,
gpointer user_data)
{
struct monitor *monitor = user_data;
bt_uuid_t audstart_uuid, audconfig_uuid, audstream_uuid;
struct report *report;
struct audiostream *audiostream;
GSList *l;
uint16_t info_handle = 0, proto_mode_handle = 0;
if (status != 0) {
const char *str = att_ecode2str(status);
DBG("Discover all characteristics failed: %s", str);
return;
}
bt_uuid16_create(&audstart_uuid, AUD_START_UUID);
bt_uuid16_create(&audconfig_uuid, AUD_CONFIG_UUID);
bt_uuid16_create(&audstream_uuid, AUD_STREAM_UUID);
for (l = chars; l; l = g_slist_next(l)) {
struct gatt_char *chr, *next;
bt_uuid_t uuid;
uint16_t start, end;
chr = l->data;
next = l->next ? l->next->data : NULL;
DBG("0x%04x UUID: %s properties: %02x",
chr->handle, chr->uuid, chr->properties);
bt_string_to_uuid(&uuid, chr->uuid);
start = chr->value_handle + 1;
end = (next ? next->handle - 1 : monitor->audioOverBle->end);
if (bt_uuid_cmp(&uuid, &audstart_uuid) == 0)
monitor->aud_start_handle = chr->value_handle;
else if (bt_uuid_cmp(&uuid, &audconfig_uuid) == 0)
monitor->aud_config_handle = chr->value_handle;
else if (bt_uuid_cmp(&uuid, &audstream_uuid) == 0)
{
audiostream = g_new0(struct audiostream, 1);
audiostream->monitor = monitor;
audiostream->decl = g_memdup(chr, sizeof(*chr));
//audiostream->chartype = 2;
monitor->audiostream = audiostream;
monitor->aud_stream_handle = chr->value_handle;
discover_descriptor(monitor->attrib, chr->value_handle,
monitor->audioOverBle->end, audiostream);
}
}
if (monitor->aud_config_handle)
{
uint8_t data[] = {0x03, 0x00, 0x00};
gatt_write_char(monitor->attrib, monitor->aud_config_handle,
data, sizeof(data), aud_cfg_written_cb, monitor);
}
}
static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
{
struct monitor *monitor = user_data;
GSList *l;
monitor->attrib = g_attrib_ref(attrib);
if (monitor->enabled.audioOverBle)
{
DBG("======= DISCOVERING AUDIO CHARACTERISTICS ========");
gatt_discover_char(monitor->attrib, monitor->audioOverBle->start,
monitor->audioOverBle->end, NULL,
char_discovered_cb, monitor);
}
else
{
DBG(" ================= AUDIO OVER BLE DISABLE ===========");
DBG(" ================= AUDIO PRIMARY WAS NULL ===========");
}
}
static void attio_disconnected_cb(gpointer user_data)
{
struct monitor *monitor = user_data;
const char *path = device_get_path(monitor->device);
GSList *l;
g_attrib_unref(monitor->attrib);
monitor->attrib = NULL;
}
static struct monitor *register_monitor(struct btd_device *device)
{
const char *path = device_get_path(device);
struct monitor *monitor;
char *level;
monitor = find_monitor(device);
if (monitor != NULL)
return monitor;
monitor = g_new0(struct monitor, 1);
monitor->device = btd_device_ref(device);
monitors = g_slist_append(monitors, monitor);
return monitor;
}
static void update_monitor(struct monitor *monitor)
{
if (!monitor->enabled.audioOverBle)
return;
if (monitor->attioid != 0)
return;
monitor->attioid = btd_device_add_attio_callback(monitor->device,
attio_connected_cb,
attio_disconnected_cb,
monitor);
}
int monitor_register_audioOverBle(struct btd_device *device,
struct enabled *enabled,
struct gatt_primary *audioOverBle)
{
struct monitor *monitor;
if (!enabled->audioOverBle)
return 0;
monitor = register_monitor(device);
if (monitor == NULL)
return -1;
monitor->audioOverBle = g_new0(struct att_range, 1);
monitor->audioOverBle->start = audioOverBle->range.start;
monitor->audioOverBle->end = audioOverBle->range.end;
monitor->audioPrim = audioOverBle;
monitor->enabled.audioOverBle = TRUE;
update_monitor(monitor);
//Create Pipe for audio data exchange
if (access(FIFO_BTD2AUDIO_FILE, F_OK) == -1)
{
int res;
res = mkfifo(FIFO_BTD2AUDIO_FILE, 0666);
if (res != 0)
{
DBG("Could not create fifo %s\n",FIFO_BTD2AUDIO_FILE);
exit(1);
}
else
DBG("==========> AUDIO PIPE CREATE %s\n",FIFO_BTD2AUDIO_FILE);
}
else
DBG("==========> AUDIO PIPE FOUND %s\n",FIFO_BTD2AUDIO_FILE);
return 0;
}
static void report_free(void *data)
{
struct report *report = data;
struct monitor *monitor = report->monitor;
if (monitor->attrib)
g_attrib_unregister(monitor->attrib, report->notifyid);
g_free(report->decl);
g_free(report);
}
static void cleanup_monitor(struct monitor *monitor)
{
struct btd_device *device = monitor->device;
const char *path = device_get_path(device);
if (monitor->audioOverBle != NULL)
return;
if (monitor->attioid != 0) {
btd_device_remove_attio_callback(device, monitor->attioid);
monitor->attioid = 0;
}
if (monitor->attrib != NULL) {
g_attrib_unref(monitor->attrib);
monitor->attrib = NULL;
}
}
void monitor_unregister_audioOverBle(struct btd_device *device)
{
struct monitor *monitor;
monitor = find_monitor(device);
if (monitor == NULL)
return;
g_free(monitor->audioOverBle);
monitor->audioOverBle = NULL;
monitor->enabled.audioOverBle = FALSE;
cleanup_monitor(monitor);
}