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