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