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