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