| /* |
| * |
| * 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 <stdbool.h> |
| |
| #include <stdbool.h> |
| #include <stdint.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <sys/stat.h> |
| |
| #include <glib.h> |
| |
| #include "lib/bluetooth.h" |
| #include "lib/sdp.h" |
| #include "lib/sdp_lib.h" |
| #include "lib/uuid.h" |
| |
| #include <gdbus/gdbus.h> |
| |
| #include "attrib/gattrib.h" |
| #include "attrib/att.h" |
| #include "attrib/gatt.h" |
| #include "src/adapter.h" |
| #include "src/attio.h" |
| #include "src/dbus-common.h" |
| #include "src/device.h" |
| #include "src/error.h" |
| #include "src/log.h" |
| #include "src/shared/util.h" |
| #include "src/textfile.h" |
| |
| #include "btvoice.h" |
| |
| #define AUD_START_UUID 0xFFF1 |
| #define AUD_CONFIG_UUID 0xFFF2 |
| #define AUD_STREAM_UUID 0xFFF4 |
| |
| #define IMMEDIATE_TIMEOUT 5 |
| #define TX_POWER_SIZE 1 |
| |
| static pthread_mutex_t audio_mutex; // = PTHREAD_MUTEX_INITIALIZER; |
| |
| struct btvoice_device { |
| uint16_t id; |
| struct btd_device *device; |
| GAttrib *attrib; |
| guint attioid; |
| struct gatt_primary *audioOverBle; |
| struct btvoiceenable voiceenable; |
| GSList *reports; |
| uint16_t aud_start_handle; |
| uint16_t aud_config_handle; |
| uint16_t aud_stream_handle; |
| }; |
| |
| struct audiostream { |
| uint8_t id; |
| uint8_t type; |
| uint16_t ccc_handle; |
| guint notifyid; |
| struct gatt_char *decl; |
| struct btvoice_device *btvoicedev; |
| }; |
| |
| static GSList *btvoicedevices = NULL; |
| |
| static struct btvoice_device *find_btvoicedev(struct btd_device *device) { |
| GSList *l; |
| |
| for (l = btvoicedevices; l; l = l->next) { |
| struct btvoice_device *btvoicedev = l->data; |
| |
| if (btvoicedev->device == device) return btvoicedev; |
| } |
| |
| 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; |
| } |
| DBG("Audio config write success\n"); |
| } |
| |
| #define FIFO_BTD2AUDIO_FILE "/tmp/Fifo_bdt_tiaudio" |
| |
| static void audio_value_cb(const uint8_t *pdu, uint16_t len, |
| gpointer user_data) { |
| struct audiostream *audiostream = user_data; |
| struct btvoice_device *btvoicedev = audiostream->btvoicedev; |
| static int PrevSeqNum = 0; |
| static int nbFrameLost, nbFrameReceived; |
| |
| static uint8_t SeqNum, i; |
| SeqNum = (pdu[3] >> 3) & 0x1F; |
| uint8_t frameType = pdu[3] & 0x07; |
| |
| if (0) { |
| // DBG("Audio noti: Len: %d, type: %d, Data: (0) 0x%x, (1) 0x%x, (2) |
| //0x%x, (3) 0x%x, (4) 0x%x, (5) 0x%x, (6) 0x%x, (7) 0x%x, (8) 0x%x, (9) |
| //0x%x, (10) 0x%x, (10) 0x%x, (11) 0x%x, (12) 0x%x, (13) 0x%x, (14) 0x%x, |
| //(15) 0x%x, (16) 0x%x, (17) 0x%x, (18) 0x%x, (19) 0x%x, (20) 0x%x, (21) |
| //0x%x, (22) 0x%x ", len, frameType, pdu[0], pdu[1], pdu[2], pdu[3], |
| //pdu[4], pdu[5], pdu[6], pdu[7], pdu[8], pdu[9], pdu[10], pdu[11], |
| //pdu[12], pdu[13], pdu[14], pdu[15], pdu[16], pdu[17], pdu[18], pdu[19], |
| //pdu[20], pdu[21], pdu[22]); |
| |
| DBG("Audio noti: Len: %d, type: %d, Data: (3) 0x%x, (4) 0x%x, (5) 0x%x, " |
| "(6) 0x%x, (7) 0x%x, (8) 0x%x, (9) 0x%x, (10) 0x%x, (11) 0x%x, (12) " |
| "0x%x, (19) 0x%x, (20) 0x%x, (21) 0x%x, (22) 0x%x ", |
| len, frameType, pdu[3], pdu[4], pdu[5], pdu[6], pdu[7], pdu[8], pdu[9], |
| pdu[10], pdu[11], pdu[12], pdu[len - 4], pdu[len - 3], pdu[len - 2], |
| pdu[len - 1]); |
| |
| if (frameType == 1) { |
| if (PrevSeqNum + 1 != SeqNum) { |
| if (SeqNum < (PrevSeqNum + 1)) { |
| nbFrameLost += 32 + SeqNum - (PrevSeqNum + 1); |
| } else { |
| nbFrameLost += SeqNum - (PrevSeqNum + 1); |
| } |
| DBG("FRAME LOST !!!!(%d != %d) (%d nbFrameLost for %d sent", |
| PrevSeqNum + 1, SeqNum, nbFrameLost, nbFrameLost + nbFrameReceived); |
| } |
| |
| if (SeqNum == 31) |
| PrevSeqNum = -1; |
| else |
| PrevSeqNum = SeqNum; |
| } |
| |
| } else { |
| // Parse AudioData in an external function |
| |
| // Send it to pipe... |
| |
| // extern audio_ParseData(const uint8_t *pdu, uint16_t len, |
| // gpointer user_data); |
| |
| // DBG("Audio noti: Len: %d, type: %d, Data: (0) 0x%x, (1) 0x%x, (2) |
| //0x%x, (3) 0x%x, (4) 0x%x, (5) 0x%x, (6) 0x%x, (7) 0x%x, (8) 0x%x, (9) |
| //0x%x, (10) 0x%x, (11) 0x%x, (12) 0x%x, (19) 0x%x, (20) 0x%x, (21) 0x%x, |
| //(22) 0x%x ", len, frameType, pdu[0], pdu[1], pdu[2], pdu[3], pdu[4], |
| //pdu[5], pdu[6], pdu[7], pdu[8], pdu[9], pdu[10], pdu[11], pdu[12], |
| //pdu[len-4], pdu[len-3], pdu[len-2], pdu[len-1]); |
| |
| pthread_mutex_lock(&audio_mutex); |
| { |
| int fp; |
| DBG("Open writing pipe \n"); |
| fp = open(FIFO_BTD2AUDIO_FILE, O_WRONLY); |
| if (fp != -1) { |
| uint8_t writebuf[256]; |
| |
| DBG("Forward info(%d bytes) to RF4CE via PIPE \n", (len + 1)); |
| writebuf[0] = len; // This byte in not included in the length |
| memcpy(&writebuf[1], pdu, len); |
| int wrote = write(fp, writebuf, len + 1); |
| DBG("Wrote %d/%d bytes", wrote, len+1); |
| close(fp); |
| } else { |
| DBG("can't open the pipe: %s", FIFO_BTD2AUDIO_FILE); |
| // perror("fopen:"); |
| } |
| |
| // DBG("Close writing pipe \n"); |
| } |
| 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 btvoice_device *btvoicedev = audiostream->btvoicedev; |
| |
| if (status != 0) { |
| error("Write audio characteristic descriptor failed: %s", |
| att_ecode2str(status)); |
| return; |
| } |
| |
| 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 btvoice_device *btvoicedev = audiostream->btvoicedev; |
| uint8_t value[] = {0x01, 0x00}; |
| |
| audiostream->notifyid = g_attrib_register( |
| btvoicedev->attrib, ATT_OP_HANDLE_NOTIFY, audiostream->decl->value_handle, |
| audio_value_cb, audiostream, NULL); |
| |
| gatt_write_char(btvoicedev->attrib, handle, value, sizeof(value), |
| audio_ccc_written_cb, audiostream); |
| } |
| |
| static void report_reference_cb(guint8 status, const guint8 *pdu, guint16 plen, |
| gpointer user_data) { |
| struct audiostream *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(uint8_t status, GSList *descs, |
| void *user_data) { |
| struct audiostream *report; |
| struct btvoice_device *btvoicedev; |
| struct att_data_list *list = NULL; |
| GAttrib *attrib = NULL; |
| uint8_t format; |
| uint16_t handle = 0xffff; |
| uint16_t uuid16; |
| int i; |
| |
| if (status != 0) { |
| error("Discover all characteristic descriptors failed: %s", |
| att_ecode2str(status)); |
| return; |
| } |
| |
| for (; descs; descs = descs->next) { |
| struct gatt_desc *desc = descs->data; |
| |
| switch (desc->uuid16) { |
| case GATT_CLIENT_CHARAC_CFG_UUID: |
| report = user_data; |
| report->ccc_handle = desc->handle; |
| attrib = report->btvoicedev->attrib; |
| audio_write_ccc(desc->handle, report); |
| DBG("Audio Notifications Enabled\n"); |
| break; |
| case GATT_REPORT_REFERENCE: |
| report = user_data; |
| attrib = report->btvoicedev->attrib; |
| gatt_read_char(attrib, desc->handle, report_reference_cb, report); |
| DBG("Audio Stream Report Reference Characterstic Read\n"); |
| break; |
| } |
| } |
| } |
| |
| static void discover_descriptor(GAttrib *attrib, uint16_t start, uint16_t end, |
| gpointer user_data) { |
| if (start > end) { |
| DBG("Start before End\n"); |
| return; |
| } |
| |
| gatt_discover_desc(attrib, start, end, NULL, discover_descriptor_cb, |
| user_data); |
| } |
| |
| static void char_discovered_cb(guint8 status, GSList *chars, void *user_data) { |
| struct btvoice_device *btvoicedev = user_data; |
| struct gatt_primary *prim = btvoicedev->audioOverBle; |
| bt_uuid_t audstart_uuid, audconfig_uuid, audstream_uuid; |
| struct audiostream *audiostream; |
| GSList *l; |
| |
| DBG("Audio Over Ble Inspecting Characterstics\n"); |
| |
| 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; |
| |
| 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); |
| |
| if (bt_uuid_cmp(&uuid, &audstart_uuid) == 0) { |
| btvoicedev->aud_start_handle = chr->value_handle; |
| DBG("btvoicedev->aud_start_handle = 0x%04x", |
| btvoicedev->aud_start_handle); |
| } else if (bt_uuid_cmp(&uuid, &audconfig_uuid) == 0) { |
| btvoicedev->aud_config_handle = chr->value_handle; |
| DBG("btvoicedev->aud_config_handle = 0x%04x", |
| btvoicedev->aud_config_handle); |
| } else if (bt_uuid_cmp(&uuid, &audstream_uuid) == 0) { |
| audiostream = g_new0(struct audiostream, 1); |
| audiostream->btvoicedev = btvoicedev; |
| audiostream->decl = g_memdup(chr, sizeof(*chr)); |
| btvoicedev->reports = g_slist_append(btvoicedev->reports, audiostream); |
| |
| btvoicedev->aud_stream_handle = chr->value_handle; |
| DBG("btvoicedev->aud_stream_handle = 0x%04x", |
| btvoicedev->aud_stream_handle); |
| DBG("Start handle = 0x%04x End handle = 0x%04x", chr->handle, |
| btvoicedev->audioOverBle->range.end); |
| discover_descriptor(btvoicedev->attrib, chr->value_handle, |
| btvoicedev->audioOverBle->range.end, audiostream); |
| } |
| } |
| |
| if (btvoicedev->aud_config_handle) { |
| uint8_t data[] = {0x04, 0x00, 0x00}; |
| gatt_write_char(btvoicedev->attrib, btvoicedev->aud_config_handle, data, |
| sizeof(data), aud_cfg_written_cb, btvoicedev); |
| } |
| } |
| |
| static void report_value_cb(const uint8_t *pdu, uint16_t len, |
| gpointer user_data) { |
| // struct audioStream *report = user_data; |
| // struct btvoice_device *btvoicedev = report->btvoicedev; |
| // uint16_t report_size = len - 3; |
| // uint8_t *buf; |
| |
| // if (len < 3) |
| // { |
| // /* 1-byte opcode + 2-byte handle */ |
| // error("Malformed ATT notification"); |
| // return; |
| // } |
| |
| // DBG("Receive ATT notificcation, len:%d, opCode %d, handle:0x%x", |
| // len, pdu[0], *((int16*)&pdu[1]); |
| // memset(&ev, 0, sizeof(ev)); |
| // ev.type = UHID_INPUT; |
| // ev.u.input.size = MIN(report_size, UHID_DATA_MAX); |
| |
| // buf = ev.u.input.data; |
| // if (hogdev->has_report_id) { |
| // *buf = report->id; |
| // buf++; |
| // ev.u.input.size++; |
| // } |
| |
| // memcpy(buf, &pdu[3], MIN(report_size, UHID_DATA_MAX)); |
| |
| // if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0) |
| // error("uHID write failed: %s", strerror(errno)); |
| // else |
| // DBG("Report from HoG device 0x%04X written to uHID fd %d", |
| // hogdev->id, hogdev->uhid_fd); |
| } |
| |
| static void attio_connected_cb(GAttrib *attrib, gpointer user_data) { |
| struct btvoice_device *btvoicedev = user_data; |
| struct gatt_primary *prim = btvoicedev->audioOverBle; |
| GSList *l; |
| |
| DBG("in"); |
| |
| btvoicedev->attrib = g_attrib_ref(attrib); |
| |
| if (btvoicedev->voiceenable.audioOverBle) { |
| DBG("======= DISCOVERING AUDIO CHARACTERISTICS ========"); |
| DBG("\n prim->range.start = %d, prim->range.end = %d", prim->range.start, |
| prim->range.end); |
| |
| gatt_discover_char(btvoicedev->attrib, prim->range.start, prim->range.end, |
| NULL, char_discovered_cb, btvoicedev); |
| |
| for (l = btvoicedev->reports; l; l = l->next) { |
| struct audiostream *r = l->data; |
| |
| r->notifyid = |
| g_attrib_register(r->btvoicedev->attrib, ATT_OP_HANDLE_NOTIFY, |
| r->decl->value_handle, report_value_cb, r, NULL); |
| } |
| } else { |
| DBG(" ================= AUDIO OVER BLE DISABLE ==========="); |
| DBG(" ================= AUDIO PRIMARY WAS NULL ==========="); |
| } |
| |
| DBG("out"); |
| } |
| |
| static void attio_disconnected_cb(gpointer user_data) { |
| struct btvoice_device *btvoicedev = user_data; |
| const char *path = device_get_path(btvoicedev->device); |
| GSList *l; |
| |
| for (l = btvoicedev->reports; l; l = l->next) { |
| struct audiostream *r = l->data; |
| |
| g_attrib_unregister(btvoicedev->attrib, r->notifyid); |
| } |
| |
| g_attrib_unref(btvoicedev->attrib); |
| btvoicedev->attrib = NULL; |
| } |
| |
| static struct btvoice_device *register_btvoicedevice( |
| struct btd_device *device) { |
| const char *path = device_get_path(device); |
| struct btvoice_device *btvoicedev; |
| char *level; |
| DBG("in"); |
| |
| btvoicedev = find_btvoicedev(device); |
| if (btvoicedev != NULL) return btvoicedev; |
| |
| btvoicedev = g_try_new0(struct btvoice_device, 1); |
| if (!btvoicedev) return NULL; |
| |
| btvoicedev->device = btd_device_ref(device); |
| |
| btvoicedevices = g_slist_append(btvoicedevices, btvoicedev); |
| |
| DBG("out"); |
| return btvoicedev; |
| } |
| |
| static void update_btvoicedevice(struct btvoice_device *btvoicedev) { |
| DBG("audioOverBle: %s", |
| btvoicedev->voiceenable.audioOverBle ? "TRUE" : "FALSE"); |
| |
| if (!btvoicedev->voiceenable.audioOverBle) return; |
| |
| if (btvoicedev->attioid != 0) return; |
| |
| btvoicedev->attioid = |
| btd_device_add_attio_callback(btvoicedev->device, attio_connected_cb, |
| attio_disconnected_cb, btvoicedev); |
| } |
| |
| int btvoice_register_audioOverBle(struct btd_device *device, |
| struct btvoiceenable *voiceenable, |
| struct gatt_primary *prim) { |
| struct btvoice_device *btvoicedev; |
| DBG("in"); |
| |
| if (!voiceenable->audioOverBle) return 0; |
| |
| btvoicedev = register_btvoicedevice(device); |
| if (btvoicedev == NULL) return -1; |
| |
| DBG("\n prim->range.start = %d, prim->range.end = %d", prim->range.start, |
| prim->range.end); |
| |
| btvoicedev->id = prim->range.start; |
| btvoicedev->audioOverBle = g_memdup(prim, sizeof(*prim)); |
| btvoicedev->voiceenable.audioOverBle = TRUE; |
| |
| update_btvoicedevice(btvoicedev); |
| |
| // 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); |
| |
| DBG("out"); |
| return 0; |
| } |
| |
| static void cleanup_btdevice(struct btvoice_device *btvoicedev) { |
| struct btd_device *device = btvoicedev->device; |
| const char *path = device_get_path(device); |
| |
| if (btvoicedev->audioOverBle != NULL) return; |
| |
| if (btvoicedev->attioid != 0) { |
| btd_device_remove_attio_callback(device, btvoicedev->attioid); |
| btvoicedev->attioid = 0; |
| } |
| |
| if (btvoicedev->attrib != NULL) { |
| g_attrib_unref(btvoicedev->attrib); |
| btvoicedev->attrib = NULL; |
| } |
| |
| if (btvoicedev->device != NULL) { |
| btd_device_unref(btvoicedev->device); |
| btvoicedev->device = NULL; |
| } |
| } |
| |
| void btvoice_unregister_audioOverBle(struct btd_device *device) { |
| struct btvoice_device *btvoicedev; |
| |
| btvoicedev = find_btvoicedev(device); |
| if (btvoicedev == NULL) return; |
| |
| g_free(btvoicedev->audioOverBle); |
| btvoicedev->audioOverBle = NULL; |
| btvoicedev->voiceenable.audioOverBle = FALSE; |
| |
| cleanup_btdevice(btvoicedev); |
| |
| g_free(btvoicedev); |
| } |