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