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