Merge branch 'master' into develop

This commit is contained in:
Matthias Ringwald 2016-03-17 14:16:38 +01:00
commit 99a1006758
7 changed files with 1397 additions and 216 deletions

1214
include/btstack/hci_cmds.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -769,40 +769,57 @@ typedef uint8_t sm_key_t[16];
/** HSP Subevent */ /** HSP Subevent */
// data: event(8), len(8), subevent(8), status(8)
/** /**
* @format 112 * @format 11
* @param subevent_code * @param subevent_code
* @param status 0 == OK * @param status 0 == OK
* @param handle
*/ */
#define HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE 0x01 #define HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE 0x01
// data: event(8), len(8), subevent(8), status(8)
/**
* @format 11
* @param subevent_code
* @param status 0 == OK
*/
#define HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE 0x02
// data: event(8), len(8), subevent(8), status(8)
/**
* @format 11
* @param subevent_code
* @param status 0 == OK
*/
#define HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE 0x03
/** /**
* @format 11 * @format 11
* @param subevent_code * @param subevent_code
* @param status 0 == OK * @param status 0 == OK
*/ */
#define HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE 0x02 #define HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE 0x04
/** /**
* @format 1 * @format 1
* @param subevent_code * @param subevent_code
*/ */
#define HSP_SUBEVENT_RING 0x03 #define HSP_SUBEVENT_RING 0x05
/** /**
* @format 11 * @format 11
* @param subevent_code * @param subevent_code
* @param gain Valid range: [0,15] * @param gain Valid range: [0,15]
*/ */
#define HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED 0x04 #define HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED 0x06
/** /**
* @format 11 * @format 11
* @param subevent_code * @param subevent_code
* @param gain Valid range: [0,15] * @param gain Valid range: [0,15]
*/ */
#define HSP_SUBEVENT_SPEAKER_GAIN_CHANGED 0x05 #define HSP_SUBEVENT_SPEAKER_GAIN_CHANGED 0x07
/** /**
* @format 1JV * @format 1JV
@ -810,7 +827,7 @@ typedef uint8_t sm_key_t[16];
* @param value_length * @param value_length
* @param value * @param value
*/ */
#define HSP_SUBEVENT_HS_COMMAND 0x06 #define HSP_SUBEVENT_HS_COMMAND 0x08
/** /**
* @format 1JV * @format 1JV
@ -818,7 +835,7 @@ typedef uint8_t sm_key_t[16];
* @param value_length * @param value_length
* @param value * @param value
*/ */
#define HSP_SUBEVENT_AG_INDICATION 0x07 #define HSP_SUBEVENT_AG_INDICATION 0x09
/** HFP Subevent */ /** HFP Subevent */

View File

@ -103,6 +103,10 @@ static hfp_phone_number_t * subscriber_numbers = NULL;
static int subscriber_numbers_count = 0; static int subscriber_numbers_count = 0;
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static void hfp_run_for_context(hfp_connection_t *hfp_connection);
static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){

View File

@ -87,24 +87,33 @@ static int hs_microphone_gain = -1;
static int hs_speaker_gain = -1; static int hs_speaker_gain = -1;
static uint8_t hs_send_button_press = 0; static uint8_t hs_send_button_press = 0;
static uint8_t hs_support_custom_indications = 0; static uint8_t wait_ok = 0;
static uint8_t hs_outgoing_connection = 0;
static uint8_t hs_support_custom_indications = 0;
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static uint8_t hsp_disconnect_rfcomm = 0;
static uint8_t hsp_establish_audio_connection = 0;
static uint8_t hsp_release_audio_connection = 0;
typedef enum { typedef enum {
HSP_IDLE, HSP_IDLE,
HSP_SDP_QUERY_RFCOMM_CHANNEL, HSP_SDP_QUERY_RFCOMM_CHANNEL,
HSP_W4_SDP_EVENT_QUERY_COMPLETE, HSP_W4_SDP_QUERY_COMPLETE,
HSP_W4_RFCOMM_CONNECTED, HSP_W4_RFCOMM_CONNECTED,
HSP_W4_USER_ACTION,
HSP_RFCOMM_CONNECTION_ESTABLISHED,
HSP_W2_CONNECT_SCO, HSP_W2_CONNECT_SCO,
HSP_W4_SCO_CONNECTED, HSP_W4_SCO_CONNECTED,
HSP_ACTIVE,
HSP_AUDIO_CONNECTION_ESTABLISHED,
HSP_W2_DISCONNECT_SCO, HSP_W2_DISCONNECT_SCO,
HSP_W4_SCO_DISCONNECTED, HSP_W4_SCO_DISCONNECTED,
HSP_W2_DISCONNECT_RFCOMM, HSP_W2_DISCONNECT_RFCOMM,
HSP_W4_RFCOMM_DISCONNECTED, HSP_W4_RFCOMM_DISCONNECTED,
HSP_W4_RFCOMM_DISCONNECTED_AND_RESTART,
HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN
} hsp_state_t; } hsp_state_t;
@ -250,15 +259,11 @@ void hsp_hs_create_sdp_record(uint8_t * service, uint32_t service_record_handle
static void hsp_hs_reset_state(void){ static void hsp_hs_reset_state(void){
hsp_state = HSP_IDLE; hsp_state = HSP_IDLE;
rfcomm_cid = 0;
rfcomm_handle = 0;
sco_handle = 0;
hs_microphone_gain = -1; hs_microphone_gain = -1;
hs_speaker_gain = -1; hs_speaker_gain = -1;
hs_send_button_press = 0; hs_send_button_press = 0;
wait_ok = 0;
hs_support_custom_indications = 0; hs_support_custom_indications = 0;
} }
@ -279,27 +284,59 @@ void hsp_hs_init(uint8_t rfcomm_channel_nr){
void hsp_hs_connect(bd_addr_t bd_addr){ void hsp_hs_connect(bd_addr_t bd_addr){
if (hsp_state != HSP_IDLE) return; if (hsp_state != HSP_IDLE) return;
hs_outgoing_connection = 1;
hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL; hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL;
memcpy(remote, bd_addr, 6); memcpy(remote, bd_addr, 6);
hsp_run(); hsp_run();
} }
void hsp_hs_disconnect(bd_addr_t bd_addr){ void hsp_hs_disconnect(void){
hsp_hs_release_audio_connection();
if (hsp_state < HSP_W4_RFCOMM_CONNECTED){
hsp_state = HSP_IDLE;
return;
}
if (hsp_state == HSP_W4_RFCOMM_CONNECTED){
hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
return;
}
if (hsp_state < HSP_W4_SCO_CONNECTED){
hsp_state = HSP_W2_DISCONNECT_RFCOMM;
return;
}
if (hsp_state < HSP_W4_SCO_DISCONNECTED){
hsp_state = HSP_W2_DISCONNECT_SCO;
return;
}
hsp_disconnect_rfcomm = 1;
hsp_run();
}
void hsp_hs_establish_audio_connection(void){
switch (hsp_state){ switch (hsp_state){
case HSP_ACTIVE: case HSP_RFCOMM_CONNECTION_ESTABLISHED:
hsp_state = HSP_W4_USER_ACTION; hsp_establish_audio_connection = 1;
hs_send_button_press = 1; hsp_state = HSP_W4_SCO_CONNECTED;
break; break;
case HSP_W4_RFCOMM_CONNECTED: case HSP_W4_RFCOMM_CONNECTED:
hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
break; break;
default: default:
return; break;
} }
hsp_run(); hsp_run();
} }
void hsp_hs_release_audio_connection(void){
if (hsp_state >= HSP_W2_DISCONNECT_SCO) return;
if (hsp_state < HSP_AUDIO_CONNECTION_ESTABLISHED) return;
hsp_release_audio_connection = 1;
hsp_run();
}
void hsp_hs_set_microphone_gain(uint8_t gain){ void hsp_hs_set_microphone_gain(uint8_t gain){
if (gain < 0 || gain >15) { if (gain < 0 || gain >15) {
@ -322,57 +359,64 @@ void hsp_hs_set_speaker_gain(uint8_t gain){
static void hsp_run(void){ static void hsp_run(void){
int err; if (!rfcomm_can_send_packet_now(rfcomm_cid)) return;
if (wait_ok) return;
if (hsp_release_audio_connection){
hsp_release_audio_connection = 0;
wait_ok = 1;
hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD);
return;
}
if (hsp_disconnect_rfcomm){
hsp_disconnect_rfcomm = 0;
hsp_establish_audio_connection = 0;
rfcomm_disconnect(rfcomm_cid);
return;
}
if (hsp_establish_audio_connection){
hsp_establish_audio_connection = 0;
wait_ok = 1;
hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD);
return;
}
if (hs_send_button_press){ if (hs_send_button_press){
hs_send_button_press = 0; hs_send_button_press = 0;
err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD); wait_ok = 1;
if (err) { hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD);
hs_send_button_press = 1;
}
return; return;
} }
switch (hsp_state){ switch (hsp_state){
case HSP_SDP_QUERY_RFCOMM_CHANNEL: case HSP_SDP_QUERY_RFCOMM_CHANNEL:
hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE; hsp_state = HSP_W4_SDP_QUERY_COMPLETE;
sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_Headset_AG); sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_Headset_AG);
break; break;
case HSP_W2_CONNECT_SCO: case HSP_AUDIO_CONNECTION_ESTABLISHED:
hsp_state = HSP_W4_SCO_CONNECTED; case HSP_RFCOMM_CONNECTION_ESTABLISHED :
break;
case HSP_W2_DISCONNECT_SCO:
hsp_state = HSP_W4_SCO_DISCONNECTED;
break;
case HSP_ACTIVE: if (hs_microphone_gain >= 0){
if (hs_microphone_gain >= 0){
int gain = hs_microphone_gain;
hs_microphone_gain = -1;
char buffer[20]; char buffer[20];
sprintf(buffer, "%s=%d\r\n", HSP_HS_MICROPHONE_GAIN, gain); sprintf(buffer, "%s=%d\r\n", HSP_HS_MICROPHONE_GAIN, hs_microphone_gain);
err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer);
if (err) { hs_microphone_gain = -1;
hs_microphone_gain = gain;
}
break; break;
} }
if (hs_speaker_gain >= 0){ if (hs_speaker_gain >= 0){
int gain = hs_speaker_gain;
hs_speaker_gain = -1;
char buffer[20]; char buffer[20];
sprintf(buffer, "%s=%d\r\n", HSP_HS_SPEAKER_GAIN, gain); sprintf(buffer, "%s=%d\r\n", HSP_HS_SPEAKER_GAIN, hs_speaker_gain);
err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer);
if (err) { hs_speaker_gain = -1;
hs_speaker_gain = gain;
}
break; break;
} }
break;
case HSP_W4_RFCOMM_DISCONNECTED:
rfcomm_disconnect(rfcomm_cid);
break; break;
default: default:
break; break;
@ -391,16 +435,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
if (strncmp((char *)packet, HSP_AG_RING, strlen(HSP_AG_RING)) == 0){ if (strncmp((char *)packet, HSP_AG_RING, strlen(HSP_AG_RING)) == 0){
emit_ring_event(); emit_ring_event();
} else if (strncmp((char *)packet, HSP_AG_OK, strlen(HSP_AG_OK)) == 0){ } else if (strncmp((char *)packet, HSP_AG_OK, strlen(HSP_AG_OK)) == 0){
switch (hsp_state){ wait_ok = 0;
case HSP_W4_RFCOMM_CONNECTED:
hsp_state = HSP_W2_CONNECT_SCO;
break;
case HSP_W4_USER_ACTION:
hsp_state = HSP_W2_DISCONNECT_SCO;
break;
default:
break;
}
} else if (strncmp((char *)packet, HSP_MICROPHONE_GAIN, strlen(HSP_MICROPHONE_GAIN)) == 0){ } else if (strncmp((char *)packet, HSP_MICROPHONE_GAIN, strlen(HSP_MICROPHONE_GAIN)) == 0){
uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_MICROPHONE_GAIN)]); uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_MICROPHONE_GAIN)]);
emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain); emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain);
@ -435,6 +470,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
switch (event) { switch (event) {
case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
if (hsp_state < HSP_RFCOMM_CONNECTION_ESTABLISHED) return;
int index = 2; int index = 2;
uint8_t status = packet[index++]; uint8_t status = packet[index++];
sco_handle = little_endian_read_16(packet, index); sco_handle = little_endian_read_16(packet, index);
@ -454,8 +490,10 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
if (status != 0){ if (status != 0){
log_error("(e)SCO Connection failed, status %u", status); log_error("(e)SCO Connection failed, status %u", status);
emit_event_audio_connected(status, sco_handle); emit_event_audio_connected(status, sco_handle);
hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED ;
break; break;
} }
switch (link_type){ switch (link_type){
case 0x00: case 0x00:
log_info("SCO Connection established."); log_info("SCO Connection established.");
@ -475,16 +513,11 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
" rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle,
bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
if (hsp_state == HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
hsp_state = HSP_W2_DISCONNECT_SCO;
break;
}
// forward event to app // forward event to app
hsp_hs_callback(packet, size); hsp_hs_callback(packet, size);
hsp_state = HSP_ACTIVE; hsp_state = HSP_AUDIO_CONNECTION_ESTABLISHED;
emit_event_audio_connected(0, sco_handle); emit_event_audio_connected(status, sco_handle);
break; break;
} }
@ -495,42 +528,27 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
reverse_bd_addr(&packet[2], event_addr); reverse_bd_addr(&packet[2], event_addr);
rfcomm_cid = little_endian_read_16(packet, 9); rfcomm_cid = little_endian_read_16(packet, 9);
log_info("RFCOMM channel %u requested for %s", packet[8], bd_addr_to_str(event_addr)); log_info("RFCOMM channel %u requested for %s", packet[8], bd_addr_to_str(event_addr));
rfcomm_accept_connection(rfcomm_cid);
hsp_state = HSP_W4_RFCOMM_CONNECTED; hsp_state = HSP_W4_RFCOMM_CONNECTED;
rfcomm_accept_connection(rfcomm_cid);
break; break;
case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
// printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); // printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
// data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
if (hsp_state != HSP_W4_RFCOMM_CONNECTED) return;
if (packet[2]) { if (packet[2]) {
log_info("RFCOMM channel open failed, status %u", packet[2]); log_info("RFCOMM channel open failed, status %u", packet[2]);
hsp_state = HSP_IDLE;
hsp_hs_reset_state(); hsp_hs_reset_state();
emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]);
hs_outgoing_connection = 0;
} else { } else {
// data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16) // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
rfcomm_handle = little_endian_read_16(packet, 9); rfcomm_handle = little_endian_read_16(packet, 9);
rfcomm_cid = little_endian_read_16(packet, 12); rfcomm_cid = little_endian_read_16(packet, 12);
mtu = little_endian_read_16(packet, 14); mtu = little_endian_read_16(packet, 14);
log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u", rfcomm_cid, mtu); log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, handle %02x", rfcomm_cid, mtu, rfcomm_handle);
hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED;
if (hs_outgoing_connection){
hs_outgoing_connection = 0;
hs_send_button_press = 1;
}
switch (hsp_state){
case HSP_W4_RFCOMM_CONNECTED:
hsp_state = HSP_W2_CONNECT_SCO;
break;
case HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
hsp_state = HSP_W2_DISCONNECT_RFCOMM;
break;
default:
break;
}
} }
emit_event(HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE, packet[2]);
break; break;
case RFCOMM_EVENT_CAN_SEND_NOW: case RFCOMM_EVENT_CAN_SEND_NOW:
@ -541,13 +559,21 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
handle = little_endian_read_16(packet,3); handle = little_endian_read_16(packet,3);
if (handle == sco_handle){ if (handle == sco_handle){
sco_handle = 0; sco_handle = 0;
hsp_state = HSP_W2_DISCONNECT_RFCOMM; hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED ;
hsp_hs_reset_state();
emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0);
break; break;
}
if (handle == rfcomm_handle) {
rfcomm_handle = 0;
hsp_state = HSP_IDLE;
hsp_hs_callback(packet, size);
hsp_hs_reset_state();
} }
break; break;
case RFCOMM_EVENT_CHANNEL_CLOSED: case RFCOMM_EVENT_CHANNEL_CLOSED:
hsp_hs_reset_state(); hsp_hs_reset_state();
emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); hsp_hs_callback(packet, size);
break; break;
default: default:
break; break;
@ -564,7 +590,7 @@ static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uin
case SDP_EVENT_QUERY_COMPLETE: case SDP_EVENT_QUERY_COMPLETE:
if (channel_nr > 0){ if (channel_nr > 0){
hsp_state = HSP_W4_RFCOMM_CONNECTED; hsp_state = HSP_W4_RFCOMM_CONNECTED;
log_info("RFCOMM create channel"); log_info("HSP: SDP_QUERY_COMPLETE. RFCOMM create channel, addr %s, rfcomm channel nr %d", bd_addr_to_str(remote), channel_nr);
rfcomm_create_channel(packet_handler, remote, channel_nr, NULL); rfcomm_create_channel(packet_handler, remote, channel_nr, NULL);
break; break;
} }
@ -575,6 +601,7 @@ static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uin
} }
void hsp_hs_send_button_press(void){ void hsp_hs_send_button_press(void){
if (hsp_state < HSP_RFCOMM_CONNECTION_ESTABLISHED || hsp_state >= HSP_W4_RFCOMM_DISCONNECTED) return;
hs_send_button_press = 1; hs_send_button_press = 1;
hsp_run(); hsp_run();
} }

View File

@ -110,7 +110,27 @@ void hsp_hs_connect(bd_addr_t bd_addr);
* Releases the RFCOMM channel. * Releases the RFCOMM channel.
* @param bd_addr * @param bd_addr
*/ */
void hsp_hs_disconnect(bd_addr_t bd_addr); void hsp_hs_disconnect(void);
/**
* @brief Send button press action. Toggle establish/release of audio connection.
*/
void hsp_hs_send_button_press(void);
/**
* @brief Triger establishing audio connection.
*
* @param bd_addr
*/
void hsp_hs_establish_audio_connection(void);
/**
* @brief Trigger releasing audio connection.
*
* @param bd_addr
*/
void hsp_hs_release_audio_connection(void);
/** /**
* @brief Set microphone gain. * @brief Set microphone gain.
@ -130,11 +150,7 @@ void hsp_hs_set_microphone_gain(uint8_t gain);
*/ */
void hsp_hs_set_speaker_gain(uint8_t gain); void hsp_hs_set_speaker_gain(uint8_t gain);
/**
* @brief Send button press action.
* @param gain Valid range: [0,15]
*/
void hsp_hs_send_button_press(void);
/** /**
* @brief Enable custom indications. * @brief Enable custom indications.

View File

@ -49,19 +49,20 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "hci_cmd.h" #include "ble/sm.h"
#include "btstack_run_loop.h"
#include "hci.h"
#include "gap.h"
#include "btstack_memory.h"
#include "btstack_event.h" #include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.h"
#include "classic/rfcomm.h"
#include "classic/sdp_client_rfcomm.h"
#include "classic/sdp_server.h"
#include "classic/sdp_util.h"
#include "classic/spp_server.h"
#include "gap.h"
#include "hci.h"
#include "hci_cmd.h"
#include "hci_dump.h" #include "hci_dump.h"
#include "l2cap.h" #include "l2cap.h"
#include "classic/rfcomm.h"
#include "classic/sdp_server.h"
#include "classic/sdp_client_rfcomm.h"
#include "ble/sm.h"
#include "stdin_support.h" #include "stdin_support.h"
static void show_usage(); static void show_usage();

View File

@ -86,90 +86,6 @@ static char hs_cmd_buffer[100];
// prototypes // prototypes
static void show_usage(); static void show_usage();
#define TABLE_SIZE (50)
static uint8_t sine[TABLE_SIZE];
static void setup_sine(void){
// create sine wave table
int i;
for( i=0; i<TABLE_SIZE; i++ ) {
sine[i] = (int8_t) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
printf("%02u %u\n", i, sine[i]);
}
}
#ifdef HAVE_PORTAUDIO
#include <portaudio.h>
// portaudio config
#define NUM_CHANNELS 1
#define SAMPLE_RATE 8000
#define FRAMES_PER_BUFFER 1000
#define PA_SAMPLE_TYPE paInt8
// portaudio globals
int phase = 0;
static PaStream * stream;
static void try_send_sco(void){
printf("try send handle %x\n", sco_handle);
if (!sco_handle) return;
if (!hci_can_send_sco_packet_now()) {
printf("try_send_sco, cannot send now\n");
return;
}
const int frames_per_packet = 9;
hci_reserve_packet_buffer();
uint8_t * sco_packet = hci_get_outgoing_packet_buffer();
// set handle + flags
little_endian_store_16(sco_packet, 0, sco_handle);
// set len
sco_packet[2] = frames_per_packet;
int i;
for (i=0;i<frames_per_packet;i++){
sco_packet[3+i] = sine[phase];
phase++;
if (phase >= TABLE_SIZE) phase = 0;
}
hci_send_sco_packet_buffer(3 + frames_per_packet);
}
static void setup_audio(void){
int err;
PaStreamParameters outputParameters;
/* -- initialize PortAudio -- */
err = Pa_Initialize();
if( err != paNoError ) return;
/* -- setup input and output -- */
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
outputParameters.channelCount = NUM_CHANNELS;
outputParameters.sampleFormat = PA_SAMPLE_TYPE;
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
/* -- setup stream -- */
err = Pa_OpenStream(
&stream,
NULL, // &inputParameters,
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
NULL, /* no callback, use blocking API */
NULL ); /* no callback, so no callback userData */
if( err != paNoError ) return;
/* -- start stream -- */
err = Pa_StartStream( stream );
if( err != paNoError ) return;
printf("Portaudio setup\n");
}
static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){
Pa_WriteStream( stream, &packet[3], size -3);
}
#endif
// Testig User Interface // Testig User Interface
static void show_usage(void){ static void show_usage(void){
uint8_t iut_address_type; uint8_t iut_address_type;
@ -210,7 +126,7 @@ static int stdin_process(struct btstack_data_source *ds){
break; break;
case 'd': case 'd':
printf("Releasing audio connection.\n"); printf("Releasing audio connection.\n");
hsp_hs_disconnect(current_addr); hsp_hs_disconnect();
break; break;
case 'z': case 'z':
printf("Setting microphone gain 0\n"); printf("Setting microphone gain 0\n");
@ -250,33 +166,26 @@ static int stdin_process(struct btstack_data_source *ds){
static void packet_handler(uint8_t * event, uint16_t event_size){ static void packet_handler(uint8_t * event, uint16_t event_size){
// printf("Packet handler event 0x%02x\n", event[0]); // printf("Packet handler event 0x%02x\n", event[0]);
// try_send_sco();
switch (event[0]) { switch (event[0]) {
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
if (event[2] != HCI_STATE_WORKING) break; if (event[2] != HCI_STATE_WORKING) break;
show_usage(); show_usage();
// request loopback mode
hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1);
break; break;
case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:
// printf("HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE status %u, %x\n", event[2], little_endian_read_16(event, 3));
if (event[2]) break;
sco_handle = little_endian_read_16(event, 3);
break;
case HCI_EVENT_HSP_META: case HCI_EVENT_HSP_META:
switch (event[2]) { switch (event[2]) {
case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE: case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE:
if (event[3] == 0){ if (event[3] == 0){
sco_handle = little_endian_read_16(event, 4);
printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
// try_send_sco();
} else { } else {
printf("Audio connection establishment failed with status %u\n", event[3]); printf("Audio connection establishment failed with status %u\n", event[3]);
sco_handle = 0;
} }
break; break;
case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE: case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE:
if (event[3] == 0){ if (event[3] == 0){
printf("Audio connection released.\n\n"); printf("Audio connection released.\n\n");
sco_handle = 0; sco_handle = 0;
} else { } else {
printf("Audio connection releasing failed with status %u\n", event[3]); printf("Audio connection releasing failed with status %u\n", event[3]);
} }
@ -309,13 +218,6 @@ static void packet_handler(uint8_t * event, uint16_t event_size){
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){ int btstack_main(int argc, const char * argv[]){
setup_sine();
#ifdef HAVE_PORTAUDIO
setup_audio();
hci_register_sco_packet_handler(&sco_packet_handler);
#endif
hsp_hs_init(rfcomm_channel_nr); hsp_hs_init(rfcomm_channel_nr);
hsp_hs_register_packet_handler(packet_handler); hsp_hs_register_packet_handler(packet_handler);