xref: /btstack/src/classic/avrcp_target.c (revision 7a4d61a3839cca16d148d13598e5b3ada33e2c26)
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_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){
401 
402     // company id is 3 bytes long
403     int pos = connection->cmd_operands_length;
404     connection->cmd_operands[pos++] = pdu_id;
405     connection->cmd_operands[pos++] = 0;
406     // param length
407     big_endian_store_16(connection->cmd_operands, pos, 1 + value_len);
408     pos += 2;
409     connection->cmd_operands[pos++] = event_id;
410     if (value && (value_len > 0)){
411         (void)memcpy(connection->cmd_operands + pos, value, value_len);
412         pos += value_len;
413     }
414     connection->cmd_operands_length = pos;
415     connection->state = AVCTP_W2_SEND_RESPONSE;
416     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
417     return ERROR_CODE_SUCCESS;
418 }
419 
420 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){
421     avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
422 
423     // company id is 3 bytes long
424     int pos = connection->cmd_operands_length;
425     connection->cmd_operands[pos++] = pdu_id;
426     connection->cmd_operands[pos++] = 0;
427     // param length
428     big_endian_store_16(connection->cmd_operands, pos, 5);
429     pos += 2;
430     connection->cmd_operands[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
431     big_endian_read_16( &connection->cmd_operands[pos], connection->addressed_player_id);
432     pos += 2;
433     big_endian_read_16( &connection->cmd_operands[pos], connection->uid_counter);
434     pos += 2;
435 
436     connection->cmd_operands_length = pos;
437     connection->state = AVCTP_W2_SEND_RESPONSE;
438     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
439     return ERROR_CODE_SUCCESS;
440 }
441 
442 
443 // static uint8_t avrcp_target_response_vendor_dependent_changed(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){
444 //     avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_SUBUNIT_TYPE_PANEL, AVRCP_SUBUNIT_ID, AVRCP_CMD_OPCODE_VENDOR_DEPENDENT);
445 //     // company id is 3 bytes long
446 //     int pos = connection->cmd_operands_length;
447 //     connection->cmd_operands[pos++] = pdu_id;
448 //     connection->cmd_operands[pos++] = 0;
449 //     // param length
450 //     big_endian_store_16(connection->cmd_operands, pos, 1);
451 //     pos += 2;
452 //     connection->cmd_operands[pos++] = event_id;
453 //     connection->cmd_operands_length = pos;
454 
455 //     connection->state = AVCTP_W2_SEND_RESPONSE;
456 //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
457 //     return ERROR_CODE_SUCCESS;
458 // }
459 
460 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){
461     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
462     if (!connection){
463         log_error("Could not find a connection.");
464         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
465     }
466     avrcp_target_response_setup(connection, cmd_type, AVRCP_SUBUNIT_TYPE_PANEL, AVRCP_SUBUNIT_ID, AVRCP_CMD_OPCODE_PASS_THROUGH);
467 
468     int pos = 0;
469     connection->cmd_operands[pos++] = opid;
470     connection->cmd_operands[pos++] = operands_length;
471     if (operands_length == 1){
472         connection->cmd_operands[pos++] = operand;
473     }
474     connection->cmd_operands_length = pos;
475 
476     connection->state = AVCTP_W2_SEND_RESPONSE;
477     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
478     return ERROR_CODE_SUCCESS;
479 }
480 
481 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
482     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand);
483 }
484 
485 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
486     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
487 }
488 
489 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
490     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
491 }
492 
493 void avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
494     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
495     if (!connection){
496         log_error("avrcp_target_operation_reject: could not find a connection.");
497         return;
498     }
499     connection->unit_type = unit_type;
500     connection->company_id = company_id;
501 }
502 
503 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){
504     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
505     if (!connection){
506         log_error("avrcp_target_operation_reject: could not find a connection.");
507         return;
508     }
509     connection->subunit_info_type = subunit_type;
510     connection->subunit_info_data = subunit_info_data;
511     connection->subunit_info_data_size = subunit_info_data_size;
512 }
513 
514 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){
515     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
516 
517     uint8_t unit = 0;
518     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
519     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
520     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
521     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
522 
523     connection->cmd_operands_length = 5;
524     connection->cmd_operands[0] = 0x07;
525     connection->cmd_operands[1] = (connection->unit_type << 4) | unit;
526     // company id is 3 bytes long
527     big_endian_store_32(connection->cmd_operands, 2, connection->company_id);
528 
529     connection->state = AVCTP_W2_SEND_RESPONSE;
530     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
531     return ERROR_CODE_SUCCESS;
532 }
533 
534 
535 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){
536     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
537     if ((offset - 4) > connection->subunit_info_data_size) return AVRCP_STATUS_INVALID_PARAMETER;
538 
539     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
540     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
541     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
542     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
543     // printf("avrcp_target_subunit_info  subunit_type %d\n", connection->subunit_type);
544 
545     uint8_t page = offset / 4;
546     uint8_t extension_code = 7;
547     connection->cmd_operands_length = 5;
548     connection->cmd_operands[0] = (page << 4) | extension_code;
549 
550     (void)memcpy(connection->cmd_operands + 1,
551                  connection->subunit_info_data + offset, 4);
552 
553     connection->state = AVCTP_W2_SEND_RESPONSE;
554     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
555     return ERROR_CODE_SUCCESS;
556 }
557 
558 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){
559     *out_connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
560     if (!*out_connection){
561         log_error("avrcp tartget: could not find a connection.");
562         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
563     }
564 
565     if ((*out_connection)->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
566     (*out_connection)->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
567     (*out_connection)->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
568     (*out_connection)->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
569     (*out_connection)->subunit_id      = AVRCP_SUBUNIT_ID;
570 
571     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = pdu_id;
572     // reserved
573     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = 0;
574     // param length
575     big_endian_store_16((*out_connection)->cmd_operands, (*out_connection)->cmd_operands_length, param_length);
576     (*out_connection)->cmd_operands_length += 2;
577     return ERROR_CODE_SUCCESS;
578 }
579 
580 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){
581     avrcp_connection_t * connection = NULL;
582     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_CAPABILITIES, 2+size);
583     if (status != ERROR_CODE_SUCCESS) return status;
584 
585     connection->cmd_operands[connection->cmd_operands_length++] = capability_id;
586     connection->cmd_operands[connection->cmd_operands_length++] = capabilities_num;
587     (void)memcpy(connection->cmd_operands + connection->cmd_operands_length,
588                  capabilities, size);
589     connection->cmd_operands_length += size;
590 
591     connection->state = AVCTP_W2_SEND_RESPONSE;
592     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
593     return ERROR_CODE_SUCCESS;
594 }
595 
596 uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
597     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, capabilities_num, capabilities, size);
598 }
599 
600 uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size ){
601     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, capabilities_num, capabilities, size);
602 }
603 
604 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){
605     avrcp_connection_t * connection = NULL;
606     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_PLAY_STATUS, 11);
607     if (status != ERROR_CODE_SUCCESS) return status;
608 
609     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_length_ms);
610     connection->cmd_operands_length += 4;
611     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_position_ms);
612     connection->cmd_operands_length += 4;
613     connection->cmd_operands[connection->cmd_operands_length++] = play_status;
614 
615     connection->state = AVCTP_W2_SEND_RESPONSE;
616     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
617     return ERROR_CODE_SUCCESS;
618 }
619 
620 static uint8_t avrcp_target_now_playing_info(avrcp_connection_t * connection){
621     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
622     connection->now_playing_info_response = 1;
623     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
624     connection->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
625     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
626     connection->subunit_id      = AVRCP_SUBUNIT_ID;
627 
628     connection->state = AVCTP_W2_SEND_RESPONSE;
629     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
630     return ERROR_CODE_SUCCESS;
631 }
632 
633 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){
634     int index = attr_id - 1;
635     if (!value) return AVRCP_STATUS_INVALID_PARAMETER;
636     connection->now_playing_info[index].value = (uint8_t*)value;
637     connection->now_playing_info[index].len   = strlen(value);
638     // printf("store %lu bytes, %s\n",  strlen(value), value);
639     return ERROR_CODE_SUCCESS;
640 }
641 
642 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){
643     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
644     if (!connection){
645         log_error("avrcp_unit_info: could not find a connection.");
646         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
647     }
648     if (connection->playback_status == playback_status) return ERROR_CODE_SUCCESS;
649 
650     connection->playback_status = playback_status;
651     connection->playback_status_changed = 1;
652     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
653     return ERROR_CODE_SUCCESS;
654 }
655 
656 void avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){
657     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
658     if (!connection){
659         log_error("avrcp_unit_info: could not find a connection. cid 0x%02x\n", avrcp_cid);
660         return;
661     }
662     if (!current_track){
663         connection->track_selected = 0;
664         connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
665         return;
666     }
667     (void)memcpy(connection->track_id, current_track->track_id, 8);
668     connection->song_length_ms = current_track->song_length_ms;
669     connection->track_nr = current_track->track_nr;
670     connection->total_tracks = total_tracks;
671     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title);
672     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist);
673     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album);
674     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre);
675     connection->track_selected = 1;
676 
677     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
678         connection->track_changed = 1;
679         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
680     }
681     return;
682 }
683 
684 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){
685     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
686     if (!connection){
687         log_error("avrcp_target_track_changed: could not find connection.");
688         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
689     }
690     if (!track_id) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
691 
692     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
693         connection->track_changed = 1;
694         (void)memcpy(connection->track_id, track_id, 8);
695         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
696     }
697     return ERROR_CODE_SUCCESS;
698 }
699 
700 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){
701     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
702     if (!connection){
703         log_error("avrcp_target_playing_content_changed: could not find a connection.");
704         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
705     }
706     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) {
707         connection->playing_content_changed = 1;
708         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
709     }
710     return ERROR_CODE_SUCCESS;
711 }
712 
713 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){
714     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
715     if (!connection){
716         log_error("avrcp_unit_info: could not find a connection.");
717         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
718     }
719     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) {
720         // printf("send AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED\n");
721         // connection->addressed_player_changed = 1;
722         connection->uid_counter = uid_counter;
723         connection->addressed_player_id = player_id;
724         // avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
725     }
726     return ERROR_CODE_SUCCESS;
727 }
728 
729 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){
730     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
731     if (!connection){
732         log_error("avrcp_unit_info: could not find a connection.");
733         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
734     }
735     if (connection->battery_status == battery_status) return ERROR_CODE_SUCCESS;
736     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) {
737         connection->battery_status = battery_status;
738         connection->battery_status_changed = 1;
739         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
740     }
741     return ERROR_CODE_SUCCESS;
742 }
743 
744 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t volume_percentage){
745     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
746     if (!connection){
747         log_error("avrcp_unit_info: could not find a connection.");
748         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
749     }
750     connection->volume_percentage = volume_percentage;
751     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) {
752         connection->notify_volume_percentage_changed = 1;
753         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
754     }
755     return ERROR_CODE_SUCCESS;
756 }
757 
758 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){
759     if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return;
760     connection->notifications_transaction_label[notification] = transaction_label;
761 }
762 
763 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){
764     if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0;
765     return connection->notifications_transaction_label[notification];
766 }
767 
768 static uint8_t * avrcp_get_company_id(uint8_t *packet, uint16_t size){
769     UNUSED(size);
770     return packet + 6;
771 }
772 
773 static uint8_t * avrcp_get_pdu(uint8_t *packet, uint16_t size){
774     UNUSED(size);
775     return packet + 9;
776 }
777 
778 static uint8_t avrcp_is_receive_pass_through_cmd(uint8_t operation_id){
779     return operation_id & 0x80;
780 }
781 
782 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
783     UNUSED(connection);
784     UNUSED(packet);
785     UNUSED(size);
786 
787     // uint8_t opcode;
788     uint16_t pid = 0;
789     uint8_t transport_header = packet[0];
790     connection->transaction_label = transport_header >> 4;
791     // uint8_t frame_type = (transport_header & 0x03) >> 1;
792     avrcp_packet_type_t packet_type = (avrcp_packet_type_t) ((transport_header & 0x0F) >> 2);
793     switch (packet_type){
794         case AVRCP_SINGLE_PACKET:
795             pid =  big_endian_read_16(packet, 1);
796             break;
797         case AVRCP_START_PACKET:
798             pid =  big_endian_read_16(packet, 2);
799             break;
800         default:
801             break;
802     }
803 
804     switch (packet_type){
805         case AVRCP_SINGLE_PACKET:
806         case AVRCP_START_PACKET:
807             if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){
808                 log_info("Invalid pid 0x%02x, expected 0x%02x", connection->invalid_pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
809                 connection->reject_transport_header = 1;
810                 connection->invalid_pid = pid;
811                 connection->transport_header = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2 ) | (AVRCP_RESPONSE_FRAME << 1) | 1;
812                 connection->state = AVCTP_W2_SEND_RESPONSE;
813                 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
814                 return;
815             }
816             break;
817         default:
818             break;
819     }
820 
821     // avrcp_command_type_t ctype = (avrcp_command_type_t) packet[3];
822     // uint8_t byte_value = packet[4];
823     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
824     avrcp_subunit_id_t   subunit_id   = (avrcp_subunit_id_t) (packet[4] & 0x07);
825     // opcode = packet[pos++];
826     // printf("    Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n",
827     //     transport_header, transaction_label, packet_type, frame_type, ipid, pid);
828     // printf_hexdump(packet+pos, size-pos);
829 
830     avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size);
831     uint8_t * company_id = avrcp_get_company_id(packet, size);
832     uint8_t * pdu = avrcp_get_pdu(packet, size);
833     // uint16_t param_length = big_endian_read_16(pdu, 2);
834 
835     int pos = 4;
836     uint16_t length;
837     avrcp_pdu_id_t   pdu_id;
838     connection->cmd_operands_length = 0;
839 
840     switch (opcode){
841         case AVRCP_CMD_OPCODE_UNIT_INFO:
842             avrcp_target_unit_info(connection);
843             break;
844         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
845             uint8_t offset =  4 * (packet[pos+2]>>4);
846             avrcp_target_subunit_info(connection, offset);
847             break;
848         }
849         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
850             log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d, operand %d", packet[6], packet[7], packet[8]);
851             avrcp_operation_id_t operation_id = (avrcp_operation_id_t) packet[6];
852 
853             if (avrcp_is_receive_pass_through_cmd(operation_id)){
854                 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7F);
855                 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
856                 break;
857             }
858 
859             switch (operation_id){
860                 case AVRCP_OPERATION_ID_PLAY:
861                 case AVRCP_OPERATION_ID_PAUSE:
862                 case AVRCP_OPERATION_ID_STOP:
863                 case AVRCP_OPERATION_ID_VOLUME_UP:
864                 case AVRCP_OPERATION_ID_VOLUME_DOWN:
865                 case AVRCP_OPERATION_ID_REWIND:
866                 case AVRCP_OPERATION_ID_FAST_FORWARD:
867                 case AVRCP_OPERATION_ID_FORWARD:
868                 case AVRCP_OPERATION_ID_BACKWARD:
869                 case AVRCP_OPERATION_ID_SKIP:
870                 case AVRCP_OPERATION_ID_MUTE:
871                 case AVRCP_OPERATION_ID_CHANNEL_UP:
872                 case AVRCP_OPERATION_ID_CHANNEL_DOWN:
873                 case AVRCP_OPERATION_ID_SELECT:
874                 case AVRCP_OPERATION_ID_UP:
875                 case AVRCP_OPERATION_ID_DOWN:
876                 case AVRCP_OPERATION_ID_LEFT:
877                 case AVRCP_OPERATION_ID_RIGHT:
878                 case AVRCP_OPERATION_ID_ROOT_MENU:
879                     avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
880                     avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, operation_id, packet[7], packet[8]);
881                     break;
882                 case AVRCP_OPERATION_ID_UNDEFINED:
883                     avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
884                     return;
885                 default:
886                     avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], packet[8]);
887                     return;
888             }
889             break;
890         }
891 
892         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
893             pdu_id = (avrcp_pdu_id_t) pdu[0];
894             // 1 - reserved
895             // 2-3 param length,
896             length = big_endian_read_16(pdu, 2);
897             (void)memcpy(connection->cmd_operands, company_id, 3);
898             connection->cmd_operands_length = 3;
899             switch (pdu_id){
900                 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{
901                     if (length == 0){
902                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID);
903                         break;
904                     }
905                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_SUCCESS);
906                     break;
907                 }
908                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
909                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) pdu[pos];
910                     switch (capability_id){
911                         case AVRCP_CAPABILITY_ID_EVENT:
912                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY);
913                             break;
914                         case AVRCP_CAPABILITY_ID_COMPANY:
915                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY);
916                             break;
917                         default:
918                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
919                             break;
920                     }
921                     break;
922                 }
923                 case AVRCP_PDU_ID_GET_PLAY_STATUS:
924                     avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
925                     break;
926                 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE:
927                     connection->cmd_operands[0] = pdu[4];
928                     connection->abort_continue_response = 1;
929                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
930                     break;
931                 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE:
932                     if (pdu[4] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){
933                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
934                         return;
935                     }
936                     connection->now_playing_info_response = 1;
937                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
938                     break;
939                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
940                     uint8_t play_identifier[8];
941                     memset(play_identifier, 0, 8);
942                     if (memcmp(pdu+pos, play_identifier, 8) != 0) {
943                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
944                         return;
945                     }
946                     pos += 8;
947                     uint8_t attribute_count = pdu[pos++];
948                     // printf("AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES attribute count %d\n", attribute_count);
949                     connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE;
950                     if (!attribute_count){
951                         connection->now_playing_info_attr_bitmap = 0xFE;
952                     } else {
953                         int i;
954                         connection->now_playing_info_attr_bitmap = 0;
955                         for (i=0; i < attribute_count; i++){
956                             uint16_t attr_id = big_endian_read_16(pdu, pos);
957                             pos += 2;
958                             connection->now_playing_info_attr_bitmap |= (1 << attr_id);
959                         }
960                     }
961                     log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap);
962                     avrcp_target_now_playing_info(connection);
963                     break;
964                 }
965                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
966                     // 0 - pdu id
967                     // 1 - reserved
968                     // 2-3 param length
969                     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) pdu[4];
970                     uint16_t event_mask = (1 << event_id);
971                     avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_label);
972 
973                     switch (event_id){
974                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:
975                             connection->notifications_enabled |= event_mask;
976                             avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
977                             if (connection->track_selected){
978                                 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8);
979                             } else {
980                                 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8);
981                             }
982                             break;
983                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:
984                             connection->notifications_enabled |= event_mask;
985                             avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
986                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1);
987                             break;
988                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:
989                             connection->notifications_enabled |= event_mask;
990                             avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
991                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0);
992                             break;
993                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:
994                             connection->notify_volume_percentage_changed = 0;
995                             connection->notifications_enabled |= event_mask;
996                             avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
997                             avrcp_target_response_vendor_dependent_interim(connection, 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_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode);
1002                             avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1);
1003                             break;
1004                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:
1005                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
1006                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:
1007                             avrcp_target_response_not_implemented(connection, subunit_type, subunit_id, opcode, pdu_id, event_id);
1008                             return;
1009                         case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED:
1010                             connection->notifications_enabled |= event_mask;
1011                             avrcp_target_response_addressed_player_changed_interim(connection, subunit_type, subunit_id, opcode, pdu_id);
1012                             return;
1013                         default:
1014                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
1015                             return;
1016                     }
1017                     break;
1018                 }
1019                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: {
1020                     if (length != 1){
1021                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1022                         break;
1023                     }
1024 
1025                     uint8_t absolute_volume = pdu[4];
1026                     if (absolute_volume < 0x80){
1027                         connection->volume_percentage = absolute_volume;
1028                     }
1029                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, connection->volume_percentage);
1030                     avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->volume_percentage);
1031                     // avrcp_target_volume_changed(connection->avrcp_cid, connection->volume_percentage);
1032                     break;
1033                 }
1034                 default:
1035                     log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id);
1036                     avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
1037                     break;
1038             }
1039             break;
1040         default:
1041             log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size));
1042             break;
1043     }
1044 }
1045 
1046 #if 0
1047 static int avrcp_target_send_addressed_player_changed_notification(uint16_t cid, avrcp_connection_t * connection, uint16_t uid, uint16_t uid_counter){
1048     if (!connection){
1049         log_error("avrcp tartget: could not find a connection.");
1050         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1051     }
1052     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1053     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
1054     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
1055     connection->subunit_id      = AVRCP_SUBUNIT_ID;
1056 
1057     uint16_t pos = 0;
1058     l2cap_reserve_packet_buffer();
1059     uint8_t * packet = l2cap_get_outgoing_buffer();
1060 
1061     connection->packet_type = AVRCP_SINGLE_PACKET;
1062     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
1063     // Profile IDentifier (PID)
1064     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
1065     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
1066 
1067     // command_type
1068     packet[pos++] = connection->command_type;
1069     // subunit_type | subunit ID
1070     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
1071     // opcode
1072     packet[pos++] = (uint8_t)connection->command_opcode;
1073 
1074     // company id is 3 bytes long
1075     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
1076     pos += 3;
1077 
1078     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
1079     packet[pos++] = 0;
1080     big_endian_store_16(packet, pos, 5);
1081     pos += 2;
1082     packet[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
1083     big_endian_store_16(packet, pos, uid);
1084     pos += 2;
1085     big_endian_store_16(packet, pos, uid_counter);
1086     pos += 2;
1087 
1088     connection->wait_to_send = 0;
1089     return l2cap_send_prepared(cid, pos);
1090 }
1091 #endif
1092 
1093 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){
1094     if (!connection){
1095         log_error("avrcp tartget: could not find a connection.");
1096         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1097     }
1098 
1099     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1100     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
1101     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
1102     connection->subunit_id      = AVRCP_SUBUNIT_ID;
1103     connection->transaction_label = avrcp_target_get_transaction_label_for_notification(connection, notification_id);
1104 
1105     uint16_t pos = 0;
1106     l2cap_reserve_packet_buffer();
1107     uint8_t * packet = l2cap_get_outgoing_buffer();
1108     uint16_t  size   = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
1109 
1110     connection->packet_type = AVRCP_SINGLE_PACKET;
1111     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
1112     // Profile IDentifier (PID)
1113     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
1114     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
1115 
1116     // command_type
1117     packet[pos++] = connection->command_type;
1118     // subunit_type | subunit ID
1119     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
1120     // opcode
1121     packet[pos++] = (uint8_t)connection->command_opcode;
1122 
1123     // company id is 3 bytes long
1124     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
1125     pos += 3;
1126 
1127     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
1128     packet[pos++] = 0;
1129     uint16_t remainig_outgoing_buffer_size = size - pos - 2;
1130 
1131     uint16_t caped_value_len = btstack_min(value_len + 1, remainig_outgoing_buffer_size);
1132     big_endian_store_16(packet, pos, caped_value_len);
1133     pos += 2;
1134     packet[pos++] = notification_id;
1135     (void)memcpy(packet + pos, value, caped_value_len - 1);
1136     pos += caped_value_len - 1;
1137     connection->wait_to_send = 0;
1138     return l2cap_send_prepared(cid, pos);
1139 }
1140 
1141 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){
1142     if (!connection){
1143         log_error("avrcp tartget: could not find a connection.");
1144         return;
1145     }
1146     connection->notifications_enabled &= ~(1 << notification_id);
1147     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1148     avrcp_target_set_transaction_label_for_notification(connection, notification_id, 0);
1149 }
1150 
1151 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1152     avrcp_connection_t * connection;
1153     switch (packet_type) {
1154         case L2CAP_DATA_PACKET:
1155             connection = get_avrcp_connection_for_l2cap_signaling_cid(AVRCP_TARGET, channel);
1156             if (!connection) break;
1157             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1158             break;
1159         case HCI_EVENT_PACKET:
1160             switch (hci_event_packet_get_type(packet)){
1161                 case HCI_EVENT_AVRCP_META:
1162                     // forward to app
1163                     (*avrcp_target_context.avrcp_callback)(packet_type, channel, packet, size);
1164                     break;
1165                 case L2CAP_EVENT_CAN_SEND_NOW:{
1166                     connection = get_avrcp_connection_for_l2cap_signaling_cid(AVRCP_TARGET, channel);
1167                     if (!connection) {
1168                         log_error("Connection not found\n");
1169                         break;
1170                     }
1171 
1172                     if (connection->accept_response){
1173                         connection->accept_response = 0;
1174                         avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
1175                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1176                         break;
1177                     }
1178                     if (connection->abort_continue_response){
1179                         connection->abort_continue_response = 0;
1180                         connection->now_playing_info_response = 0;
1181                         avrcp_target_abort_continue_response(connection->l2cap_signaling_cid, connection);
1182                         break;
1183                     }
1184 
1185                     if (connection->now_playing_info_response){
1186                         connection->now_playing_info_response = 0;
1187                         avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1188                         break;
1189                     }
1190 
1191                     if (connection->track_changed){
1192                         connection->track_changed = 0;
1193                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED, connection->track_id, 8);
1194                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED);
1195                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1196                         break;
1197                     }
1198 
1199                     if (connection->playback_status_changed){
1200                         connection->playback_status_changed = 0;
1201                         uint8_t playback_status = (uint8_t) connection->playback_status;
1202                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED, &playback_status, 1);
1203                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED);
1204                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1205                         break;
1206                     }
1207 
1208                     // if (connection->addressed_player_changed){
1209                     //     connection->playback_status_changed = 0;
1210                     //     avrcp_target_send_addressed_player_changed_notification(connection->l2cap_signaling_cid, connection, connection->addressed_player_id, connection->uid_counter);
1211                     //     avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED);
1212                     //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1213                     //     break;
1214                     // }
1215 
1216                     if (connection->playing_content_changed){
1217                         connection->playing_content_changed = 0;
1218                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED, NULL, 0);
1219                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED);
1220                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1221                         break;
1222                     }
1223 
1224                     if (connection->battery_status_changed){
1225                         connection->battery_status_changed = 0;
1226                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED, (uint8_t *)&connection->battery_status, 1);
1227                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED);
1228                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1229                         break;
1230                     }
1231 
1232                     if (connection->notify_volume_percentage_changed){
1233                         // printf("emit new volume %d\n", connection->volume_percentage);
1234                         connection->notify_volume_percentage_changed = 0;
1235                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED, &connection->volume_percentage, 1);
1236                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED);
1237                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1238                         break;
1239                     }
1240 
1241                     if (connection->reject_transport_header){
1242                         connection->state = AVCTP_CONNECTION_OPENED;
1243                         connection->reject_transport_header = 0;
1244                         l2cap_reserve_packet_buffer();
1245                         uint8_t * out_buffer = l2cap_get_outgoing_buffer();
1246                         out_buffer[0] = connection->transport_header;
1247                         big_endian_store_16(out_buffer, 1, connection->invalid_pid);
1248                         l2cap_send_prepared(connection->l2cap_signaling_cid, 3);
1249                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1250                         break;
1251                     }
1252 
1253                     switch (connection->state){
1254                         case AVCTP_W2_SEND_RESPONSE:
1255                             connection->state = AVCTP_CONNECTION_OPENED;
1256                             // if (connection->now_playing_info_response){
1257                             //     connection->now_playing_info_response = 0;
1258                             //     avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1259                             //     break;
1260                             // }
1261                             avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
1262                             avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1263                             return;
1264                         default:
1265                             break;
1266                     }
1267 
1268                     break;
1269             }
1270             default:
1271                 break;
1272         }
1273         default:
1274             break;
1275     }
1276 }
1277 
1278 void avrcp_target_init(void){
1279     avrcp_init();
1280     avrcp_target_context.role = AVRCP_TARGET;
1281     avrcp_target_context.packet_handler = avrcp_target_packet_handler;
1282     avrcp_register_target_packet_handler(&avrcp_target_packet_handler);
1283 }
1284 
1285 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
1286     if (callback == NULL){
1287         log_error("avrcp_register_packet_handler called with NULL callback");
1288         return;
1289     }
1290     avrcp_target_context.avrcp_callback = callback;
1291 }
1292 
1293 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){
1294     return avrcp_connect(AVRCP_TARGET, bd_addr, &avrcp_target_context, avrcp_cid);
1295 }
1296 
1297 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){
1298     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(AVRCP_TARGET, avrcp_cid);
1299     if (!connection){
1300         log_error("avrcp_target_disconnect: could not find a connection.");
1301         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1302     }
1303     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1304     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
1305     return ERROR_CODE_SUCCESS;
1306 }
1307 
1308