hfp.c (ba92fcf681e9bd548155fb5a2e911e0807725060) hfp.c (a0653c3b5c210bfe291627d8de2733f878ea442b)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 189 unchanged lines hidden (view full) ---

198 uint8_t event[4];
199 event[0] = HCI_EVENT_HFP_META;
200 event[1] = sizeof(event) - 2;
201 event[2] = event_subtype;
202 event[3] = value; // status 0 == OK
203 (*callback)(event, sizeof(event));
204}
205
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 189 unchanged lines hidden (view full) ---

198 uint8_t event[4];
199 event[0] = HCI_EVENT_HFP_META;
200 event[1] = sizeof(event) - 2;
201 event[2] = event_subtype;
202 event[3] = value; // status 0 == OK
203 (*callback)(event, sizeof(event));
204}
205
206void hfp_emit_string_event(hfp_callback_t callback, uint8_t event_subtype, const char * value){
206void hfp_emit_connection_event(hfp_callback_t callback, uint8_t event_subtype, uint8_t status, hci_con_handle_t con_handle){
207 if (!callback) return;
207 if (!callback) return;
208 uint8_t event[40];
208 uint8_t event[6];
209 event[0] = HCI_EVENT_HFP_META;
210 event[1] = sizeof(event) - 2;
211 event[2] = event_subtype;
209 event[0] = HCI_EVENT_HFP_META;
210 event[1] = sizeof(event) - 2;
211 event[2] = event_subtype;
212 int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4;
213 strncpy((char*)&event[3], value, size);
214 event[3 + size] = 0;
212 event[3] = status; // status 0 == OK
213 little_endian_store_16(event, 4, con_handle);
215 (*callback)(event, sizeof(event));
216}
217
214 (*callback)(event, sizeof(event));
215}
216
218static void hfp_emit_audio_connection_established_event(hfp_callback_t callback, uint8_t value, uint16_t sco_handle){
217void hfp_emit_string_event(hfp_callback_t callback, uint8_t event_subtype, const char * value){
219 if (!callback) return;
218 if (!callback) return;
220 uint8_t event[6];
219 uint8_t event[40];
221 event[0] = HCI_EVENT_HFP_META;
222 event[1] = sizeof(event) - 2;
220 event[0] = HCI_EVENT_HFP_META;
221 event[1] = sizeof(event) - 2;
223 event[2] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
224 event[3] = value; // status 0 == OK
225 little_endian_store_16(event, 4, sco_handle);
222 event[2] = event_subtype;
223 int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4;
224 strncpy((char*)&event[3], value, size);
225 event[3 + size] = 0;
226 (*callback)(event, sizeof(event));
227}
228
229btstack_linked_list_t * hfp_get_connections(void){
230 return (btstack_linked_list_t *) &hfp_connections;
231}
232
233hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){

--- 232 unchanged lines hidden (view full) ---

466 rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
467 status = rfcomm_event_channel_opened_get_status(packet);
468 // printf("RFCOMM_EVENT_CHANNEL_OPENED packet_handler adddr %s, status %u\n", bd_addr_to_str(event_addr), status);
469
470 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
471 if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return;
472
473 if (status) {
226 (*callback)(event, sizeof(event));
227}
228
229btstack_linked_list_t * hfp_get_connections(void){
230 return (btstack_linked_list_t *) &hfp_connections;
231}
232
233hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){

--- 232 unchanged lines hidden (view full) ---

466 rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
467 status = rfcomm_event_channel_opened_get_status(packet);
468 // printf("RFCOMM_EVENT_CHANNEL_OPENED packet_handler adddr %s, status %u\n", bd_addr_to_str(event_addr), status);
469
470 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
471 if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return;
472
473 if (status) {
474 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, status);
474 hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, status, rfcomm_event_channel_opened_get_con_handle(packet));
475 remove_hfp_connection_context(hfp_connection);
476 } else {
477 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
478 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
479 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
480 // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu);
481
482 switch (hfp_connection->state){

--- 90 unchanged lines hidden (view full) ---

573 if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
574 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
575 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
576 break;
577 }
578 hfp_connection->sco_handle = sco_handle;
579 hfp_connection->establish_audio_connection = 0;
580 hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
475 remove_hfp_connection_context(hfp_connection);
476 } else {
477 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
478 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
479 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
480 // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu);
481
482 switch (hfp_connection->state){

--- 90 unchanged lines hidden (view full) ---

573 if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
574 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
575 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
576 break;
577 }
578 hfp_connection->sco_handle = sco_handle;
579 hfp_connection->establish_audio_connection = 0;
580 hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
581 hfp_emit_audio_connection_established_event(hfp_callback, packet[2], sco_handle);
581 hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED, packet[2], sco_handle);
582 break;
583 }
584
585 case RFCOMM_EVENT_CHANNEL_CLOSED:
586 rfcomm_cid = little_endian_read_16(packet,2);
587 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
588 if (!hfp_connection) break;
589 if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){

--- 755 unchanged lines hidden ---
582 break;
583 }
584
585 case RFCOMM_EVENT_CHANNEL_CLOSED:
586 rfcomm_cid = little_endian_read_16(packet,2);
587 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
588 if (!hfp_connection) break;
589 if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){

--- 755 unchanged lines hidden ---