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