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