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