16983e65eSMilanka Ringwald /* 26983e65eSMilanka Ringwald * Copyright (C) 2016 BlueKitchen GmbH 36983e65eSMilanka Ringwald * 46983e65eSMilanka Ringwald * Redistribution and use in source and binary forms, with or without 56983e65eSMilanka Ringwald * modification, are permitted provided that the following conditions 66983e65eSMilanka Ringwald * are met: 76983e65eSMilanka Ringwald * 86983e65eSMilanka Ringwald * 1. Redistributions of source code must retain the above copyright 96983e65eSMilanka Ringwald * notice, this list of conditions and the following disclaimer. 106983e65eSMilanka Ringwald * 2. Redistributions in binary form must reproduce the above copyright 116983e65eSMilanka Ringwald * notice, this list of conditions and the following disclaimer in the 126983e65eSMilanka Ringwald * documentation and/or other materials provided with the distribution. 136983e65eSMilanka Ringwald * 3. Neither the name of the copyright holders nor the names of 146983e65eSMilanka Ringwald * contributors may be used to endorse or promote products derived 156983e65eSMilanka Ringwald * from this software without specific prior written permission. 166983e65eSMilanka Ringwald * 4. Any redistribution, use, or modification is done solely for 176983e65eSMilanka Ringwald * personal benefit and not for any commercial purpose or for 186983e65eSMilanka Ringwald * monetary gain. 196983e65eSMilanka Ringwald * 206983e65eSMilanka Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 216983e65eSMilanka Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 226983e65eSMilanka Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 236983e65eSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 246983e65eSMilanka Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 256983e65eSMilanka Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 266983e65eSMilanka Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 276983e65eSMilanka Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 286983e65eSMilanka Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 296983e65eSMilanka Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 306983e65eSMilanka Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 316983e65eSMilanka Ringwald * SUCH DAMAGE. 326983e65eSMilanka Ringwald * 336983e65eSMilanka Ringwald * Please inquire about commercial licensing options at 346983e65eSMilanka Ringwald * [email protected] 356983e65eSMilanka Ringwald * 366983e65eSMilanka Ringwald */ 376983e65eSMilanka Ringwald 3801dc6e35SMilanka Ringwald #define __BTSTACK_FILE__ "avrcp_controller.c" 396983e65eSMilanka Ringwald 406983e65eSMilanka Ringwald #include <stdint.h> 416983e65eSMilanka Ringwald #include <stdio.h> 426983e65eSMilanka Ringwald #include <stdlib.h> 436983e65eSMilanka Ringwald #include <string.h> 44adaba9f3SMatthias Ringwald #include <inttypes.h> 456983e65eSMilanka Ringwald 466983e65eSMilanka Ringwald #include "btstack.h" 476983e65eSMilanka Ringwald #include "classic/avrcp.h" 486983e65eSMilanka Ringwald #include "classic/avrcp_controller.h" 496983e65eSMilanka Ringwald 504adb0765SMatthias Ringwald // made public in avrcp_controller.h 514adb0765SMatthias Ringwald avrcp_context_t avrcp_controller_context; 524adb0765SMatthias Ringwald 536983e65eSMilanka Ringwald void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 546983e65eSMilanka Ringwald avrcp_create_sdp_record(1, service, service_record_handle, browsing, supported_features, service_name, service_provider_name); 556983e65eSMilanka Ringwald } 566983e65eSMilanka Ringwald 576983e65eSMilanka Ringwald static void avrcp_emit_repeat_and_shuffle_mode(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_repeat_mode_t repeat_mode, avrcp_shuffle_mode_t shuffle_mode){ 586983e65eSMilanka Ringwald if (!callback) return; 596983e65eSMilanka Ringwald uint8_t event[8]; 606983e65eSMilanka Ringwald int pos = 0; 616983e65eSMilanka Ringwald event[pos++] = HCI_EVENT_AVRCP_META; 626983e65eSMilanka Ringwald event[pos++] = sizeof(event) - 2; 636983e65eSMilanka Ringwald event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 646983e65eSMilanka Ringwald little_endian_store_16(event, pos, avrcp_cid); 656983e65eSMilanka Ringwald pos += 2; 666983e65eSMilanka Ringwald event[pos++] = ctype; 676983e65eSMilanka Ringwald event[pos++] = repeat_mode; 686983e65eSMilanka Ringwald event[pos++] = shuffle_mode; 696983e65eSMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 706983e65eSMilanka Ringwald } 716983e65eSMilanka Ringwald 726983e65eSMilanka Ringwald static void avrcp_emit_operation_status(btstack_packet_handler_t callback, uint8_t subevent, uint16_t avrcp_cid, uint8_t ctype, uint8_t operation_id){ 736983e65eSMilanka Ringwald if (!callback) return; 746983e65eSMilanka Ringwald uint8_t event[7]; 756983e65eSMilanka Ringwald int pos = 0; 766983e65eSMilanka Ringwald event[pos++] = HCI_EVENT_AVRCP_META; 776983e65eSMilanka Ringwald event[pos++] = sizeof(event) - 2; 786983e65eSMilanka Ringwald event[pos++] = subevent; 796983e65eSMilanka Ringwald little_endian_store_16(event, pos, avrcp_cid); 806983e65eSMilanka Ringwald pos += 2; 816983e65eSMilanka Ringwald event[pos++] = ctype; 826983e65eSMilanka Ringwald event[pos++] = operation_id; 836983e65eSMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 846983e65eSMilanka Ringwald } 856983e65eSMilanka Ringwald 866983e65eSMilanka Ringwald static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 876983e65eSMilanka Ringwald UNUSED(timer); 886983e65eSMilanka Ringwald avrcp_connection_t * connection = btstack_run_loop_get_timer_context(timer); 896983e65eSMilanka Ringwald btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 906983e65eSMilanka Ringwald btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 916983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 926983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 936983e65eSMilanka Ringwald } 946983e65eSMilanka Ringwald 956983e65eSMilanka Ringwald static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 966983e65eSMilanka Ringwald btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 976983e65eSMilanka Ringwald btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 986983e65eSMilanka Ringwald btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 996983e65eSMilanka Ringwald btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 1006983e65eSMilanka Ringwald btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 1016983e65eSMilanka Ringwald } 1026983e65eSMilanka Ringwald 1036983e65eSMilanka Ringwald static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 1046983e65eSMilanka Ringwald connection->continuous_fast_forward_cmd = 0; 1056983e65eSMilanka Ringwald btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 1066983e65eSMilanka Ringwald } 1076983e65eSMilanka Ringwald 1086983e65eSMilanka Ringwald static uint8_t request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 1096983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1106983e65eSMilanka Ringwald if (connection->continuous_fast_forward_cmd){ 1116983e65eSMilanka Ringwald avrcp_press_and_hold_timer_stop(connection); 1126983e65eSMilanka Ringwald } 1136983e65eSMilanka Ringwald connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 1146983e65eSMilanka Ringwald connection->transaction_label++; 1156983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1166983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 1176983e65eSMilanka Ringwald } 1186983e65eSMilanka Ringwald 1196983e65eSMilanka Ringwald static inline uint8_t request_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed, uint8_t continuous_fast_forward_cmd, avrcp_context_t * context){ 1206983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, context); 1216983e65eSMilanka Ringwald if (!connection){ 1226983e65eSMilanka Ringwald log_error("avrcp: could not find a connection."); 1236983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1246983e65eSMilanka Ringwald } 1256983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1266983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 1276983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 1286983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 1296983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1306983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 1316983e65eSMilanka Ringwald connection->cmd_operands_length = 0; 1326983e65eSMilanka Ringwald 1336983e65eSMilanka Ringwald connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd; 1346983e65eSMilanka Ringwald connection->cmd_operands_length = 2; 1356983e65eSMilanka Ringwald connection->cmd_operands[0] = opid; 1366983e65eSMilanka Ringwald if (playback_speed > 0){ 1376983e65eSMilanka Ringwald connection->cmd_operands[2] = playback_speed; 1386983e65eSMilanka Ringwald connection->cmd_operands_length++; 1396983e65eSMilanka Ringwald } 1406983e65eSMilanka Ringwald connection->cmd_operands[1] = connection->cmd_operands_length - 2; 1416983e65eSMilanka Ringwald 1426983e65eSMilanka Ringwald if (connection->continuous_fast_forward_cmd){ 1436983e65eSMilanka Ringwald avrcp_press_and_hold_timer_start(connection); 1446983e65eSMilanka Ringwald } 1456983e65eSMilanka Ringwald 1466983e65eSMilanka Ringwald connection->transaction_label++; 1476983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1486983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 1496983e65eSMilanka Ringwald } 1506983e65eSMilanka Ringwald 1516983e65eSMilanka Ringwald static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 1526983e65eSMilanka Ringwald return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0, &avrcp_controller_context); 1536983e65eSMilanka Ringwald } 1546983e65eSMilanka Ringwald 1556983e65eSMilanka Ringwald static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 1566983e65eSMilanka Ringwald return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1, &avrcp_controller_context); 1576983e65eSMilanka Ringwald } 1586983e65eSMilanka Ringwald 1596983e65eSMilanka Ringwald static int avrcp_send_cmd(uint16_t cid, avrcp_connection_t * connection){ 1606983e65eSMilanka Ringwald uint8_t command[30]; 1616983e65eSMilanka Ringwald int pos = 0; 1626983e65eSMilanka Ringwald // transport header 1636983e65eSMilanka Ringwald // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 1646983e65eSMilanka Ringwald command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 1656983e65eSMilanka Ringwald // Profile IDentifier (PID) 1666983e65eSMilanka Ringwald command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 1676983e65eSMilanka Ringwald command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 1686983e65eSMilanka Ringwald 1696983e65eSMilanka Ringwald // command_type 1706983e65eSMilanka Ringwald command[pos++] = connection->command_type; 1716983e65eSMilanka Ringwald // subunit_type | subunit ID 1726983e65eSMilanka Ringwald command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 1736983e65eSMilanka Ringwald // opcode 1746983e65eSMilanka Ringwald command[pos++] = (uint8_t)connection->command_opcode; 1756983e65eSMilanka Ringwald // operands 1766983e65eSMilanka Ringwald memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length); 1776983e65eSMilanka Ringwald pos += connection->cmd_operands_length; 1786983e65eSMilanka Ringwald 1796983e65eSMilanka Ringwald return l2cap_send(cid, command, pos); 1806983e65eSMilanka Ringwald } 1816983e65eSMilanka Ringwald 1826983e65eSMilanka Ringwald static int avrcp_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 1836983e65eSMilanka Ringwald if (connection->notifications_to_deregister & (1 << event_id)) return 0; 1846983e65eSMilanka Ringwald if (connection->notifications_enabled & (1 << event_id)) return 0; 1856983e65eSMilanka Ringwald if (connection->notifications_to_register & (1 << event_id)) return 0; 1866983e65eSMilanka Ringwald connection->notifications_to_register |= (1 << event_id); 1876983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1886983e65eSMilanka Ringwald return 1; 1896983e65eSMilanka Ringwald } 1906983e65eSMilanka Ringwald 1916983e65eSMilanka Ringwald static void avrcp_prepare_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 1926983e65eSMilanka Ringwald connection->transaction_label++; 1936983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1946983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_NOTIFY; 1956983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1966983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 1976983e65eSMilanka Ringwald int pos = 0; 1986983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1996983e65eSMilanka Ringwald pos += 3; 2006983e65eSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 2016983e65eSMilanka Ringwald connection->cmd_operands[pos++] = 0; // reserved(upper 6) | packet_type -> 0 2026983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 5); // parameter length 2036983e65eSMilanka Ringwald pos += 2; 2046983e65eSMilanka Ringwald connection->cmd_operands[pos++] = event_id; 2056983e65eSMilanka Ringwald big_endian_store_32(connection->cmd_operands, pos, 0); 2066983e65eSMilanka Ringwald pos += 4; 2076983e65eSMilanka Ringwald connection->cmd_operands_length = pos; 2086983e65eSMilanka Ringwald // AVRCP_SPEC_V14.pdf 166 2096983e65eSMilanka Ringwald // answer page 61 2106983e65eSMilanka Ringwald } 2116983e65eSMilanka Ringwald 2121bf7a74fSMilanka Ringwald 2131bf7a74fSMilanka Ringwald static void avrcp_parser_reset(avrcp_connection_t * connection){ 2141bf7a74fSMilanka Ringwald connection->list_offset = 0; 2151bf7a74fSMilanka Ringwald connection->num_attributes = 0; 2161bf7a74fSMilanka Ringwald connection->num_parsed_attributes = 0; 2171bf7a74fSMilanka Ringwald connection->parser_attribute_header_pos = 0; 2188b2b4034SMilanka Ringwald connection->num_received_fragments = 0; 2198b2b4034SMilanka Ringwald connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 2201bf7a74fSMilanka Ringwald } 2211bf7a74fSMilanka Ringwald 2221bf7a74fSMilanka Ringwald static void avrcp_controller_emit_now_playing_info_event_done(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, uint8_t status){ 2231bf7a74fSMilanka Ringwald uint8_t event[7]; 2241bf7a74fSMilanka Ringwald int pos = 0; 2251bf7a74fSMilanka Ringwald event[pos++] = HCI_EVENT_AVRCP_META; 2261bf7a74fSMilanka Ringwald event[pos++] = sizeof(event) - 2; 2271bf7a74fSMilanka Ringwald event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE; 2281bf7a74fSMilanka Ringwald little_endian_store_16(event, pos, avrcp_cid); 2291bf7a74fSMilanka Ringwald pos += 2; 2301bf7a74fSMilanka Ringwald event[pos++] = ctype; 2311bf7a74fSMilanka Ringwald event[pos++] = status; 2321bf7a74fSMilanka Ringwald // printf_hexdump(event, pos); 2331bf7a74fSMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, pos); 2341bf7a74fSMilanka Ringwald } 2351bf7a74fSMilanka Ringwald 2361bf7a74fSMilanka Ringwald static void avrcp_controller_emit_now_playing_info_event(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_media_attribute_id_t attr_id, uint8_t * value, uint16_t value_len){ 2371bf7a74fSMilanka Ringwald uint8_t event[HCI_EVENT_BUFFER_SIZE]; 2381bf7a74fSMilanka Ringwald int pos = 0; 2391bf7a74fSMilanka Ringwald event[pos++] = HCI_EVENT_AVRCP_META; 2401bf7a74fSMilanka Ringwald // reserve one byte for subevent type and data len 2411bf7a74fSMilanka Ringwald int data_len_pos = pos; 2421bf7a74fSMilanka Ringwald pos++; 2431bf7a74fSMilanka Ringwald int subevent_type_pos = pos; 2441bf7a74fSMilanka Ringwald pos++; 2451bf7a74fSMilanka Ringwald little_endian_store_16(event, pos, avrcp_cid); 2461bf7a74fSMilanka Ringwald pos += 2; 2471bf7a74fSMilanka Ringwald event[pos++] = ctype; 2481bf7a74fSMilanka Ringwald 2491bf7a74fSMilanka Ringwald switch (attr_id){ 2501bf7a74fSMilanka Ringwald case AVRCP_MEDIA_ATTR_TITLE: 2511bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO; 2521bf7a74fSMilanka Ringwald event[pos++] = value_len; 2531bf7a74fSMilanka Ringwald memcpy(event+pos, value, value_len); 2541bf7a74fSMilanka Ringwald break; 2551bf7a74fSMilanka Ringwald case AVRCP_MEDIA_ATTR_ARTIST: 2561bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO; 2571bf7a74fSMilanka Ringwald event[pos++] = value_len; 2581bf7a74fSMilanka Ringwald memcpy(event+pos, value, value_len); 2591bf7a74fSMilanka Ringwald break; 2601bf7a74fSMilanka Ringwald case AVRCP_MEDIA_ATTR_ALBUM: 2611bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO; 2621bf7a74fSMilanka Ringwald event[pos++] = value_len; 2631bf7a74fSMilanka Ringwald memcpy(event+pos, value, value_len); 2641bf7a74fSMilanka Ringwald break; 2651bf7a74fSMilanka Ringwald case AVRCP_MEDIA_ATTR_GENRE: 2661bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO; 2671bf7a74fSMilanka Ringwald event[pos++] = value_len; 2681bf7a74fSMilanka Ringwald memcpy(event+pos, value, value_len); 2691bf7a74fSMilanka Ringwald break; 2702f511ffbSMilanka Ringwald case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 2711bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO; 2721bf7a74fSMilanka Ringwald if (value){ 2731bf7a74fSMilanka Ringwald little_endian_store_32(event, pos, btstack_atoi((char *)value)); 2741bf7a74fSMilanka Ringwald } else { 2751bf7a74fSMilanka Ringwald little_endian_store_32(event, pos, 0); 2761bf7a74fSMilanka Ringwald } 2771bf7a74fSMilanka Ringwald pos += 4; 2781bf7a74fSMilanka Ringwald break; 2791bf7a74fSMilanka Ringwald case AVRCP_MEDIA_ATTR_TRACK: 2801bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO; 2811bf7a74fSMilanka Ringwald if (value){ 2821bf7a74fSMilanka Ringwald event[pos++] = btstack_atoi((char *)value); 2831bf7a74fSMilanka Ringwald } else { 2841bf7a74fSMilanka Ringwald event[pos++] = 0; 2851bf7a74fSMilanka Ringwald } 2861bf7a74fSMilanka Ringwald break; 287864d08b0SMilanka Ringwald case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 2881bf7a74fSMilanka Ringwald event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO; 2891bf7a74fSMilanka Ringwald if (value){ 2901bf7a74fSMilanka Ringwald event[pos++] = btstack_atoi((char *)value); 2911bf7a74fSMilanka Ringwald } else { 2921bf7a74fSMilanka Ringwald event[pos++] = 0; 2931bf7a74fSMilanka Ringwald } 2941bf7a74fSMilanka Ringwald break; 2951bf7a74fSMilanka Ringwald default: 2961bf7a74fSMilanka Ringwald break; 2971bf7a74fSMilanka Ringwald } 2981bf7a74fSMilanka Ringwald event[data_len_pos] = pos - 2; 2991bf7a74fSMilanka Ringwald // printf("send attr len %d, value %s\n", value_len, value); 3001bf7a74fSMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, pos); 3011bf7a74fSMilanka Ringwald } 3021bf7a74fSMilanka Ringwald 3031bf7a74fSMilanka Ringwald static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 304836408dfSMatthias Ringwald uint16_t attribute_total_value_len; 305836408dfSMatthias Ringwald uint32_t attribute_id; 3061bf7a74fSMilanka Ringwald switch(connection->parser_state){ 307836408dfSMatthias Ringwald case AVRCP_PARSER_GET_ATTRIBUTE_HEADER: 3081bf7a74fSMilanka Ringwald connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte; 3091bf7a74fSMilanka Ringwald connection->list_offset++; 310*9771e28cSMatthias Ringwald 311*9771e28cSMatthias Ringwald if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return; 312*9771e28cSMatthias Ringwald 313836408dfSMatthias Ringwald attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 3148b2b4034SMilanka Ringwald connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE); 315*9771e28cSMatthias Ringwald if (connection->attribute_value_len > 0){ 316*9771e28cSMatthias Ringwald // get ready for attribute value 317836408dfSMatthias Ringwald connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE; 318836408dfSMatthias Ringwald return; 319*9771e28cSMatthias Ringwald } 320*9771e28cSMatthias Ringwald 321*9771e28cSMatthias Ringwald // emit empty attribute 322*9771e28cSMatthias Ringwald attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 323*9771e28cSMatthias Ringwald avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, attribute_id, connection->attribute_value, connection->attribute_value_len); 324*9771e28cSMatthias Ringwald 325*9771e28cSMatthias Ringwald // done, see below 326*9771e28cSMatthias Ringwald break; 327836408dfSMatthias Ringwald 328836408dfSMatthias Ringwald case AVRCP_PARSER_GET_ATTRIBUTE_VALUE: 3291bf7a74fSMilanka Ringwald connection->attribute_value[connection->attribute_value_offset++] = byte; 3301bf7a74fSMilanka Ringwald connection->list_offset++; 331*9771e28cSMatthias Ringwald 332*9771e28cSMatthias Ringwald if (connection->attribute_value_offset < connection->attribute_value_len) return; 3338b2b4034SMilanka Ringwald 334836408dfSMatthias Ringwald // emit (potentially partial) attribute 335836408dfSMatthias Ringwald attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 3361bf7a74fSMilanka Ringwald avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, attribute_id, connection->attribute_value, connection->attribute_value_len); 3371bf7a74fSMilanka Ringwald 338836408dfSMatthias Ringwald attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 339836408dfSMatthias Ringwald if (connection->attribute_value_offset < attribute_total_value_len){ 340*9771e28cSMatthias Ringwald // ignore rest of attribute 34114e95846SMilanka Ringwald connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE; 342836408dfSMatthias Ringwald return; 3431bf7a74fSMilanka Ringwald } 344*9771e28cSMatthias Ringwald 345*9771e28cSMatthias Ringwald // done, see below 346836408dfSMatthias Ringwald break; 3471bf7a74fSMilanka Ringwald 34814e95846SMilanka Ringwald case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE: 3491bf7a74fSMilanka Ringwald connection->attribute_value_offset++; 350*9771e28cSMatthias Ringwald connection->list_offset++; 351*9771e28cSMatthias Ringwald 352*9771e28cSMatthias Ringwald attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 353*9771e28cSMatthias Ringwald if (connection->attribute_value_offset < attribute_total_value_len) return; 354*9771e28cSMatthias Ringwald 355*9771e28cSMatthias Ringwald // done, see below 356836408dfSMatthias Ringwald break; 3571bf7a74fSMilanka Ringwald 358836408dfSMatthias Ringwald default: 359836408dfSMatthias Ringwald return; 3601bf7a74fSMilanka Ringwald } 361836408dfSMatthias Ringwald 362836408dfSMatthias Ringwald // attribute fully read, check if more to come 363836408dfSMatthias Ringwald if (connection->list_offset < connection->list_size){ 364*9771e28cSMatthias Ringwald // more to come, reset parser 3651bf7a74fSMilanka Ringwald connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 3661bf7a74fSMilanka Ringwald connection->parser_attribute_header_pos = 0; 367836408dfSMatthias Ringwald } else { 368836408dfSMatthias Ringwald // fully done 369836408dfSMatthias Ringwald avrcp_parser_reset(connection); 370836408dfSMatthias Ringwald avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 3711bf7a74fSMilanka Ringwald } 3721bf7a74fSMilanka Ringwald } 3731bf7a74fSMilanka Ringwald 3741bf7a74fSMilanka Ringwald static void avrcp_source_parse_and_emit_element_attrs(uint8_t * packet, uint16_t num_bytes_to_read, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 3751bf7a74fSMilanka Ringwald int i; 3761bf7a74fSMilanka Ringwald for (i=0;i<num_bytes_to_read;i++){ 3771bf7a74fSMilanka Ringwald avrcp_parser_process_byte(packet[i], connection, ctype); 3781bf7a74fSMilanka Ringwald } 3791bf7a74fSMilanka Ringwald } 3801bf7a74fSMilanka Ringwald 3811bf7a74fSMilanka Ringwald static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 3821bf7a74fSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 3831bf7a74fSMilanka Ringwald connection->transaction_label++; 3841bf7a74fSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 3851bf7a74fSMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 3861bf7a74fSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 3871bf7a74fSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 3881bf7a74fSMilanka Ringwald int pos = 0; 3891bf7a74fSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 3901bf7a74fSMilanka Ringwald pos += 3; 3911bf7a74fSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID 3921bf7a74fSMilanka Ringwald connection->cmd_operands[pos++] = 0; 3931bf7a74fSMilanka Ringwald // Parameter Length 3941bf7a74fSMilanka Ringwald connection->cmd_operands_length = 8; 3951bf7a74fSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 1); 3961bf7a74fSMilanka Ringwald pos += 2; 3971bf7a74fSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 3981bf7a74fSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 3991bf7a74fSMilanka Ringwald return ERROR_CODE_SUCCESS; 4001bf7a74fSMilanka Ringwald } 4011bf7a74fSMilanka Ringwald 4021bf7a74fSMilanka Ringwald 4031bf7a74fSMilanka Ringwald static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 4041bf7a74fSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 4051bf7a74fSMilanka Ringwald connection->transaction_label++; 4061bf7a74fSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 4071bf7a74fSMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 4081bf7a74fSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 4091bf7a74fSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 4101bf7a74fSMilanka Ringwald int pos = 0; 4111bf7a74fSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 4121bf7a74fSMilanka Ringwald pos += 3; 4131bf7a74fSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID 4141bf7a74fSMilanka Ringwald connection->cmd_operands[pos++] = 0; 4151bf7a74fSMilanka Ringwald // Parameter Length 4161bf7a74fSMilanka Ringwald connection->cmd_operands_length = 8; 4171bf7a74fSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 1); 4181bf7a74fSMilanka Ringwald pos += 2; 4191bf7a74fSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 4201bf7a74fSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 4211bf7a74fSMilanka Ringwald return ERROR_CODE_SUCCESS; 4221bf7a74fSMilanka Ringwald } 4231bf7a74fSMilanka Ringwald 4246983e65eSMilanka Ringwald static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 4256983e65eSMilanka Ringwald uint8_t operands[20]; 4266983e65eSMilanka Ringwald uint8_t opcode; 4276983e65eSMilanka Ringwald int pos = 3; 4286983e65eSMilanka Ringwald // uint8_t transport_header = packet[0]; 4296983e65eSMilanka Ringwald // uint8_t transaction_label = transport_header >> 4; 4306983e65eSMilanka Ringwald // uint8_t packet_type = (transport_header & 0x0F) >> 2; 4316983e65eSMilanka Ringwald // uint8_t frame_type = (transport_header & 0x03) >> 1; 4326983e65eSMilanka Ringwald // uint8_t ipid = transport_header & 0x01; 4336983e65eSMilanka Ringwald // uint8_t byte_value = packet[2]; 4346983e65eSMilanka Ringwald // uint16_t pid = (byte_value << 8) | packet[2]; 4356983e65eSMilanka Ringwald 4366983e65eSMilanka Ringwald avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 4376983e65eSMilanka Ringwald uint8_t byte_value = packet[pos++]; 4386983e65eSMilanka Ringwald avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 4396983e65eSMilanka Ringwald avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 4406983e65eSMilanka Ringwald opcode = packet[pos++]; 4416983e65eSMilanka Ringwald 4426983e65eSMilanka Ringwald // printf(" Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n", 4436983e65eSMilanka Ringwald // transport_header, transaction_label, packet_type, frame_type, ipid, pid); 4446983e65eSMilanka Ringwald // // printf_hexdump(packet+pos, size-pos); 4456983e65eSMilanka Ringwald 4466983e65eSMilanka Ringwald uint8_t pdu_id; 4476983e65eSMilanka Ringwald uint16_t param_length; 4486983e65eSMilanka Ringwald switch (avrcp_cmd_opcode(packet,size)){ 449ad75da14SMilanka Ringwald case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 450ad75da14SMilanka Ringwald if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 451ad75da14SMilanka Ringwald connection->state = AVCTP_CONNECTION_OPENED; 452ad75da14SMilanka Ringwald // operands: 453ad75da14SMilanka Ringwald memcpy(operands, packet+pos, 5); 454ad75da14SMilanka Ringwald uint8_t unit_type = operands[1] >> 3; 455ad75da14SMilanka Ringwald uint8_t max_subunit_ID = operands[1] & 0x07; 456ad75da14SMilanka Ringwald log_info(" SUBUNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, max_subunit_ID %d", ctype, subunit_type, subunit_id, opcode, unit_type, max_subunit_ID); 457ad75da14SMilanka Ringwald break; 458ad75da14SMilanka Ringwald } 4596983e65eSMilanka Ringwald case AVRCP_CMD_OPCODE_UNIT_INFO:{ 4606983e65eSMilanka Ringwald if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 4616983e65eSMilanka Ringwald connection->state = AVCTP_CONNECTION_OPENED; 4626983e65eSMilanka Ringwald // operands: 4636983e65eSMilanka Ringwald memcpy(operands, packet+pos, 5); 4646983e65eSMilanka Ringwald uint8_t unit_type = operands[1] >> 3; 4656983e65eSMilanka Ringwald uint8_t unit = operands[1] & 0x07; 4666983e65eSMilanka Ringwald uint32_t company_id = operands[2] << 16 | operands[3] << 8 | operands[4]; 467adaba9f3SMatthias Ringwald log_info(" UNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06" PRIx32, 4686983e65eSMilanka Ringwald ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 4696983e65eSMilanka Ringwald break; 4706983e65eSMilanka Ringwald } 4716983e65eSMilanka Ringwald case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 4726983e65eSMilanka Ringwald if (size - pos < 7) { 4736983e65eSMilanka Ringwald log_error("avrcp: wrong packet size"); 4746983e65eSMilanka Ringwald return; 4756983e65eSMilanka Ringwald }; 4766983e65eSMilanka Ringwald // operands: 4776983e65eSMilanka Ringwald memcpy(operands, packet+pos, 7); 4786983e65eSMilanka Ringwald pos += 7; 4796983e65eSMilanka Ringwald // uint32_t company_id = operands[0] << 16 | operands[1] << 8 | operands[2]; 4806983e65eSMilanka Ringwald pdu_id = operands[3]; 4816983e65eSMilanka Ringwald 4826983e65eSMilanka Ringwald if (connection->state != AVCTP_W2_RECEIVE_RESPONSE && pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 4836983e65eSMilanka Ringwald log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 4846983e65eSMilanka Ringwald return; 4856983e65eSMilanka Ringwald } 4866983e65eSMilanka Ringwald connection->state = AVCTP_CONNECTION_OPENED; 4876983e65eSMilanka Ringwald 4886983e65eSMilanka Ringwald 4896983e65eSMilanka Ringwald // uint8_t unit_type = operands[4] >> 3; 4906983e65eSMilanka Ringwald // uint8_t unit = operands[4] & 0x07; 4916983e65eSMilanka Ringwald param_length = big_endian_read_16(operands, 5); 4926983e65eSMilanka Ringwald 4936983e65eSMilanka Ringwald // printf(" VENDOR DEPENDENT response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06x\n", 4946983e65eSMilanka Ringwald // ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 4956983e65eSMilanka Ringwald 4966983e65eSMilanka Ringwald // if (ctype == AVRCP_CTYPE_RESPONSE_INTERIM) return; 4976983e65eSMilanka Ringwald log_info(" VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 4986983e65eSMilanka Ringwald switch (pdu_id){ 4996983e65eSMilanka Ringwald case AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue:{ 5006983e65eSMilanka Ringwald uint8_t num_attributes = packet[pos++]; 5016983e65eSMilanka Ringwald int i; 5026983e65eSMilanka Ringwald avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 5036983e65eSMilanka Ringwald avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 5046983e65eSMilanka Ringwald for (i = 0; i < num_attributes; i++){ 5056983e65eSMilanka Ringwald uint8_t attribute_id = packet[pos++]; 5066983e65eSMilanka Ringwald uint8_t value = packet[pos++]; 5076983e65eSMilanka Ringwald switch (attribute_id){ 5086983e65eSMilanka Ringwald case 0x02: 5096983e65eSMilanka Ringwald repeat_mode = (avrcp_repeat_mode_t) value; 5106983e65eSMilanka Ringwald break; 5116983e65eSMilanka Ringwald case 0x03: 5126983e65eSMilanka Ringwald shuffle_mode = (avrcp_shuffle_mode_t) value; 5136983e65eSMilanka Ringwald break; 5146983e65eSMilanka Ringwald default: 5156983e65eSMilanka Ringwald break; 5166983e65eSMilanka Ringwald } 5176983e65eSMilanka Ringwald } 5186983e65eSMilanka Ringwald avrcp_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 5196983e65eSMilanka Ringwald break; 5206983e65eSMilanka Ringwald } 5216983e65eSMilanka Ringwald case AVRCP_PDU_ID_SetPlayerApplicationSettingValue:{ 5226983e65eSMilanka Ringwald uint8_t event[6]; 5236983e65eSMilanka Ringwald int offset = 0; 5246983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 5256983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 5266983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 5276983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 5286983e65eSMilanka Ringwald offset += 2; 5296983e65eSMilanka Ringwald event[offset++] = ctype; 5306983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 5316983e65eSMilanka Ringwald break; 5326983e65eSMilanka Ringwald } 5336983e65eSMilanka Ringwald case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 5346983e65eSMilanka Ringwald uint8_t event[7]; 5356983e65eSMilanka Ringwald int offset = 0; 5366983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 5376983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 5386983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 5396983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 5406983e65eSMilanka Ringwald offset += 2; 5416983e65eSMilanka Ringwald event[offset++] = ctype; 5426983e65eSMilanka Ringwald event[offset++] = packet[pos++]; 5436983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 5446983e65eSMilanka Ringwald break; 5456983e65eSMilanka Ringwald } 5466983e65eSMilanka Ringwald case AVRCP_PDU_ID_GET_CAPABILITIES:{ 5476983e65eSMilanka Ringwald avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 5486983e65eSMilanka Ringwald uint8_t capability_count = packet[pos++]; 5496983e65eSMilanka Ringwald int i; 5506983e65eSMilanka Ringwald switch (capability_id){ 5516983e65eSMilanka Ringwald case AVRCP_CAPABILITY_ID_COMPANY: 5526983e65eSMilanka Ringwald // log_info("Supported companies %d: ", capability_count); 5536983e65eSMilanka Ringwald for (i = 0; i < capability_count; i++){ 5546983e65eSMilanka Ringwald uint32_t company_id = big_endian_read_24(packet, pos); 5556983e65eSMilanka Ringwald pos += 3; 556adaba9f3SMatthias Ringwald log_info(" 0x%06" PRIx32 ", ", company_id); 5576983e65eSMilanka Ringwald } 5586983e65eSMilanka Ringwald break; 5596983e65eSMilanka Ringwald case AVRCP_CAPABILITY_ID_EVENT: 5606983e65eSMilanka Ringwald // log_info("Supported events %d: ", capability_count); 5616983e65eSMilanka Ringwald for (i = 0; i < capability_count; i++){ 5626983e65eSMilanka Ringwald uint8_t event_id = packet[pos++]; 5636983e65eSMilanka Ringwald log_info(" 0x%02x %s", event_id, avrcp_event2str(event_id)); 5646983e65eSMilanka Ringwald } 5656983e65eSMilanka Ringwald break; 5666983e65eSMilanka Ringwald } 5676983e65eSMilanka Ringwald break; 5686983e65eSMilanka Ringwald } 5696983e65eSMilanka Ringwald case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 5706983e65eSMilanka Ringwald uint32_t song_length = big_endian_read_32(packet, pos); 5716983e65eSMilanka Ringwald pos += 4; 5726983e65eSMilanka Ringwald uint32_t song_position = big_endian_read_32(packet, pos); 5736983e65eSMilanka Ringwald pos += 4; 5746983e65eSMilanka Ringwald uint8_t play_status = packet[pos]; 5756983e65eSMilanka Ringwald // log_info(" GET_PLAY_STATUS length 0x%04X, position 0x%04X, status %s", song_length, song_position, avrcp_play_status2str(play_status)); 5766983e65eSMilanka Ringwald 5776983e65eSMilanka Ringwald uint8_t event[15]; 5786983e65eSMilanka Ringwald int offset = 0; 5796983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 5806983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 5816983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 5826983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 5836983e65eSMilanka Ringwald offset += 2; 5846983e65eSMilanka Ringwald event[offset++] = ctype; 5856983e65eSMilanka Ringwald little_endian_store_32(event, offset, song_length); 5866983e65eSMilanka Ringwald offset += 4; 5876983e65eSMilanka Ringwald little_endian_store_32(event, offset, song_position); 5886983e65eSMilanka Ringwald offset += 4; 5896983e65eSMilanka Ringwald event[offset++] = play_status; 5906983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 5916983e65eSMilanka Ringwald break; 5926983e65eSMilanka Ringwald } 5936983e65eSMilanka Ringwald case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 5946983e65eSMilanka Ringwald avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos++]; 5956983e65eSMilanka Ringwald uint16_t event_mask = (1 << event_id); 5966983e65eSMilanka Ringwald uint16_t reset_event_mask = ~event_mask; 5976983e65eSMilanka Ringwald switch (ctype){ 5986983e65eSMilanka Ringwald case AVRCP_CTYPE_RESPONSE_INTERIM: 5996983e65eSMilanka Ringwald // register as enabled 6006983e65eSMilanka Ringwald connection->notifications_enabled |= event_mask; 6016983e65eSMilanka Ringwald // printf("INTERIM notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 6026983e65eSMilanka Ringwald break; 6036983e65eSMilanka Ringwald case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 6046983e65eSMilanka Ringwald // received change, event is considered deregistered 6056983e65eSMilanka Ringwald // we are re-enabling it automatically, if it is not 6066983e65eSMilanka Ringwald // explicitly disabled 6076983e65eSMilanka Ringwald connection->notifications_enabled &= reset_event_mask; 6086983e65eSMilanka Ringwald if (! (connection->notifications_to_deregister & event_mask)){ 6096983e65eSMilanka Ringwald avrcp_register_notification(connection, event_id); 6106983e65eSMilanka Ringwald // printf("CHANGED_STABLE notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 6116983e65eSMilanka Ringwald } else { 6126983e65eSMilanka Ringwald connection->notifications_to_deregister &= reset_event_mask; 6136983e65eSMilanka Ringwald } 6146983e65eSMilanka Ringwald break; 6156983e65eSMilanka Ringwald default: 6166983e65eSMilanka Ringwald connection->notifications_to_register &= reset_event_mask; 6176983e65eSMilanka Ringwald connection->notifications_enabled &= reset_event_mask; 6186983e65eSMilanka Ringwald connection->notifications_to_deregister &= reset_event_mask; 6196983e65eSMilanka Ringwald break; 6206983e65eSMilanka Ringwald } 6216983e65eSMilanka Ringwald 6226983e65eSMilanka Ringwald switch (event_id){ 6236983e65eSMilanka Ringwald case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 6246983e65eSMilanka Ringwald uint8_t event[7]; 6256983e65eSMilanka Ringwald int offset = 0; 6266983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 6276983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 6286983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 6296983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 6306983e65eSMilanka Ringwald offset += 2; 6316983e65eSMilanka Ringwald event[offset++] = ctype; 6326983e65eSMilanka Ringwald event[offset++] = packet[pos]; 6336983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 6346983e65eSMilanka Ringwald break; 6356983e65eSMilanka Ringwald } 6366983e65eSMilanka Ringwald case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 6376983e65eSMilanka Ringwald uint8_t event[6]; 6386983e65eSMilanka Ringwald int offset = 0; 6396983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 6406983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 6416983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 6426983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 6436983e65eSMilanka Ringwald offset += 2; 6446983e65eSMilanka Ringwald event[offset++] = ctype; 6456983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 6466983e65eSMilanka Ringwald break; 6476983e65eSMilanka Ringwald } 6486983e65eSMilanka Ringwald case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 6496983e65eSMilanka Ringwald uint8_t event[6]; 6506983e65eSMilanka Ringwald int offset = 0; 6516983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 6526983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 6536983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 6546983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 6556983e65eSMilanka Ringwald offset += 2; 6566983e65eSMilanka Ringwald event[offset++] = ctype; 6576983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 6586983e65eSMilanka Ringwald break; 6596983e65eSMilanka Ringwald } 6606983e65eSMilanka Ringwald case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 6616983e65eSMilanka Ringwald uint8_t event[6]; 6626983e65eSMilanka Ringwald int offset = 0; 6636983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 6646983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 6656983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 6666983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 6676983e65eSMilanka Ringwald offset += 2; 6686983e65eSMilanka Ringwald event[offset++] = ctype; 6696983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 6706983e65eSMilanka Ringwald break; 6716983e65eSMilanka Ringwald } 6726983e65eSMilanka Ringwald case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 6736983e65eSMilanka Ringwald uint8_t event[7]; 6746983e65eSMilanka Ringwald int offset = 0; 6756983e65eSMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 6766983e65eSMilanka Ringwald event[offset++] = sizeof(event) - 2; 6776983e65eSMilanka Ringwald event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 6786983e65eSMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 6796983e65eSMilanka Ringwald offset += 2; 6806983e65eSMilanka Ringwald event[offset++] = ctype; 6816983e65eSMilanka Ringwald event[offset++] = packet[pos++] & 0x7F; 6826983e65eSMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 6836983e65eSMilanka Ringwald break; 6846983e65eSMilanka Ringwald } 6854614c049SMilanka Ringwald case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 6864614c049SMilanka Ringwald uint8_t event[7]; 6874614c049SMilanka Ringwald int offset = 0; 6884614c049SMilanka Ringwald event[offset++] = HCI_EVENT_AVRCP_META; 6894614c049SMilanka Ringwald event[offset++] = sizeof(event) - 2; 6904614c049SMilanka Ringwald event[offset++] = AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED; 6914614c049SMilanka Ringwald little_endian_store_16(event, offset, connection->avrcp_cid); 6924614c049SMilanka Ringwald offset += 2; 6934614c049SMilanka Ringwald event[offset++] = ctype; 6944614c049SMilanka Ringwald event[offset++] = packet[pos++] & 0x7F; 6954614c049SMilanka Ringwald (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 6964614c049SMilanka Ringwald break; 6974614c049SMilanka Ringwald } 6984614c049SMilanka Ringwald 6996983e65eSMilanka Ringwald // case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:{ 7006983e65eSMilanka Ringwald // uint8_t num_PlayerApplicationSettingAttributes = packet[pos++]; 7016983e65eSMilanka Ringwald // int i; 7026983e65eSMilanka Ringwald // for (i = 0; i < num_PlayerApplicationSettingAttributes; i++){ 7036983e65eSMilanka Ringwald // uint8_t PlayerApplicationSetting_AttributeID = packet[pos++]; 7046983e65eSMilanka Ringwald // uint8_t PlayerApplicationSettingValueID = packet[pos++]; 7056983e65eSMilanka Ringwald // } 7066983e65eSMilanka Ringwald // break; 7076983e65eSMilanka Ringwald // } 7086983e65eSMilanka Ringwald // case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 7096983e65eSMilanka Ringwald // uint16_t player_id = big_endian_read_16(packet, pos); 7106983e65eSMilanka Ringwald // pos += 2; 7116983e65eSMilanka Ringwald // uint16_t uid_counter = big_endian_read_16(packet, pos); 7126983e65eSMilanka Ringwald // pos += 2; 7136983e65eSMilanka Ringwald // break; 7146983e65eSMilanka Ringwald // case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 7156983e65eSMilanka Ringwald // uint16_t uid_counter = big_endian_read_16(packet, pos); 7166983e65eSMilanka Ringwald // pos += 2; 7176983e65eSMilanka Ringwald // break; 7186983e65eSMilanka Ringwald default: 7196983e65eSMilanka Ringwald log_info("avrcp: not implemented"); 7206983e65eSMilanka Ringwald break; 7216983e65eSMilanka Ringwald } 7226983e65eSMilanka Ringwald if (connection->notifications_to_register != 0){ 7236983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 7246983e65eSMilanka Ringwald } 7256983e65eSMilanka Ringwald break; 7266983e65eSMilanka Ringwald } 7276983e65eSMilanka Ringwald 7286983e65eSMilanka Ringwald case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 7291bf7a74fSMilanka Ringwald avrcp_packet_type_t packet_type = operands[4] & 0x03; 7301bf7a74fSMilanka Ringwald switch (packet_type){ 7311bf7a74fSMilanka Ringwald case AVRCP_START_PACKET: 7321bf7a74fSMilanka Ringwald case AVRCP_SINGLE_PACKET: 7331bf7a74fSMilanka Ringwald avrcp_parser_reset(connection); 7341bf7a74fSMilanka Ringwald connection->list_size = param_length; 7351bf7a74fSMilanka Ringwald connection->num_attributes = packet[pos++]; 7368b2b4034SMilanka Ringwald 7371bf7a74fSMilanka Ringwald // printf("AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES num_attributes %d, total size %d, packet type 0x%02x \n", connection->num_attributes, connection->list_size, operands[4] & 0x03); 7381bf7a74fSMilanka Ringwald avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 7396983e65eSMilanka Ringwald 7401bf7a74fSMilanka Ringwald if (packet_type == AVRCP_START_PACKET){ 7411bf7a74fSMilanka Ringwald avrcp_controller_request_continue_response(connection); 7426983e65eSMilanka Ringwald } 7436983e65eSMilanka Ringwald break; 7441bf7a74fSMilanka Ringwald case AVRCP_CONTINUE_PACKET: 7451bf7a74fSMilanka Ringwald case AVRCP_END_PACKET: 7468b2b4034SMilanka Ringwald connection->num_received_fragments++; 7478b2b4034SMilanka Ringwald if (connection->num_received_fragments < connection->max_num_fragments){ 7481bf7a74fSMilanka Ringwald avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 7498b2b4034SMilanka Ringwald if (packet_type == AVRCP_CONTINUE_PACKET){ 7508b2b4034SMilanka Ringwald avrcp_controller_request_continue_response(connection); 7518b2b4034SMilanka Ringwald } 7528b2b4034SMilanka Ringwald } else { 7538b2b4034SMilanka Ringwald avrcp_controller_request_abort_continuation(connection); 7548b2b4034SMilanka Ringwald avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 7558b2b4034SMilanka Ringwald avrcp_parser_reset(connection); 7568b2b4034SMilanka Ringwald } 7571bf7a74fSMilanka Ringwald break; 7581bf7a74fSMilanka Ringwald } 7596983e65eSMilanka Ringwald } 7606983e65eSMilanka Ringwald default: 7616983e65eSMilanka Ringwald break; 7626983e65eSMilanka Ringwald } 7636983e65eSMilanka Ringwald break; 7646983e65eSMilanka Ringwald case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 7656983e65eSMilanka Ringwald // 0x80 | connection->cmd_operands[0] 7666983e65eSMilanka Ringwald uint8_t operation_id = packet[pos++]; 7676983e65eSMilanka Ringwald switch (connection->state){ 7686983e65eSMilanka Ringwald case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 7696983e65eSMilanka Ringwald if (connection->continuous_fast_forward_cmd){ 7706983e65eSMilanka Ringwald connection->state = AVCTP_W4_STOP; 7716983e65eSMilanka Ringwald } else { 7726983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 7736983e65eSMilanka Ringwald } 7746983e65eSMilanka Ringwald break; 7756983e65eSMilanka Ringwald case AVCTP_W2_RECEIVE_RESPONSE: 7766983e65eSMilanka Ringwald connection->state = AVCTP_CONNECTION_OPENED; 7776983e65eSMilanka Ringwald break; 7786983e65eSMilanka Ringwald default: 7796983e65eSMilanka Ringwald // check for notifications? move state transition down 7806983e65eSMilanka Ringwald // log_info("AVRCP_CMD_OPCODE_PASS_THROUGH state %d\n", connection->state); 7816983e65eSMilanka Ringwald break; 7826983e65eSMilanka Ringwald } 7836983e65eSMilanka Ringwald if (connection->state == AVCTP_W4_STOP){ 7846983e65eSMilanka Ringwald avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 7856983e65eSMilanka Ringwald } 7866983e65eSMilanka Ringwald if (connection->state == AVCTP_CONNECTION_OPENED) { 7876983e65eSMilanka Ringwald // RELEASE response 7886983e65eSMilanka Ringwald operation_id = operation_id & 0x7F; 7896983e65eSMilanka Ringwald avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 7906983e65eSMilanka Ringwald } 7916983e65eSMilanka Ringwald if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 7926983e65eSMilanka Ringwald // PRESS response 7936983e65eSMilanka Ringwald request_pass_through_release_control_cmd(connection); 7946983e65eSMilanka Ringwald } 7956983e65eSMilanka Ringwald break; 7966983e65eSMilanka Ringwald } 7976983e65eSMilanka Ringwald default: 7986983e65eSMilanka Ringwald break; 7996983e65eSMilanka Ringwald } 8006983e65eSMilanka Ringwald } 8016983e65eSMilanka Ringwald 8026983e65eSMilanka Ringwald static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 8036983e65eSMilanka Ringwald int i; 8046983e65eSMilanka Ringwald switch (connection->state){ 8056983e65eSMilanka Ringwald case AVCTP_W2_SEND_PRESS_COMMAND: 8066983e65eSMilanka Ringwald connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 8076983e65eSMilanka Ringwald avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 8086983e65eSMilanka Ringwald break; 8096983e65eSMilanka Ringwald case AVCTP_W2_SEND_COMMAND: 8106983e65eSMilanka Ringwald case AVCTP_W2_SEND_RELEASE_COMMAND: 8116983e65eSMilanka Ringwald connection->state = AVCTP_W2_RECEIVE_RESPONSE; 8126983e65eSMilanka Ringwald avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 8136983e65eSMilanka Ringwald break; 8146983e65eSMilanka Ringwald case AVCTP_CONNECTION_OPENED: 8156983e65eSMilanka Ringwald if (connection->notifications_to_register != 0){ 8166983e65eSMilanka Ringwald for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){ 8176983e65eSMilanka Ringwald if (connection->notifications_to_register & (1<<i)){ 8186983e65eSMilanka Ringwald connection->notifications_to_register &= ~ (1 << i); 8196983e65eSMilanka Ringwald avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i); 8206983e65eSMilanka Ringwald connection->state = AVCTP_W2_RECEIVE_RESPONSE; 8216983e65eSMilanka Ringwald avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 8226983e65eSMilanka Ringwald return; 8236983e65eSMilanka Ringwald } 8246983e65eSMilanka Ringwald } 8256983e65eSMilanka Ringwald } 8266983e65eSMilanka Ringwald return; 8276983e65eSMilanka Ringwald default: 8286983e65eSMilanka Ringwald return; 8296983e65eSMilanka Ringwald } 8306983e65eSMilanka Ringwald } 8316983e65eSMilanka Ringwald 8326983e65eSMilanka Ringwald static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 8336983e65eSMilanka Ringwald avrcp_connection_t * connection; 8346983e65eSMilanka Ringwald 8356983e65eSMilanka Ringwald switch (packet_type) { 8366983e65eSMilanka Ringwald case L2CAP_DATA_PACKET: 8376983e65eSMilanka Ringwald connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 8386983e65eSMilanka Ringwald if (!connection) break; 8396983e65eSMilanka Ringwald avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 8406983e65eSMilanka Ringwald break; 8416983e65eSMilanka Ringwald case HCI_EVENT_PACKET: 8426983e65eSMilanka Ringwald switch (hci_event_packet_get_type(packet)){ 8436983e65eSMilanka Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 8446983e65eSMilanka Ringwald connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 8456983e65eSMilanka Ringwald if (!connection) break; 8466983e65eSMilanka Ringwald avrcp_controller_handle_can_send_now(connection); 8476983e65eSMilanka Ringwald break; 8486983e65eSMilanka Ringwald default: 8496983e65eSMilanka Ringwald avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 8506983e65eSMilanka Ringwald break; 8516983e65eSMilanka Ringwald } 8526983e65eSMilanka Ringwald default: 8536983e65eSMilanka Ringwald break; 8546983e65eSMilanka Ringwald } 8556983e65eSMilanka Ringwald } 8566983e65eSMilanka Ringwald 8576983e65eSMilanka Ringwald void avrcp_controller_init(void){ 8586983e65eSMilanka Ringwald avrcp_controller_context.role = AVRCP_CONTROLLER; 8596983e65eSMilanka Ringwald avrcp_controller_context.connections = NULL; 8606983e65eSMilanka Ringwald avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 8616983e65eSMilanka Ringwald l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); 8626983e65eSMilanka Ringwald } 8636983e65eSMilanka Ringwald 8646983e65eSMilanka Ringwald void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 8656983e65eSMilanka Ringwald if (callback == NULL){ 8666983e65eSMilanka Ringwald log_error("avrcp_register_packet_handler called with NULL callback"); 8676983e65eSMilanka Ringwald return; 8686983e65eSMilanka Ringwald } 8696983e65eSMilanka Ringwald avrcp_controller_context.avrcp_callback = callback; 8706983e65eSMilanka Ringwald } 8716983e65eSMilanka Ringwald 8726983e65eSMilanka Ringwald uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 8736983e65eSMilanka Ringwald return avrcp_connect(bd_addr, &avrcp_controller_context, avrcp_cid); 8746983e65eSMilanka Ringwald } 8756983e65eSMilanka Ringwald 8766983e65eSMilanka Ringwald uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 8776983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 8786983e65eSMilanka Ringwald if (!connection){ 8796983e65eSMilanka Ringwald log_error("avrcp_unit_info: could not find a connection."); 8806983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8816983e65eSMilanka Ringwald } 8826983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 8836983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 8846983e65eSMilanka Ringwald 8856983e65eSMilanka Ringwald connection->transaction_label++; 8866983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 8876983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_STATUS; 8886983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 8896983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 8906983e65eSMilanka Ringwald memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 8916983e65eSMilanka Ringwald connection->cmd_operands_length = 5; 8926983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 8936983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 8946983e65eSMilanka Ringwald } 8956983e65eSMilanka Ringwald 896ad75da14SMilanka Ringwald uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 897ad75da14SMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 898ad75da14SMilanka Ringwald if (!connection){ 899ad75da14SMilanka Ringwald log_error("avrcp_unit_info: could not find a connection."); 900ad75da14SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 901ad75da14SMilanka Ringwald } 902ad75da14SMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 903ad75da14SMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 904ad75da14SMilanka Ringwald 905ad75da14SMilanka Ringwald connection->transaction_label++; 906ad75da14SMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 907ad75da14SMilanka Ringwald connection->command_type = AVRCP_CTYPE_STATUS; 908ad75da14SMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 909ad75da14SMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 910ad75da14SMilanka Ringwald memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 911ad75da14SMilanka Ringwald connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 912ad75da14SMilanka Ringwald connection->cmd_operands_length = 5; 913ad75da14SMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 914ad75da14SMilanka Ringwald return ERROR_CODE_SUCCESS; 915ad75da14SMilanka Ringwald } 916ad75da14SMilanka Ringwald 9176983e65eSMilanka Ringwald static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 9186983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 9196983e65eSMilanka Ringwald if (!connection){ 9206983e65eSMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 9216983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9226983e65eSMilanka Ringwald } 9236983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 9246983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 9256983e65eSMilanka Ringwald 9266983e65eSMilanka Ringwald connection->transaction_label++; 9276983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 9286983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_STATUS; 9296983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 9306983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 9316983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 9326983e65eSMilanka Ringwald connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 9336983e65eSMilanka Ringwald connection->cmd_operands[4] = 0; 9346983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 9356983e65eSMilanka Ringwald connection->cmd_operands[7] = capability_id; // capability ID 9366983e65eSMilanka Ringwald connection->cmd_operands_length = 8; 9376983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 9386983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 9396983e65eSMilanka Ringwald } 9406983e65eSMilanka Ringwald 9416983e65eSMilanka Ringwald uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 9426983e65eSMilanka Ringwald return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 9436983e65eSMilanka Ringwald } 9446983e65eSMilanka Ringwald 9456983e65eSMilanka Ringwald uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 9466983e65eSMilanka Ringwald return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 9476983e65eSMilanka Ringwald } 9486983e65eSMilanka Ringwald 9496983e65eSMilanka Ringwald 9506983e65eSMilanka Ringwald uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 9516983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 9526983e65eSMilanka Ringwald } 9536983e65eSMilanka Ringwald 9546983e65eSMilanka Ringwald uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 9556983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 9566983e65eSMilanka Ringwald } 9576983e65eSMilanka Ringwald 9586983e65eSMilanka Ringwald uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 9596983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 9606983e65eSMilanka Ringwald } 9616983e65eSMilanka Ringwald 9626983e65eSMilanka Ringwald uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 9636983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 9646983e65eSMilanka Ringwald } 9656983e65eSMilanka Ringwald 9666983e65eSMilanka Ringwald uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 9676983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 9686983e65eSMilanka Ringwald } 9696983e65eSMilanka Ringwald 9706983e65eSMilanka Ringwald uint8_t avrcp_controller_start_rewind(uint16_t avrcp_cid){ 9716983e65eSMilanka Ringwald return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 9726983e65eSMilanka Ringwald } 9736983e65eSMilanka Ringwald 9746983e65eSMilanka Ringwald uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 9756983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 9766983e65eSMilanka Ringwald } 9776983e65eSMilanka Ringwald 9786983e65eSMilanka Ringwald uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 9796983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 9806983e65eSMilanka Ringwald } 9816983e65eSMilanka Ringwald 9826983e65eSMilanka Ringwald uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 9836983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 9846983e65eSMilanka Ringwald } 9856983e65eSMilanka Ringwald 9866983e65eSMilanka Ringwald uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 9876983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 9886983e65eSMilanka Ringwald } 9896983e65eSMilanka Ringwald 9906983e65eSMilanka Ringwald uint8_t avrcp_controller_stop_rewind(uint16_t avrcp_cid){ 9916983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 9926983e65eSMilanka Ringwald if (!connection){ 9936983e65eSMilanka Ringwald log_error("avrcp_stop_rewind: could not find a connection."); 9946983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9956983e65eSMilanka Ringwald } 9966983e65eSMilanka Ringwald if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 9976983e65eSMilanka Ringwald return request_pass_through_release_control_cmd(connection); 9986983e65eSMilanka Ringwald } 9996983e65eSMilanka Ringwald 10006983e65eSMilanka Ringwald uint8_t avrcp_controller_start_fast_forward(uint16_t avrcp_cid){ 10016983e65eSMilanka Ringwald return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 10026983e65eSMilanka Ringwald } 10036983e65eSMilanka Ringwald 10046983e65eSMilanka Ringwald uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 10056983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 10066983e65eSMilanka Ringwald } 10076983e65eSMilanka Ringwald 10086983e65eSMilanka Ringwald uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 10096983e65eSMilanka Ringwald return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 10106983e65eSMilanka Ringwald } 10116983e65eSMilanka Ringwald 10126983e65eSMilanka Ringwald 10136983e65eSMilanka Ringwald uint8_t avrcp_controller_stop_fast_forward(uint16_t avrcp_cid){ 10146983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 10156983e65eSMilanka Ringwald if (!connection){ 10166983e65eSMilanka Ringwald log_error("avrcp_stop_fast_forward: could not find a connection."); 10176983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 10186983e65eSMilanka Ringwald } 10196983e65eSMilanka Ringwald if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 10206983e65eSMilanka Ringwald return request_pass_through_release_control_cmd(connection); 10216983e65eSMilanka Ringwald } 10226983e65eSMilanka Ringwald 10236983e65eSMilanka Ringwald uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 10246983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 10256983e65eSMilanka Ringwald if (!connection){ 10266983e65eSMilanka Ringwald log_error("avrcp_get_play_status: could not find a connection."); 10276983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 10286983e65eSMilanka Ringwald } 10296983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 10306983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 10316983e65eSMilanka Ringwald connection->transaction_label++; 10326983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 10336983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_STATUS; 10346983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 10356983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 10366983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 10376983e65eSMilanka Ringwald connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 10386983e65eSMilanka Ringwald connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 10396983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 10406983e65eSMilanka Ringwald connection->cmd_operands_length = 7; 10416983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 10426983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 10436983e65eSMilanka Ringwald } 10446983e65eSMilanka Ringwald 10456983e65eSMilanka Ringwald uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 10466983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 10476983e65eSMilanka Ringwald if (!connection){ 10486983e65eSMilanka Ringwald log_error("avrcp_get_play_status: could not find a connection."); 10496983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 10506983e65eSMilanka Ringwald } 10516983e65eSMilanka Ringwald avrcp_register_notification(connection, event_id); 10526983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 10536983e65eSMilanka Ringwald } 10546983e65eSMilanka Ringwald 10556983e65eSMilanka Ringwald uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 10566983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 10576983e65eSMilanka Ringwald if (!connection){ 10586983e65eSMilanka Ringwald log_error("avrcp_get_play_status: could not find a connection."); 10596983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 10606983e65eSMilanka Ringwald } 10616983e65eSMilanka Ringwald connection->notifications_to_deregister |= (1 << event_id); 10626983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 10636983e65eSMilanka Ringwald } 10646983e65eSMilanka Ringwald 1065864d08b0SMilanka Ringwald uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1066864d08b0SMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1067864d08b0SMilanka Ringwald if (!connection){ 1068864d08b0SMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 1069864d08b0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1070864d08b0SMilanka Ringwald } 1071864d08b0SMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1072864d08b0SMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 1073864d08b0SMilanka Ringwald 1074864d08b0SMilanka Ringwald connection->transaction_label++; 1075864d08b0SMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1076864d08b0SMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 1077864d08b0SMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1078864d08b0SMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 1079864d08b0SMilanka Ringwald int pos = 0; 1080864d08b0SMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1081864d08b0SMilanka Ringwald pos += 3; 1082864d08b0SMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1083864d08b0SMilanka Ringwald connection->cmd_operands[pos++] = 0; 1084864d08b0SMilanka Ringwald 1085864d08b0SMilanka Ringwald // Parameter Length 1086864d08b0SMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 2); 1087864d08b0SMilanka Ringwald pos += 2; 1088864d08b0SMilanka Ringwald 1089864d08b0SMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1090864d08b0SMilanka Ringwald pos += 2; 1091864d08b0SMilanka Ringwald 1092864d08b0SMilanka Ringwald connection->cmd_operands_length = pos; 1093864d08b0SMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1094864d08b0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1095864d08b0SMilanka Ringwald } 10961bf7a74fSMilanka Ringwald 10976983e65eSMilanka Ringwald uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 10986983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 10996983e65eSMilanka Ringwald if (!connection){ 11006983e65eSMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 11016983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 11026983e65eSMilanka Ringwald } 11036983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 11046983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 11056983e65eSMilanka Ringwald 11066983e65eSMilanka Ringwald connection->transaction_label++; 11076983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 11086983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_STATUS; 11096983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 11106983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 11116983e65eSMilanka Ringwald int pos = 0; 11126983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 11136983e65eSMilanka Ringwald pos += 3; 11146983e65eSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 11156983e65eSMilanka Ringwald connection->cmd_operands[pos++] = 0; 11166983e65eSMilanka Ringwald 11176983e65eSMilanka Ringwald // Parameter Length 11186983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 9); 11196983e65eSMilanka Ringwald pos += 2; 11206983e65eSMilanka Ringwald 11216983e65eSMilanka Ringwald // write 8 bytes value 11226983e65eSMilanka Ringwald memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 11236983e65eSMilanka Ringwald pos += 8; 11246983e65eSMilanka Ringwald 11256983e65eSMilanka Ringwald connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 11266983e65eSMilanka Ringwald // every attribute is 4 bytes long 11276983e65eSMilanka Ringwald 11286983e65eSMilanka Ringwald connection->cmd_operands_length = pos; 11296983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 11306983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 11316983e65eSMilanka Ringwald } 11326983e65eSMilanka Ringwald 11336983e65eSMilanka Ringwald uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 11346983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 11356983e65eSMilanka Ringwald if (!connection){ 11366983e65eSMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 11376983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 11386983e65eSMilanka Ringwald } 11396983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 11406983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 11416983e65eSMilanka Ringwald 11426983e65eSMilanka Ringwald connection->transaction_label++; 11436983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 11446983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 11456983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 11466983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 11476983e65eSMilanka Ringwald int pos = 0; 11486983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 11496983e65eSMilanka Ringwald pos += 3; 11506983e65eSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 11516983e65eSMilanka Ringwald connection->cmd_operands[pos++] = 0; 11526983e65eSMilanka Ringwald 11536983e65eSMilanka Ringwald // Parameter Length 11546983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 1); 11556983e65eSMilanka Ringwald pos += 2; 11566983e65eSMilanka Ringwald connection->cmd_operands[pos++] = volume; 11576983e65eSMilanka Ringwald 11586983e65eSMilanka Ringwald connection->cmd_operands_length = pos; 11596983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 11606983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 11616983e65eSMilanka Ringwald } 11626983e65eSMilanka Ringwald 11636983e65eSMilanka Ringwald uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 11646983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 11656983e65eSMilanka Ringwald if (!connection){ 11666983e65eSMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 11676983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 11686983e65eSMilanka Ringwald } 11696983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 11706983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 11716983e65eSMilanka Ringwald 11726983e65eSMilanka Ringwald connection->transaction_label++; 11736983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 11746983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_STATUS; 11756983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 11766983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 11776983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 11786983e65eSMilanka Ringwald connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID 11796983e65eSMilanka Ringwald connection->cmd_operands[4] = 0; 11806983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 11816983e65eSMilanka Ringwald connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 11826983e65eSMilanka Ringwald // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 11836983e65eSMilanka Ringwald connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 11846983e65eSMilanka Ringwald connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 11856983e65eSMilanka Ringwald connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 11866983e65eSMilanka Ringwald connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 11876983e65eSMilanka Ringwald connection->cmd_operands_length = 12; 11886983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 11896983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 11906983e65eSMilanka Ringwald } 11916983e65eSMilanka Ringwald 11926983e65eSMilanka Ringwald static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 11936983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 11946983e65eSMilanka Ringwald if (!connection){ 11956983e65eSMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 11966983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 11976983e65eSMilanka Ringwald } 11986983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 11996983e65eSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 12006983e65eSMilanka Ringwald 12016983e65eSMilanka Ringwald connection->transaction_label++; 12026983e65eSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 12036983e65eSMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 12046983e65eSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 12056983e65eSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 12066983e65eSMilanka Ringwald int pos = 0; 12076983e65eSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 12086983e65eSMilanka Ringwald pos += 3; 12096983e65eSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID 12106983e65eSMilanka Ringwald connection->cmd_operands[pos++] = 0; 12116983e65eSMilanka Ringwald // Parameter Length 12126983e65eSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 3); 12136983e65eSMilanka Ringwald pos += 2; 12146983e65eSMilanka Ringwald connection->cmd_operands[pos++] = 2; 12156983e65eSMilanka Ringwald connection->cmd_operands_length = pos; 12166983e65eSMilanka Ringwald connection->cmd_operands[pos++] = attr_id; 12176983e65eSMilanka Ringwald connection->cmd_operands[pos++] = attr_value; 12186983e65eSMilanka Ringwald connection->cmd_operands_length = pos; 12196983e65eSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 12206983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 12216983e65eSMilanka Ringwald } 12226983e65eSMilanka Ringwald 12236983e65eSMilanka Ringwald uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 12246983e65eSMilanka Ringwald if (mode < AVRCP_SHUFFLE_MODE_OFF || mode > AVRCP_SHUFFLE_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 12256983e65eSMilanka Ringwald return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 12266983e65eSMilanka Ringwald } 12276983e65eSMilanka Ringwald 12286983e65eSMilanka Ringwald uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 12296983e65eSMilanka Ringwald if (mode < AVRCP_REPEAT_MODE_OFF || mode > AVRCP_REPEAT_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 12306983e65eSMilanka Ringwald return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 12316983e65eSMilanka Ringwald } 12326983e65eSMilanka Ringwald 12336983e65eSMilanka Ringwald uint8_t avrcp_controller_disconnect(uint16_t avrcp_cid){ 12346983e65eSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 12356983e65eSMilanka Ringwald if (!connection){ 12366983e65eSMilanka Ringwald log_error("avrcp_get_capabilities: could not find a connection."); 12376983e65eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 12386983e65eSMilanka Ringwald } 12396983e65eSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1240c0a054f6SMilanka Ringwald if (connection->browsing_connection){ 1241c0a054f6SMilanka Ringwald if (connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1242c0a054f6SMilanka Ringwald l2cap_disconnect(connection->browsing_connection->l2cap_browsing_cid, 0); 1243c0a054f6SMilanka Ringwald } 12446983e65eSMilanka Ringwald l2cap_disconnect(connection->l2cap_signaling_cid, 0); 12456983e65eSMilanka Ringwald return ERROR_CODE_SUCCESS; 12466983e65eSMilanka Ringwald } 1247be65baf4SMilanka Ringwald 12484614c049SMilanka Ringwald uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1249be65baf4SMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1250be65baf4SMilanka Ringwald if (!connection){ 12514931e24aSMilanka Ringwald log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1252be65baf4SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1253be65baf4SMilanka Ringwald } 12544931e24aSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED){ 12554931e24aSMilanka Ringwald log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 12564931e24aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 12574931e24aSMilanka Ringwald } 1258be65baf4SMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 1259be65baf4SMilanka Ringwald 1260be65baf4SMilanka Ringwald connection->transaction_label++; 1261be65baf4SMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 1262be65baf4SMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1263be65baf4SMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 1264be65baf4SMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1265be65baf4SMilanka Ringwald int pos = 0; 1266be65baf4SMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1267be65baf4SMilanka Ringwald pos += 3; 1268be65baf4SMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1269be65baf4SMilanka Ringwald // reserved 1270be65baf4SMilanka Ringwald connection->cmd_operands[pos++] = 0; 1271be65baf4SMilanka Ringwald // Parameter Length 1272be65baf4SMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 11); 1273be65baf4SMilanka Ringwald pos += 2; 1274be65baf4SMilanka Ringwald connection->cmd_operands[pos++] = scope; 1275be65baf4SMilanka Ringwald memset(&connection->cmd_operands[pos], 0, 8); 1276be65baf4SMilanka Ringwald if (uid){ 1277be65baf4SMilanka Ringwald memcpy(&connection->cmd_operands[pos], uid, 8); 1278be65baf4SMilanka Ringwald } 1279be65baf4SMilanka Ringwald pos += 8; 1280be65baf4SMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1281be65baf4SMilanka Ringwald pos += 2; 1282be65baf4SMilanka Ringwald connection->cmd_operands_length = pos; 1283be65baf4SMilanka Ringwald 1284be65baf4SMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1285be65baf4SMilanka Ringwald return ERROR_CODE_SUCCESS; 1286be65baf4SMilanka Ringwald } 12878b2b4034SMilanka Ringwald 12884931e24aSMilanka Ringwald uint8_t avrcp_controller_add_item_from_scope_to_now_playing_list(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 12894931e24aSMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 12904931e24aSMilanka Ringwald if (!connection){ 12914931e24aSMilanka Ringwald log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 12924931e24aSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 12934931e24aSMilanka Ringwald } 12944931e24aSMilanka Ringwald if (connection->state != AVCTP_CONNECTION_OPENED){ 12954931e24aSMilanka Ringwald log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 12964931e24aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 12974931e24aSMilanka Ringwald } 12984931e24aSMilanka Ringwald connection->state = AVCTP_W2_SEND_COMMAND; 12994931e24aSMilanka Ringwald 13004931e24aSMilanka Ringwald connection->transaction_label++; 13014931e24aSMilanka Ringwald connection->command_type = AVRCP_CTYPE_CONTROL; 13024931e24aSMilanka Ringwald connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 13034931e24aSMilanka Ringwald connection->subunit_id = AVRCP_SUBUNIT_ID; 13044931e24aSMilanka Ringwald connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 13054931e24aSMilanka Ringwald int pos = 0; 13064931e24aSMilanka Ringwald big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 13074931e24aSMilanka Ringwald pos += 3; 13084931e24aSMilanka Ringwald connection->cmd_operands[pos++] = AVRCP_PDU_ADD_TO_NOW_PLAYING; // PDU ID 13094931e24aSMilanka Ringwald // reserved 13104931e24aSMilanka Ringwald connection->cmd_operands[pos++] = 0; 13114931e24aSMilanka Ringwald // Parameter Length 13124931e24aSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, 11); 13134931e24aSMilanka Ringwald pos += 2; 13144931e24aSMilanka Ringwald connection->cmd_operands[pos++] = scope; 13154931e24aSMilanka Ringwald memset(&connection->cmd_operands[pos], 0, 8); 13164931e24aSMilanka Ringwald if (uid){ 13174931e24aSMilanka Ringwald memcpy(&connection->cmd_operands[pos], uid, 8); 13184931e24aSMilanka Ringwald } 13194931e24aSMilanka Ringwald pos += 8; 13204931e24aSMilanka Ringwald big_endian_store_16(connection->cmd_operands, pos, uid_counter); 13214931e24aSMilanka Ringwald pos += 2; 13224931e24aSMilanka Ringwald connection->cmd_operands_length = pos; 13234931e24aSMilanka Ringwald 13244931e24aSMilanka Ringwald avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 13254931e24aSMilanka Ringwald return ERROR_CODE_SUCCESS; 13264931e24aSMilanka Ringwald } 13274931e24aSMilanka Ringwald 13288b2b4034SMilanka Ringwald uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 13298b2b4034SMilanka Ringwald avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 13308b2b4034SMilanka Ringwald if (!connection){ 13318b2b4034SMilanka Ringwald log_error("avrcp_controller_play_item: could not find a connection."); 13328b2b4034SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 13338b2b4034SMilanka Ringwald } 13348b2b4034SMilanka Ringwald connection->max_num_fragments = max_num_fragments; 13358b2b4034SMilanka Ringwald return ERROR_CODE_SUCCESS; 13368b2b4034SMilanka Ringwald } 1337