xref: /btstack/src/classic/avrcp_target.c (revision 6d5d598a96564033e815305ba03d8eece13fad5a)
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_target.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <inttypes.h>
45 
46 #include "btstack.h"
47 #include "classic/avrcp.h"
48 
49 #define AVRCP_ATTR_HEADER_LEN  8
50 
51 static const uint8_t AVRCP_NOTIFICATION_TRACK_SELECTED[] = {0,0,0,0,0,0,0,0};
52 static const uint8_t AVRCP_NOTIFICATION_TRACK_NOT_SELECTED[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
53 
54 avrcp_context_t avrcp_target_context;
55 
56 void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name){
57     avrcp_create_sdp_record(0, service, service_record_handle, browsing, supported_features, service_name, service_provider_name);
58 }
59 
60 static void avrcp_target_emit_operation(btstack_packet_handler_t callback, uint16_t avrcp_cid, avrcp_operation_id_t operation_id, uint8_t operands_length, uint8_t operand){
61     if (!callback) return;
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_OPERATION;
67     little_endian_store_16(event, pos, avrcp_cid);
68     pos += 2;
69     event[pos++] = operation_id;
70     event[pos++] = operands_length;
71     event[pos++] = operand;
72     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
73 }
74 
75 static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){
76     if (!callback) return;
77     uint8_t event[5];
78     int pos = 0;
79     event[pos++] = HCI_EVENT_AVRCP_META;
80     event[pos++] = sizeof(event) - 2;
81     event[pos++] = subevent_id;
82     little_endian_store_16(event, pos, avrcp_cid);
83     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
84 }
85 
86 static uint16_t avrcp_target_pack_single_element_attribute_number(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint32_t value){
87     if ((attr_id < 1) || (attr_id > AVRCP_MEDIA_ATTR_COUNT)) return 0;
88     uint16_t attr_value_length = sprintf((char *)(packet+pos+8), "%0" PRIu32, value);
89     big_endian_store_32(packet, pos, attr_id);
90     pos += 4;
91     big_endian_store_16(packet, pos, RFC2978_CHARSET_MIB_UTF8);
92     pos += 2;
93     big_endian_store_16(packet, pos, attr_value_length);
94     pos += 2;
95     return 8 + attr_value_length;
96 }
97 
98 static uint16_t avrcp_target_pack_single_element_attribute_string(uint8_t * packet, uint16_t pos, rfc2978_charset_mib_enumid_t mib_enumid, avrcp_media_attribute_id_t attr_id, uint8_t * attr_value, uint16_t attr_value_to_copy, uint16_t attr_value_size, uint8_t header){
99     if (attr_value_size == 0) return 0;
100     if ((attr_id < 1) || (attr_id > AVRCP_MEDIA_ATTR_COUNT)) return 0;
101     if (header){
102         big_endian_store_32(packet, pos, attr_id);
103         pos += 4;
104         big_endian_store_16(packet, pos, mib_enumid);
105         pos += 2;
106         big_endian_store_16(packet, pos, attr_value_size);
107         pos += 2;
108     }
109     memcpy(packet+pos, attr_value, attr_value_to_copy);
110     pos += attr_value_size;
111     return header * 8 + attr_value_size;
112 }
113 
114 static int avrcp_target_abort_continue_response(uint16_t cid, avrcp_connection_t * connection){
115     uint16_t pos = 0;
116     l2cap_reserve_packet_buffer();
117     uint8_t * packet = l2cap_get_outgoing_buffer();
118 
119     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
120     connection->command_type    = AVRCP_CTYPE_RESPONSE_ACCEPTED;
121     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
122     connection->subunit_id      = AVRCP_SUBUNIT_ID;
123 
124     packet[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
125     // Profile IDentifier (PID)
126     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
127     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
128 
129     // command_type
130     packet[pos++] = connection->command_type;
131     // subunit_type | subunit ID
132     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
133     // opcode
134     packet[pos++] = (uint8_t)connection->command_opcode;
135 
136     // company id is 3 bytes long
137     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
138     pos += 3;
139 
140     packet[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE;
141 
142     // reserve byte for packet type
143     packet[pos++] = AVRCP_SINGLE_PACKET;
144     big_endian_store_16(packet, pos, 0);
145     pos += 2;
146     return l2cap_send_prepared(cid, pos);
147 }
148 
149 static int avrcp_target_send_now_playing_info(uint16_t cid, avrcp_connection_t * connection){
150     uint16_t pos = 0;
151     l2cap_reserve_packet_buffer();
152     uint8_t * packet = l2cap_get_outgoing_buffer();
153     uint16_t  size   = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
154 
155     packet[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
156     // Profile IDentifier (PID)
157     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
158     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
159 
160     // command_type
161     packet[pos++] = connection->command_type;
162     // subunit_type | subunit ID
163     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
164     // opcode
165     packet[pos++] = (uint8_t)connection->command_opcode;
166 
167     // company id is 3 bytes long
168     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
169     pos += 3;
170 
171     packet[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES;
172 
173     // reserve byte for packet type
174     uint8_t pos_packet_type = pos;
175     pos++;
176 
177     uint16_t playing_info_buffer_len_position = pos;
178     pos += 2;
179     // printf("connection->next_attr_id %d \n", connection->next_attr_id);
180     if (connection->next_attr_id == AVRCP_MEDIA_ATTR_NONE){
181         packet[pos_packet_type] = AVRCP_SINGLE_PACKET;
182         connection->packet_type = AVRCP_SINGLE_PACKET;
183         packet[pos++] = count_set_bits_uint32(connection->now_playing_info_attr_bitmap);
184         connection->next_attr_id++;
185     }
186     // printf("updated connection->next_attr_id %d, connection->attribute_value_offset %d \n", connection->next_attr_id, connection->attribute_value_offset);
187 
188     uint8_t fragmented = 0;
189     int num_free_bytes = size - pos - 2;
190     uint8_t MAX_NUMBER_ATTR_LEN = 10;
191 
192     while (!fragmented && num_free_bytes > 0 && connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS){
193         avrcp_media_attribute_id_t attr_id = connection->next_attr_id;
194         int attr_index = attr_id - 1;
195 
196         if (connection->now_playing_info_attr_bitmap & (1 << attr_id)){
197             int num_written_bytes = 0;
198             int num_bytes_to_write = 0;
199             switch (attr_id){
200                 case AVRCP_MEDIA_ATTR_ALL:
201                 case AVRCP_MEDIA_ATTR_NONE:
202                     break;
203                 case AVRCP_MEDIA_ATTR_TRACK:
204                     num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
205                     if (num_free_bytes >= num_bytes_to_write){
206                         num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->track_nr);
207                         break;
208                     }
209                     fragmented = 1;
210                     connection->attribute_value_offset = 0;
211                     break;
212                 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS:
213                     num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
214                     if (num_free_bytes >= num_bytes_to_write){
215                         num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->total_tracks);
216                         break;
217                     }
218                     fragmented = 1;
219                     connection->attribute_value_offset = 0;
220                     break;
221                 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS:
222                     num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN;
223                     if (num_free_bytes >= num_bytes_to_write){
224                         num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->song_length_ms);
225                         break;
226                     }
227                     fragmented = 1;
228                     connection->attribute_value_offset = 0;
229                     break;
230                 default:{
231                     uint8_t   header = (connection->attribute_value_offset == 0);
232                     uint8_t * attr_value =     (uint8_t *) (connection->now_playing_info[attr_index].value + connection->attribute_value_offset);
233                     uint16_t  attr_value_len = connection->now_playing_info[attr_index].len - connection->attribute_value_offset;
234 
235                     num_bytes_to_write = attr_value_len + header * AVRCP_ATTR_HEADER_LEN;
236                     if (num_bytes_to_write <= num_free_bytes){
237                         connection->attribute_value_offset = 0;
238                         num_written_bytes = num_bytes_to_write;
239                         avrcp_target_pack_single_element_attribute_string(packet, pos, RFC2978_CHARSET_MIB_UTF8, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header);
240                         break;
241                     }
242                     fragmented = 1;
243                     num_written_bytes = num_free_bytes;
244                     attr_value_len = num_free_bytes - header * AVRCP_ATTR_HEADER_LEN;
245                     avrcp_target_pack_single_element_attribute_string(packet, pos, RFC2978_CHARSET_MIB_UTF8, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header);
246                     connection->attribute_value_offset += attr_value_len;
247                     break;
248                 }
249             }
250             pos += num_written_bytes;
251             num_free_bytes -= num_written_bytes;
252         }
253         if (!fragmented){
254             connection->next_attr_id++;
255         }
256     }
257 
258     if (fragmented){
259         switch (connection->packet_type){
260             case AVRCP_SINGLE_PACKET:
261                 connection->packet_type = AVRCP_START_PACKET;
262                 break;
263             default:
264                 connection->packet_type = AVRCP_CONTINUE_PACKET;
265                 break;
266         }
267     } else {
268         if (connection->next_attr_id >= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS){ // DONE
269             if (connection->packet_type != AVRCP_SINGLE_PACKET){
270                 connection->packet_type = AVRCP_END_PACKET;
271             }
272         }
273     }
274     packet[pos_packet_type] = connection->packet_type;
275     // store attr value length
276     big_endian_store_16(packet, playing_info_buffer_len_position, pos - playing_info_buffer_len_position - 2);
277     return l2cap_send_prepared(cid, size);
278 }
279 
280 
281 
282 static int avrcp_target_send_response(uint16_t cid, avrcp_connection_t * connection){
283     int pos = 0;
284     l2cap_reserve_packet_buffer();
285     uint8_t * packet = l2cap_get_outgoing_buffer();
286 
287     // transport header
288     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
289 
290     // TODO: check for fragmentation
291     connection->packet_type = AVRCP_SINGLE_PACKET;
292 
293     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
294     // Profile IDentifier (PID)
295     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
296     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
297     // command_type
298     packet[pos++] = connection->command_type;
299     // subunit_type | subunit ID
300     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
301     // opcode
302     packet[pos++] = (uint8_t)connection->command_opcode;
303     // operands
304     memcpy(packet+pos, connection->cmd_operands, connection->cmd_operands_length);
305     // printf_hexdump(packet+pos, connection->cmd_operands_length);
306 
307     pos += connection->cmd_operands_length;
308     connection->wait_to_send = 0;
309     return l2cap_send_prepared(cid, pos);
310 }
311 
312 static uint8_t avrcp_target_response_accept(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){
313     // AVRCP_CTYPE_RESPONSE_REJECTED
314     connection->command_type = AVRCP_CTYPE_RESPONSE_ACCEPTED;
315     connection->subunit_type = subunit_type;
316     connection->subunit_id =   subunit_id;
317     connection->command_opcode = opcode;
318     // company id is 3 bytes long
319     int pos = connection->cmd_operands_length;
320     connection->cmd_operands[pos++] = pdu_id;
321     connection->cmd_operands[pos++] = 0;
322     // param length
323     big_endian_store_16(connection->cmd_operands, pos, 1);
324     pos += 2;
325     connection->cmd_operands[pos++] = status;
326     connection->cmd_operands_length = pos;
327     connection->state = AVCTP_W2_SEND_RESPONSE;
328     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
329     return ERROR_CODE_SUCCESS;
330 }
331 
332 static uint8_t avrcp_target_response_reject(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){
333     // AVRCP_CTYPE_RESPONSE_REJECTED
334     connection->command_type = AVRCP_CTYPE_RESPONSE_REJECTED;
335     connection->subunit_type = subunit_type;
336     connection->subunit_id =   subunit_id;
337     connection->command_opcode = opcode;
338     // company id is 3 bytes long
339     int pos = connection->cmd_operands_length;
340     connection->cmd_operands[pos++] = pdu_id;
341     connection->cmd_operands[pos++] = 0;
342     // param length
343     big_endian_store_16(connection->cmd_operands, pos, 1);
344     pos += 2;
345     connection->cmd_operands[pos++] = status;
346     connection->cmd_operands_length = pos;
347     connection->state = AVCTP_W2_SEND_RESPONSE;
348     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
349     return ERROR_CODE_SUCCESS;
350 }
351 
352 static uint8_t avrcp_target_response_not_implemented(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t event_id){
353     connection->command_type = AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED;
354     connection->subunit_type = subunit_type;
355     connection->subunit_id =   subunit_id;
356     connection->command_opcode = opcode;
357 
358     // company id is 3 bytes long
359     int pos = connection->cmd_operands_length;
360     connection->cmd_operands[pos++] = pdu_id;
361     connection->cmd_operands[pos++] = 0;
362     // param length
363     big_endian_store_16(connection->cmd_operands, pos, 1);
364     pos += 2;
365     connection->cmd_operands[pos++] = event_id;
366     connection->cmd_operands_length = pos;
367 
368     connection->state = AVCTP_W2_SEND_RESPONSE;
369     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
370     return ERROR_CODE_SUCCESS;
371 }
372 
373 static uint8_t avrcp_target_response_vendor_dependent_interim(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){
374     connection->command_type = AVRCP_CTYPE_RESPONSE_INTERIM;
375     connection->subunit_type = subunit_type;
376     connection->subunit_id =   subunit_id;
377     connection->command_opcode = opcode;
378 
379     // company id is 3 bytes long
380     int pos = connection->cmd_operands_length;
381     connection->cmd_operands[pos++] = pdu_id;
382     connection->cmd_operands[pos++] = 0;
383     // param length
384     big_endian_store_16(connection->cmd_operands, pos, 1 + value_len);
385     pos += 2;
386     connection->cmd_operands[pos++] = event_id;
387     if (value && value_len > 0){
388         memcpy(connection->cmd_operands + pos, value, value_len);
389         pos += value_len;
390     }
391     connection->cmd_operands_length = pos;
392 
393     connection->state = AVCTP_W2_SEND_RESPONSE;
394     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
395     return ERROR_CODE_SUCCESS;
396 }
397 
398 // static uint8_t avrcp_target_response_vendor_dependent_changed(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){
399 //     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
400 //     connection->command_type = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
401 //     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
402 //     connection->subunit_id = AVRCP_SUBUNIT_ID;
403 
404 //     // company id is 3 bytes long
405 //     int pos = connection->cmd_operands_length;
406 //     connection->cmd_operands[pos++] = pdu_id;
407 //     connection->cmd_operands[pos++] = 0;
408 //     // param length
409 //     big_endian_store_16(connection->cmd_operands, pos, 1);
410 //     pos += 2;
411 //     connection->cmd_operands[pos++] = event_id;
412 //     connection->cmd_operands_length = pos;
413 
414 //     connection->state = AVCTP_W2_SEND_RESPONSE;
415 //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
416 //     return ERROR_CODE_SUCCESS;
417 // }
418 
419 static uint8_t avrcp_target_pass_through_response(uint16_t avrcp_cid, avrcp_command_type_t cmd_type, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
420     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
421     if (!connection){
422         log_error("Could not find a connection.");
423         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
424     }
425     connection->command_type = cmd_type;
426     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
427     connection->subunit_id =   AVRCP_SUBUNIT_ID;
428     connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH;
429 
430     int pos = 0;
431     connection->cmd_operands[pos++] = opid;
432     connection->cmd_operands[pos++] = operands_length;
433     if (operands_length == 1){
434         connection->cmd_operands[pos++] = operand;
435     }
436     connection->cmd_operands_length = pos;
437 
438     connection->state = AVCTP_W2_SEND_RESPONSE;
439     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
440     return ERROR_CODE_SUCCESS;
441 }
442 
443 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
444     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand);
445 }
446 
447 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
448     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
449 }
450 
451 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
452     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
453 }
454 
455 void avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
456     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
457     if (!connection){
458         log_error("avrcp_target_operation_reject: could not find a connection.");
459         return;
460     }
461     connection->unit_type = unit_type;
462     connection->company_id = company_id;
463 }
464 
465 void avrcp_target_set_subunit_info(uint16_t avrcp_cid, avrcp_subunit_type_t subunit_type, const uint8_t * subunit_info_data, uint16_t subunit_info_data_size){
466     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
467     if (!connection){
468         log_error("avrcp_target_operation_reject: could not find a connection.");
469         return;
470     }
471     connection->subunit_info_type = subunit_type;
472     connection->subunit_info_data = subunit_info_data;
473     connection->subunit_info_data_size = subunit_info_data_size;
474 }
475 
476 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){
477     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
478 
479     uint8_t unit = 0;
480     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
481     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
482     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
483     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
484 
485     connection->cmd_operands_length = 5;
486     connection->cmd_operands[0] = 0x07;
487     connection->cmd_operands[1] = (connection->unit_type << 4) | unit;
488     // company id is 3 bytes long
489     big_endian_store_32(connection->cmd_operands, 2, connection->company_id);
490 
491     connection->state = AVCTP_W2_SEND_RESPONSE;
492     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
493     return ERROR_CODE_SUCCESS;
494 }
495 
496 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){
497     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
498     if (offset - 4 > connection->subunit_info_data_size) return AVRCP_STATUS_INVALID_PARAMETER;
499 
500     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
501     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
502     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
503     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
504     // printf("avrcp_target_subunit_info  subunit_type %d\n", connection->subunit_type);
505 
506     uint8_t page = offset / 4;
507     uint8_t extension_code = 7;
508     connection->cmd_operands_length = 5;
509     connection->cmd_operands[0] = (page << 4) | extension_code;
510 
511     memcpy(connection->cmd_operands+1, connection->subunit_info_data + offset, 4);
512 
513     connection->state = AVCTP_W2_SEND_RESPONSE;
514     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
515     return ERROR_CODE_SUCCESS;
516 }
517 
518 static inline uint8_t avrcp_prepare_vendor_dependent_response(uint16_t avrcp_cid, avrcp_connection_t ** out_connection, avrcp_pdu_id_t pdu_id, uint16_t param_length){
519     *out_connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
520     if (!*out_connection){
521         log_error("avrcp tartget: could not find a connection.");
522         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
523     }
524 
525     if ((*out_connection)->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
526     (*out_connection)->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
527     (*out_connection)->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
528     (*out_connection)->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
529     (*out_connection)->subunit_id      = AVRCP_SUBUNIT_ID;
530 
531     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = pdu_id;
532     // reserved
533     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = 0;
534     // param length
535     big_endian_store_16((*out_connection)->cmd_operands, (*out_connection)->cmd_operands_length, param_length);
536     (*out_connection)->cmd_operands_length += 2;
537     return ERROR_CODE_SUCCESS;
538 }
539 
540 static uint8_t avrcp_target_capability(uint16_t avrcp_cid, avrcp_capability_id_t capability_id, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
541     avrcp_connection_t * connection = NULL;
542     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_CAPABILITIES, 2+size);
543     if (status != ERROR_CODE_SUCCESS) return status;
544 
545     connection->cmd_operands[connection->cmd_operands_length++] = capability_id;
546     connection->cmd_operands[connection->cmd_operands_length++] = capabilities_num;
547     memcpy(connection->cmd_operands+connection->cmd_operands_length, capabilities, size);
548     connection->cmd_operands_length += size;
549 
550     connection->state = AVCTP_W2_SEND_RESPONSE;
551     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
552     return ERROR_CODE_SUCCESS;
553 }
554 
555 uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
556     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, capabilities_num, capabilities, size);
557 }
558 
559 uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size ){
560     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, capabilities_num, capabilities, size);
561 }
562 
563 uint8_t avrcp_target_play_status(uint16_t avrcp_cid, uint32_t song_length_ms, uint32_t song_position_ms, avrcp_playback_status_t play_status){
564     avrcp_connection_t * connection = NULL;
565     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_PLAY_STATUS, 11);
566     if (status != ERROR_CODE_SUCCESS) return status;
567 
568     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_length_ms);
569     connection->cmd_operands_length += 4;
570     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_position_ms);
571     connection->cmd_operands_length += 4;
572     connection->cmd_operands[connection->cmd_operands_length++] = play_status;
573 
574     connection->state = AVCTP_W2_SEND_RESPONSE;
575     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
576     return ERROR_CODE_SUCCESS;
577 }
578 
579 static uint8_t avrcp_target_now_playing_info(avrcp_connection_t * connection){
580     connection->now_playing_info_response = 1;
581     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
582     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
583     connection->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
584     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
585     connection->subunit_id      = AVRCP_SUBUNIT_ID;
586 
587     connection->state = AVCTP_W2_SEND_RESPONSE;
588     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
589     return ERROR_CODE_SUCCESS;
590 }
591 
592 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){
593     int index = attr_id - 1;
594     if (!value) return AVRCP_STATUS_INVALID_PARAMETER;
595     connection->now_playing_info[index].value = (uint8_t*)value;
596     connection->now_playing_info[index].len   = strlen(value);
597     // printf("store %lu bytes, %s\n",  strlen(value), value);
598     return ERROR_CODE_SUCCESS;
599 }
600 
601 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){
602     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
603     if (!connection){
604         log_error("avrcp_unit_info: could not find a connection.");
605         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
606     }
607     if (connection->playback_status == playback_status) return ERROR_CODE_SUCCESS;
608 
609     connection->playback_status = playback_status;
610     connection->playback_status_changed = 1;
611     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
612     return ERROR_CODE_SUCCESS;
613 }
614 
615 void avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){
616     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
617     if (!connection){
618         log_error("avrcp_unit_info: could not find a connection. cid 0x%02x\n", avrcp_cid);
619         return;
620     }
621     if (!current_track){
622         connection->track_selected = 0;
623         connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
624         return;
625     }
626     memcpy(connection->track_id, current_track->track_id, 8);
627     connection->song_length_ms = current_track->song_length_ms;
628     connection->track_nr = current_track->track_nr;
629     connection->total_tracks = total_tracks;
630     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title);
631     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist);
632     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album);
633     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre);
634     connection->track_selected = 1;
635 
636     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
637         connection->track_changed = 1;
638         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
639     }
640     return;
641 }
642 
643 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){
644     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
645     if (!connection){
646         log_error("avrcp_target_track_changed: could not find connection.");
647         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
648     }
649     if (!track_id) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
650 
651     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
652         connection->track_changed = 1;
653         memcpy(connection->track_id, track_id, 8);
654         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
655     }
656     return ERROR_CODE_SUCCESS;
657 }
658 
659 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){
660     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
661     if (!connection){
662         log_error("avrcp_unit_info: could not find a connection.");
663         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
664     }
665     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) {
666         connection->playing_content_changed = 1;
667         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
668     }
669     return ERROR_CODE_SUCCESS;
670 }
671 
672 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){
673     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
674     if (!connection){
675         log_error("avrcp_unit_info: could not find a connection.");
676         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
677     }
678     if (connection->battery_status == battery_status) return ERROR_CODE_SUCCESS;
679     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) {
680         connection->battery_status = battery_status;
681         connection->battery_status_changed = 1;
682         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
683     }
684     return ERROR_CODE_SUCCESS;
685 }
686 
687 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t volume_percentage){
688     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
689     if (!connection){
690         log_error("avrcp_unit_info: could not find a connection.");
691         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
692     }
693     if (connection->volume_percentage == volume_percentage) return ERROR_CODE_SUCCESS;
694     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) {
695         connection->volume_percentage = volume_percentage;
696         connection->volume_percentage_changed = 1;
697         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
698     }
699     return ERROR_CODE_SUCCESS;
700 }
701 
702 
703 static uint8_t * avrcp_get_company_id(uint8_t *packet, uint16_t size){
704     UNUSED(size);
705     return packet + 6;
706 }
707 
708 static uint8_t * avrcp_get_pdu(uint8_t *packet, uint16_t size){
709     UNUSED(size);
710     return packet + 9;
711 }
712 
713 static uint8_t avrcp_is_receive_pass_through_cmd(uint8_t operation_id){
714     return operation_id & 0x80;
715 }
716 
717 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
718     UNUSED(connection);
719     UNUSED(packet);
720     UNUSED(size);
721 
722     // uint8_t opcode;
723 
724     uint8_t transport_header = packet[0];
725     connection->transaction_label = transport_header >> 4;
726     // uint8_t packet_type = (transport_header & 0x0F) >> 2;
727     // uint8_t frame_type = (transport_header & 0x03) >> 1;
728     // uint8_t ipid = transport_header & 0x01;
729     // uint8_t byte_value = packet[2];
730     // uint16_t pid = (byte_value << 8) | packet[2];
731     // avrcp_command_type_t ctype = (avrcp_command_type_t) packet[3];
732     // uint8_t byte_value = packet[4];
733     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
734     avrcp_subunit_id_t   subunit_id   = (avrcp_subunit_id_t) (packet[4] & 0x07);
735     // opcode = packet[pos++];
736 
737     // printf("    Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n",
738     //     transport_header, transaction_label, packet_type, frame_type, ipid, pid);
739     // printf_hexdump(packet+pos, size-pos);
740 
741     avrcp_command_opcode_t opcode = avrcp_cmd_opcode(packet,size);
742     uint8_t * company_id = avrcp_get_company_id(packet, size);
743     uint8_t * pdu = avrcp_get_pdu(packet, size);
744     // uint16_t param_length = big_endian_read_16(pdu, 2);
745 
746     int pos = 4;
747     uint8_t   pdu_id;
748     connection->cmd_operands_length = 0;
749 
750     switch (opcode){
751         case AVRCP_CMD_OPCODE_UNIT_INFO:
752             avrcp_target_unit_info(connection);
753             break;
754         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
755             uint8_t offset =  4 * (packet[pos+2]>>4);
756             avrcp_target_subunit_info(connection, offset);
757             break;
758         }
759         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
760             log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d, operand %d", packet[6], packet[7], packet[8]);
761             avrcp_operation_id_t operation_id = packet[6];
762 
763             if (avrcp_is_receive_pass_through_cmd(operation_id)){
764                 operation_id = packet[6] & 0x7F;
765                 avrcp_target_operation_accepted(connection->avrcp_cid, packet[6], packet[7], packet[8]);
766                 break;
767             }
768 
769             switch (operation_id){
770                 case AVRCP_OPERATION_ID_PLAY:
771                 case AVRCP_OPERATION_ID_PAUSE:
772                 case AVRCP_OPERATION_ID_STOP:
773                 case AVRCP_OPERATION_ID_VOLUME_UP:
774                 case AVRCP_OPERATION_ID_VOLUME_DOWN:
775                 case AVRCP_OPERATION_ID_REWIND:
776                 case AVRCP_OPERATION_ID_FAST_FORWARD:
777                 case AVRCP_OPERATION_ID_FORWARD:
778                 case AVRCP_OPERATION_ID_BACKWARD:
779                 case AVRCP_OPERATION_ID_SKIP:
780                 case AVRCP_OPERATION_ID_MUTE:
781                 case AVRCP_OPERATION_ID_CHANNEL_UP:
782                 case AVRCP_OPERATION_ID_CHANNEL_DOWN:
783                 case AVRCP_OPERATION_ID_SELECT:
784                 case AVRCP_OPERATION_ID_UP:
785                 case AVRCP_OPERATION_ID_DOWN:
786                 case AVRCP_OPERATION_ID_LEFT:
787                 case AVRCP_OPERATION_ID_RIGHT:
788                 case AVRCP_OPERATION_ID_ROOT_MENU:
789                     printf("received op 0x%02x\n", operation_id);
790                     avrcp_target_operation_accepted(connection->avrcp_cid, packet[6], packet[7], packet[8]);
791                     avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, operation_id, packet[7], packet[8]);
792                     break;
793                 case AVRCP_OPERATION_ID_UNDEFINED:
794                     avrcp_target_operation_not_implemented(connection->avrcp_cid, packet[6], packet[7], packet[8]);
795                     return;
796                 default:
797                     avrcp_target_operation_not_implemented(connection->avrcp_cid, packet[6], packet[7], packet[8]);
798                     return;
799             }
800             break;
801         }
802 
803         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
804             pdu_id = pdu[0];
805             // 1 - reserved
806             // 2-3 param length,
807             memcpy(connection->cmd_operands, company_id, 3);
808             connection->cmd_operands_length = 3;
809             switch (pdu_id){
810                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
811                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) pdu[pos];
812                     switch (capability_id){
813                         case AVRCP_CAPABILITY_ID_EVENT:
814                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY);
815                             break;
816                         case AVRCP_CAPABILITY_ID_COMPANY:
817                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY);
818                             break;
819                         default:
820                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
821                             break;
822                     }
823                     break;
824                 }
825                 case AVRCP_PDU_ID_GET_PLAY_STATUS:
826                     avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
827                     break;
828                 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE:
829                     connection->cmd_operands[0] = pdu[4];
830                     connection->abort_continue_response = 1;
831                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
832                     break;
833                 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE:
834                     if (pdu[4] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){
835                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
836                         return;
837                     }
838                     connection->now_playing_info_response = 1;
839                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
840                     break;
841                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
842                     uint8_t play_identifier[8];
843                     memset(play_identifier, 0, 8);
844                     if (memcmp(pdu+pos, play_identifier, 8) != 0) {
845                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
846                         return;
847                     }
848                     pos += 8;
849                     uint8_t attribute_count = pdu[pos++];
850                     // printf("AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES attribute count %d\n", attribute_count);
851                     connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE;
852                     if (!attribute_count){
853                         connection->now_playing_info_attr_bitmap = 0xFE;
854                     } else {
855                         int i;
856                         connection->now_playing_info_attr_bitmap = 0;
857                         for (i=0; i < attribute_count; i++){
858                             uint16_t attr_id = big_endian_read_16(pdu, pos);
859                             pos += 2;
860                             connection->now_playing_info_attr_bitmap |= (1 << attr_id);
861                         }
862                     }
863                     log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap);
864                     avrcp_target_now_playing_info(connection);
865                     break;
866                 }
867                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
868                     // 0 - pdu id
869                     // 1 - reserved
870                     // 2-3 param length
871                     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) pdu[4];
872                     uint16_t event_mask = (1 << event_id);
873 
874                     switch (event_id){
875                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:
876                             connection->notifications_enabled |= event_mask;
877                             if (connection->track_selected){
878                                 avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8);
879                             } else {
880                                 avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8);
881                             }
882                             break;
883                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:
884                             connection->notifications_enabled |= event_mask;
885                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1);
886                             break;
887                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:
888                             connection->notifications_enabled |= event_mask;
889                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, NULL, 0);
890                             break;
891                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:
892                             connection->notifications_enabled |= event_mask;
893                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->volume_percentage, 1);
894                             break;
895                         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:
896                             connection->notifications_enabled |= event_mask;
897                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1);
898                             break;
899                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:
900                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
901                         case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED:
902                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:
903                             avrcp_target_response_not_implemented(connection, subunit_type, subunit_id, opcode, pdu_id, event_id);
904                             return;
905                         default:
906                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
907                             return;
908                     }
909                     break;
910                 }
911                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: {
912                     uint16_t param_length = big_endian_read_16(pdu,2);
913                     if (param_length != 1){
914                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
915                         printf("param_length is %d != 1 \n", param_length);
916                         break;
917                     }
918 
919                     uint8_t absolute_volume = pdu[4];
920                     if (absolute_volume > 0x7F){
921                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
922                         printf("absolute_volume %d  > 127 \n", absolute_volume);
923                         break;
924                     }
925                     connection->volume_percentage = absolute_volume;
926                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, absolute_volume);
927                     break;
928                 }
929                 default:
930                     log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id);
931                     avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
932                     break;
933             }
934             break;
935         default:
936             log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size));
937             break;
938     }
939 }
940 
941 static int avrcp_target_send_notification(uint16_t cid, avrcp_connection_t * connection, uint8_t notification_id, uint8_t * value, uint16_t value_len){
942     if (!connection){
943         log_error("avrcp tartget: could not find a connection.");
944         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
945     }
946 
947     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
948     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
949     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
950     connection->subunit_id      = AVRCP_SUBUNIT_ID;
951 
952     uint16_t pos = 0;
953     l2cap_reserve_packet_buffer();
954     uint8_t * packet = l2cap_get_outgoing_buffer();
955     uint16_t  size   = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
956 
957     connection->packet_type = AVRCP_SINGLE_PACKET;
958     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
959     // Profile IDentifier (PID)
960     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
961     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
962 
963     // command_type
964     packet[pos++] = connection->command_type;
965     // subunit_type | subunit ID
966     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
967     // opcode
968     packet[pos++] = (uint8_t)connection->command_opcode;
969 
970     // company id is 3 bytes long
971     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
972     pos += 3;
973 
974     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
975     packet[pos++] = 0;
976     uint16_t remainig_outgoing_buffer_size = size > (value_len + 2 + 1) ? size - (value_len + 2 + 1): 0;
977     uint16_t caped_value_len = value_len > remainig_outgoing_buffer_size ? remainig_outgoing_buffer_size : value_len;
978     big_endian_store_16(packet, pos, caped_value_len);
979     pos += 2;
980     packet[pos++] = notification_id;
981 
982     memcpy(packet+pos, value, caped_value_len);
983     pos += caped_value_len;
984     connection->wait_to_send = 0;
985     return l2cap_send_prepared(cid, pos);
986 }
987 
988 static void avrcp_target_reset_notification(avrcp_connection_t * connection, uint8_t notification_id){
989     if (!connection){
990         log_error("avrcp tartget: could not find a connection.");
991         return;
992     }
993     connection->notifications_enabled &= ~(1 << notification_id);
994     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
995 
996 }
997 
998 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
999     avrcp_connection_t * connection;
1000     switch (packet_type) {
1001         case L2CAP_DATA_PACKET:
1002             connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
1003             if (!connection) break;
1004             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1005             break;
1006         case HCI_EVENT_PACKET:
1007             switch (hci_event_packet_get_type(packet)){
1008                 case L2CAP_EVENT_CAN_SEND_NOW:{
1009                     connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
1010                     if (!connection) {
1011                         log_error("Connection not found\n");
1012                         break;
1013                     }
1014 
1015                     if (connection->abort_continue_response){
1016                         connection->abort_continue_response = 0;
1017                         connection->now_playing_info_response = 0;
1018                         avrcp_target_abort_continue_response(connection->l2cap_signaling_cid, connection);
1019                         break;
1020                     }
1021 
1022                     if (connection->now_playing_info_response){
1023                         connection->now_playing_info_response = 0;
1024                         avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1025                         break;
1026                     }
1027 
1028                     if (connection->track_changed){
1029                         connection->track_changed = 0;
1030                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED, connection->track_id, 8);
1031                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED);
1032                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1033                         break;
1034                     }
1035 
1036                     if (connection->playback_status_changed){
1037                         connection->playback_status_changed = 0;
1038                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED, &connection->playback_status_changed, 1);
1039                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED);
1040                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1041                         break;
1042                     }
1043 
1044                     if (connection->playing_content_changed){
1045                         connection->playing_content_changed = 0;
1046                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED, NULL, 0);
1047                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED);
1048                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1049                         break;
1050                     }
1051 
1052                     if (connection->battery_status_changed){
1053                         connection->battery_status_changed = 0;
1054                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED, (uint8_t *)&connection->battery_status, 1);
1055                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED);
1056                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1057                         break;
1058                     }
1059 
1060                     if (connection->volume_percentage_changed){
1061                         connection->volume_percentage_changed = 0;
1062                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED, &connection->volume_percentage, 1);
1063                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED);
1064                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1065                         break;
1066                     }
1067 
1068                     switch (connection->state){
1069                         case AVCTP_W2_SEND_RESPONSE:
1070                             connection->state = AVCTP_CONNECTION_OPENED;
1071                             // if (connection->now_playing_info_response){
1072                             //     connection->now_playing_info_response = 0;
1073                             //     avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1074                             //     break;
1075                             // }
1076                             avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
1077                             break;
1078                         default:
1079                             return;
1080                     }
1081                     break;
1082             }
1083             default:
1084                 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_target_context);
1085                 break;
1086         }
1087         default:
1088             break;
1089     }
1090 }
1091 
1092 void avrcp_target_init(void){
1093     avrcp_target_context.role = AVRCP_TARGET;
1094     avrcp_target_context.connections = NULL;
1095     avrcp_target_context.packet_handler = avrcp_target_packet_handler;
1096     l2cap_register_service(&avrcp_target_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0);
1097 }
1098 
1099 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
1100     if (callback == NULL){
1101         log_error("avrcp_register_packet_handler called with NULL callback");
1102         return;
1103     }
1104     avrcp_target_context.avrcp_callback = callback;
1105 }
1106 
1107 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){
1108     return avrcp_connect(bd_addr, &avrcp_target_context, avrcp_cid);
1109 }
1110 
1111 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){
1112     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
1113     if (!connection){
1114         log_error("avrcp_get_capabilities: could not find a connection.");
1115         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1116     }
1117     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1118     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
1119     return ERROR_CODE_SUCCESS;
1120 }
1121 
1122