xref: /btstack/src/classic/avrcp_controller.c (revision dbe92de6a93924b91532f8d818af55adf6b370d8)
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 "btstack.h"
45 #include "classic/avrcp.h"
46 #include "classic/avrcp_controller.h"
47 
48 // made public in avrcp_controller.h
49 avrcp_context_t avrcp_controller_context;
50 
51 static int avrcp_controller_supports_browsing(uint16_t controller_supported_features){
52     return controller_supported_features & (1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_BROWSING);
53 }
54 
55 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){
56     avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name);
57 }
58 
59 static void avrcp_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){
60     btstack_assert(callback != NULL);
61 
62     uint8_t event[8];
63     int pos = 0;
64     event[pos++] = HCI_EVENT_AVRCP_META;
65     event[pos++] = sizeof(event) - 2;
66     event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE;
67     little_endian_store_16(event, pos, avrcp_cid);
68     pos += 2;
69     event[pos++] = ctype;
70     event[pos++] = repeat_mode;
71     event[pos++] = shuffle_mode;
72     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
73 }
74 
75 static void avrcp_emit_operation_status(btstack_packet_handler_t callback, uint8_t subevent, uint16_t avrcp_cid, uint8_t ctype, uint8_t operation_id){
76     btstack_assert(callback != NULL);
77 
78     uint8_t event[7];
79     int pos = 0;
80     event[pos++] = HCI_EVENT_AVRCP_META;
81     event[pos++] = sizeof(event) - 2;
82     event[pos++] = subevent;
83     little_endian_store_16(event, pos, avrcp_cid);
84     pos += 2;
85     event[pos++] = ctype;
86     event[pos++] = operation_id;
87     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
88 }
89 
90 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){
91     UNUSED(timer);
92     avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer);
93     btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout
94     btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer);
95     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
96     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
97 }
98 
99 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){
100     btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer);
101     btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler);
102     btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection);
103     btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout
104     btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer);
105 }
106 
107 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){
108     connection->continuous_fast_forward_cmd = 0;
109     btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer);
110 }
111 
112 static uint8_t request_pass_through_release_control_cmd(avrcp_connection_t * connection){
113     connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
114     if (connection->continuous_fast_forward_cmd){
115         avrcp_press_and_hold_timer_stop(connection);
116     }
117     connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0];
118     connection->transaction_label++;
119     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
120     return ERROR_CODE_SUCCESS;
121 }
122 
123 static inline uint8_t request_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed, uint8_t continuous_fast_forward_cmd){
124     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
125     if (!connection){
126         log_error("avrcp: could not find a connection. avrcp cid 0x%02x", avrcp_cid);
127         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
128     }
129 
130     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
131     connection->state = AVCTP_W2_SEND_PRESS_COMMAND;
132     connection->command_opcode =  AVRCP_CMD_OPCODE_PASS_THROUGH;
133     connection->command_type = AVRCP_CTYPE_CONTROL;
134     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
135     connection->subunit_id =   AVRCP_SUBUNIT_ID;
136     connection->cmd_operands_length = 0;
137 
138     connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd;
139     connection->cmd_operands_length = 2;
140     connection->cmd_operands[0] = opid;
141     if (playback_speed > 0){
142         connection->cmd_operands[2] = playback_speed;
143         connection->cmd_operands_length++;
144     }
145     connection->cmd_operands[1] = connection->cmd_operands_length - 2;
146 
147     if (connection->continuous_fast_forward_cmd){
148         avrcp_press_and_hold_timer_start(connection);
149     }
150 
151     connection->transaction_label++;
152     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
153     return ERROR_CODE_SUCCESS;
154 }
155 
156 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){
157     return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0);
158 }
159 
160 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){
161     return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1);
162 }
163 
164 #define AVRCP_CMD_BUFFER_SIZE 30
165 static uint16_t avrcp_get_max_payload_size_for_packet_type(avrcp_packet_type_t packet_type){
166     switch (packet_type){
167         case AVRCP_SINGLE_PACKET:
168             return AVRCP_CMD_BUFFER_SIZE - 3;
169         case AVRCP_START_PACKET:
170             return AVRCP_CMD_BUFFER_SIZE - 4;
171         case AVRCP_CONTINUE_PACKET:
172         case AVRCP_END_PACKET:
173             return AVRCP_CMD_BUFFER_SIZE - 1;
174     }
175     return 0;
176 }
177 
178 static int avrcp_send_cmd(avrcp_connection_t * connection, avrcp_packet_type_t packet_type){
179     uint8_t command[AVRCP_CMD_BUFFER_SIZE];
180     int pos = 0;
181     uint16_t max_bytes = sizeof(command) - 1;
182 
183     // transport header
184     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
185     command[pos++] = (connection->transaction_label << 4) | (packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
186 
187     if (packet_type == AVRCP_START_PACKET){
188         // num packets: (3 bytes overhead (PID, num packets) + command) / (MTU - transport header)
189         command[pos++] = ((connection->cmd_operands_fragmented_len + 3 - 1) / (AVRCP_CMD_BUFFER_SIZE - 1)) + 1;
190         max_bytes -= 3;
191     }
192 
193     if ((packet_type == AVRCP_SINGLE_PACKET) || (packet_type == AVRCP_START_PACKET)){
194         // Profile IDentifier (PID)
195         command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
196         command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
197 
198         // command_type
199         command[pos++] = connection->command_type;
200         // subunit_type | subunit ID
201         command[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
202         // opcode
203         command[pos++] = (uint8_t)connection->command_opcode;
204     }
205 
206     if (packet_type == AVRCP_SINGLE_PACKET){
207         // operands
208         (void)memcpy(command + pos, connection->cmd_operands,
209                      connection->cmd_operands_length);
210         pos += connection->cmd_operands_length;
211     } else {
212         uint16_t bytes_to_copy = btstack_min(connection->cmd_operands_fragmented_len-connection->cmd_operands_fragmented_pos, max_bytes);
213         (void)memcpy(command + pos,
214                      &connection->cmd_operands_fragmented_buffer[connection->cmd_operands_fragmented_pos],
215                      bytes_to_copy);
216         pos += bytes_to_copy;
217         connection->cmd_operands_fragmented_pos += bytes_to_copy;
218     }
219 
220     return l2cap_send(connection->l2cap_signaling_cid, command, pos);
221 }
222 
223 static int avrcp_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){
224     if (connection->notifications_to_deregister & (1 << event_id)) return 0;
225     if (connection->notifications_enabled & (1 << event_id)) return 0;
226     if (connection->notifications_to_register & (1 << event_id)) return 0;
227     connection->notifications_to_register |= (1 << event_id);
228     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
229     return 1;
230 }
231 
232 static void avrcp_prepare_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){
233     connection->transaction_label++;
234     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
235     connection->command_type = AVRCP_CTYPE_NOTIFY;
236     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
237     connection->subunit_id = AVRCP_SUBUNIT_ID;
238     int pos = 0;
239     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
240     pos += 3;
241     connection->cmd_operands[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
242     connection->cmd_operands[pos++] = 0;                     // reserved(upper 6) | packet_type -> 0
243     big_endian_store_16(connection->cmd_operands, pos, 5);     // parameter length
244     pos += 2;
245     connection->cmd_operands[pos++] = event_id;
246     big_endian_store_32(connection->cmd_operands, pos, 1); // send notification on playback position every second, for other notifications it is ignored
247     pos += 4;
248     connection->cmd_operands_length = pos;
249     // AVRCP_SPEC_V14.pdf 166
250     // answer page 61
251 }
252 
253 
254 static void avrcp_parser_reset(avrcp_connection_t * connection){
255     connection->list_offset = 0;
256     connection->num_attributes = 0;
257     connection->num_parsed_attributes = 0;
258     connection->parser_attribute_header_pos = 0;
259     connection->num_received_fragments = 0;
260     connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
261 }
262 
263 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){
264     uint8_t event[7];
265     int pos = 0;
266     event[pos++] = HCI_EVENT_AVRCP_META;
267     event[pos++] = sizeof(event) - 2;
268     event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE;
269     little_endian_store_16(event, pos, avrcp_cid);
270     pos += 2;
271     event[pos++] = ctype;
272     event[pos++] = status;
273     // printf_hexdump(event, pos);
274     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
275 }
276 
277 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){
278     uint8_t event[HCI_EVENT_BUFFER_SIZE];
279     int pos = 0;
280     event[pos++] = HCI_EVENT_AVRCP_META;
281     // reserve one byte for subevent type and data len
282     int data_len_pos = pos;
283     pos++;
284     int subevent_type_pos = pos;
285     pos++;
286     little_endian_store_16(event, pos, avrcp_cid);
287     pos += 2;
288     event[pos++] = ctype;
289 
290     switch (attr_id){
291         case AVRCP_MEDIA_ATTR_TITLE:
292             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO;
293             event[pos++] = value_len;
294             (void)memcpy(event + pos, value, value_len);
295             break;
296         case AVRCP_MEDIA_ATTR_ARTIST:
297             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO;
298             event[pos++] = value_len;
299             (void)memcpy(event + pos, value, value_len);
300             break;
301         case AVRCP_MEDIA_ATTR_ALBUM:
302             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO;
303             event[pos++] = value_len;
304             (void)memcpy(event + pos, value, value_len);
305             break;
306         case AVRCP_MEDIA_ATTR_GENRE:
307             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO;
308             event[pos++] = value_len;
309             (void)memcpy(event + pos, value, value_len);
310             break;
311         case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
312             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO;
313             if (value){
314                 little_endian_store_32(event, pos, btstack_atoi((char *)value));
315             } else {
316                 little_endian_store_32(event, pos, 0);
317             }
318             pos += 4;
319             break;
320         case AVRCP_MEDIA_ATTR_TRACK:
321             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO;
322             if (value){
323                 event[pos++] = btstack_atoi((char *)value);
324             } else {
325                 event[pos++] = 0;
326             }
327             break;
328         case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
329             event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO;
330             if (value){
331                 event[pos++] = btstack_atoi((char *)value);
332             } else {
333                 event[pos++] = 0;
334             }
335             break;
336         default:
337             break;
338     }
339     event[data_len_pos] = pos - 2;
340     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
341 }
342 
343 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){
344     uint16_t attribute_total_value_len;
345     uint32_t attribute_id;
346     // printf("avrcp_parser_process_byte: %02x, state %02x\n", byte, connection->parser_state);
347     switch(connection->parser_state){
348         case AVRCP_PARSER_GET_ATTRIBUTE_HEADER:
349             connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte;
350             connection->list_offset++;
351 
352             if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return;
353 
354             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
355             connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE);
356             if (connection->attribute_value_len > 0){
357                 // get ready for attribute value
358                 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE;
359                 return;
360             }
361 
362             // emit empty attribute
363             attribute_id = big_endian_read_32(connection->parser_attribute_header, 0);
364             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);
365 
366             // done, see below
367             break;
368 
369         case AVRCP_PARSER_GET_ATTRIBUTE_VALUE:
370             connection->attribute_value[connection->attribute_value_offset++] = byte;
371             connection->list_offset++;
372 
373             if (connection->attribute_value_offset < connection->attribute_value_len) return;
374 
375             // emit (potentially partial) attribute
376             attribute_id = big_endian_read_32(connection->parser_attribute_header, 0);
377             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);
378 
379             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
380             if (connection->attribute_value_offset < attribute_total_value_len){
381                 // ignore rest of attribute
382                 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE;
383                 return;
384             }
385 
386             // done, see below
387             break;
388 
389         case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE:
390             connection->attribute_value_offset++;
391             connection->list_offset++;
392 
393             attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6);
394             if (connection->attribute_value_offset < attribute_total_value_len) return;
395 
396             // done, see below
397             break;
398 
399         default:
400             return;
401     }
402 
403     // attribute fully read, check if more to come
404     if (connection->list_offset < connection->list_size){
405         // more to come, reset parser
406         connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
407         connection->parser_attribute_header_pos = 0;
408         connection->attribute_value_offset = 0;
409     } else {
410         // fully done
411         avrcp_parser_reset(connection);
412         avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0);
413     }
414 }
415 
416 static void avrcp_source_parse_and_emit_element_attrs(uint8_t * packet, uint16_t num_bytes_to_read, avrcp_connection_t * connection, avrcp_command_type_t ctype){
417     int i;
418     for (i=0;i<num_bytes_to_read;i++){
419         avrcp_parser_process_byte(packet[i], connection, ctype);
420     }
421 }
422 
423 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){
424     connection->state = AVCTP_W2_SEND_COMMAND;
425     connection->transaction_label++;
426     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
427     connection->command_type = AVRCP_CTYPE_CONTROL;
428     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
429     connection->subunit_id = AVRCP_SUBUNIT_ID;
430     int pos = 0;
431     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
432     pos += 3;
433     connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID
434     connection->cmd_operands[pos++] = 0;
435     // Parameter Length
436     connection->cmd_operands_length = 8;
437     big_endian_store_16(connection->cmd_operands, pos, 1);
438     pos += 2;
439     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
440     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
441     return ERROR_CODE_SUCCESS;
442 }
443 
444 
445 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){
446     connection->state = AVCTP_W2_SEND_COMMAND;
447     connection->transaction_label++;
448     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
449     connection->command_type = AVRCP_CTYPE_CONTROL;
450     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
451     connection->subunit_id = AVRCP_SUBUNIT_ID;
452     int pos = 0;
453     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
454     pos += 3;
455     connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID
456     connection->cmd_operands[pos++] = 0;
457     // Parameter Length
458     connection->cmd_operands_length = 8;
459     big_endian_store_16(connection->cmd_operands, pos, 1);
460     pos += 2;
461     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
462     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
463     return ERROR_CODE_SUCCESS;
464 }
465 
466 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
467     uint8_t operands[20];
468     uint8_t opcode;
469     int     pos = 3;
470 
471     avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++];
472     uint8_t byte_value = packet[pos++];
473     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3);
474     avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t)   (byte_value & 0x07);
475     opcode = packet[pos++];
476 
477     uint8_t pdu_id;
478     uint16_t param_length;
479     switch (avrcp_cmd_opcode(packet,size)){
480         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
481             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
482             connection->state = AVCTP_CONNECTION_OPENED;
483             // operands:
484             (void)memcpy(operands, packet + pos, 5);
485             uint8_t unit_type = operands[1] >> 3;
486             uint8_t max_subunit_ID = operands[1] & 0x07;
487             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);
488             break;
489         }
490         case AVRCP_CMD_OPCODE_UNIT_INFO:{
491             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
492             connection->state = AVCTP_CONNECTION_OPENED;
493             // operands:
494             (void)memcpy(operands, packet + pos, 5);
495             uint8_t unit_type = operands[1] >> 3;
496             uint8_t unit = operands[1] & 0x07;
497             uint32_t company_id = (operands[2] << 16) | (operands[3] << 8) | operands[4];
498             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,
499                 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id);
500             break;
501         }
502         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
503             if ((size - pos) < 7) {
504                 log_error("avrcp: wrong packet size");
505                 return;
506             };
507             // operands:
508             (void)memcpy(operands, packet + pos, 7);
509             pos += 7;
510             pdu_id = operands[3];
511 
512             if ((connection->state != AVCTP_W2_RECEIVE_RESPONSE) && (pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION)){
513                 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state);
514                 return;
515             }
516             connection->state = AVCTP_CONNECTION_OPENED;
517 
518             param_length = big_endian_read_16(operands, 5);
519 
520             log_info("        VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype));
521             switch (pdu_id){
522                 case AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue:{
523                     uint8_t num_attributes = packet[pos++];
524                     int i;
525                     avrcp_repeat_mode_t  repeat_mode =  AVRCP_REPEAT_MODE_INVALID;
526                     avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID;
527                     for (i = 0; i < num_attributes; i++){
528                         uint8_t attribute_id    = packet[pos++];
529                         uint8_t value = packet[pos++];
530                         switch (attribute_id){
531                             case 0x02:
532                                 repeat_mode = (avrcp_repeat_mode_t) value;
533                                 break;
534                             case 0x03:
535                                 shuffle_mode = (avrcp_shuffle_mode_t) value;
536                                 break;
537                             default:
538                                 break;
539                         }
540                     }
541                     avrcp_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode);
542                     break;
543                 }
544                 case AVRCP_PDU_ID_SetPlayerApplicationSettingValue:{
545                     uint8_t event[6];
546                     int offset = 0;
547                     event[offset++] = HCI_EVENT_AVRCP_META;
548                     event[offset++] = sizeof(event) - 2;
549                     event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE;
550                     little_endian_store_16(event, offset, connection->avrcp_cid);
551                     offset += 2;
552                     event[offset++] = ctype;
553                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
554                     break;
555                 }
556                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{
557                     uint8_t event[7];
558                     int offset = 0;
559                     event[offset++] = HCI_EVENT_AVRCP_META;
560                     event[offset++] = sizeof(event) - 2;
561                     event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE;
562                     little_endian_store_16(event, offset, connection->avrcp_cid);
563                     offset += 2;
564                     event[offset++] = ctype;
565                     event[offset++] = packet[pos++];
566                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
567                     break;
568                 }
569                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
570                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++];
571                     uint8_t capability_count = packet[pos++];
572                     int i;
573 
574                     switch (capability_id){
575                         int offset = 0;
576                         uint8_t event[10];
577 
578                         case AVRCP_CAPABILITY_ID_COMPANY:
579                             for (i = 0; i < capability_count; i++){
580                                 uint32_t company_id = big_endian_read_24(packet, pos);
581                                 pos += 3;
582                                 log_info("  0x%06" PRIx32 ", ", company_id);
583 
584                                 offset = 0;
585                                 event[offset++] = HCI_EVENT_AVRCP_META;
586                                 event[offset++] = sizeof(event) - 2;
587                                 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID;
588                                 little_endian_store_16(event, offset, connection->avrcp_cid);
589                                 offset += 2;
590                                 event[offset++] = ctype;
591                                 event[offset++] = 0;
592                                 little_endian_store_24(event, offset, company_id);
593                                 offset += 3;
594                                 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
595                                 break;
596                             }
597 
598                             offset = 0;
599                             event[offset++] = HCI_EVENT_AVRCP_META;
600                             event[offset++] = sizeof(event) - 2;
601                             event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE;
602                             little_endian_store_16(event, offset, connection->avrcp_cid);
603                             offset += 2;
604                             event[offset++] = ctype;
605                             event[offset++] = 0;
606                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
607                             break;
608 
609                         case AVRCP_CAPABILITY_ID_EVENT:
610                             for (i = 0; i < capability_count; i++){
611                                 uint8_t event_id = packet[pos++];
612                                 log_info("  0x%02x %s", event_id, avrcp_event2str(event_id));
613 
614                                 offset = 0;
615                                 event[offset++] = HCI_EVENT_AVRCP_META;
616                                 event[offset++] = sizeof(event) - 2;
617                                 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID;
618                                 little_endian_store_16(event, offset, connection->avrcp_cid);
619                                 offset += 2;
620                                 event[offset++] = ctype;
621                                 event[offset++] = 0;
622                                 event[offset++] = event_id;
623                                 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
624                             }
625 
626                             offset = 0;
627                             event[offset++] = HCI_EVENT_AVRCP_META;
628                             event[offset++] = sizeof(event) - 2;
629                             event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE;
630                             little_endian_store_16(event, offset, connection->avrcp_cid);
631                             offset += 2;
632                             event[offset++] = ctype;
633                             event[offset++] = 0;
634                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
635                             break;
636                     }
637                     break;
638                 }
639                 case AVRCP_PDU_ID_GET_PLAY_STATUS:{
640                     uint32_t song_length = big_endian_read_32(packet, pos);
641                     pos += 4;
642                     uint32_t song_position = big_endian_read_32(packet, pos);
643                     pos += 4;
644                     uint8_t play_status = packet[pos];
645 
646                     uint8_t event[15];
647                     int offset = 0;
648                     event[offset++] = HCI_EVENT_AVRCP_META;
649                     event[offset++] = sizeof(event) - 2;
650                     event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS;
651                     little_endian_store_16(event, offset, connection->avrcp_cid);
652                     offset += 2;
653                     event[offset++] = ctype;
654                     little_endian_store_32(event, offset, song_length);
655                     offset += 4;
656                     little_endian_store_32(event, offset, song_position);
657                     offset += 4;
658                     event[offset++] = play_status;
659                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
660                     break;
661                 }
662                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
663                     avrcp_notification_event_id_t  event_id = (avrcp_notification_event_id_t) packet[pos++];
664                     uint16_t event_mask = (1 << event_id);
665                     uint16_t reset_event_mask = ~event_mask;
666                     switch (ctype){
667                         case AVRCP_CTYPE_RESPONSE_INTERIM:
668                             // register as enabled
669                             connection->notifications_enabled |= event_mask;
670                             break;
671                         case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE:
672                             // received change, event is considered deregistered
673                             // we are re-enabling it automatically, if it is not
674                             // explicitly disabled
675                             connection->notifications_enabled &= reset_event_mask;
676                             if (! (connection->notifications_to_deregister & event_mask)){
677                                 avrcp_register_notification(connection, event_id);
678                             } else {
679                                 connection->notifications_to_deregister &= reset_event_mask;
680                             }
681                             break;
682                         default:
683                             connection->notifications_to_register &= reset_event_mask;
684                             connection->notifications_enabled &= reset_event_mask;
685                             connection->notifications_to_deregister &= reset_event_mask;
686                             break;
687                     }
688 
689                     switch (event_id){
690                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{
691                             uint32_t song_position = big_endian_read_32(packet, pos);
692                             uint8_t event[10];
693                             int offset = 0;
694                             event[offset++] = HCI_EVENT_AVRCP_META;
695                             event[offset++] = sizeof(event) - 2;
696                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED;
697                             little_endian_store_16(event, offset, connection->avrcp_cid);
698                             offset += 2;
699                             event[offset++] = ctype;
700                             little_endian_store_32(event, offset, song_position);
701                             offset += 4;
702                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
703                             break;
704                         }
705                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{
706                             uint8_t event[7];
707                             int offset = 0;
708                             event[offset++] = HCI_EVENT_AVRCP_META;
709                             event[offset++] = sizeof(event) - 2;
710                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED;
711                             little_endian_store_16(event, offset, connection->avrcp_cid);
712                             offset += 2;
713                             event[offset++] = ctype;
714                             event[offset++] = packet[pos];
715                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
716                             break;
717                         }
718                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{
719                             uint8_t event[6];
720                             int offset = 0;
721                             event[offset++] = HCI_EVENT_AVRCP_META;
722                             event[offset++] = sizeof(event) - 2;
723                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED;
724                             little_endian_store_16(event, offset, connection->avrcp_cid);
725                             offset += 2;
726                             event[offset++] = ctype;
727                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
728                             break;
729                         }
730                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{
731                             uint8_t event[6];
732                             int offset = 0;
733                             event[offset++] = HCI_EVENT_AVRCP_META;
734                             event[offset++] = sizeof(event) - 2;
735                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED;
736                             little_endian_store_16(event, offset, connection->avrcp_cid);
737                             offset += 2;
738                             event[offset++] = ctype;
739                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
740                             break;
741                         }
742                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{
743                             uint8_t event[6];
744                             int offset = 0;
745                             event[offset++] = HCI_EVENT_AVRCP_META;
746                             event[offset++] = sizeof(event) - 2;
747                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED;
748                             little_endian_store_16(event, offset, connection->avrcp_cid);
749                             offset += 2;
750                             event[offset++] = ctype;
751                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
752                             break;
753                         }
754                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{
755                             uint8_t event[7];
756                             int offset = 0;
757                             event[offset++] = HCI_EVENT_AVRCP_META;
758                             event[offset++] = sizeof(event) - 2;
759                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
760                             little_endian_store_16(event, offset, connection->avrcp_cid);
761                             offset += 2;
762                             event[offset++] = ctype;
763                             event[offset++] = packet[pos++] & 0x7F;
764                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
765                             break;
766                         }
767                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{
768                             uint8_t event[8];
769                             uint16_t uuid = big_endian_read_16(packet, pos);
770                             int offset = 0;
771                             event[offset++] = HCI_EVENT_AVRCP_META;
772                             event[offset++] = sizeof(event) - 2;
773                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED;
774                             little_endian_store_16(event, offset, connection->avrcp_cid);
775                             offset += 2;
776                             event[offset++] = ctype;
777                             little_endian_store_16(event, offset, uuid);
778                             offset += 2;
779                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
780                             break;
781                         }
782 
783                         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{
784                             uint8_t event[6];
785                             int offset = 0;
786                             event[offset++] = HCI_EVENT_AVRCP_META;
787                             event[offset++] = sizeof(event) - 2;
788                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END;
789                             little_endian_store_16(event, offset, connection->avrcp_cid);
790                             offset += 2;
791                             event[offset++] = ctype;
792                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
793                             break;
794                         }
795                         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{
796                             uint8_t event[6];
797                             int offset = 0;
798                             event[offset++] = HCI_EVENT_AVRCP_META;
799                             event[offset++] = sizeof(event) - 2;
800                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START;
801                             little_endian_store_16(event, offset, connection->avrcp_cid);
802                             offset += 2;
803                             event[offset++] = ctype;
804                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
805                             break;
806                         }
807                         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{
808                             uint8_t event[7];
809                             int offset = 0;
810                             event[offset++] = HCI_EVENT_AVRCP_META;
811                             event[offset++] = sizeof(event) - 2;
812                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED;
813                             little_endian_store_16(event, offset, connection->avrcp_cid);
814                             offset += 2;
815                             event[offset++] = ctype;
816                             event[offset++] = packet[pos++];
817                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
818                             break;
819                         }
820 
821                         case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{
822                             uint8_t event[7];
823                             int offset = 0;
824                             event[offset++] = HCI_EVENT_AVRCP_META;
825                             event[offset++] = sizeof(event) - 2;
826                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED;
827                             little_endian_store_16(event, offset, connection->avrcp_cid);
828                             offset += 2;
829                             event[offset++] = ctype;
830                             event[offset++] = packet[pos];
831                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
832                             break;
833                         }
834 
835                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
836                         default:
837                             log_info("avrcp: not implemented");
838                             break;
839                     }
840                     if (connection->notifications_to_register != 0){
841                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
842                     }
843                     break;
844                 }
845 
846                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
847                     avrcp_packet_type_t packet_type = (avrcp_packet_type_t) (operands[4] & 0x03);
848                     switch (packet_type){
849                         case AVRCP_START_PACKET:
850                         case AVRCP_SINGLE_PACKET:
851                             avrcp_parser_reset(connection);
852                             connection->list_size = param_length;
853                             connection->num_attributes = packet[pos++];
854 
855                             avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
856 
857                             if (packet_type == AVRCP_START_PACKET){
858                                 avrcp_controller_request_continue_response(connection);
859                             }
860                             break;
861                         case AVRCP_CONTINUE_PACKET:
862                         case AVRCP_END_PACKET:
863                             connection->num_received_fragments++;
864                             if (connection->num_received_fragments < connection->max_num_fragments){
865                                 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
866                                 if (packet_type == AVRCP_CONTINUE_PACKET){
867                                     avrcp_controller_request_continue_response(connection);
868                                 }
869                             } else {
870                                 avrcp_controller_request_abort_continuation(connection);
871                                 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1);
872                                 avrcp_parser_reset(connection);
873                             }
874                             break;
875                     }
876                 }
877                 default:
878                     break;
879             }
880             break;
881         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
882             // 0x80 | connection->cmd_operands[0]
883             uint8_t operation_id = packet[pos++];
884             switch (connection->state){
885                 case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
886                     if (connection->continuous_fast_forward_cmd){
887                         connection->state = AVCTP_W4_STOP;
888                     } else {
889                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
890                     }
891                     break;
892                 case AVCTP_W2_RECEIVE_RESPONSE:
893                     connection->state = AVCTP_CONNECTION_OPENED;
894                     break;
895                 default:
896                     break;
897             }
898             if (connection->state == AVCTP_W4_STOP){
899                 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id);
900             }
901             if (connection->state == AVCTP_CONNECTION_OPENED) {
902                 // RELEASE response
903                 operation_id = operation_id & 0x7F;
904                 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id);
905             }
906             if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){
907                 // PRESS response
908                 request_pass_through_release_control_cmd(connection);
909             }
910             break;
911         }
912         default:
913             break;
914     }
915 
916     // trigger pending notification reqistrations
917     if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->notifications_to_register){
918         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
919     }
920 }
921 
922 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){
923     int i;
924     switch (connection->state){
925         case AVCTP_W2_SEND_PRESS_COMMAND:
926             connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE;
927             avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
928             break;
929         case AVCTP_W2_SEND_COMMAND:
930         case AVCTP_W2_SEND_RELEASE_COMMAND:
931             connection->state = AVCTP_W2_RECEIVE_RESPONSE;
932             avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
933             break;
934         case AVCTP_CONNECTION_OPENED:
935             if (connection->notifications_to_register != 0){
936                 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){
937                     if (connection->notifications_to_register & (1<<i)){
938                         connection->notifications_to_register &= ~ (1 << i);
939                         avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i);
940                         connection->state = AVCTP_W2_RECEIVE_RESPONSE;
941                         avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
942                         return;
943                     }
944                 }
945             }
946             return;
947         case AVCTP_W2_SEND_FRAGMENTED_COMMAND:
948             if (connection->cmd_operands_fragmented_pos == 0){
949                  avrcp_send_cmd(connection, AVRCP_START_PACKET);
950                  avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
951             } else {
952                 if ((connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos) > avrcp_get_max_payload_size_for_packet_type(AVRCP_CONTINUE_PACKET)){
953                      avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET);
954                      avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
955                  } else {
956                     connection->state = AVCTP_W2_RECEIVE_RESPONSE;
957                     avrcp_send_cmd(connection, AVRCP_END_PACKET);
958                  }
959             }
960         default:
961             return;
962     }
963 }
964 
965 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
966     avrcp_connection_t * connection;
967 
968     switch (packet_type) {
969         case L2CAP_DATA_PACKET:
970             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
971             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
972             break;
973 
974         case HCI_EVENT_PACKET:
975             switch (hci_event_packet_get_type(packet)){
976                 case L2CAP_EVENT_CAN_SEND_NOW:
977                     connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
978                     avrcp_controller_handle_can_send_now(connection);
979                     break;
980             default:
981                 break;
982         }
983         default:
984             break;
985     }
986 }
987 
988 void avrcp_controller_init(void){
989     avrcp_controller_context.role = AVRCP_CONTROLLER;
990     avrcp_controller_context.packet_handler = avrcp_controller_packet_handler;
991     avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler);
992 }
993 
994 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){
995     btstack_assert(callback != NULL);
996     avrcp_controller_context.avrcp_callback = callback;
997 }
998 
999 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){
1000     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1001     if (!connection){
1002         log_error("avrcp_unit_info: could not find a connection.");
1003         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1004     }
1005     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1006     connection->state = AVCTP_W2_SEND_COMMAND;
1007 
1008     connection->transaction_label++;
1009     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
1010     connection->command_type = AVRCP_CTYPE_STATUS;
1011     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
1012     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
1013     memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length);
1014     connection->cmd_operands_length = 5;
1015     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1016     return ERROR_CODE_SUCCESS;
1017 }
1018 
1019 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){
1020     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1021     if (!connection){
1022         log_error("avrcp_unit_info: could not find a connection.");
1023         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1024     }
1025     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1026     connection->state = AVCTP_W2_SEND_COMMAND;
1027 
1028     connection->transaction_label++;
1029     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
1030     connection->command_type = AVRCP_CTYPE_STATUS;
1031     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
1032     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
1033     memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length);
1034     connection->cmd_operands[0] = 7; // page: 0, extention_code: 7
1035     connection->cmd_operands_length = 5;
1036     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1037     return ERROR_CODE_SUCCESS;
1038 }
1039 
1040 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){
1041     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1042     if (!connection){
1043         log_error("avrcp_get_capabilities: could not find a connection.");
1044         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1045     }
1046     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1047     connection->state = AVCTP_W2_SEND_COMMAND;
1048 
1049     connection->transaction_label++;
1050     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1051     connection->command_type = AVRCP_CTYPE_STATUS;
1052     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1053     connection->subunit_id = AVRCP_SUBUNIT_ID;
1054     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1055     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID
1056     connection->cmd_operands[4] = 0;
1057     big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length
1058     connection->cmd_operands[7] = capability_id;                  // capability ID
1059     connection->cmd_operands_length = 8;
1060     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1061     return ERROR_CODE_SUCCESS;
1062 }
1063 
1064 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){
1065     return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY);
1066 }
1067 
1068 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){
1069     return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT);
1070 }
1071 
1072 
1073 uint8_t avrcp_controller_play(uint16_t avrcp_cid){
1074     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1075 }
1076 
1077 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){
1078     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1079 }
1080 
1081 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){
1082     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1083 }
1084 
1085 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){
1086     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1087 }
1088 
1089 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){
1090     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1091 }
1092 
1093 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){
1094     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1095 }
1096 
1097 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){
1098     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1099 }
1100 
1101 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){
1102     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1103 }
1104 
1105 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){
1106     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0);
1107 }
1108 
1109 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){
1110     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1111 }
1112 
1113 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){
1114     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1115 }
1116 
1117 
1118 /* start cmds */
1119 
1120 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){
1121     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1122     if (!connection){
1123         log_error("avrcp_stop_play: could not find a connection.");
1124         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1125     }
1126     if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED;
1127     return request_pass_through_release_control_cmd(connection);
1128 }
1129 
1130 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){
1131     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1132 }
1133 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){
1134     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1135 }
1136 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){
1137     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1138 }
1139 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){
1140     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1141 }
1142 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){
1143     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1144 }
1145 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){
1146     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1147 }
1148 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){
1149     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1150 }
1151 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){
1152     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1153 }
1154 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){
1155     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1156 }
1157 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){
1158     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1159 }
1160 
1161 
1162 /* stop continuous cmds */
1163 
1164 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){
1165     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1166     if (!connection){
1167         log_error("avrcp_get_play_status: could not find a connection.");
1168         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1169     }
1170     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1171     connection->state = AVCTP_W2_SEND_COMMAND;
1172     connection->transaction_label++;
1173     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1174     connection->command_type = AVRCP_CTYPE_STATUS;
1175     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1176     connection->subunit_id = AVRCP_SUBUNIT_ID;
1177     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1178     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS;
1179     connection->cmd_operands[4] = 0;                     // reserved(upper 6) | packet_type -> 0
1180     big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length
1181     connection->cmd_operands_length = 7;
1182     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1183     return ERROR_CODE_SUCCESS;
1184 }
1185 
1186 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1187     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1188     if (!connection){
1189         log_error("avrcp_get_play_status: could not find a connection.");
1190         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1191     }
1192     avrcp_register_notification(connection, event_id);
1193     return ERROR_CODE_SUCCESS;
1194 }
1195 
1196 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1197     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1198     if (!connection){
1199         log_error("avrcp_get_play_status: could not find a connection.");
1200         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1201     }
1202     connection->notifications_to_deregister |= (1 << event_id);
1203     return ERROR_CODE_SUCCESS;
1204 }
1205 
1206 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){
1207     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1208     if (!connection){
1209         log_error("avrcp_get_capabilities: could not find a connection.");
1210         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1211     }
1212     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1213     connection->state = AVCTP_W2_SEND_COMMAND;
1214 
1215     connection->transaction_label++;
1216     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1217     connection->command_type = AVRCP_CTYPE_CONTROL;
1218     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1219     connection->subunit_id = AVRCP_SUBUNIT_ID;
1220     int pos = 0;
1221     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1222     pos += 3;
1223     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID
1224     connection->cmd_operands[pos++] = 0;
1225 
1226     // Parameter Length
1227     big_endian_store_16(connection->cmd_operands, pos, 2);
1228     pos += 2;
1229 
1230     big_endian_store_16(connection->cmd_operands, pos, addressed_player_id);
1231     pos += 2;
1232 
1233     connection->cmd_operands_length = pos;
1234     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1235     return ERROR_CODE_SUCCESS;
1236 }
1237 
1238 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){
1239     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1240     if (!connection){
1241         log_error("avrcp_get_capabilities: could not find a connection.");
1242         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1243     }
1244     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1245     connection->state = AVCTP_W2_SEND_COMMAND;
1246 
1247     connection->transaction_label++;
1248     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1249     connection->command_type = AVRCP_CTYPE_STATUS;
1250     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1251     connection->subunit_id = AVRCP_SUBUNIT_ID;
1252     int pos = 0;
1253     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1254     pos += 3;
1255     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID
1256     connection->cmd_operands[pos++] = 0;
1257 
1258     // Parameter Length
1259     big_endian_store_16(connection->cmd_operands, pos, 9);
1260     pos += 2;
1261 
1262     // write 8 bytes value
1263     memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING
1264     pos += 8;
1265 
1266     connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes
1267     // every attribute is 4 bytes long
1268 
1269     connection->cmd_operands_length = pos;
1270     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1271     return ERROR_CODE_SUCCESS;
1272 }
1273 
1274 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){
1275      avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1276     if (!connection){
1277         log_error("avrcp_get_capabilities: could not find a connection.");
1278         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1279     }
1280     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1281     connection->state = AVCTP_W2_SEND_COMMAND;
1282 
1283     connection->transaction_label++;
1284     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1285     connection->command_type = AVRCP_CTYPE_CONTROL;
1286     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1287     connection->subunit_id = AVRCP_SUBUNIT_ID;
1288     int pos = 0;
1289     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1290     pos += 3;
1291     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID
1292     connection->cmd_operands[pos++] = 0;
1293 
1294     // Parameter Length
1295     big_endian_store_16(connection->cmd_operands, pos, 1);
1296     pos += 2;
1297     connection->cmd_operands[pos++] = volume;
1298 
1299     connection->cmd_operands_length = pos;
1300     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1301     return ERROR_CODE_SUCCESS;
1302 }
1303 
1304 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){
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_capabilities: could not find a connection.");
1308         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1309     }
1310     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1311     connection->state = AVCTP_W2_SEND_COMMAND;
1312 
1313     connection->transaction_label++;
1314     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1315     connection->command_type = AVRCP_CTYPE_STATUS;
1316     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1317     connection->subunit_id = AVRCP_SUBUNIT_ID;
1318     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1319     connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID
1320     connection->cmd_operands[4] = 0;
1321     big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length
1322     connection->cmd_operands[7] = 4;                     // NumPlayerApplicationSettingAttributeID
1323     // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133
1324     connection->cmd_operands[8]  = 0x01;    // equalizer  (1-OFF, 2-ON)
1325     connection->cmd_operands[9]  = 0x02;    // repeat     (1-off, 2-single track, 3-all tracks, 4-group repeat)
1326     connection->cmd_operands[10] = 0x03;    // shuffle    (1-off, 2-all tracks, 3-group shuffle)
1327     connection->cmd_operands[11] = 0x04;    // scan       (1-off, 2-all tracks, 3-group scan)
1328     connection->cmd_operands_length = 12;
1329     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1330     return ERROR_CODE_SUCCESS;
1331 }
1332 
1333 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){
1334     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1335     if (!connection){
1336         log_error("avrcp_get_capabilities: could not find a connection.");
1337         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1338     }
1339     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1340     connection->state = AVCTP_W2_SEND_COMMAND;
1341 
1342     connection->transaction_label++;
1343     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1344     connection->command_type = AVRCP_CTYPE_CONTROL;
1345     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1346     connection->subunit_id = AVRCP_SUBUNIT_ID;
1347     int pos = 0;
1348     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1349     pos += 3;
1350     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID
1351     connection->cmd_operands[pos++] = 0;
1352     // Parameter Length
1353     big_endian_store_16(connection->cmd_operands, pos, 3);
1354     pos += 2;
1355     connection->cmd_operands[pos++] = 2;
1356     connection->cmd_operands_length = pos;
1357     connection->cmd_operands[pos++]  = attr_id;
1358     connection->cmd_operands[pos++]  = attr_value;
1359     connection->cmd_operands_length = pos;
1360     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1361     return ERROR_CODE_SUCCESS;
1362 }
1363 
1364 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){
1365     if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1366     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode);
1367 }
1368 
1369 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){
1370     if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1371     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode);
1372 }
1373 
1374 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){
1375     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1376     if (!connection){
1377         log_error("Could not find a connection with cid 0%02x.", avrcp_cid);
1378         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1379     }
1380     if (connection->state != AVCTP_CONNECTION_OPENED){
1381         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1382         return ERROR_CODE_COMMAND_DISALLOWED;
1383     }
1384     connection->state = AVCTP_W2_SEND_COMMAND;
1385 
1386     connection->transaction_label++;
1387     connection->command_type = AVRCP_CTYPE_CONTROL;
1388     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1389     connection->subunit_id = AVRCP_SUBUNIT_ID;
1390     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1391     int pos = 0;
1392     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1393     pos += 3;
1394     connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID
1395     // reserved
1396     connection->cmd_operands[pos++] = 0;
1397     // Parameter Length
1398     big_endian_store_16(connection->cmd_operands, pos, 11);
1399     pos += 2;
1400     connection->cmd_operands[pos++]  = scope;
1401     memset(&connection->cmd_operands[pos], 0, 8);
1402     if (uid){
1403         (void)memcpy(&connection->cmd_operands[pos], uid, 8);
1404     }
1405     pos += 8;
1406     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
1407     pos += 2;
1408     connection->cmd_operands_length = pos;
1409 
1410     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1411     return ERROR_CODE_SUCCESS;
1412 }
1413 
1414 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){
1415     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1416     if (!connection){
1417         log_error("Could not find a connection with cid 0%02x.", avrcp_cid);
1418         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1419     }
1420     if (connection->state != AVCTP_CONNECTION_OPENED){
1421         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1422         return ERROR_CODE_COMMAND_DISALLOWED;
1423     }
1424     connection->state = AVCTP_W2_SEND_COMMAND;
1425 
1426     connection->transaction_label++;
1427     connection->command_type = AVRCP_CTYPE_CONTROL;
1428     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1429     connection->subunit_id = AVRCP_SUBUNIT_ID;
1430     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1431     int pos = 0;
1432     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1433     pos += 3;
1434     connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID
1435     // reserved
1436     connection->cmd_operands[pos++] = 0;
1437     // Parameter Length
1438     big_endian_store_16(connection->cmd_operands, pos, 11);
1439     pos += 2;
1440     connection->cmd_operands[pos++]  = scope;
1441     memset(&connection->cmd_operands[pos], 0, 8);
1442     if (uid){
1443         (void)memcpy(&connection->cmd_operands[pos], uid, 8);
1444     }
1445     pos += 8;
1446     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
1447     pos += 2;
1448     connection->cmd_operands_length = pos;
1449 
1450     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1451     return ERROR_CODE_SUCCESS;
1452 }
1453 
1454 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){
1455     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1456     if (!connection){
1457         log_error("avrcp_controller_play_item: could not find a connection.");
1458         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1459     }
1460     connection->max_num_fragments = max_num_fragments;
1461     return ERROR_CODE_SUCCESS;
1462 }
1463 
1464 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){
1465     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1466     if (!connection){
1467         log_error("avrcp_controller_play_item: could not find a connection.");
1468         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1469     }
1470 
1471     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1472     connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND;
1473 
1474     connection->transaction_label++;
1475     connection->command_opcode = command_opcode;
1476     connection->command_type = command_type;
1477     connection->subunit_type = subunit_type;
1478     connection->subunit_id = subunit_id;
1479     connection->cmd_operands_fragmented_buffer = command_buffer;
1480     connection->cmd_operands_fragmented_pos = 0;
1481     connection->cmd_operands_fragmented_len = command_len;
1482 
1483     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1484     return ERROR_CODE_SUCCESS;
1485 }
1486