xref: /btstack/src/classic/avrcp_target.c (revision ef07fa3cfac746dddbdc1cd475931b124929d5a9)
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 BLUEKITCHEN
24  * GMBH 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 <string.h>
43 #include <inttypes.h>
44 
45 #include "classic/avrcp.h"
46 #include "classic/avrcp_target.h"
47 
48 #include "bluetooth_sdp.h"
49 #include "btstack_debug.h"
50 #include "btstack_event.h"
51 #include "btstack_util.h"
52 #include "l2cap.h"
53 
54 #include <stdio.h>
55 #define AVRCP_ATTR_HEADER_LEN  8
56 
57 static const uint8_t AVRCP_NOTIFICATION_TRACK_SELECTED[] = {0,0,0,0,0,0,0,0};
58 static const uint8_t AVRCP_NOTIFICATION_TRACK_NOT_SELECTED[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
59 
60 avrcp_context_t avrcp_target_context;
61 
62 static uint32_t default_companies[] = {
63     0x581900 //BT SIG registered CompanyID
64 };
65 
66 static int avrcp_target_supports_browsing(uint16_t target_supported_features){
67     return target_supported_features & AVRCP_FEATURE_MASK_BROWSING;
68 }
69 
70 void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
71     avrcp_create_sdp_record(0, service, service_record_handle, avrcp_target_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
72 }
73 
74 static void
75 avrcp_target_emit_operation(btstack_packet_handler_t callback, uint16_t avrcp_cid, avrcp_operation_id_t operation_id,
76                             bool button_pressed, uint8_t operands_length, uint8_t operand) {
77     btstack_assert(callback != NULL);
78 
79     uint8_t event[9];
80     int pos = 0;
81     event[pos++] = HCI_EVENT_AVRCP_META;
82     event[pos++] = sizeof(event) - 2;
83     event[pos++] = AVRCP_SUBEVENT_OPERATION;
84     little_endian_store_16(event, pos, avrcp_cid);
85     pos += 2;
86     event[pos++] = operation_id;
87     event[pos++] = button_pressed ? 1 : 0;
88     event[pos++] = operands_length;
89     event[pos++] = operand;
90     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
91 }
92 
93 static void avrcp_target_emit_volume_changed(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t absolute_volume){
94     btstack_assert(callback != NULL);
95 
96     uint8_t event[7];
97     int offset = 0;
98     event[offset++] = HCI_EVENT_AVRCP_META;
99     event[offset++] = sizeof(event) - 2;
100     event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
101     little_endian_store_16(event, offset, avrcp_cid);
102     offset += 2;
103     event[offset++] = AVRCP_CTYPE_NOTIFY;
104     event[offset++] = absolute_volume;
105     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
106 }
107 
108 static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){
109     btstack_assert(callback != NULL);
110 
111     uint8_t event[5];
112     int pos = 0;
113     event[pos++] = HCI_EVENT_AVRCP_META;
114     event[pos++] = sizeof(event) - 2;
115     event[pos++] = subevent_id;
116     little_endian_store_16(event, pos, avrcp_cid);
117     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
118 }
119 
120 // returns number of bytes stored
121 static uint16_t avrcp_target_pack_single_element_header(uint8_t * buffer, avrcp_media_attribute_id_t attr_id, uint16_t attr_value_size){
122     btstack_assert(attr_id > AVRCP_MEDIA_ATTR_ALL);
123     btstack_assert(attr_id < AVRCP_MEDIA_ATTR_RESERVED);
124     uint16_t pos = 0;
125     big_endian_store_32(buffer, pos, attr_id);
126     big_endian_store_16(buffer, pos + 4, RFC2978_CHARSET_MIB_UTF8);
127     big_endian_store_16(buffer, pos + 6, attr_value_size);
128     return 8;
129 }
130 
131 static uint16_t avrcp_now_playing_info_attr_id_value_len(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id){
132     char buffer[AVRCP_MAX_ATTRIBUTE_SIZE];
133     uint16_t str_len;
134     switch (attr_id) {
135         case AVRCP_MEDIA_ATTR_ALL:
136         case AVRCP_MEDIA_ATTR_NONE:
137             return 0;
138         case AVRCP_MEDIA_ATTR_TRACK:
139             str_len = sprintf(buffer, "%0" PRIu32, connection->track_nr);
140             break;
141         case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
142             str_len = sprintf(buffer, "%0" PRIu32, connection->total_tracks);
143             break;
144         case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
145             str_len = sprintf(buffer, "%0" PRIu32, connection->song_length_ms);
146             break;
147         default:
148             str_len = connection->now_playing_info[(uint16_t)attr_id - 1].len;
149             break;
150     }
151     return str_len;
152 }
153 
154 static uint16_t avrcp_now_playing_info_value_len_with_headers(avrcp_connection_t * connection){
155     uint16_t playing_info_len = 0;
156 
157     uint8_t i;
158     for ( i = (uint8_t)AVRCP_MEDIA_ATTR_ALL + 1; i < (uint8_t) AVRCP_MEDIA_ATTR_RESERVED; i++){
159         avrcp_media_attribute_id_t attr_id = (avrcp_media_attribute_id_t) i;
160 
161         if ((connection->now_playing_info_attr_bitmap & (1 << attr_id)) == 0) {
162             continue;
163         }
164 
165         switch (attr_id) {
166             case AVRCP_MEDIA_ATTR_ALL:
167             case AVRCP_MEDIA_ATTR_NONE:
168             case AVRCP_MEDIA_ATTR_DEFAULT_COVER_ART:
169                 break;
170             default:
171                 playing_info_len += AVRCP_ATTR_HEADER_LEN + avrcp_now_playing_info_attr_id_value_len(connection, attr_id);
172                 break;
173         }
174     }
175     // for total num bytes that of the attributes + headers
176     playing_info_len += 1;
177     return playing_info_len;
178 }
179 
180 static uint8_t * avrcp_get_attribute_value_from_u32(avrcp_connection_t * connection, uint32_t value, uint16_t * num_bytes_to_copy){
181     *num_bytes_to_copy = 0;
182 
183     if (connection->attribute_value_len == 0){
184         connection->attribute_value_len = sprintf((char *)connection->attribute_value, "%0" PRIu32, value);
185         connection->attribute_value_offset = 0;
186     }
187     *num_bytes_to_copy = connection->attribute_value_len - connection->attribute_value_offset;
188     return connection->attribute_value + connection->attribute_value_offset;
189 }
190 
191 static uint8_t * avrcp_get_next_value_fragment_for_attribute_id(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, uint16_t * num_bytes_to_copy){
192     switch (attr_id){
193         case AVRCP_MEDIA_ATTR_TRACK:
194             return avrcp_get_attribute_value_from_u32(connection, connection->track_nr, num_bytes_to_copy);
195         case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
196             return avrcp_get_attribute_value_from_u32(connection, connection->total_tracks, num_bytes_to_copy);
197         case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
198             return avrcp_get_attribute_value_from_u32(connection, connection->song_length_ms, num_bytes_to_copy);
199         default:
200             break;
201     }
202     int attr_index = attr_id - 1;
203     if (connection->attribute_value_len == 0){
204         connection->attribute_value_len = avrcp_now_playing_info_attr_id_value_len(connection, attr_id);
205         connection->attribute_value_offset = 0;
206     }
207     *num_bytes_to_copy = connection->now_playing_info[attr_index].len - connection->attribute_value_offset;
208     return (uint8_t *) (connection->now_playing_info[attr_index].value + connection->attribute_value_offset);
209 }
210 
211 // TODO Review
212 static uint16_t avrcp_store_avctp_now_playing_info_fragment(avrcp_connection_t * connection, uint16_t packet_size, uint8_t * packet){
213     uint16_t num_free_bytes = packet_size;
214 
215     uint16_t bytes_stored = 0;
216 
217     while ((num_free_bytes > 0) && (connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS)){
218         if ((connection->now_playing_info_attr_bitmap & (1 << (uint8_t)connection->next_attr_id)) == 0) {
219             connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1);
220             continue;
221         }
222 
223         // prepare attribute value
224         uint16_t num_bytes_to_copy;
225         uint8_t * attr_value_with_offset = avrcp_get_next_value_fragment_for_attribute_id(connection,
226                                                                                           connection->next_attr_id,
227                                                                                           &num_bytes_to_copy);
228 
229         // store header
230         if (connection->attribute_value_offset == 0){
231             // pack the whole attribute value header
232             if (connection->parser_attribute_header_pos == 0) {
233                 avrcp_target_pack_single_element_header(connection->parser_attribute_header, connection->next_attr_id,
234                                                         connection->attribute_value_len);
235             }
236         }
237 
238         if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN){
239             uint16_t num_header_bytes_to_store = btstack_min(num_free_bytes, AVRCP_ATTRIBUTE_HEADER_LEN - connection->parser_attribute_header_pos);
240             memcpy(packet + bytes_stored, connection->parser_attribute_header + connection->parser_attribute_header_pos, num_header_bytes_to_store);
241             connection->parser_attribute_header_pos += num_header_bytes_to_store;
242             bytes_stored += num_header_bytes_to_store;
243             num_free_bytes -= num_header_bytes_to_store;
244             connection->data_offset += num_header_bytes_to_store;
245 
246             if (num_free_bytes == 0){
247                 continue;
248             }
249         }
250 
251         // store value
252         uint16_t num_attr_value_bytes_to_store = btstack_min(num_free_bytes, connection->attribute_value_len - connection->attribute_value_offset);
253         memcpy(packet + bytes_stored, attr_value_with_offset, num_attr_value_bytes_to_store);
254         bytes_stored   += num_attr_value_bytes_to_store;
255         num_free_bytes -= num_attr_value_bytes_to_store;
256         connection->attribute_value_offset += num_attr_value_bytes_to_store;
257         connection->data_offset += num_attr_value_bytes_to_store;
258 
259         if (connection->attribute_value_offset == connection->attribute_value_len){
260             // C++ compatible version of connection->next_attr_id++
261             connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1);
262             connection->attribute_value_offset = 0;
263             connection->attribute_value_len = 0;
264             connection->parser_attribute_header_pos = 0;
265         }
266     }
267     return bytes_stored;
268 }
269 
270 static void avrcp_send_response_with_avctp_fragmentation(avrcp_connection_t * connection){
271     l2cap_reserve_packet_buffer();
272     uint8_t * packet = l2cap_get_outgoing_buffer();
273 
274     // transport header
275     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
276 
277     uint16_t max_payload_size;
278     connection->avctp_packet_type = avctp_get_packet_type(connection, &max_payload_size);
279     connection->packet_type = avrcp_get_packet_type(connection);
280 
281     // AVCTP header
282     // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
283     uint16_t pos = 0;
284     packet[pos++] = (connection->transaction_id << 4) | (connection->avctp_packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
285 
286     uint8_t param_len = connection->data_len;
287 
288     if (connection->avctp_packet_type == AVCTP_START_PACKET){
289         uint8_t max_frame_size = btstack_min(l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid), AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE);
290         // first packet: max_payload_size
291         // rest packets
292         uint16_t num_payload_bytes = param_len - max_payload_size;
293         uint16_t frame_size_for_continue_packet = max_frame_size - avctp_get_num_bytes_for_header(AVCTP_CONTINUE_PACKET);
294         uint16_t num_avctp_packets = (num_payload_bytes + frame_size_for_continue_packet - 1)/frame_size_for_continue_packet + 1;
295         packet[pos++] = num_avctp_packets;
296     }
297 
298     uint16_t bytes_stored = 0;
299     uint8_t i;
300 
301     switch (connection->avctp_packet_type) {
302         case AVCTP_SINGLE_PACKET:
303         case AVCTP_START_PACKET:
304             // Profile IDentifier (PID)
305             packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
306             packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
307 
308             // AVRCP message
309             // command_type
310             packet[pos++] = connection->command_type;
311             // subunit_type | subunit ID
312             packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
313             // opcode
314             packet[pos++] = (uint8_t) connection->command_opcode;
315 
316             switch (connection->command_opcode) {
317                 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
318                     big_endian_store_24(packet, pos, connection->company_id);
319                     pos += 3;
320                     packet[pos++] = connection->pdu_id;
321                     // AVRCP packet type
322 
323                     packet[pos++] = (uint8_t)connection->packet_type;
324                     // parameter length
325                     big_endian_store_16(packet, pos, param_len);
326                     pos += 2;
327 
328                     switch (connection->pdu_id) {
329                         // message is small enough to fit the single packet, no need for extra check
330                         case AVRCP_PDU_ID_GET_CAPABILITIES:
331                             // capability ID
332                             packet[pos++] = connection->data[0];
333                             // num_capabilities
334                             packet[pos++] = connection->data[1];
335 
336                             switch ((avrcp_capability_id_t) connection->data[0]) {
337                                 case AVRCP_CAPABILITY_ID_EVENT:
338                                     for (i = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX;
339                                          i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++) {
340                                         if ((connection->target_supported_notifications & (1 << i)) == 0) {
341                                             continue;
342                                         }
343                                         packet[pos++] = i;
344                                     }
345                                     break;
346                                 case AVRCP_CAPABILITY_ID_COMPANY:
347                                     // use Bluetooth SIG as default company
348                                     for (i = 0; i < connection->data[1]; i++) {
349                                         little_endian_store_24(packet, pos,
350                                                                connection->target_supported_companies[i]);
351                                         pos += 3;
352                                     }
353                                     break;
354                                 default:
355                                     // error response
356                                     break;
357                             }
358                             l2cap_send_prepared(connection->l2cap_signaling_cid, pos);
359                             return;
360 
361                         case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:
362 
363                             packet[pos++] = connection->num_attributes;
364                             max_payload_size--;
365 
366                             bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos);
367 
368                             connection->avrcp_frame_bytes_sent += bytes_stored + pos;
369                             l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored);
370                             return;
371 
372                         default:
373                             break;
374                     }
375                     break;
376 
377                 case AVRCP_CMD_OPCODE_PASS_THROUGH:
378                     packet[pos++] = connection->operation_id;
379                     // parameter length
380                     packet[pos++] = (uint8_t) connection->data_len;
381                     pos += 2;
382                     break;
383                 case AVRCP_CMD_OPCODE_UNIT_INFO:
384                     break;
385                 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:
386                     break;
387                 default:
388                     btstack_assert(false);
389                     return;
390             }
391             break;
392         case AVCTP_CONTINUE_PACKET:
393         case AVCTP_END_PACKET:
394             switch (connection->pdu_id) {
395                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:
396                     bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos);
397 
398                     connection->avrcp_frame_bytes_sent += bytes_stored + pos;
399                     l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored);
400                     return;
401 
402                 default:
403                     break;
404             }
405             break;
406         default:
407             btstack_assert(false);
408             return;
409     }
410 
411     // compare number of bytes to store with the remaining buffer size
412     uint16_t bytes_to_copy = btstack_min(connection->data_len - connection->data_offset, max_payload_size - pos);
413 
414     (void)memcpy(packet + pos, &connection->data[connection->data_offset], bytes_to_copy);
415     pos += bytes_to_copy;
416     connection->data_offset += bytes_to_copy;
417     connection->avrcp_frame_bytes_sent += pos;
418 
419     l2cap_send_prepared(connection->l2cap_signaling_cid, pos);
420 }
421 
422 static void avctp_send_reject_cmd_wrong_pid(avrcp_connection_t * connection){
423     l2cap_reserve_packet_buffer();
424     uint8_t * packet = l2cap_get_outgoing_buffer();
425 
426     // AVCTP header
427     // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
428     packet[0] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 1;
429     big_endian_store_16(packet, 1, connection->cmd_operands[0]);
430     l2cap_send_prepared(connection->l2cap_signaling_cid, 3);
431 }
432 
433 static void avrcp_target_custome_command_data_init(avrcp_connection_t * connection,
434     avrcp_command_opcode_t opcode, avrcp_command_type_t command_type,
435     avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id,
436     avrcp_pdu_id_t pdu_id, uint32_t company_id){
437 
438     connection->command_opcode = opcode;
439     connection->command_type = command_type;
440     connection->subunit_type = subunit_type;
441     connection->subunit_id = subunit_id;
442     connection->company_id = company_id << 16;
443     connection->pdu_id = pdu_id;
444     connection->data = NULL;
445     connection->data_offset = 0;
446     connection->data_len = 0;
447 }
448 
449 static void avrcp_target_vendor_dependent_response_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_pdu_id_t pdu_id){
450     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
451     connection->subunit_type   = AVRCP_SUBUNIT_TYPE_PANEL;
452     connection->subunit_id     = AVRCP_SUBUNIT_ID;
453     connection->company_id     = BT_SIG_COMPANY_ID;
454 
455     connection->command_type = command_type;
456     connection->pdu_id = pdu_id;
457     connection->data = connection->cmd_operands;
458     connection->data_offset = 0;
459     connection->avrcp_frame_bytes_sent = 0;
460     connection->state = AVCTP_W2_SEND_RESPONSE;
461 }
462 
463 static void avrcp_target_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_operation_id_t opid){
464     connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH;
465     connection->subunit_type   = AVRCP_SUBUNIT_TYPE_PANEL;
466     connection->subunit_id     = AVRCP_SUBUNIT_ID;
467 
468     connection->command_type = command_type;
469     connection->company_id = 0;
470     connection->pdu_id = AVRCP_PDU_ID_UNDEFINED;
471     connection->operation_id = opid;
472 
473     connection->data = connection->cmd_operands;
474     connection->data_offset = 0;
475     connection->data_len = 0;
476 }
477 
478 
479 static uint8_t avrcp_target_vendor_dependent_response_accept(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t status){
480     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, pdu_id);
481     connection->data_len = 1;
482     connection->data[0] = status;
483 
484     connection->accept_response = 1;
485     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
486     return ERROR_CODE_SUCCESS;
487 }
488 
489 static uint8_t avrcp_target_response_vendor_dependent_reject(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){
490     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_REJECTED, pdu_id);
491     connection->data_len = 1;
492     connection->data[0] = status;
493 
494     connection->state = AVCTP_W2_SEND_RESPONSE;
495     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
496     return ERROR_CODE_SUCCESS;
497 }
498 
499 static uint8_t avrcp_target_response_vendor_dependent_not_implemented(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){
500     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, pdu_id);
501     connection->data_len = 1;
502     connection->data[0] = event_id;
503 
504     connection->state = AVCTP_W2_SEND_RESPONSE;
505     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
506     return ERROR_CODE_SUCCESS;
507 }
508 
509 static uint8_t avrcp_target_response_vendor_dependent_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){
510     btstack_assert(value_len + 1 < AVRCP_MAX_COMMAND_PARAMETER_LENGTH);
511 
512     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id);
513     connection->data_len = 1 + value_len;
514     connection->data[0] = event_id;
515 
516     if (value && (value_len > 0)){
517         (void)memcpy(connection->data + 1, value, value_len);
518     }
519 
520     connection->state = AVCTP_W2_SEND_RESPONSE;
521     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
522     return ERROR_CODE_SUCCESS;
523 }
524 
525 static uint8_t avrcp_target_response_addressed_player_changed_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){
526     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id);
527 
528     connection->data_len = 5;
529     connection->data[0] = event_id;
530     big_endian_store_16(connection->data, 1, connection->addressed_player_id);
531     big_endian_store_16(connection->data, 3, connection->uid_counter);
532 
533     connection->state = AVCTP_W2_SEND_RESPONSE;
534     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
535     return ERROR_CODE_SUCCESS;
536 }
537 
538 static uint8_t avrcp_target_pass_through_response(uint16_t avrcp_cid, avrcp_command_type_t ctype, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
539     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
540     if (!connection){
541         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
542     }
543     avrcp_target_pass_through_command_data_init(connection, ctype, opid);
544 
545     if (operands_length == 1){
546         connection->data_len = 1;
547         connection->cmd_operands[0] = operand;
548     }
549 
550     connection->state = AVCTP_W2_SEND_RESPONSE;
551     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
552     return ERROR_CODE_SUCCESS;
553 }
554 
555 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
556     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand);
557 }
558 
559 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
560     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
561 }
562 
563 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
564     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
565 }
566 
567 uint8_t avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
568     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
569     if (!connection){
570         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
571     }
572     connection->unit_type = unit_type;
573     connection->company_id = company_id;
574     return ERROR_CODE_SUCCESS;
575 }
576 
577 uint8_t 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){
578     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
579     if (!connection){
580         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
581     }
582     connection->subunit_info_type = subunit_type;
583     connection->subunit_info_data = subunit_info_data;
584     connection->subunit_info_data_size = subunit_info_data_size;
585     return ERROR_CODE_SUCCESS;
586 }
587 
588 // TODO Review
589 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){
590     if (connection->state != AVCTP_CONNECTION_OPENED){
591         return ERROR_CODE_COMMAND_DISALLOWED;
592     }
593     connection->state = AVCTP_W2_SEND_RESPONSE;
594 
595     avrcp_target_custom_command_data_init(connection,
596                                           AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE,
597                                           AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED,
598                                           connection->company_id);
599 
600     uint8_t unit = 0;
601     connection->data = connection->cmd_operands;
602     connection->data_len = 5;
603     connection->data[0] = 0x07;
604     connection->data[1] = (connection->unit_type << 4) | unit;
605     // company id is 3 bytes long
606     big_endian_store_24(connection->data, 2, connection->company_id);
607 
608     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
609     return ERROR_CODE_SUCCESS;
610 }
611 
612 // TODO Review
613 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){
614     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
615     if (offset >= 32) return AVRCP_STATUS_INVALID_PARAMETER;
616 
617     connection->state = AVCTP_W2_SEND_RESPONSE;
618 
619     avrcp_target_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO,
620                                           AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE,
621                                           AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED,
622                                           connection->company_id);
623 
624     uint8_t page = offset / 4;
625     uint8_t extension_code = 7;
626     connection->data = connection->cmd_operands;
627     connection->data_len = 5;
628     connection->data[0] = (page << 4) | extension_code;
629 
630     // mark non-existent entries with 0xff
631     memset(&connection->cmd_operands[1], 0xFF, 4);
632     if ((connection->data != NULL) && (offset < connection->subunit_info_data_size)){
633         uint8_t bytes_to_copy = btstack_min(connection->subunit_info_data_size - offset, 4);
634         memcpy(&connection->data[1], &connection->subunit_info_data[offset], bytes_to_copy);
635     }
636 
637     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
638     return ERROR_CODE_SUCCESS;
639 }
640 
641 static uint8_t avrcp_target_response_vendor_dependent_supported_events(avrcp_connection_t * connection){
642     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES);
643     connection->state = AVCTP_W2_SEND_RESPONSE;
644 
645     uint8_t event_id;
646     uint8_t num_events = 0;
647     for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){
648         if ((connection->target_supported_notifications & (1<<event_id)) == 0){
649             continue;
650         }
651         num_events++;
652     }
653 
654     connection->data[0] = AVRCP_CAPABILITY_ID_EVENT;
655     connection->data[1] = num_events;
656     connection->data_len = 2 + num_events;
657 
658     // fill the data later directly to the L2CAP outgoing buffer
659     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
660     return ERROR_CODE_SUCCESS;
661 }
662 
663 static uint8_t avrcp_target_response_vendor_dependent_supported_companies(avrcp_connection_t * connection){
664     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES);
665     connection->state = AVCTP_W2_SEND_RESPONSE;
666 
667     connection->data[0] = AVRCP_CAPABILITY_ID_COMPANY;
668     if (connection->target_supported_companies_num == 0){
669         connection->target_supported_companies_num = 1;
670         connection->target_supported_companies = default_companies;
671     }
672 
673     connection->data[1] = connection->target_supported_companies_num;
674     connection->data_len = 2 + connection->data[1] * 3;
675 
676     // fill the data later directly to the L2CAP outgoing buffer and
677     // use Bluetooth SIG as default company
678     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
679     return ERROR_CODE_SUCCESS;
680 }
681 
682 uint8_t avrcp_target_support_event(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
683     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
684     if (!connection){
685         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
686     }
687 
688     if ((event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){
689         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
690     }
691 
692     connection->target_supported_notifications |= (1 << (uint8_t)event_id);
693     return ERROR_CODE_SUCCESS;
694 }
695 
696 uint8_t avrcp_target_support_companies(uint16_t avrcp_cid, uint8_t num_companies, const uint32_t *companies){
697     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
698     if (!connection){
699         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
700     }
701 
702     connection->target_supported_companies_num = num_companies;
703     connection->target_supported_companies     = companies;
704     return ERROR_CODE_SUCCESS;
705 }
706 
707 // TODO Review (use flags)
708 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){
709     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
710     if (!connection){
711         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
712     }
713     if (connection->state != AVCTP_CONNECTION_OPENED){
714         return ERROR_CODE_COMMAND_DISALLOWED;
715     }
716 
717     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_PLAY_STATUS);
718     connection->data_len = 9;
719     big_endian_store_32(connection->data, 0, song_length_ms);
720     big_endian_store_32(connection->data, 4, song_position_ms);
721     connection->data[8] = play_status;
722 
723     connection->state = AVCTP_W2_SEND_RESPONSE;
724     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
725     return ERROR_CODE_SUCCESS;
726 }
727 
728 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){
729     int index = attr_id - 1;
730     if (!value) return AVRCP_STATUS_INVALID_PARAMETER;
731     connection->now_playing_info[index].value = (uint8_t*)value;
732     connection->now_playing_info[index].len   = strlen(value);
733     return ERROR_CODE_SUCCESS;
734 }
735 
736 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){
737     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
738     if (!connection){
739         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
740     }
741     if (connection->playback_status == playback_status){
742         return ERROR_CODE_SUCCESS;
743     }
744 
745     connection->playback_status = playback_status;
746     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED)) {
747         connection->playback_status_changed = 1;
748         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
749     }
750     return ERROR_CODE_SUCCESS;
751 }
752 
753 uint8_t avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){
754     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
755     if (!connection){
756         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
757     }
758     if (!current_track){
759         connection->track_selected = 0;
760         connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
761         return ERROR_CODE_COMMAND_DISALLOWED;
762     }
763 
764     (void)memcpy(connection->track_id, current_track->track_id, 8);
765     connection->song_length_ms = current_track->song_length_ms;
766     connection->track_nr = current_track->track_nr;
767     connection->total_tracks = total_tracks;
768     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title);
769     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist);
770     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album);
771     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre);
772 
773     connection->track_selected = 1;
774 
775     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
776         connection->track_changed = 1;
777         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
778     }
779     return ERROR_CODE_SUCCESS;
780 }
781 
782 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){
783     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
784     if (!connection){
785         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
786     }
787     if (!track_id){
788         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
789     }
790 
791     (void)memcpy(connection->track_id, track_id, 8);
792     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
793         connection->track_changed = 1;
794         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
795     }
796     return ERROR_CODE_SUCCESS;
797 }
798 
799 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){
800     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
801     if (!connection){
802         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
803     }
804     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) {
805         connection->playing_content_changed = 1;
806         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
807     }
808     return ERROR_CODE_SUCCESS;
809 }
810 
811 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){
812     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
813     if (!connection){
814         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
815     }
816 
817     if (connection->addressed_player_id == player_id){
818         return ERROR_CODE_SUCCESS;
819     }
820 
821     connection->uid_counter = uid_counter;
822     connection->addressed_player_id = player_id;
823 
824     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) {
825         connection->addressed_player_changed = 1;
826         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
827     }
828     return ERROR_CODE_SUCCESS;
829 }
830 
831 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){
832     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
833     if (!connection){
834         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
835     }
836     if (connection->battery_status == battery_status){
837         return ERROR_CODE_SUCCESS;
838     }
839 
840     connection->battery_status = battery_status;
841 
842     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) {
843         connection->battery_status_changed = 1;
844         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
845     }
846     return ERROR_CODE_SUCCESS;
847 }
848 
849 uint8_t avrcp_target_adjust_absolute_volume(uint16_t avrcp_cid, uint8_t absolute_volume){
850     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
851     if (!connection){
852         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
853     }
854 
855     connection->absolute_volume = absolute_volume;
856     return ERROR_CODE_SUCCESS;
857 }
858 
859 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t absolute_volume){
860     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
861     if (!connection){
862         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
863     }
864     if (connection->absolute_volume == absolute_volume){
865         return ERROR_CODE_SUCCESS;
866     }
867 
868     connection->absolute_volume = absolute_volume;
869 
870     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) {
871         connection->notify_absolute_volume_changed = 1;
872         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
873     }
874     return ERROR_CODE_SUCCESS;
875 }
876 
877 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){
878     if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return;
879     connection->notifications_transaction_label[notification] = transaction_label;
880 }
881 
882 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){
883     if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0;
884     return connection->notifications_transaction_label[notification];
885 }
886 
887 static bool avcrp_operation_id_is_valid(avrcp_operation_id_t operation_id){
888     if (operation_id < AVRCP_OPERATION_ID_RESERVED_1) return true;
889 
890     if (operation_id < AVRCP_OPERATION_ID_0) return false;
891     if (operation_id < AVRCP_OPERATION_ID_RESERVED_2) return true;
892 
893     if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false;
894     if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true;
895 
896     if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false;
897     if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true;
898 
899     if (operation_id < AVRCP_OPERATION_ID_SKIP) return false;
900     if (operation_id == AVRCP_OPERATION_ID_SKIP) return true;
901 
902     if (operation_id < AVRCP_OPERATION_ID_POWER) return false;
903     if (operation_id < AVRCP_OPERATION_ID_RESERVED_4) return true;
904 
905     if (operation_id < AVRCP_OPERATION_ID_ANGLE) return false;
906     if (operation_id < AVRCP_OPERATION_ID_RESERVED_5) return true;
907 
908     if (operation_id < AVRCP_OPERATION_ID_F1) return false;
909     if (operation_id < AVRCP_OPERATION_ID_RESERVED_6) return true;
910 
911     return false;
912 }
913 
914 
915 #ifdef ENABLE_AVCTP_FRAGMENTATION
916 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){
917     // after header (transaction label and packet type)
918     uint16_t pos;
919     uint16_t bytes_to_store;
920 
921     switch (packet_type){
922         case AVCTP_START_PACKET:
923             if (size < 2) return;
924 
925             // store header
926             pos = 0;
927             connection->avctp_reassembly_buffer[pos] = packet[pos];
928             pos++;
929             connection->avctp_reassembly_size = pos;
930 
931             // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it
932             pos++;
933 
934             // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called
935 
936             bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size);
937             memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store);
938             connection->avctp_reassembly_size += bytes_to_store;
939             break;
940 
941         case AVCTP_CONTINUE_PACKET:
942         case AVCTP_END_PACKET:
943             if (size < 1) return;
944 
945             // store remaining data, ignore header
946             pos = 1;
947             bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size);
948             memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store);
949             connection->avctp_reassembly_size += bytes_to_store;
950             break;
951 
952         default:
953             return;
954     }
955 }
956 #endif
957 
958 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t * packet, uint16_t size){
959     uint8_t avctp_header = packet[0];
960     connection->transaction_id = avctp_header >> 4;
961 
962     avctp_packet_type_t avctp_packet_type = (avctp_packet_type_t) ((avctp_header & 0x0F) >> 2);
963     switch (avctp_packet_type){
964         case AVCTP_SINGLE_PACKET:
965             break;
966 
967 #ifdef ENABLE_AVCTP_FRAGMENTATION
968         case AVCTP_START_PACKET:
969         case AVCTP_CONTINUE_PACKET:
970             avctp_reassemble_message(connection, avctp_packet_type, packet, size);
971             return;
972 
973         case AVCTP_END_PACKET:
974             avctp_reassemble_message(connection, avctp_packet_type, packet, size);
975 
976             packet = connection->avctp_reassembly_buffer;
977             size   = connection->avctp_reassembly_size;
978             break;
979 #endif
980         default:
981             return;
982     }
983 
984     if (size < 6u) return;
985 
986     uint16_t pid = big_endian_read_16(packet, 1);
987 
988     if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){
989         log_info("Invalid pid 0x%02x, expected 0x%02x", pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
990         connection->reject_transport_header = true;
991         connection->invalid_pid = pid;
992         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
993         return;
994     }
995 
996     // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
997     // avrcp_subunit_id_t   subunit_id   = (avrcp_subunit_id_t) (packet[4] & 0x07);
998 
999     avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size);
1000 
1001     int pos = 6;
1002     uint16_t length;
1003     avrcp_pdu_id_t   pdu_id;
1004     // connection->data_len = 0;
1005     uint8_t offset;
1006     uint8_t operand;
1007     uint16_t event_mask;
1008     avrcp_operation_id_t operation_id;
1009 
1010     switch (opcode){
1011         case AVRCP_CMD_OPCODE_UNIT_INFO:
1012             avrcp_target_unit_info(connection);
1013             break;
1014         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:
1015             if ((size - pos) < 3) return;
1016             // page: packet[pos] >> 4,
1017             offset =  4 * (packet[pos]>>4);
1018             // extension code (fixed 7) = packet[pos] & 0x0F
1019             // 4 bytes paga data, all 0xFF
1020             avrcp_target_subunit_info(connection, offset);
1021             break;
1022 
1023         case AVRCP_CMD_OPCODE_PASS_THROUGH:
1024             if (size < 8) return;
1025             log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]);
1026             operation_id = (avrcp_operation_id_t) (packet[6] & 0x7f);
1027             operand = 0;
1028             if ((packet[7] >= 1) && (size >= 9)){
1029                 operand = packet[8];
1030             }
1031 
1032             if (avcrp_operation_id_is_valid(operation_id)){
1033                 bool button_pressed = (packet[6] & 0x80) == 0;
1034 
1035                 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand);
1036                 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid,
1037                                                 operation_id, button_pressed, packet[7], operand);
1038             } else {
1039                 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand);
1040             }
1041             break;
1042 
1043 
1044         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
1045 
1046             if (size < 13) return;
1047 
1048             // pos = 6 - company id
1049             (void)memcpy(connection->cmd_operands, &packet[pos], 3);
1050             // connection->data_len = 3;
1051             pos += 3;
1052             // pos = 9
1053             pdu_id = (avrcp_pdu_id_t) packet[pos++];
1054             // 1 - reserved
1055             pos++;
1056             // 2-3 param length,
1057             length = big_endian_read_16(packet, pos);
1058             pos += 2;
1059             // pos = 13
1060             switch (pdu_id){
1061                 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{
1062                     if ((pos + 2) > size) return;
1063                     bool ok = length == 4;
1064                     if (avrcp_target_context.set_addressed_player_callback != NULL){
1065                         uint16_t player_id = big_endian_read_16(packet, pos);
1066                         ok = avrcp_target_context.set_addressed_player_callback(player_id);
1067                     }
1068                     if (ok){
1069                         avrcp_target_vendor_dependent_response_accept(connection, pdu_id, AVRCP_STATUS_SUCCESS);
1070                     } else {
1071                         avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID);
1072                     }
1073                     break;
1074                 }
1075                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
1076                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos];
1077                     switch (capability_id){
1078                         case AVRCP_CAPABILITY_ID_EVENT:
1079                              avrcp_target_response_vendor_dependent_supported_events(connection);
1080                             break;
1081                         case AVRCP_CAPABILITY_ID_COMPANY:
1082                             avrcp_target_response_vendor_dependent_supported_companies(connection);
1083                             break;
1084                         default:
1085                             avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
1086                             break;
1087                     }
1088                     break;
1089                 }
1090                 case AVRCP_PDU_ID_GET_PLAY_STATUS:
1091                     avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
1092                     break;
1093                 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE:
1094                     if ((pos + 1) > size) return;
1095                     connection->cmd_operands[0] = packet[pos];
1096                     connection->abort_continue_response = 1;
1097                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1098                     break;
1099                 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE:
1100                     if ((pos + 1) > size) return;
1101                     if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){
1102                         avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1103                         return;
1104                     }
1105                     connection->now_playing_info_response = true;
1106                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1107                     break;
1108                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
1109                     if ((pos + 9) > size) return;
1110                     uint8_t play_identifier[8];
1111                     memset(play_identifier, 0, 8);
1112                     if (memcmp(&packet[pos], play_identifier, 8) != 0) {
1113                         avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
1114                         return;
1115                     }
1116                     pos += 8;
1117                     uint8_t attribute_count = packet[pos++];
1118                     connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE;
1119                     if (!attribute_count){
1120                         connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE;
1121                         connection->now_playing_info_attr_bitmap = 0xFE;
1122                     } else {
1123                         int i;
1124                         connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE;
1125                         connection->now_playing_info_attr_bitmap = 0;
1126                         connection->num_attributes = 0;
1127                         if ((pos + attribute_count * 4) > size) return;
1128                         for (i=0; i < attribute_count; i++){
1129                             uint32_t attr_id = big_endian_read_32(packet, pos);
1130                             connection->now_playing_info_attr_bitmap |= (1 << attr_id);
1131                             pos += 4;
1132                         }
1133                     }
1134                     log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap);
1135                     connection->num_attributes = count_set_bits_uint32(connection->now_playing_info_attr_bitmap);
1136                     connection->now_playing_info_response = true;
1137                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1138                     break;
1139                 }
1140                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
1141                     if ((pos + 1) > size) return;
1142                     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos];
1143 
1144                     avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_id);
1145 
1146                     if (event_id < AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED ||
1147                         event_id > AVRCP_NOTIFICATION_EVENT_MAX_VALUE){
1148                         avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
1149                         return;
1150                     }
1151 
1152                     switch (event_id){
1153                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:
1154                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
1155                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:
1156                         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:
1157                         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:
1158                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:
1159                         case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:
1160                         case AVRCP_NOTIFICATION_EVENT_MAX_VALUE:
1161                             avrcp_target_response_vendor_dependent_not_implemented(connection, pdu_id, event_id);
1162                             return;
1163                         default:
1164                             break;
1165                     }
1166 
1167                     event_mask = (1 << event_id);
1168                     connection->notifications_enabled |= event_mask;
1169 
1170                     switch (event_id){
1171                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:
1172                             if (connection->track_selected){
1173                                 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8);
1174                             } else {
1175                                 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8);
1176                             }
1177                             break;
1178                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:
1179                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1);
1180                             break;
1181                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:
1182                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0);
1183                             break;
1184                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:
1185                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->absolute_volume, 1);
1186                             break;
1187                         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:
1188                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1);
1189                             break;
1190                         case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED:
1191                             avrcp_target_response_addressed_player_changed_interim(connection, pdu_id, event_id);
1192                             return;
1193                         default:
1194                             btstack_assert(false);
1195                             return;
1196                     }
1197                     break;
1198                 }
1199                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: {
1200                     if ( (length != 1) || ((pos + 1) > size)){
1201                         avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1202                         break;
1203                     }
1204 
1205                     uint8_t absolute_volume = packet[pos];
1206                     if (absolute_volume < 0x80){
1207                         connection->absolute_volume = absolute_volume;
1208                     }
1209                     avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->absolute_volume);
1210                     avrcp_target_vendor_dependent_response_accept(connection, pdu_id, connection->absolute_volume);
1211                     break;
1212                 }
1213                 default:
1214                     log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id);
1215                     avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1216                     break;
1217             }
1218             break;
1219         default:
1220             log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size));
1221             break;
1222     }
1223 }
1224 
1225 static void avrcp_target_notification_init(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){
1226     btstack_assert((value_len == 0) || (value != NULL));
1227 
1228     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION);
1229     connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, notification_id);
1230 
1231     connection->data_len = 1 + value_len;
1232     connection->data[0] = notification_id;
1233     if (value_len > 0){
1234         (void)memcpy(connection->data + 1, value, value_len);
1235     }
1236 }
1237 
1238 static void avrcp_target_notification_addressed_player_changed_init(avrcp_connection_t * connection){
1239     avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION);
1240     connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED);
1241 
1242     connection->data_len = 5;
1243     connection->data[0] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
1244     big_endian_store_16(connection->data, 1, connection->addressed_player_id);
1245     big_endian_store_16(connection->data, 3, connection->uid_counter);
1246 }
1247 
1248 
1249 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){
1250     if (notification_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX || notification_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX){
1251         return;
1252     }
1253     connection->notifications_enabled &= ~(1 << notification_id);
1254     connection->notifications_transaction_label[notification_id] = 0;
1255 }
1256 
1257 static void avrcp_request_next_avctp_segment(avrcp_connection_t * connection){
1258     // AVCTP
1259     switch (connection->avctp_packet_type){
1260         case AVCTP_END_PACKET:
1261         case AVCTP_SINGLE_PACKET:
1262             connection->avrcp_frame_bytes_sent = 0;
1263             connection->data_offset = 0;
1264             connection->data_len = 0;
1265             connection->state = AVCTP_CONNECTION_OPENED;
1266             break;
1267         default:
1268             connection->state = AVCTP_W2_SEND_RESPONSE;
1269             avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1270             break;
1271     }
1272 }
1273 
1274 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1275     avrcp_connection_t * connection;
1276     bool send_response = true;
1277     avrcp_notification_event_id_t notification_id = AVRCP_NOTIFICATION_EVENT_NONE;
1278 
1279     switch (packet_type){
1280         case L2CAP_DATA_PACKET:
1281             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel);
1282             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1283             return;
1284 
1285         case HCI_EVENT_PACKET:
1286             if (hci_event_packet_get_type(packet) != L2CAP_EVENT_CAN_SEND_NOW){
1287                 return;
1288             }
1289 
1290             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel);
1291             if (connection == NULL){
1292                 return;
1293             }
1294 
1295             // START AVCTP
1296             if (connection->reject_transport_header){
1297                 connection->reject_transport_header = false;
1298                 avctp_send_reject_cmd_wrong_pid(connection);
1299                 connection->state = AVCTP_CONNECTION_OPENED;
1300                 return;
1301             }
1302             // END AVCTP
1303 
1304             if (connection->accept_response){
1305                 connection->accept_response = 0;
1306                 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE);
1307                 break;
1308             }
1309 
1310             if (connection->abort_continue_response){
1311                 connection->abort_continue_response = 0;
1312                 connection->now_playing_info_response = false;
1313                 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE);
1314                 break;
1315             }
1316 
1317             if (connection->now_playing_info_response){
1318                 connection->now_playing_info_response = false;
1319                 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES);
1320                 connection->data_len = avrcp_now_playing_info_value_len_with_headers(connection);
1321                 break;
1322             }
1323 
1324             if (connection->track_changed){
1325                 connection->track_changed = 0;
1326                 notification_id = AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED;
1327                 avrcp_target_notification_init(connection, notification_id, connection->track_id, 8);
1328                 break;
1329             }
1330 
1331             if (connection->playback_status_changed){
1332                 connection->playback_status_changed = 0;
1333                 notification_id = AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED;
1334                 uint8_t playback_status = (uint8_t) connection->playback_status;
1335                 avrcp_target_notification_init(connection, notification_id, &playback_status, 1);
1336                 break;
1337             }
1338 
1339             if (connection->playing_content_changed){
1340                 connection->playing_content_changed = 0;
1341                 notification_id = AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED;
1342                 avrcp_target_notification_init(connection, notification_id, NULL, avrcp_now_playing_info_value_len_with_headers(connection));
1343                 break;
1344             }
1345 
1346             if (connection->battery_status_changed){
1347                 connection->battery_status_changed = 0;
1348                 notification_id = AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED;
1349                 avrcp_target_notification_init(connection, notification_id, (uint8_t *)&connection->battery_status, 1);
1350                 break;
1351             }
1352 
1353             if (connection->notify_absolute_volume_changed){
1354                 connection->notify_absolute_volume_changed = 0;
1355                 notification_id = AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED;
1356                 avrcp_target_notification_init(connection, notification_id, &connection->absolute_volume, 1);
1357                 break;
1358             }
1359 
1360             if (connection->addressed_player_changed){
1361                 connection->addressed_player_changed = 0;
1362                 notification_id = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
1363                 avrcp_target_notification_addressed_player_changed_init(connection);
1364                 break;
1365             }
1366 
1367             send_response = false;
1368             break;
1369 
1370         default:
1371             return;
1372     }
1373 
1374     if (connection != NULL && (send_response || connection->state == AVCTP_W2_SEND_RESPONSE)){
1375         avrcp_send_response_with_avctp_fragmentation(connection);
1376         avrcp_target_reset_notification(connection, notification_id);
1377         avrcp_request_next_avctp_segment(connection);
1378     }
1379 }
1380 
1381 void avrcp_target_init(void){
1382     avrcp_target_context.role = AVRCP_TARGET;
1383     avrcp_target_context.packet_handler = avrcp_target_packet_handler;
1384     avrcp_register_target_packet_handler(&avrcp_target_packet_handler);
1385 }
1386 
1387 void avrcp_target_deinit(void){
1388     memset(&avrcp_target_context, 0, sizeof(avrcp_context_t));
1389 }
1390 
1391 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
1392     btstack_assert(callback != NULL);
1393     avrcp_target_context.avrcp_callback = callback;
1394 }
1395 
1396 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){
1397     btstack_assert(callback != NULL);
1398     avrcp_target_context.set_addressed_player_callback = callback;
1399 }
1400 
1401 
1402