1 /* 2 * Copyright (C) 2016 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 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "avrcp_controller.c" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <inttypes.h> 45 46 #include "btstack.h" 47 #include "classic/avrcp.h" 48 #include "classic/avrcp_controller.h" 49 50 // made public in avrcp_controller.h 51 avrcp_context_t avrcp_controller_context; 52 53 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){ 54 avrcp_create_sdp_record(1, service, service_record_handle, browsing, supported_features, service_name, service_provider_name); 55 } 56 57 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){ 58 if (!callback) return; 59 uint8_t event[8]; 60 int pos = 0; 61 event[pos++] = HCI_EVENT_AVRCP_META; 62 event[pos++] = sizeof(event) - 2; 63 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 64 little_endian_store_16(event, pos, avrcp_cid); 65 pos += 2; 66 event[pos++] = ctype; 67 event[pos++] = repeat_mode; 68 event[pos++] = shuffle_mode; 69 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 70 } 71 72 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){ 73 if (!callback) return; 74 uint8_t event[7]; 75 int pos = 0; 76 event[pos++] = HCI_EVENT_AVRCP_META; 77 event[pos++] = sizeof(event) - 2; 78 event[pos++] = subevent; 79 little_endian_store_16(event, pos, avrcp_cid); 80 pos += 2; 81 event[pos++] = ctype; 82 event[pos++] = operation_id; 83 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 84 } 85 86 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 87 UNUSED(timer); 88 avrcp_connection_t * connection = btstack_run_loop_get_timer_context(timer); 89 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 90 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 91 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 92 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 93 } 94 95 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 96 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 97 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 98 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 99 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 100 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 101 } 102 103 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 104 connection->continuous_fast_forward_cmd = 0; 105 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 106 } 107 108 static uint8_t request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 109 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 110 if (connection->continuous_fast_forward_cmd){ 111 avrcp_press_and_hold_timer_stop(connection); 112 } 113 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 114 connection->transaction_label++; 115 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 116 return ERROR_CODE_SUCCESS; 117 } 118 119 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){ 120 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, context); 121 if (!connection){ 122 log_error("avrcp: could not find a connection."); 123 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 124 } 125 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 126 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 127 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 128 connection->command_type = AVRCP_CTYPE_CONTROL; 129 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 130 connection->subunit_id = AVRCP_SUBUNIT_ID; 131 connection->cmd_operands_length = 0; 132 133 connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd; 134 connection->cmd_operands_length = 2; 135 connection->cmd_operands[0] = opid; 136 if (playback_speed > 0){ 137 connection->cmd_operands[2] = playback_speed; 138 connection->cmd_operands_length++; 139 } 140 connection->cmd_operands[1] = connection->cmd_operands_length - 2; 141 142 if (connection->continuous_fast_forward_cmd){ 143 avrcp_press_and_hold_timer_start(connection); 144 } 145 146 connection->transaction_label++; 147 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 148 return ERROR_CODE_SUCCESS; 149 } 150 151 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 152 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0, &avrcp_controller_context); 153 } 154 155 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 156 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1, &avrcp_controller_context); 157 } 158 159 static int avrcp_send_cmd(uint16_t cid, avrcp_connection_t * connection){ 160 uint8_t command[30]; 161 int pos = 0; 162 // transport header 163 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 164 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 165 // Profile IDentifier (PID) 166 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 167 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 168 169 // command_type 170 command[pos++] = connection->command_type; 171 // subunit_type | subunit ID 172 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 173 // opcode 174 command[pos++] = (uint8_t)connection->command_opcode; 175 // operands 176 memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length); 177 pos += connection->cmd_operands_length; 178 179 return l2cap_send(cid, command, pos); 180 } 181 182 static int avrcp_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 183 if (connection->notifications_to_deregister & (1 << event_id)) return 0; 184 if (connection->notifications_enabled & (1 << event_id)) return 0; 185 if (connection->notifications_to_register & (1 << event_id)) return 0; 186 connection->notifications_to_register |= (1 << event_id); 187 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 188 return 1; 189 } 190 191 static void avrcp_prepare_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 192 connection->transaction_label++; 193 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 194 connection->command_type = AVRCP_CTYPE_NOTIFY; 195 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 196 connection->subunit_id = AVRCP_SUBUNIT_ID; 197 int pos = 0; 198 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 199 pos += 3; 200 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 201 connection->cmd_operands[pos++] = 0; // reserved(upper 6) | packet_type -> 0 202 big_endian_store_16(connection->cmd_operands, pos, 5); // parameter length 203 pos += 2; 204 connection->cmd_operands[pos++] = event_id; 205 big_endian_store_32(connection->cmd_operands, pos, 0); 206 pos += 4; 207 connection->cmd_operands_length = pos; 208 // AVRCP_SPEC_V14.pdf 166 209 // answer page 61 210 } 211 212 213 static void avrcp_parser_reset(avrcp_connection_t * connection){ 214 connection->list_offset = 0; 215 connection->num_attributes = 0; 216 connection->num_parsed_attributes = 0; 217 connection->parser_attribute_header_pos = 0; 218 connection->num_received_fragments = 0; 219 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 220 } 221 222 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){ 223 uint8_t event[7]; 224 int pos = 0; 225 event[pos++] = HCI_EVENT_AVRCP_META; 226 event[pos++] = sizeof(event) - 2; 227 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE; 228 little_endian_store_16(event, pos, avrcp_cid); 229 pos += 2; 230 event[pos++] = ctype; 231 event[pos++] = status; 232 // printf_hexdump(event, pos); 233 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 234 } 235 236 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){ 237 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 238 int pos = 0; 239 event[pos++] = HCI_EVENT_AVRCP_META; 240 // reserve one byte for subevent type and data len 241 int data_len_pos = pos; 242 pos++; 243 int subevent_type_pos = pos; 244 pos++; 245 little_endian_store_16(event, pos, avrcp_cid); 246 pos += 2; 247 event[pos++] = ctype; 248 249 switch (attr_id){ 250 case AVRCP_MEDIA_ATTR_TITLE: 251 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO; 252 event[pos++] = value_len; 253 memcpy(event+pos, value, value_len); 254 break; 255 case AVRCP_MEDIA_ATTR_ARTIST: 256 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO; 257 event[pos++] = value_len; 258 memcpy(event+pos, value, value_len); 259 break; 260 case AVRCP_MEDIA_ATTR_ALBUM: 261 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO; 262 event[pos++] = value_len; 263 memcpy(event+pos, value, value_len); 264 break; 265 case AVRCP_MEDIA_ATTR_GENRE: 266 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO; 267 event[pos++] = value_len; 268 memcpy(event+pos, value, value_len); 269 break; 270 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 271 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO; 272 if (value){ 273 little_endian_store_32(event, pos, btstack_atoi((char *)value)); 274 } else { 275 little_endian_store_32(event, pos, 0); 276 } 277 pos += 4; 278 break; 279 case AVRCP_MEDIA_ATTR_TRACK: 280 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO; 281 if (value){ 282 event[pos++] = btstack_atoi((char *)value); 283 } else { 284 event[pos++] = 0; 285 } 286 break; 287 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 288 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO; 289 if (value){ 290 event[pos++] = btstack_atoi((char *)value); 291 } else { 292 event[pos++] = 0; 293 } 294 break; 295 default: 296 break; 297 } 298 event[data_len_pos] = pos - 2; 299 // printf("send attr len %d, value %s\n", value_len, value); 300 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 301 } 302 303 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 304 switch(connection->parser_state){ 305 case AVRCP_PARSER_GET_ATTRIBUTE_HEADER:{ 306 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) { 307 connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte; 308 connection->list_offset++; 309 break; 310 } 311 uint16_t attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 312 connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE); 313 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE; 314 break; 315 } 316 case AVRCP_PARSER_GET_ATTRIBUTE_VALUE:{ 317 if (connection->attribute_value_offset < connection->attribute_value_len){ 318 connection->attribute_value[connection->attribute_value_offset++] = byte; 319 connection->list_offset++; 320 break; 321 } 322 323 uint32_t attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 324 if (attribute_id > AVRCP_MEDIA_ATTR_NONE && attribute_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS){ 325 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); 326 } 327 328 if (connection->attribute_value_offset < big_endian_read_16(connection->parser_attribute_header, 6)){ 329 // printf("parse until end of valuE, and ignore it\n"); 330 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE; 331 break; 332 } 333 334 if (connection->list_offset == connection->list_size){ 335 avrcp_parser_reset(connection); 336 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 337 break; 338 } 339 340 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 341 connection->parser_attribute_header_pos = 0; 342 break; 343 } 344 case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE: 345 if (connection->attribute_value_offset < big_endian_read_16(connection->parser_attribute_header, 6)){ 346 connection->list_offset++; 347 connection->attribute_value_offset++; 348 break; 349 } 350 // printf("read %d, total %d\n", connection->attribute_value_offset, big_endian_read_16(connection->parser_attribute_header, 6)); 351 352 if (connection->list_offset == connection->list_size){ 353 avrcp_parser_reset(connection); 354 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 355 break; 356 } 357 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 358 connection->parser_attribute_header_pos = 0; 359 break; 360 default: 361 break; 362 } 363 } 364 365 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){ 366 int i; 367 for (i=0;i<num_bytes_to_read;i++){ 368 avrcp_parser_process_byte(packet[i], connection, ctype); 369 } 370 } 371 372 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 373 connection->state = AVCTP_W2_SEND_COMMAND; 374 connection->transaction_label++; 375 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 376 connection->command_type = AVRCP_CTYPE_CONTROL; 377 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 378 connection->subunit_id = AVRCP_SUBUNIT_ID; 379 int pos = 0; 380 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 381 pos += 3; 382 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID 383 connection->cmd_operands[pos++] = 0; 384 // Parameter Length 385 connection->cmd_operands_length = 8; 386 big_endian_store_16(connection->cmd_operands, pos, 1); 387 pos += 2; 388 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 389 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 390 return ERROR_CODE_SUCCESS; 391 } 392 393 394 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 395 connection->state = AVCTP_W2_SEND_COMMAND; 396 connection->transaction_label++; 397 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 398 connection->command_type = AVRCP_CTYPE_CONTROL; 399 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 400 connection->subunit_id = AVRCP_SUBUNIT_ID; 401 int pos = 0; 402 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 403 pos += 3; 404 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID 405 connection->cmd_operands[pos++] = 0; 406 // Parameter Length 407 connection->cmd_operands_length = 8; 408 big_endian_store_16(connection->cmd_operands, pos, 1); 409 pos += 2; 410 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 411 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 412 return ERROR_CODE_SUCCESS; 413 } 414 415 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 416 uint8_t operands[20]; 417 uint8_t opcode; 418 int pos = 3; 419 // uint8_t transport_header = packet[0]; 420 // uint8_t transaction_label = transport_header >> 4; 421 // uint8_t packet_type = (transport_header & 0x0F) >> 2; 422 // uint8_t frame_type = (transport_header & 0x03) >> 1; 423 // uint8_t ipid = transport_header & 0x01; 424 // uint8_t byte_value = packet[2]; 425 // uint16_t pid = (byte_value << 8) | packet[2]; 426 427 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 428 uint8_t byte_value = packet[pos++]; 429 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 430 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 431 opcode = packet[pos++]; 432 433 // printf(" Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n", 434 // transport_header, transaction_label, packet_type, frame_type, ipid, pid); 435 // // printf_hexdump(packet+pos, size-pos); 436 437 uint8_t pdu_id; 438 uint16_t param_length; 439 switch (avrcp_cmd_opcode(packet,size)){ 440 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 441 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 442 connection->state = AVCTP_CONNECTION_OPENED; 443 // operands: 444 memcpy(operands, packet+pos, 5); 445 uint8_t unit_type = operands[1] >> 3; 446 uint8_t max_subunit_ID = operands[1] & 0x07; 447 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); 448 break; 449 } 450 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 451 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 452 connection->state = AVCTP_CONNECTION_OPENED; 453 // operands: 454 memcpy(operands, packet+pos, 5); 455 uint8_t unit_type = operands[1] >> 3; 456 uint8_t unit = operands[1] & 0x07; 457 uint32_t company_id = operands[2] << 16 | operands[3] << 8 | operands[4]; 458 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, 459 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 460 break; 461 } 462 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 463 if (size - pos < 7) { 464 log_error("avrcp: wrong packet size"); 465 return; 466 }; 467 // operands: 468 memcpy(operands, packet+pos, 7); 469 pos += 7; 470 // uint32_t company_id = operands[0] << 16 | operands[1] << 8 | operands[2]; 471 pdu_id = operands[3]; 472 473 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE && pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 474 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 475 return; 476 } 477 connection->state = AVCTP_CONNECTION_OPENED; 478 479 480 // uint8_t unit_type = operands[4] >> 3; 481 // uint8_t unit = operands[4] & 0x07; 482 param_length = big_endian_read_16(operands, 5); 483 484 // 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", 485 // ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 486 487 // if (ctype == AVRCP_CTYPE_RESPONSE_INTERIM) return; 488 log_info(" VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 489 switch (pdu_id){ 490 case AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue:{ 491 uint8_t num_attributes = packet[pos++]; 492 int i; 493 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 494 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 495 for (i = 0; i < num_attributes; i++){ 496 uint8_t attribute_id = packet[pos++]; 497 uint8_t value = packet[pos++]; 498 switch (attribute_id){ 499 case 0x02: 500 repeat_mode = (avrcp_repeat_mode_t) value; 501 break; 502 case 0x03: 503 shuffle_mode = (avrcp_shuffle_mode_t) value; 504 break; 505 default: 506 break; 507 } 508 } 509 avrcp_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 510 break; 511 } 512 case AVRCP_PDU_ID_SetPlayerApplicationSettingValue:{ 513 uint8_t event[6]; 514 int offset = 0; 515 event[offset++] = HCI_EVENT_AVRCP_META; 516 event[offset++] = sizeof(event) - 2; 517 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 518 little_endian_store_16(event, offset, connection->avrcp_cid); 519 offset += 2; 520 event[offset++] = ctype; 521 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 522 break; 523 } 524 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 525 uint8_t event[7]; 526 int offset = 0; 527 event[offset++] = HCI_EVENT_AVRCP_META; 528 event[offset++] = sizeof(event) - 2; 529 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 530 little_endian_store_16(event, offset, connection->avrcp_cid); 531 offset += 2; 532 event[offset++] = ctype; 533 event[offset++] = packet[pos++]; 534 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 535 break; 536 } 537 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 538 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 539 uint8_t capability_count = packet[pos++]; 540 int i; 541 switch (capability_id){ 542 case AVRCP_CAPABILITY_ID_COMPANY: 543 // log_info("Supported companies %d: ", capability_count); 544 for (i = 0; i < capability_count; i++){ 545 uint32_t company_id = big_endian_read_24(packet, pos); 546 pos += 3; 547 log_info(" 0x%06" PRIx32 ", ", company_id); 548 } 549 break; 550 case AVRCP_CAPABILITY_ID_EVENT: 551 // log_info("Supported events %d: ", capability_count); 552 for (i = 0; i < capability_count; i++){ 553 uint8_t event_id = packet[pos++]; 554 log_info(" 0x%02x %s", event_id, avrcp_event2str(event_id)); 555 } 556 break; 557 } 558 break; 559 } 560 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 561 uint32_t song_length = big_endian_read_32(packet, pos); 562 pos += 4; 563 uint32_t song_position = big_endian_read_32(packet, pos); 564 pos += 4; 565 uint8_t play_status = packet[pos]; 566 // log_info(" GET_PLAY_STATUS length 0x%04X, position 0x%04X, status %s", song_length, song_position, avrcp_play_status2str(play_status)); 567 568 uint8_t event[15]; 569 int offset = 0; 570 event[offset++] = HCI_EVENT_AVRCP_META; 571 event[offset++] = sizeof(event) - 2; 572 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 573 little_endian_store_16(event, offset, connection->avrcp_cid); 574 offset += 2; 575 event[offset++] = ctype; 576 little_endian_store_32(event, offset, song_length); 577 offset += 4; 578 little_endian_store_32(event, offset, song_position); 579 offset += 4; 580 event[offset++] = play_status; 581 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 582 break; 583 } 584 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 585 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos++]; 586 uint16_t event_mask = (1 << event_id); 587 uint16_t reset_event_mask = ~event_mask; 588 switch (ctype){ 589 case AVRCP_CTYPE_RESPONSE_INTERIM: 590 // register as enabled 591 connection->notifications_enabled |= event_mask; 592 // printf("INTERIM notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 593 break; 594 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 595 // received change, event is considered deregistered 596 // we are re-enabling it automatically, if it is not 597 // explicitly disabled 598 connection->notifications_enabled &= reset_event_mask; 599 if (! (connection->notifications_to_deregister & event_mask)){ 600 avrcp_register_notification(connection, event_id); 601 // printf("CHANGED_STABLE notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 602 } else { 603 connection->notifications_to_deregister &= reset_event_mask; 604 } 605 break; 606 default: 607 connection->notifications_to_register &= reset_event_mask; 608 connection->notifications_enabled &= reset_event_mask; 609 connection->notifications_to_deregister &= reset_event_mask; 610 break; 611 } 612 613 switch (event_id){ 614 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 615 uint8_t event[7]; 616 int offset = 0; 617 event[offset++] = HCI_EVENT_AVRCP_META; 618 event[offset++] = sizeof(event) - 2; 619 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 620 little_endian_store_16(event, offset, connection->avrcp_cid); 621 offset += 2; 622 event[offset++] = ctype; 623 event[offset++] = packet[pos]; 624 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 625 break; 626 } 627 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 628 uint8_t event[6]; 629 int offset = 0; 630 event[offset++] = HCI_EVENT_AVRCP_META; 631 event[offset++] = sizeof(event) - 2; 632 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 633 little_endian_store_16(event, offset, connection->avrcp_cid); 634 offset += 2; 635 event[offset++] = ctype; 636 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 637 break; 638 } 639 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 640 uint8_t event[6]; 641 int offset = 0; 642 event[offset++] = HCI_EVENT_AVRCP_META; 643 event[offset++] = sizeof(event) - 2; 644 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 645 little_endian_store_16(event, offset, connection->avrcp_cid); 646 offset += 2; 647 event[offset++] = ctype; 648 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 649 break; 650 } 651 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 652 uint8_t event[6]; 653 int offset = 0; 654 event[offset++] = HCI_EVENT_AVRCP_META; 655 event[offset++] = sizeof(event) - 2; 656 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 657 little_endian_store_16(event, offset, connection->avrcp_cid); 658 offset += 2; 659 event[offset++] = ctype; 660 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 661 break; 662 } 663 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 664 uint8_t event[7]; 665 int offset = 0; 666 event[offset++] = HCI_EVENT_AVRCP_META; 667 event[offset++] = sizeof(event) - 2; 668 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 669 little_endian_store_16(event, offset, connection->avrcp_cid); 670 offset += 2; 671 event[offset++] = ctype; 672 event[offset++] = packet[pos++] & 0x7F; 673 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 674 break; 675 } 676 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 677 uint8_t event[7]; 678 int offset = 0; 679 event[offset++] = HCI_EVENT_AVRCP_META; 680 event[offset++] = sizeof(event) - 2; 681 event[offset++] = AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED; 682 little_endian_store_16(event, offset, connection->avrcp_cid); 683 offset += 2; 684 event[offset++] = ctype; 685 event[offset++] = packet[pos++] & 0x7F; 686 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 687 break; 688 } 689 690 // case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:{ 691 // uint8_t num_PlayerApplicationSettingAttributes = packet[pos++]; 692 // int i; 693 // for (i = 0; i < num_PlayerApplicationSettingAttributes; i++){ 694 // uint8_t PlayerApplicationSetting_AttributeID = packet[pos++]; 695 // uint8_t PlayerApplicationSettingValueID = packet[pos++]; 696 // } 697 // break; 698 // } 699 // case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 700 // uint16_t player_id = big_endian_read_16(packet, pos); 701 // pos += 2; 702 // uint16_t uid_counter = big_endian_read_16(packet, pos); 703 // pos += 2; 704 // break; 705 // case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 706 // uint16_t uid_counter = big_endian_read_16(packet, pos); 707 // pos += 2; 708 // break; 709 default: 710 log_info("avrcp: not implemented"); 711 break; 712 } 713 if (connection->notifications_to_register != 0){ 714 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 715 } 716 break; 717 } 718 719 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 720 avrcp_packet_type_t packet_type = operands[4] & 0x03; 721 switch (packet_type){ 722 case AVRCP_START_PACKET: 723 case AVRCP_SINGLE_PACKET: 724 avrcp_parser_reset(connection); 725 connection->list_size = param_length; 726 connection->num_attributes = packet[pos++]; 727 728 // 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); 729 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 730 731 if (packet_type == AVRCP_START_PACKET){ 732 avrcp_controller_request_continue_response(connection); 733 } 734 break; 735 case AVRCP_CONTINUE_PACKET: 736 case AVRCP_END_PACKET: 737 connection->num_received_fragments++; 738 if (connection->num_received_fragments < connection->max_num_fragments){ 739 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 740 if (packet_type == AVRCP_CONTINUE_PACKET){ 741 avrcp_controller_request_continue_response(connection); 742 } 743 } else { 744 avrcp_controller_request_abort_continuation(connection); 745 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 746 avrcp_parser_reset(connection); 747 } 748 break; 749 } 750 } 751 default: 752 break; 753 } 754 break; 755 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 756 // 0x80 | connection->cmd_operands[0] 757 uint8_t operation_id = packet[pos++]; 758 switch (connection->state){ 759 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 760 if (connection->continuous_fast_forward_cmd){ 761 connection->state = AVCTP_W4_STOP; 762 } else { 763 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 764 } 765 break; 766 case AVCTP_W2_RECEIVE_RESPONSE: 767 connection->state = AVCTP_CONNECTION_OPENED; 768 break; 769 default: 770 // check for notifications? move state transition down 771 // log_info("AVRCP_CMD_OPCODE_PASS_THROUGH state %d\n", connection->state); 772 break; 773 } 774 if (connection->state == AVCTP_W4_STOP){ 775 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 776 } 777 if (connection->state == AVCTP_CONNECTION_OPENED) { 778 // RELEASE response 779 operation_id = operation_id & 0x7F; 780 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 781 } 782 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 783 // PRESS response 784 request_pass_through_release_control_cmd(connection); 785 } 786 break; 787 } 788 default: 789 break; 790 } 791 } 792 793 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 794 int i; 795 switch (connection->state){ 796 case AVCTP_W2_SEND_PRESS_COMMAND: 797 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 798 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 799 break; 800 case AVCTP_W2_SEND_COMMAND: 801 case AVCTP_W2_SEND_RELEASE_COMMAND: 802 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 803 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 804 break; 805 case AVCTP_CONNECTION_OPENED: 806 if (connection->notifications_to_register != 0){ 807 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){ 808 if (connection->notifications_to_register & (1<<i)){ 809 connection->notifications_to_register &= ~ (1 << i); 810 avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i); 811 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 812 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 813 return; 814 } 815 } 816 } 817 return; 818 default: 819 return; 820 } 821 } 822 823 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 824 avrcp_connection_t * connection; 825 826 switch (packet_type) { 827 case L2CAP_DATA_PACKET: 828 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 829 if (!connection) break; 830 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 831 break; 832 case HCI_EVENT_PACKET: 833 switch (hci_event_packet_get_type(packet)){ 834 case L2CAP_EVENT_CAN_SEND_NOW: 835 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 836 if (!connection) break; 837 avrcp_controller_handle_can_send_now(connection); 838 break; 839 default: 840 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 841 break; 842 } 843 default: 844 break; 845 } 846 } 847 848 void avrcp_controller_init(void){ 849 avrcp_controller_context.role = AVRCP_CONTROLLER; 850 avrcp_controller_context.connections = NULL; 851 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 852 l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); 853 } 854 855 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 856 if (callback == NULL){ 857 log_error("avrcp_register_packet_handler called with NULL callback"); 858 return; 859 } 860 avrcp_controller_context.avrcp_callback = callback; 861 } 862 863 uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 864 return avrcp_connect(bd_addr, &avrcp_controller_context, avrcp_cid); 865 } 866 867 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 868 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 869 if (!connection){ 870 log_error("avrcp_unit_info: could not find a connection."); 871 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 872 } 873 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 874 connection->state = AVCTP_W2_SEND_COMMAND; 875 876 connection->transaction_label++; 877 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 878 connection->command_type = AVRCP_CTYPE_STATUS; 879 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 880 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 881 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 882 connection->cmd_operands_length = 5; 883 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 884 return ERROR_CODE_SUCCESS; 885 } 886 887 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 888 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 889 if (!connection){ 890 log_error("avrcp_unit_info: could not find a connection."); 891 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 892 } 893 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 894 connection->state = AVCTP_W2_SEND_COMMAND; 895 896 connection->transaction_label++; 897 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 898 connection->command_type = AVRCP_CTYPE_STATUS; 899 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 900 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 901 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 902 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 903 connection->cmd_operands_length = 5; 904 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 905 return ERROR_CODE_SUCCESS; 906 } 907 908 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 909 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 910 if (!connection){ 911 log_error("avrcp_get_capabilities: could not find a connection."); 912 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 913 } 914 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 915 connection->state = AVCTP_W2_SEND_COMMAND; 916 917 connection->transaction_label++; 918 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 919 connection->command_type = AVRCP_CTYPE_STATUS; 920 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 921 connection->subunit_id = AVRCP_SUBUNIT_ID; 922 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 923 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 924 connection->cmd_operands[4] = 0; 925 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 926 connection->cmd_operands[7] = capability_id; // capability ID 927 connection->cmd_operands_length = 8; 928 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 929 return ERROR_CODE_SUCCESS; 930 } 931 932 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 933 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 934 } 935 936 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 937 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 938 } 939 940 941 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 942 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 943 } 944 945 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 946 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 947 } 948 949 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 950 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 951 } 952 953 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 954 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 955 } 956 957 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 958 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 959 } 960 961 uint8_t avrcp_controller_start_rewind(uint16_t avrcp_cid){ 962 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 963 } 964 965 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 966 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 967 } 968 969 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 970 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 971 } 972 973 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 974 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 975 } 976 977 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 978 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 979 } 980 981 uint8_t avrcp_controller_stop_rewind(uint16_t avrcp_cid){ 982 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 983 if (!connection){ 984 log_error("avrcp_stop_rewind: could not find a connection."); 985 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 986 } 987 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 988 return request_pass_through_release_control_cmd(connection); 989 } 990 991 uint8_t avrcp_controller_start_fast_forward(uint16_t avrcp_cid){ 992 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 993 } 994 995 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 996 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 997 } 998 999 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1000 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1001 } 1002 1003 1004 uint8_t avrcp_controller_stop_fast_forward(uint16_t avrcp_cid){ 1005 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1006 if (!connection){ 1007 log_error("avrcp_stop_fast_forward: could not find a connection."); 1008 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1009 } 1010 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1011 return request_pass_through_release_control_cmd(connection); 1012 } 1013 1014 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1015 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1016 if (!connection){ 1017 log_error("avrcp_get_play_status: could not find a connection."); 1018 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1019 } 1020 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1021 connection->state = AVCTP_W2_SEND_COMMAND; 1022 connection->transaction_label++; 1023 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1024 connection->command_type = AVRCP_CTYPE_STATUS; 1025 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1026 connection->subunit_id = AVRCP_SUBUNIT_ID; 1027 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1028 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1029 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1030 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1031 connection->cmd_operands_length = 7; 1032 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1033 return ERROR_CODE_SUCCESS; 1034 } 1035 1036 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1037 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1038 if (!connection){ 1039 log_error("avrcp_get_play_status: could not find a connection."); 1040 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1041 } 1042 avrcp_register_notification(connection, event_id); 1043 return ERROR_CODE_SUCCESS; 1044 } 1045 1046 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1047 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1048 if (!connection){ 1049 log_error("avrcp_get_play_status: could not find a connection."); 1050 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1051 } 1052 connection->notifications_to_deregister |= (1 << event_id); 1053 return ERROR_CODE_SUCCESS; 1054 } 1055 1056 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1057 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1058 if (!connection){ 1059 log_error("avrcp_get_capabilities: could not find a connection."); 1060 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1061 } 1062 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1063 connection->state = AVCTP_W2_SEND_COMMAND; 1064 1065 connection->transaction_label++; 1066 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1067 connection->command_type = AVRCP_CTYPE_CONTROL; 1068 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1069 connection->subunit_id = AVRCP_SUBUNIT_ID; 1070 int pos = 0; 1071 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1072 pos += 3; 1073 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1074 connection->cmd_operands[pos++] = 0; 1075 1076 // Parameter Length 1077 big_endian_store_16(connection->cmd_operands, pos, 2); 1078 pos += 2; 1079 1080 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1081 pos += 2; 1082 1083 connection->cmd_operands_length = pos; 1084 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1085 return ERROR_CODE_SUCCESS; 1086 } 1087 1088 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1089 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1090 if (!connection){ 1091 log_error("avrcp_get_capabilities: could not find a connection."); 1092 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1093 } 1094 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1095 connection->state = AVCTP_W2_SEND_COMMAND; 1096 1097 connection->transaction_label++; 1098 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1099 connection->command_type = AVRCP_CTYPE_STATUS; 1100 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1101 connection->subunit_id = AVRCP_SUBUNIT_ID; 1102 int pos = 0; 1103 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1104 pos += 3; 1105 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1106 connection->cmd_operands[pos++] = 0; 1107 1108 // Parameter Length 1109 big_endian_store_16(connection->cmd_operands, pos, 9); 1110 pos += 2; 1111 1112 // write 8 bytes value 1113 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1114 pos += 8; 1115 1116 connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 1117 // every attribute is 4 bytes long 1118 1119 connection->cmd_operands_length = pos; 1120 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1121 return ERROR_CODE_SUCCESS; 1122 } 1123 1124 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1125 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1126 if (!connection){ 1127 log_error("avrcp_get_capabilities: could not find a connection."); 1128 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1129 } 1130 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1131 connection->state = AVCTP_W2_SEND_COMMAND; 1132 1133 connection->transaction_label++; 1134 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1135 connection->command_type = AVRCP_CTYPE_CONTROL; 1136 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1137 connection->subunit_id = AVRCP_SUBUNIT_ID; 1138 int pos = 0; 1139 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1140 pos += 3; 1141 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1142 connection->cmd_operands[pos++] = 0; 1143 1144 // Parameter Length 1145 big_endian_store_16(connection->cmd_operands, pos, 1); 1146 pos += 2; 1147 connection->cmd_operands[pos++] = volume; 1148 1149 connection->cmd_operands_length = pos; 1150 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1151 return ERROR_CODE_SUCCESS; 1152 } 1153 1154 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1155 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1156 if (!connection){ 1157 log_error("avrcp_get_capabilities: could not find a connection."); 1158 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1159 } 1160 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1161 connection->state = AVCTP_W2_SEND_COMMAND; 1162 1163 connection->transaction_label++; 1164 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1165 connection->command_type = AVRCP_CTYPE_STATUS; 1166 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1167 connection->subunit_id = AVRCP_SUBUNIT_ID; 1168 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1169 connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID 1170 connection->cmd_operands[4] = 0; 1171 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1172 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1173 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1174 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1175 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1176 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1177 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1178 connection->cmd_operands_length = 12; 1179 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1180 return ERROR_CODE_SUCCESS; 1181 } 1182 1183 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1184 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1185 if (!connection){ 1186 log_error("avrcp_get_capabilities: could not find a connection."); 1187 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1188 } 1189 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1190 connection->state = AVCTP_W2_SEND_COMMAND; 1191 1192 connection->transaction_label++; 1193 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1194 connection->command_type = AVRCP_CTYPE_CONTROL; 1195 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1196 connection->subunit_id = AVRCP_SUBUNIT_ID; 1197 int pos = 0; 1198 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1199 pos += 3; 1200 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID 1201 connection->cmd_operands[pos++] = 0; 1202 // Parameter Length 1203 big_endian_store_16(connection->cmd_operands, pos, 3); 1204 pos += 2; 1205 connection->cmd_operands[pos++] = 2; 1206 connection->cmd_operands_length = pos; 1207 connection->cmd_operands[pos++] = attr_id; 1208 connection->cmd_operands[pos++] = attr_value; 1209 connection->cmd_operands_length = pos; 1210 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1211 return ERROR_CODE_SUCCESS; 1212 } 1213 1214 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1215 if (mode < AVRCP_SHUFFLE_MODE_OFF || mode > AVRCP_SHUFFLE_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1216 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1217 } 1218 1219 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1220 if (mode < AVRCP_REPEAT_MODE_OFF || mode > AVRCP_REPEAT_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1221 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1222 } 1223 1224 uint8_t avrcp_controller_disconnect(uint16_t avrcp_cid){ 1225 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1226 if (!connection){ 1227 log_error("avrcp_get_capabilities: could not find a connection."); 1228 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1229 } 1230 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1231 if (connection->browsing_connection){ 1232 if (connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1233 l2cap_disconnect(connection->browsing_connection->l2cap_browsing_cid, 0); 1234 } 1235 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1236 return ERROR_CODE_SUCCESS; 1237 } 1238 1239 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1240 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1241 if (!connection){ 1242 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1243 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1244 } 1245 if (connection->state != AVCTP_CONNECTION_OPENED){ 1246 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1247 return ERROR_CODE_COMMAND_DISALLOWED; 1248 } 1249 connection->state = AVCTP_W2_SEND_COMMAND; 1250 1251 connection->transaction_label++; 1252 connection->command_type = AVRCP_CTYPE_CONTROL; 1253 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1254 connection->subunit_id = AVRCP_SUBUNIT_ID; 1255 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1256 int pos = 0; 1257 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1258 pos += 3; 1259 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1260 // reserved 1261 connection->cmd_operands[pos++] = 0; 1262 // Parameter Length 1263 big_endian_store_16(connection->cmd_operands, pos, 11); 1264 pos += 2; 1265 connection->cmd_operands[pos++] = scope; 1266 memset(&connection->cmd_operands[pos], 0, 8); 1267 if (uid){ 1268 memcpy(&connection->cmd_operands[pos], uid, 8); 1269 } 1270 pos += 8; 1271 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1272 pos += 2; 1273 connection->cmd_operands_length = pos; 1274 1275 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1276 return ERROR_CODE_SUCCESS; 1277 } 1278 1279 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){ 1280 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1281 if (!connection){ 1282 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1283 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1284 } 1285 if (connection->state != AVCTP_CONNECTION_OPENED){ 1286 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1287 return ERROR_CODE_COMMAND_DISALLOWED; 1288 } 1289 connection->state = AVCTP_W2_SEND_COMMAND; 1290 1291 connection->transaction_label++; 1292 connection->command_type = AVRCP_CTYPE_CONTROL; 1293 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1294 connection->subunit_id = AVRCP_SUBUNIT_ID; 1295 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1296 int pos = 0; 1297 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1298 pos += 3; 1299 connection->cmd_operands[pos++] = AVRCP_PDU_ADD_TO_NOW_PLAYING; // PDU ID 1300 // reserved 1301 connection->cmd_operands[pos++] = 0; 1302 // Parameter Length 1303 big_endian_store_16(connection->cmd_operands, pos, 11); 1304 pos += 2; 1305 connection->cmd_operands[pos++] = scope; 1306 memset(&connection->cmd_operands[pos], 0, 8); 1307 if (uid){ 1308 memcpy(&connection->cmd_operands[pos], uid, 8); 1309 } 1310 pos += 8; 1311 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1312 pos += 2; 1313 connection->cmd_operands_length = pos; 1314 1315 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1316 return ERROR_CODE_SUCCESS; 1317 } 1318 1319 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1320 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1321 if (!connection){ 1322 log_error("avrcp_controller_play_item: could not find a connection."); 1323 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1324 } 1325 connection->max_num_fragments = max_num_fragments; 1326 return ERROR_CODE_SUCCESS; 1327 }