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