1 /******************************************************************************
2 *
3 * Copyright 2003-2016 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * Interface to AVRCP mandatory commands
22 *
23 ******************************************************************************/
24 #include <base/logging.h>
25 #include <string.h>
26
27 #include "avrc_api.h"
28 #include "avrc_int.h"
29 #include "bt_common.h"
30 #include "btu.h"
31 #include "osi/include/fixed_queue.h"
32 #include "osi/include/log.h"
33 #include "osi/include/osi.h"
34 #include "osi/include/properties.h"
35
36 /*****************************************************************************
37 * Global data
38 ****************************************************************************/
39
40 #define AVRC_MAX_RCV_CTRL_EVT AVCT_BROWSE_UNCONG_IND_EVT
41
42 #ifndef MAX
43 #define MAX(a, b) ((a) > (b) ? (a) : (b))
44 #endif
45
46 static const uint8_t avrc_ctrl_event_map[] = {
47 AVRC_OPEN_IND_EVT, /* AVCT_CONNECT_CFM_EVT */
48 AVRC_OPEN_IND_EVT, /* AVCT_CONNECT_IND_EVT */
49 AVRC_CLOSE_IND_EVT, /* AVCT_DISCONNECT_CFM_EVT */
50 AVRC_CLOSE_IND_EVT, /* AVCT_DISCONNECT_IND_EVT */
51 AVRC_CONG_IND_EVT, /* AVCT_CONG_IND_EVT */
52 AVRC_UNCONG_IND_EVT, /* AVCT_UNCONG_IND_EVT */
53 AVRC_BROWSE_OPEN_IND_EVT, /* AVCT_BROWSE_CONN_CFM_EVT */
54 AVRC_BROWSE_OPEN_IND_EVT, /* AVCT_BROWSE_CONN_IND_EVT */
55 AVRC_BROWSE_CLOSE_IND_EVT, /* AVCT_BROWSE_DISCONN_CFM_EVT */
56 AVRC_BROWSE_CLOSE_IND_EVT, /* AVCT_BROWSE_DISCONN_IND_EVT */
57 AVRC_BROWSE_CONG_IND_EVT, /* AVCT_BROWSE_CONG_IND_EVT */
58 AVRC_BROWSE_UNCONG_IND_EVT /* AVCT_BROWSE_UNCONG_IND_EVT */
59 };
60
61 /* use this unused opcode to indication no need to call the callback function */
62 #define AVRC_OP_DROP 0xFE
63 /* use this unused opcode to indication no need to call the callback function &
64 * free buffer */
65 #define AVRC_OP_DROP_N_FREE 0xFD
66
67 #define AVRC_OP_UNIT_INFO_RSP_LEN 8
68 #define AVRC_OP_SUB_UNIT_INFO_RSP_LEN 8
69 #define AVRC_OP_REJ_MSG_LEN 11
70
71 /* Flags definitions for AVRC_MsgReq */
72 #define AVRC_MSG_MASK_IS_VENDOR_CMD 0x01
73 #define AVRC_MSG_MASK_IS_CONTINUATION_RSP 0x02
74
75 /******************************************************************************
76 *
77 * Function avrc_ctrl_cback
78 *
79 * Description This is the callback function used by AVCTP to report
80 * received link events.
81 *
82 * Returns Nothing.
83 *
84 *****************************************************************************/
avrc_ctrl_cback(uint8_t handle,uint8_t event,uint16_t result,const RawAddress * peer_addr)85 static void avrc_ctrl_cback(uint8_t handle, uint8_t event, uint16_t result,
86 const RawAddress* peer_addr) {
87 uint8_t avrc_event;
88
89 if (event <= AVRC_MAX_RCV_CTRL_EVT && avrc_cb.ccb[handle].ctrl_cback) {
90 avrc_event = avrc_ctrl_event_map[event];
91 if (event == AVCT_CONNECT_CFM_EVT) {
92 if (result != 0) /* failed */
93 avrc_event = AVRC_CLOSE_IND_EVT;
94 }
95 avrc_cb.ccb[handle].ctrl_cback.Run(handle, avrc_event, result, peer_addr);
96 }
97
98 if ((event == AVCT_DISCONNECT_CFM_EVT) ||
99 (event == AVCT_DISCONNECT_IND_EVT)) {
100 avrc_flush_cmd_q(handle);
101 alarm_free(avrc_cb.ccb_int[handle].tle);
102 avrc_cb.ccb_int[handle].tle = NULL;
103 }
104 }
105
106 /******************************************************************************
107 *
108 * Function avrc_flush_cmd_q
109 *
110 * Description Flush command queue for the specified avrc handle
111 *
112 * Returns Nothing.
113 *
114 *****************************************************************************/
avrc_flush_cmd_q(uint8_t handle)115 void avrc_flush_cmd_q(uint8_t handle) {
116 AVRC_TRACE_DEBUG("AVRC: Flushing command queue for handle=0x%02x", handle);
117 avrc_cb.ccb_int[handle].flags &= ~AVRC_CB_FLAGS_RSP_PENDING;
118
119 alarm_cancel(avrc_cb.ccb_int[handle].tle);
120 fixed_queue_free(avrc_cb.ccb_int[handle].cmd_q, osi_free);
121 avrc_cb.ccb_int[handle].cmd_q = NULL;
122 }
123
124 /******************************************************************************
125 *
126 * Function avrc_process_timeout
127 *
128 * Description Handle avrc command timeout
129 *
130 * Returns Nothing.
131 *
132 *****************************************************************************/
avrc_process_timeout(void * data)133 void avrc_process_timeout(void* data) {
134 tAVRC_PARAM* param = (tAVRC_PARAM*)data;
135
136 AVRC_TRACE_DEBUG("AVRC: command timeout (handle=0x%02x, label=0x%02x)",
137 param->handle, param->label);
138
139 /* Notify app */
140 if (avrc_cb.ccb[param->handle].ctrl_cback) {
141 avrc_cb.ccb[param->handle].ctrl_cback.Run(
142 param->handle, AVRC_CMD_TIMEOUT_EVT, param->label, NULL);
143 }
144
145 /* If vendor command timed-out, then send next command in the queue */
146 if (param->msg_mask & AVRC_MSG_MASK_IS_VENDOR_CMD) {
147 avrc_send_next_vendor_cmd(param->handle);
148 }
149 osi_free(param);
150 }
151
152 /******************************************************************************
153 *
154 * Function avrc_send_next_vendor_cmd
155 *
156 * Description Dequeue and send next vendor command for given handle
157 *
158 * Returns Nothing.
159 *
160 *****************************************************************************/
avrc_send_next_vendor_cmd(uint8_t handle)161 void avrc_send_next_vendor_cmd(uint8_t handle) {
162 BT_HDR* p_next_cmd;
163 uint8_t next_label;
164
165 while ((p_next_cmd = (BT_HDR*)fixed_queue_try_dequeue(
166 avrc_cb.ccb_int[handle].cmd_q)) != NULL) {
167 p_next_cmd->event &= 0xFF; /* opcode */
168 next_label = (p_next_cmd->layer_specific) >> 8; /* extract label */
169 p_next_cmd->layer_specific &= 0xFF; /* AVCT_DATA_CTRL or AVCT_DATA_BROWSE */
170
171 AVRC_TRACE_DEBUG(
172 "AVRC: Dequeuing command 0x%08x (handle=0x%02x, label=0x%02x)",
173 p_next_cmd, handle, next_label);
174
175 /* Send the message */
176 if ((AVCT_MsgReq(handle, next_label, AVCT_CMD, p_next_cmd)) ==
177 AVCT_SUCCESS) {
178 /* Start command timer to wait for response */
179 avrc_start_cmd_timer(handle, next_label, AVRC_MSG_MASK_IS_VENDOR_CMD);
180 return;
181 }
182 }
183
184 if (p_next_cmd == NULL) {
185 /* cmd queue empty */
186 avrc_cb.ccb_int[handle].flags &= ~AVRC_CB_FLAGS_RSP_PENDING;
187 }
188 }
189
190 /******************************************************************************
191 *
192 * Function avrc_start_cmd_timer
193 *
194 * Description Start timer for waiting for responses
195 *
196 * Returns Nothing.
197 *
198 *****************************************************************************/
avrc_start_cmd_timer(uint8_t handle,uint8_t label,uint8_t msg_mask)199 void avrc_start_cmd_timer(uint8_t handle, uint8_t label, uint8_t msg_mask) {
200 tAVRC_PARAM* param =
201 static_cast<tAVRC_PARAM*>(osi_malloc(sizeof(tAVRC_PARAM)));
202 param->handle = handle;
203 param->label = label;
204 param->msg_mask = msg_mask;
205
206 AVRC_TRACE_DEBUG("AVRC: starting timer (handle=0x%02x, label=0x%02x)", handle,
207 label);
208
209 alarm_set_on_mloop(avrc_cb.ccb_int[handle].tle, AVRC_CMD_TOUT_MS,
210 avrc_process_timeout, param);
211 }
212
213 /******************************************************************************
214 *
215 * Function avrc_get_data_ptr
216 *
217 * Description Gets a pointer to the data payload in the packet.
218 *
219 * Returns A pointer to the data payload.
220 *
221 *****************************************************************************/
avrc_get_data_ptr(BT_HDR * p_pkt)222 static uint8_t* avrc_get_data_ptr(BT_HDR* p_pkt) {
223 return (uint8_t*)(p_pkt + 1) + p_pkt->offset;
224 }
225
226 /******************************************************************************
227 *
228 * Function avrc_copy_packet
229 *
230 * Description Copies an AVRC packet to a new buffer. In the new buffer,
231 * the payload offset is at least AVCT_MSG_OFFSET octets.
232 *
233 * Returns The buffer with the copied data.
234 *
235 *****************************************************************************/
avrc_copy_packet(BT_HDR * p_pkt,int rsp_pkt_len)236 static BT_HDR* avrc_copy_packet(BT_HDR* p_pkt, int rsp_pkt_len) {
237 const int offset = MAX(AVCT_MSG_OFFSET, p_pkt->offset);
238 const int pkt_len = MAX(rsp_pkt_len, p_pkt->len);
239 BT_HDR* p_pkt_copy = (BT_HDR*)osi_calloc(BT_HDR_SIZE + offset + pkt_len);
240
241 /* Copy the packet header, set the new offset, and copy the payload */
242 memcpy(p_pkt_copy, p_pkt, BT_HDR_SIZE);
243 p_pkt_copy->offset = offset;
244 uint8_t* p_data = avrc_get_data_ptr(p_pkt);
245 uint8_t* p_data_copy = avrc_get_data_ptr(p_pkt_copy);
246 memcpy(p_data_copy, p_data, p_pkt->len);
247
248 return p_pkt_copy;
249 }
250
251 /******************************************************************************
252 *
253 * Function avrc_prep_end_frag
254 *
255 * Description This function prepares an end response fragment
256 *
257 * Returns Nothing.
258 *
259 *****************************************************************************/
avrc_prep_end_frag(uint8_t handle)260 static void avrc_prep_end_frag(uint8_t handle) {
261 tAVRC_FRAG_CB* p_fcb;
262 BT_HDR* p_pkt_new;
263 uint8_t *p_data, *p_orig_data;
264 uint8_t rsp_type;
265
266 AVRC_TRACE_DEBUG("%s", __func__);
267 p_fcb = &avrc_cb.fcb[handle];
268
269 /* The response type of the end fragment should be the same as the the PDU of
270 * "End Fragment Response" Errata:
271 * https://www.bluetooth.org/errata/errata_view.cfm?errata_id=4383
272 */
273 p_orig_data = ((uint8_t*)(p_fcb->p_fmsg + 1) + p_fcb->p_fmsg->offset);
274 rsp_type = ((*p_orig_data) & AVRC_CTYPE_MASK);
275
276 p_pkt_new = p_fcb->p_fmsg;
277 p_pkt_new->len -=
278 (AVRC_MAX_CTRL_DATA_LEN - AVRC_VENDOR_HDR_SIZE - AVRC_MIN_META_HDR_SIZE);
279 p_pkt_new->offset +=
280 (AVRC_MAX_CTRL_DATA_LEN - AVRC_VENDOR_HDR_SIZE - AVRC_MIN_META_HDR_SIZE);
281 p_data = (uint8_t*)(p_pkt_new + 1) + p_pkt_new->offset;
282 *p_data++ = rsp_type;
283 *p_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
284 *p_data++ = AVRC_OP_VENDOR;
285 AVRC_CO_ID_TO_BE_STREAM(p_data, AVRC_CO_METADATA);
286 *p_data++ = p_fcb->frag_pdu;
287 *p_data++ = AVRC_PKT_END;
288
289 /* 4=pdu, pkt_type & len */
290 UINT16_TO_BE_STREAM(
291 p_data, (p_pkt_new->len - AVRC_VENDOR_HDR_SIZE - AVRC_MIN_META_HDR_SIZE));
292 }
293
294 /******************************************************************************
295 *
296 * Function avrc_send_continue_frag
297 *
298 * Description This function sends a continue response fragment
299 *
300 * Returns AVRC_SUCCESS if successful.
301 * AVRC_BAD_HANDLE if handle is invalid.
302 *
303 *****************************************************************************/
avrc_send_continue_frag(uint8_t handle,uint8_t label)304 static uint16_t avrc_send_continue_frag(uint8_t handle, uint8_t label) {
305 tAVRC_FRAG_CB* p_fcb;
306 BT_HDR *p_pkt_old, *p_pkt;
307 uint8_t *p_old, *p_data;
308 uint8_t cr = AVCT_RSP;
309
310 p_fcb = &avrc_cb.fcb[handle];
311 p_pkt = p_fcb->p_fmsg;
312
313 AVRC_TRACE_DEBUG("%s handle = %u label = %u len = %d", __func__, handle,
314 label, p_pkt->len);
315 if (p_pkt->len > AVRC_MAX_CTRL_DATA_LEN) {
316 int offset_len = MAX(AVCT_MSG_OFFSET, p_pkt->offset);
317 p_pkt_old = p_fcb->p_fmsg;
318 p_pkt = (BT_HDR*)osi_calloc(AVRC_PACKET_LEN + offset_len + BT_HDR_SIZE);
319 p_pkt->len = AVRC_MAX_CTRL_DATA_LEN;
320 p_pkt->offset = AVCT_MSG_OFFSET;
321 p_pkt->layer_specific = p_pkt_old->layer_specific;
322 p_pkt->event = p_pkt_old->event;
323 p_old = (uint8_t*)(p_pkt_old + 1) + p_pkt_old->offset;
324 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
325 memcpy(p_data, p_old, AVRC_MAX_CTRL_DATA_LEN);
326 /* use AVRC continue packet type */
327 p_data += AVRC_VENDOR_HDR_SIZE;
328 p_data++; /* pdu */
329 *p_data++ = AVRC_PKT_CONTINUE;
330 /* 4=pdu, pkt_type & len */
331 UINT16_TO_BE_STREAM(p_data,
332 (AVRC_MAX_CTRL_DATA_LEN - AVRC_VENDOR_HDR_SIZE - 4));
333
334 /* prepare the left over for as an end fragment */
335 avrc_prep_end_frag(handle);
336 } else {
337 /* end fragment. clean the control block */
338 p_fcb->frag_enabled = false;
339 p_fcb->p_fmsg = NULL;
340 }
341 return AVCT_MsgReq(handle, label, cr, p_pkt);
342 }
343
344 /******************************************************************************
345 *
346 * Function avrc_proc_vendor_command
347 *
348 * Description This function processes received vendor command.
349 *
350 * Returns if not NULL, the response to send right away.
351 *
352 *****************************************************************************/
avrc_proc_vendor_command(uint8_t handle,uint8_t label,BT_HDR * p_pkt,tAVRC_MSG_VENDOR * p_msg)353 static BT_HDR* avrc_proc_vendor_command(uint8_t handle, uint8_t label,
354 BT_HDR* p_pkt,
355 tAVRC_MSG_VENDOR* p_msg) {
356 BT_HDR* p_rsp = NULL;
357 uint8_t* p_data;
358 uint8_t* p_begin;
359 uint8_t pkt_type;
360 bool abort_frag = false;
361 tAVRC_STS status = AVRC_STS_NO_ERROR;
362 tAVRC_FRAG_CB* p_fcb;
363
364 p_begin = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
365 p_data = p_begin + AVRC_VENDOR_HDR_SIZE;
366 pkt_type = *(p_data + 1) & AVRC_PKT_TYPE_MASK;
367
368 if (pkt_type != AVRC_PKT_SINGLE) {
369 /* reject - commands can only be in single packets at AVRCP level */
370 AVRC_TRACE_ERROR("commands must be in single packet pdu:0x%x", *p_data);
371 /* use the current GKI buffer to send the reject */
372 status = AVRC_STS_BAD_CMD;
373 }
374 /* check if there are fragments waiting to be sent */
375 else if (avrc_cb.fcb[handle].frag_enabled) {
376 p_fcb = &avrc_cb.fcb[handle];
377 if (p_msg->company_id == AVRC_CO_METADATA) {
378 switch (*p_data) {
379 case AVRC_PDU_ABORT_CONTINUATION_RSP:
380 /* aborted by CT - send accept response */
381 abort_frag = true;
382 p_begin = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
383 *p_begin = (AVRC_RSP_ACCEPT & AVRC_CTYPE_MASK);
384 if (*(p_data + 4) != p_fcb->frag_pdu) {
385 *p_begin = (AVRC_RSP_REJ & AVRC_CTYPE_MASK);
386 *(p_data + 4) = AVRC_STS_BAD_PARAM;
387 } else {
388 p_data = (p_begin + AVRC_VENDOR_HDR_SIZE + 2);
389 UINT16_TO_BE_STREAM(p_data, 0);
390 p_pkt->len = (p_data - p_begin);
391 }
392 AVCT_MsgReq(handle, label, AVCT_RSP, p_pkt);
393 p_msg->hdr.opcode =
394 AVRC_OP_DROP; /* used the p_pkt to send response */
395 break;
396
397 case AVRC_PDU_REQUEST_CONTINUATION_RSP:
398 if (*(p_data + 4) == p_fcb->frag_pdu) {
399 avrc_send_continue_frag(handle, label);
400 p_msg->hdr.opcode = AVRC_OP_DROP_N_FREE;
401 } else {
402 /* the pdu id does not match - reject the command using the current
403 * GKI buffer */
404 AVRC_TRACE_ERROR(
405 "%s continue pdu: 0x%x does not match the current pdu: 0x%x",
406 __func__, *(p_data + 4), p_fcb->frag_pdu);
407 status = AVRC_STS_BAD_PARAM;
408 abort_frag = true;
409 }
410 break;
411
412 default:
413 /* implicit abort */
414 abort_frag = true;
415 }
416 } else {
417 abort_frag = true;
418 /* implicit abort */
419 }
420
421 if (abort_frag) {
422 osi_free_and_reset((void**)&p_fcb->p_fmsg);
423 p_fcb->frag_enabled = false;
424 }
425 }
426
427 if (status != AVRC_STS_NO_ERROR) {
428 p_rsp = (BT_HDR*)osi_calloc(BT_DEFAULT_BUFFER_SIZE);
429 p_rsp->offset = p_pkt->offset;
430 p_data = (uint8_t*)(p_rsp + 1) + p_pkt->offset;
431 *p_data++ = AVRC_RSP_REJ;
432 p_data += AVRC_VENDOR_HDR_SIZE; /* pdu */
433 *p_data++ = 0; /* pkt_type */
434 UINT16_TO_BE_STREAM(p_data, 1); /* len */
435 *p_data++ = status; /* error code */
436 p_rsp->len = AVRC_VENDOR_HDR_SIZE + 5;
437 }
438
439 return p_rsp;
440 }
441
442 /******************************************************************************
443 *
444 * Function avrc_proc_far_msg
445 *
446 * Description This function processes metadata fragmenation
447 * and reassembly
448 *
449 * Returns 0, to report the message with msg_cback .
450 *
451 *****************************************************************************/
avrc_proc_far_msg(uint8_t handle,uint8_t label,uint8_t cr,BT_HDR ** pp_pkt,tAVRC_MSG_VENDOR * p_msg)452 static uint8_t avrc_proc_far_msg(uint8_t handle, uint8_t label, uint8_t cr,
453 BT_HDR** pp_pkt, tAVRC_MSG_VENDOR* p_msg) {
454 BT_HDR* p_pkt = *pp_pkt;
455 uint8_t* p_data;
456 uint8_t drop_code = 0;
457 bool buf_overflow = false;
458 BT_HDR* p_rsp = NULL;
459 BT_HDR* p_cmd = NULL;
460 bool req_continue = false;
461 BT_HDR* p_pkt_new = NULL;
462 uint8_t pkt_type;
463 tAVRC_RASM_CB* p_rcb;
464 tAVRC_NEXT_CMD avrc_cmd;
465 tAVRC_STS status;
466
467 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
468
469 /* Skip over vendor header (ctype, subunit*, opcode, CO_ID) */
470 p_data += AVRC_VENDOR_HDR_SIZE;
471
472 pkt_type = *(p_data + 1) & AVRC_PKT_TYPE_MASK;
473 AVRC_TRACE_DEBUG("pkt_type %d", pkt_type);
474 p_rcb = &avrc_cb.rcb[handle];
475
476 /* check if the message needs to be re-assembled */
477 if (pkt_type == AVRC_PKT_SINGLE || pkt_type == AVRC_PKT_START) {
478 /* previous fragments need to be dropped, when received another new message
479 */
480 p_rcb->rasm_offset = 0;
481 osi_free_and_reset((void**)&p_rcb->p_rmsg);
482 }
483
484 if (pkt_type != AVRC_PKT_SINGLE && cr == AVCT_RSP) {
485 /* not a single response packet - need to re-assemble metadata messages */
486 if (pkt_type == AVRC_PKT_START) {
487 /* Allocate buffer for re-assembly */
488 p_rcb->rasm_pdu = *p_data;
489 p_rcb->p_rmsg = (BT_HDR*)osi_calloc(BT_DEFAULT_BUFFER_SIZE);
490 /* Copy START packet to buffer for re-assembling fragments */
491 memcpy(p_rcb->p_rmsg, p_pkt, sizeof(BT_HDR)); /* Copy bt hdr */
492
493 /* Copy metadata message */
494 memcpy((uint8_t*)(p_rcb->p_rmsg + 1),
495 (uint8_t*)(p_pkt + 1) + p_pkt->offset, p_pkt->len);
496
497 /* offset of start of metadata response in reassembly buffer */
498 p_rcb->p_rmsg->offset = p_rcb->rasm_offset = 0;
499
500 /*
501 * Free original START packet, replace with pointer to
502 * reassembly buffer.
503 */
504 osi_free(p_pkt);
505 *pp_pkt = p_rcb->p_rmsg;
506
507 /*
508 * Set offset to point to where to copy next - use the same
509 * reassembly logic as AVCT.
510 */
511 p_rcb->p_rmsg->offset += p_rcb->p_rmsg->len;
512 req_continue = true;
513 } else if (p_rcb->p_rmsg == NULL) {
514 /* Received a CONTINUE/END, but no corresponding START
515 (or previous fragmented response was dropped) */
516 AVRC_TRACE_DEBUG(
517 "Received a CONTINUE/END without no corresponding START \
518 (or previous fragmented response was dropped)");
519 drop_code = 5;
520 osi_free(p_pkt);
521 *pp_pkt = NULL;
522 } else {
523 /* get size of buffer holding assembled message */
524 /*
525 * NOTE: The buffer is allocated above at the beginning of the
526 * reassembly, and is always of size BT_DEFAULT_BUFFER_SIZE.
527 */
528 uint16_t buf_len = BT_DEFAULT_BUFFER_SIZE - sizeof(BT_HDR);
529 /* adjust offset and len of fragment for header byte */
530 p_pkt->offset += (AVRC_VENDOR_HDR_SIZE + AVRC_MIN_META_HDR_SIZE);
531 p_pkt->len -= (AVRC_VENDOR_HDR_SIZE + AVRC_MIN_META_HDR_SIZE);
532 /* verify length */
533 if ((p_rcb->p_rmsg->offset + p_pkt->len) > buf_len) {
534 AVRC_TRACE_WARNING(
535 "Fragmented message too big! - report the partial message");
536 p_pkt->len = buf_len - p_rcb->p_rmsg->offset;
537 pkt_type = AVRC_PKT_END;
538 buf_overflow = true;
539 }
540
541 /* copy contents of p_pkt to p_rx_msg */
542 memcpy((uint8_t*)(p_rcb->p_rmsg + 1) + p_rcb->p_rmsg->offset,
543 (uint8_t*)(p_pkt + 1) + p_pkt->offset, p_pkt->len);
544
545 if (pkt_type == AVRC_PKT_END) {
546 p_rcb->p_rmsg->offset = p_rcb->rasm_offset;
547 p_rcb->p_rmsg->len += p_pkt->len;
548 p_pkt_new = p_rcb->p_rmsg;
549 p_rcb->rasm_offset = 0;
550 p_rcb->p_rmsg = NULL;
551 p_msg->p_vendor_data = (uint8_t*)(p_pkt_new + 1) + p_pkt_new->offset;
552 p_msg->hdr.ctype = p_msg->p_vendor_data[0] & AVRC_CTYPE_MASK;
553 /* 6 = ctype, subunit*, opcode & CO_ID */
554 p_msg->p_vendor_data += AVRC_VENDOR_HDR_SIZE;
555 p_msg->vendor_len = p_pkt_new->len - AVRC_VENDOR_HDR_SIZE;
556 p_data = p_msg->p_vendor_data + 1; /* skip pdu */
557 *p_data++ = AVRC_PKT_SINGLE;
558 UINT16_TO_BE_STREAM(p_data,
559 (p_msg->vendor_len - AVRC_MIN_META_HDR_SIZE));
560 AVRC_TRACE_DEBUG("end frag:%d, total len:%d, offset:%d", p_pkt->len,
561 p_pkt_new->len, p_pkt_new->offset);
562 } else {
563 p_rcb->p_rmsg->offset += p_pkt->len;
564 p_rcb->p_rmsg->len += p_pkt->len;
565 p_pkt_new = NULL;
566 req_continue = true;
567 }
568 osi_free(p_pkt);
569 *pp_pkt = p_pkt_new;
570 }
571 }
572
573 if (cr == AVCT_CMD) {
574 p_rsp = avrc_proc_vendor_command(handle, label, *pp_pkt, p_msg);
575 if (p_rsp) {
576 AVCT_MsgReq(handle, label, AVCT_RSP, p_rsp);
577 osi_free_and_reset((void**)pp_pkt);
578 drop_code = 3;
579 } else if (p_msg->hdr.opcode == AVRC_OP_DROP) {
580 drop_code = 1;
581 } else if (p_msg->hdr.opcode == AVRC_OP_DROP_N_FREE)
582 drop_code = 4;
583
584 } else if (cr == AVCT_RSP) {
585 if (req_continue) {
586 avrc_cmd.pdu = AVRC_PDU_REQUEST_CONTINUATION_RSP;
587 drop_code = 2;
588 } else if (buf_overflow) {
589 /* Incoming message too big to fit in BT_DEFAULT_BUFFER_SIZE. Send abort
590 * to peer */
591 avrc_cmd.pdu = AVRC_PDU_ABORT_CONTINUATION_RSP;
592 drop_code = 4;
593 } else {
594 return drop_code;
595 }
596 avrc_cmd.status = AVRC_STS_NO_ERROR;
597 avrc_cmd.target_pdu = p_rcb->rasm_pdu;
598
599 tAVRC_COMMAND avrc_command;
600 avrc_command.continu = avrc_cmd;
601 status = AVRC_BldCommand(&avrc_command, &p_cmd);
602 if (status == AVRC_STS_NO_ERROR) {
603 AVRC_MsgReq(handle, (uint8_t)(label), AVRC_CMD_CTRL, p_cmd);
604 }
605 }
606
607 return drop_code;
608 }
609
610 /******************************************************************************
611 *
612 * Function avrc_msg_cback
613 *
614 * Description This is the callback function used by AVCTP to report
615 * received AV control messages.
616 *
617 * Returns Nothing.
618 *
619 *****************************************************************************/
avrc_msg_cback(uint8_t handle,uint8_t label,uint8_t cr,BT_HDR * p_pkt)620 static void avrc_msg_cback(uint8_t handle, uint8_t label, uint8_t cr,
621 BT_HDR* p_pkt) {
622 uint8_t opcode;
623 tAVRC_MSG msg;
624 uint8_t* p_data;
625 uint8_t* p_begin;
626 bool drop = false;
627 bool do_free = true;
628 BT_HDR* p_rsp = NULL;
629 uint8_t* p_rsp_data;
630 int xx;
631 bool reject = false;
632 const char* p_drop_msg = "dropped";
633 tAVRC_MSG_VENDOR* p_msg = &msg.vendor;
634
635 if (cr == AVCT_CMD && (p_pkt->layer_specific & AVCT_DATA_CTRL &&
636 p_pkt->len > AVRC_PACKET_LEN)) {
637 android_errorWriteLog(0x534e4554, "177611958");
638 AVRC_TRACE_WARNING("%s: Command length %d too long: must be at most %d",
639 __func__, p_pkt->len, AVRC_PACKET_LEN);
640 osi_free(p_pkt);
641 return;
642 }
643
644 if (cr == AVCT_REJ) {
645 /* The peer thinks that this PID is no longer open - remove this handle */
646 /* */
647 osi_free(p_pkt);
648 AVCT_RemoveConn(handle);
649 return;
650 } else if (cr == AVCT_RSP) {
651 /* Received response. Stop command timeout timer */
652 AVRC_TRACE_DEBUG("AVRC: stopping timer (handle=0x%02x)", handle);
653 alarm_cancel(avrc_cb.ccb_int[handle].tle);
654 }
655
656 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
657 memset(&msg, 0, sizeof(tAVRC_MSG));
658
659 if (p_pkt->layer_specific == AVCT_DATA_BROWSE) {
660 opcode = AVRC_OP_BROWSE;
661 msg.browse.hdr.ctype = cr;
662 msg.browse.p_browse_data = p_data;
663 msg.browse.browse_len = p_pkt->len;
664 msg.browse.p_browse_pkt = p_pkt;
665 } else {
666 if (p_pkt->len < AVRC_AVC_HDR_SIZE) {
667 android_errorWriteLog(0x534e4554, "111803925");
668 AVRC_TRACE_WARNING("%s: message length %d too short: must be at least %d",
669 __func__, p_pkt->len, AVRC_AVC_HDR_SIZE);
670 osi_free(p_pkt);
671 return;
672 }
673 msg.hdr.ctype = p_data[0] & AVRC_CTYPE_MASK;
674 AVRC_TRACE_DEBUG("%s handle:%d, ctype:%d, offset:%d, len: %d", __func__,
675 handle, msg.hdr.ctype, p_pkt->offset, p_pkt->len);
676 msg.hdr.subunit_type =
677 (p_data[1] & AVRC_SUBTYPE_MASK) >> AVRC_SUBTYPE_SHIFT;
678 msg.hdr.subunit_id = p_data[1] & AVRC_SUBID_MASK;
679 opcode = p_data[2];
680 }
681
682 if (((avrc_cb.ccb[handle].control & AVRC_CT_TARGET) && (cr == AVCT_CMD)) ||
683 ((avrc_cb.ccb[handle].control & AVRC_CT_CONTROL) && (cr == AVCT_RSP))) {
684 switch (opcode) {
685 case AVRC_OP_UNIT_INFO:
686 if (cr == AVCT_CMD) {
687 /* send the response to the peer */
688 p_rsp = avrc_copy_packet(p_pkt, AVRC_OP_UNIT_INFO_RSP_LEN);
689 p_rsp_data = avrc_get_data_ptr(p_rsp);
690 *p_rsp_data = AVRC_RSP_IMPL_STBL;
691 /* check & set the offset. set response code, set subunit_type &
692 subunit_id,
693 set AVRC_OP_UNIT_INFO */
694 /* 3 bytes: ctype, subunit*, opcode */
695 p_rsp_data += AVRC_AVC_HDR_SIZE;
696 *p_rsp_data++ = 7;
697 /* Panel subunit & id=0 */
698 *p_rsp_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
699 AVRC_CO_ID_TO_BE_STREAM(p_rsp_data, avrc_cb.ccb[handle].company_id);
700 p_rsp->len =
701 (uint16_t)(p_rsp_data - (uint8_t*)(p_rsp + 1) - p_rsp->offset);
702 cr = AVCT_RSP;
703 p_drop_msg = "auto respond";
704 } else {
705 /* parse response */
706 if (p_pkt->len < AVRC_OP_UNIT_INFO_RSP_LEN) {
707 AVRC_TRACE_WARNING(
708 "%s: message length %d too short: must be at least %d",
709 __func__, p_pkt->len, AVRC_OP_UNIT_INFO_RSP_LEN);
710 android_errorWriteLog(0x534e4554, "79883824");
711 drop = true;
712 p_drop_msg = "UNIT_INFO_RSP too short";
713 break;
714 }
715 p_data += 4; /* 3 bytes: ctype, subunit*, opcode + octet 3 (is 7)*/
716 msg.unit.unit_type =
717 (*p_data & AVRC_SUBTYPE_MASK) >> AVRC_SUBTYPE_SHIFT;
718 msg.unit.unit = *p_data & AVRC_SUBID_MASK;
719 p_data++;
720 AVRC_BE_STREAM_TO_CO_ID(msg.unit.company_id, p_data);
721 }
722 break;
723
724 case AVRC_OP_SUB_INFO:
725 if (cr == AVCT_CMD) {
726 /* send the response to the peer */
727 p_rsp = avrc_copy_packet(p_pkt, AVRC_OP_SUB_UNIT_INFO_RSP_LEN);
728 p_rsp_data = avrc_get_data_ptr(p_rsp);
729 *p_rsp_data = AVRC_RSP_IMPL_STBL;
730 /* check & set the offset. set response code, set (subunit_type &
731 subunit_id),
732 set AVRC_OP_SUB_INFO, set (page & extention code) */
733 p_rsp_data += 4;
734 /* Panel subunit & id=0 */
735 *p_rsp_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
736 memset(p_rsp_data, AVRC_CMD_OPRND_PAD, AVRC_SUBRSP_OPRND_BYTES);
737 p_rsp_data += AVRC_SUBRSP_OPRND_BYTES;
738 p_rsp->len =
739 (uint16_t)(p_rsp_data - (uint8_t*)(p_rsp + 1) - p_rsp->offset);
740 cr = AVCT_RSP;
741 p_drop_msg = "auto responded";
742 } else {
743 /* parse response */
744 if (p_pkt->len < AVRC_OP_SUB_UNIT_INFO_RSP_LEN) {
745 AVRC_TRACE_WARNING(
746 "%s: message length %d too short: must be at least %d",
747 __func__, p_pkt->len, AVRC_OP_SUB_UNIT_INFO_RSP_LEN);
748 android_errorWriteLog(0x534e4554, "79883824");
749 drop = true;
750 p_drop_msg = "SUB_UNIT_INFO_RSP too short";
751 break;
752 }
753 p_data += AVRC_AVC_HDR_SIZE; /* 3 bytes: ctype, subunit*, opcode */
754 msg.sub.page =
755 (*p_data++ >> AVRC_SUB_PAGE_SHIFT) & AVRC_SUB_PAGE_MASK;
756 xx = 0;
757 while (*p_data != AVRC_CMD_OPRND_PAD && xx < AVRC_SUB_TYPE_LEN) {
758 msg.sub.subunit_type[xx] = *p_data++ >> AVRC_SUBTYPE_SHIFT;
759 if (msg.sub.subunit_type[xx] == AVRC_SUB_PANEL)
760 msg.sub.panel = true;
761 xx++;
762 }
763 }
764 break;
765
766 case AVRC_OP_VENDOR: {
767 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
768 p_begin = p_data;
769 if (p_pkt->len <
770 AVRC_VENDOR_HDR_SIZE) /* 6 = ctype, subunit*, opcode & CO_ID */
771 {
772 if (cr == AVCT_CMD)
773 reject = true;
774 else
775 drop = true;
776 break;
777 }
778 p_data += AVRC_AVC_HDR_SIZE; /* skip the first 3 bytes: ctype, subunit*,
779 opcode */
780 AVRC_BE_STREAM_TO_CO_ID(p_msg->company_id, p_data);
781 p_msg->p_vendor_data = p_data;
782 p_msg->vendor_len = p_pkt->len - (p_data - p_begin);
783
784 uint8_t drop_code = 0;
785 if (p_msg->company_id == AVRC_CO_METADATA) {
786 /* Validate length for metadata message */
787 if (p_pkt->len < (AVRC_VENDOR_HDR_SIZE + AVRC_MIN_META_HDR_SIZE)) {
788 if (cr == AVCT_CMD)
789 reject = true;
790 else
791 drop = true;
792 break;
793 }
794
795 /* Check+handle fragmented messages */
796 drop_code = avrc_proc_far_msg(handle, label, cr, &p_pkt, p_msg);
797 if (drop_code > 0) drop = true;
798 }
799 if (drop_code > 0) {
800 if (drop_code != 4) do_free = false;
801 switch (drop_code) {
802 case 1:
803 p_drop_msg = "sent_frag";
804 break;
805 case 2:
806 p_drop_msg = "req_cont";
807 break;
808 case 3:
809 p_drop_msg = "sent_frag3";
810 break;
811 case 4:
812 p_drop_msg = "sent_frag_free";
813 break;
814 default:
815 p_drop_msg = "sent_fragd";
816 }
817 }
818 /* If vendor response received, and did not ask for continuation */
819 /* then check queue for addition commands to send */
820 if ((cr == AVCT_RSP) && (drop_code != 2)) {
821 avrc_send_next_vendor_cmd(handle);
822 }
823 } break;
824
825 case AVRC_OP_PASS_THRU:
826 if (p_pkt->len < 5) /* 3 bytes: ctype, subunit*, opcode & op_id & len */
827 {
828 if (cr == AVCT_CMD)
829 reject = true;
830 else
831 drop = true;
832 break;
833 }
834 p_data += AVRC_AVC_HDR_SIZE; /* skip the first 3 bytes: ctype, subunit*,
835 opcode */
836 msg.pass.op_id = (AVRC_PASS_OP_ID_MASK & *p_data);
837 if (AVRC_PASS_STATE_MASK & *p_data)
838 msg.pass.state = true;
839 else
840 msg.pass.state = false;
841 p_data++;
842 msg.pass.pass_len = *p_data++;
843 if (msg.pass.pass_len != p_pkt->len - 5)
844 msg.pass.pass_len = p_pkt->len - 5;
845 if (msg.pass.pass_len)
846 msg.pass.p_pass_data = p_data;
847 else
848 msg.pass.p_pass_data = NULL;
849 break;
850
851 case AVRC_OP_BROWSE:
852 /* If browse response received, then check queue for addition commands
853 * to send */
854 if (cr == AVCT_RSP) {
855 avrc_send_next_vendor_cmd(handle);
856 }
857 break;
858
859 default:
860 if ((avrc_cb.ccb[handle].control & AVRC_CT_TARGET) &&
861 (cr == AVCT_CMD)) {
862 /* reject unsupported opcode */
863 reject = true;
864 }
865 drop = true;
866 break;
867 }
868 } else /* drop the event */
869 {
870 if (opcode != AVRC_OP_BROWSE) drop = true;
871 }
872
873 if (reject) {
874 /* reject unsupported opcode */
875 p_rsp = avrc_copy_packet(p_pkt, AVRC_OP_REJ_MSG_LEN);
876 p_rsp_data = avrc_get_data_ptr(p_rsp);
877 *p_rsp_data = AVRC_RSP_REJ;
878 p_drop_msg = "rejected";
879 cr = AVCT_RSP;
880 drop = true;
881 }
882
883 if (p_rsp) {
884 /* set to send response right away */
885 AVCT_MsgReq(handle, label, cr, p_rsp);
886 drop = true;
887 }
888
889 if (!drop) {
890 msg.hdr.opcode = opcode;
891 avrc_cb.ccb[handle].msg_cback.Run(handle, label, opcode, &msg);
892 } else {
893 AVRC_TRACE_WARNING("%s %s msg handle:%d, control:%d, cr:%d, opcode:x%x",
894 __func__, p_drop_msg, handle,
895 avrc_cb.ccb[handle].control, cr, opcode);
896 }
897
898 if (opcode == AVRC_OP_BROWSE && msg.browse.p_browse_pkt == NULL) {
899 do_free = false;
900 }
901
902 if (do_free) osi_free(p_pkt);
903 }
904
905 /******************************************************************************
906 *
907 * Function avrc_pass_msg
908 *
909 * Description Compose a PASS THROUGH command according to p_msg
910 *
911 * Input Parameters:
912 * p_msg: Pointer to PASS THROUGH message structure.
913 *
914 * Output Parameters:
915 * None.
916 *
917 * Returns pointer to a valid GKI buffer if successful.
918 * NULL if p_msg is NULL.
919 *
920 *****************************************************************************/
avrc_pass_msg(tAVRC_MSG_PASS * p_msg)921 static BT_HDR* avrc_pass_msg(tAVRC_MSG_PASS* p_msg) {
922 CHECK(p_msg != NULL);
923 CHECK(AVRC_CMD_BUF_SIZE > (AVRC_MIN_CMD_LEN + p_msg->pass_len));
924
925 BT_HDR* p_cmd = (BT_HDR*)osi_calloc(AVRC_CMD_BUF_SIZE);
926 p_cmd->offset = AVCT_MSG_OFFSET;
927 p_cmd->layer_specific = AVCT_DATA_CTRL;
928
929 uint8_t* p_data = (uint8_t*)(p_cmd + 1) + p_cmd->offset;
930 *p_data++ = (p_msg->hdr.ctype & AVRC_CTYPE_MASK);
931 *p_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT); /* Panel subunit & id=0 */
932 *p_data++ = AVRC_OP_PASS_THRU;
933 *p_data = (AVRC_PASS_OP_ID_MASK & p_msg->op_id);
934 if (p_msg->state) *p_data |= AVRC_PASS_STATE_MASK;
935 p_data++;
936
937 if (p_msg->op_id == AVRC_ID_VENDOR) {
938 *p_data++ = p_msg->pass_len;
939 if (p_msg->pass_len && p_msg->p_pass_data) {
940 memcpy(p_data, p_msg->p_pass_data, p_msg->pass_len);
941 p_data += p_msg->pass_len;
942 }
943 } else {
944 /* set msg len to 0 for other op_id */
945 *p_data++ = 0;
946 }
947 p_cmd->len = (uint16_t)(p_data - (uint8_t*)(p_cmd + 1) - p_cmd->offset);
948
949 return p_cmd;
950 }
951
952 /******************************************************************************
953 *
954 * Function ARVC_GetProfileVersion
955 *
956 * Description Get the user assigned AVRCP profile version
957 *
958 * Returns The AVRCP profile version
959 *
960 *****************************************************************************/
AVRC_GetProfileVersion()961 uint16_t AVRC_GetProfileVersion() {
962 uint16_t profile_version = AVRC_REV_1_4;
963 char avrcp_version[PROPERTY_VALUE_MAX] = {0};
964 osi_property_get(AVRC_VERSION_PROPERTY, avrcp_version, AVRC_DEFAULT_VERSION);
965
966 if (!strncmp(AVRC_1_6_STRING, avrcp_version, sizeof(AVRC_1_6_STRING))) {
967 profile_version = AVRC_REV_1_6;
968 } else if (!strncmp(AVRC_1_5_STRING, avrcp_version,
969 sizeof(AVRC_1_5_STRING))) {
970 profile_version = AVRC_REV_1_5;
971 } else if (!strncmp(AVRC_1_3_STRING, avrcp_version,
972 sizeof(AVRC_1_3_STRING))) {
973 profile_version = AVRC_REV_1_3;
974 }
975
976 return profile_version;
977 }
978
979 /******************************************************************************
980 *
981 * Function AVRC_Open
982 *
983 * Description This function is called to open a connection to AVCTP.
984 * The connection can be either an initiator or acceptor, as
985 * determined by the p_ccb->stream parameter.
986 * The connection can be a target, a controller or for both
987 * role, as determined by the p_ccb->control parameter.
988 * By definition, a target connection is an acceptor connection
989 * that waits for an incoming AVCTP connection from the peer.
990 * The connection remains available to the application until
991 * the application closes it by calling AVRC_Close(). The
992 * application does not need to reopen the connection after an
993 * AVRC_CLOSE_IND_EVT is received.
994 *
995 * Input Parameters:
996 * p_ccb->company_id: Company Identifier.
997 *
998 * p_ccb->p_ctrl_cback: Pointer to control callback
999 * function.
1000 *
1001 * p_ccb->p_msg_cback: Pointer to message callback
1002 * function.
1003 *
1004 * p_ccb->conn: AVCTP connection role. This is set to
1005 * AVCTP_INT for initiator connections and AVCTP_ACP
1006 * for acceptor connections.
1007 *
1008 * p_ccb->control: Control role. This is set to
1009 * AVRC_CT_TARGET for target connections, AVRC_CT_CONTROL
1010 * for control connections or
1011 * (AVRC_CT_TARGET|AVRC_CT_CONTROL)
1012 * for connections that support both roles.
1013 *
1014 * peer_addr: BD address of peer device. This value is
1015 * only used for initiator connections; for acceptor
1016 * connections it can be set to NULL.
1017 *
1018 * Output Parameters:
1019 * p_handle: Pointer to handle. This parameter is only
1020 * valid if AVRC_SUCCESS is returned.
1021 *
1022 * Returns AVRC_SUCCESS if successful.
1023 * AVRC_NO_RESOURCES if there are not enough resources to open
1024 * the connection.
1025 *
1026 *****************************************************************************/
AVRC_Open(uint8_t * p_handle,tAVRC_CONN_CB * p_ccb,const RawAddress & peer_addr)1027 uint16_t AVRC_Open(uint8_t* p_handle, tAVRC_CONN_CB* p_ccb,
1028 const RawAddress& peer_addr) {
1029 uint16_t status;
1030 tAVCT_CC cc;
1031
1032 cc.p_ctrl_cback = avrc_ctrl_cback; /* Control callback */
1033 cc.p_msg_cback = avrc_msg_cback; /* Message callback */
1034 cc.pid = UUID_SERVCLASS_AV_REMOTE_CONTROL; /* Profile ID */
1035 cc.role = p_ccb->conn; /* Initiator/acceptor role */
1036 cc.control = p_ccb->control; /* Control role (Control/Target) */
1037
1038 status = AVCT_CreateConn(p_handle, &cc, peer_addr);
1039 if (status == AVCT_SUCCESS) {
1040 avrc_cb.ccb[*p_handle] = *p_ccb;
1041 memset(&avrc_cb.ccb_int[*p_handle], 0, sizeof(tAVRC_CONN_INT_CB));
1042 memset(&avrc_cb.fcb[*p_handle], 0, sizeof(tAVRC_FRAG_CB));
1043 memset(&avrc_cb.rcb[*p_handle], 0, sizeof(tAVRC_RASM_CB));
1044 avrc_cb.ccb_int[*p_handle].tle = alarm_new("avrcp.commandTimer");
1045 avrc_cb.ccb_int[*p_handle].cmd_q = fixed_queue_new(SIZE_MAX);
1046 }
1047 AVRC_TRACE_DEBUG("%s role: %d, control:%d status:%d, handle:%d", __func__,
1048 cc.role, cc.control, status, *p_handle);
1049
1050 return status;
1051 }
1052
1053 /******************************************************************************
1054 *
1055 * Function AVRC_Close
1056 *
1057 * Description Close a connection opened with AVRC_Open().
1058 * This function is called when the
1059 * application is no longer using a connection.
1060 *
1061 * Input Parameters:
1062 * handle: Handle of this connection.
1063 *
1064 * Output Parameters:
1065 * None.
1066 *
1067 * Returns AVRC_SUCCESS if successful.
1068 * AVRC_BAD_HANDLE if handle is invalid.
1069 *
1070 *****************************************************************************/
AVRC_Close(uint8_t handle)1071 uint16_t AVRC_Close(uint8_t handle) {
1072 AVRC_TRACE_DEBUG("%s handle:%d", __func__, handle);
1073 avrc_flush_cmd_q(handle);
1074 return AVCT_RemoveConn(handle);
1075 }
1076
1077 /******************************************************************************
1078 *
1079 * Function AVRC_OpenBrowse
1080 *
1081 * Description This function is called to open a browsing connection to
1082 * AVCTP. The connection can be either an initiator or
1083 * acceptor, as determined by the p_conn_role.
1084 * The handle is returned by a previous call to AVRC_Open.
1085 *
1086 * Returns AVRC_SUCCESS if successful.
1087 * AVRC_NO_RESOURCES if there are not enough resources to open
1088 * the connection.
1089 *
1090 *****************************************************************************/
AVRC_OpenBrowse(uint8_t handle,uint8_t conn_role)1091 uint16_t AVRC_OpenBrowse(uint8_t handle, uint8_t conn_role) {
1092 return AVCT_CreateBrowse(handle, conn_role);
1093 }
1094
1095 /******************************************************************************
1096 *
1097 * Function AVRC_CloseBrowse
1098 *
1099 * Description Close a connection opened with AVRC_OpenBrowse().
1100 * This function is called when the
1101 * application is no longer using a connection.
1102 *
1103 * Returns AVRC_SUCCESS if successful.
1104 * AVRC_BAD_HANDLE if handle is invalid.
1105 *
1106 *****************************************************************************/
AVRC_CloseBrowse(uint8_t handle)1107 uint16_t AVRC_CloseBrowse(uint8_t handle) { return AVCT_RemoveBrowse(handle); }
1108
1109 /******************************************************************************
1110 *
1111 * Function AVRC_MsgReq
1112 *
1113 * Description This function is used to send the AVRCP byte stream in p_pkt
1114 * down to AVCTP.
1115 *
1116 * It is expected that p_pkt->offset is at least
1117 * AVCT_MSG_OFFSET
1118 * p_pkt->layer_specific is AVCT_DATA_CTRL or AVCT_DATA_BROWSE
1119 * p_pkt->event is AVRC_OP_VENDOR, AVRC_OP_PASS_THRU or
1120 * AVRC_OP_BROWSE
1121 * The above BT_HDR settings are set by the AVRC_Bld*
1122 * functions.
1123 *
1124 * Returns AVRC_SUCCESS if successful.
1125 * AVRC_BAD_HANDLE if handle is invalid.
1126 *
1127 *****************************************************************************/
AVRC_MsgReq(uint8_t handle,uint8_t label,uint8_t ctype,BT_HDR * p_pkt)1128 uint16_t AVRC_MsgReq(uint8_t handle, uint8_t label, uint8_t ctype,
1129 BT_HDR* p_pkt) {
1130 uint8_t* p_data;
1131 uint8_t cr = AVCT_CMD;
1132 bool chk_frag = true;
1133 uint8_t* p_start = NULL;
1134 tAVRC_FRAG_CB* p_fcb;
1135 uint16_t len;
1136 uint16_t status;
1137 uint8_t msg_mask = 0;
1138 uint16_t peer_mtu;
1139
1140 if (!p_pkt) return AVRC_BAD_PARAM;
1141
1142 AVRC_TRACE_DEBUG("%s handle = %u label = %u ctype = %u len = %d", __func__,
1143 handle, label, ctype, p_pkt->len);
1144 /* Handle for AVRCP fragment */
1145 bool is_new_avrcp = osi_property_get_bool("persist.bluetooth.enablenewavrcp", true);
1146 if (ctype >= AVRC_RSP_NOT_IMPL) cr = AVCT_RSP;
1147
1148 if (p_pkt->event == AVRC_OP_VENDOR) {
1149 if (is_new_avrcp) {
1150 p_start = (uint8_t*)(p_pkt + 1) + p_pkt->offset + AVRC_VENDOR_HDR_SIZE;
1151 } else {
1152 /* add AVRCP Vendor Dependent headers */
1153 p_start = ((uint8_t*)(p_pkt + 1) + p_pkt->offset);
1154 p_pkt->offset -= AVRC_VENDOR_HDR_SIZE;
1155 p_pkt->len += AVRC_VENDOR_HDR_SIZE;
1156 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1157 *p_data++ = (ctype & AVRC_CTYPE_MASK);
1158 *p_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
1159 *p_data++ = AVRC_OP_VENDOR;
1160 AVRC_CO_ID_TO_BE_STREAM(p_data, AVRC_CO_METADATA);
1161
1162 /* Check if this is a AVRC_PDU_REQUEST_CONTINUATION_RSP */
1163 if (cr == AVCT_CMD) {
1164 msg_mask |= AVRC_MSG_MASK_IS_VENDOR_CMD;
1165
1166 if ((*p_start == AVRC_PDU_REQUEST_CONTINUATION_RSP) ||
1167 (*p_start == AVRC_PDU_ABORT_CONTINUATION_RSP)) {
1168 msg_mask |= AVRC_MSG_MASK_IS_CONTINUATION_RSP;
1169 }
1170 }
1171 }
1172 } else if (p_pkt->event == AVRC_OP_PASS_THRU) {
1173 /* add AVRCP Pass Through headers */
1174 p_start = ((uint8_t*)(p_pkt + 1) + p_pkt->offset);
1175 p_pkt->offset -= AVRC_PASS_THRU_SIZE;
1176 p_pkt->len += AVRC_PASS_THRU_SIZE;
1177 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1178 *p_data++ = (ctype & AVRC_CTYPE_MASK);
1179 *p_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
1180 *p_data++ = AVRC_OP_PASS_THRU; /* opcode */
1181 *p_data++ = AVRC_ID_VENDOR; /* operation id */
1182 *p_data++ = 5; /* operation data len */
1183 AVRC_CO_ID_TO_BE_STREAM(p_data, AVRC_CO_METADATA);
1184 } else {
1185 chk_frag = false;
1186 if (p_pkt->layer_specific == AVCT_DATA_BROWSE) {
1187 peer_mtu = AVCT_GetBrowseMtu(handle);
1188 } else {
1189 peer_mtu = AVCT_GetPeerMtu(handle);
1190 }
1191 if (p_pkt->len > (peer_mtu - AVCT_HDR_LEN_SINGLE)) {
1192 AVRC_TRACE_ERROR(
1193 "%s bigger than peer mtu (p_pkt->len(%d) > peer_mtu(%d-%d))",
1194 __func__, p_pkt->len, peer_mtu, AVCT_HDR_LEN_SINGLE);
1195 osi_free(p_pkt);
1196 return AVRC_MSG_TOO_BIG;
1197 }
1198 }
1199
1200 /* abandon previous fragments */
1201 p_fcb = &avrc_cb.fcb[handle];
1202
1203 if (p_fcb == NULL) {
1204 AVRC_TRACE_ERROR("%s p_fcb is NULL", __func__);
1205 osi_free(p_pkt);
1206 return AVRC_NOT_OPEN;
1207 }
1208
1209 if (p_fcb->frag_enabled) p_fcb->frag_enabled = false;
1210
1211 osi_free_and_reset((void**)&p_fcb->p_fmsg);
1212
1213 /* AVRCP spec has not defined any control channel commands that needs
1214 * fragmentation at this level
1215 * check for fragmentation only on the response */
1216 if ((cr == AVCT_RSP) && (chk_frag)) {
1217 if (p_pkt->len > AVRC_MAX_CTRL_DATA_LEN) {
1218 int offset_len = MAX(AVCT_MSG_OFFSET, p_pkt->offset);
1219 BT_HDR* p_pkt_new =
1220 (BT_HDR*)osi_calloc(AVRC_PACKET_LEN + offset_len + BT_HDR_SIZE);
1221 if (p_start != NULL) {
1222 p_fcb->frag_enabled = true;
1223 p_fcb->p_fmsg = p_pkt;
1224 p_fcb->frag_pdu = *p_start;
1225 p_pkt = p_pkt_new;
1226 p_pkt_new = p_fcb->p_fmsg;
1227 p_pkt->len = AVRC_MAX_CTRL_DATA_LEN;
1228 p_pkt->offset = p_pkt_new->offset;
1229 p_pkt->layer_specific = p_pkt_new->layer_specific;
1230 p_pkt->event = p_pkt_new->event;
1231 p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1232 p_start -= AVRC_VENDOR_HDR_SIZE;
1233 memcpy(p_data, p_start, AVRC_MAX_CTRL_DATA_LEN);
1234 /* use AVRC start packet type */
1235 p_data += AVRC_VENDOR_HDR_SIZE;
1236 p_data++; /* pdu */
1237 *p_data++ = AVRC_PKT_START;
1238
1239 /* 4 pdu, pkt_type & len */
1240 len = (AVRC_MAX_CTRL_DATA_LEN - AVRC_VENDOR_HDR_SIZE -
1241 AVRC_MIN_META_HDR_SIZE);
1242 UINT16_TO_BE_STREAM(p_data, len);
1243
1244 /* prepare the left over for as an end fragment */
1245 avrc_prep_end_frag(handle);
1246 AVRC_TRACE_DEBUG("%s p_pkt len:%d/%d, next len:%d", __func__,
1247 p_pkt->len, len, p_fcb->p_fmsg->len);
1248 } else {
1249 /* TODO: Is this "else" block valid? Remove it? */
1250 AVRC_TRACE_ERROR("%s no buffers for fragmentation", __func__);
1251 osi_free(p_pkt);
1252 return AVRC_NO_RESOURCES;
1253 }
1254 }
1255 } else if ((p_pkt->event == AVRC_OP_VENDOR) && (cr == AVCT_CMD) &&
1256 (avrc_cb.ccb_int[handle].flags & AVRC_CB_FLAGS_RSP_PENDING) &&
1257 !(msg_mask & AVRC_MSG_MASK_IS_CONTINUATION_RSP)) {
1258 /* If we are sending a vendor specific command, and a response is pending,
1259 * then enqueue the command until the response has been received.
1260 * This is to interop with TGs that abort sending responses whenever a new
1261 * command
1262 * is received (exception is continuation request command
1263 * must sent that to get additional response frags) */
1264 AVRC_TRACE_DEBUG(
1265 "AVRC: Enqueuing command 0x%08x (handle=0x%02x, label=0x%02x)", p_pkt,
1266 handle, label);
1267
1268 /* label in BT_HDR (will need this later when the command is dequeued) */
1269 p_pkt->layer_specific = (label << 8) | (p_pkt->layer_specific & 0xFF);
1270
1271 /* Enqueue the command */
1272 fixed_queue_enqueue(avrc_cb.ccb_int[handle].cmd_q, p_pkt);
1273 return AVRC_SUCCESS;
1274 }
1275
1276 /* Send the message */
1277 status = AVCT_MsgReq(handle, label, cr, p_pkt);
1278 if ((status == AVCT_SUCCESS) && (cr == AVCT_CMD)) {
1279 /* If a command was successfully sent, indicate that a response is pending
1280 */
1281 avrc_cb.ccb_int[handle].flags |= AVRC_CB_FLAGS_RSP_PENDING;
1282
1283 /* Start command timer to wait for response */
1284 avrc_start_cmd_timer(handle, label, msg_mask);
1285 }
1286
1287 return status;
1288 }
1289
1290 /******************************************************************************
1291 *
1292 * Function AVRC_PassCmd
1293 *
1294 * Description Send a PASS THROUGH command to the peer device. This
1295 * function can only be called for controller role connections.
1296 * Any response message from the peer is passed back through
1297 * the tAVRC_MSG_CBACK callback function.
1298 *
1299 * Input Parameters:
1300 * handle: Handle of this connection.
1301 *
1302 * label: Transaction label.
1303 *
1304 * p_msg: Pointer to PASS THROUGH message structure.
1305 *
1306 * Output Parameters:
1307 * None.
1308 *
1309 * Returns AVRC_SUCCESS if successful.
1310 * AVRC_BAD_HANDLE if handle is invalid.
1311 *
1312 *****************************************************************************/
AVRC_PassCmd(uint8_t handle,uint8_t label,tAVRC_MSG_PASS * p_msg)1313 uint16_t AVRC_PassCmd(uint8_t handle, uint8_t label, tAVRC_MSG_PASS* p_msg) {
1314 BT_HDR* p_buf;
1315 uint16_t status = AVRC_NO_RESOURCES;
1316 if (!p_msg) return AVRC_BAD_PARAM;
1317
1318 p_msg->hdr.ctype = AVRC_CMD_CTRL;
1319 p_buf = avrc_pass_msg(p_msg);
1320 if (p_buf) {
1321 status = AVCT_MsgReq(handle, label, AVCT_CMD, p_buf);
1322 if (status == AVCT_SUCCESS) {
1323 /* Start command timer to wait for response */
1324 avrc_start_cmd_timer(handle, label, 0);
1325 }
1326 }
1327 return (status);
1328 }
1329
1330 /******************************************************************************
1331 *
1332 * Function AVRC_PassRsp
1333 *
1334 * Description Send a PASS THROUGH response to the peer device. This
1335 * function can only be called for target role connections.
1336 * This function must be called when a PASS THROUGH command
1337 * message is received from the peer through the
1338 * tAVRC_MSG_CBACK callback function.
1339 *
1340 * Input Parameters:
1341 * handle: Handle of this connection.
1342 *
1343 * label: Transaction label. Must be the same value as
1344 * passed with the command message in the callback
1345 * function.
1346 *
1347 * p_msg: Pointer to PASS THROUGH message structure.
1348 *
1349 * Output Parameters:
1350 * None.
1351 *
1352 * Returns AVRC_SUCCESS if successful.
1353 * AVRC_BAD_HANDLE if handle is invalid.
1354 *
1355 *****************************************************************************/
AVRC_PassRsp(uint8_t handle,uint8_t label,tAVRC_MSG_PASS * p_msg)1356 uint16_t AVRC_PassRsp(uint8_t handle, uint8_t label, tAVRC_MSG_PASS* p_msg) {
1357 BT_HDR* p_buf;
1358 if (!p_msg) return AVRC_BAD_PARAM;
1359
1360 p_buf = avrc_pass_msg(p_msg);
1361 if (p_buf) return AVCT_MsgReq(handle, label, AVCT_RSP, p_buf);
1362 return AVRC_NO_RESOURCES;
1363 }
1364