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