2016-01-14 13:48:02 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 BlueKitchen GmbH
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
* monetary gain.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Please inquire about commercial licensing options at
|
|
|
|
* contact@bluekitchen-gmbh.com
|
|
|
|
*
|
|
|
|
*/
|
2016-03-10 12:29:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hsp_hs_demo.c
|
|
|
|
*/
|
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
// *****************************************************************************
|
2016-03-10 12:29:54 +01:00
|
|
|
/* EXAMPLE_START(hsp_hs_demo): HSP Headset Demo
|
|
|
|
*
|
|
|
|
* @text This example implements a HSP Headset device that sends and receives
|
|
|
|
* audio signal over HCI SCO. It demonstrates how to receive
|
|
|
|
* an output from a remote audio gateway (AG), and,
|
|
|
|
* if HAVE_STDIO is defined, how to control the AG.
|
|
|
|
*/
|
2016-01-14 13:48:02 +01:00
|
|
|
// *****************************************************************************
|
|
|
|
|
2016-03-10 12:29:54 +01:00
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
#include "btstack-config.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2016-03-17 11:49:30 +01:00
|
|
|
#include <unistd.h>
|
2016-01-14 13:48:02 +01:00
|
|
|
|
|
|
|
#include <btstack/hci_cmds.h>
|
|
|
|
#include <btstack/run_loop.h>
|
|
|
|
#include <btstack/sdp_util.h>
|
|
|
|
|
|
|
|
#include "sdp.h"
|
|
|
|
#include "hsp_hs.h"
|
2016-03-17 11:49:30 +01:00
|
|
|
#include "stdin_support.h"
|
2016-01-14 13:48:02 +01:00
|
|
|
|
|
|
|
#include "hci.h"
|
|
|
|
#include "l2cap.h"
|
2016-03-17 11:49:30 +01:00
|
|
|
#include "rfcomm.h"
|
2016-01-14 13:48:02 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
2016-03-17 11:49:30 +01:00
|
|
|
#define SCO_REPORT_PERIOD 255
|
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
static uint32_t hsp_service_buffer[150/4]; // implicit alignment to 4-byte memory address
|
|
|
|
static const uint8_t rfcomm_channel_nr = 1;
|
|
|
|
static const char hsp_hs_service_name[] = "Headset Test";
|
|
|
|
static uint16_t sco_handle = 0;
|
|
|
|
|
|
|
|
static char hs_cmd_buffer[100];
|
2016-03-23 14:37:26 +01:00
|
|
|
static bd_addr_t device_addr = {0x00,0x1b,0xDC,0x07,0x32,0xEF};
|
2016-01-14 13:48:02 +01:00
|
|
|
|
|
|
|
static int phase = 0;
|
|
|
|
|
2016-03-17 11:49:30 +01:00
|
|
|
// input signal: pre-computed sine wave, 160 Hz
|
|
|
|
static const uint8_t sine[] = {
|
|
|
|
0, 15, 31, 46, 61, 74, 86, 97, 107, 114,
|
|
|
|
120, 124, 126, 126, 124, 120, 114, 107, 97, 86,
|
|
|
|
74, 61, 46, 31, 15, 0, 241, 225, 210, 195,
|
|
|
|
182, 170, 159, 149, 142, 136, 132, 130, 130, 132,
|
|
|
|
136, 142, 149, 159, 170, 182, 195, 210, 225, 241,
|
|
|
|
};
|
|
|
|
|
2016-03-10 12:29:54 +01:00
|
|
|
/* @section Audio Transfer Setup
|
|
|
|
*
|
|
|
|
* @text A pre-computed sine wave (160Hz) is used as the input audio signal. 160 Hz.
|
|
|
|
* To send and receive an audio signal, HAVE_SCO_OVER_HCI has to be defined.
|
|
|
|
*
|
|
|
|
* Tested working setups:
|
|
|
|
* - Ubuntu 14 64-bit, CC2564B connected via FTDI USB-2-UART adapter, 921600 baud
|
2016-03-17 11:49:30 +01:00
|
|
|
* - Ubuntu 14 64-bit, CSR USB dongle
|
|
|
|
* - OS X 10.11, CSR USB dongle
|
2016-03-10 12:29:54 +01:00
|
|
|
*
|
|
|
|
* Broken setups:
|
|
|
|
* - OS X 10.11, CC2564B connected via FDTI USB-2-UART adapter, 921600 baud
|
|
|
|
* - select(..) blocks > 400 ms -> num completed is received to late -> gaps between audio
|
|
|
|
* - looks like bug in select->FTDI driver as it works correct on Linux
|
|
|
|
*
|
|
|
|
* SCO not routed over HCI yet:
|
|
|
|
* - CSR UART dongle
|
|
|
|
* - Broadcom USB dongle
|
|
|
|
* - Broadcom UART chipset
|
|
|
|
* - ..
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-23 14:37:26 +01:00
|
|
|
|
2016-03-17 11:49:30 +01:00
|
|
|
static void show_usage(void){
|
|
|
|
uint8_t iut_address_type;
|
|
|
|
bd_addr_t iut_address;
|
|
|
|
hci_le_advertisement_address(&iut_address_type, iut_address);
|
|
|
|
|
|
|
|
printf("\n--- Bluetooth HSP Headset Test Console %s ---\n", bd_addr_to_str(iut_address));
|
|
|
|
printf("---\n");
|
2016-03-23 14:37:26 +01:00
|
|
|
printf("c - Connect to %s\n", bd_addr_to_str(device_addr));
|
2016-03-17 11:49:30 +01:00
|
|
|
printf("C - Disconnect\n");
|
|
|
|
printf("a - establish audio connection\n");
|
|
|
|
printf("A - release audio connection\n");
|
|
|
|
printf("b - press user button\n");
|
|
|
|
printf("z - set microphone gain 0\n");
|
|
|
|
printf("m - set microphone gain 8\n");
|
|
|
|
printf("M - set microphone gain 15\n");
|
|
|
|
printf("o - set speaker gain 0\n");
|
|
|
|
printf("s - set speaker gain 8\n");
|
|
|
|
printf("S - set speaker gain 15\n");
|
|
|
|
printf("---\n");
|
|
|
|
printf("Ctrl-c - exit\n");
|
|
|
|
printf("---\n");
|
|
|
|
}
|
|
|
|
|
2016-03-23 14:37:26 +01:00
|
|
|
#ifdef HAVE_STDIO
|
2016-03-17 11:49:30 +01:00
|
|
|
static int stdin_process(struct data_source *ds){
|
|
|
|
char buffer;
|
|
|
|
read(ds->fd, &buffer, 1);
|
2016-01-14 13:48:02 +01:00
|
|
|
|
2016-03-17 11:49:30 +01:00
|
|
|
switch (buffer){
|
|
|
|
case 'c':
|
2016-03-23 14:37:26 +01:00
|
|
|
printf("Connect to %s\n", bd_addr_to_str(device_addr));
|
|
|
|
hsp_hs_connect(device_addr);
|
2016-03-17 11:49:30 +01:00
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
printf("Disconnect.\n");
|
|
|
|
hsp_hs_disconnect();
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
printf("Establish audio connection\n");
|
|
|
|
hsp_hs_establish_audio_connection();
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
printf("Release audio connection\n");
|
|
|
|
hsp_hs_release_audio_connection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'z':
|
|
|
|
printf("Setting microphone gain 0\n");
|
|
|
|
hsp_hs_set_microphone_gain(0);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
printf("Setting microphone gain 8\n");
|
|
|
|
hsp_hs_set_microphone_gain(8);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
printf("Setting microphone gain 15\n");
|
|
|
|
hsp_hs_set_microphone_gain(15);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
printf("Setting speaker gain 0\n");
|
|
|
|
hsp_hs_set_speaker_gain(0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
printf("Setting speaker gain 8\n");
|
|
|
|
hsp_hs_set_speaker_gain(8);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
printf("Setting speaker gain 15\n");
|
|
|
|
hsp_hs_set_speaker_gain(15);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
printf("Press user button\n");
|
|
|
|
hsp_hs_send_button_press();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
show_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-23 14:37:26 +01:00
|
|
|
#endif
|
2016-01-14 13:48:02 +01:00
|
|
|
|
|
|
|
static void try_send_sco(void){
|
|
|
|
if (!sco_handle) return;
|
|
|
|
if (!hci_can_send_sco_packet_now(sco_handle)) {
|
|
|
|
// printf("try_send_sco, cannot send now\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-01-16 21:57:34 +01:00
|
|
|
|
|
|
|
const int sco_packet_length = hci_get_sco_packet_length();
|
|
|
|
const int sco_payload_length = sco_packet_length - 3;
|
|
|
|
const int frames_per_packet = sco_payload_length; // for 8-bit data. for 16-bit data it's /2
|
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
uint8_t * sco_packet = hci_get_outgoing_packet_buffer();
|
|
|
|
// set handle + flags
|
|
|
|
bt_store_16(sco_packet, 0, sco_handle);
|
|
|
|
// set len
|
2016-01-16 21:57:34 +01:00
|
|
|
sco_packet[2] = sco_payload_length;
|
2016-01-14 13:48:02 +01:00
|
|
|
int i;
|
|
|
|
for (i=0;i<frames_per_packet;i++){
|
|
|
|
sco_packet[3+i] = sine[phase];
|
|
|
|
phase++;
|
|
|
|
if (phase >= sizeof(sine)) phase = 0;
|
|
|
|
}
|
2016-01-16 21:57:34 +01:00
|
|
|
hci_send_sco_packet_buffer(sco_packet_length);
|
2016-01-14 13:48:02 +01:00
|
|
|
static int count = 0;
|
|
|
|
count++;
|
2016-03-17 11:49:30 +01:00
|
|
|
if ((count & SCO_REPORT_PERIOD) == 0) printf("Sent %u\n", count);
|
2016-01-14 13:48:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){
|
|
|
|
static int count = 0;
|
|
|
|
count++;
|
2016-03-17 11:49:30 +01:00
|
|
|
if ((count & SCO_REPORT_PERIOD)) return;
|
2016-01-14 13:48:02 +01:00
|
|
|
printf("SCO packets %u\n", count);
|
2016-03-17 11:49:30 +01:00
|
|
|
hexdumpf(packet, size);
|
2016-01-14 13:48:02 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 13:52:46 +01:00
|
|
|
static void packet_handler(uint8_t * event, uint16_t event_size){
|
2016-01-14 13:48:02 +01:00
|
|
|
switch (event[0]) {
|
|
|
|
case BTSTACK_EVENT_STATE:
|
|
|
|
if (event[2] != HCI_STATE_WORKING) break;
|
2016-03-17 11:49:30 +01:00
|
|
|
show_usage();
|
2016-01-14 13:48:02 +01:00
|
|
|
break;
|
|
|
|
case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
|
|
|
|
case DAEMON_EVENT_HCI_PACKET_SENT:
|
2016-03-16 16:35:09 +01:00
|
|
|
try_send_sco();
|
2016-01-14 13:48:02 +01:00
|
|
|
break;
|
|
|
|
case HCI_EVENT_HSP_META:
|
|
|
|
switch (event[2]) {
|
2016-03-17 11:49:30 +01:00
|
|
|
case HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE:
|
|
|
|
if (event[3] == 0){
|
|
|
|
printf("RFCOMM connection established.\n");
|
|
|
|
} else {
|
|
|
|
printf("RFCOMM connection establishement failed.\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE:
|
|
|
|
if (event[3] == 0){
|
|
|
|
printf("RFCOMM disconnected.\n");
|
|
|
|
} else {
|
|
|
|
printf("RFCOMM disconnection failed.\n");
|
|
|
|
}
|
|
|
|
break;
|
2016-01-14 13:48:02 +01:00
|
|
|
case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE:
|
|
|
|
if (event[3] == 0){
|
2016-01-24 19:07:46 +01:00
|
|
|
sco_handle = READ_BT_16(event, 4);
|
2016-01-14 13:48:02 +01:00
|
|
|
printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
|
2016-03-16 16:35:09 +01:00
|
|
|
try_send_sco();
|
2016-01-14 13:48:02 +01:00
|
|
|
} else {
|
|
|
|
printf("Audio connection establishment failed with status %u\n", event[3]);
|
2016-01-24 19:07:46 +01:00
|
|
|
sco_handle = 0;
|
2016-01-14 13:48:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE:
|
|
|
|
if (event[3] == 0){
|
|
|
|
printf("Audio connection released.\n\n");
|
2016-03-17 13:37:59 +01:00
|
|
|
sco_handle = 0;
|
2016-01-14 13:48:02 +01:00
|
|
|
} else {
|
|
|
|
printf("Audio connection releasing failed with status %u\n", event[3]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED:
|
|
|
|
printf("Received microphone gain change %d\n", event[3]);
|
|
|
|
break;
|
|
|
|
case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED:
|
|
|
|
printf("Received speaker gain change %d\n", event[3]);
|
|
|
|
break;
|
|
|
|
case HSP_SUBEVENT_RING:
|
|
|
|
printf("HS: RING RING!\n");
|
|
|
|
break;
|
|
|
|
case HSP_SUBEVENT_AG_INDICATION:
|
|
|
|
memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer));
|
2016-02-17 12:07:24 +01:00
|
|
|
int size = event[3] <= sizeof(hs_cmd_buffer)? event[3] : sizeof(hs_cmd_buffer);
|
|
|
|
memcpy(hs_cmd_buffer, &event[4], size - 1);
|
2016-01-14 13:48:02 +01:00
|
|
|
printf("Received custom indication: \"%s\". \nExit code or call hsp_hs_send_result.\n", hs_cmd_buffer);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("event not handled %u\n", event[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 12:29:54 +01:00
|
|
|
/* @section Main Application Setup
|
|
|
|
*
|
|
|
|
* @text Listing MainConfiguration shows main application code.
|
|
|
|
* To run a HSP Headset service you need to initialize the SDP, and to create and register HSP HS record with it.
|
|
|
|
* In this example, the SCO over HCI is used to receive and send an audio signal.
|
|
|
|
*
|
|
|
|
* Two packet handlers are registered:
|
|
|
|
* - The HCI SCO packet handler receives audio data.
|
|
|
|
* - The HSP HS packet handler is used to trigger sending of audio data and commands to the AG. It also receives the AG's answers.
|
|
|
|
*
|
2016-03-17 11:49:30 +01:00
|
|
|
* The stdin_process callback allows for sending commands to the AG.
|
2016-03-10 12:29:54 +01:00
|
|
|
* At the end the Bluetooth stack is started.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* LISTING_START(MainConfiguration): Setup packet handlers and audio data channel for HSP Headset */
|
2016-01-14 13:52:46 +01:00
|
|
|
int btstack_main(int argc, const char * argv[]);
|
2016-01-14 13:48:02 +01:00
|
|
|
int btstack_main(int argc, const char * argv[]){
|
2016-03-17 11:49:30 +01:00
|
|
|
l2cap_init();
|
|
|
|
rfcomm_init();
|
2016-03-10 12:29:54 +01:00
|
|
|
sdp_init();
|
2016-03-17 11:49:30 +01:00
|
|
|
|
2016-03-10 12:29:54 +01:00
|
|
|
memset((uint8_t *)hsp_service_buffer, 0, sizeof(hsp_service_buffer));
|
2016-03-17 13:37:43 +01:00
|
|
|
|
|
|
|
/* LISTING_PAUSE */
|
|
|
|
#ifdef EMBEDDED
|
|
|
|
/* LISTING_RESUME */
|
|
|
|
service_record_item_t * service_record_item = (service_record_item_t *) hsp_service_buffer;
|
|
|
|
hsp_hs_create_sdp_record((uint8_t*) &service_record_item->service_record, rfcomm_channel_nr, hsp_hs_service_name, 0);
|
|
|
|
printf("SDP service buffer size: %u\n", (uint16_t) (sizeof(service_record_item_t) + de_get_len((uint8_t*) &service_record_item->service_record)));
|
|
|
|
sdp_register_service_internal(NULL, service_record_item);
|
|
|
|
/* LISTING_PAUSE */
|
|
|
|
#else
|
|
|
|
hsp_hs_create_sdp_record((uint8_t *) hsp_service_buffer, rfcomm_channel_nr, hsp_hs_service_name, 0);
|
|
|
|
printf("SDP service record size: %u\n", de_get_len((uint8_t*) hsp_service_buffer));
|
|
|
|
sdp_register_service_internal(NULL, (uint8_t*)hsp_service_buffer);
|
|
|
|
#endif
|
|
|
|
/* LISTING_RESUME */
|
2016-01-15 23:15:52 +01:00
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
hci_register_sco_packet_handler(&sco_packet_handler);
|
2016-03-10 12:29:54 +01:00
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
hsp_hs_init(rfcomm_channel_nr);
|
|
|
|
hsp_hs_register_packet_handler(packet_handler);
|
|
|
|
|
2016-03-23 14:37:26 +01:00
|
|
|
#ifdef HAVE_STDIO
|
2016-03-17 11:49:30 +01:00
|
|
|
btstack_stdin_setup(stdin_process);
|
2016-03-23 14:37:26 +01:00
|
|
|
#endif
|
2016-03-17 11:49:30 +01:00
|
|
|
|
2016-03-10 12:29:54 +01:00
|
|
|
gap_set_local_name("BTstack HSP HS");
|
|
|
|
hci_discoverable_control(1);
|
|
|
|
hci_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
|
2016-01-14 13:48:02 +01:00
|
|
|
// turn on!
|
2016-03-17 11:49:30 +01:00
|
|
|
hci_set_class_of_device(0x240404);
|
|
|
|
|
2016-01-14 13:48:02 +01:00
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|