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