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