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