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