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