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