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