xref: /btstack/src/classic/avrcp_controller.c (revision 3acd1f1c67e58dd613485780105290dd863ac1c9)
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     uint8_t opcode;
473     int     pos = 3;
474 
475     if (size < 6u) return;
476 
477     uint8_t pdu_id;
478 
479     avrcp_frame_type_t  frame_type = (avrcp_frame_type_t)((packet[0] >> 1) & 0x01);
480     if (frame_type != AVRCP_RESPONSE_FRAME) return;
481 
482     avrcp_packet_type_t packet_type = (avrcp_packet_type_t)((packet[0] >> 2) & 0x03);
483     switch (packet_type){
484         case AVRCP_SINGLE_PACKET:
485             break;
486         default:
487             log_info("Fragmentation is not supported");
488             return;
489     }
490 
491     avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++];
492 
493     uint8_t byte_value = packet[pos++];
494     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3);
495     avrcp_subunit_type_t subunit_id   = (avrcp_subunit_type_t)   (byte_value & 0x07);
496     uint8_t opcode = packet[pos++];
497     // Company ID (3)
498     pos += 3;
499 
500     uint16_t param_length;
501 
502     switch (opcode){
503         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
504             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
505             connection->state = AVCTP_CONNECTION_OPENED;
506             // operands:
507             (void)memcpy(operands, packet + pos, 5);
508             uint8_t unit_type = operands[1] >> 3;
509             uint8_t max_subunit_ID = operands[1] & 0x07;
510             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);
511             break;
512         }
513         case AVRCP_CMD_OPCODE_UNIT_INFO:{
514             if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return;
515             connection->state = AVCTP_CONNECTION_OPENED;
516             // operands:
517             (void)memcpy(operands, packet + pos, 5);
518             uint8_t unit_type = operands[1] >> 3;
519             uint8_t unit = operands[1] & 0x07;
520             uint32_t company_id = (operands[2] << 16) | (operands[3] << 8) | operands[4];
521             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,
522                 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id);
523             break;
524         }
525         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
526             if ((size - pos) < 7) {
527                 log_error("avrcp: wrong packet size");
528                 return;
529             };
530             // operands:
531             (void)memcpy(operands, packet + pos, 7);
532             pos += 7;
533             pdu_id = operands[3];
534 
535             if ((connection->state != AVCTP_W2_RECEIVE_RESPONSE) && (pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION)){
536                 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state);
537                 return;
538             }
539             connection->state = AVCTP_CONNECTION_OPENED;
540 
541             param_length = big_endian_read_16(operands, 5);
542 
543             log_info("        VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype));
544             switch (pdu_id){
545                 case AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:{
546                     uint8_t num_attributes = packet[pos++];
547                     int i;
548                     avrcp_repeat_mode_t  repeat_mode =  AVRCP_REPEAT_MODE_INVALID;
549                     avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID;
550                     for (i = 0; i < num_attributes; i++){
551                         uint8_t attribute_id    = packet[pos++];
552                         uint8_t value = packet[pos++];
553                         switch (attribute_id){
554                             case 0x02:
555                                 repeat_mode = (avrcp_repeat_mode_t) value;
556                                 break;
557                             case 0x03:
558                                 shuffle_mode = (avrcp_shuffle_mode_t) value;
559                                 break;
560                             default:
561                                 break;
562                         }
563                     }
564                     avrcp_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode);
565                     break;
566                 }
567                 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{
568                     uint8_t event[6];
569                     int offset = 0;
570                     event[offset++] = HCI_EVENT_AVRCP_META;
571                     event[offset++] = sizeof(event) - 2;
572                     event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE;
573                     little_endian_store_16(event, offset, connection->avrcp_cid);
574                     offset += 2;
575                     event[offset++] = ctype;
576                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
577                     break;
578                 }
579                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{
580                     uint8_t event[7];
581                     int offset = 0;
582                     event[offset++] = HCI_EVENT_AVRCP_META;
583                     event[offset++] = sizeof(event) - 2;
584                     event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE;
585                     little_endian_store_16(event, offset, connection->avrcp_cid);
586                     offset += 2;
587                     event[offset++] = ctype;
588                     event[offset++] = packet[pos++];
589                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
590                     break;
591                 }
592                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
593                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++];
594                     uint8_t capability_count = packet[pos++];
595                     int i;
596 
597                     switch (capability_id){
598                         int offset = 0;
599                         uint8_t event[10];
600 
601                         case AVRCP_CAPABILITY_ID_COMPANY:
602                             for (i = 0; i < capability_count; i++){
603                                 uint32_t company_id = big_endian_read_24(packet, pos);
604                                 pos += 3;
605                                 log_info("  0x%06" PRIx32 ", ", company_id);
606 
607                                 offset = 0;
608                                 event[offset++] = HCI_EVENT_AVRCP_META;
609                                 event[offset++] = sizeof(event) - 2;
610                                 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID;
611                                 little_endian_store_16(event, offset, connection->avrcp_cid);
612                                 offset += 2;
613                                 event[offset++] = ctype;
614                                 event[offset++] = 0;
615                                 little_endian_store_24(event, offset, company_id);
616                                 offset += 3;
617                                 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
618                                 break;
619                             }
620 
621                             offset = 0;
622                             event[offset++] = HCI_EVENT_AVRCP_META;
623                             event[offset++] = sizeof(event) - 2;
624                             event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE;
625                             little_endian_store_16(event, offset, connection->avrcp_cid);
626                             offset += 2;
627                             event[offset++] = ctype;
628                             event[offset++] = 0;
629                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
630                             break;
631 
632                         case AVRCP_CAPABILITY_ID_EVENT:
633                             for (i = 0; i < capability_count; i++){
634                                 uint8_t event_id = packet[pos++];
635                                 log_info("  0x%02x %s", event_id, avrcp_event2str(event_id));
636 
637                                 offset = 0;
638                                 event[offset++] = HCI_EVENT_AVRCP_META;
639                                 event[offset++] = sizeof(event) - 2;
640                                 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID;
641                                 little_endian_store_16(event, offset, connection->avrcp_cid);
642                                 offset += 2;
643                                 event[offset++] = ctype;
644                                 event[offset++] = 0;
645                                 event[offset++] = event_id;
646                                 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset);
647                             }
648 
649                             offset = 0;
650                             event[offset++] = HCI_EVENT_AVRCP_META;
651                             event[offset++] = sizeof(event) - 2;
652                             event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE;
653                             little_endian_store_16(event, offset, connection->avrcp_cid);
654                             offset += 2;
655                             event[offset++] = ctype;
656                             event[offset++] = 0;
657                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
658                             break;
659                     }
660                     break;
661                 }
662                 case AVRCP_PDU_ID_GET_PLAY_STATUS:{
663                     uint32_t song_length = big_endian_read_32(packet, pos);
664                     pos += 4;
665                     uint32_t song_position = big_endian_read_32(packet, pos);
666                     pos += 4;
667                     uint8_t play_status = packet[pos];
668 
669                     uint8_t event[15];
670                     int offset = 0;
671                     event[offset++] = HCI_EVENT_AVRCP_META;
672                     event[offset++] = sizeof(event) - 2;
673                     event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS;
674                     little_endian_store_16(event, offset, connection->avrcp_cid);
675                     offset += 2;
676                     event[offset++] = ctype;
677                     little_endian_store_32(event, offset, song_length);
678                     offset += 4;
679                     little_endian_store_32(event, offset, song_position);
680                     offset += 4;
681                     event[offset++] = play_status;
682                     (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
683                     break;
684                 }
685                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
686                     avrcp_notification_event_id_t  event_id = (avrcp_notification_event_id_t) packet[pos++];
687                     uint16_t event_mask = (1 << event_id);
688                     uint16_t reset_event_mask = ~event_mask;
689                     switch (ctype){
690                         case AVRCP_CTYPE_RESPONSE_INTERIM:
691                             // register as enabled
692                             connection->notifications_enabled |= event_mask;
693                             break;
694                         case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE:
695                             // received change, event is considered deregistered
696                             // we are re-enabling it automatically, if it is not
697                             // explicitly disabled
698                             connection->notifications_enabled &= reset_event_mask;
699                             if (! (connection->notifications_to_deregister & event_mask)){
700                                 avrcp_register_notification(connection, event_id);
701                             } else {
702                                 connection->notifications_to_deregister &= reset_event_mask;
703                             }
704                             break;
705                         default:
706                             connection->notifications_to_register &= reset_event_mask;
707                             connection->notifications_enabled &= reset_event_mask;
708                             connection->notifications_to_deregister &= reset_event_mask;
709                             break;
710                     }
711 
712                     switch (event_id){
713                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{
714                             uint32_t song_position = big_endian_read_32(packet, pos);
715                             uint8_t event[10];
716                             int offset = 0;
717                             event[offset++] = HCI_EVENT_AVRCP_META;
718                             event[offset++] = sizeof(event) - 2;
719                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED;
720                             little_endian_store_16(event, offset, connection->avrcp_cid);
721                             offset += 2;
722                             event[offset++] = ctype;
723                             little_endian_store_32(event, offset, song_position);
724                             offset += 4;
725                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
726                             break;
727                         }
728                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{
729                             uint8_t event[7];
730                             int offset = 0;
731                             event[offset++] = HCI_EVENT_AVRCP_META;
732                             event[offset++] = sizeof(event) - 2;
733                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED;
734                             little_endian_store_16(event, offset, connection->avrcp_cid);
735                             offset += 2;
736                             event[offset++] = ctype;
737                             event[offset++] = packet[pos];
738                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
739                             break;
740                         }
741                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{
742                             uint8_t event[6];
743                             int offset = 0;
744                             event[offset++] = HCI_EVENT_AVRCP_META;
745                             event[offset++] = sizeof(event) - 2;
746                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED;
747                             little_endian_store_16(event, offset, connection->avrcp_cid);
748                             offset += 2;
749                             event[offset++] = ctype;
750                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
751                             break;
752                         }
753                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{
754                             uint8_t event[6];
755                             int offset = 0;
756                             event[offset++] = HCI_EVENT_AVRCP_META;
757                             event[offset++] = sizeof(event) - 2;
758                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED;
759                             little_endian_store_16(event, offset, connection->avrcp_cid);
760                             offset += 2;
761                             event[offset++] = ctype;
762                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
763                             break;
764                         }
765                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{
766                             uint8_t event[6];
767                             int offset = 0;
768                             event[offset++] = HCI_EVENT_AVRCP_META;
769                             event[offset++] = sizeof(event) - 2;
770                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED;
771                             little_endian_store_16(event, offset, connection->avrcp_cid);
772                             offset += 2;
773                             event[offset++] = ctype;
774                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
775                             break;
776                         }
777                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{
778                             uint8_t event[7];
779                             int offset = 0;
780                             event[offset++] = HCI_EVENT_AVRCP_META;
781                             event[offset++] = sizeof(event) - 2;
782                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED;
783                             little_endian_store_16(event, offset, connection->avrcp_cid);
784                             offset += 2;
785                             event[offset++] = ctype;
786                             event[offset++] = packet[pos++] & 0x7F;
787                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
788                             break;
789                         }
790                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{
791                             uint8_t event[8];
792                             uint16_t uuid = big_endian_read_16(packet, pos);
793                             int offset = 0;
794                             event[offset++] = HCI_EVENT_AVRCP_META;
795                             event[offset++] = sizeof(event) - 2;
796                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED;
797                             little_endian_store_16(event, offset, connection->avrcp_cid);
798                             offset += 2;
799                             event[offset++] = ctype;
800                             little_endian_store_16(event, offset, uuid);
801                             offset += 2;
802                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
803                             break;
804                         }
805 
806                         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{
807                             uint8_t event[6];
808                             int offset = 0;
809                             event[offset++] = HCI_EVENT_AVRCP_META;
810                             event[offset++] = sizeof(event) - 2;
811                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END;
812                             little_endian_store_16(event, offset, connection->avrcp_cid);
813                             offset += 2;
814                             event[offset++] = ctype;
815                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
816                             break;
817                         }
818                         case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{
819                             uint8_t event[6];
820                             int offset = 0;
821                             event[offset++] = HCI_EVENT_AVRCP_META;
822                             event[offset++] = sizeof(event) - 2;
823                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START;
824                             little_endian_store_16(event, offset, connection->avrcp_cid);
825                             offset += 2;
826                             event[offset++] = ctype;
827                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
828                             break;
829                         }
830                         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{
831                             uint8_t event[7];
832                             int offset = 0;
833                             event[offset++] = HCI_EVENT_AVRCP_META;
834                             event[offset++] = sizeof(event) - 2;
835                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED;
836                             little_endian_store_16(event, offset, connection->avrcp_cid);
837                             offset += 2;
838                             event[offset++] = ctype;
839                             event[offset++] = packet[pos++];
840                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
841                             break;
842                         }
843 
844                         case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{
845                             uint8_t event[7];
846                             int offset = 0;
847                             event[offset++] = HCI_EVENT_AVRCP_META;
848                             event[offset++] = sizeof(event) - 2;
849                             event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED;
850                             little_endian_store_16(event, offset, connection->avrcp_cid);
851                             offset += 2;
852                             event[offset++] = ctype;
853                             event[offset++] = packet[pos];
854                             (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
855                             break;
856                         }
857 
858                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
859                         default:
860                             log_info("avrcp: not implemented");
861                             break;
862                     }
863                     if (connection->notifications_to_register != 0){
864                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
865                     }
866                     break;
867                 }
868 
869                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
870                     switch (packet_type){
871                         case AVRCP_START_PACKET:
872                         case AVRCP_SINGLE_PACKET:
873                             avrcp_parser_reset(connection);
874                             connection->list_size = param_length;
875                             connection->num_attributes = packet[pos++];
876 
877                             avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
878 
879                             if (packet_type == AVRCP_START_PACKET){
880                                 avrcp_controller_request_continue_response(connection);
881                             }
882                             break;
883                         case AVRCP_CONTINUE_PACKET:
884                         case AVRCP_END_PACKET:
885                             connection->num_received_fragments++;
886                             if (connection->num_received_fragments < connection->max_num_fragments){
887                                 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype);
888                                 if (packet_type == AVRCP_CONTINUE_PACKET){
889                                     avrcp_controller_request_continue_response(connection);
890                                 }
891                             } else {
892                                 avrcp_controller_request_abort_continuation(connection);
893                                 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1);
894                                 avrcp_parser_reset(connection);
895                             }
896                             break;
897                     }
898                 }
899                 default:
900                     break;
901             }
902             break;
903         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
904             // 0x80 | connection->cmd_operands[0]
905             uint8_t operation_id = packet[pos++];
906             switch (connection->state){
907                 case AVCTP_W2_RECEIVE_PRESS_RESPONSE:
908                     if (connection->continuous_fast_forward_cmd){
909                         connection->state = AVCTP_W4_STOP;
910                     } else {
911                         connection->state = AVCTP_W2_SEND_RELEASE_COMMAND;
912                     }
913                     break;
914                 case AVCTP_W2_RECEIVE_RESPONSE:
915                     connection->state = AVCTP_CONNECTION_OPENED;
916                     break;
917                 default:
918                     break;
919             }
920             if (connection->state == AVCTP_W4_STOP){
921                 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id);
922             }
923             if (connection->state == AVCTP_CONNECTION_OPENED) {
924                 // RELEASE response
925                 operation_id = operation_id & 0x7F;
926                 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id);
927             }
928             if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){
929                 // PRESS response
930                 request_pass_through_release_control_cmd(connection);
931             }
932             break;
933         }
934         default:
935             break;
936     }
937 
938     // trigger pending notification reqistrations
939     if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->notifications_to_register){
940         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
941     }
942 }
943 
944 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){
945     int i;
946     switch (connection->state){
947         case AVCTP_W2_SEND_PRESS_COMMAND:
948             connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE;
949             avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
950             break;
951         case AVCTP_W2_SEND_COMMAND:
952         case AVCTP_W2_SEND_RELEASE_COMMAND:
953             connection->state = AVCTP_W2_RECEIVE_RESPONSE;
954             avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
955             break;
956         case AVCTP_CONNECTION_OPENED:
957             if (connection->notifications_to_register != 0){
958                 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){
959                     if (connection->notifications_to_register & (1<<i)){
960                         connection->notifications_to_register &= ~ (1 << i);
961                         avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i);
962                         connection->state = AVCTP_W2_RECEIVE_RESPONSE;
963                         avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET);
964                         return;
965                     }
966                 }
967             }
968             return;
969         case AVCTP_W2_SEND_FRAGMENTED_COMMAND:
970             if (connection->cmd_operands_fragmented_pos == 0){
971                  avrcp_send_cmd(connection, AVRCP_START_PACKET);
972                  avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
973             } else {
974                 if ((connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos) > avrcp_get_max_payload_size_for_packet_type(AVRCP_CONTINUE_PACKET)){
975                      avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET);
976                      avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
977                  } else {
978                     connection->state = AVCTP_W2_RECEIVE_RESPONSE;
979                     avrcp_send_cmd(connection, AVRCP_END_PACKET);
980                  }
981             }
982         default:
983             return;
984     }
985 }
986 
987 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
988     avrcp_connection_t * connection;
989 
990     switch (packet_type) {
991         case L2CAP_DATA_PACKET:
992             connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
993             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
994             break;
995 
996         case HCI_EVENT_PACKET:
997             switch (hci_event_packet_get_type(packet)){
998                 case L2CAP_EVENT_CAN_SEND_NOW:
999                     connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel);
1000                     avrcp_controller_handle_can_send_now(connection);
1001                     break;
1002             default:
1003                 break;
1004         }
1005         default:
1006             break;
1007     }
1008 }
1009 
1010 void avrcp_controller_init(void){
1011     avrcp_controller_context.role = AVRCP_CONTROLLER;
1012     avrcp_controller_context.packet_handler = avrcp_controller_packet_handler;
1013     avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler);
1014 }
1015 
1016 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){
1017     btstack_assert(callback != NULL);
1018     avrcp_controller_context.avrcp_callback = callback;
1019 }
1020 
1021 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){
1022     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1023     if (!connection){
1024         log_error("avrcp_unit_info: could not find a connection.");
1025         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1026     }
1027     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1028     connection->state = AVCTP_W2_SEND_COMMAND;
1029 
1030     connection->transaction_label++;
1031     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
1032     connection->command_type = AVRCP_CTYPE_STATUS;
1033     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
1034     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
1035     memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length);
1036     connection->cmd_operands_length = 5;
1037     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1038     return ERROR_CODE_SUCCESS;
1039 }
1040 
1041 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){
1042     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1043     if (!connection){
1044         log_error("avrcp_unit_info: could not find a connection.");
1045         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1046     }
1047     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1048     connection->state = AVCTP_W2_SEND_COMMAND;
1049 
1050     connection->transaction_label++;
1051     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
1052     connection->command_type = AVRCP_CTYPE_STATUS;
1053     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
1054     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
1055     memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length);
1056     connection->cmd_operands[0] = 7; // page: 0, extention_code: 7
1057     connection->cmd_operands_length = 5;
1058     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1059     return ERROR_CODE_SUCCESS;
1060 }
1061 
1062 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){
1063     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1064     if (!connection){
1065         log_error("avrcp_get_capabilities: could not find a connection.");
1066         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1067     }
1068     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1069     connection->state = AVCTP_W2_SEND_COMMAND;
1070 
1071     connection->transaction_label++;
1072     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1073     connection->command_type = AVRCP_CTYPE_STATUS;
1074     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1075     connection->subunit_id = AVRCP_SUBUNIT_ID;
1076     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1077     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID
1078     connection->cmd_operands[4] = 0;
1079     big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length
1080     connection->cmd_operands[7] = capability_id;                  // capability ID
1081     connection->cmd_operands_length = 8;
1082     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1083     return ERROR_CODE_SUCCESS;
1084 }
1085 
1086 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){
1087     return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY);
1088 }
1089 
1090 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){
1091     return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT);
1092 }
1093 
1094 
1095 uint8_t avrcp_controller_play(uint16_t avrcp_cid){
1096     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1097 }
1098 
1099 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){
1100     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1101 }
1102 
1103 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){
1104     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1105 }
1106 
1107 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){
1108     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1109 }
1110 
1111 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){
1112     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1113 }
1114 
1115 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){
1116     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1117 }
1118 
1119 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){
1120     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1121 }
1122 
1123 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){
1124     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1125 }
1126 
1127 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){
1128     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0);
1129 }
1130 
1131 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){
1132     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1133 }
1134 
1135 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){
1136     return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1137 }
1138 
1139 
1140 /* start cmds */
1141 
1142 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){
1143     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1144     if (!connection){
1145         log_error("avrcp_stop_play: could not find a connection.");
1146         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1147     }
1148     if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED;
1149     return request_pass_through_release_control_cmd(connection);
1150 }
1151 
1152 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){
1153     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0);
1154 }
1155 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){
1156     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0);
1157 }
1158 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){
1159     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0);
1160 }
1161 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){
1162     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0);
1163 }
1164 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){
1165     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0);
1166 }
1167 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){
1168     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0);
1169 }
1170 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){
1171     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0);
1172 }
1173 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){
1174     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0);
1175 }
1176 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){
1177     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0);
1178 }
1179 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){
1180     return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0);
1181 }
1182 
1183 
1184 /* stop continuous cmds */
1185 
1186 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){
1187     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1188     if (!connection){
1189         log_error("avrcp_get_play_status: could not find a connection.");
1190         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1191     }
1192     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1193     connection->state = AVCTP_W2_SEND_COMMAND;
1194     connection->transaction_label++;
1195     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1196     connection->command_type = AVRCP_CTYPE_STATUS;
1197     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1198     connection->subunit_id = AVRCP_SUBUNIT_ID;
1199     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1200     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS;
1201     connection->cmd_operands[4] = 0;                     // reserved(upper 6) | packet_type -> 0
1202     big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length
1203     connection->cmd_operands_length = 7;
1204     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1205     return ERROR_CODE_SUCCESS;
1206 }
1207 
1208 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1209     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1210     if (!connection){
1211         log_error("avrcp_get_play_status: could not find a connection.");
1212         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1213     }
1214     avrcp_register_notification(connection, event_id);
1215     return ERROR_CODE_SUCCESS;
1216 }
1217 
1218 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){
1219     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1220     if (!connection){
1221         log_error("avrcp_get_play_status: could not find a connection.");
1222         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1223     }
1224     connection->notifications_to_deregister |= (1 << event_id);
1225     return ERROR_CODE_SUCCESS;
1226 }
1227 
1228 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){
1229     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1230     if (!connection){
1231         log_error("avrcp_get_capabilities: could not find a connection.");
1232         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1233     }
1234     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1235     connection->state = AVCTP_W2_SEND_COMMAND;
1236 
1237     connection->transaction_label++;
1238     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1239     connection->command_type = AVRCP_CTYPE_CONTROL;
1240     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1241     connection->subunit_id = AVRCP_SUBUNIT_ID;
1242     int pos = 0;
1243     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1244     pos += 3;
1245     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID
1246     connection->cmd_operands[pos++] = 0;
1247 
1248     // Parameter Length
1249     big_endian_store_16(connection->cmd_operands, pos, 2);
1250     pos += 2;
1251 
1252     big_endian_store_16(connection->cmd_operands, pos, addressed_player_id);
1253     pos += 2;
1254 
1255     connection->cmd_operands_length = pos;
1256     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1257     return ERROR_CODE_SUCCESS;
1258 }
1259 
1260 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){
1261     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1262     if (!connection){
1263         log_error("avrcp_get_capabilities: could not find a connection.");
1264         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1265     }
1266     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1267     connection->state = AVCTP_W2_SEND_COMMAND;
1268 
1269     connection->transaction_label++;
1270     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1271     connection->command_type = AVRCP_CTYPE_STATUS;
1272     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1273     connection->subunit_id = AVRCP_SUBUNIT_ID;
1274     int pos = 0;
1275     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1276     pos += 3;
1277     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID
1278     connection->cmd_operands[pos++] = 0;
1279 
1280     // Parameter Length
1281     big_endian_store_16(connection->cmd_operands, pos, 9);
1282     pos += 2;
1283 
1284     // write 8 bytes value
1285     memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING
1286     pos += 8;
1287 
1288     connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes
1289     // every attribute is 4 bytes long
1290 
1291     connection->cmd_operands_length = pos;
1292     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1293     return ERROR_CODE_SUCCESS;
1294 }
1295 
1296 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){
1297      avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1298     if (!connection){
1299         log_error("avrcp_get_capabilities: could not find a connection.");
1300         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1301     }
1302     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1303     connection->state = AVCTP_W2_SEND_COMMAND;
1304 
1305     connection->transaction_label++;
1306     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1307     connection->command_type = AVRCP_CTYPE_CONTROL;
1308     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1309     connection->subunit_id = AVRCP_SUBUNIT_ID;
1310     int pos = 0;
1311     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1312     pos += 3;
1313     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID
1314     connection->cmd_operands[pos++] = 0;
1315 
1316     // Parameter Length
1317     big_endian_store_16(connection->cmd_operands, pos, 1);
1318     pos += 2;
1319     connection->cmd_operands[pos++] = volume;
1320 
1321     connection->cmd_operands_length = pos;
1322     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1323     return ERROR_CODE_SUCCESS;
1324 }
1325 
1326 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){
1327     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1328     if (!connection){
1329         log_error("avrcp_get_capabilities: could not find a connection.");
1330         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1331     }
1332     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1333     connection->state = AVCTP_W2_SEND_COMMAND;
1334 
1335     connection->transaction_label++;
1336     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1337     connection->command_type = AVRCP_CTYPE_STATUS;
1338     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1339     connection->subunit_id = AVRCP_SUBUNIT_ID;
1340     big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID);
1341     connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID
1342     connection->cmd_operands[4] = 0;
1343     big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length
1344     connection->cmd_operands[7] = 4;                     // NumPlayerApplicationSettingAttributeID
1345     // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133
1346     connection->cmd_operands[8]  = 0x01;    // equalizer  (1-OFF, 2-ON)
1347     connection->cmd_operands[9]  = 0x02;    // repeat     (1-off, 2-single track, 3-all tracks, 4-group repeat)
1348     connection->cmd_operands[10] = 0x03;    // shuffle    (1-off, 2-all tracks, 3-group shuffle)
1349     connection->cmd_operands[11] = 0x04;    // scan       (1-off, 2-all tracks, 3-group scan)
1350     connection->cmd_operands_length = 12;
1351     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1352     return ERROR_CODE_SUCCESS;
1353 }
1354 
1355 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){
1356     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1357     if (!connection){
1358         log_error("avrcp_get_capabilities: could not find a connection.");
1359         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1360     }
1361     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1362     connection->state = AVCTP_W2_SEND_COMMAND;
1363 
1364     connection->transaction_label++;
1365     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1366     connection->command_type = AVRCP_CTYPE_CONTROL;
1367     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1368     connection->subunit_id = AVRCP_SUBUNIT_ID;
1369     int pos = 0;
1370     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1371     pos += 3;
1372     connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID
1373     connection->cmd_operands[pos++] = 0;
1374     // Parameter Length
1375     big_endian_store_16(connection->cmd_operands, pos, 3);
1376     pos += 2;
1377     connection->cmd_operands[pos++] = 2;
1378     connection->cmd_operands_length = pos;
1379     connection->cmd_operands[pos++]  = attr_id;
1380     connection->cmd_operands[pos++]  = attr_value;
1381     connection->cmd_operands_length = pos;
1382     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1383     return ERROR_CODE_SUCCESS;
1384 }
1385 
1386 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){
1387     if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1388     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode);
1389 }
1390 
1391 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){
1392     if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1393     return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode);
1394 }
1395 
1396 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){
1397     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1398     if (!connection){
1399         log_error("Could not find a connection with cid 0%02x.", avrcp_cid);
1400         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1401     }
1402     if (connection->state != AVCTP_CONNECTION_OPENED){
1403         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1404         return ERROR_CODE_COMMAND_DISALLOWED;
1405     }
1406     connection->state = AVCTP_W2_SEND_COMMAND;
1407 
1408     connection->transaction_label++;
1409     connection->command_type = AVRCP_CTYPE_CONTROL;
1410     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1411     connection->subunit_id = AVRCP_SUBUNIT_ID;
1412     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1413     int pos = 0;
1414     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1415     pos += 3;
1416     connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID
1417     // reserved
1418     connection->cmd_operands[pos++] = 0;
1419     // Parameter Length
1420     big_endian_store_16(connection->cmd_operands, pos, 11);
1421     pos += 2;
1422     connection->cmd_operands[pos++]  = scope;
1423     memset(&connection->cmd_operands[pos], 0, 8);
1424     if (uid){
1425         (void)memcpy(&connection->cmd_operands[pos], uid, 8);
1426     }
1427     pos += 8;
1428     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
1429     pos += 2;
1430     connection->cmd_operands_length = pos;
1431 
1432     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1433     return ERROR_CODE_SUCCESS;
1434 }
1435 
1436 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){
1437     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1438     if (!connection){
1439         log_error("Could not find a connection with cid 0%02x.", avrcp_cid);
1440         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1441     }
1442     if (connection->state != AVCTP_CONNECTION_OPENED){
1443         log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state);
1444         return ERROR_CODE_COMMAND_DISALLOWED;
1445     }
1446     connection->state = AVCTP_W2_SEND_COMMAND;
1447 
1448     connection->transaction_label++;
1449     connection->command_type = AVRCP_CTYPE_CONTROL;
1450     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
1451     connection->subunit_id = AVRCP_SUBUNIT_ID;
1452     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1453     int pos = 0;
1454     big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID);
1455     pos += 3;
1456     connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID
1457     // reserved
1458     connection->cmd_operands[pos++] = 0;
1459     // Parameter Length
1460     big_endian_store_16(connection->cmd_operands, pos, 11);
1461     pos += 2;
1462     connection->cmd_operands[pos++]  = scope;
1463     memset(&connection->cmd_operands[pos], 0, 8);
1464     if (uid){
1465         (void)memcpy(&connection->cmd_operands[pos], uid, 8);
1466     }
1467     pos += 8;
1468     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
1469     pos += 2;
1470     connection->cmd_operands_length = pos;
1471 
1472     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1473     return ERROR_CODE_SUCCESS;
1474 }
1475 
1476 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){
1477     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1478     if (!connection){
1479         log_error("avrcp_controller_play_item: could not find a connection.");
1480         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1481     }
1482     connection->max_num_fragments = max_num_fragments;
1483     return ERROR_CODE_SUCCESS;
1484 }
1485 
1486 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){
1487     avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
1488     if (!connection){
1489         log_error("avrcp_controller_play_item: could not find a connection.");
1490         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1491     }
1492 
1493     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1494     connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND;
1495 
1496     connection->transaction_label++;
1497     connection->command_opcode = command_opcode;
1498     connection->command_type = command_type;
1499     connection->subunit_type = subunit_type;
1500     connection->subunit_id = subunit_id;
1501     connection->cmd_operands_fragmented_buffer = command_buffer;
1502     connection->cmd_operands_fragmented_pos = 0;
1503     connection->cmd_operands_fragmented_len = command_len;
1504 
1505     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1506     return ERROR_CODE_SUCCESS;
1507 }
1508