xref: /btstack/src/classic/avrcp_controller.c (revision f98d104180e0bf45b339c340a6e0291cae19f984)
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 
51 static const char * avrcp_default_controller_service_name = "AVRCP Controller";
52 static const char * avrcp_default_controller_service_provider_name = "BlueKitchen";
53 
54 // made public in avrcp_controller.h
55 avrcp_context_t avrcp_controller_context;
56 
57 static uint8_t avrcp_controller_calc_next_transaction_label(uint8_t current_transaction_label){
58     current_transaction_label++;
59     if (current_transaction_label == 16){
60         current_transaction_label = 1;
61     }
62     return current_transaction_label;
63 }
64 
65 static uint8_t avrcp_controller_get_next_transaction_label(avrcp_connection_t * connection){
66     connection->transaction_id_counter = avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter);
67     return connection->transaction_id_counter;
68 }
69 
70 static bool avrcp_controller_is_transaction_id_valid(avrcp_connection_t * connection, uint8_t transaction_id){
71     uint8_t delta = ((int8_t) transaction_id - connection->controller_last_confirmed_transaction_id) & 0x0f;
72     return delta < 15;
73 }
74 
75 static void avrcp_controller_custom_command_data_init(avrcp_connection_t * connection,
76                                                       avrcp_command_opcode_t opcode, avrcp_command_type_t command_type,
77                                                       avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id,
78                                                       avrcp_pdu_id_t pdu_id, uint32_t company_id){
79 
80     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
81     connection->command_opcode = opcode;
82     connection->command_type = command_type;
83     connection->subunit_type = subunit_type;
84     connection->subunit_id = subunit_id;
85     connection->company_id = company_id;
86     connection->pdu_id = pdu_id;
87     connection->data = NULL;
88     connection->data_offset = 0;
89     connection->data_len = 0;
90 }
91 
92 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){
93     if (get_next_transaction_label){
94         connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
95     }
96     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
97     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
98     connection->subunit_id = AVRCP_SUBUNIT_ID;
99     connection->company_id = BT_SIG_COMPANY_ID;
100 
101     connection->command_type = command_type;
102     connection->pdu_id = pdu_id;
103     connection->data = connection->message_body;
104     connection->data_offset = 0;
105     connection->data_len = 0;
106 }
107 
108 static void avrcp_controller_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_operation_id_t opid){
109     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
110     connection->command_opcode =  AVRCP_CMD_OPCODE_PASS_THROUGH;
111     connection->command_type = AVRCP_CTYPE_CONTROL;
112     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
113     connection->subunit_id =   AVRCP_SUBUNIT_ID;
114 
115     connection->company_id = 0;
116     connection->pdu_id = AVRCP_PDU_ID_UNDEFINED;
117     connection->operation_id = opid;
118 
119     connection->data = connection->message_body;
120     connection->data_offset = 0;
121     connection->data_len = 0;
122 }
123 
124 static int avrcp_controller_supports_browsing(uint16_t controller_supported_features){
125     return controller_supported_features & AVRCP_FEATURE_MASK_BROWSING;
126 }
127 
128 static void avrcp_controller_prepare_custom_command_response(avrcp_connection_t * connection, uint16_t response_len, uint8_t * in_place_buffer){
129     uint8_t pos = 0;
130     in_place_buffer[pos++] = HCI_EVENT_AVRCP_META;
131     // skip len
132     pos++;
133     in_place_buffer[pos++] = AVRCP_SUBEVENT_CUSTOM_COMMAND_RESPONSE;
134     little_endian_store_16(in_place_buffer, pos, connection->avrcp_cid);
135     pos += 2;
136     in_place_buffer[pos++] = (uint8_t)connection->command_type;
137     in_place_buffer[pos++] = (uint8_t)connection->pdu_id;
138     little_endian_store_16(in_place_buffer, pos, response_len);
139     pos += 2;
140     in_place_buffer[1] = pos + response_len - 2;
141 }
142 
143 static void avrcp_controller_emit_notification_complete(avrcp_connection_t * connection, uint8_t status, uint8_t event_id, bool enabled){
144     uint8_t event[8];
145     uint8_t pos = 0;
146     event[pos++] = HCI_EVENT_AVRCP_META;
147     event[pos++] = sizeof(event) - 2;
148     event[pos++] = AVRCP_SUBEVENT_NOTIFICATION_STATE;
149     little_endian_store_16(event, pos, connection->avrcp_cid);
150     pos += 2;
151     event[pos++] = status;
152     event[pos++] = enabled ? 1 : 0;
153     event[pos++] = event_id;
154     UNUSED(pos);
155     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
156 }
157 
158 static void avrcp_controller_emit_supported_events(avrcp_connection_t * connection){
159     uint8_t ctype = (uint8_t) AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
160     uint8_t event_id;
161 
162     for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){
163         if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){
164             continue;
165         }
166         uint8_t event[8];
167         uint8_t pos = 0;
168         event[pos++] = HCI_EVENT_AVRCP_META;
169         event[pos++] = sizeof(event) - 2;
170         event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID;
171         little_endian_store_16(event, pos, connection->avrcp_cid);
172         pos += 2;
173         event[pos++] = ctype;
174         event[pos++] = 0;
175         event[pos++] = event_id;
176         UNUSED(pos);
177         (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
178     }
179 
180     uint8_t event[7];
181     uint8_t pos = 0;
182     event[pos++] = HCI_EVENT_AVRCP_META;
183     event[pos++] = sizeof(event) - 2;
184     event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE;
185     little_endian_store_16(event, pos, connection->avrcp_cid);
186     pos += 2;
187     event[pos++] = ctype;
188     event[pos++] = 0;
189     UNUSED(pos);
190     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
191 }
192 
193 static void avrcp_controller_emit_notification_for_event_id(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id,
194                                                             avrcp_command_type_t ctype, const uint8_t *payload,
195                                                             uint16_t size) {
196     switch (event_id){
197         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{
198             if (size < 4) break;
199             uint32_t song_position = big_endian_read_32(payload, 0);
200             uint16_t offset = 0;
201             uint8_t event[10];
202             event[offset++] = HCI_EVENT_AVRCP_META;
203             event[offset++] = sizeof(event) - 2;
204             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED;
205             little_endian_store_16(event, offset, avrcp_cid);
206             offset += 2;
207             event[offset++] = ctype;
208             little_endian_store_32(event, offset, song_position);
209             offset += 4;
210             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
211             break;
212         }
213         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{
214             if (size < 1) break;
215             uint16_t offset = 0;
216             uint8_t event[7];
217             event[offset++] = HCI_EVENT_AVRCP_META;
218             event[offset++] = sizeof(event) - 2;
219             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED;
220             little_endian_store_16(event, offset, avrcp_cid);
221             offset += 2;
222             event[offset++] = ctype;
223             event[offset++] = payload[0];
224             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
225             break;
226         }
227         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{
228             uint16_t offset = 0;
229             uint8_t event[6];
230             event[offset++] = HCI_EVENT_AVRCP_META;
231             event[offset++] = sizeof(event) - 2;
232             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED;
233             little_endian_store_16(event, offset, avrcp_cid);
234             offset += 2;
235             event[offset++] = ctype;
236             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
237             break;
238         }
239         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{
240             uint16_t offset = 0;
241             uint8_t event[6];
242             event[offset++] = HCI_EVENT_AVRCP_META;
243             event[offset++] = sizeof(event) - 2;
244             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED;
245             little_endian_store_16(event, offset, avrcp_cid);
246             offset += 2;
247             event[offset++] = ctype;
248             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
249             break;
250         }
251         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{
252             uint16_t offset = 0;
253             uint8_t event[6];
254             event[offset++] = HCI_EVENT_AVRCP_META;
255             event[offset++] = sizeof(event) - 2;
256             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED;
257             little_endian_store_16(event, offset, avrcp_cid);
258             offset += 2;
259             event[offset++] = ctype;
260             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
261             break;
262         }
263         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{
264             if (size < 1) break;
265             uint16_t offset = 0;
266             uint8_t event[7];
267             event[offset++] = HCI_EVENT_AVRCP_META;
268             event[offset++] = sizeof(event) - 2;
269             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
270             little_endian_store_16(event, offset, avrcp_cid);
271             offset += 2;
272             event[offset++] = ctype;
273             event[offset++] = payload[0] & 0x7F;
274             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
275             break;
276         }
277         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{
278             if (size < 2) break;
279             uint8_t event[8];
280             uint16_t offset = 0;
281             uint16_t uuid = big_endian_read_16(payload, 0);
282             event[offset++] = HCI_EVENT_AVRCP_META;
283             event[offset++] = sizeof(event) - 2;
284             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED;
285             little_endian_store_16(event, offset, avrcp_cid);
286             offset += 2;
287             event[offset++] = ctype;
288             little_endian_store_16(event, offset, uuid);
289             offset += 2;
290             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
291             break;
292         }
293 
294         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{
295             uint16_t offset = 0;
296             uint8_t event[6];
297             event[offset++] = HCI_EVENT_AVRCP_META;
298             event[offset++] = sizeof(event) - 2;
299             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END;
300             little_endian_store_16(event, offset, avrcp_cid);
301             offset += 2;
302             event[offset++] = ctype;
303             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
304             break;
305         }
306         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{
307             uint16_t offset = 0;
308             uint8_t event[6];
309             event[offset++] = HCI_EVENT_AVRCP_META;
310             event[offset++] = sizeof(event) - 2;
311             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START;
312             little_endian_store_16(event, offset, avrcp_cid);
313             offset += 2;
314             event[offset++] = ctype;
315             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
316             break;
317         }
318         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{
319             if (size < 1) break;
320             uint16_t offset = 0;
321             uint8_t event[7];
322             event[offset++] = HCI_EVENT_AVRCP_META;
323             event[offset++] = sizeof(event) - 2;
324             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED;
325             little_endian_store_16(event, offset, avrcp_cid);
326             offset += 2;
327             event[offset++] = ctype;
328             event[offset++] = payload[0];
329             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
330             break;
331         }
332 
333         case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{
334             if (size < 1) break;
335             uint16_t offset = 0;
336             uint8_t event[7];
337             event[offset++] = HCI_EVENT_AVRCP_META;
338             event[offset++] = sizeof(event) - 2;
339             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED;
340             little_endian_store_16(event, offset, avrcp_cid);
341             offset += 2;
342             event[offset++] = ctype;
343             event[offset++] = payload[0];
344             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
345             break;
346         }
347 
348         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
349         default:
350             log_info("avrcp: not implemented");
351             break;
352     }
353 }
354 
355 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){
356     btstack_assert(callback != NULL);
357 
358     uint8_t event[8];
359     int pos = 0;
360     event[pos++] = HCI_EVENT_AVRCP_META;
361     event[pos++] = sizeof(event) - 2;
362     event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE;
363     little_endian_store_16(event, pos, avrcp_cid);
364     pos += 2;
365     event[pos++] = ctype;
366     event[pos++] = repeat_mode;
367     event[pos++] = shuffle_mode;
368     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
369 }
370 
371 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){
372     uint8_t event[7];
373     int pos = 0;
374     event[pos++] = HCI_EVENT_AVRCP_META;
375     event[pos++] = sizeof(event) - 2;
376     event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE;
377     little_endian_store_16(event, pos, avrcp_cid);
378     pos += 2;
379     event[pos++] = ctype;
380     event[pos++] = status;
381     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
382 }
383 
384 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){
385     uint8_t event[HCI_EVENT_BUFFER_SIZE];
386     uint16_t pos = 0;
387     event[pos++] = HCI_EVENT_AVRCP_META;
388     // reserve one byte for subevent type and data len
389 	uint16_t data_len_pos = pos;
390     pos++;
391 	uint16_t subevent_type_pos = pos;
392     pos++;
393     little_endian_store_16(event, pos, avrcp_cid);
394     pos += 2;
395     event[pos++] = ctype;
396 
397     switch (attr_id){
398         case AVRCP_MEDIA_ATTR_TITLE:
399             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO;
400 			btstack_assert(value_len <= 255);
401             event[pos++] = (uint8_t) value_len;
402             (void)memcpy(event + pos, value, value_len);
403             break;
404         case AVRCP_MEDIA_ATTR_ARTIST:
405             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO;
406 			btstack_assert(value_len <= 255);
407 			event[pos++] = (uint8_t) value_len;
408             (void)memcpy(event + pos, value, value_len);
409             break;
410         case AVRCP_MEDIA_ATTR_ALBUM:
411             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO;
412 			btstack_assert(value_len <= 255);
413 			event[pos++] = (uint8_t) value_len;
414             (void)memcpy(event + pos, value, value_len);
415             break;
416         case AVRCP_MEDIA_ATTR_GENRE:
417             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO;
418 			btstack_assert(value_len <= 255);
419 			event[pos++] = (uint8_t) value_len;
420             (void)memcpy(event + pos, value, value_len);
421             break;
422 #ifdef ENABLE_AVRCP_COVER_ART
423         case AVRCP_MEDIA_ATTR_DEFAULT_COVER_ART:
424             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_COVER_ART_INFO;
425             btstack_assert(value_len <= 255);
426             event[pos++] = (uint8_t) value_len;
427             (void)memcpy(event + pos, value, value_len);
428             break;
429 #endif
430         case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
431             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO;
432             if (value){
433                 little_endian_store_32(event, pos, btstack_atoi((char *)value));
434             } else {
435                 little_endian_store_32(event, pos, 0);
436             }
437             pos += 4;
438             break;
439         case AVRCP_MEDIA_ATTR_TRACK:
440             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO;
441             if (value){
442                 event[pos++] = btstack_atoi((char *)value);
443             } else {
444                 event[pos++] = 0;
445             }
446             break;
447         case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
448             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO;
449             if (value){
450                 event[pos++] = btstack_atoi((char *)value);
451             } else {
452                 event[pos++] = 0;
453             }
454             break;
455         default:
456             break;
457     }
458     event[data_len_pos] = pos - 2;
459     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
460 }
461 
462 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){
463     btstack_assert(callback != NULL);
464 
465     uint8_t event[7];
466     int pos = 0;
467     event[pos++] = HCI_EVENT_AVRCP_META;
468     event[pos++] = sizeof(event) - 2;
469     event[pos++] = subevent;
470     little_endian_store_16(event, pos, avrcp_cid);
471     pos += 2;
472     event[pos++] = ctype;
473     event[pos++] = operation_id;
474     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
475 }
476 
477 static void avrcp_parser_reset(avrcp_connection_t * connection){
478     connection->list_offset = 0;
479     connection->parser_attribute_header_pos = 0;
480     connection->controller_num_received_fragments = 0;
481     connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
482 }
483 
484 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){
485     uint16_t attribute_total_value_len;
486     uint32_t attribute_id;
487     switch(connection->parser_state){
488         case AVRCP_PARSER_GET_ATTRIBUTE_HEADER:
489             connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte;
490             connection->list_offset++;
491 
492             if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return;
493 
494             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
495             connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTE_SIZE);
496             if (connection->attribute_value_len > 0){
497                 // get ready for attribute value
498                 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE;
499                 return;
500             }
501 
502             // emit empty attribute
503             attribute_id = big_endian_read_32(connection->parser_attribute_header, 0);
504             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);
505 
506             // done, see below
507             break;
508 
509         case AVRCP_PARSER_GET_ATTRIBUTE_VALUE:
510             connection->attribute_value[connection->attribute_value_offset++] = byte;
511             connection->list_offset++;
512 
513             if (connection->attribute_value_offset < connection->attribute_value_len) return;
514 
515             // emit (potentially partial) attribute
516             attribute_id = big_endian_read_32(connection->parser_attribute_header, 0);
517             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);
518 
519             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
520             if (connection->attribute_value_offset < attribute_total_value_len){
521                 // ignore rest of attribute
522                 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE;
523                 return;
524             }
525 
526             // done, see below
527             break;
528 
529         case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE:
530             connection->attribute_value_offset++;
531             connection->list_offset++;
532 
533             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
534             if (connection->attribute_value_offset < attribute_total_value_len) return;
535 
536             // done, see below
537             break;
538 
539         default:
540             return;
541     }
542 
543     // attribute fully read, check if more to come
544     if (connection->list_offset < connection->list_size){
545         // more to come, reset parser
546         connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
547         connection->parser_attribute_header_pos = 0;
548         connection->attribute_value_offset = 0;
549     } else {
550         // fully done
551         avrcp_parser_reset(connection);
552         avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0);
553     }
554 }
555 
556 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){
557     int i;
558     for (i=0;i<num_bytes_to_read;i++){
559         avrcp_parser_process_byte(packet[i], connection, ctype);
560     }
561 }
562 
563 static void avrcp_send_cmd_with_avctp_fragmentation(avrcp_connection_t * connection){
564     l2cap_reserve_packet_buffer();
565     uint8_t * packet = l2cap_get_outgoing_buffer();
566 
567     uint16_t max_payload_size;
568     connection->avctp_packet_type = avctp_get_packet_type(connection, &max_payload_size);
569     connection->avrcp_packet_type = avrcp_get_packet_type(connection);
570 
571     // non-fragmented: transport header (1) + PID (2)
572     // fragmented:     transport header (1) + num packets (1) + PID (2)
573 
574     uint16_t param_len = connection->data_len;
575     // AVCTP header
576     // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
577     uint16_t pos = 0;
578     packet[pos++] = (connection->transaction_id << 4) | (connection->avctp_packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
579 
580     if (connection->avctp_packet_type == AVCTP_START_PACKET){
581         uint16_t max_frame_size = btstack_min(connection->l2cap_mtu, AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE);
582         // first packet: max_payload_size
583         // rest packets
584         uint16_t num_payload_bytes = param_len - max_payload_size;
585         uint16_t frame_size_for_continue_packet = max_frame_size - avctp_get_num_bytes_for_header(AVCTP_CONTINUE_PACKET);
586         uint16_t num_avctp_packets = (num_payload_bytes + frame_size_for_continue_packet - 1)/frame_size_for_continue_packet + 1;
587 		btstack_assert(num_avctp_packets <= 255);
588         packet[pos++] = (uint8_t) num_avctp_packets;
589     }
590 
591     switch (connection->avctp_packet_type){
592         case AVCTP_SINGLE_PACKET:
593         case AVCTP_START_PACKET:
594             connection->data_offset = 0;
595             // Profile IDentifier (PID)
596             packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
597             packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
598 
599             // command_type
600             packet[pos++] = connection->command_type;
601             // subunit_type | subunit ID
602             packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
603             // opcode
604             packet[pos++] = (uint8_t)connection->command_opcode;
605 
606             switch (connection->command_opcode){
607                 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
608                     big_endian_store_24(packet, pos, connection->company_id);
609                     pos += 3;
610                     packet[pos++] = connection->pdu_id;
611                     packet[pos++] = connection->avrcp_packet_type;              // reserved(upper 6) | AVRCP packet_type
612                     big_endian_store_16(packet, pos, connection->data_len);     // parameter length
613                     pos += 2;
614                     break;
615                 case AVRCP_CMD_OPCODE_PASS_THROUGH:
616                     packet[pos++] = connection->operation_id;
617                     packet[pos++] = (uint8_t)connection->data_len;     // parameter length
618                     if ((connection->operation_id & 0x7F) != AVRCP_OPERATION_ID_VENDOR_UNIQUE){
619                         pos += 2;
620                     }
621                     break;
622                 case AVRCP_CMD_OPCODE_UNIT_INFO:
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;
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             UNUSED(pos);
1217             switch (connection->state){
1218                 case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
1219                     // trigger release for simple command:
1220                     if (!connection->controller_press_and_hold_cmd_active){
1221                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
1222                         break;
1223                     }
1224                     // for press and hold, send release if it just has been requested, otherwise, wait for next repeat
1225                     if (connection->controller_press_and_hold_cmd_release){
1226                         connection->controller_press_and_hold_cmd_release = false;
1227                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
1228                     } else {
1229                         connection->state = AVCTP_W4_STOP;
1230                     }
1231                     break;
1232                 case AVCTP_W2_RECEIVE_RESPONSE:
1233                     connection->state = AVCTP_CONNECTION_OPENED;
1234                     break;
1235                 default:
1236                     break;
1237             }
1238             if (connection->state == AVCTP_W4_STOP){
1239                 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id);
1240             }
1241             if (connection->state == AVCTP_CONNECTION_OPENED) {
1242                 // RELEASE response
1243                 operation_id = operation_id & 0x7F;
1244                 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id);
1245             }
1246             if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){
1247                 // PRESS response
1248                 avrcp_controller_request_pass_through_release_control_cmd(connection);
1249             }
1250             break;
1251         }
1252         default:
1253             break;
1254     }
1255 
1256     // trigger pending notification reqistrations
1257     if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->controller_notifications_to_register){
1258         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1259     }
1260 }
1261 
1262 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){
1263     // identifier (9) + num attributes (1) + attributes (4 * num attributes)
1264     uint8_t get_element_attributes_command[9+8*((AVRCP_MEDIA_ATTR_RESERVED-AVRCP_MEDIA_ATTR_TITLE) + 1)];
1265     uint16_t pos;
1266     uint16_t num_attributes_index;
1267     uint8_t i;
1268 
1269     switch (connection->state){
1270         case AVCTP_W2_SEND_PRESS_COMMAND:
1271             avrcp_send_cmd_with_avctp_fragmentation(connection);
1272             connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE;
1273             return;
1274 
1275         case AVCTP_W2_SEND_RELEASE_COMMAND:
1276             avrcp_send_cmd_with_avctp_fragmentation(connection);
1277             connection->state = AVCTP_W2_RECEIVE_RESPONSE;
1278             return;
1279 
1280         case AVCTP_W2_SEND_COMMAND:
1281             avrcp_send_cmd_with_avctp_fragmentation(connection);
1282             if (connection->data_offset < connection->data_len){
1283                 // continue AVCTP fragmentation
1284                 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1285                 return;
1286             }
1287             connection->state = AVCTP_W2_RECEIVE_RESPONSE;
1288             return;
1289         case AVCTP_W2_SEND_GET_ELEMENT_ATTRIBUTES_REQUEST:
1290             // build command in local buffer
1291             pos = 0;
1292             connection->data = get_element_attributes_command;
1293             // write identifier
1294             memset(connection->data, 0, 8);
1295             pos += 8;
1296             num_attributes_index = pos;
1297             // If num_attributes is set to zero, all attribute information shall be returned,
1298             // and the AttributeID field is omitted
1299             connection->data[num_attributes_index] = 0;
1300             pos++;
1301             for (i = 0; i < (uint8_t)AVRCP_MEDIA_ATTR_RESERVED - (uint8_t)AVRCP_MEDIA_ATTR_TITLE; i++){
1302                 if ((connection->controller_element_attributes & (1<<i)) != 0){
1303                     // every attribute is 4 bytes long
1304                     big_endian_store_32(connection->data, pos, AVRCP_MEDIA_ATTR_TITLE + i);
1305                     pos += 4;
1306                     connection->data[num_attributes_index]++;
1307                 }
1308             }
1309 
1310             // Parameter Length
1311             connection->data_len = pos;
1312 
1313             log_info("AVCTP_W2_SEND_GET_ELEMENT_ATTRIBUTES_REQUEST, len %u", connection->data_len);
1314 
1315             // send data - assume it will fit into one packet
1316             avrcp_send_cmd_with_avctp_fragmentation(connection);
1317             connection->state = AVCTP_W2_RECEIVE_RESPONSE;
1318             return;
1319         default:
1320             break;
1321     }
1322 
1323     // send register notification if queued,
1324     // avrcp_handle_l2cap_data_packet_for_signaling_connection will trigger next one
1325     if (connection->controller_notifications_to_register != 0){
1326         uint8_t event_id;
1327         for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){
1328             if (connection->controller_notifications_to_register & (1 << event_id)){
1329                 connection->controller_notifications_to_register &= ~ (1 << event_id);
1330                 avrcp_send_register_notification(connection, event_id);
1331                 return;
1332             }
1333         }
1334     }
1335 }
1336 
1337 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1338     avrcp_connection_t * connection;
1339 
1340     switch (packet_type) {
1341         case L2CAP_DATA_PACKET:
1342             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1343             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1344             break;
1345 
1346         case HCI_EVENT_PACKET:
1347             switch (hci_event_packet_get_type(packet)){
1348                 case L2CAP_EVENT_CAN_SEND_NOW:
1349                     connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1350                     avrcp_controller_handle_can_send_now(connection);
1351                     break;
1352             default:
1353                 break;
1354         }
1355         default:
1356             break;
1357     }
1358 }
1359 
1360 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){
1361     if (service_name == NULL){
1362         service_name = avrcp_default_controller_service_name;
1363     }
1364     if (service_provider_name == NULL){
1365         service_provider_name = avrcp_default_controller_service_provider_name;
1366     }
1367     avrcp_create_sdp_record(true, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
1368 }
1369 
1370 void avrcp_controller_init(void){
1371     avrcp_controller_context.role = AVRCP_CONTROLLER;
1372     avrcp_controller_context.packet_handler = avrcp_controller_packet_handler;
1373     avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler);
1374 }
1375 
1376 void avrcp_controller_deinit(void){
1377     memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t));
1378 }
1379 
1380 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){
1381     btstack_assert(callback != NULL);
1382     avrcp_controller_context.avrcp_callback = callback;
1383 }
1384 
1385 
1386 uint8_t avrcp_controller_play(uint16_t avrcp_cid){
1387     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1388 }
1389 
1390 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){
1391     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1392 }
1393 
1394 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){
1395     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1396 }
1397 
1398 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){
1399     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1400 }
1401 
1402 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){
1403     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1404 }
1405 
1406 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){
1407     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1408 }
1409 
1410 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){
1411     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1412 }
1413 
1414 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){
1415     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1416 }
1417 
1418 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){
1419     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0);
1420 }
1421 
1422 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){
1423     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1424 }
1425 
1426 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){
1427     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1428 }
1429 
1430 /* start continuous cmds */
1431 
1432 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){
1433     return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0);
1434 }
1435 
1436 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){
1437     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1438 }
1439 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){
1440     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1441 }
1442 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){
1443     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1444 }
1445 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){
1446     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1447 }
1448 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){
1449     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1450 }
1451 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){
1452     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1453 }
1454 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){
1455     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1456 }
1457 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){
1458     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1459 }
1460 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){
1461     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1462 }
1463 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){
1464     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1465 }
1466 
1467 static uint8_t avrcp_controller_request_group_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_group_operation_id_t opid){
1468     log_info("Send group command %d", opid);
1469     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1470     if (!connection){
1471         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1472     }
1473 
1474     if (connection->state != AVCTP_CONNECTION_OPENED){
1475         log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid);
1476         return ERROR_CODE_COMMAND_DISALLOWED;
1477     }
1478     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
1479     avrcp_controller_pass_through_command_data_init(connection, AVRCP_OPERATION_ID_VENDOR_UNIQUE);
1480     connection->company_id = BT_SIG_COMPANY_ID;
1481     connection->data_len = 5;
1482     big_endian_store_24(connection->data, 0, BT_SIG_COMPANY_ID);
1483     big_endian_store_16(connection->data, 3, opid);
1484 
1485     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1486     return ERROR_CODE_SUCCESS;
1487 }
1488 
1489 uint8_t avrcp_controller_next_group(uint16_t avrcp_cid){
1490     return avrcp_controller_request_group_pass_through_press_control_cmd(avrcp_cid, AVRCP_GROUP_OPERATION_ID_GOTO_NEXT);
1491 }
1492 
1493 uint8_t avrcp_controller_previous_group(uint16_t avrcp_cid){
1494     return avrcp_controller_request_group_pass_through_press_control_cmd(avrcp_cid, AVRCP_GROUP_OPERATION_ID_GOTO_PREVIOUS);
1495 }
1496 
1497 
1498 /* stop continuous cmds */
1499 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){
1500     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1501     if (!connection){
1502         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1503     }
1504 
1505     switch (connection->state){
1506         // respond when we receive response for (repeated) press command
1507         case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
1508             connection->controller_press_and_hold_cmd_release = true;
1509             break;
1510 
1511         // release already sent or on the way, nothing to do
1512         case AVCTP_W2_RECEIVE_RESPONSE:
1513         case AVCTP_W2_SEND_RELEASE_COMMAND:
1514             break;
1515 
1516         // about to send next repeated press command or wait for it -> release right away
1517         case AVCTP_W2_SEND_PRESS_COMMAND:
1518         case AVCTP_W4_STOP:
1519             return avrcp_controller_request_pass_through_release_control_cmd(connection);
1520 
1521         // otherwise reject request
1522         default:
1523             return ERROR_CODE_COMMAND_DISALLOWED;
1524     }
1525     return ERROR_CODE_SUCCESS;
1526 }
1527 
1528 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1529     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1530     if (!connection){
1531         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1532     }
1533     return avrcp_controller_register_notification(connection, event_id);
1534 }
1535 
1536 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1537     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1538     if (!connection){
1539         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1540     }
1541     if (connection->remote_capabilities_state != AVRCP_REMOTE_CAPABILITIES_KNOWN){
1542         return ERROR_CODE_COMMAND_DISALLOWED;
1543     }
1544 
1545     if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){
1546         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1547     }
1548 
1549     if ((connection->notifications_enabled & (1 << event_id)) == 0){
1550         return ERROR_CODE_SUCCESS;
1551     }
1552 
1553     connection->controller_notifications_to_deregister |= (1 << event_id);
1554     return ERROR_CODE_SUCCESS;
1555 }
1556 
1557 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){
1558     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1559     if (!connection){
1560         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1561     }
1562     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1563 
1564     connection->state = AVCTP_W2_SEND_COMMAND;
1565     avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_STATUS,
1566                                               AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED,
1567                                               0);
1568 
1569     connection->data = connection->message_body;
1570     memset(connection->data, 0xFF, 5);
1571     connection->data_len = 5;
1572     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1573     return ERROR_CODE_SUCCESS;
1574 }
1575 
1576 uint8_t avrcp_controller_subunit_info(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) return ERROR_CODE_COMMAND_DISALLOWED;
1582 
1583     connection->state = AVCTP_W2_SEND_COMMAND;
1584     avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, AVRCP_CTYPE_STATUS,
1585                                               AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED,
1586                                               0);
1587 
1588     connection->data = connection->message_body;
1589     memset(connection->data, 0xFF, 5);
1590     connection->data[0] = 7; // page: 0, extension_code: 7
1591     connection->data_len = 5;
1592     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1593     return ERROR_CODE_SUCCESS;
1594 }
1595 
1596 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){
1597     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1598     if (!connection){
1599         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1600     }
1601     if (connection->state != AVCTP_CONNECTION_OPENED){
1602         return ERROR_CODE_COMMAND_DISALLOWED;
1603     }
1604     avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY);
1605     return ERROR_CODE_SUCCESS;
1606 }
1607 
1608 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){
1609     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1610     if (!connection){
1611         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1612     }
1613     if (connection->state != AVCTP_CONNECTION_OPENED){
1614         return ERROR_CODE_COMMAND_DISALLOWED;
1615     }
1616 
1617     switch (connection->remote_capabilities_state){
1618         case AVRCP_REMOTE_CAPABILITIES_NONE:
1619             connection->remote_capabilities_state = AVRCP_REMOTE_CAPABILITIES_W4_QUERY_RESULT;
1620             avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT);
1621             break;
1622         case AVRCP_REMOTE_CAPABILITIES_KNOWN:
1623             avrcp_controller_emit_supported_events(connection);
1624             break;
1625         default:
1626             break;
1627     }
1628     return ERROR_CODE_SUCCESS;
1629 }
1630 
1631 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){
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     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1637 
1638     connection->state = AVCTP_W2_SEND_COMMAND;
1639     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAY_STATUS, true);
1640 
1641     connection->data_len = 0;
1642     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1643     return ERROR_CODE_SUCCESS;
1644 }
1645 
1646 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){
1647     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1648     if (!connection){
1649         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1650     }
1651     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1652 
1653     connection->state = AVCTP_W2_SEND_COMMAND;
1654     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ADDRESSED_PLAYER, true);
1655 
1656     connection->data_len = 2;
1657     big_endian_store_16(connection->data, 0, addressed_player_id);
1658 
1659     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1660     return ERROR_CODE_SUCCESS;
1661 }
1662 
1663 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){
1664     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1665     if (!connection){
1666         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1667     }
1668 
1669     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1670 
1671     if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) {
1672         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
1673     }
1674 
1675     connection->state = AVCTP_W2_SEND_GET_ELEMENT_ATTRIBUTES_REQUEST;
1676     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES, true);
1677 
1678     // collect element attributes
1679     uint8_t i;
1680     uint16_t attributes_set = 0;
1681     for (i = 0; i < num_attributes; i++){
1682         // ignore invalid attribute ID and "get all attributes"
1683         if (AVRCP_MEDIA_ATTR_ALL < attributes[i] && attributes[i] < AVRCP_MEDIA_ATTR_RESERVED){
1684             attributes_set |= 1 << (attributes[i] - AVRCP_MEDIA_ATTR_TITLE);
1685         }
1686     }
1687     connection->controller_element_attributes = attributes_set;
1688 
1689     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1690     return ERROR_CODE_SUCCESS;
1691 }
1692 
1693 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){
1694     return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL);
1695 }
1696 
1697 uint8_t avrcp_controller_get_now_playing_info_for_media_attribute_id(uint16_t avrcp_cid, avrcp_media_attribute_id_t media_attribute_id){
1698     if (media_attribute_id == AVRCP_MEDIA_ATTR_ALL){
1699         return avrcp_controller_get_now_playing_info(avrcp_cid);
1700     }
1701     avrcp_media_attribute_id_t media_attrs[1];
1702     media_attrs[0] = media_attribute_id;
1703     return avrcp_controller_get_element_attributes(avrcp_cid, 1, media_attrs);
1704 }
1705 
1706 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){
1707      avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1708     if (!connection){
1709         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1710     }
1711 
1712     //
1713     // allow sending of multiple set abs volume commands without waiting for response
1714     //
1715     uint8_t status = ERROR_CODE_COMMAND_DISALLOWED;
1716     switch (connection->state){
1717         case AVCTP_CONNECTION_OPENED:
1718             status = ERROR_CODE_SUCCESS;
1719             break;
1720         case AVCTP_W2_RECEIVE_RESPONSE:
1721             // - is pending response also set abs volume
1722             if (connection->command_opcode  != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break;
1723             if (connection->command_type    != AVRCP_CTYPE_CONTROL)               break;
1724             if (connection->subunit_type    != AVRCP_SUBUNIT_TYPE_PANEL)          break;
1725             if (connection->subunit_id      != AVRCP_SUBUNIT_ID)                  break;
1726             if (connection->pdu_id          != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME)  break;
1727             // - is next transaction id valid in window
1728             if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break;
1729             status = ERROR_CODE_SUCCESS;
1730             break;
1731         default:
1732             break;
1733     }
1734     if (status != ERROR_CODE_SUCCESS) return status;
1735 
1736     connection->state = AVCTP_W2_SEND_COMMAND;
1737     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME, true);
1738 
1739     // Parameter Length
1740     connection->data_len = 1;
1741     connection->data[0] = volume;
1742 
1743     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1744     return ERROR_CODE_SUCCESS;
1745 }
1746 
1747 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){
1748     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1749     if (!connection){
1750         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1751     }
1752     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1753 
1754     connection->state = AVCTP_W2_SEND_COMMAND;
1755     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, true);
1756 
1757     connection->data_len = 5;
1758     connection->data[0] = 4;                     // NumPlayerApplicationSettingAttributeID
1759     // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133
1760     connection->data[1] = 0x01;   // equalizer  (1-OFF, 2-ON)
1761     connection->data[2] = 0x02;   // repeat     (1-off, 2-single track, 3-all tracks, 4-group repeat)
1762     connection->data[3] = 0x03;   // shuffle    (1-off, 2-all tracks, 3-group shuffle)
1763     connection->data[4] = 0x04;   // scan       (1-off, 2-all tracks, 3-group scan)
1764 
1765     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1766     return ERROR_CODE_SUCCESS;
1767 }
1768 
1769 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){
1770     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1771     if (!connection){
1772         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1773     }
1774     if (connection->state != AVCTP_CONNECTION_OPENED) 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_SET_PLAYER_APPLICATION_SETTING_VALUE, true);
1778 
1779     // Parameter Length
1780     connection->data_len = 3;
1781     connection->data[0]  = 2;
1782     connection->data[1]  = attr_id;
1783     connection->data[2]  = attr_value;
1784 
1785     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1786     return ERROR_CODE_SUCCESS;
1787 }
1788 
1789 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){
1790     if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1791     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode);
1792 }
1793 
1794 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){
1795     if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1796     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode);
1797 }
1798 
1799 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){
1800     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1801     if (!connection){
1802         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1803     }
1804     if (connection->state != AVCTP_CONNECTION_OPENED){
1805         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1806         return ERROR_CODE_COMMAND_DISALLOWED;
1807     }
1808     connection->state = AVCTP_W2_SEND_COMMAND;
1809     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_PLAY_ITEM, true);
1810 
1811     // Parameter Length
1812     connection->data_len = 11;
1813     connection->data[0]  = scope;
1814     memset(&connection->data[1], 0, 8);
1815     if (uid){
1816         (void)memcpy(&connection->data[1], uid, 8);
1817     }
1818     big_endian_store_16(connection->data, 9, uid_counter);
1819 
1820     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1821     return ERROR_CODE_SUCCESS;
1822 }
1823 
1824 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){
1825     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1826     if (!connection){
1827         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1828     }
1829     if (connection->state != AVCTP_CONNECTION_OPENED){
1830         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1831         return ERROR_CODE_COMMAND_DISALLOWED;
1832     }
1833 
1834     connection->state = AVCTP_W2_SEND_COMMAND;
1835     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_ADD_TO_NOW_PLAYING, true);
1836 
1837     // Parameter Length
1838     connection->data_len = 11;
1839     connection->data[0]  = scope;
1840     memset(&connection->data[1], 0, 8);
1841     if (uid){
1842         (void)memcpy(&connection->data[1], uid, 8);
1843     }
1844     big_endian_store_16(connection->data, 9, uid_counter);
1845 
1846     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1847     return ERROR_CODE_SUCCESS;
1848 }
1849 
1850 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){
1851     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1852     if (!connection){
1853         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1854     }
1855     connection->controller_max_num_fragments = max_num_fragments;
1856     return ERROR_CODE_SUCCESS;
1857 }
1858 
1859 
1860 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid,
1861     avrcp_command_type_t command_type,
1862     avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id,
1863     avrcp_pdu_id_t pdu_id, uint32_t company_id,
1864     const uint8_t * data, uint16_t data_len){
1865 
1866     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1867     if (!connection){
1868         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1869     }
1870     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1871 
1872     connection->state = AVCTP_W2_SEND_COMMAND;
1873     avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_VENDOR_DEPENDENT, command_type, subunit_type,
1874                                               subunit_id, pdu_id, company_id);
1875 
1876     connection->data = (uint8_t *)data;
1877     connection->data_len = data_len;
1878 
1879     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1880     return ERROR_CODE_SUCCESS;
1881 }
1882 
1883 uint8_t avrcp_controller_force_send_press_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid){
1884     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1885     if (!connection){
1886         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1887     }
1888     if (connection->state < AVCTP_CONNECTION_OPENED){
1889         return ERROR_CODE_COMMAND_DISALLOWED;
1890     }
1891 
1892     connection->state = AVCTP_W2_SEND_COMMAND;
1893     avrcp_controller_pass_through_command_data_init(connection, opid);
1894     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1895     return ERROR_CODE_SUCCESS;
1896 }