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 <string.h> 42 #include <inttypes.h> 43 44 #include "classic/avrcp.h" 45 #include "classic/avrcp_controller.h" 46 47 #include "bluetooth_sdp.h" 48 #include "btstack_debug.h" 49 #include "btstack_event.h" 50 #include "btstack_util.h" 51 #include "l2cap.h" 52 53 #define AVRCP_CMD_BUFFER_SIZE 30 54 55 // made public in avrcp_controller.h 56 avrcp_context_t avrcp_controller_context; 57 58 static uint8_t avrcp_controller_calc_next_transaction_label(uint8_t current_transaction_label){ 59 current_transaction_label++; 60 if (current_transaction_label == 16){ 61 current_transaction_label = 1; 62 } 63 return current_transaction_label; 64 } 65 66 static uint8_t avrcp_controller_get_next_transaction_label(avrcp_connection_t * connection){ 67 connection->transaction_id_counter = avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter); 68 return connection->transaction_id_counter; 69 } 70 71 static bool avrcp_controller_is_transaction_id_valid(avrcp_connection_t * connection, uint8_t transaction_id){ 72 uint8_t delta = ((int8_t) transaction_id - connection->last_confirmed_transaction_id) & 0x0f; 73 return delta < 15; 74 } 75 76 static uint16_t avrcp_get_max_payload_size_for_packet_type(avrcp_packet_type_t packet_type){ 77 switch (packet_type){ 78 case AVRCP_SINGLE_PACKET: 79 return AVRCP_CMD_BUFFER_SIZE - 3; 80 case AVRCP_START_PACKET: 81 return AVRCP_CMD_BUFFER_SIZE - 4; 82 case AVRCP_CONTINUE_PACKET: 83 case AVRCP_END_PACKET: 84 return AVRCP_CMD_BUFFER_SIZE - 1; 85 default: 86 btstack_assert(false); 87 return 0; 88 } 89 } 90 91 static int avrcp_controller_supports_browsing(uint16_t controller_supported_features){ 92 return controller_supported_features & AVRCP_FEATURE_MASK_BROWSING; 93 } 94 95 static void avrcp_controller_emit_notification_complete(avrcp_connection_t * connection, uint8_t status, uint8_t event_id, bool enabled){ 96 uint8_t event[8]; 97 uint8_t pos = 0; 98 event[pos++] = HCI_EVENT_AVRCP_META; 99 event[pos++] = sizeof(event) - 2; 100 event[pos++] = AVRCP_SUBEVENT_NOTIFICATION_STATE; 101 little_endian_store_16(event, pos, connection->avrcp_cid); 102 pos += 2; 103 event[pos++] = status; 104 event[pos++] = enabled ? 1 : 0; 105 event[pos++] = event_id; 106 UNUSED(pos); 107 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 108 } 109 110 static void avrcp_controller_emit_supported_events(avrcp_connection_t * connection){ 111 uint8_t ctype = (uint8_t) AVRCP_CTYPE_RESPONSE_CHANGED_STABLE; 112 uint8_t event_id; 113 114 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 115 if ( (connection->remote_supported_notifications & (1<<event_id)) == 0){ 116 continue; 117 } 118 uint8_t event[8]; 119 uint8_t pos = 0; 120 event[pos++] = HCI_EVENT_AVRCP_META; 121 event[pos++] = sizeof(event) - 2; 122 event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID; 123 little_endian_store_16(event, pos, connection->avrcp_cid); 124 pos += 2; 125 event[pos++] = ctype; 126 event[pos++] = 0; 127 event[pos++] = event_id; 128 UNUSED(pos); 129 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 130 } 131 132 uint8_t event[7]; 133 uint8_t pos = 0; 134 event[pos++] = HCI_EVENT_AVRCP_META; 135 event[pos++] = sizeof(event) - 2; 136 event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE; 137 little_endian_store_16(event, pos, connection->avrcp_cid); 138 pos += 2; 139 event[pos++] = ctype; 140 event[pos++] = 0; 141 UNUSED(pos); 142 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 143 } 144 145 static void avrcp_controller_emit_notification_for_event_id(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id, 146 avrcp_command_type_t ctype, const uint8_t *payload, 147 uint16_t size) { 148 switch (event_id){ 149 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{ 150 if (size < 4) break; 151 uint32_t song_position = big_endian_read_32(payload, 0); 152 uint16_t offset = 0; 153 uint8_t event[10]; 154 event[offset++] = HCI_EVENT_AVRCP_META; 155 event[offset++] = sizeof(event) - 2; 156 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED; 157 little_endian_store_16(event, offset, avrcp_cid); 158 offset += 2; 159 event[offset++] = ctype; 160 little_endian_store_32(event, offset, song_position); 161 offset += 4; 162 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 163 break; 164 } 165 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 166 if (size < 1) break; 167 uint16_t offset = 0; 168 uint8_t event[7]; 169 event[offset++] = HCI_EVENT_AVRCP_META; 170 event[offset++] = sizeof(event) - 2; 171 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 172 little_endian_store_16(event, offset, avrcp_cid); 173 offset += 2; 174 event[offset++] = ctype; 175 event[offset++] = payload[0]; 176 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 177 break; 178 } 179 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 180 uint16_t offset = 0; 181 uint8_t event[6]; 182 event[offset++] = HCI_EVENT_AVRCP_META; 183 event[offset++] = sizeof(event) - 2; 184 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 185 little_endian_store_16(event, offset, avrcp_cid); 186 offset += 2; 187 event[offset++] = ctype; 188 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 189 break; 190 } 191 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 192 uint16_t offset = 0; 193 uint8_t event[6]; 194 event[offset++] = HCI_EVENT_AVRCP_META; 195 event[offset++] = sizeof(event) - 2; 196 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 197 little_endian_store_16(event, offset, avrcp_cid); 198 offset += 2; 199 event[offset++] = ctype; 200 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 201 break; 202 } 203 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 204 uint16_t offset = 0; 205 uint8_t event[6]; 206 event[offset++] = HCI_EVENT_AVRCP_META; 207 event[offset++] = sizeof(event) - 2; 208 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 209 little_endian_store_16(event, offset, avrcp_cid); 210 offset += 2; 211 event[offset++] = ctype; 212 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 213 break; 214 } 215 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 216 if (size < 1) break; 217 uint16_t offset = 0; 218 uint8_t event[7]; 219 event[offset++] = HCI_EVENT_AVRCP_META; 220 event[offset++] = sizeof(event) - 2; 221 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 222 little_endian_store_16(event, offset, avrcp_cid); 223 offset += 2; 224 event[offset++] = ctype; 225 event[offset++] = payload[0] & 0x7F; 226 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 227 break; 228 } 229 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 230 if (size < 2) break; 231 uint8_t event[8]; 232 uint16_t offset = 0; 233 uint16_t uuid = big_endian_read_16(payload, 0); 234 event[offset++] = HCI_EVENT_AVRCP_META; 235 event[offset++] = sizeof(event) - 2; 236 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED; 237 little_endian_store_16(event, offset, avrcp_cid); 238 offset += 2; 239 event[offset++] = ctype; 240 little_endian_store_16(event, offset, uuid); 241 offset += 2; 242 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 243 break; 244 } 245 246 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{ 247 uint16_t offset = 0; 248 uint8_t event[6]; 249 event[offset++] = HCI_EVENT_AVRCP_META; 250 event[offset++] = sizeof(event) - 2; 251 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END; 252 little_endian_store_16(event, offset, avrcp_cid); 253 offset += 2; 254 event[offset++] = ctype; 255 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 256 break; 257 } 258 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{ 259 uint16_t offset = 0; 260 uint8_t event[6]; 261 event[offset++] = HCI_EVENT_AVRCP_META; 262 event[offset++] = sizeof(event) - 2; 263 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START; 264 little_endian_store_16(event, offset, avrcp_cid); 265 offset += 2; 266 event[offset++] = ctype; 267 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 268 break; 269 } 270 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{ 271 if (size < 1) break; 272 uint16_t offset = 0; 273 uint8_t event[7]; 274 event[offset++] = HCI_EVENT_AVRCP_META; 275 event[offset++] = sizeof(event) - 2; 276 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 277 little_endian_store_16(event, offset, avrcp_cid); 278 offset += 2; 279 event[offset++] = ctype; 280 event[offset++] = payload[0]; 281 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 282 break; 283 } 284 285 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{ 286 if (size < 1) break; 287 uint16_t offset = 0; 288 uint8_t event[7]; 289 event[offset++] = HCI_EVENT_AVRCP_META; 290 event[offset++] = sizeof(event) - 2; 291 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED; 292 little_endian_store_16(event, offset, avrcp_cid); 293 offset += 2; 294 event[offset++] = ctype; 295 event[offset++] = payload[0]; 296 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 297 break; 298 } 299 300 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 301 default: 302 log_info("avrcp: not implemented"); 303 break; 304 } 305 } 306 307 static void avrcp_controller_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){ 308 btstack_assert(callback != NULL); 309 310 uint8_t event[8]; 311 int pos = 0; 312 event[pos++] = HCI_EVENT_AVRCP_META; 313 event[pos++] = sizeof(event) - 2; 314 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 315 little_endian_store_16(event, pos, avrcp_cid); 316 pos += 2; 317 event[pos++] = ctype; 318 event[pos++] = repeat_mode; 319 event[pos++] = shuffle_mode; 320 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 321 } 322 323 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){ 324 uint8_t event[7]; 325 int pos = 0; 326 event[pos++] = HCI_EVENT_AVRCP_META; 327 event[pos++] = sizeof(event) - 2; 328 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE; 329 little_endian_store_16(event, pos, avrcp_cid); 330 pos += 2; 331 event[pos++] = ctype; 332 event[pos++] = status; 333 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 334 } 335 336 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){ 337 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 338 int pos = 0; 339 event[pos++] = HCI_EVENT_AVRCP_META; 340 // reserve one byte for subevent type and data len 341 int data_len_pos = pos; 342 pos++; 343 int subevent_type_pos = pos; 344 pos++; 345 little_endian_store_16(event, pos, avrcp_cid); 346 pos += 2; 347 event[pos++] = ctype; 348 349 switch (attr_id){ 350 case AVRCP_MEDIA_ATTR_TITLE: 351 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO; 352 event[pos++] = value_len; 353 (void)memcpy(event + pos, value, value_len); 354 break; 355 case AVRCP_MEDIA_ATTR_ARTIST: 356 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO; 357 event[pos++] = value_len; 358 (void)memcpy(event + pos, value, value_len); 359 break; 360 case AVRCP_MEDIA_ATTR_ALBUM: 361 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO; 362 event[pos++] = value_len; 363 (void)memcpy(event + pos, value, value_len); 364 break; 365 case AVRCP_MEDIA_ATTR_GENRE: 366 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO; 367 event[pos++] = value_len; 368 (void)memcpy(event + pos, value, value_len); 369 break; 370 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 371 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO; 372 if (value){ 373 little_endian_store_32(event, pos, btstack_atoi((char *)value)); 374 } else { 375 little_endian_store_32(event, pos, 0); 376 } 377 pos += 4; 378 break; 379 case AVRCP_MEDIA_ATTR_TRACK: 380 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO; 381 if (value){ 382 event[pos++] = btstack_atoi((char *)value); 383 } else { 384 event[pos++] = 0; 385 } 386 break; 387 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 388 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO; 389 if (value){ 390 event[pos++] = btstack_atoi((char *)value); 391 } else { 392 event[pos++] = 0; 393 } 394 break; 395 default: 396 break; 397 } 398 event[data_len_pos] = pos - 2; 399 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 400 } 401 402 static void avrcp_controller_emit_operation_status(btstack_packet_handler_t callback, uint8_t subevent, uint16_t avrcp_cid, uint8_t ctype, uint8_t operation_id){ 403 btstack_assert(callback != NULL); 404 405 uint8_t event[7]; 406 int pos = 0; 407 event[pos++] = HCI_EVENT_AVRCP_META; 408 event[pos++] = sizeof(event) - 2; 409 event[pos++] = subevent; 410 little_endian_store_16(event, pos, avrcp_cid); 411 pos += 2; 412 event[pos++] = ctype; 413 event[pos++] = operation_id; 414 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 415 } 416 417 static void avrcp_parser_reset(avrcp_connection_t * connection){ 418 connection->list_offset = 0; 419 connection->num_attributes = 0; 420 connection->num_parsed_attributes = 0; 421 connection->parser_attribute_header_pos = 0; 422 connection->num_received_fragments = 0; 423 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 424 } 425 426 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 427 uint16_t attribute_total_value_len; 428 uint32_t attribute_id; 429 switch(connection->parser_state){ 430 case AVRCP_PARSER_GET_ATTRIBUTE_HEADER: 431 connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte; 432 connection->list_offset++; 433 434 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return; 435 436 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 437 connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE); 438 if (connection->attribute_value_len > 0){ 439 // get ready for attribute value 440 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE; 441 return; 442 } 443 444 // emit empty attribute 445 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 446 avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, (avrcp_media_attribute_id_t) attribute_id, connection->attribute_value, connection->attribute_value_len); 447 448 // done, see below 449 break; 450 451 case AVRCP_PARSER_GET_ATTRIBUTE_VALUE: 452 connection->attribute_value[connection->attribute_value_offset++] = byte; 453 connection->list_offset++; 454 455 if (connection->attribute_value_offset < connection->attribute_value_len) return; 456 457 // emit (potentially partial) attribute 458 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 459 avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, (avrcp_media_attribute_id_t) attribute_id, connection->attribute_value, connection->attribute_value_len); 460 461 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 462 if (connection->attribute_value_offset < attribute_total_value_len){ 463 // ignore rest of attribute 464 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE; 465 return; 466 } 467 468 // done, see below 469 break; 470 471 case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE: 472 connection->attribute_value_offset++; 473 connection->list_offset++; 474 475 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 476 if (connection->attribute_value_offset < attribute_total_value_len) return; 477 478 // done, see below 479 break; 480 481 default: 482 return; 483 } 484 485 // attribute fully read, check if more to come 486 if (connection->list_offset < connection->list_size){ 487 // more to come, reset parser 488 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 489 connection->parser_attribute_header_pos = 0; 490 connection->attribute_value_offset = 0; 491 } else { 492 // fully done 493 avrcp_parser_reset(connection); 494 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 495 } 496 } 497 498 static void avrcp_controller_parse_and_emit_element_attrs(uint8_t * packet, uint16_t num_bytes_to_read, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 499 int i; 500 for (i=0;i<num_bytes_to_read;i++){ 501 avrcp_parser_process_byte(packet[i], connection, ctype); 502 } 503 } 504 505 506 static int avrcp_send_cmd(avrcp_connection_t * connection, avrcp_packet_type_t packet_type){ 507 uint8_t command[AVRCP_CMD_BUFFER_SIZE]; 508 uint16_t pos = 0; 509 510 // non-fragmented: transport header (1) + PID (2) 511 // fragmented: transport header (1) + num packets (1) + PID (2) 512 513 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 514 command[pos++] = (connection->transaction_id << 4) | (packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 515 516 if (packet_type == AVRCP_START_PACKET){ 517 // num packets: (3 bytes overhead (PID, num packets) + command) / (MTU - transport header). 518 // to get number of packets using integer division, we subtract 1 from the data e.g. len = 5, packet size 5 => need 1 packet 519 command[pos++] = ((connection->cmd_operands_fragmented_len + 3 - 1) / (AVRCP_CMD_BUFFER_SIZE - 1)) + 1; 520 } 521 522 if ((packet_type == AVRCP_SINGLE_PACKET) || (packet_type == AVRCP_START_PACKET)){ 523 // Profile IDentifier (PID) 524 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 525 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 526 527 // command_type 528 command[pos++] = connection->command_type; 529 // subunit_type | subunit ID 530 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 531 // opcode 532 command[pos++] = (uint8_t)connection->command_opcode; 533 } 534 535 if (packet_type == AVRCP_SINGLE_PACKET){ 536 // operands 537 (void)memcpy(command + pos, connection->cmd_operands, 538 connection->cmd_operands_length); 539 pos += connection->cmd_operands_length; 540 } else { 541 uint16_t bytes_free = AVRCP_CMD_BUFFER_SIZE - pos; 542 uint16_t bytes_to_store = connection->cmd_operands_fragmented_len-connection->cmd_operands_fragmented_pos; 543 uint16_t bytes_to_copy = btstack_min(bytes_to_store, bytes_free); 544 (void)memcpy(command + pos, 545 &connection->cmd_operands_fragmented_buffer[connection->cmd_operands_fragmented_pos], 546 bytes_to_copy); 547 pos += bytes_to_copy; 548 connection->cmd_operands_fragmented_pos += bytes_to_copy; 549 } 550 551 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 552 } 553 554 static int avrcp_send_register_notification(avrcp_connection_t * connection, uint8_t event_id){ 555 uint8_t command[18]; 556 uint16_t pos = 0; 557 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 558 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 559 command[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 560 561 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 562 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 563 command[pos++] = AVRCP_CTYPE_NOTIFY; 564 command[pos++] = (AVRCP_SUBUNIT_TYPE_PANEL << 3) | AVRCP_SUBUNIT_ID; 565 command[pos++] = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 566 567 big_endian_store_24(command, pos, BT_SIG_COMPANY_ID); 568 pos += 3; 569 command[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 570 command[pos++] = 0; // reserved(upper 6) | packet_type -> 0 571 big_endian_store_16(command, pos, 5); // parameter length 572 pos += 2; 573 command[pos++] = event_id; 574 big_endian_store_32(command, pos, 1); // send notification on playback position every second, for other notifications it is ignored 575 pos += 4; 576 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 577 } 578 579 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 580 UNUSED(timer); 581 avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer); 582 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 583 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 584 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 585 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 586 } 587 588 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 589 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 590 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 591 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 592 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 593 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 594 } 595 596 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 597 connection->press_and_hold_cmd_active = false; 598 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 599 } 600 601 602 static uint8_t avrcp_controller_request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 603 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 604 if (connection->press_and_hold_cmd_active){ 605 avrcp_press_and_hold_timer_stop(connection); 606 } 607 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 608 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 609 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 610 return ERROR_CODE_SUCCESS; 611 } 612 613 static uint8_t avrcp_controller_request_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed, bool continuous_cmd){ 614 log_info("Send command %d", opid); 615 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 616 if (!connection){ 617 log_error("Could not find a connection. avrcp cid 0x%02x", avrcp_cid); 618 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 619 } 620 621 if (connection->state != AVCTP_CONNECTION_OPENED){ 622 log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid); 623 return ERROR_CODE_COMMAND_DISALLOWED; 624 } 625 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 626 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 627 connection->command_type = AVRCP_CTYPE_CONTROL; 628 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 629 connection->subunit_id = AVRCP_SUBUNIT_ID; 630 connection->cmd_operands_length = 0; 631 632 connection->press_and_hold_cmd_active = continuous_cmd; 633 connection->cmd_operands_length = 2; 634 connection->cmd_operands[0] = opid; 635 if (playback_speed > 0){ 636 connection->cmd_operands[2] = playback_speed; 637 connection->cmd_operands_length++; 638 } 639 connection->cmd_operands[1] = connection->cmd_operands_length - 2; 640 641 if (connection->press_and_hold_cmd_active){ 642 avrcp_press_and_hold_timer_start(connection); 643 } 644 645 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 646 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 647 return ERROR_CODE_SUCCESS; 648 } 649 650 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 651 return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, false); 652 } 653 654 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 655 return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, true); 656 } 657 658 static void avrcp_controller_get_capabilities_for_connection(avrcp_connection_t * connection, uint8_t capability_id){ 659 connection->state = AVCTP_W2_SEND_COMMAND; 660 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 661 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 662 connection->command_type = AVRCP_CTYPE_STATUS; 663 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 664 connection->subunit_id = AVRCP_SUBUNIT_ID; 665 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 666 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 667 connection->cmd_operands[4] = 0; 668 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 669 connection->cmd_operands[7] = capability_id; // capability ID 670 connection->cmd_operands_length = 8; 671 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 672 } 673 674 static uint8_t avrcp_controller_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 675 if (connection->remote_supported_notifications_queried && (connection->remote_supported_notifications & (1 << event_id)) == 0){ 676 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 677 } 678 if ( (connection->notifications_to_deregister & (1 << event_id)) != 0){ 679 return ERROR_CODE_COMMAND_DISALLOWED; 680 } 681 if ( (connection->notifications_enabled & (1 << event_id)) != 0){ 682 return ERROR_CODE_SUCCESS; 683 } 684 connection->notifications_to_register |= (1 << event_id); 685 686 if (!connection->remote_supported_notifications_queried){ 687 connection->remote_supported_notifications_suppress_emit_result = true; 688 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 689 return ERROR_CODE_SUCCESS; 690 } 691 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 692 return ERROR_CODE_SUCCESS; 693 } 694 695 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 696 connection->state = AVCTP_W2_SEND_COMMAND; 697 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 698 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 699 connection->command_type = AVRCP_CTYPE_CONTROL; 700 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 701 connection->subunit_id = AVRCP_SUBUNIT_ID; 702 int pos = 0; 703 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 704 pos += 3; 705 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID 706 connection->cmd_operands[pos++] = 0; 707 // Parameter Length 708 connection->cmd_operands_length = 8; 709 big_endian_store_16(connection->cmd_operands, pos, 1); 710 pos += 2; 711 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 712 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 713 return ERROR_CODE_SUCCESS; 714 } 715 716 717 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 718 connection->state = AVCTP_W2_SEND_COMMAND; 719 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 720 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 721 connection->command_type = AVRCP_CTYPE_CONTROL; 722 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 723 connection->subunit_id = AVRCP_SUBUNIT_ID; 724 int pos = 0; 725 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 726 pos += 3; 727 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID 728 connection->cmd_operands[pos++] = 0; 729 // Parameter Length 730 connection->cmd_operands_length = 8; 731 big_endian_store_16(connection->cmd_operands, pos, 1); 732 pos += 2; 733 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 734 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 735 return ERROR_CODE_SUCCESS; 736 } 737 738 static void avrcp_controller_handle_notification(avrcp_connection_t *connection, avrcp_command_type_t ctype, uint8_t *payload, uint16_t size) { 739 if (size < 1) return; 740 uint16_t pos = 0; 741 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) payload[pos++]; 742 if ( (event_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 743 return; 744 } 745 746 uint16_t event_mask = (1 << event_id); 747 uint16_t reset_event_mask = ~event_mask; 748 749 switch (ctype){ 750 case AVRCP_CTYPE_RESPONSE_INTERIM: 751 // register as enabled 752 connection->notifications_enabled |= event_mask; 753 754 // check if initial value is already sent 755 if ( (connection->initial_status_reported & event_mask) != 0 ){ 756 return; 757 } 758 // emit event only once, initially 759 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, true); 760 connection->initial_status_reported |= event_mask; 761 break; 762 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 763 // received change, event is considered de-registered 764 // we are re-enabling it automatically, if it is not 765 // explicitly disabled 766 connection->notifications_enabled &= reset_event_mask; 767 if ((connection->notifications_to_deregister & event_mask) == 0){ 768 avrcp_controller_register_notification(connection, event_id); 769 } else { 770 connection->notifications_to_deregister &= reset_event_mask; 771 connection->notifications_to_register &= reset_event_mask; 772 connection->initial_status_reported &= reset_event_mask; 773 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, false); 774 } 775 break; 776 default: 777 return; 778 } 779 780 avrcp_controller_emit_notification_for_event_id(connection->avrcp_cid, event_id, ctype, payload + pos, size - pos); 781 } 782 783 #ifdef ENABLE_AVCTP_FRAGMENTATION 784 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 785 // after header (transaction label and packet type) 786 uint16_t pos; 787 uint16_t bytes_to_store; 788 789 switch (packet_type){ 790 case AVCTP_START_PACKET: 791 if (size < 2) return; 792 793 // store header 794 pos = 0; 795 connection->avctp_reassembly_buffer[pos] = packet[pos]; 796 pos++; 797 connection->avctp_reassembly_size = pos; 798 799 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 800 pos++; 801 802 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 803 804 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 805 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 806 connection->avctp_reassembly_size += bytes_to_store; 807 break; 808 809 case AVCTP_CONTINUE_PACKET: 810 case AVCTP_END_PACKET: 811 if (size < 1) return; 812 813 // store remaining data, ignore header 814 pos = 1; 815 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 816 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 817 connection->avctp_reassembly_size += bytes_to_store; 818 break; 819 820 default: 821 return; 822 } 823 } 824 #endif 825 826 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 827 if (size < 6u) return; 828 uint8_t pdu_id; 829 avrcp_packet_type_t vendor_dependent_packet_type; 830 831 uint16_t pos = 0; 832 connection->last_confirmed_transaction_id = packet[pos] >> 4; 833 avrcp_frame_type_t frame_type = (avrcp_frame_type_t)((packet[pos] >> 1) & 0x01); 834 avctp_packet_type_t packet_type = (avctp_packet_type_t)((packet[pos] >> 2) & 0x03); 835 pos++; 836 837 if (frame_type != AVRCP_RESPONSE_FRAME) return; 838 839 switch (packet_type){ 840 case AVCTP_SINGLE_PACKET: 841 break; 842 843 #ifdef ENABLE_AVCTP_FRAGMENTATION 844 case AVCTP_START_PACKET: 845 case AVCTP_CONTINUE_PACKET: 846 avctp_reassemble_message(connection, packet_type, packet, size); 847 return; 848 849 case AVCTP_END_PACKET: 850 avctp_reassemble_message(connection, packet_type, packet, size); 851 852 packet = connection->avctp_reassembly_buffer; 853 size = connection->avctp_reassembly_size; 854 break; 855 #endif 856 857 default: 858 return; 859 } 860 861 pos += 2; // PID 862 863 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 864 865 #ifdef ENABLE_LOG_INFO 866 uint8_t byte_value = packet[pos]; 867 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 868 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 869 #endif 870 pos++; 871 872 uint8_t opcode = packet[pos++]; 873 uint16_t param_length; 874 875 switch (opcode){ 876 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 877 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 878 connection->state = AVCTP_CONNECTION_OPENED; 879 880 #ifdef ENABLE_LOG_INFO 881 // page, extension code (1) 882 pos++; 883 uint8_t unit_type = packet[pos] >> 3; 884 uint8_t max_subunit_ID = packet[pos] & 0x07; 885 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); 886 #endif 887 break; 888 } 889 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 890 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 891 connection->state = AVCTP_CONNECTION_OPENED; 892 893 #ifdef ENABLE_LOG_INFO 894 // byte value 7 (1) 895 pos++; 896 uint8_t unit_type = packet[pos] >> 3; 897 uint8_t unit = packet[pos] & 0x07; 898 pos++; 899 uint32_t company_id = big_endian_read_24(packet, pos); 900 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, 901 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 902 #endif 903 break; 904 } 905 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 906 907 if ((size - pos) < 7) return; 908 909 // Company ID (3) 910 pos += 3; 911 pdu_id = packet[pos++]; 912 vendor_dependent_packet_type = (avrcp_packet_type_t)(packet[pos++] & 0x03); 913 param_length = big_endian_read_16(packet, pos); 914 pos += 2; 915 916 if ((size - pos) < param_length) return; 917 918 // handle asynchronous notifications, without changing state 919 if (pdu_id == AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 920 avrcp_controller_handle_notification(connection, ctype, packet + pos, size - pos); 921 break; 922 } 923 924 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE){ 925 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 926 return; 927 } 928 connection->state = AVCTP_CONNECTION_OPENED; 929 930 log_info("VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 931 switch (pdu_id){ 932 case AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:{ 933 uint8_t num_attributes = packet[pos++]; 934 int i; 935 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 936 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 937 for (i = 0; i < num_attributes; i++){ 938 uint8_t attribute_id = packet[pos++]; 939 uint8_t value = packet[pos++]; 940 switch (attribute_id){ 941 case 0x02: 942 repeat_mode = (avrcp_repeat_mode_t) value; 943 break; 944 case 0x03: 945 shuffle_mode = (avrcp_shuffle_mode_t) value; 946 break; 947 default: 948 break; 949 } 950 } 951 avrcp_controller_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 952 break; 953 } 954 955 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{ 956 uint16_t offset = 0; 957 uint8_t event[6]; 958 event[offset++] = HCI_EVENT_AVRCP_META; 959 event[offset++] = sizeof(event) - 2; 960 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 961 little_endian_store_16(event, offset, connection->avrcp_cid); 962 offset += 2; 963 event[offset++] = ctype; 964 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 965 break; 966 } 967 968 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 969 uint16_t offset = 0; 970 uint8_t event[7]; 971 event[offset++] = HCI_EVENT_AVRCP_META; 972 event[offset++] = sizeof(event) - 2; 973 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 974 little_endian_store_16(event, offset, connection->avrcp_cid); 975 offset += 2; 976 event[offset++] = ctype; 977 event[offset++] = packet[pos++]; 978 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 979 break; 980 } 981 982 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 983 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 984 uint8_t capability_count = 0; 985 if (param_length > 1){ 986 capability_count = packet[pos++]; 987 } 988 uint16_t i; 989 uint16_t offset = 0; 990 uint8_t event[10]; 991 992 switch (capability_id){ 993 994 case AVRCP_CAPABILITY_ID_COMPANY: 995 for (i = 0; (i < capability_count) && ((size - pos) >= 3); i++){ 996 uint32_t company_id = big_endian_read_24(packet, pos); 997 pos += 3; 998 log_info(" 0x%06" PRIx32 ", ", company_id); 999 1000 offset = 0; 1001 event[offset++] = HCI_EVENT_AVRCP_META; 1002 event[offset++] = sizeof(event) - 2; 1003 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID; 1004 little_endian_store_16(event, offset, connection->avrcp_cid); 1005 offset += 2; 1006 event[offset++] = ctype; 1007 event[offset++] = 0; 1008 little_endian_store_24(event, offset, company_id); 1009 offset += 3; 1010 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 1011 } 1012 1013 offset = 0; 1014 event[offset++] = HCI_EVENT_AVRCP_META; 1015 event[offset++] = sizeof(event) - 2; 1016 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE; 1017 little_endian_store_16(event, offset, connection->avrcp_cid); 1018 offset += 2; 1019 event[offset++] = ctype; 1020 event[offset++] = 0; 1021 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 1022 break; 1023 1024 case AVRCP_CAPABILITY_ID_EVENT: 1025 for (i = 0; (i < capability_count) && ((size - pos) >= 1); i++){ 1026 uint8_t event_id = packet[pos++]; 1027 connection->remote_supported_notifications |= (1 << event_id); 1028 } 1029 1030 // if the get supported events query is triggered by avrcp_controller_enable_notification call, 1031 // avrcp_controller_emit_supported_events should be suppressed 1032 if (connection->remote_supported_notifications_suppress_emit_result){ 1033 connection->remote_supported_notifications_suppress_emit_result = false; 1034 // also, notification might not be supported 1035 // if so, emit AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE event to app, 1036 // and update notifications_to_register bitmap 1037 for (i = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++){ 1038 if ((connection->notifications_to_register & (1<<i)) != 0){ 1039 if ((connection->remote_supported_notifications & (1<<i)) == 0){ 1040 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, i, false); 1041 connection->notifications_to_register &= ~(1 << i); 1042 } 1043 } 1044 } 1045 break; 1046 } 1047 // supported events are emitted only if the get supported events query 1048 // is triggered by avrcp_controller_get_supported_events call 1049 avrcp_controller_emit_supported_events(connection); 1050 break; 1051 1052 default: 1053 // ignore 1054 break; 1055 } 1056 break; 1057 } 1058 1059 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 1060 uint32_t song_length = big_endian_read_32(packet, pos); 1061 pos += 4; 1062 uint32_t song_position = big_endian_read_32(packet, pos); 1063 pos += 4; 1064 uint8_t play_status = packet[pos]; 1065 1066 uint8_t event[15]; 1067 int offset = 0; 1068 event[offset++] = HCI_EVENT_AVRCP_META; 1069 event[offset++] = sizeof(event) - 2; 1070 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 1071 little_endian_store_16(event, offset, connection->avrcp_cid); 1072 offset += 2; 1073 event[offset++] = ctype; 1074 little_endian_store_32(event, offset, song_length); 1075 offset += 4; 1076 little_endian_store_32(event, offset, song_position); 1077 offset += 4; 1078 event[offset++] = play_status; 1079 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1080 break; 1081 } 1082 1083 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 1084 switch (vendor_dependent_packet_type){ 1085 case AVRCP_START_PACKET: 1086 case AVRCP_SINGLE_PACKET: 1087 avrcp_parser_reset(connection); 1088 connection->list_size = param_length; 1089 connection->num_attributes = packet[pos++]; 1090 1091 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 1092 if (vendor_dependent_packet_type == AVRCP_START_PACKET){ 1093 avrcp_controller_request_continue_response(connection); 1094 } 1095 break; 1096 case AVRCP_CONTINUE_PACKET: 1097 case AVRCP_END_PACKET: 1098 connection->num_received_fragments++; 1099 1100 if (connection->num_received_fragments < connection->max_num_fragments){ 1101 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 1102 1103 if (vendor_dependent_packet_type == AVRCP_CONTINUE_PACKET){ 1104 avrcp_controller_request_continue_response(connection); 1105 } 1106 } else { 1107 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 1108 avrcp_parser_reset(connection); 1109 avrcp_controller_request_abort_continuation(connection); 1110 } 1111 break; 1112 default: 1113 // TODO check 1114 btstack_assert(false); 1115 break; 1116 } 1117 } 1118 default: 1119 break; 1120 } 1121 break; 1122 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 1123 if ((size - pos) < 1) return; 1124 uint8_t operation_id = packet[pos++]; 1125 switch (connection->state){ 1126 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1127 // trigger release for simple command: 1128 if (!connection->press_and_hold_cmd_active){ 1129 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1130 break; 1131 } 1132 // for press and hold, send release if it just has been requested, otherwise, wait for next repeat 1133 if (connection->press_and_hold_cmd_release){ 1134 connection->press_and_hold_cmd_release = false; 1135 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1136 } else { 1137 connection->state = AVCTP_W4_STOP; 1138 } 1139 break; 1140 case AVCTP_W2_RECEIVE_RESPONSE: 1141 connection->state = AVCTP_CONNECTION_OPENED; 1142 break; 1143 default: 1144 break; 1145 } 1146 if (connection->state == AVCTP_W4_STOP){ 1147 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 1148 } 1149 if (connection->state == AVCTP_CONNECTION_OPENED) { 1150 // RELEASE response 1151 operation_id = operation_id & 0x7F; 1152 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 1153 } 1154 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 1155 // PRESS response 1156 avrcp_controller_request_pass_through_release_control_cmd(connection); 1157 } 1158 break; 1159 } 1160 default: 1161 break; 1162 } 1163 1164 // trigger pending notification reqistrations 1165 if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->notifications_to_register){ 1166 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1167 } 1168 } 1169 1170 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 1171 switch (connection->state){ 1172 case AVCTP_W2_SEND_PRESS_COMMAND: 1173 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 1174 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 1175 return; 1176 case AVCTP_W2_SEND_COMMAND: 1177 case AVCTP_W2_SEND_RELEASE_COMMAND: 1178 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1179 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 1180 return; 1181 case AVCTP_W2_SEND_FRAGMENTED_COMMAND: 1182 if (connection->cmd_operands_fragmented_pos == 0){ 1183 avrcp_send_cmd(connection, AVRCP_START_PACKET); 1184 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1185 } else { 1186 if ((connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos) > avrcp_get_max_payload_size_for_packet_type(AVRCP_CONTINUE_PACKET)){ 1187 avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET); 1188 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1189 } else { 1190 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1191 avrcp_send_cmd(connection, AVRCP_END_PACKET); 1192 } 1193 } 1194 return; 1195 default: 1196 break; 1197 } 1198 // send register notification if queued 1199 if (connection->notifications_to_register != 0){ 1200 uint8_t event_id; 1201 for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 1202 if (connection->notifications_to_register & (1<<event_id)){ 1203 connection->notifications_to_register &= ~ (1 << event_id); 1204 avrcp_send_register_notification(connection, event_id); 1205 return; 1206 } 1207 } 1208 } 1209 } 1210 1211 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1212 avrcp_connection_t * connection; 1213 1214 switch (packet_type) { 1215 case L2CAP_DATA_PACKET: 1216 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1217 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1218 break; 1219 1220 case HCI_EVENT_PACKET: 1221 switch (hci_event_packet_get_type(packet)){ 1222 case L2CAP_EVENT_CAN_SEND_NOW: 1223 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1224 avrcp_controller_handle_can_send_now(connection); 1225 break; 1226 default: 1227 break; 1228 } 1229 default: 1230 break; 1231 } 1232 } 1233 1234 void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 1235 avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 1236 } 1237 1238 void avrcp_controller_init(void){ 1239 avrcp_controller_context.role = AVRCP_CONTROLLER; 1240 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 1241 avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler); 1242 } 1243 1244 void avrcp_controller_deinit(void){ 1245 memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t)); 1246 } 1247 1248 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 1249 btstack_assert(callback != NULL); 1250 avrcp_controller_context.avrcp_callback = callback; 1251 } 1252 1253 1254 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1255 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1256 } 1257 1258 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1259 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1260 } 1261 1262 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1263 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1264 } 1265 1266 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1267 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1268 } 1269 1270 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1271 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1272 } 1273 1274 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1275 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1276 } 1277 1278 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1279 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1280 } 1281 1282 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1283 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1284 } 1285 1286 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1287 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1288 } 1289 1290 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1291 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1292 } 1293 1294 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1295 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1296 } 1297 1298 /* start continuous cmds */ 1299 1300 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){ 1301 return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0); 1302 } 1303 1304 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1305 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1306 } 1307 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1308 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1309 } 1310 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1311 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1312 } 1313 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1314 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1315 } 1316 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1317 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1318 } 1319 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1320 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1321 } 1322 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1323 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1324 } 1325 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1326 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1327 } 1328 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1329 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1330 } 1331 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1332 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1333 } 1334 1335 /* stop continuous cmds */ 1336 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1337 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1338 if (!connection){ 1339 log_error("avrcp_stop_play: could not find a connection."); 1340 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1341 } 1342 1343 switch (connection->state){ 1344 // respond when we receive response for (repeated) press command 1345 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1346 connection->press_and_hold_cmd_release = true; 1347 break; 1348 1349 // release already sent or on the way, nothing to do 1350 case AVCTP_W2_RECEIVE_RESPONSE: 1351 case AVCTP_W2_SEND_RELEASE_COMMAND: 1352 break; 1353 1354 // about to send next repeated press command or wait for it -> release right away 1355 case AVCTP_W2_SEND_PRESS_COMMAND: 1356 case AVCTP_W4_STOP: 1357 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1358 1359 // otherwise reject request 1360 default: 1361 return ERROR_CODE_COMMAND_DISALLOWED; 1362 } 1363 return ERROR_CODE_SUCCESS; 1364 } 1365 1366 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1367 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1368 if (!connection){ 1369 log_error("avrcp_get_play_status: could not find a connection."); 1370 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1371 } 1372 return avrcp_controller_register_notification(connection, event_id); 1373 } 1374 1375 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1376 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1377 if (!connection){ 1378 log_error("avrcp_get_play_status: could not find a connection."); 1379 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1380 } 1381 if (!connection->remote_supported_notifications_queried){ 1382 return ERROR_CODE_COMMAND_DISALLOWED; 1383 } 1384 1385 if ((connection->remote_supported_notifications & (1 << event_id)) == 0){ 1386 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1387 } 1388 1389 if ((connection->notifications_enabled & (1 << event_id)) == 0){ 1390 return ERROR_CODE_SUCCESS; 1391 } 1392 1393 connection->notifications_to_deregister |= (1 << event_id); 1394 return ERROR_CODE_SUCCESS; 1395 } 1396 1397 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1398 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1399 if (!connection){ 1400 log_error("avrcp_unit_info: could not find a connection."); 1401 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1402 } 1403 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1404 connection->state = AVCTP_W2_SEND_COMMAND; 1405 1406 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1407 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 1408 connection->command_type = AVRCP_CTYPE_STATUS; 1409 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1410 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1411 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1412 connection->cmd_operands_length = 5; 1413 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1414 return ERROR_CODE_SUCCESS; 1415 } 1416 1417 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1418 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1419 if (!connection){ 1420 log_error("avrcp_unit_info: could not find a connection."); 1421 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1422 } 1423 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1424 connection->state = AVCTP_W2_SEND_COMMAND; 1425 1426 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1427 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 1428 connection->command_type = AVRCP_CTYPE_STATUS; 1429 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1430 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1431 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1432 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 1433 connection->cmd_operands_length = 5; 1434 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1435 return ERROR_CODE_SUCCESS; 1436 } 1437 1438 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1439 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1440 if (!connection){ 1441 log_error("avrcp_get_capabilities: could not find a connection."); 1442 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1443 } 1444 if (connection->state != AVCTP_CONNECTION_OPENED){ 1445 return ERROR_CODE_COMMAND_DISALLOWED; 1446 } 1447 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY); 1448 return ERROR_CODE_SUCCESS; 1449 } 1450 1451 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1452 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1453 if (!connection){ 1454 log_error("avrcp_get_capabilities: could not find a connection."); 1455 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1456 } 1457 if (connection->state != AVCTP_CONNECTION_OPENED){ 1458 return ERROR_CODE_COMMAND_DISALLOWED; 1459 } 1460 1461 if (!connection->remote_supported_notifications_queried){ 1462 connection->remote_supported_notifications_queried = true; 1463 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 1464 return ERROR_CODE_SUCCESS; 1465 } 1466 1467 avrcp_controller_emit_supported_events(connection); 1468 return ERROR_CODE_SUCCESS; 1469 } 1470 1471 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1472 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1473 if (!connection){ 1474 log_error("avrcp_get_play_status: could not find a connection."); 1475 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1476 } 1477 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1478 connection->state = AVCTP_W2_SEND_COMMAND; 1479 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1480 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1481 connection->command_type = AVRCP_CTYPE_STATUS; 1482 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1483 connection->subunit_id = AVRCP_SUBUNIT_ID; 1484 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1485 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1486 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1487 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1488 connection->cmd_operands_length = 7; 1489 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1490 return ERROR_CODE_SUCCESS; 1491 } 1492 1493 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1494 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1495 if (!connection){ 1496 log_error("avrcp_get_capabilities: could not find a connection."); 1497 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1498 } 1499 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1500 connection->state = AVCTP_W2_SEND_COMMAND; 1501 1502 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1503 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1504 connection->command_type = AVRCP_CTYPE_CONTROL; 1505 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1506 connection->subunit_id = AVRCP_SUBUNIT_ID; 1507 int pos = 0; 1508 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1509 pos += 3; 1510 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1511 connection->cmd_operands[pos++] = 0; 1512 1513 // Parameter Length 1514 big_endian_store_16(connection->cmd_operands, pos, 2); 1515 pos += 2; 1516 1517 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1518 pos += 2; 1519 1520 connection->cmd_operands_length = pos; 1521 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1522 return ERROR_CODE_SUCCESS; 1523 } 1524 1525 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){ 1526 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1527 if (!connection){ 1528 log_error("avrcp_get_capabilities: could not find a connection."); 1529 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1530 } 1531 1532 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1533 1534 if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) { 1535 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1536 } 1537 connection->state = AVCTP_W2_SEND_COMMAND; 1538 1539 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1540 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1541 connection->command_type = AVRCP_CTYPE_STATUS; 1542 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1543 connection->subunit_id = AVRCP_SUBUNIT_ID; 1544 int pos = 0; 1545 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1546 pos += 3; 1547 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1548 connection->cmd_operands[pos++] = 0; 1549 1550 // Parameter Length 1551 big_endian_store_16(connection->cmd_operands, pos, 9); 1552 pos += 2; 1553 1554 // write 8 bytes value 1555 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1556 pos += 8; 1557 1558 connection->cmd_operands[pos++] = num_attributes; // attribute count, if 0 get all attributes 1559 1560 int i; 1561 for (i = 0; i < num_attributes; i++){ 1562 // every attribute is 4 bytes long 1563 big_endian_store_32(connection->cmd_operands, pos, attributes[i]); 1564 pos += 4; 1565 } 1566 1567 connection->cmd_operands_length = pos; 1568 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1569 return ERROR_CODE_SUCCESS; 1570 } 1571 1572 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1573 return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL); 1574 } 1575 1576 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1577 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1578 if (!connection){ 1579 log_error("avrcp_get_capabilities: could not find a connection."); 1580 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1581 } 1582 1583 // 1584 // allow sending of multiple set abs volume commands without waiting for response 1585 // 1586 uint8_t status = ERROR_CODE_COMMAND_DISALLOWED; 1587 switch (connection->state){ 1588 case AVCTP_CONNECTION_OPENED: 1589 status = ERROR_CODE_SUCCESS; 1590 break; 1591 case AVCTP_W2_RECEIVE_RESPONSE: 1592 // - is pending response also set abs volume 1593 if (connection->command_opcode != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break; 1594 if (connection->command_type != AVRCP_CTYPE_CONTROL) break; 1595 if (connection->subunit_type != AVRCP_SUBUNIT_TYPE_PANEL) break; 1596 if (connection->subunit_id != AVRCP_SUBUNIT_ID) break; 1597 if (connection->cmd_operands[3] != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME) break; 1598 // - is next transaction id valid in window 1599 if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break; 1600 status = ERROR_CODE_SUCCESS; 1601 break; 1602 default: 1603 break; 1604 } 1605 if (status != ERROR_CODE_SUCCESS) return status; 1606 1607 connection->state = AVCTP_W2_SEND_COMMAND; 1608 1609 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1610 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1611 connection->command_type = AVRCP_CTYPE_CONTROL; 1612 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1613 connection->subunit_id = AVRCP_SUBUNIT_ID; 1614 int pos = 0; 1615 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1616 pos += 3; 1617 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1618 connection->cmd_operands[pos++] = 0; 1619 1620 // Parameter Length 1621 big_endian_store_16(connection->cmd_operands, pos, 1); 1622 pos += 2; 1623 connection->cmd_operands[pos++] = volume; 1624 1625 connection->cmd_operands_length = pos; 1626 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1627 return ERROR_CODE_SUCCESS; 1628 } 1629 1630 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1631 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1632 if (!connection){ 1633 log_error("avrcp_get_capabilities: could not find a connection."); 1634 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1635 } 1636 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1637 connection->state = AVCTP_W2_SEND_COMMAND; 1638 1639 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1640 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1641 connection->command_type = AVRCP_CTYPE_STATUS; 1642 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1643 connection->subunit_id = AVRCP_SUBUNIT_ID; 1644 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1645 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1646 connection->cmd_operands[4] = 0; 1647 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1648 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1649 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1650 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1651 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1652 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1653 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1654 connection->cmd_operands_length = 12; 1655 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1656 return ERROR_CODE_SUCCESS; 1657 } 1658 1659 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1660 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1661 if (!connection){ 1662 log_error("avrcp_get_capabilities: could not find a connection."); 1663 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1664 } 1665 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1666 connection->state = AVCTP_W2_SEND_COMMAND; 1667 1668 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1669 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1670 connection->command_type = AVRCP_CTYPE_CONTROL; 1671 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1672 connection->subunit_id = AVRCP_SUBUNIT_ID; 1673 int pos = 0; 1674 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1675 pos += 3; 1676 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1677 connection->cmd_operands[pos++] = 0; 1678 // Parameter Length 1679 big_endian_store_16(connection->cmd_operands, pos, 3); 1680 pos += 2; 1681 connection->cmd_operands[pos++] = 2; 1682 connection->cmd_operands_length = pos; 1683 connection->cmd_operands[pos++] = attr_id; 1684 connection->cmd_operands[pos++] = attr_value; 1685 connection->cmd_operands_length = pos; 1686 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1687 return ERROR_CODE_SUCCESS; 1688 } 1689 1690 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1691 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1692 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1693 } 1694 1695 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1696 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1697 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1698 } 1699 1700 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1701 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1702 if (!connection){ 1703 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1704 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1705 } 1706 if (connection->state != AVCTP_CONNECTION_OPENED){ 1707 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1708 return ERROR_CODE_COMMAND_DISALLOWED; 1709 } 1710 connection->state = AVCTP_W2_SEND_COMMAND; 1711 1712 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1713 connection->command_type = AVRCP_CTYPE_CONTROL; 1714 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1715 connection->subunit_id = AVRCP_SUBUNIT_ID; 1716 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1717 int pos = 0; 1718 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1719 pos += 3; 1720 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1721 // reserved 1722 connection->cmd_operands[pos++] = 0; 1723 // Parameter Length 1724 big_endian_store_16(connection->cmd_operands, pos, 11); 1725 pos += 2; 1726 connection->cmd_operands[pos++] = scope; 1727 memset(&connection->cmd_operands[pos], 0, 8); 1728 if (uid){ 1729 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1730 } 1731 pos += 8; 1732 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1733 pos += 2; 1734 connection->cmd_operands_length = pos; 1735 1736 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1737 return ERROR_CODE_SUCCESS; 1738 } 1739 1740 uint8_t avrcp_controller_add_item_from_scope_to_now_playing_list(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1741 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1742 if (!connection){ 1743 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1744 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1745 } 1746 if (connection->state != AVCTP_CONNECTION_OPENED){ 1747 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1748 return ERROR_CODE_COMMAND_DISALLOWED; 1749 } 1750 connection->state = AVCTP_W2_SEND_COMMAND; 1751 1752 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1753 connection->command_type = AVRCP_CTYPE_CONTROL; 1754 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1755 connection->subunit_id = AVRCP_SUBUNIT_ID; 1756 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1757 int pos = 0; 1758 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1759 pos += 3; 1760 connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID 1761 // reserved 1762 connection->cmd_operands[pos++] = 0; 1763 // Parameter Length 1764 big_endian_store_16(connection->cmd_operands, pos, 11); 1765 pos += 2; 1766 connection->cmd_operands[pos++] = scope; 1767 memset(&connection->cmd_operands[pos], 0, 8); 1768 if (uid){ 1769 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1770 } 1771 pos += 8; 1772 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1773 pos += 2; 1774 connection->cmd_operands_length = pos; 1775 1776 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1777 return ERROR_CODE_SUCCESS; 1778 } 1779 1780 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1781 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1782 if (!connection){ 1783 log_error("avrcp_controller_play_item: could not find a connection."); 1784 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1785 } 1786 connection->max_num_fragments = max_num_fragments; 1787 return ERROR_CODE_SUCCESS; 1788 } 1789 1790 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid, avrcp_command_type_t command_type, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t command_opcode, const uint8_t * command_buffer, uint16_t command_len){ 1791 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1792 if (!connection){ 1793 log_error("avrcp_controller_play_item: could not find a connection."); 1794 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1795 } 1796 1797 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1798 connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND; 1799 1800 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1801 connection->command_opcode = command_opcode; 1802 connection->command_type = command_type; 1803 connection->subunit_type = subunit_type; 1804 connection->subunit_id = subunit_id; 1805 connection->cmd_operands_fragmented_buffer = command_buffer; 1806 connection->cmd_operands_fragmented_pos = 0; 1807 connection->cmd_operands_fragmented_len = command_len; 1808 1809 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1810 return ERROR_CODE_SUCCESS; 1811 } 1812