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