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