xref: /btstack/src/classic/avrcp_controller.c (revision 589c77124f34937159827d1e826ad710eb9f85db)
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 BLUEKITCHEN
24  * GMBH 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_controller.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 #include <inttypes.h>
43 
44 #include "classic/avrcp.h"
45 #include "classic/avrcp_controller.h"
46 
47 #include "bluetooth_sdp.h"
48 #include "btstack_debug.h"
49 #include "btstack_event.h"
50 #include "btstack_util.h"
51 #include "l2cap.h"
52 
53 #define AVRCP_CMD_BUFFER_SIZE 30
54 #define AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE 512
55 
56 // made public in avrcp_controller.h
57 avrcp_context_t avrcp_controller_context;
58 
59 static uint8_t avrcp_controller_calc_next_transaction_label(uint8_t current_transaction_label){
60     current_transaction_label++;
61     if (current_transaction_label == 16){
62         current_transaction_label = 1;
63     }
64     return current_transaction_label;
65 }
66 
67 static uint8_t avrcp_controller_get_next_transaction_label(avrcp_connection_t * connection){
68     connection->transaction_id_counter = avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter);
69     return connection->transaction_id_counter;
70 }
71 
72 static bool avrcp_controller_is_transaction_id_valid(avrcp_connection_t * connection, uint8_t transaction_id){
73     uint8_t delta = ((int8_t) transaction_id - connection->last_confirmed_transaction_id) & 0x0f;
74     return delta < 15;
75 }
76 
77 static uint16_t avrcp_get_max_payload_size_for_avctp_packet_type(avrcp_connection_t * connection, avctp_packet_type_t avctp_packet_type){
78     uint16_t max_frame_size = btstack_min(l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid), AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE);
79 
80     switch (avctp_packet_type){
81         case AVCTP_SINGLE_PACKET:
82             return max_frame_size - 3;
83         case AVCTP_START_PACKET:
84             return max_frame_size - 4;
85         case AVCTP_CONTINUE_PACKET:
86         case AVCTP_END_PACKET:
87             return max_frame_size - 1;
88         default:
89             btstack_assert(false);
90             return 0;
91     }
92 }
93 
94 static int avrcp_controller_supports_browsing(uint16_t controller_supported_features){
95     return controller_supported_features & AVRCP_FEATURE_MASK_BROWSING;
96 }
97 
98 static void avrcp_controller_emit_notification_complete(avrcp_connection_t * connection, uint8_t status, uint8_t event_id, bool enabled){
99     uint8_t event[8];
100     uint8_t pos = 0;
101     event[pos++] = HCI_EVENT_AVRCP_META;
102     event[pos++] = sizeof(event) - 2;
103     event[pos++] = AVRCP_SUBEVENT_NOTIFICATION_STATE;
104     little_endian_store_16(event, pos, connection->avrcp_cid);
105     pos += 2;
106     event[pos++] = status;
107     event[pos++] = enabled ? 1 : 0;
108     event[pos++] = event_id;
109     UNUSED(pos);
110     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
111 }
112 
113 static void avrcp_controller_emit_supported_events(avrcp_connection_t * connection){
114     uint8_t ctype = (uint8_t) AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
115     uint8_t event_id;
116 
117     for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){
118         if ( (connection->target_supported_notifications & (1<<event_id)) == 0){
119             continue;
120         }
121         uint8_t event[8];
122         uint8_t pos = 0;
123         event[pos++] = HCI_EVENT_AVRCP_META;
124         event[pos++] = sizeof(event) - 2;
125         event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID;
126         little_endian_store_16(event, pos, connection->avrcp_cid);
127         pos += 2;
128         event[pos++] = ctype;
129         event[pos++] = 0;
130         event[pos++] = event_id;
131         UNUSED(pos);
132         (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
133     }
134 
135     uint8_t event[7];
136     uint8_t pos = 0;
137     event[pos++] = HCI_EVENT_AVRCP_META;
138     event[pos++] = sizeof(event) - 2;
139     event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE;
140     little_endian_store_16(event, pos, connection->avrcp_cid);
141     pos += 2;
142     event[pos++] = ctype;
143     event[pos++] = 0;
144     UNUSED(pos);
145     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
146 }
147 
148 static void avrcp_controller_emit_notification_for_event_id(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id,
149                                                             avrcp_command_type_t ctype, const uint8_t *payload,
150                                                             uint16_t size) {
151     switch (event_id){
152         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{
153             if (size < 4) break;
154             uint32_t song_position = big_endian_read_32(payload, 0);
155             uint16_t offset = 0;
156             uint8_t event[10];
157             event[offset++] = HCI_EVENT_AVRCP_META;
158             event[offset++] = sizeof(event) - 2;
159             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED;
160             little_endian_store_16(event, offset, avrcp_cid);
161             offset += 2;
162             event[offset++] = ctype;
163             little_endian_store_32(event, offset, song_position);
164             offset += 4;
165             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
166             break;
167         }
168         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{
169             if (size < 1) break;
170             uint16_t offset = 0;
171             uint8_t event[7];
172             event[offset++] = HCI_EVENT_AVRCP_META;
173             event[offset++] = sizeof(event) - 2;
174             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED;
175             little_endian_store_16(event, offset, avrcp_cid);
176             offset += 2;
177             event[offset++] = ctype;
178             event[offset++] = payload[0];
179             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
180             break;
181         }
182         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{
183             uint16_t offset = 0;
184             uint8_t event[6];
185             event[offset++] = HCI_EVENT_AVRCP_META;
186             event[offset++] = sizeof(event) - 2;
187             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED;
188             little_endian_store_16(event, offset, avrcp_cid);
189             offset += 2;
190             event[offset++] = ctype;
191             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
192             break;
193         }
194         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{
195             uint16_t offset = 0;
196             uint8_t event[6];
197             event[offset++] = HCI_EVENT_AVRCP_META;
198             event[offset++] = sizeof(event) - 2;
199             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED;
200             little_endian_store_16(event, offset, avrcp_cid);
201             offset += 2;
202             event[offset++] = ctype;
203             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
204             break;
205         }
206         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{
207             uint16_t offset = 0;
208             uint8_t event[6];
209             event[offset++] = HCI_EVENT_AVRCP_META;
210             event[offset++] = sizeof(event) - 2;
211             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED;
212             little_endian_store_16(event, offset, avrcp_cid);
213             offset += 2;
214             event[offset++] = ctype;
215             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
216             break;
217         }
218         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{
219             if (size < 1) break;
220             uint16_t offset = 0;
221             uint8_t event[7];
222             event[offset++] = HCI_EVENT_AVRCP_META;
223             event[offset++] = sizeof(event) - 2;
224             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
225             little_endian_store_16(event, offset, avrcp_cid);
226             offset += 2;
227             event[offset++] = ctype;
228             event[offset++] = payload[0] & 0x7F;
229             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
230             break;
231         }
232         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{
233             if (size < 2) break;
234             uint8_t event[8];
235             uint16_t offset = 0;
236             uint16_t uuid = big_endian_read_16(payload, 0);
237             event[offset++] = HCI_EVENT_AVRCP_META;
238             event[offset++] = sizeof(event) - 2;
239             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED;
240             little_endian_store_16(event, offset, avrcp_cid);
241             offset += 2;
242             event[offset++] = ctype;
243             little_endian_store_16(event, offset, uuid);
244             offset += 2;
245             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
246             break;
247         }
248 
249         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{
250             uint16_t offset = 0;
251             uint8_t event[6];
252             event[offset++] = HCI_EVENT_AVRCP_META;
253             event[offset++] = sizeof(event) - 2;
254             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END;
255             little_endian_store_16(event, offset, avrcp_cid);
256             offset += 2;
257             event[offset++] = ctype;
258             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
259             break;
260         }
261         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{
262             uint16_t offset = 0;
263             uint8_t event[6];
264             event[offset++] = HCI_EVENT_AVRCP_META;
265             event[offset++] = sizeof(event) - 2;
266             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START;
267             little_endian_store_16(event, offset, avrcp_cid);
268             offset += 2;
269             event[offset++] = ctype;
270             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
271             break;
272         }
273         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{
274             if (size < 1) break;
275             uint16_t offset = 0;
276             uint8_t event[7];
277             event[offset++] = HCI_EVENT_AVRCP_META;
278             event[offset++] = sizeof(event) - 2;
279             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED;
280             little_endian_store_16(event, offset, avrcp_cid);
281             offset += 2;
282             event[offset++] = ctype;
283             event[offset++] = payload[0];
284             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
285             break;
286         }
287 
288         case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{
289             if (size < 1) break;
290             uint16_t offset = 0;
291             uint8_t event[7];
292             event[offset++] = HCI_EVENT_AVRCP_META;
293             event[offset++] = sizeof(event) - 2;
294             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED;
295             little_endian_store_16(event, offset, avrcp_cid);
296             offset += 2;
297             event[offset++] = ctype;
298             event[offset++] = payload[0];
299             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
300             break;
301         }
302 
303         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
304         default:
305             log_info("avrcp: not implemented");
306             break;
307     }
308 }
309 
310 static void avrcp_controller_emit_repeat_and_shuffle_mode(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_repeat_mode_t repeat_mode, avrcp_shuffle_mode_t shuffle_mode){
311     btstack_assert(callback != NULL);
312 
313     uint8_t event[8];
314     int pos = 0;
315     event[pos++] = HCI_EVENT_AVRCP_META;
316     event[pos++] = sizeof(event) - 2;
317     event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE;
318     little_endian_store_16(event, pos, avrcp_cid);
319     pos += 2;
320     event[pos++] = ctype;
321     event[pos++] = repeat_mode;
322     event[pos++] = shuffle_mode;
323     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
324 }
325 
326 static void avrcp_controller_emit_now_playing_info_event_done(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, uint8_t status){
327     uint8_t event[7];
328     int pos = 0;
329     event[pos++] = HCI_EVENT_AVRCP_META;
330     event[pos++] = sizeof(event) - 2;
331     event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE;
332     little_endian_store_16(event, pos, avrcp_cid);
333     pos += 2;
334     event[pos++] = ctype;
335     event[pos++] = status;
336     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
337 }
338 
339 static void avrcp_controller_emit_now_playing_info_event(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_media_attribute_id_t attr_id, uint8_t * value, uint16_t value_len){
340     uint8_t event[HCI_EVENT_BUFFER_SIZE];
341     int pos = 0;
342     event[pos++] = HCI_EVENT_AVRCP_META;
343     // reserve one byte for subevent type and data len
344     int data_len_pos = pos;
345     pos++;
346     int subevent_type_pos = pos;
347     pos++;
348     little_endian_store_16(event, pos, avrcp_cid);
349     pos += 2;
350     event[pos++] = ctype;
351 
352     switch (attr_id){
353         case AVRCP_MEDIA_ATTR_TITLE:
354             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO;
355             event[pos++] = value_len;
356             (void)memcpy(event + pos, value, value_len);
357             break;
358         case AVRCP_MEDIA_ATTR_ARTIST:
359             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO;
360             event[pos++] = value_len;
361             (void)memcpy(event + pos, value, value_len);
362             break;
363         case AVRCP_MEDIA_ATTR_ALBUM:
364             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO;
365             event[pos++] = value_len;
366             (void)memcpy(event + pos, value, value_len);
367             break;
368         case AVRCP_MEDIA_ATTR_GENRE:
369             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO;
370             event[pos++] = value_len;
371             (void)memcpy(event + pos, value, value_len);
372             break;
373         case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
374             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO;
375             if (value){
376                 little_endian_store_32(event, pos, btstack_atoi((char *)value));
377             } else {
378                 little_endian_store_32(event, pos, 0);
379             }
380             pos += 4;
381             break;
382         case AVRCP_MEDIA_ATTR_TRACK:
383             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO;
384             if (value){
385                 event[pos++] = btstack_atoi((char *)value);
386             } else {
387                 event[pos++] = 0;
388             }
389             break;
390         case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
391             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO;
392             if (value){
393                 event[pos++] = btstack_atoi((char *)value);
394             } else {
395                 event[pos++] = 0;
396             }
397             break;
398         default:
399             break;
400     }
401     event[data_len_pos] = pos - 2;
402     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
403 }
404 
405 static void avrcp_controller_emit_operation_status(btstack_packet_handler_t callback, uint8_t subevent, uint16_t avrcp_cid, uint8_t ctype, uint8_t operation_id){
406     btstack_assert(callback != NULL);
407 
408     uint8_t event[7];
409     int pos = 0;
410     event[pos++] = HCI_EVENT_AVRCP_META;
411     event[pos++] = sizeof(event) - 2;
412     event[pos++] = subevent;
413     little_endian_store_16(event, pos, avrcp_cid);
414     pos += 2;
415     event[pos++] = ctype;
416     event[pos++] = operation_id;
417     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
418 }
419 
420 static void avrcp_parser_reset(avrcp_connection_t * connection){
421     connection->list_offset = 0;
422     connection->num_attributes = 0;
423     connection->num_parsed_attributes = 0;
424     connection->parser_attribute_header_pos = 0;
425     connection->num_received_fragments = 0;
426     connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
427 }
428 
429 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){
430     uint16_t attribute_total_value_len;
431     uint32_t attribute_id;
432     switch(connection->parser_state){
433         case AVRCP_PARSER_GET_ATTRIBUTE_HEADER:
434             connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte;
435             connection->list_offset++;
436 
437             if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return;
438 
439             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
440             connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE);
441             if (connection->attribute_value_len > 0){
442                 // get ready for attribute value
443                 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE;
444                 return;
445             }
446 
447             // emit empty attribute
448             attribute_id = big_endian_read_32(connection->parser_attribute_header, 0);
449             avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, (avrcp_media_attribute_id_t) attribute_id, connection->attribute_value, connection->attribute_value_len);
450 
451             // done, see below
452             break;
453 
454         case AVRCP_PARSER_GET_ATTRIBUTE_VALUE:
455             connection->attribute_value[connection->attribute_value_offset++] = byte;
456             connection->list_offset++;
457 
458             if (connection->attribute_value_offset < connection->attribute_value_len) return;
459 
460             // emit (potentially partial) attribute
461             attribute_id = big_endian_read_32(connection->parser_attribute_header, 0);
462             avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, (avrcp_media_attribute_id_t) attribute_id, connection->attribute_value, connection->attribute_value_len);
463 
464             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
465             if (connection->attribute_value_offset < attribute_total_value_len){
466                 // ignore rest of attribute
467                 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE;
468                 return;
469             }
470 
471             // done, see below
472             break;
473 
474         case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE:
475             connection->attribute_value_offset++;
476             connection->list_offset++;
477 
478             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
479             if (connection->attribute_value_offset < attribute_total_value_len) return;
480 
481             // done, see below
482             break;
483 
484         default:
485             return;
486     }
487 
488     // attribute fully read, check if more to come
489     if (connection->list_offset < connection->list_size){
490         // more to come, reset parser
491         connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
492         connection->parser_attribute_header_pos = 0;
493         connection->attribute_value_offset = 0;
494     } else {
495         // fully done
496         avrcp_parser_reset(connection);
497         avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0);
498     }
499 }
500 
501 static void avrcp_controller_parse_and_emit_element_attrs(uint8_t * packet, uint16_t num_bytes_to_read, avrcp_connection_t * connection, avrcp_command_type_t ctype){
502     int i;
503     for (i=0;i<num_bytes_to_read;i++){
504         avrcp_parser_process_byte(packet[i], connection, ctype);
505     }
506 }
507 
508 
509 static int avrcp_send_cmd(avrcp_connection_t * connection, avrcp_packet_type_t packet_type){
510     uint8_t  command[AVRCP_CMD_BUFFER_SIZE];
511     uint16_t pos = 0;
512 
513     // non-fragmented: transport header (1) + PID (2)
514     // fragmented:     transport header (1) + num packets (1) + PID (2)
515 
516     // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
517     command[pos++] = (connection->transaction_id << 4) | (packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
518 
519     if (packet_type == AVRCP_START_PACKET){
520         // num packets: (3 bytes overhead (PID, num packets) + command) / (MTU - transport header).
521         // to get number of packets using integer division, we subtract 1 from the data e.g. len = 5, packet size 5 => need 1 packet
522         command[pos++] = ((connection->cmd_operands_fragmented_len + 3 - 1) / (AVRCP_CMD_BUFFER_SIZE - 1)) + 1;
523     }
524 
525     if ((packet_type == AVRCP_SINGLE_PACKET) || (packet_type == AVRCP_START_PACKET)){
526         // Profile IDentifier (PID)
527         command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
528         command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
529 
530         // command_type
531         command[pos++] = connection->command_type;
532         // subunit_type | subunit ID
533         command[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
534         // opcode
535         command[pos++] = (uint8_t)connection->command_opcode;
536     }
537 
538     if (packet_type == AVRCP_SINGLE_PACKET){
539         // operands
540         (void)memcpy(command + pos, connection->cmd_operands,
541                      connection->cmd_operands_length);
542         pos += connection->cmd_operands_length;
543     } else {
544         uint16_t bytes_free = AVRCP_CMD_BUFFER_SIZE - pos;
545         uint16_t bytes_to_store = connection->cmd_operands_fragmented_len-connection->cmd_operands_fragmented_pos;
546         uint16_t bytes_to_copy = btstack_min(bytes_to_store, bytes_free);
547         (void)memcpy(command + pos,
548                      &connection->cmd_operands_fragmented_buffer[connection->cmd_operands_fragmented_pos],
549                      bytes_to_copy);
550         pos += bytes_to_copy;
551         connection->cmd_operands_fragmented_pos += bytes_to_copy;
552     }
553 
554     return l2cap_send(connection->l2cap_signaling_cid, command, pos);
555 }
556 
557 static int avrcp_send_register_notification(avrcp_connection_t * connection, uint8_t event_id){
558     uint8_t command[18];
559     uint16_t pos = 0;
560     // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
561     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
562     command[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
563 
564     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
565     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
566     command[pos++] = AVRCP_CTYPE_NOTIFY;
567     command[pos++] = (AVRCP_SUBUNIT_TYPE_PANEL << 3) | AVRCP_SUBUNIT_ID;
568     command[pos++] = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
569 
570     big_endian_store_24(command, pos, BT_SIG_COMPANY_ID);
571     pos += 3;
572     command[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
573     command[pos++] = 0;                       // reserved(upper 6) | packet_type -> 0
574     big_endian_store_16(command, pos, 5);     // parameter length
575     pos += 2;
576     command[pos++] = event_id;
577     big_endian_store_32(command, pos, 1);     // send notification on playback position every second, for other notifications it is ignored
578     pos += 4;
579     return l2cap_send(connection->l2cap_signaling_cid, command, pos);
580 }
581 
582 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){
583     UNUSED(timer);
584     avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer);
585     btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout
586     btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer);
587     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
588     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
589 }
590 
591 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){
592     btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer);
593     btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler);
594     btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection);
595     btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout
596     btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer);
597 }
598 
599 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){
600     connection->press_and_hold_cmd_active = false;
601     btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer);
602 }
603 
604 
605 static uint8_t avrcp_controller_request_pass_through_release_control_cmd(avrcp_connection_t * connection){
606     connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
607     if (connection->press_and_hold_cmd_active){
608         avrcp_press_and_hold_timer_stop(connection);
609     }
610     connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0];
611     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
612     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
613     return ERROR_CODE_SUCCESS;
614 }
615 
616 static uint8_t avrcp_controller_request_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed, bool continuous_cmd){
617     log_info("Send command %d", opid);
618     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
619     if (!connection){
620         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
621     }
622 
623     if (connection->state != AVCTP_CONNECTION_OPENED){
624         log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid);
625         return ERROR_CODE_COMMAND_DISALLOWED;
626     }
627     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
628     connection->command_opcode =  AVRCP_CMD_OPCODE_PASS_THROUGH;
629     connection->command_type = AVRCP_CTYPE_CONTROL;
630     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
631     connection->subunit_id =   AVRCP_SUBUNIT_ID;
632     connection->cmd_operands_length = 0;
633 
634     connection->press_and_hold_cmd_active = continuous_cmd;
635     connection->cmd_operands_length = 2;
636     connection->cmd_operands[0] = opid;
637     if (playback_speed > 0){
638         connection->cmd_operands[2] = playback_speed;
639         connection->cmd_operands_length++;
640     }
641     connection->cmd_operands[1] = connection->cmd_operands_length - 2;
642 
643     if (connection->press_and_hold_cmd_active){
644         avrcp_press_and_hold_timer_start(connection);
645     }
646 
647     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
648     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
649     return ERROR_CODE_SUCCESS;
650 }
651 
652 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){
653     return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, false);
654 }
655 
656 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){
657     return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, true);
658 }
659 
660 static void avrcp_controller_get_capabilities_for_connection(avrcp_connection_t * connection, uint8_t capability_id){
661     connection->state = AVCTP_W2_SEND_COMMAND;
662     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
663     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
664     connection->command_type = AVRCP_CTYPE_STATUS;
665     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
666     connection->subunit_id = AVRCP_SUBUNIT_ID;
667     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
668     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID
669     connection->cmd_operands[4] = 0;
670     big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length
671     connection->cmd_operands[7] = capability_id;                  // capability ID
672     connection->cmd_operands_length = 8;
673     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
674 }
675 
676 static uint8_t avrcp_controller_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){
677     if (connection->target_supported_notifications_queried && (connection->target_supported_notifications & (1 << event_id)) == 0){
678         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
679     }
680     if ( (connection->notifications_to_deregister & (1 << event_id)) != 0){
681         return ERROR_CODE_COMMAND_DISALLOWED;
682     }
683     if ( (connection->notifications_enabled & (1 << event_id)) != 0){
684         return ERROR_CODE_SUCCESS;
685     }
686     connection->notifications_to_register |= (1 << event_id);
687 
688     if (!connection->target_supported_notifications_queried){
689         connection->target_supported_notifications_suppress_emit_result = true;
690         avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT);
691         return ERROR_CODE_SUCCESS;
692     }
693     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
694     return ERROR_CODE_SUCCESS;
695 }
696 
697 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){
698     connection->state = AVCTP_W2_SEND_COMMAND;
699     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
700     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
701     connection->command_type = AVRCP_CTYPE_CONTROL;
702     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
703     connection->subunit_id = AVRCP_SUBUNIT_ID;
704     int pos = 0;
705     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
706     pos += 3;
707     connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID
708     connection->cmd_operands[pos++] = 0;
709     // Parameter Length
710     connection->cmd_operands_length = 8;
711     big_endian_store_16(connection->cmd_operands, pos, 1);
712     pos += 2;
713     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
714     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
715     return ERROR_CODE_SUCCESS;
716 }
717 
718 
719 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){
720     connection->state = AVCTP_W2_SEND_COMMAND;
721     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
722     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
723     connection->command_type = AVRCP_CTYPE_CONTROL;
724     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
725     connection->subunit_id = AVRCP_SUBUNIT_ID;
726     int pos = 0;
727     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
728     pos += 3;
729     connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID
730     connection->cmd_operands[pos++] = 0;
731     // Parameter Length
732     connection->cmd_operands_length = 8;
733     big_endian_store_16(connection->cmd_operands, pos, 1);
734     pos += 2;
735     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
736     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
737     return ERROR_CODE_SUCCESS;
738 }
739 
740 static void avrcp_controller_handle_notification(avrcp_connection_t *connection, avrcp_command_type_t ctype, uint8_t *payload, uint16_t size) {
741     if (size < 1) return;
742     uint16_t pos = 0;
743     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) payload[pos++];
744     if ( (event_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){
745         return;
746     }
747 
748     uint16_t event_mask = (1 << event_id);
749     uint16_t reset_event_mask = ~event_mask;
750 
751     switch (ctype){
752         case AVRCP_CTYPE_RESPONSE_REJECTED:
753             connection->notifications_to_deregister &= reset_event_mask;
754             connection->notifications_to_register   &= reset_event_mask;
755             connection->initial_status_reported     &= reset_event_mask;
756             avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, event_id, false);
757             return;
758 
759         case AVRCP_CTYPE_RESPONSE_INTERIM:
760             // register as enabled
761             connection->notifications_enabled |= event_mask;
762 
763             // check if initial value is already sent
764             if ( (connection->initial_status_reported & event_mask) != 0 ){
765                 return;
766             }
767             // emit event only once, initially
768             avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, true);
769             connection->initial_status_reported |= event_mask;
770             // emit initial value after this switch
771             break;
772 
773         case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE:
774             // received change, event is considered de-registered
775             // we are re-enabling it automatically, if it is not
776             // explicitly disabled
777             connection->notifications_enabled &= reset_event_mask;
778             if ((connection->notifications_to_deregister & event_mask) == 0){
779                 avrcp_controller_register_notification(connection, event_id);
780             } else {
781                 connection->notifications_to_deregister &= reset_event_mask;
782                 connection->notifications_to_register   &= reset_event_mask;
783                 connection->initial_status_reported     &= reset_event_mask;
784                 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, false);
785             }
786             break;
787 
788         default:
789             return;
790     }
791 
792     avrcp_controller_emit_notification_for_event_id(connection->avrcp_cid, event_id, ctype, payload + pos, size - pos);
793 }
794 
795 #ifdef ENABLE_AVCTP_FRAGMENTATION
796 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){
797     // after header (transaction label and packet type)
798     uint16_t pos;
799     uint16_t bytes_to_store;
800 
801     switch (packet_type){
802         case AVCTP_START_PACKET:
803             if (size < 2) return;
804 
805             // store header
806             pos = 0;
807             connection->avctp_reassembly_buffer[pos] = packet[pos];
808             pos++;
809             connection->avctp_reassembly_size = pos;
810 
811             // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it
812             pos++;
813 
814             // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called
815 
816             bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size);
817             memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store);
818             connection->avctp_reassembly_size += bytes_to_store;
819             break;
820 
821         case AVCTP_CONTINUE_PACKET:
822         case AVCTP_END_PACKET:
823             if (size < 1) return;
824 
825             // store remaining data, ignore header
826             pos = 1;
827             bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size);
828             memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store);
829             connection->avctp_reassembly_size += bytes_to_store;
830             break;
831 
832         default:
833             return;
834     }
835 }
836 #endif
837 
838 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
839     if (size < 6u) return;
840     uint8_t  pdu_id;
841     avrcp_packet_type_t  vendor_dependent_packet_type;
842 
843     uint16_t pos = 0;
844     connection->last_confirmed_transaction_id = packet[pos] >> 4;
845     avrcp_frame_type_t  frame_type =  (avrcp_frame_type_t)((packet[pos] >> 1) & 0x01);
846     avctp_packet_type_t packet_type = (avctp_packet_type_t)((packet[pos] >> 2) & 0x03);
847     pos++;
848 
849     if (frame_type != AVRCP_RESPONSE_FRAME) return;
850 
851     switch (packet_type){
852         case AVCTP_SINGLE_PACKET:
853             break;
854 
855 #ifdef ENABLE_AVCTP_FRAGMENTATION
856         case AVCTP_START_PACKET:
857         case AVCTP_CONTINUE_PACKET:
858             avctp_reassemble_message(connection, packet_type, packet, size);
859             return;
860 
861         case AVCTP_END_PACKET:
862             avctp_reassemble_message(connection, packet_type, packet, size);
863 
864             packet = connection->avctp_reassembly_buffer;
865             size   = connection->avctp_reassembly_size;
866             break;
867 #endif
868 
869         default:
870             return;
871     }
872 
873     pos += 2; // PID
874 
875     avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++];
876 
877 #ifdef ENABLE_LOG_INFO
878     uint8_t byte_value = packet[pos];
879     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3);
880     avrcp_subunit_type_t subunit_id   = (avrcp_subunit_type_t) (byte_value & 0x07);
881 #endif
882     pos++;
883 
884     uint8_t opcode = packet[pos++];
885     uint16_t param_length;
886 
887     switch (opcode){
888         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
889             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
890             connection->state = AVCTP_CONNECTION_OPENED;
891 
892 #ifdef ENABLE_LOG_INFO
893             // page, extension code (1)
894             pos++;
895             uint8_t unit_type = packet[pos] >> 3;
896             uint8_t max_subunit_ID = packet[pos] & 0x07;
897             log_info("SUBUNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, max_subunit_ID %d", ctype, subunit_type, subunit_id, opcode, unit_type, max_subunit_ID);
898 #endif
899             break;
900         }
901         case AVRCP_CMD_OPCODE_UNIT_INFO:{
902             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
903             connection->state = AVCTP_CONNECTION_OPENED;
904 
905 #ifdef ENABLE_LOG_INFO
906             // byte value 7 (1)
907             pos++;
908             uint8_t unit_type = packet[pos] >> 3;
909             uint8_t unit = packet[pos] & 0x07;
910             pos++;
911             uint32_t company_id = big_endian_read_24(packet, pos);
912             log_info("UNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06" PRIx32,
913                 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id);
914 #endif
915             break;
916         }
917         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
918 
919             if ((size - pos) < 7) return;
920 
921             // Company ID (3)
922             pos += 3;
923             pdu_id = packet[pos++];
924             vendor_dependent_packet_type = (avrcp_packet_type_t)(packet[pos++] & 0x03);
925             param_length = big_endian_read_16(packet, pos);
926             pos += 2;
927 
928             if ((size - pos) < param_length) return;
929 
930             // handle asynchronous notifications, without changing state
931             if (pdu_id == AVRCP_PDU_ID_REGISTER_NOTIFICATION){
932                 avrcp_controller_handle_notification(connection, ctype, packet + pos, size - pos);
933                 break;
934             }
935 
936             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE){
937                 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state);
938                 return;
939             }
940             connection->state = AVCTP_CONNECTION_OPENED;
941 
942             log_info("VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype));
943             switch (pdu_id){
944                 case AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:{
945                     uint8_t num_attributes = packet[pos++];
946                     int i;
947                     avrcp_repeat_mode_t  repeat_mode =  AVRCP_REPEAT_MODE_INVALID;
948                     avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID;
949                     for (i = 0; i < num_attributes; i++){
950                         uint8_t attribute_id    = packet[pos++];
951                         uint8_t value = packet[pos++];
952                         switch (attribute_id){
953                             case 0x02:
954                                 repeat_mode = (avrcp_repeat_mode_t) value;
955                                 break;
956                             case 0x03:
957                                 shuffle_mode = (avrcp_shuffle_mode_t) value;
958                                 break;
959                             default:
960                                 break;
961                         }
962                     }
963                     avrcp_controller_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode);
964                     break;
965                 }
966 
967                 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{
968                     uint16_t offset = 0;
969                     uint8_t event[6];
970                     event[offset++] = HCI_EVENT_AVRCP_META;
971                     event[offset++] = sizeof(event) - 2;
972                     event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE;
973                     little_endian_store_16(event, offset, connection->avrcp_cid);
974                     offset += 2;
975                     event[offset++] = ctype;
976                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
977                     break;
978                 }
979 
980                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{
981                     uint16_t offset = 0;
982                     uint8_t event[7];
983                     event[offset++] = HCI_EVENT_AVRCP_META;
984                     event[offset++] = sizeof(event) - 2;
985                     event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE;
986                     little_endian_store_16(event, offset, connection->avrcp_cid);
987                     offset += 2;
988                     event[offset++] = ctype;
989                     event[offset++] = packet[pos++];
990                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
991                     break;
992                 }
993 
994                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
995                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++];
996                     uint8_t capability_count = 0;
997                     if (param_length > 1){
998                         capability_count = packet[pos++];
999                     }
1000                     uint16_t i;
1001                     uint16_t offset = 0;
1002                     uint8_t event[10];
1003 
1004                     switch (capability_id){
1005 
1006                         case AVRCP_CAPABILITY_ID_COMPANY:
1007                             for (i = 0; (i < capability_count) && ((size - pos) >= 3); i++){
1008                                 uint32_t company_id = big_endian_read_24(packet, pos);
1009                                 pos += 3;
1010                                 log_info("  0x%06" PRIx32 ", ", company_id);
1011 
1012                                 offset = 0;
1013                                 event[offset++] = HCI_EVENT_AVRCP_META;
1014                                 event[offset++] = sizeof(event) - 2;
1015                                 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID;
1016                                 little_endian_store_16(event, offset, connection->avrcp_cid);
1017                                 offset += 2;
1018                                 event[offset++] = ctype;
1019                                 event[offset++] = 0;
1020                                 little_endian_store_24(event, offset, company_id);
1021                                 offset += 3;
1022                                 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
1023                             }
1024 
1025                             offset = 0;
1026                             event[offset++] = HCI_EVENT_AVRCP_META;
1027                             event[offset++] = sizeof(event) - 2;
1028                             event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE;
1029                             little_endian_store_16(event, offset, connection->avrcp_cid);
1030                             offset += 2;
1031                             event[offset++] = ctype;
1032                             event[offset++] = 0;
1033                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
1034                             break;
1035 
1036                         case AVRCP_CAPABILITY_ID_EVENT:
1037                             for (i = 0; (i < capability_count) && ((size - pos) >= 1); i++){
1038                                 uint8_t event_id = packet[pos++];
1039                                 connection->target_supported_notifications |= (1 << event_id);
1040                             }
1041 
1042                             // if the get supported events query is triggered by avrcp_controller_enable_notification call,
1043                             // avrcp_controller_emit_supported_events should be suppressed
1044                             if (connection->target_supported_notifications_suppress_emit_result){
1045                                 connection->target_supported_notifications_suppress_emit_result = false;
1046                                 // also, notification might not be supported
1047                                 // if so, emit AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE event to app,
1048                                 // and update notifications_to_register bitmap
1049                                 for (i = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++){
1050                                     if ((connection->notifications_to_register & (1<<i)) != 0){
1051                                         if ((connection->target_supported_notifications & (1<<i)) == 0){
1052                                             avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, i, false);
1053                                             connection->notifications_to_register &= ~(1 << i);
1054                                         }
1055                                     }
1056                                 }
1057                                 break;
1058                             }
1059                             // supported events are emitted only if the get supported events query
1060                             // is triggered by avrcp_controller_get_supported_events call
1061                             avrcp_controller_emit_supported_events(connection);
1062                             break;
1063 
1064                         default:
1065                             // ignore
1066                             break;
1067                     }
1068                     break;
1069                 }
1070 
1071                 case AVRCP_PDU_ID_GET_PLAY_STATUS:{
1072                     uint32_t song_length = big_endian_read_32(packet, pos);
1073                     pos += 4;
1074                     uint32_t song_position = big_endian_read_32(packet, pos);
1075                     pos += 4;
1076                     uint8_t play_status = packet[pos];
1077 
1078                     uint8_t event[15];
1079                     int offset = 0;
1080                     event[offset++] = HCI_EVENT_AVRCP_META;
1081                     event[offset++] = sizeof(event) - 2;
1082                     event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS;
1083                     little_endian_store_16(event, offset, connection->avrcp_cid);
1084                     offset += 2;
1085                     event[offset++] = ctype;
1086                     little_endian_store_32(event, offset, song_length);
1087                     offset += 4;
1088                     little_endian_store_32(event, offset, song_position);
1089                     offset += 4;
1090                     event[offset++] = play_status;
1091                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1092                     break;
1093                 }
1094 
1095                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
1096                     switch (vendor_dependent_packet_type){
1097                         case AVRCP_START_PACKET:
1098                         case AVRCP_SINGLE_PACKET:
1099                             avrcp_parser_reset(connection);
1100                             connection->list_size = param_length;
1101                             connection->num_attributes = packet[pos++];
1102 
1103                             avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
1104                             if (vendor_dependent_packet_type == AVRCP_START_PACKET){
1105                                 avrcp_controller_request_continue_response(connection);
1106                             }
1107                             break;
1108                         case AVRCP_CONTINUE_PACKET:
1109                         case AVRCP_END_PACKET:
1110                             connection->num_received_fragments++;
1111 
1112                             if (connection->num_received_fragments < connection->max_num_fragments){
1113                                 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
1114 
1115                                 if (vendor_dependent_packet_type == AVRCP_CONTINUE_PACKET){
1116                                     avrcp_controller_request_continue_response(connection);
1117                                 }
1118                             } else {
1119                                 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1);
1120                                 avrcp_parser_reset(connection);
1121                                 avrcp_controller_request_abort_continuation(connection);
1122                             }
1123                             break;
1124                         default:
1125                             // TODO check
1126                             btstack_assert(false);
1127                             break;
1128                     }
1129                 }
1130                 default:
1131                     break;
1132             }
1133             break;
1134         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
1135             if ((size - pos) < 1) return;
1136             uint8_t operation_id = packet[pos++];
1137             switch (connection->state){
1138                 case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
1139                     // trigger release for simple command:
1140                     if (!connection->press_and_hold_cmd_active){
1141                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
1142                         break;
1143                     }
1144                     // for press and hold, send release if it just has been requested, otherwise, wait for next repeat
1145                     if (connection->press_and_hold_cmd_release){
1146                         connection->press_and_hold_cmd_release = false;
1147                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
1148                     } else {
1149                         connection->state = AVCTP_W4_STOP;
1150                     }
1151                     break;
1152                 case AVCTP_W2_RECEIVE_RESPONSE:
1153                     connection->state = AVCTP_CONNECTION_OPENED;
1154                     break;
1155                 default:
1156                     break;
1157             }
1158             if (connection->state == AVCTP_W4_STOP){
1159                 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id);
1160             }
1161             if (connection->state == AVCTP_CONNECTION_OPENED) {
1162                 // RELEASE response
1163                 operation_id = operation_id & 0x7F;
1164                 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id);
1165             }
1166             if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){
1167                 // PRESS response
1168                 avrcp_controller_request_pass_through_release_control_cmd(connection);
1169             }
1170             break;
1171         }
1172         default:
1173             break;
1174     }
1175 
1176     // trigger pending notification reqistrations
1177     if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->notifications_to_register){
1178         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1179     }
1180 }
1181 
1182 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){
1183     switch (connection->state){
1184         case AVCTP_W2_SEND_PRESS_COMMAND:
1185             connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE;
1186             avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
1187             return;
1188         case AVCTP_W2_SEND_COMMAND:
1189         case AVCTP_W2_SEND_RELEASE_COMMAND:
1190             connection->state = AVCTP_W2_RECEIVE_RESPONSE;
1191             avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
1192             return;
1193         case AVCTP_W2_SEND_AVCTP_FRAGMENTED_MESSAGE:
1194             if (connection->cmd_operands_fragmented_pos == 0){
1195                  avrcp_send_cmd(connection, AVRCP_START_PACKET);
1196                  avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1197             } else {
1198                 if ((connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos) > avrcp_get_max_payload_size_for_avctp_packet_type(connection, AVCTP_CONTINUE_PACKET)){
1199                      avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET);
1200                      avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1201                  } else {
1202                     connection->state = AVCTP_W2_RECEIVE_RESPONSE;
1203                     avrcp_send_cmd(connection, AVRCP_END_PACKET);
1204                  }
1205             }
1206             return;
1207         default:
1208             break;
1209     }
1210     // send register notification if queued
1211     if (connection->notifications_to_register != 0){
1212         uint8_t event_id;
1213         for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){
1214             if (connection->notifications_to_register & (1<<event_id)){
1215                 connection->notifications_to_register &= ~ (1 << event_id);
1216                 avrcp_send_register_notification(connection, event_id);
1217                 return;
1218             }
1219         }
1220     }
1221 }
1222 
1223 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1224     avrcp_connection_t * connection;
1225 
1226     switch (packet_type) {
1227         case L2CAP_DATA_PACKET:
1228             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1229             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1230             break;
1231 
1232         case HCI_EVENT_PACKET:
1233             switch (hci_event_packet_get_type(packet)){
1234                 case L2CAP_EVENT_CAN_SEND_NOW:
1235                     connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1236                     avrcp_controller_handle_can_send_now(connection);
1237                     break;
1238             default:
1239                 break;
1240         }
1241         default:
1242             break;
1243     }
1244 }
1245 
1246 void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
1247     avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
1248 }
1249 
1250 void avrcp_controller_init(void){
1251     avrcp_controller_context.role = AVRCP_CONTROLLER;
1252     avrcp_controller_context.packet_handler = avrcp_controller_packet_handler;
1253     avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler);
1254 }
1255 
1256 void avrcp_controller_deinit(void){
1257     memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t));
1258 }
1259 
1260 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){
1261     btstack_assert(callback != NULL);
1262     avrcp_controller_context.avrcp_callback = callback;
1263 }
1264 
1265 
1266 uint8_t avrcp_controller_play(uint16_t avrcp_cid){
1267     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1268 }
1269 
1270 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){
1271     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1272 }
1273 
1274 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){
1275     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1276 }
1277 
1278 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){
1279     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1280 }
1281 
1282 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){
1283     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1284 }
1285 
1286 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){
1287     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1288 }
1289 
1290 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){
1291     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1292 }
1293 
1294 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){
1295     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1296 }
1297 
1298 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){
1299     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0);
1300 }
1301 
1302 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){
1303     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1304 }
1305 
1306 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){
1307     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1308 }
1309 
1310 /* start continuous cmds */
1311 
1312 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){
1313     return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0);
1314 }
1315 
1316 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){
1317     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1318 }
1319 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){
1320     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1321 }
1322 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){
1323     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1324 }
1325 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){
1326     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1327 }
1328 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){
1329     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1330 }
1331 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){
1332     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1333 }
1334 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){
1335     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1336 }
1337 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){
1338     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1339 }
1340 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){
1341     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1342 }
1343 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){
1344     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1345 }
1346 
1347 /* stop continuous cmds */
1348 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){
1349     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1350     if (!connection){
1351         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1352     }
1353 
1354     switch (connection->state){
1355         // respond when we receive response for (repeated) press command
1356         case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
1357             connection->press_and_hold_cmd_release = true;
1358             break;
1359 
1360         // release already sent or on the way, nothing to do
1361         case AVCTP_W2_RECEIVE_RESPONSE:
1362         case AVCTP_W2_SEND_RELEASE_COMMAND:
1363             break;
1364 
1365         // about to send next repeated press command or wait for it -> release right away
1366         case AVCTP_W2_SEND_PRESS_COMMAND:
1367         case AVCTP_W4_STOP:
1368             return avrcp_controller_request_pass_through_release_control_cmd(connection);
1369 
1370         // otherwise reject request
1371         default:
1372             return ERROR_CODE_COMMAND_DISALLOWED;
1373     }
1374     return ERROR_CODE_SUCCESS;
1375 }
1376 
1377 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1378     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1379     if (!connection){
1380         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1381     }
1382     return avrcp_controller_register_notification(connection, event_id);
1383 }
1384 
1385 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1386     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1387     if (!connection){
1388         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1389     }
1390     if (!connection->target_supported_notifications_queried){
1391         return ERROR_CODE_COMMAND_DISALLOWED;
1392     }
1393 
1394     if ((connection->target_supported_notifications & (1 << event_id)) == 0){
1395         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1396     }
1397 
1398     if ((connection->notifications_enabled & (1 << event_id)) == 0){
1399         return ERROR_CODE_SUCCESS;
1400     }
1401 
1402     connection->notifications_to_deregister |= (1 << event_id);
1403     return ERROR_CODE_SUCCESS;
1404 }
1405 
1406 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){
1407     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1408     if (!connection){
1409         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1410     }
1411     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1412     connection->state = AVCTP_W2_SEND_COMMAND;
1413 
1414     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1415     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
1416     connection->command_type = AVRCP_CTYPE_STATUS;
1417     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
1418     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
1419     memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length);
1420     connection->cmd_operands_length = 5;
1421     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1422     return ERROR_CODE_SUCCESS;
1423 }
1424 
1425 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){
1426     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1427     if (!connection){
1428         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1429     }
1430     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1431     connection->state = AVCTP_W2_SEND_COMMAND;
1432 
1433     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1434     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
1435     connection->command_type = AVRCP_CTYPE_STATUS;
1436     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
1437     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
1438     memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length);
1439     connection->cmd_operands[0] = 7; // page: 0, extention_code: 7
1440     connection->cmd_operands_length = 5;
1441     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1442     return ERROR_CODE_SUCCESS;
1443 }
1444 
1445 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){
1446     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1447     if (!connection){
1448         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1449     }
1450     if (connection->state != AVCTP_CONNECTION_OPENED){
1451         return ERROR_CODE_COMMAND_DISALLOWED;
1452     }
1453     avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY);
1454     return ERROR_CODE_SUCCESS;
1455 }
1456 
1457 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){
1458     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1459     if (!connection){
1460         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1461     }
1462     if (connection->state != AVCTP_CONNECTION_OPENED){
1463         return ERROR_CODE_COMMAND_DISALLOWED;
1464     }
1465 
1466     if (!connection->target_supported_notifications_queried){
1467         connection->target_supported_notifications_queried = true;
1468         avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT);
1469         return ERROR_CODE_SUCCESS;
1470     }
1471 
1472     avrcp_controller_emit_supported_events(connection);
1473     return ERROR_CODE_SUCCESS;
1474 }
1475 
1476 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){
1477     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1478     if (!connection){
1479         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1480     }
1481     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1482     connection->state = AVCTP_W2_SEND_COMMAND;
1483     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1484     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1485     connection->command_type = AVRCP_CTYPE_STATUS;
1486     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1487     connection->subunit_id = AVRCP_SUBUNIT_ID;
1488     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1489     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS;
1490     connection->cmd_operands[4] = 0;                     // reserved(upper 6) | packet_type -> 0
1491     big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length
1492     connection->cmd_operands_length = 7;
1493     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1494     return ERROR_CODE_SUCCESS;
1495 }
1496 
1497 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){
1498     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1499     if (!connection){
1500         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1501     }
1502     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1503     connection->state = AVCTP_W2_SEND_COMMAND;
1504 
1505     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1506     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1507     connection->command_type = AVRCP_CTYPE_CONTROL;
1508     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1509     connection->subunit_id = AVRCP_SUBUNIT_ID;
1510     int pos = 0;
1511     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1512     pos += 3;
1513     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID
1514     connection->cmd_operands[pos++] = 0;
1515 
1516     // Parameter Length
1517     big_endian_store_16(connection->cmd_operands, pos, 2);
1518     pos += 2;
1519 
1520     big_endian_store_16(connection->cmd_operands, pos, addressed_player_id);
1521     pos += 2;
1522 
1523     connection->cmd_operands_length = pos;
1524     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1525     return ERROR_CODE_SUCCESS;
1526 }
1527 
1528 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){
1529     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1530     if (!connection){
1531         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1532     }
1533 
1534     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1535 
1536     if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) {
1537         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
1538     }
1539     connection->state = AVCTP_W2_SEND_COMMAND;
1540 
1541     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1542     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1543     connection->command_type = AVRCP_CTYPE_STATUS;
1544     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1545     connection->subunit_id = AVRCP_SUBUNIT_ID;
1546     int pos = 0;
1547     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1548     pos += 3;
1549     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID
1550     connection->cmd_operands[pos++] = 0;
1551 
1552     // Parameter Length
1553     big_endian_store_16(connection->cmd_operands, pos, 9);
1554     pos += 2;
1555 
1556     // write 8 bytes value
1557     memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING
1558     pos += 8;
1559 
1560     connection->cmd_operands[pos++] = num_attributes; // attribute count, if 0 get all attributes
1561 
1562     int i;
1563     for (i = 0; i < num_attributes; i++){
1564         // every attribute is 4 bytes long
1565         big_endian_store_32(connection->cmd_operands, pos, attributes[i]);
1566         pos += 4;
1567     }
1568 
1569     connection->cmd_operands_length = pos;
1570     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1571     return ERROR_CODE_SUCCESS;
1572 }
1573 
1574 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){
1575     return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL);
1576 }
1577 
1578 uint8_t avrcp_controller_get_now_playing_info_for_media_attribute_id(uint16_t avrcp_cid, avrcp_media_attribute_id_t media_attribute_id){
1579     if (media_attribute_id == AVRCP_MEDIA_ATTR_ALL){
1580         return avrcp_controller_get_now_playing_info(avrcp_cid);
1581     }
1582     return avrcp_controller_get_element_attributes(avrcp_cid, 1, &media_attribute_id);
1583 }
1584 
1585 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){
1586      avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1587     if (!connection){
1588         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1589     }
1590 
1591     //
1592     // allow sending of multiple set abs volume commands without waiting for response
1593     //
1594     uint8_t status = ERROR_CODE_COMMAND_DISALLOWED;
1595     switch (connection->state){
1596         case AVCTP_CONNECTION_OPENED:
1597             status = ERROR_CODE_SUCCESS;
1598             break;
1599         case AVCTP_W2_RECEIVE_RESPONSE:
1600             // - is pending response also set abs volume
1601             if (connection->command_opcode  != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break;
1602             if (connection->command_type    != AVRCP_CTYPE_CONTROL)               break;
1603             if (connection->subunit_type    != AVRCP_SUBUNIT_TYPE_PANEL)          break;
1604             if (connection->subunit_id      != AVRCP_SUBUNIT_ID)                  break;
1605             if (connection->cmd_operands[3] != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME)  break;
1606             // - is next transaction id valid in window
1607             if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break;
1608             status = ERROR_CODE_SUCCESS;
1609             break;
1610         default:
1611             break;
1612     }
1613     if (status != ERROR_CODE_SUCCESS) return status;
1614 
1615     connection->state = AVCTP_W2_SEND_COMMAND;
1616 
1617     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1618     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1619     connection->command_type = AVRCP_CTYPE_CONTROL;
1620     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1621     connection->subunit_id = AVRCP_SUBUNIT_ID;
1622     int pos = 0;
1623     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1624     pos += 3;
1625     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID
1626     connection->cmd_operands[pos++] = 0;
1627 
1628     // Parameter Length
1629     big_endian_store_16(connection->cmd_operands, pos, 1);
1630     pos += 2;
1631     connection->cmd_operands[pos++] = volume;
1632 
1633     connection->cmd_operands_length = pos;
1634     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1635     return ERROR_CODE_SUCCESS;
1636 }
1637 
1638 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){
1639     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1640     if (!connection){
1641         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1642     }
1643     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1644     connection->state = AVCTP_W2_SEND_COMMAND;
1645 
1646     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1647     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1648     connection->command_type = AVRCP_CTYPE_STATUS;
1649     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1650     connection->subunit_id = AVRCP_SUBUNIT_ID;
1651     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1652     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID
1653     connection->cmd_operands[4] = 0;
1654     big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length
1655     connection->cmd_operands[7] = 4;                     // NumPlayerApplicationSettingAttributeID
1656     // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133
1657     connection->cmd_operands[8]  = 0x01;    // equalizer  (1-OFF, 2-ON)
1658     connection->cmd_operands[9]  = 0x02;    // repeat     (1-off, 2-single track, 3-all tracks, 4-group repeat)
1659     connection->cmd_operands[10] = 0x03;    // shuffle    (1-off, 2-all tracks, 3-group shuffle)
1660     connection->cmd_operands[11] = 0x04;    // scan       (1-off, 2-all tracks, 3-group scan)
1661     connection->cmd_operands_length = 12;
1662     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1663     return ERROR_CODE_SUCCESS;
1664 }
1665 
1666 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){
1667     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1668     if (!connection){
1669         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1670     }
1671     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1672     connection->state = AVCTP_W2_SEND_COMMAND;
1673 
1674     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1675     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1676     connection->command_type = AVRCP_CTYPE_CONTROL;
1677     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1678     connection->subunit_id = AVRCP_SUBUNIT_ID;
1679     int pos = 0;
1680     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1681     pos += 3;
1682     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID
1683     connection->cmd_operands[pos++] = 0;
1684     // Parameter Length
1685     big_endian_store_16(connection->cmd_operands, pos, 3);
1686     pos += 2;
1687     connection->cmd_operands[pos++] = 2;
1688     connection->cmd_operands_length = pos;
1689     connection->cmd_operands[pos++]  = attr_id;
1690     connection->cmd_operands[pos++]  = attr_value;
1691     connection->cmd_operands_length = pos;
1692     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1693     return ERROR_CODE_SUCCESS;
1694 }
1695 
1696 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){
1697     if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1698     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode);
1699 }
1700 
1701 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){
1702     if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1703     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode);
1704 }
1705 
1706 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){
1707     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1708     if (!connection){
1709         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1710     }
1711     if (connection->state != AVCTP_CONNECTION_OPENED){
1712         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1713         return ERROR_CODE_COMMAND_DISALLOWED;
1714     }
1715     connection->state = AVCTP_W2_SEND_COMMAND;
1716 
1717     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1718     connection->command_type = AVRCP_CTYPE_CONTROL;
1719     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1720     connection->subunit_id = AVRCP_SUBUNIT_ID;
1721     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1722     int pos = 0;
1723     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1724     pos += 3;
1725     connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID
1726     // reserved
1727     connection->cmd_operands[pos++] = 0;
1728     // Parameter Length
1729     big_endian_store_16(connection->cmd_operands, pos, 11);
1730     pos += 2;
1731     connection->cmd_operands[pos++]  = scope;
1732     memset(&connection->cmd_operands[pos], 0, 8);
1733     if (uid){
1734         (void)memcpy(&connection->cmd_operands[pos], uid, 8);
1735     }
1736     pos += 8;
1737     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
1738     pos += 2;
1739     connection->cmd_operands_length = pos;
1740 
1741     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1742     return ERROR_CODE_SUCCESS;
1743 }
1744 
1745 uint8_t avrcp_controller_add_item_from_scope_to_now_playing_list(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){
1746     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1747     if (!connection){
1748         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1749     }
1750     if (connection->state != AVCTP_CONNECTION_OPENED){
1751         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1752         return ERROR_CODE_COMMAND_DISALLOWED;
1753     }
1754     connection->state = AVCTP_W2_SEND_COMMAND;
1755 
1756     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1757     connection->command_type = AVRCP_CTYPE_CONTROL;
1758     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1759     connection->subunit_id = AVRCP_SUBUNIT_ID;
1760     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1761     int pos = 0;
1762     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1763     pos += 3;
1764     connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID
1765     // reserved
1766     connection->cmd_operands[pos++] = 0;
1767     // Parameter Length
1768     big_endian_store_16(connection->cmd_operands, pos, 11);
1769     pos += 2;
1770     connection->cmd_operands[pos++]  = scope;
1771     memset(&connection->cmd_operands[pos], 0, 8);
1772     if (uid){
1773         (void)memcpy(&connection->cmd_operands[pos], uid, 8);
1774     }
1775     pos += 8;
1776     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
1777     pos += 2;
1778     connection->cmd_operands_length = pos;
1779 
1780     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1781     return ERROR_CODE_SUCCESS;
1782 }
1783 
1784 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){
1785     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1786     if (!connection){
1787         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1788     }
1789     connection->max_num_fragments = max_num_fragments;
1790     return ERROR_CODE_SUCCESS;
1791 }
1792 
1793 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid, avrcp_command_type_t command_type, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t command_opcode, const uint8_t * command_buffer, uint16_t command_len){
1794     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1795     if (!connection){
1796         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1797     }
1798 
1799     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1800     connection->state = AVCTP_W2_SEND_AVCTP_FRAGMENTED_MESSAGE;
1801 
1802     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
1803     connection->command_opcode = command_opcode;
1804     connection->command_type = command_type;
1805     connection->subunit_type = subunit_type;
1806     connection->subunit_id = subunit_id;
1807     connection->cmd_operands_fragmented_buffer = command_buffer;
1808     connection->cmd_operands_fragmented_pos = 0;
1809     connection->cmd_operands_fragmented_len = command_len;
1810 
1811     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1812     return ERROR_CODE_SUCCESS;
1813 }
1814