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