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