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