hsp_ag.c (5611a760af48d1ce1beea59c7908be73bd2393f1) hsp_ag.c (f8fbdce0c5067e7e7edd3a29934b1f9b79c8ff2d)
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

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

141
142static void emit_event_audio_connected(uint8_t status, uint16_t handle){
143 if (!hsp_hs_callback) return;
144 uint8_t event[6];
145 event[0] = HCI_EVENT_HSP_META;
146 event[1] = sizeof(event) - 2;
147 event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE;
148 event[3] = status;
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

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

141
142static void emit_event_audio_connected(uint8_t status, uint16_t handle){
143 if (!hsp_hs_callback) return;
144 uint8_t event[6];
145 event[0] = HCI_EVENT_HSP_META;
146 event[1] = sizeof(event) - 2;
147 event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE;
148 event[3] = status;
149 bt_store_16(event, 4, handle);
149 little_endian_store_16(event, 4, handle);
150 (*hsp_hs_callback)(event, sizeof(event));
151}
152
153void hsp_ag_create_service(uint8_t * service, int rfcomm_channel_nr, const char * name){
154 uint8_t* attribute;
155 de_create_sequence(service);
156
157 // 0x0000 "Service Record Handle"

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

501 // inform about pin code request
502 printf("Pin code request - using '0000'\n\r");
503 bt_flip_addr(event_addr, &packet[2]);
504 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
505 break;
506 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
507 int index = 2;
508 uint8_t status = packet[index++];
150 (*hsp_hs_callback)(event, sizeof(event));
151}
152
153void hsp_ag_create_service(uint8_t * service, int rfcomm_channel_nr, const char * name){
154 uint8_t* attribute;
155 de_create_sequence(service);
156
157 // 0x0000 "Service Record Handle"

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

501 // inform about pin code request
502 printf("Pin code request - using '0000'\n\r");
503 bt_flip_addr(event_addr, &packet[2]);
504 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
505 break;
506 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
507 int index = 2;
508 uint8_t status = packet[index++];
509 sco_handle = READ_BT_16(packet, index);
509 sco_handle = little_endian_read_16(packet, index);
510 index+=2;
511 bd_addr_t address;
512 memcpy(address, &packet[index], 6);
513 index+=6;
514 uint8_t link_type = packet[index++];
515 uint8_t transmission_interval = packet[index++]; // measured in slots
516 uint8_t retransmission_interval = packet[index++];// measured in slots
510 index+=2;
511 bd_addr_t address;
512 memcpy(address, &packet[index], 6);
513 index+=6;
514 uint8_t link_type = packet[index++];
515 uint8_t transmission_interval = packet[index++]; // measured in slots
516 uint8_t retransmission_interval = packet[index++];// measured in slots
517 uint16_t rx_packet_length = READ_BT_16(packet, index); // measured in bytes
517 uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes
518 index+=2;
518 index+=2;
519 uint16_t tx_packet_length = READ_BT_16(packet, index); // measured in bytes
519 uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes
520 index+=2;
521 uint8_t air_mode = packet[index];
522
523 if (status != 0){
524 log_error("(e)SCO Connection failed, status %u", status);
525 emit_event_audio_connected(status, sco_handle);
526 break;
527 }

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

554 break;
555 }
556
557 case RFCOMM_EVENT_INCOMING_CONNECTION:
558 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
559 if (hsp_state != HSP_IDLE) return;
560
561 bt_flip_addr(event_addr, &packet[2]);
520 index+=2;
521 uint8_t air_mode = packet[index];
522
523 if (status != 0){
524 log_error("(e)SCO Connection failed, status %u", status);
525 emit_event_audio_connected(status, sco_handle);
526 break;
527 }

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

554 break;
555 }
556
557 case RFCOMM_EVENT_INCOMING_CONNECTION:
558 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
559 if (hsp_state != HSP_IDLE) return;
560
561 bt_flip_addr(event_addr, &packet[2]);
562 rfcomm_cid = READ_BT_16(packet, 9);
562 rfcomm_cid = little_endian_read_16(packet, 9);
563 printf("RFCOMM channel %u requested for %s\n", packet[8], bd_addr_to_str(event_addr));
564 rfcomm_accept_connection(rfcomm_cid);
565
566 hsp_state = HSP_W4_RFCOMM_CONNECTED;
567
568 break;
569
570 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
571 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
572 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
573 if (packet[2]) {
574 printf("RFCOMM channel open failed, status %u\n", packet[2]);
575 hsp_ag_reset_state();
576 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]);
577 } else {
578 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
563 printf("RFCOMM channel %u requested for %s\n", packet[8], bd_addr_to_str(event_addr));
564 rfcomm_accept_connection(rfcomm_cid);
565
566 hsp_state = HSP_W4_RFCOMM_CONNECTED;
567
568 break;
569
570 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
571 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
572 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
573 if (packet[2]) {
574 printf("RFCOMM channel open failed, status %u\n", packet[2]);
575 hsp_ag_reset_state();
576 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]);
577 } else {
578 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
579 rfcomm_handle = READ_BT_16(packet, 9);
580 rfcomm_cid = READ_BT_16(packet, 12);
581 mtu = READ_BT_16(packet, 14);
579 rfcomm_handle = little_endian_read_16(packet, 9);
580 rfcomm_cid = little_endian_read_16(packet, 12);
581 mtu = little_endian_read_16(packet, 14);
582 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d\n", rfcomm_cid, mtu, hsp_state);
583
584 switch (hsp_state){
585 case HSP_W4_RFCOMM_CONNECTED:
586 ag_num_button_press_received = 0;
587 hsp_state = HSP_W2_CONNECT_SCO;
588 break;
589 case HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:

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

598 case DAEMON_EVENT_HCI_PACKET_SENT:
599 case RFCOMM_EVENT_CREDITS:
600 break;
601
602 case HCI_EVENT_DISCONNECTION_COMPLETE:
603 if (hsp_state != HSP_W4_SCO_DISCONNECTED){
604 log_info("received gap disconnect in wrong hsp state");
605 }
582 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d\n", rfcomm_cid, mtu, hsp_state);
583
584 switch (hsp_state){
585 case HSP_W4_RFCOMM_CONNECTED:
586 ag_num_button_press_received = 0;
587 hsp_state = HSP_W2_CONNECT_SCO;
588 break;
589 case HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:

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

598 case DAEMON_EVENT_HCI_PACKET_SENT:
599 case RFCOMM_EVENT_CREDITS:
600 break;
601
602 case HCI_EVENT_DISCONNECTION_COMPLETE:
603 if (hsp_state != HSP_W4_SCO_DISCONNECTED){
604 log_info("received gap disconnect in wrong hsp state");
605 }
606 handle = READ_BT_16(packet,3);
606 handle = little_endian_read_16(packet,3);
607 if (handle == sco_handle){
608 printf("SCO disconnected, w2 disconnect RFCOMM\n");
609 sco_handle = 0;
610 hsp_state = HSP_W2_DISCONNECT_RFCOMM;
611 break;
612 }
613 break;
614 case RFCOMM_EVENT_CHANNEL_CLOSED:

--- 40 unchanged lines hidden ---
607 if (handle == sco_handle){
608 printf("SCO disconnected, w2 disconnect RFCOMM\n");
609 sco_handle = 0;
610 hsp_state = HSP_W2_DISCONNECT_RFCOMM;
611 break;
612 }
613 break;
614 case RFCOMM_EVENT_CHANNEL_CLOSED:

--- 40 unchanged lines hidden ---