diff --git a/src/hsp_ag.c b/src/hsp_ag.c index 961f705a2..fbe24d502 100644 --- a/src/hsp_ag.c +++ b/src/hsp_ag.c @@ -37,7 +37,7 @@ // ***************************************************************************** // -// Minimal setup for HSP Audio Gateway (!! UNDER DEVELOPMENT !!) +// HSP Audio Gateway // // ***************************************************************************** @@ -60,8 +60,6 @@ #include "debug.h" #include "hsp_ag.h" -#define RFCOMM_SERVER_CHANNEL 1 - #define HSP_HS_BUTTON_PRESS "AT+CKPD=200" #define HSP_HS_AT_CKPD "AT+CKPD\r\n" #define HSP_AG_OK "\r\nOK\r\n" @@ -84,8 +82,6 @@ static uint16_t sco_handle = 0; static uint16_t rfcomm_handle = 0; static timer_source_t hs_timeout; -// static uint8_t connection_state = 0; - static int ag_microphone_gain = -1; static int ag_speaker_gain = -1; static uint8_t ag_ring = 0; @@ -221,14 +217,13 @@ static int hsp_ag_send_str_over_rfcomm(uint16_t cid, char * command){ if (!rfcomm_can_send_packet_now(cid)) return 1; int err = rfcomm_send_internal(cid, (uint8_t*) command, strlen(command)); if (err){ - printf("rfcomm_send_internal -> error 0X%02x", err); + log_error("rfcomm_send_internal -> error 0X%02x", err); return err; } - printf("Send string: \"%s\"\n", command); return err; } -void hsp_ag_support_custom_commands(int enable){ +void hsp_ag_enable_custom_commands(int enable){ ag_support_custom_commands = enable; } @@ -298,7 +293,7 @@ void hsp_ag_disconnect(void){ void hsp_ag_set_microphone_gain(uint8_t gain){ if (gain < 0 || gain >15) { - printf("Gain must be in interval [0..15], it is given %d\n", gain); + log_error("Gain must be in interval [0..15], it is given %d", gain); return; } ag_microphone_gain = gain; @@ -308,7 +303,7 @@ void hsp_ag_set_microphone_gain(uint8_t gain){ // AG +VGS=5 [0..15] ; HS AT+VGM=6 | AG OK void hsp_ag_set_speaker_gain(uint8_t gain){ if (gain < 0 || gain >15) { - printf("Gain must be in interval [0..15], it is given %d\n", gain); + log_error("Gain must be in interval [0..15], it is given %d", gain); return; } ag_speaker_gain = gain; @@ -368,7 +363,7 @@ static void hsp_run(void){ switch (hsp_state){ case HSP_SDP_QUERY_RFCOMM_CHANNEL: hsp_state = HSP_W4_SDP_QUERY_COMPLETE; - printf("Start SDP query %s, 0x%02x\n", bd_addr_to_str(remote), SDP_HSP); + log_info("Start SDP query %s, 0x%02x", bd_addr_to_str(remote), SDP_HSP); sdp_query_rfcomm_channel_and_name_for_uuid(remote, SDP_HSP); break; @@ -444,7 +439,7 @@ static void hsp_run(void){ static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ - // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); + // log_info("packet_handler type %u, packet[0] %x", packet_type, packet[0]); if (packet_type == RFCOMM_DATA_PACKET){ while (size > 0 && (packet[0] == '\n' || packet[0] == '\r')){ size--; @@ -452,7 +447,7 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha } if (strncmp((char *)packet, HSP_HS_BUTTON_PRESS, strlen(HSP_HS_BUTTON_PRESS)) == 0){ - printf("Received button press %s\n", HSP_HS_BUTTON_PRESS); + log_info("Received button press %s", HSP_HS_BUTTON_PRESS); ag_num_button_press_received++; ag_send_ok = 1; if (hsp_state == HSP_ACTIVE && ag_num_button_press_received >=2){ @@ -490,19 +485,6 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha uint16_t handle; switch (event) { - case BTSTACK_EVENT_STATE: - // BTstack activated, get started - if (packet[2] == HCI_STATE_WORKING){ - printf("BTstack activated, get started .\n"); - } - break; - - case HCI_EVENT_PIN_CODE_REQUEST: - // inform about pin code request - printf("Pin code request - using '0000'\n\r"); - bt_flip_addr(event_addr, &packet[2]); - hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); - break; case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ int index = 2; uint8_t status = packet[index++]; @@ -527,14 +509,14 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha } switch (link_type){ case 0x00: - printf("SCO Connection established. \n"); + log_info("SCO Connection established."); if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); break; case 0x02: - printf("eSCO Connection established. \n"); + log_info("eSCO Connection established."); break; default: log_error("(e)SCO reserved link_type 0x%2x", link_type); @@ -560,18 +542,17 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha bt_flip_addr(event_addr, &packet[2]); rfcomm_cid = READ_BT_16(packet, 9); - printf("RFCOMM channel %u requested for %s\n", 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_internal(rfcomm_cid); hsp_state = HSP_W4_RFCOMM_CONNECTED; - break; case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: - printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); + log_info("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x", 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) if (packet[2]) { - printf("RFCOMM channel open failed, status %u\n", packet[2]); + log_info("RFCOMM channel open failed, status %u§", packet[2]); hsp_ag_reset_state(); emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]); } else { @@ -579,7 +560,7 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha rfcomm_handle = READ_BT_16(packet, 9); rfcomm_cid = READ_BT_16(packet, 12); mtu = READ_BT_16(packet, 14); - printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d\n", rfcomm_cid, mtu, hsp_state); + log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d", rfcomm_cid, mtu, hsp_state); switch (hsp_state){ case HSP_W4_RFCOMM_CONNECTED: @@ -590,7 +571,7 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha hsp_state = HSP_W2_DISCONNECT_RFCOMM; break; default: - printf("no valid state\n"); + log_error("no valid state"); break; } } @@ -600,22 +581,16 @@ static void packet_handler (void * connection, uint8_t packet_type, uint16_t cha break; case HCI_EVENT_DISCONNECTION_COMPLETE: - if (hsp_state != HSP_W4_SCO_DISCONNECTED){ - log_info("received gap disconnect in wrong hsp state"); - } handle = READ_BT_16(packet,3); if (handle == sco_handle){ - printf("SCO disconnected, w2 disconnect RFCOMM\n"); + log_info("SCO disconnected, w2 disconnect RFCOMM"); sco_handle = 0; hsp_state = HSP_W2_DISCONNECT_RFCOMM; break; } break; case RFCOMM_EVENT_CHANNEL_CLOSED: - if (hsp_state != HSP_W4_RFCOMM_DISCONNECTED){ - log_info("received RFCOMM disconnect in wrong hsp state"); - } - printf("RFCOMM channel closed\n"); + log_info("RFCOMM channel closed"); hsp_ag_reset_state(); emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); break; @@ -633,19 +608,19 @@ static void handle_query_rfcomm_event(sdp_query_event_t * event, void * context) case SDP_QUERY_RFCOMM_SERVICE: ve = (sdp_query_rfcomm_service_event_t*) event; channel_nr = ve->channel_nr; - printf("** Service name: '%s', RFCOMM port %u\n", ve->service_name, channel_nr); + log_info("** Service name: '%s', RFCOMM port %u", ve->service_name, channel_nr); break; case SDP_QUERY_COMPLETE: ce = (sdp_query_complete_event_t*) event; if (channel_nr > 0){ hsp_state = HSP_W4_RFCOMM_CONNECTED; - printf("RFCOMM create channel. state %d\n", HSP_W4_RFCOMM_CONNECTED); + log_info("RFCOMM create channel. state %d", HSP_W4_RFCOMM_CONNECTED); rfcomm_create_channel_internal(NULL, remote, channel_nr); break; } hsp_ag_reset_state(); - printf("Service not found, status %u.\n", ce->status); + log_info("Service not found, status %u.", ce->status); if (ce->status){ emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, ce->status); } else { diff --git a/src/hsp_ag.h b/src/hsp_ag.h index 03764290b..7c9646021 100644 --- a/src/hsp_ag.h +++ b/src/hsp_ag.h @@ -37,7 +37,7 @@ // ***************************************************************************** // -// HSP Audio Gateway (!! UNDER DEVELOPMENT !!) +// HSP Audio Gateway // // ***************************************************************************** @@ -53,27 +53,34 @@ extern "C" { #endif typedef void (*hsp_ag_callback_t)(uint8_t * event, uint16_t event_size); -// Register callback (packet handler) for hsp audio gateway -void hsp_ag_register_packet_handler(hsp_ag_callback_t callback); -void hsp_ag_create_service(uint8_t * service, int rfcomm_channel_nr, const char * name); void hsp_ag_init(uint8_t rfcomm_channel_nr); + +void hsp_ag_create_service(uint8_t * service, int rfcomm_channel_nr, const char * name); + +// Register callback (packet handler) for hsp audio gateway +void hsp_ag_register_packet_handler(hsp_ag_callback_t callback); + void hsp_ag_connect(bd_addr_t bd_addr); + void hsp_ag_disconnect(void); // +VGM=[0..15] void hsp_ag_set_microphone_gain(uint8_t gain); + // +VGS=[0..15] void hsp_ag_set_speaker_gain(uint8_t gain); void hsp_ag_start_ringing(void); + void hsp_ag_stop_ringing(void); -void hsp_ag_support_custom_commands(int enable); - -// When support custom commands is enabled, AG will send HSP_SUBEVENT_HS_COMMAND. +// When support custom commands is enabled, AG will emit HSP_SUBEVENT_HS_COMMAND. // On occurance of this event, client's packet handler must send the result back // by calling hsp_ag_send_result function. + +void hsp_ag_enable_custom_commands(int enable); + int hsp_ag_send_result(char * result); #if defined __cplusplus