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