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