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