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