xref: /btstack/src/classic/avrcp_target.c (revision 6704943572d070786e23f9eecd82c4aa3ea7c3ab)
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,
374     avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){
375     connection->command_type = AVRCP_CTYPE_RESPONSE_INTERIM;
376     connection->subunit_type = subunit_type;
377     connection->subunit_id =   subunit_id;
378     connection->command_opcode = opcode;
379 
380     // company id is 3 bytes long
381     int pos = connection->cmd_operands_length;
382     connection->cmd_operands[pos++] = pdu_id;
383     connection->cmd_operands[pos++] = 0;
384     // param length
385     big_endian_store_16(connection->cmd_operands, pos, 1 + value_len);
386     pos += 2;
387     connection->cmd_operands[pos++] = event_id;
388     if (value && value_len > 0){
389         memcpy(connection->cmd_operands + pos, value, value_len);
390         pos += value_len;
391     }
392     connection->cmd_operands_length = pos;
393 
394     connection->state = AVCTP_W2_SEND_RESPONSE;
395     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
396     return ERROR_CODE_SUCCESS;
397 }
398 
399 static uint8_t avrcp_target_response_addressed_player_changed_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){
400     connection->command_type = AVRCP_CTYPE_RESPONSE_INTERIM;
401     connection->subunit_type = subunit_type;
402     connection->subunit_id =   subunit_id;
403     connection->command_opcode = opcode;
404 
405     // company id is 3 bytes long
406     int pos = connection->cmd_operands_length;
407     connection->cmd_operands[pos++] = pdu_id;
408     connection->cmd_operands[pos++] = 0;
409     // param length
410     big_endian_store_16(connection->cmd_operands, pos, 5);
411     pos += 2;
412     connection->cmd_operands[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
413     big_endian_read_16( &connection->cmd_operands[pos], connection->addressed_player_id);
414     pos += 2;
415     big_endian_read_16( &connection->cmd_operands[pos], connection->uid_counter);
416     pos += 2;
417 
418     connection->cmd_operands_length = pos;
419     connection->state = AVCTP_W2_SEND_RESPONSE;
420     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
421     return ERROR_CODE_SUCCESS;
422 }
423 
424 
425 // static uint8_t avrcp_target_response_vendor_dependent_changed(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){
426 //     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
427 //     connection->command_type = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
428 //     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
429 //     connection->subunit_id = AVRCP_SUBUNIT_ID;
430 
431 //     // company id is 3 bytes long
432 //     int pos = connection->cmd_operands_length;
433 //     connection->cmd_operands[pos++] = pdu_id;
434 //     connection->cmd_operands[pos++] = 0;
435 //     // param length
436 //     big_endian_store_16(connection->cmd_operands, pos, 1);
437 //     pos += 2;
438 //     connection->cmd_operands[pos++] = event_id;
439 //     connection->cmd_operands_length = pos;
440 
441 //     connection->state = AVCTP_W2_SEND_RESPONSE;
442 //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
443 //     return ERROR_CODE_SUCCESS;
444 // }
445 
446 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){
447     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
448     if (!connection){
449         log_error("Could not find a connection.");
450         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
451     }
452     connection->command_type = cmd_type;
453     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL;
454     connection->subunit_id =   AVRCP_SUBUNIT_ID;
455     connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH;
456 
457     int pos = 0;
458     connection->cmd_operands[pos++] = opid;
459     connection->cmd_operands[pos++] = operands_length;
460     if (operands_length == 1){
461         connection->cmd_operands[pos++] = operand;
462     }
463     connection->cmd_operands_length = pos;
464 
465     connection->state = AVCTP_W2_SEND_RESPONSE;
466     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
467     return ERROR_CODE_SUCCESS;
468 }
469 
470 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
471     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand);
472 }
473 
474 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
475     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
476 }
477 
478 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){
479     return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand);
480 }
481 
482 void avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
483     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
484     if (!connection){
485         log_error("avrcp_target_operation_reject: could not find a connection.");
486         return;
487     }
488     connection->unit_type = unit_type;
489     connection->company_id = company_id;
490 }
491 
492 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){
493     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
494     if (!connection){
495         log_error("avrcp_target_operation_reject: could not find a connection.");
496         return;
497     }
498     connection->subunit_info_type = subunit_type;
499     connection->subunit_info_data = subunit_info_data;
500     connection->subunit_info_data_size = subunit_info_data_size;
501 }
502 
503 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){
504     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
505 
506     uint8_t unit = 0;
507     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
508     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
509     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
510     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
511 
512     connection->cmd_operands_length = 5;
513     connection->cmd_operands[0] = 0x07;
514     connection->cmd_operands[1] = (connection->unit_type << 4) | unit;
515     // company id is 3 bytes long
516     big_endian_store_32(connection->cmd_operands, 2, connection->company_id);
517 
518     connection->state = AVCTP_W2_SEND_RESPONSE;
519     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
520     return ERROR_CODE_SUCCESS;
521 }
522 
523 
524 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){
525     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
526     if (offset - 4 > connection->subunit_info_data_size) return AVRCP_STATUS_INVALID_PARAMETER;
527 
528     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
529     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
530     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
531     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
532     // printf("avrcp_target_subunit_info  subunit_type %d\n", connection->subunit_type);
533 
534     uint8_t page = offset / 4;
535     uint8_t extension_code = 7;
536     connection->cmd_operands_length = 5;
537     connection->cmd_operands[0] = (page << 4) | extension_code;
538 
539     memcpy(connection->cmd_operands+1, connection->subunit_info_data + offset, 4);
540 
541     connection->state = AVCTP_W2_SEND_RESPONSE;
542     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
543     return ERROR_CODE_SUCCESS;
544 }
545 
546 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){
547     *out_connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
548     if (!*out_connection){
549         log_error("avrcp tartget: could not find a connection.");
550         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
551     }
552 
553     if ((*out_connection)->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
554     (*out_connection)->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
555     (*out_connection)->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
556     (*out_connection)->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
557     (*out_connection)->subunit_id      = AVRCP_SUBUNIT_ID;
558 
559     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = pdu_id;
560     // reserved
561     (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = 0;
562     // param length
563     big_endian_store_16((*out_connection)->cmd_operands, (*out_connection)->cmd_operands_length, param_length);
564     (*out_connection)->cmd_operands_length += 2;
565     return ERROR_CODE_SUCCESS;
566 }
567 
568 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){
569     avrcp_connection_t * connection = NULL;
570     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_CAPABILITIES, 2+size);
571     if (status != ERROR_CODE_SUCCESS) return status;
572 
573     connection->cmd_operands[connection->cmd_operands_length++] = capability_id;
574     connection->cmd_operands[connection->cmd_operands_length++] = capabilities_num;
575     memcpy(connection->cmd_operands+connection->cmd_operands_length, capabilities, size);
576     connection->cmd_operands_length += size;
577 
578     connection->state = AVCTP_W2_SEND_RESPONSE;
579     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
580     return ERROR_CODE_SUCCESS;
581 }
582 
583 uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
584     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, capabilities_num, capabilities, size);
585 }
586 
587 uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size ){
588     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, capabilities_num, capabilities, size);
589 }
590 
591 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){
592     avrcp_connection_t * connection = NULL;
593     uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_PLAY_STATUS, 11);
594     if (status != ERROR_CODE_SUCCESS) return status;
595 
596     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_length_ms);
597     connection->cmd_operands_length += 4;
598     big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_position_ms);
599     connection->cmd_operands_length += 4;
600     connection->cmd_operands[connection->cmd_operands_length++] = play_status;
601 
602     connection->state = AVCTP_W2_SEND_RESPONSE;
603     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
604     return ERROR_CODE_SUCCESS;
605 }
606 
607 static uint8_t avrcp_target_now_playing_info(avrcp_connection_t * connection){
608     connection->now_playing_info_response = 1;
609     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
610     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
611     connection->command_type    = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
612     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
613     connection->subunit_id      = AVRCP_SUBUNIT_ID;
614 
615     connection->state = AVCTP_W2_SEND_RESPONSE;
616     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
617     return ERROR_CODE_SUCCESS;
618 }
619 
620 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){
621     int index = attr_id - 1;
622     if (!value) return AVRCP_STATUS_INVALID_PARAMETER;
623     connection->now_playing_info[index].value = (uint8_t*)value;
624     connection->now_playing_info[index].len   = strlen(value);
625     // printf("store %lu bytes, %s\n",  strlen(value), value);
626     return ERROR_CODE_SUCCESS;
627 }
628 
629 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){
630     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
631     if (!connection){
632         log_error("avrcp_unit_info: could not find a connection.");
633         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
634     }
635     if (connection->playback_status == playback_status) return ERROR_CODE_SUCCESS;
636 
637     connection->playback_status = playback_status;
638     connection->playback_status_changed = 1;
639     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
640     return ERROR_CODE_SUCCESS;
641 }
642 
643 void avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){
644     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
645     if (!connection){
646         log_error("avrcp_unit_info: could not find a connection. cid 0x%02x\n", avrcp_cid);
647         return;
648     }
649     if (!current_track){
650         connection->track_selected = 0;
651         connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
652         return;
653     }
654     memcpy(connection->track_id, current_track->track_id, 8);
655     connection->song_length_ms = current_track->song_length_ms;
656     connection->track_nr = current_track->track_nr;
657     connection->total_tracks = total_tracks;
658     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title);
659     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist);
660     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album);
661     avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre);
662     connection->track_selected = 1;
663 
664     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
665         connection->track_changed = 1;
666         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
667     }
668     return;
669 }
670 
671 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){
672     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
673     if (!connection){
674         log_error("avrcp_target_track_changed: could not find connection.");
675         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
676     }
677     if (!track_id) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
678 
679     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) {
680         connection->track_changed = 1;
681         memcpy(connection->track_id, track_id, 8);
682         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
683     }
684     return ERROR_CODE_SUCCESS;
685 }
686 
687 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){
688     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
689     if (!connection){
690         log_error("avrcp_target_playing_content_changed: could not find a connection.");
691         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
692     }
693     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) {
694         connection->playing_content_changed = 1;
695         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
696     }
697     return ERROR_CODE_SUCCESS;
698 }
699 
700 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){
701     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
702     if (!connection){
703         log_error("avrcp_unit_info: could not find a connection.");
704         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
705     }
706     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) {
707         // printf("send AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED\n");
708         // connection->addressed_player_changed = 1;
709         connection->uid_counter = uid_counter;
710         connection->addressed_player_id = player_id;
711         // avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
712     }
713     return ERROR_CODE_SUCCESS;
714 }
715 
716 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){
717     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
718     if (!connection){
719         log_error("avrcp_unit_info: could not find a connection.");
720         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
721     }
722     if (connection->battery_status == battery_status) return ERROR_CODE_SUCCESS;
723     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) {
724         connection->battery_status = battery_status;
725         connection->battery_status_changed = 1;
726         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
727     }
728     return ERROR_CODE_SUCCESS;
729 }
730 
731 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t volume_percentage){
732     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
733     if (!connection){
734         log_error("avrcp_unit_info: could not find a connection.");
735         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
736     }
737     if (connection->volume_percentage == volume_percentage) return ERROR_CODE_SUCCESS;
738     if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) {
739         connection->volume_percentage = volume_percentage;
740         connection->volume_percentage_changed = 1;
741         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
742     }
743     return ERROR_CODE_SUCCESS;
744 }
745 
746 
747 static uint8_t * avrcp_get_company_id(uint8_t *packet, uint16_t size){
748     UNUSED(size);
749     return packet + 6;
750 }
751 
752 static uint8_t * avrcp_get_pdu(uint8_t *packet, uint16_t size){
753     UNUSED(size);
754     return packet + 9;
755 }
756 
757 static uint8_t avrcp_is_receive_pass_through_cmd(uint8_t operation_id){
758     return operation_id & 0x80;
759 }
760 
761 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
762     UNUSED(connection);
763     UNUSED(packet);
764     UNUSED(size);
765 
766     // uint8_t opcode;
767 
768     uint8_t transport_header = packet[0];
769     connection->transaction_label = transport_header >> 4;
770     // uint8_t packet_type = (transport_header & 0x0F) >> 2;
771     // uint8_t frame_type = (transport_header & 0x03) >> 1;
772 
773     uint16_t pid = big_endian_read_16(packet, 1);
774     printf("PID 0x%02x, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL 0x%02x, header 0x%02x\n",pid,  BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL, transport_header);
775     if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){
776         connection->reject_transport_header = 1;
777         connection->transport_header = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2 ) | (AVRCP_RESPONSE_FRAME << 1) | 0;
778 
779         printf("reject pid, header 0x%02x\n", connection->transport_header);
780         connection->state = AVCTP_W2_SEND_RESPONSE;
781         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
782         return;
783     }
784 
785     // avrcp_command_type_t ctype = (avrcp_command_type_t) packet[3];
786     // uint8_t byte_value = packet[4];
787     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
788     avrcp_subunit_id_t   subunit_id   = (avrcp_subunit_id_t) (packet[4] & 0x07);
789     // opcode = packet[pos++];
790 
791     // printf("    Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n",
792     //     transport_header, transaction_label, packet_type, frame_type, ipid, pid);
793     // printf_hexdump(packet+pos, size-pos);
794 
795     avrcp_command_opcode_t opcode = avrcp_cmd_opcode(packet,size);
796     uint8_t * company_id = avrcp_get_company_id(packet, size);
797     uint8_t * pdu = avrcp_get_pdu(packet, size);
798     // uint16_t param_length = big_endian_read_16(pdu, 2);
799 
800     int pos = 4;
801     uint8_t   pdu_id;
802     connection->cmd_operands_length = 0;
803 
804     switch (opcode){
805         case AVRCP_CMD_OPCODE_UNIT_INFO:
806             avrcp_target_unit_info(connection);
807             break;
808         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
809             uint8_t offset =  4 * (packet[pos+2]>>4);
810             avrcp_target_subunit_info(connection, offset);
811             break;
812         }
813         case AVRCP_CMD_OPCODE_PASS_THROUGH:{
814             log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d, operand %d", packet[6], packet[7], packet[8]);
815             avrcp_operation_id_t operation_id = packet[6];
816 
817             if (avrcp_is_receive_pass_through_cmd(operation_id)){
818                 operation_id = packet[6] & 0x7F;
819                 avrcp_target_operation_accepted(connection->avrcp_cid, packet[6], packet[7], packet[8]);
820                 break;
821             }
822 
823             switch (operation_id){
824                 case AVRCP_OPERATION_ID_PLAY:
825                 case AVRCP_OPERATION_ID_PAUSE:
826                 case AVRCP_OPERATION_ID_STOP:
827                 case AVRCP_OPERATION_ID_VOLUME_UP:
828                 case AVRCP_OPERATION_ID_VOLUME_DOWN:
829                 case AVRCP_OPERATION_ID_REWIND:
830                 case AVRCP_OPERATION_ID_FAST_FORWARD:
831                 case AVRCP_OPERATION_ID_FORWARD:
832                 case AVRCP_OPERATION_ID_BACKWARD:
833                 case AVRCP_OPERATION_ID_SKIP:
834                 case AVRCP_OPERATION_ID_MUTE:
835                 case AVRCP_OPERATION_ID_CHANNEL_UP:
836                 case AVRCP_OPERATION_ID_CHANNEL_DOWN:
837                 case AVRCP_OPERATION_ID_SELECT:
838                 case AVRCP_OPERATION_ID_UP:
839                 case AVRCP_OPERATION_ID_DOWN:
840                 case AVRCP_OPERATION_ID_LEFT:
841                 case AVRCP_OPERATION_ID_RIGHT:
842                 case AVRCP_OPERATION_ID_ROOT_MENU:
843                     avrcp_target_operation_accepted(connection->avrcp_cid, packet[6], packet[7], packet[8]);
844                     avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, operation_id, packet[7], packet[8]);
845                     break;
846                 case AVRCP_OPERATION_ID_UNDEFINED:
847                     avrcp_target_operation_not_implemented(connection->avrcp_cid, packet[6], packet[7], packet[8]);
848                     return;
849                 default:
850                     avrcp_target_operation_not_implemented(connection->avrcp_cid, packet[6], packet[7], packet[8]);
851                     return;
852             }
853             break;
854         }
855 
856         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
857             pdu_id = pdu[0];
858             // 1 - reserved
859             // 2-3 param length,
860             uint16_t length = big_endian_read_16(pdu, 2);
861             memcpy(connection->cmd_operands, company_id, 3);
862             connection->cmd_operands_length = 3;
863             switch (pdu_id){
864                 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{
865                     if (length == 0){
866                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID);
867                         break;
868                     }
869                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_SUCCESS);
870                     break;
871                 }
872                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
873                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) pdu[pos];
874                     switch (capability_id){
875                         case AVRCP_CAPABILITY_ID_EVENT:
876                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY);
877                             break;
878                         case AVRCP_CAPABILITY_ID_COMPANY:
879                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY);
880                             break;
881                         default:
882                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
883                             break;
884                     }
885                     break;
886                 }
887                 case AVRCP_PDU_ID_GET_PLAY_STATUS:
888                     avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
889                     break;
890                 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE:
891                     connection->cmd_operands[0] = pdu[4];
892                     connection->abort_continue_response = 1;
893                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
894                     break;
895                 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE:
896                     if (pdu[4] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){
897                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
898                         return;
899                     }
900                     connection->now_playing_info_response = 1;
901                     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
902                     break;
903                 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{
904                     uint8_t play_identifier[8];
905                     memset(play_identifier, 0, 8);
906                     if (memcmp(pdu+pos, play_identifier, 8) != 0) {
907                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
908                         return;
909                     }
910                     pos += 8;
911                     uint8_t attribute_count = pdu[pos++];
912                     // printf("AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES attribute count %d\n", attribute_count);
913                     connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE;
914                     if (!attribute_count){
915                         connection->now_playing_info_attr_bitmap = 0xFE;
916                     } else {
917                         int i;
918                         connection->now_playing_info_attr_bitmap = 0;
919                         for (i=0; i < attribute_count; i++){
920                             uint16_t attr_id = big_endian_read_16(pdu, pos);
921                             pos += 2;
922                             connection->now_playing_info_attr_bitmap |= (1 << attr_id);
923                         }
924                     }
925                     log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap);
926                     avrcp_target_now_playing_info(connection);
927                     break;
928                 }
929                 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{
930                     // 0 - pdu id
931                     // 1 - reserved
932                     // 2-3 param length
933                     avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) pdu[4];
934                     uint16_t event_mask = (1 << event_id);
935 
936                     switch (event_id){
937                         case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:
938                             connection->notifications_enabled |= event_mask;
939                             if (connection->track_selected){
940                                 avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8);
941                             } else {
942                                 avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8);
943                             }
944                             break;
945                         case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:
946                             connection->notifications_enabled |= event_mask;
947                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1);
948                             break;
949                         case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:
950                             connection->notifications_enabled |= event_mask;
951                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, NULL, 0);
952                             break;
953                         case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:
954                             connection->notifications_enabled |= event_mask;
955                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->volume_percentage, 1);
956                             break;
957                         case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:
958                             connection->notifications_enabled |= event_mask;
959                             avrcp_target_response_vendor_dependent_interim(connection, subunit_type, subunit_id, opcode, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1);
960                             break;
961                         case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:
962                         case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:
963                         case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:
964                             avrcp_target_response_not_implemented(connection, subunit_type, subunit_id, opcode, pdu_id, event_id);
965                             return;
966                         case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED:
967                             connection->notifications_enabled |= event_mask;
968                             avrcp_target_response_addressed_player_changed_interim(connection, subunit_type, subunit_id, opcode, pdu_id);
969                             return;
970                         default:
971                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
972                             return;
973                     }
974                     break;
975                 }
976                 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: {
977                     if (length != 1){
978                         avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
979                         break;
980                     }
981 
982                     uint8_t absolute_volume = pdu[4];
983                     if (absolute_volume < 0x80){
984                         connection->volume_percentage = absolute_volume;
985                     }
986                     avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, connection->volume_percentage);
987                     break;
988                 }
989                 default:
990                     log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id);
991                     avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
992                     break;
993             }
994             break;
995         default:
996             log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size));
997             break;
998     }
999 }
1000 
1001 #if 0
1002 static int avrcp_target_send_addressed_player_changed_notification(uint16_t cid, avrcp_connection_t * connection, uint16_t uid, uint16_t uid_counter){
1003     if (!connection){
1004         log_error("avrcp tartget: could not find a connection.");
1005         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1006     }
1007     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1008     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
1009     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
1010     connection->subunit_id      = AVRCP_SUBUNIT_ID;
1011 
1012     uint16_t pos = 0;
1013     l2cap_reserve_packet_buffer();
1014     uint8_t * packet = l2cap_get_outgoing_buffer();
1015 
1016     connection->packet_type = AVRCP_SINGLE_PACKET;
1017     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
1018     // Profile IDentifier (PID)
1019     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
1020     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
1021 
1022     // command_type
1023     packet[pos++] = connection->command_type;
1024     // subunit_type | subunit ID
1025     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
1026     // opcode
1027     packet[pos++] = (uint8_t)connection->command_opcode;
1028 
1029     // company id is 3 bytes long
1030     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
1031     pos += 3;
1032 
1033     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
1034     packet[pos++] = 0;
1035     big_endian_store_16(packet, pos, 5);
1036     pos += 2;
1037     packet[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED;
1038     big_endian_store_16(packet, pos, uid);
1039     pos += 2;
1040     big_endian_store_16(packet, pos, uid_counter);
1041     pos += 2;
1042 
1043     connection->wait_to_send = 0;
1044     return l2cap_send_prepared(cid, pos);
1045 }
1046 #endif
1047 
1048 static int avrcp_target_send_notification(uint16_t cid, avrcp_connection_t * connection, uint8_t notification_id, uint8_t * value, uint16_t value_len){
1049     if (!connection){
1050         log_error("avrcp tartget: could not find a connection.");
1051         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1052     }
1053 
1054     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1055     connection->command_type    = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE;
1056     connection->subunit_type    = AVRCP_SUBUNIT_TYPE_PANEL;
1057     connection->subunit_id      = AVRCP_SUBUNIT_ID;
1058 
1059     uint16_t pos = 0;
1060     l2cap_reserve_packet_buffer();
1061     uint8_t * packet = l2cap_get_outgoing_buffer();
1062     uint16_t  size   = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid);
1063 
1064     connection->packet_type = AVRCP_SINGLE_PACKET;
1065     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
1066     // Profile IDentifier (PID)
1067     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
1068     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
1069 
1070     // command_type
1071     packet[pos++] = connection->command_type;
1072     // subunit_type | subunit ID
1073     packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
1074     // opcode
1075     packet[pos++] = (uint8_t)connection->command_opcode;
1076 
1077     // company id is 3 bytes long
1078     big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
1079     pos += 3;
1080 
1081     packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION;
1082     packet[pos++] = 0;
1083     uint16_t remainig_outgoing_buffer_size = size > (value_len + 2 + 1) ? size - (value_len + 2 + 1): 0;
1084     uint16_t caped_value_len = value_len > remainig_outgoing_buffer_size ? remainig_outgoing_buffer_size : value_len;
1085     big_endian_store_16(packet, pos, caped_value_len);
1086     pos += 2;
1087     packet[pos++] = notification_id;
1088 
1089     memcpy(packet+pos, value, caped_value_len);
1090     pos += caped_value_len;
1091     connection->wait_to_send = 0;
1092     return l2cap_send_prepared(cid, pos);
1093 }
1094 
1095 static void avrcp_target_reset_notification(avrcp_connection_t * connection, uint8_t notification_id){
1096     if (!connection){
1097         log_error("avrcp tartget: could not find a connection.");
1098         return;
1099     }
1100     connection->notifications_enabled &= ~(1 << notification_id);
1101     connection->command_opcode  = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
1102 
1103 }
1104 
1105 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1106     avrcp_connection_t * connection;
1107     switch (packet_type) {
1108         case L2CAP_DATA_PACKET:
1109             connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
1110             if (!connection) break;
1111             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
1112             break;
1113         case HCI_EVENT_PACKET:
1114             switch (hci_event_packet_get_type(packet)){
1115                 case L2CAP_EVENT_CAN_SEND_NOW:{
1116                     connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
1117                     if (!connection) {
1118                         log_error("Connection not found\n");
1119                         break;
1120                     }
1121 
1122                     if (connection->reject_transport_header){
1123                         connection->reject_transport_header = 0;
1124                         printf("send reject_transport_header \n");
1125                         l2cap_reserve_packet_buffer();
1126                         uint8_t * out_buffer = l2cap_get_outgoing_buffer();
1127                         out_buffer[0] = connection->transport_header;
1128                         l2cap_send_prepared(connection->l2cap_signaling_cid, 1);
1129                         break;
1130                     }
1131 
1132                     if (connection->abort_continue_response){
1133                         connection->abort_continue_response = 0;
1134                         connection->now_playing_info_response = 0;
1135                         avrcp_target_abort_continue_response(connection->l2cap_signaling_cid, connection);
1136                         break;
1137                     }
1138 
1139                     if (connection->now_playing_info_response){
1140                         connection->now_playing_info_response = 0;
1141                         avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1142                         break;
1143                     }
1144 
1145                     if (connection->track_changed){
1146                         connection->track_changed = 0;
1147                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED, connection->track_id, 8);
1148                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED);
1149                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1150                         break;
1151                     }
1152 
1153                     if (connection->playback_status_changed){
1154                         connection->playback_status_changed = 0;
1155                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED, &connection->playback_status_changed, 1);
1156                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED);
1157                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1158                         break;
1159                     }
1160 
1161                     // if (connection->addressed_player_changed){
1162                     //     connection->playback_status_changed = 0;
1163                     //     avrcp_target_send_addressed_player_changed_notification(connection->l2cap_signaling_cid, connection, connection->addressed_player_id, connection->uid_counter);
1164                     //     avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED);
1165                     //     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1166                     //     break;
1167                     // }
1168 
1169                     if (connection->playing_content_changed){
1170                         connection->playing_content_changed = 0;
1171                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED, NULL, 0);
1172                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED);
1173                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1174                         break;
1175                     }
1176 
1177                     if (connection->battery_status_changed){
1178                         connection->battery_status_changed = 0;
1179                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED, (uint8_t *)&connection->battery_status, 1);
1180                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED);
1181                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1182                         break;
1183                     }
1184 
1185                     if (connection->volume_percentage_changed){
1186                         connection->volume_percentage_changed = 0;
1187                         avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED, &connection->volume_percentage, 1);
1188                         avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED);
1189                         avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
1190                         break;
1191                     }
1192 
1193                     switch (connection->state){
1194                         case AVCTP_W2_SEND_RESPONSE:
1195                             connection->state = AVCTP_CONNECTION_OPENED;
1196                             // if (connection->now_playing_info_response){
1197                             //     connection->now_playing_info_response = 0;
1198                             //     avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection);
1199                             //     break;
1200                             // }
1201                             avrcp_target_send_response(connection->l2cap_signaling_cid, connection);
1202                             break;
1203                         default:
1204                             return;
1205                     }
1206                     break;
1207             }
1208             default:
1209                 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_target_context);
1210                 break;
1211         }
1212         default:
1213             break;
1214     }
1215 }
1216 
1217 void avrcp_target_init(void){
1218     avrcp_target_context.role = AVRCP_TARGET;
1219     avrcp_target_context.connections = NULL;
1220     avrcp_target_context.packet_handler = avrcp_target_packet_handler;
1221     l2cap_register_service(&avrcp_target_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0);
1222 }
1223 
1224 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
1225     if (callback == NULL){
1226         log_error("avrcp_register_packet_handler called with NULL callback");
1227         return;
1228     }
1229     avrcp_target_context.avrcp_callback = callback;
1230 }
1231 
1232 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){
1233     return avrcp_connect(bd_addr, &avrcp_target_context, avrcp_cid);
1234 }
1235 
1236 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){
1237     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
1238     if (!connection){
1239         log_error("avrcp_get_capabilities: could not find a connection.");
1240         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1241     }
1242     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
1243     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
1244     return ERROR_CODE_SUCCESS;
1245 }
1246 
1247