xref: /btstack/src/classic/avrcp_target.c (revision 5e845fceafc5c45ae9e63d6ef82dca30d8211666)
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_target.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <inttypes.h>
45 
46 #include "btstack.h"
47 #include "classic/avrcp.h"
48 
49 #define AVRCP_ATTR_HEADER_LEN  8
50 
51 static const uint8_t AVRCP_NOTIFICATION_TRACK_SELECTED[] = {0,0,0,0,0,0,0,0};
52 static const uint8_t AVRCP_NOTIFICATION_TRACK_NOT_SELECTED[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
53 
54 avrcp_context_t avrcp_target_context;
55 
56 static int avrcp_target_supports_browsing(uint16_t target_supported_features){
57     return target_supported_features & (1 << AVRCP_TARGET_SUPPORTED_FEATURE_BROWSING);
58 }
59 
60 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){
61     avrcp_create_sdp_record(0, service, service_record_handle, avrcp_target_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
62 }
63 
64 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){
65     if (!callback) return;
66     uint8_t event[8];
67     int pos = 0;
68     event[pos++] = HCI_EVENT_AVRCP_META;
69     event[pos++] = sizeof(event) - 2;
70     event[pos++] = AVRCP_SUBEVENT_OPERATION;
71     little_endian_store_16(event, pos, avrcp_cid);
72     pos += 2;
73     event[pos++] = operation_id;
74     event[pos++] = operands_length;
75     event[pos++] = operand;
76     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
77 }
78 
79 static void avrcp_target_emit_volume_changed(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t absolute_volume){
80     if (!callback) return;
81     uint8_t event[7];
82     int offset = 0;
83     event[offset++] = HCI_EVENT_AVRCP_META;
84     event[offset++] = sizeof(event) - 2;
85     event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
86     little_endian_store_16(event, offset, avrcp_cid);
87     offset += 2;
88     event[offset++] = AVRCP_CTYPE_NOTIFY;
89     event[offset++] = absolute_volume;
90     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
91 }
92 
93 static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){
94     if (!callback) return;
95     uint8_t event[5];
96     int pos = 0;
97     event[pos++] = HCI_EVENT_AVRCP_META;
98     event[pos++] = sizeof(event) - 2;
99     event[pos++] = subevent_id;
100     little_endian_store_16(event, pos, avrcp_cid);
101     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
102 }
103 
104 // returns number of bytes stored
105 static uint16_t avrcp_target_pack_single_element_header(uint8_t * packet, uint16_t pos, rfc2978_charset_mib_enumid_t mib_enumid, avrcp_media_attribute_id_t attr_id, uint16_t attr_value_size){
106     btstack_assert(attr_id >= 1);
107     btstack_assert(attr_id <= AVRCP_MEDIA_ATTR_COUNT);
108 
109     big_endian_store_32(packet, pos, attr_id);
110     big_endian_store_16(packet, pos+4, mib_enumid);
111     big_endian_store_16(packet, pos+6, attr_value_size);
112     return 8;
113 }
114 
115 // returns number of bytes stored
116 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){
117     if ((attr_id < 1) || (attr_id > AVRCP_MEDIA_ATTR_COUNT)) return 0;
118     uint16_t attr_value_length = sprintf((char *)(packet+pos+8), "%0" PRIu32, value);
119     (void) avrcp_target_pack_single_element_header(packet, pos, RFC2978_CHARSET_MIB_UTF8, attr_id, attr_value_length);
120     return 8 + attr_value_length;
121 }
122 
123 // returns number of bytes stored
124 static uint16_t avrcp_target_pack_single_element_attribute_string_fragment(uint8_t * packet, uint16_t pos, rfc2978_charset_mib_enumid_t mib_enumid, avrcp_media_attribute_id_t attr_id, uint8_t * attr_value, uint16_t attr_value_to_copy, uint16_t attr_value_size, uint8_t header){
125     if (attr_value_size == 0) return 0;
126     if ((attr_id < 1) || (attr_id > AVRCP_MEDIA_ATTR_COUNT)) return 0;
127     uint16_t bytes_stored = 0;
128     if (header){
129         bytes_stored += avrcp_target_pack_single_element_header(packet, pos, mib_enumid, attr_id, attr_value_size);
130     }
131     (void)memcpy(packet + pos + bytes_stored, attr_value, attr_value_to_copy);
132     bytes_stored += attr_value_to_copy;
133     return bytes_stored;
134 }
135 
136 static int avrcp_target_abort_continue_response(uint16_t cid, avrcp_connection_t * connection){
137     uint16_t pos = 0;
138     l2cap_reserve_packet_buffer();
139     uint8_t * packet = l2cap_get_outgoing_buffer();
140 
141     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
142     connection->command_type    = AVRCP_CTYPE_RESPONSE_ACCEPTED;
143     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
144     connection->subunit_id      = AVRCP_SUBUNIT_ID;
145 
146     packet[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
147     // Profile IDentifier (PID)
148     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
149     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
150 
151     // command_type
152     packet[pos++] = connection->command_type;
153     // subunit_type | subunit ID
154     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
155     // opcode
156     packet[pos++] = (uint8_t)connection->command_opcode;
157 
158     // company id is 3 bytes long
159     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
160     pos += 3;
161 
162     packet[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE;
163 
164     // reserve byte for packet type
165     packet[pos++] = AVRCP_SINGLE_PACKET;
166     big_endian_store_16(packet, pos, 0);
167     pos += 2;
168     return l2cap_send_prepared(cid, pos);
169 }
170 
171 static int avrcp_target_send_now_playing_info(uint16_t cid, avrcp_connection_t * connection){
172     uint16_t pos = 0;
173     l2cap_reserve_packet_buffer();
174     uint8_t * packet = l2cap_get_outgoing_buffer();
175     uint16_t  size   = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
176 
177     packet[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
178     // Profile IDentifier (PID)
179     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
180     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
181 
182     // command_type
183     packet[pos++] = connection->command_type;
184     // subunit_type | subunit ID
185     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
186     // opcode
187     packet[pos++] = (uint8_t)connection->command_opcode;
188 
189     // company id is 3 bytes long
190     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
191     pos += 3;
192 
193     packet[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
194 
195     // reserve byte for packet type
196     uint8_t pos_packet_type = pos;
197     pos++;
198 
199     uint16_t playing_info_buffer_len_position = pos;
200     pos += 2;
201     // printf("connection->next_attr_id %d \n", connection->next_attr_id);
202     if (connection->next_attr_id == AVRCP_MEDIA_ATTR_NONE){
203         packet[pos_packet_type] = AVRCP_SINGLE_PACKET;
204         connection->packet_type = AVRCP_SINGLE_PACKET;
205         packet[pos++] = count_set_bits_uint32(connection->now_playing_info_attr_bitmap);
206         connection->next_attr_id = AVRCP_MEDIA_ATTR_ALL;
207     }
208     // printf("updated connection->next_attr_id %d, connection->attribute_value_offset %d \n", connection->next_attr_id, connection->attribute_value_offset);
209 
210     uint8_t fragmented = 0;
211     int num_free_bytes = size - pos - 2;
212     uint8_t MAX_NUMBER_ATTR_LEN = 10;
213 
214     while (!fragmented && (num_free_bytes > 0) && (connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS)){
215         avrcp_media_attribute_id_t attr_id = connection->next_attr_id;
216         int attr_index = attr_id - 1;
217 
218         if (connection->now_playing_info_attr_bitmap & (1 << attr_id)){
219             int num_written_bytes = 0;
220             int num_bytes_to_write = 0;
221             switch (attr_id){
222                 case AVRCP_MEDIA_ATTR_ALL:
223                 case AVRCP_MEDIA_ATTR_NONE:
224                     break;
225                 case AVRCP_MEDIA_ATTR_TRACK:
226                     num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
227                     if (num_free_bytes >= num_bytes_to_write){
228                         num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->track_nr);
229                         break;
230                     }
231                     fragmented = 1;
232                     connection->attribute_value_offset = 0;
233                     break;
234                 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
235                     num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
236                     if (num_free_bytes >= num_bytes_to_write){
237                         num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->total_tracks);
238                         break;
239                     }
240                     fragmented = 1;
241                     connection->attribute_value_offset = 0;
242                     break;
243                 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
244                     num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
245                     if (num_free_bytes >= num_bytes_to_write){
246                         num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->song_length_ms);
247                         break;
248                     }
249                     fragmented = 1;
250                     connection->attribute_value_offset = 0;
251                     break;
252                 default:{
253                     uint8_t   header = (connection->attribute_value_offset == 0);
254                     uint8_t * attr_value =     (uint8_t *) (connection->now_playing_info[attr_index].value + connection->attribute_value_offset);
255                     uint16_t  attr_value_len = connection->now_playing_info[attr_index].len - connection->attribute_value_offset;
256 
257                     num_bytes_to_write = attr_value_len + (header * AVRCP_ATTR_HEADER_LEN);
258                     if (num_bytes_to_write <= num_free_bytes){
259                         connection->attribute_value_offset = 0;
260                         num_written_bytes = num_bytes_to_write;
261                         avrcp_target_pack_single_element_attribute_string_fragment(packet, pos, RFC2978_CHARSET_MIB_UTF8, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header);
262                         break;
263                     }
264                     fragmented = 1;
265                     num_written_bytes = num_free_bytes;
266                     attr_value_len = num_free_bytes - (header * AVRCP_ATTR_HEADER_LEN);
267                     avrcp_target_pack_single_element_attribute_string_fragment(packet, pos, RFC2978_CHARSET_MIB_UTF8, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header);
268                     connection->attribute_value_offset += attr_value_len;
269                     break;
270                 }
271             }
272             pos += num_written_bytes;
273             num_free_bytes -= num_written_bytes;
274         }
275         if (!fragmented){
276             // C++ compatible version of connection->next_attr_id++
277             connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1);
278         }
279     }
280 
281     if (fragmented){
282         switch (connection->packet_type){
283             case AVRCP_SINGLE_PACKET:
284                 connection->packet_type = AVRCP_START_PACKET;
285                 break;
286             default:
287                 connection->packet_type = AVRCP_CONTINUE_PACKET;
288                 break;
289         }
290     } else {
291         if (connection->next_attr_id >= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS){ // DONE
292             if (connection->packet_type != AVRCP_SINGLE_PACKET){
293                 connection->packet_type = AVRCP_END_PACKET;
294             }
295         }
296     }
297     packet[pos_packet_type] = connection->packet_type;
298     // store attr value length
299     big_endian_store_16(packet, playing_info_buffer_len_position, pos - playing_info_buffer_len_position - 2);
300     return l2cap_send_prepared(cid, size);
301 }
302 
303 
304 
305 static int avrcp_target_send_response(uint16_t cid, avrcp_connection_t * connection){
306     int pos = 0;
307     l2cap_reserve_packet_buffer();
308     uint8_t * packet = l2cap_get_outgoing_buffer();
309 
310     // transport header
311     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
312 
313     // TODO: check for fragmentation
314     connection->packet_type = AVRCP_SINGLE_PACKET;
315 
316     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
317     // Profile IDentifier (PID)
318     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
319     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
320     // command_type
321     packet[pos++] = connection->command_type;
322     // subunit_type | subunit ID
323     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
324     // opcode
325     packet[pos++] = (uint8_t)connection->command_opcode;
326 
327     // if (connection->command_opcode  == AVRCP_CMD_OPCODE_VENDOR_DEPENDENT){
328     //     // company id is 3 bytes long
329     //     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
330     //     pos += 3;
331     // }
332     // operands
333     (void)memcpy(packet + pos, connection->cmd_operands,
334                  connection->cmd_operands_length);
335     pos += connection->cmd_operands_length;
336     // printf(" pos to send %d\n", pos);
337     // printf_hexdump(packet, pos);
338 
339     connection->wait_to_send = 0;
340     return l2cap_send_prepared(cid, pos);
341 }
342 
343 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){
344     // AVRCP_CTYPE_RESPONSE_REJECTED
345     connection->command_type = AVRCP_CTYPE_RESPONSE_ACCEPTED;
346     connection->subunit_type = subunit_type;
347     connection->subunit_id =   subunit_id;
348     connection->command_opcode = opcode;
349     // company id is 3 bytes long
350     int pos = connection->cmd_operands_length;
351     connection->cmd_operands[pos++] = pdu_id;
352     connection->cmd_operands[pos++] = 0;
353     // param length
354     big_endian_store_16(connection->cmd_operands, pos, 1);
355     pos += 2;
356     connection->cmd_operands[pos++] = status;
357     connection->cmd_operands_length = pos;
358     connection->accept_response = 1;
359     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
360     return ERROR_CODE_SUCCESS;
361 }
362 
363 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){
364     // AVRCP_CTYPE_RESPONSE_REJECTED
365     connection->command_type = AVRCP_CTYPE_RESPONSE_REJECTED;
366     connection->subunit_type = subunit_type;
367     connection->subunit_id =   subunit_id;
368     connection->command_opcode = opcode;
369     // company id is 3 bytes long
370     int pos = connection->cmd_operands_length;
371     connection->cmd_operands[pos++] = pdu_id;
372     connection->cmd_operands[pos++] = 0;
373     // param length
374     big_endian_store_16(connection->cmd_operands, pos, 1);
375     pos += 2;
376     connection->cmd_operands[pos++] = status;
377     connection->cmd_operands_length = pos;
378     connection->state = AVCTP_W2_SEND_RESPONSE;
379     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
380     return ERROR_CODE_SUCCESS;
381 }
382 
383 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){
384     connection->command_type = AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED;
385     connection->subunit_type = subunit_type;
386     connection->subunit_id =   subunit_id;
387     connection->command_opcode = opcode;
388 
389     // company id is 3 bytes long
390     int pos = connection->cmd_operands_length;
391     connection->cmd_operands[pos++] = pdu_id;
392     connection->cmd_operands[pos++] = 0;
393     // param length
394     big_endian_store_16(connection->cmd_operands, pos, 1);
395     pos += 2;
396     connection->cmd_operands[pos++] = event_id;
397     connection->cmd_operands_length = pos;
398 
399     connection->state = AVCTP_W2_SEND_RESPONSE;
400     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
401     return ERROR_CODE_SUCCESS;
402 }
403 
404 static uint8_t avrcp_target_response_vendor_dependent_interim(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id,
405     avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){
406     connection->command_type = AVRCP_CTYPE_RESPONSE_INTERIM;
407     connection->subunit_type = subunit_type;
408     connection->subunit_id =   subunit_id;
409     connection->command_opcode = opcode;
410 
411     // company id is 3 bytes long
412     int pos = connection->cmd_operands_length;
413     connection->cmd_operands[pos++] = pdu_id;
414     connection->cmd_operands[pos++] = 0;
415     // param length
416     big_endian_store_16(connection->cmd_operands, pos, 1 + value_len);
417     pos += 2;
418     connection->cmd_operands[pos++] = event_id;
419     if (value && (value_len > 0)){
420         (void)memcpy(connection->cmd_operands + pos, value, value_len);
421         pos += value_len;
422     }
423     connection->cmd_operands_length = pos;
424     connection->state = AVCTP_W2_SEND_RESPONSE;
425     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
426     return ERROR_CODE_SUCCESS;
427 }
428 
429 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){
430     connection->command_type = AVRCP_CTYPE_RESPONSE_INTERIM;
431     connection->subunit_type = subunit_type;
432     connection->subunit_id =   subunit_id;
433     connection->command_opcode = opcode;
434 
435     // company id is 3 bytes long
436     int pos = connection->cmd_operands_length;
437     connection->cmd_operands[pos++] = pdu_id;
438     connection->cmd_operands[pos++] = 0;
439     // param length
440     big_endian_store_16(connection->cmd_operands, pos, 5);
441     pos += 2;
442     connection->cmd_operands[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
443     big_endian_read_16( &connection->cmd_operands[pos], connection->addressed_player_id);
444     pos += 2;
445     big_endian_read_16( &connection->cmd_operands[pos], connection->uid_counter);
446     pos += 2;
447 
448     connection->cmd_operands_length = pos;
449     connection->state = AVCTP_W2_SEND_RESPONSE;
450     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
451     return ERROR_CODE_SUCCESS;
452 }
453 
454 
455 // static uint8_t avrcp_target_response_vendor_dependent_changed(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){
456 //     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
457 //     connection->command_type = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
458 //     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
459 //     connection->subunit_id = AVRCP_SUBUNIT_ID;
460 
461 //     // company id is 3 bytes long
462 //     int pos = connection->cmd_operands_length;
463 //     connection->cmd_operands[pos++] = pdu_id;
464 //     connection->cmd_operands[pos++] = 0;
465 //     // param length
466 //     big_endian_store_16(connection->cmd_operands, pos, 1);
467 //     pos += 2;
468 //     connection->cmd_operands[pos++] = event_id;
469 //     connection->cmd_operands_length = pos;
470 
471 //     connection->state = AVCTP_W2_SEND_RESPONSE;
472 //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
473 //     return ERROR_CODE_SUCCESS;
474 // }
475 
476 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){
477     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
478     if (!connection){
479         log_error("Could not find a connection.");
480         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
481     }
482     connection->command_type = cmd_type;
483     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
484     connection->subunit_id =   AVRCP_SUBUNIT_ID;
485     connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH;
486 
487     int pos = 0;
488     connection->cmd_operands[pos++] = opid;
489     connection->cmd_operands[pos++] = operands_length;
490     if (operands_length == 1){
491         connection->cmd_operands[pos++] = operand;
492     }
493     connection->cmd_operands_length = pos;
494 
495     connection->state = AVCTP_W2_SEND_RESPONSE;
496     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
497     return ERROR_CODE_SUCCESS;
498 }
499 
500 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
501     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand);
502 }
503 
504 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
505     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
506 }
507 
508 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
509     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
510 }
511 
512 void avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
513     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
514     if (!connection){
515         log_error("avrcp_target_operation_reject: could not find a connection.");
516         return;
517     }
518     connection->unit_type = unit_type;
519     connection->company_id = company_id;
520 }
521 
522 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){
523     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
524     if (!connection){
525         log_error("avrcp_target_operation_reject: could not find a connection.");
526         return;
527     }
528     connection->subunit_info_type = subunit_type;
529     connection->subunit_info_data = subunit_info_data;
530     connection->subunit_info_data_size = subunit_info_data_size;
531 }
532 
533 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){
534     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
535 
536     uint8_t unit = 0;
537     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
538     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
539     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
540     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
541 
542     connection->cmd_operands_length = 5;
543     connection->cmd_operands[0] = 0x07;
544     connection->cmd_operands[1] = (connection->unit_type << 4) | unit;
545     // company id is 3 bytes long
546     big_endian_store_32(connection->cmd_operands, 2, connection->company_id);
547 
548     connection->state = AVCTP_W2_SEND_RESPONSE;
549     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
550     return ERROR_CODE_SUCCESS;
551 }
552 
553 
554 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){
555     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
556     if ((offset - 4) > connection->subunit_info_data_size) return AVRCP_STATUS_INVALID_PARAMETER;
557 
558     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
559     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
560     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
561     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
562     // printf("avrcp_target_subunit_info  subunit_type %d\n", connection->subunit_type);
563 
564     uint8_t page = offset / 4;
565     uint8_t extension_code = 7;
566     connection->cmd_operands_length = 5;
567     connection->cmd_operands[0] = (page << 4) | extension_code;
568 
569     (void)memcpy(connection->cmd_operands + 1,
570                  connection->subunit_info_data + offset, 4);
571 
572     connection->state = AVCTP_W2_SEND_RESPONSE;
573     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
574     return ERROR_CODE_SUCCESS;
575 }
576 
577 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){
578     *out_connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
579     if (!*out_connection){
580         log_error("avrcp tartget: could not find a connection.");
581         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
582     }
583 
584     if ((*out_connection)->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
585     (*out_connection)->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
586     (*out_connection)->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
587     (*out_connection)->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
588     (*out_connection)->subunit_id      = AVRCP_SUBUNIT_ID;
589 
590     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = pdu_id;
591     // reserved
592     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = 0;
593     // param length
594     big_endian_store_16((*out_connection)->cmd_operands, (*out_connection)->cmd_operands_length, param_length);
595     (*out_connection)->cmd_operands_length += 2;
596     return ERROR_CODE_SUCCESS;
597 }
598 
599 static uint8_t avrcp_target_capability(uint16_t avrcp_cid, avrcp_capability_id_t capability_id, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
600     avrcp_connection_t * connection = NULL;
601     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_CAPABILITIES, 2+size);
602     if (status != ERROR_CODE_SUCCESS) return status;
603 
604     connection->cmd_operands[connection->cmd_operands_length++] = capability_id;
605     connection->cmd_operands[connection->cmd_operands_length++] = capabilities_num;
606     (void)memcpy(connection->cmd_operands + connection->cmd_operands_length,
607                  capabilities, size);
608     connection->cmd_operands_length += size;
609 
610     connection->state = AVCTP_W2_SEND_RESPONSE;
611     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
612     return ERROR_CODE_SUCCESS;
613 }
614 
615 uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
616     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, capabilities_num, capabilities, size);
617 }
618 
619 uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size ){
620     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, capabilities_num, capabilities, size);
621 }
622 
623 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){
624     avrcp_connection_t * connection = NULL;
625     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_PLAY_STATUS, 11);
626     if (status != ERROR_CODE_SUCCESS) return status;
627 
628     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_length_ms);
629     connection->cmd_operands_length += 4;
630     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_position_ms);
631     connection->cmd_operands_length += 4;
632     connection->cmd_operands[connection->cmd_operands_length++] = play_status;
633 
634     connection->state = AVCTP_W2_SEND_RESPONSE;
635     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
636     return ERROR_CODE_SUCCESS;
637 }
638 
639 static uint8_t avrcp_target_now_playing_info(avrcp_connection_t * connection){
640     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
641     connection->now_playing_info_response = 1;
642     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
643     connection->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
644     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
645     connection->subunit_id      = AVRCP_SUBUNIT_ID;
646 
647     connection->state = AVCTP_W2_SEND_RESPONSE;
648     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
649     return ERROR_CODE_SUCCESS;
650 }
651 
652 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){
653     int index = attr_id - 1;
654     if (!value) return AVRCP_STATUS_INVALID_PARAMETER;
655     connection->now_playing_info[index].value = (uint8_t*)value;
656     connection->now_playing_info[index].len   = strlen(value);
657     // printf("store %lu bytes, %s\n",  strlen(value), value);
658     return ERROR_CODE_SUCCESS;
659 }
660 
661 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){
662     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
663     if (!connection){
664         log_error("avrcp_unit_info: could not find a connection.");
665         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
666     }
667     if (connection->playback_status == playback_status) return ERROR_CODE_SUCCESS;
668 
669     connection->playback_status = playback_status;
670     connection->playback_status_changed = 1;
671     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
672     return ERROR_CODE_SUCCESS;
673 }
674 
675 void avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){
676     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
677     if (!connection){
678         log_error("avrcp_unit_info: could not find a connection. cid 0x%02x\n", avrcp_cid);
679         return;
680     }
681     if (!current_track){
682         connection->track_selected = 0;
683         connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
684         return;
685     }
686     (void)memcpy(connection->track_id, current_track->track_id, 8);
687     connection->song_length_ms = current_track->song_length_ms;
688     connection->track_nr = current_track->track_nr;
689     connection->total_tracks = total_tracks;
690     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title);
691     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist);
692     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album);
693     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre);
694     connection->track_selected = 1;
695 
696     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
697         connection->track_changed = 1;
698         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
699     }
700     return;
701 }
702 
703 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){
704     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
705     if (!connection){
706         log_error("avrcp_target_track_changed: could not find connection.");
707         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
708     }
709     if (!track_id) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
710 
711     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
712         connection->track_changed = 1;
713         (void)memcpy(connection->track_id, track_id, 8);
714         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
715     }
716     return ERROR_CODE_SUCCESS;
717 }
718 
719 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){
720     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
721     if (!connection){
722         log_error("avrcp_target_playing_content_changed: could not find a connection.");
723         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
724     }
725     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) {
726         connection->playing_content_changed = 1;
727         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
728     }
729     return ERROR_CODE_SUCCESS;
730 }
731 
732 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){
733     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
734     if (!connection){
735         log_error("avrcp_unit_info: could not find a connection.");
736         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
737     }
738     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) {
739         // printf("send AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED\n");
740         // connection->addressed_player_changed = 1;
741         connection->uid_counter = uid_counter;
742         connection->addressed_player_id = player_id;
743         // avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
744     }
745     return ERROR_CODE_SUCCESS;
746 }
747 
748 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){
749     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
750     if (!connection){
751         log_error("avrcp_unit_info: could not find a connection.");
752         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
753     }
754     if (connection->battery_status == battery_status) return ERROR_CODE_SUCCESS;
755     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) {
756         connection->battery_status = battery_status;
757         connection->battery_status_changed = 1;
758         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
759     }
760     return ERROR_CODE_SUCCESS;
761 }
762 
763 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t volume_percentage){
764     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
765     if (!connection){
766         log_error("avrcp_unit_info: could not find a connection.");
767         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
768     }
769     // if (connection->volume_percentage == volume_percentage) return ERROR_CODE_SUCCESS;
770     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) {
771         connection->volume_percentage = volume_percentage;
772         connection->notify_volume_percentage_changed = 1;
773         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
774     }
775     return ERROR_CODE_SUCCESS;
776 }
777 
778 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){
779     if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return;
780     connection->notifications_transaction_label[notification] = transaction_label;
781 }
782 
783 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){
784     if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0;
785     return connection->notifications_transaction_label[notification];
786 }
787 
788 static uint8_t * avrcp_get_company_id(uint8_t *packet, uint16_t size){
789     UNUSED(size);
790     return packet + 6;
791 }
792 
793 static uint8_t * avrcp_get_pdu(uint8_t *packet, uint16_t size){
794     UNUSED(size);
795     return packet + 9;
796 }
797 
798 static uint8_t avrcp_is_receive_pass_through_cmd(uint8_t operation_id){
799     return operation_id & 0x80;
800 }
801 
802 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
803     UNUSED(connection);
804     UNUSED(packet);
805     UNUSED(size);
806 
807     // uint8_t opcode;
808     uint16_t pid = 0;
809     uint8_t transport_header = packet[0];
810     connection->transaction_label = transport_header >> 4;
811     // uint8_t frame_type = (transport_header & 0x03) >> 1;
812     avrcp_packet_type_t packet_type = (avrcp_packet_type_t) ((transport_header & 0x0F) >> 2);
813     switch (packet_type){
814         case AVRCP_SINGLE_PACKET:
815             pid =  big_endian_read_16(packet, 1);
816             break;
817         case AVRCP_START_PACKET:
818             pid =  big_endian_read_16(packet, 2);
819             break;
820         default:
821             break;
822     }
823 
824     switch (packet_type){
825         case AVRCP_SINGLE_PACKET:
826         case AVRCP_START_PACKET:
827             if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){
828                 log_info("Invalid pid 0x%02x, expected 0x%02x", connection->invalid_pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
829                 connection->reject_transport_header = 1;
830                 connection->invalid_pid = pid;
831                 connection->transport_header = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2 ) | (AVRCP_RESPONSE_FRAME << 1) | 1;
832                 connection->state = AVCTP_W2_SEND_RESPONSE;
833                 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
834                 return;
835             }
836             break;
837         default:
838             break;
839     }
840 
841     // avrcp_command_type_t ctype = (avrcp_command_type_t) packet[3];
842     // uint8_t byte_value = packet[4];
843     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
844     avrcp_subunit_id_t   subunit_id   = (avrcp_subunit_id_t) (packet[4] & 0x07);
845     // opcode = packet[pos++];
846     // printf("    Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n",
847     //     transport_header, transaction_label, packet_type, frame_type, ipid, pid);
848     // printf_hexdump(packet+pos, size-pos);
849 
850     avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size);
851     uint8_t * company_id = avrcp_get_company_id(packet, size);
852     uint8_t * pdu = avrcp_get_pdu(packet, size);
853     // uint16_t param_length = big_endian_read_16(pdu, 2);
854 
855     int pos = 4;
856     uint16_t length;
857     avrcp_pdu_id_t   pdu_id;
858     connection->cmd_operands_length = 0;
859 
860     switch (opcode){
861         case AVRCP_CMD_OPCODE_UNIT_INFO:
862             avrcp_target_unit_info(connection);
863             break;
864         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
865             uint8_t offset =  4 * (packet[pos+2]>>4);
866             avrcp_target_subunit_info(connection, offset);
867             break;
868         }
869         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
870             log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d, operand %d", packet[6], packet[7], packet[8]);
871             avrcp_operation_id_t operation_id = (avrcp_operation_id_t) packet[6];
872 
873             if (avrcp_is_receive_pass_through_cmd(operation_id)){
874                 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7F);
875                 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
876                 break;
877             }
878 
879             switch (operation_id){
880                 case AVRCP_OPERATION_ID_PLAY:
881                 case AVRCP_OPERATION_ID_PAUSE:
882                 case AVRCP_OPERATION_ID_STOP:
883                 case AVRCP_OPERATION_ID_VOLUME_UP:
884                 case AVRCP_OPERATION_ID_VOLUME_DOWN:
885                 case AVRCP_OPERATION_ID_REWIND:
886                 case AVRCP_OPERATION_ID_FAST_FORWARD:
887                 case AVRCP_OPERATION_ID_FORWARD:
888                 case AVRCP_OPERATION_ID_BACKWARD:
889                 case AVRCP_OPERATION_ID_SKIP:
890                 case AVRCP_OPERATION_ID_MUTE:
891                 case AVRCP_OPERATION_ID_CHANNEL_UP:
892                 case AVRCP_OPERATION_ID_CHANNEL_DOWN:
893                 case AVRCP_OPERATION_ID_SELECT:
894                 case AVRCP_OPERATION_ID_UP:
895                 case AVRCP_OPERATION_ID_DOWN:
896                 case AVRCP_OPERATION_ID_LEFT:
897                 case AVRCP_OPERATION_ID_RIGHT:
898                 case AVRCP_OPERATION_ID_ROOT_MENU:
899                     avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
900                     avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, operation_id, packet[7], packet[8]);
901                     break;
902                 case AVRCP_OPERATION_ID_UNDEFINED:
903                     avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
904                     return;
905                 default:
906                     avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
907                     return;
908             }
909             break;
910         }
911 
912         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
913             pdu_id = (avrcp_pdu_id_t) pdu[0];
914             // 1 - reserved
915             // 2-3 param length,
916             length = big_endian_read_16(pdu, 2);
917             (void)memcpy(connection->cmd_operands, company_id, 3);
918             connection->cmd_operands_length = 3;
919             switch (pdu_id){
920                 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{
921                     if (length == 0){
922                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID);
923                         break;
924                     }
925                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_SUCCESS);
926                     break;
927                 }
928                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
929                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) pdu[pos];
930                     switch (capability_id){
931                         case AVRCP_CAPABILITY_ID_EVENT:
932                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY);
933                             break;
934                         case AVRCP_CAPABILITY_ID_COMPANY:
935                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY);
936                             break;
937                         default:
938                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
939                             break;
940                     }
941                     break;
942                 }
943                 case AVRCP_PDU_ID_GET_PLAY_STATUS:
944                     avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
945                     break;
946                 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE:
947                     connection->cmd_operands[0] = pdu[4];
948                     connection->abort_continue_response = 1;
949                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
950                     break;
951                 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE:
952                     if (pdu[4] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){
953                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
954                         return;
955                     }
956                     connection->now_playing_info_response = 1;
957                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
958                     break;
959                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
960                     uint8_t play_identifier[8];
961                     memset(play_identifier, 0, 8);
962                     if (memcmp(pdu+pos, play_identifier, 8) != 0) {
963                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
964                         return;
965                     }
966                     pos += 8;
967                     uint8_t attribute_count = pdu[pos++];
968                     // printf("AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES attribute count %d\n", attribute_count);
969                     connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE;
970                     if (!attribute_count){
971                         connection->now_playing_info_attr_bitmap = 0xFE;
972                     } else {
973                         int i;
974                         connection->now_playing_info_attr_bitmap = 0;
975                         for (i=0; i < attribute_count; i++){
976                             uint16_t attr_id = big_endian_read_16(pdu, pos);
977                             pos += 2;
978                             connection->now_playing_info_attr_bitmap |= (1 << attr_id);
979                         }
980                     }
981                     log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap);
982                     avrcp_target_now_playing_info(connection);
983                     break;
984                 }
985                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
986                     // 0 - pdu id
987                     // 1 - reserved
988                     // 2-3 param length
989                     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) pdu[4];
990                     uint16_t event_mask = (1 << event_id);
991                     avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_label);
992 
993                     switch (event_id){
994                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:
995                             connection->notifications_enabled |= event_mask;
996                             if (connection->track_selected){
997                                 avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8);
998                             } else {
999                                 avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8);
1000                             }
1001                             break;
1002                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:
1003                             connection->notifications_enabled |= event_mask;
1004                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1);
1005                             break;
1006                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:
1007                             connection->notifications_enabled |= event_mask;
1008                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, NULL, 0);
1009                             break;
1010                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:
1011                             connection->notify_volume_percentage_changed = 0;
1012                             connection->notifications_enabled |= event_mask;
1013                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->volume_percentage, 1);
1014                             break;
1015                         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:
1016                             connection->notifications_enabled |= event_mask;
1017                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1);
1018                             break;
1019                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:
1020                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
1021                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:
1022                             avrcp_target_response_not_implemented(connection, subunit_type, subunit_id, opcode, pdu_id, event_id);
1023                             return;
1024                         case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED:
1025                             connection->notifications_enabled |= event_mask;
1026                             avrcp_target_response_addressed_player_changed_interim(connection, subunit_type, subunit_id, opcode, pdu_id);
1027                             return;
1028                         default:
1029                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
1030                             return;
1031                     }
1032                     break;
1033                 }
1034                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: {
1035                     if (length != 1){
1036                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1037                         break;
1038                     }
1039 
1040                     uint8_t absolute_volume = pdu[4];
1041                     if (absolute_volume < 0x80){
1042                         connection->volume_percentage = absolute_volume;
1043                     }
1044                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, connection->volume_percentage);
1045                     avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->volume_percentage);
1046                     // avrcp_target_volume_changed(connection->avrcp_cid, connection->volume_percentage);
1047                     break;
1048                 }
1049                 default:
1050                     log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id);
1051                     avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1052                     break;
1053             }
1054             break;
1055         default:
1056             log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size));
1057             break;
1058     }
1059 }
1060 
1061 #if 0
1062 static int avrcp_target_send_addressed_player_changed_notification(uint16_t cid, avrcp_connection_t * connection, uint16_t uid, uint16_t uid_counter){
1063     if (!connection){
1064         log_error("avrcp tartget: could not find a connection.");
1065         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1066     }
1067     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1068     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
1069     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
1070     connection->subunit_id      = AVRCP_SUBUNIT_ID;
1071 
1072     uint16_t pos = 0;
1073     l2cap_reserve_packet_buffer();
1074     uint8_t * packet = l2cap_get_outgoing_buffer();
1075 
1076     connection->packet_type = AVRCP_SINGLE_PACKET;
1077     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
1078     // Profile IDentifier (PID)
1079     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
1080     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
1081 
1082     // command_type
1083     packet[pos++] = connection->command_type;
1084     // subunit_type | subunit ID
1085     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
1086     // opcode
1087     packet[pos++] = (uint8_t)connection->command_opcode;
1088 
1089     // company id is 3 bytes long
1090     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
1091     pos += 3;
1092 
1093     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
1094     packet[pos++] = 0;
1095     big_endian_store_16(packet, pos, 5);
1096     pos += 2;
1097     packet[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
1098     big_endian_store_16(packet, pos, uid);
1099     pos += 2;
1100     big_endian_store_16(packet, pos, uid_counter);
1101     pos += 2;
1102 
1103     connection->wait_to_send = 0;
1104     return l2cap_send_prepared(cid, pos);
1105 }
1106 #endif
1107 
1108 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){
1109     if (!connection){
1110         log_error("avrcp tartget: could not find a connection.");
1111         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1112     }
1113 
1114     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1115     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
1116     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
1117     connection->subunit_id      = AVRCP_SUBUNIT_ID;
1118     connection->transaction_label = avrcp_target_get_transaction_label_for_notification(connection, notification_id);
1119 
1120     uint16_t pos = 0;
1121     l2cap_reserve_packet_buffer();
1122     uint8_t * packet = l2cap_get_outgoing_buffer();
1123     uint16_t  size   = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
1124 
1125     connection->packet_type = AVRCP_SINGLE_PACKET;
1126     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
1127     // Profile IDentifier (PID)
1128     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
1129     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
1130 
1131     // command_type
1132     packet[pos++] = connection->command_type;
1133     // subunit_type | subunit ID
1134     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
1135     // opcode
1136     packet[pos++] = (uint8_t)connection->command_opcode;
1137 
1138     // company id is 3 bytes long
1139     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
1140     pos += 3;
1141 
1142     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
1143     packet[pos++] = 0;
1144     uint16_t remainig_outgoing_buffer_size = size - pos - 2;
1145 
1146     uint16_t caped_value_len = btstack_min(value_len + 1, remainig_outgoing_buffer_size);
1147     big_endian_store_16(packet, pos, caped_value_len);
1148     pos += 2;
1149     packet[pos++] = notification_id;
1150     (void)memcpy(packet + pos, value, caped_value_len - 1);
1151     pos += caped_value_len - 1;
1152     connection->wait_to_send = 0;
1153     return l2cap_send_prepared(cid, pos);
1154 }
1155 
1156 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){
1157     if (!connection){
1158         log_error("avrcp tartget: could not find a connection.");
1159         return;
1160     }
1161     connection->notifications_enabled &= ~(1 << notification_id);
1162     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1163     avrcp_target_set_transaction_label_for_notification(connection, notification_id, 0);
1164 }
1165 
1166 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1167     avrcp_connection_t * connection;
1168     switch (packet_type) {
1169         case L2CAP_DATA_PACKET:
1170             connection = get_avrcp_connection_for_l2cap_signaling_cid(AVRCP_TARGET, channel);
1171             if (!connection) break;
1172             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1173             break;
1174         case HCI_EVENT_PACKET:
1175             switch (hci_event_packet_get_type(packet)){
1176                 case HCI_EVENT_AVRCP_META:
1177                     // forward to app
1178                     (*avrcp_target_context.avrcp_callback)(packet_type, channel, packet, size);
1179                     break;
1180                 case L2CAP_EVENT_CAN_SEND_NOW:{
1181                     connection = get_avrcp_connection_for_l2cap_signaling_cid(AVRCP_TARGET, channel);
1182                     if (!connection) {
1183                         log_error("Connection not found\n");
1184                         break;
1185                     }
1186 
1187                     if (connection->accept_response){
1188                         connection->accept_response = 0;
1189                         avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
1190                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1191                         break;
1192                     }
1193                     if (connection->abort_continue_response){
1194                         connection->abort_continue_response = 0;
1195                         connection->now_playing_info_response = 0;
1196                         avrcp_target_abort_continue_response(connection->l2cap_signaling_cid, connection);
1197                         break;
1198                     }
1199 
1200                     if (connection->now_playing_info_response){
1201                         connection->now_playing_info_response = 0;
1202                         avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1203                         break;
1204                     }
1205 
1206                     if (connection->track_changed){
1207                         connection->track_changed = 0;
1208                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED, connection->track_id, 8);
1209                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED);
1210                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1211                         break;
1212                     }
1213 
1214                     if (connection->playback_status_changed){
1215                         connection->playback_status_changed = 0;
1216                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED, &connection->playback_status_changed, 1);
1217                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED);
1218                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1219                         break;
1220                     }
1221 
1222                     // if (connection->addressed_player_changed){
1223                     //     connection->playback_status_changed = 0;
1224                     //     avrcp_target_send_addressed_player_changed_notification(connection->l2cap_signaling_cid, connection, connection->addressed_player_id, connection->uid_counter);
1225                     //     avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED);
1226                     //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1227                     //     break;
1228                     // }
1229 
1230                     if (connection->playing_content_changed){
1231                         connection->playing_content_changed = 0;
1232                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED, NULL, 0);
1233                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED);
1234                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1235                         break;
1236                     }
1237 
1238                     if (connection->battery_status_changed){
1239                         connection->battery_status_changed = 0;
1240                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED, (uint8_t *)&connection->battery_status, 1);
1241                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED);
1242                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1243                         break;
1244                     }
1245 
1246                     if (connection->notify_volume_percentage_changed){
1247                         // printf("emit new volume %d\n", connection->volume_percentage);
1248                         connection->notify_volume_percentage_changed = 0;
1249                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED, &connection->volume_percentage, 1);
1250                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED);
1251                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1252                         break;
1253                     }
1254 
1255                     if (connection->reject_transport_header){
1256                         connection->state = AVCTP_CONNECTION_OPENED;
1257                         connection->reject_transport_header = 0;
1258                         l2cap_reserve_packet_buffer();
1259                         uint8_t * out_buffer = l2cap_get_outgoing_buffer();
1260                         out_buffer[0] = connection->transport_header;
1261                         big_endian_store_16(out_buffer, 1, connection->invalid_pid);
1262                         l2cap_send_prepared(connection->l2cap_signaling_cid, 3);
1263                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1264                         break;
1265                     }
1266 
1267                     switch (connection->state){
1268                         case AVCTP_W2_SEND_RESPONSE:
1269                             connection->state = AVCTP_CONNECTION_OPENED;
1270                             // if (connection->now_playing_info_response){
1271                             //     connection->now_playing_info_response = 0;
1272                             //     avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1273                             //     break;
1274                             // }
1275                             avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
1276                             avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1277                             return;
1278                         default:
1279                             break;
1280                     }
1281 
1282                     break;
1283             }
1284             default:
1285                 break;
1286         }
1287         default:
1288             break;
1289     }
1290 }
1291 
1292 void avrcp_target_init(void){
1293     avrcp_init();
1294     avrcp_target_context.role = AVRCP_TARGET;
1295     avrcp_target_context.packet_handler = avrcp_target_packet_handler;
1296     avrcp_register_target_packet_handler(&avrcp_target_packet_handler);
1297 }
1298 
1299 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
1300     if (callback == NULL){
1301         log_error("avrcp_register_packet_handler called with NULL callback");
1302         return;
1303     }
1304     avrcp_target_context.avrcp_callback = callback;
1305 }
1306 
1307 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){
1308     return avrcp_connect(AVRCP_TARGET, bd_addr, &avrcp_target_context, avrcp_cid);
1309 }
1310 
1311 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){
1312     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
1313     if (!connection){
1314         log_error("avrcp_target_disconnect: could not find a connection.");
1315         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1316     }
1317     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1318     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
1319     return ERROR_CODE_SUCCESS;
1320 }
1321 
1322