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