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