xref: /btstack/src/classic/avrcp_controller.c (revision 66b242bbd9ba4c4f1e5a61cd1a7e41aa511e6eb8)
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 << 16;
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 
551     switch (avctp_packet_type){
552         case AVCTP_SINGLE_PACKET:
553         case AVCTP_START_PACKET:
554             if (avctp_packet_type == AVCTP_START_PACKET){
555                 // num packets: (3 bytes overhead (PID, num packets) + command) / (MTU - transport header).
556                 // to get number of packets using integer division, we subtract 1 from the data e.g. len = 5, packet size 5 => need 1 packet
557                 command[pos++] = ((connection->data_len + 3 - 1) / (max_frame_size - 1)) + 1;
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 
601     // compare number of bytes to store with the remaining buffer size
602     uint16_t bytes_to_copy = btstack_min(connection->data_len - connection->data_offset, max_frame_size - pos);
603 
604     (void)memcpy(command + pos, &connection->data[connection->data_offset], bytes_to_copy);
605     pos += bytes_to_copy;
606     connection->data_offset += bytes_to_copy;
607 
608     l2cap_send_prepared(connection->l2cap_signaling_cid, pos);
609 }
610 
611 static int avrcp_send_register_notification(avrcp_connection_t * connection, uint8_t event_id){
612     uint8_t command[18];
613     uint16_t pos = 0;
614     // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
615     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
616     command[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
617 
618     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
619     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
620     command[pos++] = AVRCP_CTYPE_NOTIFY;
621     command[pos++] = (AVRCP_SUBUNIT_TYPE_PANEL << 3) | AVRCP_SUBUNIT_ID;
622     command[pos++] = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
623 
624     big_endian_store_24(command, pos, BT_SIG_COMPANY_ID);
625     pos += 3;
626     command[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
627     command[pos++] = 0;                       // reserved(upper 6) | packet_type -> 0
628     big_endian_store_16(command, pos, 5);     // parameter length
629     pos += 2;
630     command[pos++] = event_id;
631     big_endian_store_32(command, pos, 1);     // send notification on playback position every second, for other notifications it is ignored
632     pos += 4;
633     return l2cap_send(connection->l2cap_signaling_cid, command, pos);
634 }
635 
636 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){
637     UNUSED(timer);
638     avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer);
639     btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout
640     btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer);
641     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
642     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
643 }
644 
645 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){
646     btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer);
647     btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler);
648     btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection);
649     btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout
650     btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer);
651 }
652 
653 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){
654     connection->press_and_hold_cmd_active = false;
655     btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer);
656 }
657 
658 
659 static uint8_t avrcp_controller_request_pass_through_release_control_cmd(avrcp_connection_t * connection){
660     connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
661     if (connection->press_and_hold_cmd_active){
662         avrcp_press_and_hold_timer_stop(connection);
663     }
664     connection->operation_id = 0x80 | connection->operation_id;
665     connection->transaction_id = avrcp_controller_get_next_transaction_label(connection);
666     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
667     return ERROR_CODE_SUCCESS;
668 }
669 
670 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){
671     log_info("Send command %d", opid);
672     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
673     if (!connection){
674         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
675     }
676 
677     if (connection->state != AVCTP_CONNECTION_OPENED){
678         log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid);
679         return ERROR_CODE_COMMAND_DISALLOWED;
680     }
681     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
682     avrcp_controller_pass_through_command_data_init(connection, opid);
683 
684     if (playback_speed > 0){
685         connection->data[0] = playback_speed;
686         connection->data_len = 1;
687     }
688 
689     connection->press_and_hold_cmd_active = continuous_cmd;
690     if (connection->press_and_hold_cmd_active){
691         avrcp_press_and_hold_timer_start(connection);
692     }
693 
694     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
695     return ERROR_CODE_SUCCESS;
696 }
697 
698 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){
699     return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, false);
700 }
701 
702 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){
703     return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, true);
704 }
705 
706 static void avrcp_controller_get_capabilities_for_connection(avrcp_connection_t * connection, uint8_t capability_id){
707     connection->state = AVCTP_W2_SEND_COMMAND;
708     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CAPABILITIES);
709 
710     // Parameter Length
711     connection->data_len = 1;
712     connection->data[0] = capability_id;  // capability ID
713 
714     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
715 }
716 
717 static uint8_t avrcp_controller_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){
718     if (connection->target_supported_notifications_queried && (connection->target_supported_notifications & (1 << event_id)) == 0){
719         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
720     }
721     if ( (connection->notifications_to_deregister & (1 << event_id)) != 0){
722         return ERROR_CODE_COMMAND_DISALLOWED;
723     }
724     if ( (connection->notifications_enabled & (1 << event_id)) != 0){
725         return ERROR_CODE_SUCCESS;
726     }
727     connection->notifications_to_register |= (1 << event_id);
728 
729     if (!connection->target_supported_notifications_queried){
730         connection->target_supported_notifications_suppress_emit_result = true;
731         avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT);
732         return ERROR_CODE_SUCCESS;
733     }
734     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
735     return ERROR_CODE_SUCCESS;
736 }
737 
738 static uint8_t avrcp_controller_request_continuation(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id){
739     connection->state = AVCTP_W2_SEND_COMMAND;
740     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, pdu_id);
741 
742     // Parameter Length
743     connection->data_len = 3;
744     big_endian_store_16(connection->data, 0, 1);
745     connection->data[2] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
746 
747     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
748     return ERROR_CODE_SUCCESS;
749 }
750 
751 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){
752     return avrcp_controller_request_continuation(connection, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE);
753 }
754 
755 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){
756     return avrcp_controller_request_continuation(connection, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE);
757 }
758 
759 static void avrcp_controller_handle_notification(avrcp_connection_t *connection, avrcp_command_type_t ctype, uint8_t *payload, uint16_t size) {
760     if (size < 1) return;
761     uint16_t pos = 0;
762     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) payload[pos++];
763     if ( (event_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){
764         return;
765     }
766 
767     uint16_t event_mask = (1 << event_id);
768     uint16_t reset_event_mask = ~event_mask;
769 
770     switch (ctype){
771         case AVRCP_CTYPE_RESPONSE_REJECTED:
772             connection->notifications_to_deregister &= reset_event_mask;
773             connection->notifications_to_register   &= reset_event_mask;
774             connection->initial_status_reported     &= reset_event_mask;
775             avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, event_id, false);
776             return;
777 
778         case AVRCP_CTYPE_RESPONSE_INTERIM:
779             // register as enabled
780             connection->notifications_enabled |= event_mask;
781 
782             // check if initial value is already sent
783             if ( (connection->initial_status_reported & event_mask) != 0 ){
784                 return;
785             }
786             // emit event only once, initially
787             avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, true);
788             connection->initial_status_reported |= event_mask;
789             // emit initial value after this switch
790             break;
791 
792         case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE:
793             // received change, event is considered de-registered
794             // we are re-enabling it automatically, if it is not
795             // explicitly disabled
796             connection->notifications_enabled &= reset_event_mask;
797             if ((connection->notifications_to_deregister & event_mask) == 0){
798                 avrcp_controller_register_notification(connection, event_id);
799             } else {
800                 connection->notifications_to_deregister &= reset_event_mask;
801                 connection->notifications_to_register   &= reset_event_mask;
802                 connection->initial_status_reported     &= reset_event_mask;
803                 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, false);
804             }
805             break;
806 
807         default:
808             return;
809     }
810 
811     avrcp_controller_emit_notification_for_event_id(connection->avrcp_cid, event_id, ctype, payload + pos, size - pos);
812 }
813 
814 #ifdef ENABLE_AVCTP_FRAGMENTATION
815 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){
816     // after header (transaction label and packet type)
817     uint16_t pos;
818     uint16_t bytes_to_store;
819 
820     switch (packet_type){
821         case AVCTP_START_PACKET:
822             if (size < 2) return;
823 
824             // store header
825             pos = 0;
826             connection->avctp_reassembly_buffer[pos] = packet[pos];
827             pos++;
828             connection->avctp_reassembly_size = pos;
829 
830             // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it
831             pos++;
832 
833             // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called
834 
835             bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size);
836             memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store);
837             connection->avctp_reassembly_size += bytes_to_store;
838             break;
839 
840         case AVCTP_CONTINUE_PACKET:
841         case AVCTP_END_PACKET:
842             if (size < 1) return;
843 
844             // store remaining data, ignore header
845             pos = 1;
846             bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size);
847             memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store);
848             connection->avctp_reassembly_size += bytes_to_store;
849             break;
850 
851         default:
852             return;
853     }
854 }
855 #endif
856 
857 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
858     if (size < 6u) return;
859     uint8_t  pdu_id;
860     avrcp_packet_type_t  vendor_dependent_packet_type;
861 
862     uint16_t pos = 0;
863     connection->last_confirmed_transaction_id = packet[pos] >> 4;
864     avrcp_frame_type_t  frame_type =  (avrcp_frame_type_t)((packet[pos] >> 1) & 0x01);
865     avctp_packet_type_t packet_type = (avctp_packet_type_t)((packet[pos] >> 2) & 0x03);
866     pos++;
867 
868     if (frame_type != AVRCP_RESPONSE_FRAME) return;
869 
870     switch (packet_type){
871         case AVCTP_SINGLE_PACKET:
872             break;
873 
874 #ifdef ENABLE_AVCTP_FRAGMENTATION
875         case AVCTP_START_PACKET:
876         case AVCTP_CONTINUE_PACKET:
877             avctp_reassemble_message(connection, packet_type, packet, size);
878             return;
879 
880         case AVCTP_END_PACKET:
881             avctp_reassemble_message(connection, packet_type, packet, size);
882 
883             packet = connection->avctp_reassembly_buffer;
884             size   = connection->avctp_reassembly_size;
885             break;
886 #endif
887 
888         default:
889             return;
890     }
891 
892     pos += 2; // PID
893 
894     avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++];
895 
896 #ifdef ENABLE_LOG_INFO
897     uint8_t byte_value = packet[pos];
898     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3);
899     avrcp_subunit_type_t subunit_id   = (avrcp_subunit_type_t) (byte_value & 0x07);
900 #endif
901     pos++;
902 
903     uint8_t opcode = packet[pos++];
904     uint16_t param_length;
905 
906     switch (opcode){
907         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
908             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
909             connection->state = AVCTP_CONNECTION_OPENED;
910 
911 #ifdef ENABLE_LOG_INFO
912             // page, extension code (1)
913             pos++;
914             uint8_t unit_type = packet[pos] >> 3;
915             uint8_t max_subunit_ID = packet[pos] & 0x07;
916             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);
917 #endif
918             break;
919         }
920         case AVRCP_CMD_OPCODE_UNIT_INFO:{
921             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
922             connection->state = AVCTP_CONNECTION_OPENED;
923 
924 #ifdef ENABLE_LOG_INFO
925             // byte value 7 (1)
926             pos++;
927             uint8_t unit_type = packet[pos] >> 3;
928             uint8_t unit = packet[pos] & 0x07;
929             pos++;
930             uint32_t company_id = big_endian_read_24(packet, pos);
931             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,
932                 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id);
933 #endif
934             break;
935         }
936         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
937 
938             if ((size - pos) < 7) return;
939 
940             // Company ID (3)
941             pos += 3;
942             pdu_id = packet[pos++];
943             vendor_dependent_packet_type = (avrcp_packet_type_t)(packet[pos++] & 0x03);
944             param_length = big_endian_read_16(packet, pos);
945             pos += 2;
946 
947             if ((size - pos) < param_length) return;
948 
949             // handle asynchronous notifications, without changing state
950             if (pdu_id == AVRCP_PDU_ID_REGISTER_NOTIFICATION){
951                 avrcp_controller_handle_notification(connection, ctype, packet + pos, size - pos);
952                 break;
953             }
954 
955             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE){
956                 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state);
957                 return;
958             }
959             connection->state = AVCTP_CONNECTION_OPENED;
960 
961             log_info("VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype));
962             switch (pdu_id){
963                 case AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:{
964                     uint8_t num_attributes = packet[pos++];
965                     int i;
966                     avrcp_repeat_mode_t  repeat_mode =  AVRCP_REPEAT_MODE_INVALID;
967                     avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID;
968                     for (i = 0; i < num_attributes; i++){
969                         uint8_t attribute_id    = packet[pos++];
970                         uint8_t value = packet[pos++];
971                         switch (attribute_id){
972                             case 0x02:
973                                 repeat_mode = (avrcp_repeat_mode_t) value;
974                                 break;
975                             case 0x03:
976                                 shuffle_mode = (avrcp_shuffle_mode_t) value;
977                                 break;
978                             default:
979                                 break;
980                         }
981                     }
982                     avrcp_controller_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode);
983                     break;
984                 }
985 
986                 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{
987                     uint16_t offset = 0;
988                     uint8_t event[6];
989                     event[offset++] = HCI_EVENT_AVRCP_META;
990                     event[offset++] = sizeof(event) - 2;
991                     event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE;
992                     little_endian_store_16(event, offset, connection->avrcp_cid);
993                     offset += 2;
994                     event[offset++] = ctype;
995                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
996                     break;
997                 }
998 
999                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{
1000                     uint16_t offset = 0;
1001                     uint8_t event[7];
1002                     event[offset++] = HCI_EVENT_AVRCP_META;
1003                     event[offset++] = sizeof(event) - 2;
1004                     event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE;
1005                     little_endian_store_16(event, offset, connection->avrcp_cid);
1006                     offset += 2;
1007                     event[offset++] = ctype;
1008                     event[offset++] = packet[pos++];
1009                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1010                     break;
1011                 }
1012 
1013                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
1014                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++];
1015                     uint8_t capability_count = 0;
1016                     if (param_length > 1){
1017                         capability_count = packet[pos++];
1018                     }
1019                     uint16_t i;
1020                     uint16_t offset = 0;
1021                     uint8_t event[10];
1022 
1023                     switch (capability_id){
1024 
1025                         case AVRCP_CAPABILITY_ID_COMPANY:
1026                             for (i = 0; (i < capability_count) && ((size - pos) >= 3); i++){
1027                                 uint32_t company_id = big_endian_read_24(packet, pos);
1028                                 pos += 3;
1029                                 log_info("  0x%06" PRIx32 ", ", company_id);
1030 
1031                                 offset = 0;
1032                                 event[offset++] = HCI_EVENT_AVRCP_META;
1033                                 event[offset++] = sizeof(event) - 2;
1034                                 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID;
1035                                 little_endian_store_16(event, offset, connection->avrcp_cid);
1036                                 offset += 2;
1037                                 event[offset++] = ctype;
1038                                 event[offset++] = 0;
1039                                 little_endian_store_24(event, offset, company_id);
1040                                 offset += 3;
1041                                 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
1042                             }
1043 
1044                             offset = 0;
1045                             event[offset++] = HCI_EVENT_AVRCP_META;
1046                             event[offset++] = sizeof(event) - 2;
1047                             event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE;
1048                             little_endian_store_16(event, offset, connection->avrcp_cid);
1049                             offset += 2;
1050                             event[offset++] = ctype;
1051                             event[offset++] = 0;
1052                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
1053                             break;
1054 
1055                         case AVRCP_CAPABILITY_ID_EVENT:
1056                             for (i = 0; (i < capability_count) && ((size - pos) >= 1); i++){
1057                                 uint8_t event_id = packet[pos++];
1058                                 connection->target_supported_notifications |= (1 << event_id);
1059                             }
1060 
1061                             // if the get supported events query is triggered by avrcp_controller_enable_notification call,
1062                             // avrcp_controller_emit_supported_events should be suppressed
1063                             if (connection->target_supported_notifications_suppress_emit_result){
1064                                 connection->target_supported_notifications_suppress_emit_result = false;
1065                                 // also, notification might not be supported
1066                                 // if so, emit AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE event to app,
1067                                 // and update notifications_to_register bitmap
1068                                 for (i = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++){
1069                                     if ((connection->notifications_to_register & (1<<i)) != 0){
1070                                         if ((connection->target_supported_notifications & (1<<i)) == 0){
1071                                             avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, i, false);
1072                                             connection->notifications_to_register &= ~(1 << i);
1073                                         }
1074                                     }
1075                                 }
1076                                 break;
1077                             }
1078                             // supported events are emitted only if the get supported events query
1079                             // is triggered by avrcp_controller_get_supported_events call
1080                             avrcp_controller_emit_supported_events(connection);
1081                             break;
1082 
1083                         default:
1084                             // ignore
1085                             break;
1086                     }
1087                     break;
1088                 }
1089 
1090                 case AVRCP_PDU_ID_GET_PLAY_STATUS:{
1091                     uint32_t song_length = big_endian_read_32(packet, pos);
1092                     pos += 4;
1093                     uint32_t song_position = big_endian_read_32(packet, pos);
1094                     pos += 4;
1095                     uint8_t play_status = packet[pos];
1096 
1097                     uint8_t event[15];
1098                     int offset = 0;
1099                     event[offset++] = HCI_EVENT_AVRCP_META;
1100                     event[offset++] = sizeof(event) - 2;
1101                     event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS;
1102                     little_endian_store_16(event, offset, connection->avrcp_cid);
1103                     offset += 2;
1104                     event[offset++] = ctype;
1105                     little_endian_store_32(event, offset, song_length);
1106                     offset += 4;
1107                     little_endian_store_32(event, offset, song_position);
1108                     offset += 4;
1109                     event[offset++] = play_status;
1110                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1111                     break;
1112                 }
1113 
1114                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
1115                     switch (vendor_dependent_packet_type){
1116                         case AVRCP_START_PACKET:
1117                         case AVRCP_SINGLE_PACKET:
1118                             avrcp_parser_reset(connection);
1119                             connection->list_size = param_length;
1120                             connection->num_attributes = packet[pos++];
1121 
1122                             avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
1123                             if (vendor_dependent_packet_type == AVRCP_START_PACKET){
1124                                 avrcp_controller_request_continue_response(connection);
1125                             }
1126                             break;
1127                         case AVRCP_CONTINUE_PACKET:
1128                         case AVRCP_END_PACKET:
1129                             connection->num_received_fragments++;
1130 
1131                             if (connection->num_received_fragments < connection->max_num_fragments){
1132                                 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
1133 
1134                                 if (vendor_dependent_packet_type == AVRCP_CONTINUE_PACKET){
1135                                     avrcp_controller_request_continue_response(connection);
1136                                 }
1137                             } else {
1138                                 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1);
1139                                 avrcp_parser_reset(connection);
1140                                 avrcp_controller_request_abort_continuation(connection);
1141                             }
1142                             break;
1143                         default:
1144                             // TODO check
1145                             btstack_assert(false);
1146                             break;
1147                     }
1148                 }
1149                 default:
1150                     break;
1151             }
1152             break;
1153         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
1154             if ((size - pos) < 1) return;
1155             uint8_t operation_id = packet[pos++];
1156             switch (connection->state){
1157                 case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
1158                     // trigger release for simple command:
1159                     if (!connection->press_and_hold_cmd_active){
1160                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
1161                         break;
1162                     }
1163                     // for press and hold, send release if it just has been requested, otherwise, wait for next repeat
1164                     if (connection->press_and_hold_cmd_release){
1165                         connection->press_and_hold_cmd_release = false;
1166                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
1167                     } else {
1168                         connection->state = AVCTP_W4_STOP;
1169                     }
1170                     break;
1171                 case AVCTP_W2_RECEIVE_RESPONSE:
1172                     connection->state = AVCTP_CONNECTION_OPENED;
1173                     break;
1174                 default:
1175                     break;
1176             }
1177             if (connection->state == AVCTP_W4_STOP){
1178                 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id);
1179             }
1180             if (connection->state == AVCTP_CONNECTION_OPENED) {
1181                 // RELEASE response
1182                 operation_id = operation_id & 0x7F;
1183                 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id);
1184             }
1185             if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){
1186                 // PRESS response
1187                 avrcp_controller_request_pass_through_release_control_cmd(connection);
1188             }
1189             break;
1190         }
1191         default:
1192             break;
1193     }
1194 
1195     // trigger pending notification reqistrations
1196     if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->notifications_to_register){
1197         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1198     }
1199 }
1200 
1201 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){
1202     switch (connection->state){
1203         case AVCTP_W2_SEND_PRESS_COMMAND:
1204         case AVCTP_W2_SEND_COMMAND:
1205         case AVCTP_W2_SEND_RELEASE_COMMAND:
1206         case AVCTP_W2_SEND_AVCTP_FRAGMENTED_MESSAGE:
1207             avrcp_send_cmd_with_avctp_fragmentation(connection);
1208 
1209             if (connection->data_offset < connection->data_len){
1210                 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1211                 return;
1212             }
1213             if (connection->state == AVCTP_W2_SEND_PRESS_COMMAND){
1214                 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE;
1215             } else {
1216                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
1217             }
1218             break;
1219         default:
1220             break;
1221     }
1222 
1223     // send register notification if queued
1224     if (connection->notifications_to_register != 0){
1225         uint8_t event_id;
1226         for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){
1227             if (connection->notifications_to_register & (1<<event_id)){
1228                 connection->notifications_to_register &= ~ (1 << event_id);
1229                 avrcp_send_register_notification(connection, event_id);
1230                 return;
1231             }
1232         }
1233     }
1234 }
1235 
1236 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1237     avrcp_connection_t * connection;
1238 
1239     switch (packet_type) {
1240         case L2CAP_DATA_PACKET:
1241             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1242             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1243             break;
1244 
1245         case HCI_EVENT_PACKET:
1246             switch (hci_event_packet_get_type(packet)){
1247                 case L2CAP_EVENT_CAN_SEND_NOW:
1248                     connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1249                     avrcp_controller_handle_can_send_now(connection);
1250                     break;
1251             default:
1252                 break;
1253         }
1254         default:
1255             break;
1256     }
1257 }
1258 
1259 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){
1260     avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
1261 }
1262 
1263 void avrcp_controller_init(void){
1264     avrcp_controller_context.role = AVRCP_CONTROLLER;
1265     avrcp_controller_context.packet_handler = avrcp_controller_packet_handler;
1266     avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler);
1267 }
1268 
1269 void avrcp_controller_deinit(void){
1270     memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t));
1271 }
1272 
1273 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){
1274     btstack_assert(callback != NULL);
1275     avrcp_controller_context.avrcp_callback = callback;
1276 }
1277 
1278 
1279 uint8_t avrcp_controller_play(uint16_t avrcp_cid){
1280     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1281 }
1282 
1283 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){
1284     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1285 }
1286 
1287 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){
1288     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1289 }
1290 
1291 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){
1292     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1293 }
1294 
1295 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){
1296     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1297 }
1298 
1299 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){
1300     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1301 }
1302 
1303 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){
1304     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1305 }
1306 
1307 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){
1308     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1309 }
1310 
1311 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){
1312     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0);
1313 }
1314 
1315 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){
1316     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1317 }
1318 
1319 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){
1320     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1321 }
1322 
1323 /* start continuous cmds */
1324 
1325 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){
1326     return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0);
1327 }
1328 
1329 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){
1330     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1331 }
1332 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){
1333     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1334 }
1335 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){
1336     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1337 }
1338 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){
1339     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1340 }
1341 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){
1342     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1343 }
1344 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){
1345     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1346 }
1347 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){
1348     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1349 }
1350 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){
1351     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1352 }
1353 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){
1354     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1355 }
1356 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){
1357     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1358 }
1359 
1360 /* stop continuous cmds */
1361 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){
1362     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1363     if (!connection){
1364         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1365     }
1366 
1367     switch (connection->state){
1368         // respond when we receive response for (repeated) press command
1369         case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
1370             connection->press_and_hold_cmd_release = true;
1371             break;
1372 
1373         // release already sent or on the way, nothing to do
1374         case AVCTP_W2_RECEIVE_RESPONSE:
1375         case AVCTP_W2_SEND_RELEASE_COMMAND:
1376             break;
1377 
1378         // about to send next repeated press command or wait for it -> release right away
1379         case AVCTP_W2_SEND_PRESS_COMMAND:
1380         case AVCTP_W4_STOP:
1381             return avrcp_controller_request_pass_through_release_control_cmd(connection);
1382 
1383         // otherwise reject request
1384         default:
1385             return ERROR_CODE_COMMAND_DISALLOWED;
1386     }
1387     return ERROR_CODE_SUCCESS;
1388 }
1389 
1390 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1391     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1392     if (!connection){
1393         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1394     }
1395     return avrcp_controller_register_notification(connection, event_id);
1396 }
1397 
1398 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1399     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1400     if (!connection){
1401         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1402     }
1403     if (!connection->target_supported_notifications_queried){
1404         return ERROR_CODE_COMMAND_DISALLOWED;
1405     }
1406 
1407     if ((connection->target_supported_notifications & (1 << event_id)) == 0){
1408         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1409     }
1410 
1411     if ((connection->notifications_enabled & (1 << event_id)) == 0){
1412         return ERROR_CODE_SUCCESS;
1413     }
1414 
1415     connection->notifications_to_deregister |= (1 << event_id);
1416     return ERROR_CODE_SUCCESS;
1417 }
1418 
1419 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){
1420     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1421     if (!connection){
1422         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1423     }
1424     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1425 
1426     connection->state = AVCTP_W2_SEND_COMMAND;
1427     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);
1428 
1429     memset(connection->data, 0xFF, 5);
1430     connection->data_len = 5;
1431     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1432     return ERROR_CODE_SUCCESS;
1433 }
1434 
1435 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){
1436     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1437     if (!connection){
1438         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1439     }
1440     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1441 
1442     connection->state = AVCTP_W2_SEND_COMMAND;
1443     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);
1444 
1445     memset(connection->data, 0xFF, 5);
1446     connection->data[0] = 7; // page: 0, extention_code: 7
1447     connection->data_len = 5;
1448     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1449     return ERROR_CODE_SUCCESS;
1450 }
1451 
1452 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){
1453     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1454     if (!connection){
1455         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1456     }
1457     if (connection->state != AVCTP_CONNECTION_OPENED){
1458         return ERROR_CODE_COMMAND_DISALLOWED;
1459     }
1460     avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY);
1461     return ERROR_CODE_SUCCESS;
1462 }
1463 
1464 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){
1465     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1466     if (!connection){
1467         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1468     }
1469     if (connection->state != AVCTP_CONNECTION_OPENED){
1470         return ERROR_CODE_COMMAND_DISALLOWED;
1471     }
1472 
1473     if (!connection->target_supported_notifications_queried){
1474         connection->target_supported_notifications_queried = true;
1475         avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT);
1476         return ERROR_CODE_SUCCESS;
1477     }
1478 
1479     avrcp_controller_emit_supported_events(connection);
1480     return ERROR_CODE_SUCCESS;
1481 }
1482 
1483 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){
1484     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1485     if (!connection){
1486         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1487     }
1488     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1489 
1490     connection->state = AVCTP_W2_SEND_COMMAND;
1491     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAY_STATUS);
1492 
1493     connection->data_len = 0;
1494     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1495     return ERROR_CODE_SUCCESS;
1496 }
1497 
1498 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){
1499     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1500     if (!connection){
1501         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1502     }
1503     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1504 
1505     connection->state = AVCTP_W2_SEND_COMMAND;
1506     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ADDRESSED_PLAYER);
1507 
1508     connection->data_len = 2;
1509     big_endian_store_16(connection->data, 0, addressed_player_id);
1510 
1511     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1512     return ERROR_CODE_SUCCESS;
1513 }
1514 
1515 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){
1516     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1517     if (!connection){
1518         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1519     }
1520 
1521     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1522 
1523     if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) {
1524         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
1525     }
1526 
1527     connection->state = AVCTP_W2_SEND_COMMAND;
1528     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES);
1529 
1530     uint8_t pos = 0;
1531     // write 8 bytes value
1532     memset(connection->data, pos, 8); // identifier: PLAYING
1533     pos += 8;
1534 
1535     uint8_t num_attributes_index = pos;
1536     pos++;
1537 
1538     // If num_attributes is set to zero, all attribute information shall be returned,
1539     // and the AttributeID field is omitted
1540     connection->data[num_attributes_index] = 0;
1541     uint8_t i;
1542     for (i = 0; i < num_attributes; i++){
1543         // ignore invalid attribute ID and "get all attributes"
1544         if (AVRCP_MEDIA_ATTR_ALL > attributes[i] && attributes[i] < AVRCP_MEDIA_ATTR_RESERVED){
1545             // every attribute is 4 bytes long
1546             big_endian_store_32(connection->data, pos, attributes[i]);
1547             pos += 4;
1548             connection->data[num_attributes_index]++;
1549         }
1550     }
1551 
1552     // Parameter Length
1553     connection->data_len = pos;
1554 
1555     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1556     return ERROR_CODE_SUCCESS;
1557 }
1558 
1559 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){
1560     return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL);
1561 }
1562 
1563 uint8_t avrcp_controller_get_now_playing_info_for_media_attribute_id(uint16_t avrcp_cid, avrcp_media_attribute_id_t media_attribute_id){
1564     if (media_attribute_id == AVRCP_MEDIA_ATTR_ALL){
1565         return avrcp_controller_get_now_playing_info(avrcp_cid);
1566     }
1567     return avrcp_controller_get_element_attributes(avrcp_cid, 1, &media_attribute_id);
1568 }
1569 
1570 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){
1571      avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1572     if (!connection){
1573         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1574     }
1575 
1576     //
1577     // allow sending of multiple set abs volume commands without waiting for response
1578     //
1579     uint8_t status = ERROR_CODE_COMMAND_DISALLOWED;
1580     switch (connection->state){
1581         case AVCTP_CONNECTION_OPENED:
1582             status = ERROR_CODE_SUCCESS;
1583             break;
1584         case AVCTP_W2_RECEIVE_RESPONSE:
1585             // - is pending response also set abs volume
1586             if (connection->command_opcode  != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break;
1587             if (connection->command_type    != AVRCP_CTYPE_CONTROL)               break;
1588             if (connection->subunit_type    != AVRCP_SUBUNIT_TYPE_PANEL)          break;
1589             if (connection->subunit_id      != AVRCP_SUBUNIT_ID)                  break;
1590             if (connection->pdu_id          != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME)  break;
1591             // - is next transaction id valid in window
1592             if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break;
1593             status = ERROR_CODE_SUCCESS;
1594             break;
1595         default:
1596             break;
1597     }
1598     if (status != ERROR_CODE_SUCCESS) return status;
1599 
1600     connection->state = AVCTP_W2_SEND_COMMAND;
1601     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME);
1602 
1603     // Parameter Length
1604     connection->data_len = 1;
1605     connection->data[0] = volume;
1606 
1607     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1608     return ERROR_CODE_SUCCESS;
1609 }
1610 
1611 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){
1612     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1613     if (!connection){
1614         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1615     }
1616     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1617 
1618     connection->state = AVCTP_W2_SEND_COMMAND;
1619     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE);
1620 
1621     connection->data_len = 5;
1622     connection->data[0] = 4;                     // NumPlayerApplicationSettingAttributeID
1623     // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133
1624     connection->data[1] = 0x01;   // equalizer  (1-OFF, 2-ON)
1625     connection->data[2] = 0x02;   // repeat     (1-off, 2-single track, 3-all tracks, 4-group repeat)
1626     connection->data[3] = 0x03;   // shuffle    (1-off, 2-all tracks, 3-group shuffle)
1627     connection->data[4] = 0x04;   // scan       (1-off, 2-all tracks, 3-group scan)
1628 
1629     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1630     return ERROR_CODE_SUCCESS;
1631 }
1632 
1633 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){
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     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1639 
1640     connection->state = AVCTP_W2_SEND_COMMAND;
1641     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE);
1642 
1643     // Parameter Length
1644     connection->data_len = 3;
1645     connection->data[0]  = 2;
1646     connection->data[1]  = attr_id;
1647     connection->data[2]  = attr_value;
1648 
1649     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1650     return ERROR_CODE_SUCCESS;
1651 }
1652 
1653 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){
1654     if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1655     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode);
1656 }
1657 
1658 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){
1659     if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1660     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode);
1661 }
1662 
1663 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){
1664     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1665     if (!connection){
1666         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1667     }
1668     if (connection->state != AVCTP_CONNECTION_OPENED){
1669         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1670         return ERROR_CODE_COMMAND_DISALLOWED;
1671     }
1672     connection->state = AVCTP_W2_SEND_COMMAND;
1673     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_PLAY_ITEM);
1674 
1675     // Parameter Length
1676     connection->data_len = 11;
1677     connection->data[0]  = scope;
1678     memset(&connection->data[1], 0, 8);
1679     if (uid){
1680         (void)memcpy(&connection->data[1], uid, 8);
1681     }
1682     big_endian_store_16(connection->data, 9, uid_counter);
1683 
1684     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1685     return ERROR_CODE_SUCCESS;
1686 }
1687 
1688 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){
1689     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1690     if (!connection){
1691         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1692     }
1693     if (connection->state != AVCTP_CONNECTION_OPENED){
1694         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1695         return ERROR_CODE_COMMAND_DISALLOWED;
1696     }
1697 
1698     connection->state = AVCTP_W2_SEND_COMMAND;
1699     avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_ADD_TO_NOW_PLAYING);
1700 
1701     // Parameter Length
1702     connection->data_len = 11;
1703     connection->data[0]  = scope;
1704     memset(&connection->data[1], 0, 8);
1705     if (uid){
1706         (void)memcpy(&connection->data[1], uid, 8);
1707     }
1708     big_endian_store_16(connection->data, 9, uid_counter);
1709 
1710     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1711     return ERROR_CODE_SUCCESS;
1712 }
1713 
1714 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){
1715     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1716     if (!connection){
1717         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1718     }
1719     connection->max_num_fragments = max_num_fragments;
1720     return ERROR_CODE_SUCCESS;
1721 }
1722 
1723 
1724 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid,
1725     avrcp_command_type_t command_type,
1726     avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id,
1727     avrcp_pdu_id_t pdu_id, uint32_t company_id,
1728     const uint8_t * data, uint16_t data_len){
1729 
1730     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1731     if (!connection){
1732         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1733     }
1734     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1735 
1736     connection->state = AVCTP_W2_SEND_AVCTP_FRAGMENTED_MESSAGE;
1737     avrcp_controller_custome_command_data_init(connection, AVRCP_CMD_OPCODE_VENDOR_DEPENDENT, command_type, subunit_type, subunit_id, pdu_id, company_id);
1738 
1739     connection->data = (uint8_t *)data;
1740     connection->data_len = data_len;
1741 
1742     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1743     return ERROR_CODE_SUCCESS;
1744 }
1745