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