| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189 |
- /*
- * Copyright (C) 2016 BlueKitchen GmbH
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holders nor the names of
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * 4. Any redistribution, use, or modification is done solely for
- * personal benefit and not for any commercial purpose or for
- * monetary gain.
- *
- * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
- * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
- * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * Please inquire about commercial licensing options at
- * contact@bluekitchen-gmbh.com
- *
- */
- #define BTSTACK_FILE__ "avrcp_target.c"
- #include <stdint.h>
- #include <string.h>
- #include <inttypes.h>
- #include "btstack.h"
- #include "classic/avrcp.h"
- #define AVRCP_ATTR_HEADER_LEN 8
- static const uint8_t AVRCP_NOTIFICATION_TRACK_SELECTED[] = {0,0,0,0,0,0,0,0};
- static const uint8_t AVRCP_NOTIFICATION_TRACK_NOT_SELECTED[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- avrcp_context_t avrcp_target_context;
- static int avrcp_target_supports_browsing(uint16_t target_supported_features){
- return target_supported_features & AVRCP_FEATURE_MASK_BROWSING;
- }
- void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
- avrcp_create_sdp_record(0, service, service_record_handle, avrcp_target_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
- }
- static void avrcp_target_emit_operation(btstack_packet_handler_t callback, uint16_t avrcp_cid, avrcp_operation_id_t operation_id, uint8_t operands_length, uint8_t operand){
- btstack_assert(callback != NULL);
- uint8_t event[8];
- int pos = 0;
- event[pos++] = HCI_EVENT_AVRCP_META;
- event[pos++] = sizeof(event) - 2;
- event[pos++] = AVRCP_SUBEVENT_OPERATION;
- little_endian_store_16(event, pos, avrcp_cid);
- pos += 2;
- event[pos++] = operation_id;
- event[pos++] = operands_length;
- event[pos++] = operand;
- (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
- }
- static void avrcp_target_emit_volume_changed(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t absolute_volume){
- btstack_assert(callback != NULL);
- uint8_t event[7];
- int offset = 0;
- event[offset++] = HCI_EVENT_AVRCP_META;
- event[offset++] = sizeof(event) - 2;
- event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
- little_endian_store_16(event, offset, avrcp_cid);
- offset += 2;
- event[offset++] = AVRCP_CTYPE_NOTIFY;
- event[offset++] = absolute_volume;
- (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
- }
- static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){
- btstack_assert(callback != NULL);
- uint8_t event[5];
- int pos = 0;
- event[pos++] = HCI_EVENT_AVRCP_META;
- event[pos++] = sizeof(event) - 2;
- event[pos++] = subevent_id;
- little_endian_store_16(event, pos, avrcp_cid);
- (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
- }
- // returns number of bytes stored
- static uint16_t avrcp_target_pack_single_element_header(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint16_t attr_value_size){
- btstack_assert(attr_id >= 1);
- btstack_assert(attr_id <= AVRCP_MEDIA_ATTR_COUNT);
- big_endian_store_32(packet, pos, attr_id);
- big_endian_store_16(packet, pos+4, RFC2978_CHARSET_MIB_UTF8);
- big_endian_store_16(packet, pos+6, attr_value_size);
- return 8;
- }
- // returns number of bytes stored
- static uint16_t avrcp_target_pack_single_element_attribute_number(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint32_t value){
- uint16_t attr_value_length = sprintf((char *)(packet+pos+8), "%0" PRIu32, value);
- (void) avrcp_target_pack_single_element_header(packet, pos, attr_id, attr_value_length);
- return 8 + attr_value_length;
- }
- // returns number of bytes stored
- static uint16_t avrcp_target_pack_single_element_attribute_string_fragment(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint8_t * attr_value, uint16_t attr_value_to_copy, uint16_t attr_value_size, bool header){
- if (attr_value_size == 0) return 0;
- uint16_t bytes_stored = 0;
- if (header){
- bytes_stored += avrcp_target_pack_single_element_header(packet, pos, attr_id, attr_value_size);
- }
- (void)memcpy(packet + pos + bytes_stored, attr_value, attr_value_to_copy);
- bytes_stored += attr_value_to_copy;
- return bytes_stored;
- }
- static int avrcp_target_abort_continue_response(uint16_t cid, avrcp_connection_t * connection){
- uint16_t pos = 0;
- l2cap_reserve_packet_buffer();
- uint8_t * packet = l2cap_get_outgoing_buffer();
-
- connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
- connection->command_type = AVRCP_CTYPE_RESPONSE_ACCEPTED;
- connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
- connection->subunit_id = AVRCP_SUBUNIT_ID;
- packet[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
- // Profile IDentifier (PID)
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
- // command_type
- packet[pos++] = connection->command_type;
- // subunit_type | subunit ID
- packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
- // opcode
- packet[pos++] = (uint8_t)connection->command_opcode;
- // company id is 3 bytes long
- big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
- pos += 3;
- packet[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE;
-
- // reserve byte for packet type
- packet[pos++] = AVRCP_SINGLE_PACKET;
- big_endian_store_16(packet, pos, 0);
- pos += 2;
- return l2cap_send_prepared(cid, pos);
- }
- static int avrcp_target_send_now_playing_info(uint16_t cid, avrcp_connection_t * connection){
- uint16_t pos = 0;
- l2cap_reserve_packet_buffer();
- uint8_t * packet = l2cap_get_outgoing_buffer();
- uint16_t size = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
-
- packet[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
- // Profile IDentifier (PID)
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
- // command_type
- packet[pos++] = connection->command_type;
- // subunit_type | subunit ID
- packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
- // opcode
- packet[pos++] = (uint8_t)connection->command_opcode;
- // company id is 3 bytes long
- big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
- pos += 3;
- packet[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
-
- // reserve byte for packet type
- uint8_t pos_packet_type = pos;
- pos++;
-
- uint16_t playing_info_buffer_len_position = pos;
- pos += 2;
- if (connection->next_attr_id == AVRCP_MEDIA_ATTR_NONE){
- packet[pos_packet_type] = AVRCP_SINGLE_PACKET;
- connection->packet_type = AVRCP_SINGLE_PACKET;
- packet[pos++] = count_set_bits_uint32(connection->now_playing_info_attr_bitmap);
- connection->next_attr_id = AVRCP_MEDIA_ATTR_ALL;
- }
-
- uint8_t fragmented = 0;
- int num_free_bytes = size - pos - 2;
- uint8_t MAX_NUMBER_ATTR_LEN = 10;
- while (!fragmented && (num_free_bytes > 0) && (connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS)){
- avrcp_media_attribute_id_t attr_id = connection->next_attr_id;
- int attr_index = attr_id - 1;
- if (connection->now_playing_info_attr_bitmap & (1 << attr_id)){
- int num_written_bytes = 0;
- int num_bytes_to_write = 0;
- switch (attr_id){
- case AVRCP_MEDIA_ATTR_ALL:
- case AVRCP_MEDIA_ATTR_NONE:
- break;
- case AVRCP_MEDIA_ATTR_TRACK:
- num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
- if (num_free_bytes >= num_bytes_to_write){
- num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->track_nr);
- break;
- }
- fragmented = 1;
- connection->attribute_value_offset = 0;
- break;
- case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
- num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
- if (num_free_bytes >= num_bytes_to_write){
- num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->total_tracks);
- break;
- }
- fragmented = 1;
- connection->attribute_value_offset = 0;
- break;
- case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
- num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
- if (num_free_bytes >= num_bytes_to_write){
- num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->song_length_ms);
- break;
- }
- fragmented = 1;
- connection->attribute_value_offset = 0;
- break;
- default:{
- bool header = connection->attribute_value_offset == 0;
- uint8_t * attr_value = (uint8_t *) (connection->now_playing_info[attr_index].value + connection->attribute_value_offset);
- uint16_t attr_value_len = connection->now_playing_info[attr_index].len - connection->attribute_value_offset;
- num_bytes_to_write = attr_value_len + (header * AVRCP_ATTR_HEADER_LEN);
- if (num_bytes_to_write <= num_free_bytes){
- connection->attribute_value_offset = 0;
- num_written_bytes = num_bytes_to_write;
- avrcp_target_pack_single_element_attribute_string_fragment(packet, pos, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header);
- break;
- }
- fragmented = 1;
- num_written_bytes = num_free_bytes;
- attr_value_len = num_free_bytes - (header * AVRCP_ATTR_HEADER_LEN);
- avrcp_target_pack_single_element_attribute_string_fragment(packet, pos, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header);
- connection->attribute_value_offset += attr_value_len;
- break;
- }
- }
- pos += num_written_bytes;
- num_free_bytes -= num_written_bytes;
- }
- if (!fragmented){
- // C++ compatible version of connection->next_attr_id++
- connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1);
- }
- }
- if (fragmented){
- switch (connection->packet_type){
- case AVRCP_SINGLE_PACKET:
- connection->packet_type = AVRCP_START_PACKET;
- break;
- default:
- connection->packet_type = AVRCP_CONTINUE_PACKET;
- break;
- }
- } else {
- if (connection->next_attr_id >= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS){ // DONE
- if (connection->packet_type != AVRCP_SINGLE_PACKET){
- connection->packet_type = AVRCP_END_PACKET;
- }
- }
- }
- packet[pos_packet_type] = connection->packet_type;
- // store attr value length
- big_endian_store_16(packet, playing_info_buffer_len_position, pos - playing_info_buffer_len_position - 2);
- return l2cap_send_prepared(cid, size);
- }
- static int avrcp_target_send_response(uint16_t cid, avrcp_connection_t * connection){
- int pos = 0;
- l2cap_reserve_packet_buffer();
- uint8_t * packet = l2cap_get_outgoing_buffer();
- // transport header
- // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
- // TODO: check for fragmentation
- connection->packet_type = AVRCP_SINGLE_PACKET;
- packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
- // Profile IDentifier (PID)
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
- // command_type
- packet[pos++] = connection->command_type;
- // subunit_type | subunit ID
- packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
- // opcode
- packet[pos++] = (uint8_t)connection->command_opcode;
- (void)memcpy(packet + pos, connection->cmd_operands,
- connection->cmd_operands_length);
- pos += connection->cmd_operands_length;
-
- connection->wait_to_send = false;
- return l2cap_send_prepared(cid, pos);
- }
- static void avrcp_target_response_setup(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id,
- avrcp_command_opcode_t opcode){
- connection->command_type = command_type;
- connection->subunit_type = subunit_type;
- connection->subunit_id = subunit_id;
- connection->command_opcode = opcode;
- }
- static uint8_t avrcp_target_response_accept(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t status){
- // AVRCP_CTYPE_RESPONSE_REJECTED
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, subunit_type, subunit_id, opcode);
- // company id is 3 bytes long
- int pos = connection->cmd_operands_length;
- connection->cmd_operands[pos++] = pdu_id;
- connection->cmd_operands[pos++] = 0;
- // param length
- big_endian_store_16(connection->cmd_operands, pos, 1);
- pos += 2;
- connection->cmd_operands[pos++] = status;
- connection->cmd_operands_length = pos;
- connection->accept_response = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_response_reject(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){
- // AVRCP_CTYPE_RESPONSE_REJECTED
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_REJECTED, subunit_type, subunit_id, opcode);
- // company id is 3 bytes long
- int pos = connection->cmd_operands_length;
- connection->cmd_operands[pos++] = pdu_id;
- connection->cmd_operands[pos++] = 0;
- // param length
- big_endian_store_16(connection->cmd_operands, pos, 1);
- pos += 2;
- connection->cmd_operands[pos++] = status;
- connection->cmd_operands_length = pos;
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_response_not_implemented(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t event_id){
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, subunit_type, subunit_id, opcode);
- // company id is 3 bytes long
- int pos = connection->cmd_operands_length;
- connection->cmd_operands[pos++] = pdu_id;
- connection->cmd_operands[pos++] = 0;
- // param length
- big_endian_store_16(connection->cmd_operands, pos, 1);
- pos += 2;
- connection->cmd_operands[pos++] = event_id;
- connection->cmd_operands_length = pos;
-
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_response_vendor_dependent_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){
- // company id is 3 bytes long
- int pos = connection->cmd_operands_length;
- connection->cmd_operands[pos++] = pdu_id;
- connection->cmd_operands[pos++] = 0;
- // param length
- big_endian_store_16(connection->cmd_operands, pos, 1 + value_len);
- pos += 2;
- connection->cmd_operands[pos++] = event_id;
- if (value && (value_len > 0)){
- (void)memcpy(connection->cmd_operands + pos, value, value_len);
- pos += value_len;
- }
- connection->cmd_operands_length = pos;
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_response_addressed_player_changed_interim(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id){
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
- // company id is 3 bytes long
- int pos = connection->cmd_operands_length;
- connection->cmd_operands[pos++] = pdu_id;
- connection->cmd_operands[pos++] = 0;
- // param length
- big_endian_store_16(connection->cmd_operands, pos, 5);
- pos += 2;
- connection->cmd_operands[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
- big_endian_read_16( &connection->cmd_operands[pos], connection->addressed_player_id);
- pos += 2;
- big_endian_read_16( &connection->cmd_operands[pos], connection->uid_counter);
- pos += 2;
-
- connection->cmd_operands_length = pos;
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_pass_through_response(uint16_t avrcp_cid, avrcp_command_type_t cmd_type, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("Could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- avrcp_target_response_setup(connection, cmd_type, AVRCP_SUBUNIT_TYPE_PANEL, AVRCP_SUBUNIT_ID, AVRCP_CMD_OPCODE_PASS_THROUGH);
- int pos = 0;
- connection->cmd_operands[pos++] = opid;
- connection->cmd_operands[pos++] = operands_length;
- if (operands_length == 1){
- connection->cmd_operands[pos++] = operand;
- }
- connection->cmd_operands_length = pos;
-
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
- return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand);
- }
- uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
- return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
- }
- uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
- return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
- }
- void avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_target_set_unit_info: could not find a connection.");
- return;
- }
- connection->unit_type = unit_type;
- connection->company_id = company_id;
- }
- void avrcp_target_set_subunit_info(uint16_t avrcp_cid, avrcp_subunit_type_t subunit_type, const uint8_t * subunit_info_data, uint16_t subunit_info_data_size){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_target_set_subunit_info: could not find a connection.");
- return;
- }
- connection->subunit_info_type = subunit_type;
- connection->subunit_info_data = subunit_info_data;
- connection->subunit_info_data_size = subunit_info_data_size;
- }
- static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){
- if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
-
- uint8_t unit = 0;
- connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
- connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
- connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE;
- connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
-
- connection->cmd_operands_length = 5;
- connection->cmd_operands[0] = 0x07;
- connection->cmd_operands[1] = (connection->unit_type << 4) | unit;
- // company id is 3 bytes long
- big_endian_store_32(connection->cmd_operands, 2, connection->company_id);
-
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){
- if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
- if ((offset - 4) > connection->subunit_info_data_size) return AVRCP_STATUS_INVALID_PARAMETER;
- connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
- connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
- connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
- connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE;
- uint8_t page = offset / 4;
- uint8_t extension_code = 7;
- connection->cmd_operands_length = 5;
- connection->cmd_operands[0] = (page << 4) | extension_code;
- (void)memcpy(connection->cmd_operands + 1,
- connection->subunit_info_data + offset, 4);
-
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static inline uint8_t avrcp_prepare_vendor_dependent_response(uint16_t avrcp_cid, avrcp_connection_t ** out_connection, avrcp_pdu_id_t pdu_id, uint16_t param_length){
- *out_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!*out_connection){
- log_error("avrcp tartget: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- if ((*out_connection)->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
- (*out_connection)->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
- (*out_connection)->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
- (*out_connection)->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
- (*out_connection)->subunit_id = AVRCP_SUBUNIT_ID;
- (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = pdu_id;
- // reserved
- (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = 0;
- // param length
- big_endian_store_16((*out_connection)->cmd_operands, (*out_connection)->cmd_operands_length, param_length);
- (*out_connection)->cmd_operands_length += 2;
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_capability(uint16_t avrcp_cid, avrcp_capability_id_t capability_id, uint8_t num_capabilities, uint8_t * capabilities, uint8_t capabilities_size){
- avrcp_connection_t * connection = NULL;
- uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_CAPABILITIES, 2 + capabilities_size);
- if (status != ERROR_CODE_SUCCESS) return status;
- connection->cmd_operands[connection->cmd_operands_length++] = capability_id;
- connection->cmd_operands[connection->cmd_operands_length++] = num_capabilities;
- (void)memcpy(connection->cmd_operands + connection->cmd_operands_length,
- capabilities, capabilities_size);
- connection->cmd_operands_length += capabilities_size;
-
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t num_event_ids, uint8_t * event_ids, uint8_t event_ids_size){
- return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, num_event_ids, event_ids, event_ids_size);
- }
- uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t num_company_ids, uint8_t * company_ids, uint8_t company_ids_size){
- return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, num_company_ids, company_ids, company_ids_size);
- }
- uint8_t avrcp_target_play_status(uint16_t avrcp_cid, uint32_t song_length_ms, uint32_t song_position_ms, avrcp_playback_status_t play_status){
- avrcp_connection_t * connection = NULL;
- uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_PLAY_STATUS, 11);
- if (status != ERROR_CODE_SUCCESS) return status;
- big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_length_ms);
- connection->cmd_operands_length += 4;
- big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_position_ms);
- connection->cmd_operands_length += 4;
- connection->cmd_operands[connection->cmd_operands_length++] = play_status;
-
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_now_playing_info(avrcp_connection_t * connection){
- if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
- connection->now_playing_info_response = 1;
- connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
- connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
- connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
- connection->subunit_id = AVRCP_SUBUNIT_ID;
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){
- int index = attr_id - 1;
- if (!value) return AVRCP_STATUS_INVALID_PARAMETER;
- connection->now_playing_info[index].value = (uint8_t*)value;
- connection->now_playing_info[index].len = strlen(value);
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_unit_info: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- if (connection->playback_status == playback_status) return ERROR_CODE_SUCCESS;
- connection->playback_status = playback_status;
- connection->playback_status_changed = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return ERROR_CODE_SUCCESS;
- }
- void avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_unit_info: could not find a connection. cid 0x%02x\n", avrcp_cid);
- return;
- }
- if (!current_track){
- connection->track_selected = 0;
- connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
- return;
- }
- (void)memcpy(connection->track_id, current_track->track_id, 8);
- connection->song_length_ms = current_track->song_length_ms;
- connection->track_nr = current_track->track_nr;
- connection->total_tracks = total_tracks;
- avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title);
- avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist);
- avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album);
- avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre);
- connection->track_selected = 1;
-
- if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
- connection->track_changed = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- }
- return;
- }
- uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_target_track_changed: could not find connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- if (!track_id) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
- connection->track_changed = 1;
- (void)memcpy(connection->track_id, track_id, 8);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- }
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_target_playing_content_changed: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) {
- connection->playing_content_changed = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- }
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_unit_info: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) {
- connection->uid_counter = uid_counter;
- connection->addressed_player_id = player_id;
- }
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_unit_info: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- if (connection->battery_status == battery_status) return ERROR_CODE_SUCCESS;
- if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) {
- connection->battery_status = battery_status;
- connection->battery_status_changed = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- }
- return ERROR_CODE_SUCCESS;
- }
- uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t volume_percentage){
- avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
- if (!connection){
- log_error("avrcp_unit_info: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
- connection->volume_percentage = volume_percentage;
- if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) {
- connection->notify_volume_percentage_changed = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- }
- return ERROR_CODE_SUCCESS;
- }
- static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){
- if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return;
- connection->notifications_transaction_label[notification] = transaction_label;
- }
- static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){
- if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0;
- return connection->notifications_transaction_label[notification];
- }
- static uint8_t * avrcp_get_company_id(uint8_t *packet, uint16_t size){
- UNUSED(size);
- return packet + 6;
- }
- static uint8_t * avrcp_get_pdu(uint8_t *packet, uint16_t size){
- UNUSED(size);
- return packet + 9;
- }
- static uint8_t avrcp_is_receive_pass_through_cmd(uint8_t operation_id){
- return operation_id & 0x80;
- }
- static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
- if (size < 6u) return;
- uint16_t pid = 0;
- uint8_t transport_header = packet[0];
- connection->transaction_label = transport_header >> 4;
- avrcp_packet_type_t packet_type = (avrcp_packet_type_t) ((transport_header & 0x0F) >> 2);
- switch (packet_type){
- case AVRCP_SINGLE_PACKET:
- pid = big_endian_read_16(packet, 1);
- break;
- case AVRCP_START_PACKET:
- pid = big_endian_read_16(packet, 2);
- break;
- default:
- break;
- }
-
- switch (packet_type){
- case AVRCP_SINGLE_PACKET:
- case AVRCP_START_PACKET:
- if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){
- log_info("Invalid pid 0x%02x, expected 0x%02x", connection->invalid_pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
- connection->reject_transport_header = 1;
- connection->invalid_pid = pid;
- connection->transport_header = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2 ) | (AVRCP_RESPONSE_FRAME << 1) | 1;
- connection->state = AVCTP_W2_SEND_RESPONSE;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
- break;
- default:
- break;
- }
- if (packet_type != AVRCP_SINGLE_PACKET) return;
-
- avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
- avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07);
-
- avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size);
- uint8_t * company_id = avrcp_get_company_id(packet, size);
- uint8_t * pdu = avrcp_get_pdu(packet, size);
-
- int pos = 4;
- uint16_t length;
- avrcp_pdu_id_t pdu_id;
- connection->cmd_operands_length = 0;
-
- switch (opcode){
- case AVRCP_CMD_OPCODE_UNIT_INFO:
- avrcp_target_unit_info(connection);
- break;
- case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
- uint8_t offset = 4 * (packet[pos+2]>>4);
- avrcp_target_subunit_info(connection, offset);
- break;
- }
- case AVRCP_CMD_OPCODE_PASS_THROUGH:{
- log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d, operand %d", packet[6], packet[7], packet[8]);
- avrcp_operation_id_t operation_id = (avrcp_operation_id_t) packet[6];
-
- if (avrcp_is_receive_pass_through_cmd(operation_id)){
- operation_id = (avrcp_operation_id_t) (packet[6] & 0x7F);
- avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
- break;
- }
-
- switch (operation_id){
- case AVRCP_OPERATION_ID_PLAY:
- case AVRCP_OPERATION_ID_PAUSE:
- case AVRCP_OPERATION_ID_STOP:
- case AVRCP_OPERATION_ID_VOLUME_UP:
- case AVRCP_OPERATION_ID_VOLUME_DOWN:
- case AVRCP_OPERATION_ID_REWIND:
- case AVRCP_OPERATION_ID_FAST_FORWARD:
- case AVRCP_OPERATION_ID_FORWARD:
- case AVRCP_OPERATION_ID_BACKWARD:
- case AVRCP_OPERATION_ID_SKIP:
- case AVRCP_OPERATION_ID_MUTE:
- case AVRCP_OPERATION_ID_CHANNEL_UP:
- case AVRCP_OPERATION_ID_CHANNEL_DOWN:
- case AVRCP_OPERATION_ID_SELECT:
- case AVRCP_OPERATION_ID_UP:
- case AVRCP_OPERATION_ID_DOWN:
- case AVRCP_OPERATION_ID_LEFT:
- case AVRCP_OPERATION_ID_RIGHT:
- case AVRCP_OPERATION_ID_ROOT_MENU:
- avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
- avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, operation_id, packet[7], packet[8]);
- break;
- case AVRCP_OPERATION_ID_UNDEFINED:
- avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
- return;
- default:
- avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
- return;
- }
- break;
- }
- case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
- pdu_id = (avrcp_pdu_id_t) pdu[0];
- // 1 - reserved
- // 2-3 param length,
- length = big_endian_read_16(pdu, 2);
- (void)memcpy(connection->cmd_operands, company_id, 3);
- connection->cmd_operands_length = 3;
- switch (pdu_id){
- case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{
- bool ok = length == 4;
- if (avrcp_target_context.set_addressed_player_callback != NULL){
- uint16_t player_id = big_endian_read_16(pdu, 4);
- ok = avrcp_target_context.set_addressed_player_callback(player_id);
- }
- if (ok){
- avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_SUCCESS);
- } else {
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID);
- }
- break;
- }
- case AVRCP_PDU_ID_GET_CAPABILITIES:{
- avrcp_capability_id_t capability_id = (avrcp_capability_id_t) pdu[pos];
- switch (capability_id){
- case AVRCP_CAPABILITY_ID_EVENT:
- avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY);
- break;
- case AVRCP_CAPABILITY_ID_COMPANY:
- avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY);
- break;
- default:
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
- break;
- }
- break;
- }
- case AVRCP_PDU_ID_GET_PLAY_STATUS:
- avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
- break;
- case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE:
- connection->cmd_operands[0] = pdu[4];
- connection->abort_continue_response = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- break;
- case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE:
- if (pdu[4] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
- return;
- }
- connection->now_playing_info_response = 1;
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- break;
- case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
- uint8_t play_identifier[8];
- memset(play_identifier, 0, 8);
- if (memcmp(pdu+pos, play_identifier, 8) != 0) {
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
- return;
- }
- pos += 8;
- uint8_t attribute_count = pdu[pos++];
- connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE;
- if (!attribute_count){
- connection->now_playing_info_attr_bitmap = 0xFE;
- } else {
- int i;
- connection->now_playing_info_attr_bitmap = 0;
- for (i=0; i < attribute_count; i++){
- uint16_t attr_id = big_endian_read_16(pdu, pos);
- pos += 2;
- connection->now_playing_info_attr_bitmap |= (1 << attr_id);
- }
- }
- log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap);
- avrcp_target_now_playing_info(connection);
- break;
- }
- case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
- // 0 - pdu id
- // 1 - reserved
- // 2-3 param length
- avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) pdu[4];
- uint16_t event_mask = (1 << event_id);
- avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_label);
-
- switch (event_id){
- case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:
- connection->notifications_enabled |= event_mask;
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
- if (connection->track_selected){
- avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8);
- } else {
- avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8);
- }
- break;
- case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:
- connection->notifications_enabled |= event_mask;
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
- avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1);
- break;
- case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:
- connection->notifications_enabled |= event_mask;
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
- avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0);
- break;
- case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:
- connection->notify_volume_percentage_changed = 0;
- connection->notifications_enabled |= event_mask;
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
- avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->volume_percentage, 1);
- break;
- case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:
- connection->notifications_enabled |= event_mask;
- avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
- avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1);
- break;
- case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:
- case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
- case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:
- avrcp_target_response_not_implemented(connection, subunit_type, subunit_id, opcode, pdu_id, event_id);
- return;
- case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED:
- connection->notifications_enabled |= event_mask;
- avrcp_target_response_addressed_player_changed_interim(connection, subunit_type, subunit_id, opcode, pdu_id);
- return;
- default:
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
- return;
- }
- break;
- }
- case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: {
- if (length != 1){
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
- break;
- }
- uint8_t absolute_volume = pdu[4];
- if (absolute_volume < 0x80){
- connection->volume_percentage = absolute_volume;
- }
- avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, connection->volume_percentage);
- avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->volume_percentage);
- break;
- }
- default:
- log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id);
- avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
- break;
- }
- break;
- default:
- log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size));
- break;
- }
- }
- static int avrcp_target_send_notification(uint16_t cid, avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){
- if (!connection){
- log_error("avrcp tartget: could not find a connection.");
- return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
- }
-
- connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
- connection->command_type = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
- connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
- connection->subunit_id = AVRCP_SUBUNIT_ID;
- connection->transaction_label = avrcp_target_get_transaction_label_for_notification(connection, notification_id);
-
- uint16_t pos = 0;
- l2cap_reserve_packet_buffer();
- uint8_t * packet = l2cap_get_outgoing_buffer();
- uint16_t size = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
- connection->packet_type = AVRCP_SINGLE_PACKET;
- packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
- // Profile IDentifier (PID)
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
- packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
- // command_type
- packet[pos++] = connection->command_type;
- // subunit_type | subunit ID
- packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
- // opcode
- packet[pos++] = (uint8_t)connection->command_opcode;
- // company id is 3 bytes long
- big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
- pos += 3;
- packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
- packet[pos++] = 0;
- uint16_t remainig_outgoing_buffer_size = size - pos - 2;
- uint16_t caped_value_len = btstack_min(value_len + 1, remainig_outgoing_buffer_size);
- big_endian_store_16(packet, pos, caped_value_len);
- pos += 2;
- packet[pos++] = notification_id;
- (void)memcpy(packet + pos, value, caped_value_len - 1);
- pos += caped_value_len - 1;
- connection->wait_to_send = false;
- return l2cap_send_prepared(cid, pos);
- }
- static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){
- if (!connection){
- log_error("avrcp tartget: could not find a connection.");
- return;
- }
- connection->notifications_enabled &= ~(1 << notification_id);
- connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
- avrcp_target_set_transaction_label_for_notification(connection, notification_id, 0);
- }
- static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
- avrcp_connection_t * connection;
- switch (packet_type) {
- case L2CAP_DATA_PACKET:
- connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel);
- avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
- break;
- case HCI_EVENT_PACKET:
- switch (hci_event_packet_get_type(packet)){
- case L2CAP_EVENT_CAN_SEND_NOW:{
- connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel);
-
- if (connection->accept_response){
- connection->accept_response = 0;
- avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
- if (connection->abort_continue_response){
- connection->abort_continue_response = 0;
- connection->now_playing_info_response = 0;
- avrcp_target_abort_continue_response(connection->l2cap_signaling_cid, connection);
- return;
- }
- if (connection->now_playing_info_response){
- connection->now_playing_info_response = 0;
- avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
- return;
- }
-
- if (connection->track_changed){
- connection->track_changed = 0;
- avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED, connection->track_id, 8);
- avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
-
- if (connection->playback_status_changed){
- connection->playback_status_changed = 0;
- uint8_t playback_status = (uint8_t) connection->playback_status;
- avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED, &playback_status, 1);
- avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
-
- if (connection->playing_content_changed){
- connection->playing_content_changed = 0;
- avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED, NULL, 0);
- avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
-
- if (connection->battery_status_changed){
- connection->battery_status_changed = 0;
- avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED, (uint8_t *)&connection->battery_status, 1);
- avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
-
- if (connection->notify_volume_percentage_changed){
- connection->notify_volume_percentage_changed = 0;
- avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED, &connection->volume_percentage, 1);
- avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
- if (connection->reject_transport_header){
- connection->state = AVCTP_CONNECTION_OPENED;
- connection->reject_transport_header = 0;
- l2cap_reserve_packet_buffer();
- uint8_t * out_buffer = l2cap_get_outgoing_buffer();
- out_buffer[0] = connection->transport_header;
- big_endian_store_16(out_buffer, 1, connection->invalid_pid);
- l2cap_send_prepared(connection->l2cap_signaling_cid, 3);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- }
- switch (connection->state){
- case AVCTP_W2_SEND_RESPONSE:
- connection->state = AVCTP_CONNECTION_OPENED;
- avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
- avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
- return;
- default:
- break;
- }
- break;
- }
- default:
- break;
- }
- default:
- break;
- }
- }
- void avrcp_target_init(void){
- avrcp_target_context.role = AVRCP_TARGET;
- avrcp_target_context.packet_handler = avrcp_target_packet_handler;
- avrcp_register_target_packet_handler(&avrcp_target_packet_handler);
- }
- void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
- btstack_assert(callback != NULL);
- avrcp_target_context.avrcp_callback = callback;
- }
- void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){
- btstack_assert(callback != NULL);
- avrcp_target_context.set_addressed_player_callback = callback;
- }
|