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 45 #include "btstack.h" 46 #include "classic/avrcp.h" 47 #include "classic/avrcp_controller.h" 48 49 static avrcp_context_t avrcp_controller_context; 50 51 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){ 52 avrcp_create_sdp_record(1, service, service_record_handle, browsing, supported_features, service_name, service_provider_name); 53 } 54 55 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){ 56 if (!callback) return; 57 uint8_t event[8]; 58 int pos = 0; 59 event[pos++] = HCI_EVENT_AVRCP_META; 60 event[pos++] = sizeof(event) - 2; 61 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 62 little_endian_store_16(event, pos, avrcp_cid); 63 pos += 2; 64 event[pos++] = ctype; 65 event[pos++] = repeat_mode; 66 event[pos++] = shuffle_mode; 67 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 68 } 69 70 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){ 71 if (!callback) return; 72 uint8_t event[7]; 73 int pos = 0; 74 event[pos++] = HCI_EVENT_AVRCP_META; 75 event[pos++] = sizeof(event) - 2; 76 event[pos++] = subevent; 77 little_endian_store_16(event, pos, avrcp_cid); 78 pos += 2; 79 event[pos++] = ctype; 80 event[pos++] = operation_id; 81 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 82 } 83 84 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 85 UNUSED(timer); 86 avrcp_connection_t * connection = btstack_run_loop_get_timer_context(timer); 87 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 88 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 89 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 90 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 91 } 92 93 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 94 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 95 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 96 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 97 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 98 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 99 } 100 101 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 102 connection->continuous_fast_forward_cmd = 0; 103 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 104 } 105 106 static uint8_t request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 107 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 108 if (connection->continuous_fast_forward_cmd){ 109 avrcp_press_and_hold_timer_stop(connection); 110 } 111 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 112 connection->transaction_label++; 113 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 114 return ERROR_CODE_SUCCESS; 115 } 116 117 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){ 118 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, context); 119 if (!connection){ 120 log_error("avrcp: could not find a connection."); 121 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 122 } 123 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 124 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 125 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 126 connection->command_type = AVRCP_CTYPE_CONTROL; 127 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 128 connection->subunit_id = AVRCP_SUBUNIT_ID; 129 connection->cmd_operands_length = 0; 130 131 connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd; 132 connection->cmd_operands_length = 2; 133 connection->cmd_operands[0] = opid; 134 if (playback_speed > 0){ 135 connection->cmd_operands[2] = playback_speed; 136 connection->cmd_operands_length++; 137 } 138 connection->cmd_operands[1] = connection->cmd_operands_length - 2; 139 140 if (connection->continuous_fast_forward_cmd){ 141 avrcp_press_and_hold_timer_start(connection); 142 } 143 144 connection->transaction_label++; 145 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 146 return ERROR_CODE_SUCCESS; 147 } 148 149 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 150 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0, &avrcp_controller_context); 151 } 152 153 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 154 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1, &avrcp_controller_context); 155 } 156 157 static int avrcp_send_cmd(uint16_t cid, avrcp_connection_t * connection){ 158 uint8_t command[30]; 159 int pos = 0; 160 // transport header 161 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 162 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 163 // Profile IDentifier (PID) 164 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 165 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 166 167 // command_type 168 command[pos++] = connection->command_type; 169 // subunit_type | subunit ID 170 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 171 // opcode 172 command[pos++] = (uint8_t)connection->command_opcode; 173 // operands 174 memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length); 175 pos += connection->cmd_operands_length; 176 177 return l2cap_send(cid, command, pos); 178 } 179 180 static int avrcp_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 181 if (connection->notifications_to_deregister & (1 << event_id)) return 0; 182 if (connection->notifications_enabled & (1 << event_id)) return 0; 183 if (connection->notifications_to_register & (1 << event_id)) return 0; 184 connection->notifications_to_register |= (1 << event_id); 185 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 186 return 1; 187 } 188 189 static void avrcp_prepare_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 190 connection->transaction_label++; 191 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 192 connection->command_type = AVRCP_CTYPE_NOTIFY; 193 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 194 connection->subunit_id = AVRCP_SUBUNIT_ID; 195 int pos = 0; 196 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 197 pos += 3; 198 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 199 connection->cmd_operands[pos++] = 0; // reserved(upper 6) | packet_type -> 0 200 big_endian_store_16(connection->cmd_operands, pos, 5); // parameter length 201 pos += 2; 202 connection->cmd_operands[pos++] = event_id; 203 big_endian_store_32(connection->cmd_operands, pos, 0); 204 pos += 4; 205 connection->cmd_operands_length = pos; 206 // AVRCP_SPEC_V14.pdf 166 207 // answer page 61 208 } 209 210 static uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size){ 211 uint8_t cmd_opcode_index = 5; 212 if (cmd_opcode_index > size) return AVRCP_CMD_OPCODE_UNDEFINED; 213 return packet[cmd_opcode_index]; 214 } 215 216 217 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 218 uint8_t operands[20]; 219 uint8_t opcode; 220 int pos = 3; 221 // uint8_t transport_header = packet[0]; 222 // uint8_t transaction_label = transport_header >> 4; 223 // uint8_t packet_type = (transport_header & 0x0F) >> 2; 224 // uint8_t frame_type = (transport_header & 0x03) >> 1; 225 // uint8_t ipid = transport_header & 0x01; 226 // uint8_t byte_value = packet[2]; 227 // uint16_t pid = (byte_value << 8) | packet[2]; 228 229 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 230 uint8_t byte_value = packet[pos++]; 231 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 232 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 233 opcode = packet[pos++]; 234 235 // printf(" Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n", 236 // transport_header, transaction_label, packet_type, frame_type, ipid, pid); 237 // // printf_hexdump(packet+pos, size-pos); 238 239 uint8_t pdu_id; 240 uint16_t param_length; 241 switch (avrcp_cmd_opcode(packet,size)){ 242 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 243 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 244 connection->state = AVCTP_CONNECTION_OPENED; 245 246 // operands: 247 memcpy(operands, packet+pos, 5); 248 uint8_t unit_type = operands[1] >> 3; 249 uint8_t unit = operands[1] & 0x07; 250 uint32_t company_id = operands[2] << 16 | operands[3] << 8 | operands[4]; 251 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%06x", 252 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 253 break; 254 } 255 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 256 if (size - pos < 7) { 257 log_error("avrcp: wrong packet size"); 258 return; 259 }; 260 // operands: 261 memcpy(operands, packet+pos, 7); 262 pos += 7; 263 // uint32_t company_id = operands[0] << 16 | operands[1] << 8 | operands[2]; 264 pdu_id = operands[3]; 265 266 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE && pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 267 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 268 return; 269 } 270 connection->state = AVCTP_CONNECTION_OPENED; 271 272 273 // uint8_t unit_type = operands[4] >> 3; 274 // uint8_t unit = operands[4] & 0x07; 275 param_length = big_endian_read_16(operands, 5); 276 277 // 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", 278 // ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 279 280 // if (ctype == AVRCP_CTYPE_RESPONSE_INTERIM) return; 281 log_info(" VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 282 switch (pdu_id){ 283 case AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue:{ 284 uint8_t num_attributes = packet[pos++]; 285 int i; 286 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 287 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 288 for (i = 0; i < num_attributes; i++){ 289 uint8_t attribute_id = packet[pos++]; 290 uint8_t value = packet[pos++]; 291 switch (attribute_id){ 292 case 0x02: 293 repeat_mode = (avrcp_repeat_mode_t) value; 294 break; 295 case 0x03: 296 shuffle_mode = (avrcp_shuffle_mode_t) value; 297 break; 298 default: 299 break; 300 } 301 } 302 avrcp_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 303 break; 304 } 305 case AVRCP_PDU_ID_SetPlayerApplicationSettingValue:{ 306 uint8_t event[6]; 307 int offset = 0; 308 event[offset++] = HCI_EVENT_AVRCP_META; 309 event[offset++] = sizeof(event) - 2; 310 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 311 little_endian_store_16(event, offset, connection->avrcp_cid); 312 offset += 2; 313 event[offset++] = ctype; 314 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 315 break; 316 } 317 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 318 uint8_t event[7]; 319 int offset = 0; 320 event[offset++] = HCI_EVENT_AVRCP_META; 321 event[offset++] = sizeof(event) - 2; 322 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 323 little_endian_store_16(event, offset, connection->avrcp_cid); 324 offset += 2; 325 event[offset++] = ctype; 326 event[offset++] = packet[pos++]; 327 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 328 break; 329 } 330 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 331 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 332 uint8_t capability_count = packet[pos++]; 333 int i; 334 switch (capability_id){ 335 case AVRCP_CAPABILITY_ID_COMPANY: 336 // log_info("Supported companies %d: ", capability_count); 337 for (i = 0; i < capability_count; i++){ 338 uint32_t company_id = big_endian_read_24(packet, pos); 339 pos += 3; 340 log_info(" 0x%06x, ", company_id); 341 } 342 break; 343 case AVRCP_CAPABILITY_ID_EVENT: 344 // log_info("Supported events %d: ", capability_count); 345 for (i = 0; i < capability_count; i++){ 346 uint8_t event_id = packet[pos++]; 347 log_info(" 0x%02x %s", event_id, avrcp_event2str(event_id)); 348 } 349 break; 350 } 351 break; 352 } 353 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 354 uint32_t song_length = big_endian_read_32(packet, pos); 355 pos += 4; 356 uint32_t song_position = big_endian_read_32(packet, pos); 357 pos += 4; 358 uint8_t play_status = packet[pos]; 359 // log_info(" GET_PLAY_STATUS length 0x%04X, position 0x%04X, status %s", song_length, song_position, avrcp_play_status2str(play_status)); 360 361 uint8_t event[15]; 362 int offset = 0; 363 event[offset++] = HCI_EVENT_AVRCP_META; 364 event[offset++] = sizeof(event) - 2; 365 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 366 little_endian_store_16(event, offset, connection->avrcp_cid); 367 offset += 2; 368 event[offset++] = ctype; 369 little_endian_store_32(event, offset, song_length); 370 offset += 4; 371 little_endian_store_32(event, offset, song_position); 372 offset += 4; 373 event[offset++] = play_status; 374 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 375 break; 376 } 377 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 378 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos++]; 379 uint16_t event_mask = (1 << event_id); 380 uint16_t reset_event_mask = ~event_mask; 381 switch (ctype){ 382 case AVRCP_CTYPE_RESPONSE_INTERIM: 383 // register as enabled 384 connection->notifications_enabled |= event_mask; 385 // printf("INTERIM notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 386 break; 387 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 388 // received change, event is considered deregistered 389 // we are re-enabling it automatically, if it is not 390 // explicitly disabled 391 connection->notifications_enabled &= reset_event_mask; 392 if (! (connection->notifications_to_deregister & event_mask)){ 393 avrcp_register_notification(connection, event_id); 394 // printf("CHANGED_STABLE notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 395 } else { 396 connection->notifications_to_deregister &= reset_event_mask; 397 } 398 break; 399 default: 400 connection->notifications_to_register &= reset_event_mask; 401 connection->notifications_enabled &= reset_event_mask; 402 connection->notifications_to_deregister &= reset_event_mask; 403 break; 404 } 405 406 switch (event_id){ 407 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 408 uint8_t event[7]; 409 int offset = 0; 410 event[offset++] = HCI_EVENT_AVRCP_META; 411 event[offset++] = sizeof(event) - 2; 412 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 413 little_endian_store_16(event, offset, connection->avrcp_cid); 414 offset += 2; 415 event[offset++] = ctype; 416 event[offset++] = packet[pos]; 417 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 418 break; 419 } 420 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 421 uint8_t event[6]; 422 int offset = 0; 423 event[offset++] = HCI_EVENT_AVRCP_META; 424 event[offset++] = sizeof(event) - 2; 425 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 426 little_endian_store_16(event, offset, connection->avrcp_cid); 427 offset += 2; 428 event[offset++] = ctype; 429 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 430 break; 431 } 432 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 433 uint8_t event[6]; 434 int offset = 0; 435 event[offset++] = HCI_EVENT_AVRCP_META; 436 event[offset++] = sizeof(event) - 2; 437 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 438 little_endian_store_16(event, offset, connection->avrcp_cid); 439 offset += 2; 440 event[offset++] = ctype; 441 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 442 break; 443 } 444 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 445 uint8_t event[6]; 446 int offset = 0; 447 event[offset++] = HCI_EVENT_AVRCP_META; 448 event[offset++] = sizeof(event) - 2; 449 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 450 little_endian_store_16(event, offset, connection->avrcp_cid); 451 offset += 2; 452 event[offset++] = ctype; 453 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 454 break; 455 } 456 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 457 uint8_t event[7]; 458 int offset = 0; 459 event[offset++] = HCI_EVENT_AVRCP_META; 460 event[offset++] = sizeof(event) - 2; 461 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 462 little_endian_store_16(event, offset, connection->avrcp_cid); 463 offset += 2; 464 event[offset++] = ctype; 465 event[offset++] = packet[pos++] & 0x7F; 466 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 467 break; 468 } 469 // case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:{ 470 // uint8_t num_PlayerApplicationSettingAttributes = packet[pos++]; 471 // int i; 472 // for (i = 0; i < num_PlayerApplicationSettingAttributes; i++){ 473 // uint8_t PlayerApplicationSetting_AttributeID = packet[pos++]; 474 // uint8_t PlayerApplicationSettingValueID = packet[pos++]; 475 // } 476 // break; 477 // } 478 // case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 479 // uint16_t player_id = big_endian_read_16(packet, pos); 480 // pos += 2; 481 // uint16_t uid_counter = big_endian_read_16(packet, pos); 482 // pos += 2; 483 // break; 484 // case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 485 // uint16_t uid_counter = big_endian_read_16(packet, pos); 486 // pos += 2; 487 // break; 488 default: 489 log_info("avrcp: not implemented"); 490 break; 491 } 492 if (connection->notifications_to_register != 0){ 493 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 494 } 495 break; 496 } 497 498 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 499 uint8_t num_attributes = packet[pos++]; 500 int i; 501 struct item { 502 uint16_t len; 503 uint8_t * value; 504 } items[AVRCP_MEDIA_ATTR_COUNT]; 505 memset(items, 0, sizeof(items)); 506 507 uint16_t string_attributes_len = 0; 508 uint8_t num_string_attributes = 0; 509 uint16_t total_event_payload_for_string_attributes = HCI_EVENT_PAYLOAD_SIZE-2; 510 uint16_t max_string_attribute_value_len = 0; 511 if (ctype == AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE || ctype == AVRCP_CTYPE_RESPONSE_CHANGED_STABLE){ 512 for (i = 0; i < num_attributes; i++){ 513 avrcp_media_attribute_id_t attr_id = (avrcp_media_attribute_id_t) big_endian_read_32(packet, pos); 514 pos += 4; 515 // uint16_t character_set = big_endian_read_16(packet, pos); 516 pos += 2; 517 uint16_t attr_value_length = big_endian_read_16(packet, pos); 518 pos += 2; 519 520 // debug - to remove later 521 uint8_t value[100]; 522 uint16_t value_len = sizeof(value) <= attr_value_length? sizeof(value) - 1 : attr_value_length; 523 memcpy(value, packet+pos, value_len); 524 value[value_len] = 0; 525 // printf("Now Playing Info %s: %s \n", attribute2str(attr_id), value); 526 // end debug 527 528 if ((attr_id >= 1) || (attr_id <= AVRCP_MEDIA_ATTR_COUNT)) { 529 items[attr_id-1].len = attr_value_length; 530 items[attr_id-1].value = &packet[pos]; 531 switch (attr_id){ 532 case AVRCP_MEDIA_ATTR_TITLE: 533 case AVRCP_MEDIA_ATTR_ARTIST: 534 case AVRCP_MEDIA_ATTR_ALBUM: 535 case AVRCP_MEDIA_ATTR_GENRE: 536 num_string_attributes++; 537 string_attributes_len += attr_value_length; 538 if (max_string_attribute_value_len < attr_value_length){ 539 max_string_attribute_value_len = attr_value_length; 540 } 541 break; 542 default: 543 break; 544 } 545 } 546 pos += attr_value_length; 547 } 548 } 549 550 // subtract space for fixed fields 551 total_event_payload_for_string_attributes -= 14 + 4; // 4 for '\0' 552 553 // @TODO optimize space by repeatedly decreasing max_string_attribute_value_len until it fits into buffer instead of crude divion 554 uint16_t max_value_len = total_event_payload_for_string_attributes > string_attributes_len? max_string_attribute_value_len : total_event_payload_for_string_attributes/(string_attributes_len+1) - 1; 555 // printf("num_string_attributes %d, string_attributes_len %d, total_event_payload_for_string_attributes %d, max_value_len %d \n", num_string_attributes, string_attributes_len, total_event_payload_for_string_attributes, max_value_len); 556 557 const uint8_t attribute_order[] = { 558 AVRCP_MEDIA_ATTR_TRACK, 559 AVRCP_MEDIA_ATTR_TOTAL_TRACKS, 560 AVRCP_MEDIA_ATTR_SONG_LENGTH, 561 AVRCP_MEDIA_ATTR_TITLE, 562 AVRCP_MEDIA_ATTR_ARTIST, 563 AVRCP_MEDIA_ATTR_ALBUM, 564 AVRCP_MEDIA_ATTR_GENRE 565 }; 566 567 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 568 event[0] = HCI_EVENT_AVRCP_META; 569 pos = 2; 570 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO; 571 little_endian_store_16(event, pos, connection->avrcp_cid); 572 pos += 2; 573 event[pos++] = ctype; 574 for (i = 0; i < sizeof(attribute_order); i++){ 575 avrcp_media_attribute_id_t attr_id = (avrcp_media_attribute_id_t) attribute_order[i]; 576 uint16_t value_len = 0; 577 switch (attr_id){ 578 case AVRCP_MEDIA_ATTR_TITLE: 579 case AVRCP_MEDIA_ATTR_ARTIST: 580 case AVRCP_MEDIA_ATTR_ALBUM: 581 case AVRCP_MEDIA_ATTR_GENRE: 582 if (items[attr_id-1].value){ 583 value_len = items[attr_id-1].len <= max_value_len ? items[attr_id-1].len : max_value_len; 584 } 585 event[pos++] = value_len + 1; 586 if (value_len){ 587 memcpy(event+pos, items[attr_id-1].value, value_len); 588 pos += value_len; 589 } 590 event[pos++] = 0; 591 break; 592 case AVRCP_MEDIA_ATTR_SONG_LENGTH: 593 if (items[attr_id-1].value){ 594 little_endian_store_32(event, pos, btstack_atoi((char *)items[attr_id-1].value)); 595 } else { 596 little_endian_store_32(event, pos, 0); 597 } 598 pos += 4; 599 break; 600 case AVRCP_MEDIA_ATTR_TRACK: 601 case AVRCP_MEDIA_ATTR_TOTAL_TRACKS: 602 if (items[attr_id-1].value){ 603 event[pos++] = btstack_atoi((char *)items[attr_id-1].value); 604 } else { 605 event[pos++] = 0; 606 } 607 break; 608 } 609 } 610 event[1] = pos - 2; 611 // printf_hexdump(event, pos); 612 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, pos); 613 break; 614 } 615 default: 616 break; 617 } 618 break; 619 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 620 // 0x80 | connection->cmd_operands[0] 621 uint8_t operation_id = packet[pos++]; 622 switch (connection->state){ 623 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 624 if (connection->continuous_fast_forward_cmd){ 625 connection->state = AVCTP_W4_STOP; 626 } else { 627 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 628 } 629 break; 630 case AVCTP_W2_RECEIVE_RESPONSE: 631 connection->state = AVCTP_CONNECTION_OPENED; 632 break; 633 default: 634 // check for notifications? move state transition down 635 // log_info("AVRCP_CMD_OPCODE_PASS_THROUGH state %d\n", connection->state); 636 break; 637 } 638 if (connection->state == AVCTP_W4_STOP){ 639 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 640 } 641 if (connection->state == AVCTP_CONNECTION_OPENED) { 642 // RELEASE response 643 operation_id = operation_id & 0x7F; 644 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 645 } 646 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 647 // PRESS response 648 request_pass_through_release_control_cmd(connection); 649 } 650 break; 651 } 652 default: 653 break; 654 } 655 } 656 657 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 658 int i; 659 switch (connection->state){ 660 case AVCTP_W2_SEND_PRESS_COMMAND: 661 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 662 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 663 break; 664 case AVCTP_W2_SEND_COMMAND: 665 case AVCTP_W2_SEND_RELEASE_COMMAND: 666 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 667 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 668 break; 669 case AVCTP_CONNECTION_OPENED: 670 if (connection->notifications_to_register != 0){ 671 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){ 672 if (connection->notifications_to_register & (1<<i)){ 673 connection->notifications_to_register &= ~ (1 << i); 674 avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i); 675 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 676 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 677 return; 678 } 679 } 680 } 681 return; 682 default: 683 return; 684 } 685 } 686 687 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 688 avrcp_connection_t * connection; 689 690 switch (packet_type) { 691 case L2CAP_DATA_PACKET: 692 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 693 if (!connection) break; 694 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 695 break; 696 case HCI_EVENT_PACKET: 697 switch (hci_event_packet_get_type(packet)){ 698 case L2CAP_EVENT_CAN_SEND_NOW: 699 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 700 if (!connection) break; 701 avrcp_controller_handle_can_send_now(connection); 702 break; 703 default: 704 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 705 break; 706 } 707 default: 708 break; 709 } 710 } 711 712 void avrcp_controller_init(void){ 713 avrcp_controller_context.role = AVRCP_CONTROLLER; 714 avrcp_controller_context.connections = NULL; 715 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 716 l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); 717 } 718 719 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 720 if (callback == NULL){ 721 log_error("avrcp_register_packet_handler called with NULL callback"); 722 return; 723 } 724 avrcp_controller_context.avrcp_callback = callback; 725 } 726 727 uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 728 return avrcp_connect(bd_addr, &avrcp_controller_context, avrcp_cid); 729 } 730 731 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 732 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 733 if (!connection){ 734 log_error("avrcp_unit_info: could not find a connection."); 735 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 736 } 737 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 738 connection->state = AVCTP_W2_SEND_COMMAND; 739 740 connection->transaction_label++; 741 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 742 connection->command_type = AVRCP_CTYPE_STATUS; 743 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 744 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 745 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 746 connection->cmd_operands_length = 5; 747 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 748 return ERROR_CODE_SUCCESS; 749 } 750 751 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 752 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 753 if (!connection){ 754 log_error("avrcp_get_capabilities: could not find a connection."); 755 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 756 } 757 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 758 connection->state = AVCTP_W2_SEND_COMMAND; 759 760 connection->transaction_label++; 761 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 762 connection->command_type = AVRCP_CTYPE_STATUS; 763 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 764 connection->subunit_id = AVRCP_SUBUNIT_ID; 765 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 766 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 767 connection->cmd_operands[4] = 0; 768 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 769 connection->cmd_operands[7] = capability_id; // capability ID 770 connection->cmd_operands_length = 8; 771 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 772 return ERROR_CODE_SUCCESS; 773 } 774 775 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 776 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 777 } 778 779 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 780 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 781 } 782 783 784 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 785 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 786 } 787 788 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 789 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 790 } 791 792 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 793 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 794 } 795 796 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 797 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 798 } 799 800 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 801 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 802 } 803 804 uint8_t avrcp_controller_start_rewind(uint16_t avrcp_cid){ 805 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 806 } 807 808 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 809 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 810 } 811 812 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 813 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 814 } 815 816 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 817 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 818 } 819 820 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 821 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 822 } 823 824 uint8_t avrcp_controller_stop_rewind(uint16_t avrcp_cid){ 825 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 826 if (!connection){ 827 log_error("avrcp_stop_rewind: could not find a connection."); 828 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 829 } 830 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 831 return request_pass_through_release_control_cmd(connection); 832 } 833 834 uint8_t avrcp_controller_start_fast_forward(uint16_t avrcp_cid){ 835 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 836 } 837 838 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 839 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 840 } 841 842 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 843 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 844 } 845 846 847 uint8_t avrcp_controller_stop_fast_forward(uint16_t avrcp_cid){ 848 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 849 if (!connection){ 850 log_error("avrcp_stop_fast_forward: could not find a connection."); 851 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 852 } 853 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 854 return request_pass_through_release_control_cmd(connection); 855 } 856 857 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 858 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 859 if (!connection){ 860 log_error("avrcp_get_play_status: could not find a connection."); 861 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 862 } 863 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 864 connection->state = AVCTP_W2_SEND_COMMAND; 865 connection->transaction_label++; 866 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 867 connection->command_type = AVRCP_CTYPE_STATUS; 868 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 869 connection->subunit_id = AVRCP_SUBUNIT_ID; 870 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 871 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 872 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 873 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 874 connection->cmd_operands_length = 7; 875 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 876 return ERROR_CODE_SUCCESS; 877 } 878 879 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 880 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 881 if (!connection){ 882 log_error("avrcp_get_play_status: could not find a connection."); 883 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 884 } 885 avrcp_register_notification(connection, event_id); 886 return ERROR_CODE_SUCCESS; 887 } 888 889 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_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_play_status: could not find a connection."); 893 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 894 } 895 connection->notifications_to_deregister |= (1 << event_id); 896 return ERROR_CODE_SUCCESS; 897 } 898 899 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 900 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 901 if (!connection){ 902 log_error("avrcp_get_capabilities: could not find a connection."); 903 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 904 } 905 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 906 connection->state = AVCTP_W2_SEND_COMMAND; 907 908 connection->transaction_label++; 909 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 910 connection->command_type = AVRCP_CTYPE_STATUS; 911 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 912 connection->subunit_id = AVRCP_SUBUNIT_ID; 913 int pos = 0; 914 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 915 pos += 3; 916 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 917 connection->cmd_operands[pos++] = 0; 918 919 // Parameter Length 920 big_endian_store_16(connection->cmd_operands, pos, 9); 921 pos += 2; 922 923 // write 8 bytes value 924 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 925 pos += 8; 926 927 connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 928 // every attribute is 4 bytes long 929 930 connection->cmd_operands_length = pos; 931 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 932 return ERROR_CODE_SUCCESS; 933 } 934 935 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 936 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 937 if (!connection){ 938 log_error("avrcp_get_capabilities: could not find a connection."); 939 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 940 } 941 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 942 connection->state = AVCTP_W2_SEND_COMMAND; 943 944 connection->transaction_label++; 945 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 946 connection->command_type = AVRCP_CTYPE_CONTROL; 947 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 948 connection->subunit_id = AVRCP_SUBUNIT_ID; 949 int pos = 0; 950 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 951 pos += 3; 952 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 953 connection->cmd_operands[pos++] = 0; 954 955 // Parameter Length 956 big_endian_store_16(connection->cmd_operands, pos, 1); 957 pos += 2; 958 connection->cmd_operands[pos++] = volume; 959 960 connection->cmd_operands_length = pos; 961 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 962 return ERROR_CODE_SUCCESS; 963 } 964 965 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 966 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 967 if (!connection){ 968 log_error("avrcp_get_capabilities: could not find a connection."); 969 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 970 } 971 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 972 connection->state = AVCTP_W2_SEND_COMMAND; 973 974 connection->transaction_label++; 975 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 976 connection->command_type = AVRCP_CTYPE_STATUS; 977 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 978 connection->subunit_id = AVRCP_SUBUNIT_ID; 979 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 980 connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID 981 connection->cmd_operands[4] = 0; 982 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 983 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 984 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 985 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 986 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 987 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 988 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 989 connection->cmd_operands_length = 12; 990 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 991 return ERROR_CODE_SUCCESS; 992 } 993 994 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 995 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 996 if (!connection){ 997 log_error("avrcp_get_capabilities: could not find a connection."); 998 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 999 } 1000 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1001 connection->state = AVCTP_W2_SEND_COMMAND; 1002 1003 connection->transaction_label++; 1004 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1005 connection->command_type = AVRCP_CTYPE_CONTROL; 1006 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1007 connection->subunit_id = AVRCP_SUBUNIT_ID; 1008 int pos = 0; 1009 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1010 pos += 3; 1011 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID 1012 connection->cmd_operands[pos++] = 0; 1013 // Parameter Length 1014 big_endian_store_16(connection->cmd_operands, pos, 3); 1015 pos += 2; 1016 connection->cmd_operands[pos++] = 2; 1017 connection->cmd_operands_length = pos; 1018 connection->cmd_operands[pos++] = attr_id; 1019 connection->cmd_operands[pos++] = attr_value; 1020 connection->cmd_operands_length = pos; 1021 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1022 return ERROR_CODE_SUCCESS; 1023 } 1024 1025 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1026 if (mode < AVRCP_SHUFFLE_MODE_OFF || mode > AVRCP_SHUFFLE_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1027 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1028 } 1029 1030 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1031 if (mode < AVRCP_REPEAT_MODE_OFF || mode > AVRCP_REPEAT_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1032 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1033 } 1034 1035 uint8_t avrcp_controller_disconnect(uint16_t avrcp_cid){ 1036 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1037 if (!connection){ 1038 log_error("avrcp_get_capabilities: could not find a connection."); 1039 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1040 } 1041 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1042 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1043 return ERROR_CODE_SUCCESS; 1044 }