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