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