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_malloc(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                          AVRC_PACKET_LEN < sizeof(p_pkt->len))) {
638     /* Ignore the invalid AV/C command frame */
639     p_drop_msg = "dropped - too long AV/C cmd frame size";
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_malloc(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         AVRC_Open
955  *
956  * Description      This function is called to open a connection to AVCTP.
957  *                  The connection can be either an initiator or acceptor, as
958  *                  determined by the p_ccb->stream parameter.
959  *                  The connection can be a target, a controller or for both
960  *                  role, as determined by the p_ccb->control parameter.
961  *                  By definition, a target connection is an acceptor connection
962  *                  that waits for an incoming AVCTP connection from the peer.
963  *                  The connection remains available to the application until
964  *                  the application closes it by calling AVRC_Close().  The
965  *                  application does not need to reopen the connection after an
966  *                  AVRC_CLOSE_IND_EVT is received.
967  *
968  *                  Input Parameters:
969  *                      p_ccb->company_id: Company Identifier.
970  *
971  *                      p_ccb->p_ctrl_cback:  Pointer to control callback
972  *                                            function.
973  *
974  *                      p_ccb->p_msg_cback:  Pointer to message callback
975  *                                            function.
976  *
977  *                      p_ccb->conn: AVCTP connection role.  This is set to
978  *                      AVCTP_INT for initiator connections and AVCTP_ACP
979  *                      for acceptor connections.
980  *
981  *                      p_ccb->control: Control role.  This is set to
982  *                      AVRC_CT_TARGET for target connections, AVRC_CT_CONTROL
983  *                      for control connections or
984  *                      (AVRC_CT_TARGET|AVRC_CT_CONTROL)
985  *                      for connections that support both roles.
986  *
987  *                      peer_addr: BD address of peer device.  This value is
988  *                      only used for initiator connections; for acceptor
989  *                      connections it can be set to NULL.
990  *
991  *                  Output Parameters:
992  *                      p_handle: Pointer to handle.  This parameter is only
993  *                                valid if AVRC_SUCCESS is returned.
994  *
995  * Returns          AVRC_SUCCESS if successful.
996  *                  AVRC_NO_RESOURCES if there are not enough resources to open
997  *                  the connection.
998  *
999  *****************************************************************************/
AVRC_Open(uint8_t * p_handle,tAVRC_CONN_CB * p_ccb,const RawAddress & peer_addr)1000 uint16_t AVRC_Open(uint8_t* p_handle, tAVRC_CONN_CB* p_ccb,
1001                    const RawAddress& peer_addr) {
1002   uint16_t status;
1003   tAVCT_CC cc;
1004 
1005   cc.p_ctrl_cback = avrc_ctrl_cback;         /* Control callback */
1006   cc.p_msg_cback = avrc_msg_cback;           /* Message callback */
1007   cc.pid = UUID_SERVCLASS_AV_REMOTE_CONTROL; /* Profile ID */
1008   cc.role = p_ccb->conn;                     /* Initiator/acceptor role */
1009   cc.control = p_ccb->control;               /* Control role (Control/Target) */
1010 
1011   status = AVCT_CreateConn(p_handle, &cc, peer_addr);
1012   if (status == AVCT_SUCCESS) {
1013     avrc_cb.ccb[*p_handle] = *p_ccb;
1014     memset(&avrc_cb.ccb_int[*p_handle], 0, sizeof(tAVRC_CONN_INT_CB));
1015     memset(&avrc_cb.fcb[*p_handle], 0, sizeof(tAVRC_FRAG_CB));
1016     memset(&avrc_cb.rcb[*p_handle], 0, sizeof(tAVRC_RASM_CB));
1017     avrc_cb.ccb_int[*p_handle].tle = alarm_new("avrcp.commandTimer");
1018     avrc_cb.ccb_int[*p_handle].cmd_q = fixed_queue_new(SIZE_MAX);
1019   }
1020   AVRC_TRACE_DEBUG("%s role: %d, control:%d status:%d, handle:%d", __func__,
1021                    cc.role, cc.control, status, *p_handle);
1022 
1023   return status;
1024 }
1025 
1026 /******************************************************************************
1027  *
1028  * Function         AVRC_Close
1029  *
1030  * Description      Close a connection opened with AVRC_Open().
1031  *                  This function is called when the
1032  *                  application is no longer using a connection.
1033  *
1034  *                  Input Parameters:
1035  *                      handle: Handle of this connection.
1036  *
1037  *                  Output Parameters:
1038  *                      None.
1039  *
1040  * Returns          AVRC_SUCCESS if successful.
1041  *                  AVRC_BAD_HANDLE if handle is invalid.
1042  *
1043  *****************************************************************************/
AVRC_Close(uint8_t handle)1044 uint16_t AVRC_Close(uint8_t handle) {
1045   AVRC_TRACE_DEBUG("%s handle:%d", __func__, handle);
1046   avrc_flush_cmd_q(handle);
1047   return AVCT_RemoveConn(handle);
1048 }
1049 
1050 /******************************************************************************
1051  *
1052  * Function         AVRC_OpenBrowse
1053  *
1054  * Description      This function is called to open a browsing connection to
1055  *                  AVCTP. The connection can be either an initiator or
1056  *                  acceptor, as determined by the p_conn_role.
1057  *                  The handle is returned by a previous call to AVRC_Open.
1058  *
1059  * Returns          AVRC_SUCCESS if successful.
1060  *                  AVRC_NO_RESOURCES if there are not enough resources to open
1061  *                  the connection.
1062  *
1063  *****************************************************************************/
AVRC_OpenBrowse(uint8_t handle,uint8_t conn_role)1064 uint16_t AVRC_OpenBrowse(uint8_t handle, uint8_t conn_role) {
1065   return AVCT_CreateBrowse(handle, conn_role);
1066 }
1067 
1068 /******************************************************************************
1069  *
1070  * Function         AVRC_CloseBrowse
1071  *
1072  * Description      Close a connection opened with AVRC_OpenBrowse().
1073  *                  This function is called when the
1074  *                  application is no longer using a connection.
1075  *
1076  * Returns          AVRC_SUCCESS if successful.
1077  *                  AVRC_BAD_HANDLE if handle is invalid.
1078  *
1079  *****************************************************************************/
AVRC_CloseBrowse(uint8_t handle)1080 uint16_t AVRC_CloseBrowse(uint8_t handle) { return AVCT_RemoveBrowse(handle); }
1081 
1082 /******************************************************************************
1083  *
1084  * Function         AVRC_MsgReq
1085  *
1086  * Description      This function is used to send the AVRCP byte stream in p_pkt
1087  *                  down to AVCTP.
1088  *
1089  *                  It is expected that p_pkt->offset is at least
1090  *                  AVCT_MSG_OFFSET
1091  *                  p_pkt->layer_specific is AVCT_DATA_CTRL or AVCT_DATA_BROWSE
1092  *                  p_pkt->event is AVRC_OP_VENDOR, AVRC_OP_PASS_THRU or
1093  *                  AVRC_OP_BROWSE
1094  *                  The above BT_HDR settings are set by the AVRC_Bld*
1095  *                  functions.
1096  *
1097  * Returns          AVRC_SUCCESS if successful.
1098  *                  AVRC_BAD_HANDLE if handle is invalid.
1099  *
1100  *****************************************************************************/
AVRC_MsgReq(uint8_t handle,uint8_t label,uint8_t ctype,BT_HDR * p_pkt)1101 uint16_t AVRC_MsgReq(uint8_t handle, uint8_t label, uint8_t ctype,
1102                      BT_HDR* p_pkt) {
1103   uint8_t* p_data;
1104   uint8_t cr = AVCT_CMD;
1105   bool chk_frag = true;
1106   uint8_t* p_start = NULL;
1107   tAVRC_FRAG_CB* p_fcb;
1108   uint16_t len;
1109   uint16_t status;
1110   uint8_t msg_mask = 0;
1111   uint16_t peer_mtu;
1112 
1113   if (!p_pkt) return AVRC_BAD_PARAM;
1114 
1115   AVRC_TRACE_DEBUG("%s handle = %u label = %u ctype = %u len = %d", __func__,
1116                    handle, label, ctype, p_pkt->len);
1117   /* Handle for AVRCP fragment */
1118   bool is_new_avrcp = osi_property_get_bool("persist.bluetooth.enablenewavrcp", true);
1119   if (ctype >= AVRC_RSP_NOT_IMPL) cr = AVCT_RSP;
1120 
1121   if (p_pkt->event == AVRC_OP_VENDOR) {
1122     if (is_new_avrcp) {
1123       p_start = (uint8_t*)(p_pkt + 1) + p_pkt->offset + AVRC_VENDOR_HDR_SIZE;
1124     } else {
1125       /* add AVRCP Vendor Dependent headers */
1126       p_start = ((uint8_t*)(p_pkt + 1) + p_pkt->offset);
1127       p_pkt->offset -= AVRC_VENDOR_HDR_SIZE;
1128       p_pkt->len += AVRC_VENDOR_HDR_SIZE;
1129       p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1130       *p_data++ = (ctype & AVRC_CTYPE_MASK);
1131       *p_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
1132       *p_data++ = AVRC_OP_VENDOR;
1133       AVRC_CO_ID_TO_BE_STREAM(p_data, AVRC_CO_METADATA);
1134 
1135       /* Check if this is a AVRC_PDU_REQUEST_CONTINUATION_RSP */
1136       if (cr == AVCT_CMD) {
1137         msg_mask |= AVRC_MSG_MASK_IS_VENDOR_CMD;
1138 
1139         if ((*p_start == AVRC_PDU_REQUEST_CONTINUATION_RSP) ||
1140             (*p_start == AVRC_PDU_ABORT_CONTINUATION_RSP)) {
1141           msg_mask |= AVRC_MSG_MASK_IS_CONTINUATION_RSP;
1142         }
1143       }
1144     }
1145   } else if (p_pkt->event == AVRC_OP_PASS_THRU) {
1146     /* add AVRCP Pass Through headers */
1147     p_start = ((uint8_t*)(p_pkt + 1) + p_pkt->offset);
1148     p_pkt->offset -= AVRC_PASS_THRU_SIZE;
1149     p_pkt->len += AVRC_PASS_THRU_SIZE;
1150     p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1151     *p_data++ = (ctype & AVRC_CTYPE_MASK);
1152     *p_data++ = (AVRC_SUB_PANEL << AVRC_SUBTYPE_SHIFT);
1153     *p_data++ = AVRC_OP_PASS_THRU; /* opcode */
1154     *p_data++ = AVRC_ID_VENDOR;    /* operation id */
1155     *p_data++ = 5;                 /* operation data len */
1156     AVRC_CO_ID_TO_BE_STREAM(p_data, AVRC_CO_METADATA);
1157   } else {
1158     chk_frag = false;
1159     if (p_pkt->layer_specific == AVCT_DATA_BROWSE) {
1160       peer_mtu = AVCT_GetBrowseMtu(handle);
1161     } else {
1162       peer_mtu = AVCT_GetPeerMtu(handle);
1163     }
1164     if (p_pkt->len > (peer_mtu - AVCT_HDR_LEN_SINGLE)) {
1165       AVRC_TRACE_ERROR(
1166           "%s bigger than peer mtu (p_pkt->len(%d) > peer_mtu(%d-%d))",
1167           __func__, p_pkt->len, peer_mtu, AVCT_HDR_LEN_SINGLE);
1168       osi_free(p_pkt);
1169       return AVRC_MSG_TOO_BIG;
1170     }
1171   }
1172 
1173   /* abandon previous fragments */
1174   p_fcb = &avrc_cb.fcb[handle];
1175 
1176   if (p_fcb == NULL) {
1177     AVRC_TRACE_ERROR("%s p_fcb is NULL", __func__);
1178     osi_free(p_pkt);
1179     return AVRC_NOT_OPEN;
1180   }
1181 
1182   if (p_fcb->frag_enabled) p_fcb->frag_enabled = false;
1183 
1184   osi_free_and_reset((void**)&p_fcb->p_fmsg);
1185 
1186   /* AVRCP spec has not defined any control channel commands that needs
1187    * fragmentation at this level
1188    * check for fragmentation only on the response */
1189   if ((cr == AVCT_RSP) && (chk_frag)) {
1190     if (p_pkt->len > AVRC_MAX_CTRL_DATA_LEN) {
1191       int offset_len = MAX(AVCT_MSG_OFFSET, p_pkt->offset);
1192       BT_HDR* p_pkt_new =
1193           (BT_HDR*)osi_malloc(AVRC_PACKET_LEN + offset_len + BT_HDR_SIZE);
1194       if (p_start != NULL) {
1195         p_fcb->frag_enabled = true;
1196         p_fcb->p_fmsg = p_pkt;
1197         p_fcb->frag_pdu = *p_start;
1198         p_pkt = p_pkt_new;
1199         p_pkt_new = p_fcb->p_fmsg;
1200         p_pkt->len = AVRC_MAX_CTRL_DATA_LEN;
1201         p_pkt->offset = p_pkt_new->offset;
1202         p_pkt->layer_specific = p_pkt_new->layer_specific;
1203         p_pkt->event = p_pkt_new->event;
1204         p_data = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1205         p_start -= AVRC_VENDOR_HDR_SIZE;
1206         memcpy(p_data, p_start, AVRC_MAX_CTRL_DATA_LEN);
1207         /* use AVRC start packet type */
1208         p_data += AVRC_VENDOR_HDR_SIZE;
1209         p_data++; /* pdu */
1210         *p_data++ = AVRC_PKT_START;
1211 
1212         /* 4 pdu, pkt_type & len */
1213         len = (AVRC_MAX_CTRL_DATA_LEN - AVRC_VENDOR_HDR_SIZE -
1214                AVRC_MIN_META_HDR_SIZE);
1215         UINT16_TO_BE_STREAM(p_data, len);
1216 
1217         /* prepare the left over for as an end fragment */
1218         avrc_prep_end_frag(handle);
1219         AVRC_TRACE_DEBUG("%s p_pkt len:%d/%d, next len:%d", __func__,
1220                          p_pkt->len, len, p_fcb->p_fmsg->len);
1221       } else {
1222         /* TODO: Is this "else" block valid? Remove it? */
1223         AVRC_TRACE_ERROR("%s no buffers for fragmentation", __func__);
1224         osi_free(p_pkt);
1225         return AVRC_NO_RESOURCES;
1226       }
1227     }
1228   } else if ((p_pkt->event == AVRC_OP_VENDOR) && (cr == AVCT_CMD) &&
1229              (avrc_cb.ccb_int[handle].flags & AVRC_CB_FLAGS_RSP_PENDING) &&
1230              !(msg_mask & AVRC_MSG_MASK_IS_CONTINUATION_RSP)) {
1231     /* If we are sending a vendor specific command, and a response is pending,
1232      * then enqueue the command until the response has been received.
1233      * This is to interop with TGs that abort sending responses whenever a new
1234      * command
1235      * is received (exception is continuation request command
1236      * must sent that to get additional response frags) */
1237     AVRC_TRACE_DEBUG(
1238         "AVRC: Enqueuing command 0x%08x (handle=0x%02x, label=0x%02x)", p_pkt,
1239         handle, label);
1240 
1241     /* label in BT_HDR (will need this later when the command is dequeued) */
1242     p_pkt->layer_specific = (label << 8) | (p_pkt->layer_specific & 0xFF);
1243 
1244     /* Enqueue the command */
1245     fixed_queue_enqueue(avrc_cb.ccb_int[handle].cmd_q, p_pkt);
1246     return AVRC_SUCCESS;
1247   }
1248 
1249   /* Send the message */
1250   status = AVCT_MsgReq(handle, label, cr, p_pkt);
1251   if ((status == AVCT_SUCCESS) && (cr == AVCT_CMD)) {
1252     /* If a command was successfully sent, indicate that a response is pending
1253      */
1254     avrc_cb.ccb_int[handle].flags |= AVRC_CB_FLAGS_RSP_PENDING;
1255 
1256     /* Start command timer to wait for response */
1257     avrc_start_cmd_timer(handle, label, msg_mask);
1258   }
1259 
1260   return status;
1261 }
1262 
1263 /******************************************************************************
1264  *
1265  * Function         AVRC_PassCmd
1266  *
1267  * Description      Send a PASS THROUGH command to the peer device.  This
1268  *                  function can only be called for controller role connections.
1269  *                  Any response message from the peer is passed back through
1270  *                  the tAVRC_MSG_CBACK callback function.
1271  *
1272  *                  Input Parameters:
1273  *                      handle: Handle of this connection.
1274  *
1275  *                      label: Transaction label.
1276  *
1277  *                      p_msg: Pointer to PASS THROUGH message structure.
1278  *
1279  *                  Output Parameters:
1280  *                      None.
1281  *
1282  * Returns          AVRC_SUCCESS if successful.
1283  *                  AVRC_BAD_HANDLE if handle is invalid.
1284  *
1285  *****************************************************************************/
AVRC_PassCmd(uint8_t handle,uint8_t label,tAVRC_MSG_PASS * p_msg)1286 uint16_t AVRC_PassCmd(uint8_t handle, uint8_t label, tAVRC_MSG_PASS* p_msg) {
1287   BT_HDR* p_buf;
1288   uint16_t status = AVRC_NO_RESOURCES;
1289   if (!p_msg) return AVRC_BAD_PARAM;
1290 
1291   p_msg->hdr.ctype = AVRC_CMD_CTRL;
1292   p_buf = avrc_pass_msg(p_msg);
1293   if (p_buf) {
1294     status = AVCT_MsgReq(handle, label, AVCT_CMD, p_buf);
1295     if (status == AVCT_SUCCESS) {
1296       /* Start command timer to wait for response */
1297       avrc_start_cmd_timer(handle, label, 0);
1298     }
1299   }
1300   return (status);
1301 }
1302 
1303 /******************************************************************************
1304  *
1305  * Function         AVRC_PassRsp
1306  *
1307  * Description      Send a PASS THROUGH response to the peer device.  This
1308  *                  function can only be called for target role connections.
1309  *                  This function must be called when a PASS THROUGH command
1310  *                  message is received from the peer through the
1311  *                  tAVRC_MSG_CBACK callback function.
1312  *
1313  *                  Input Parameters:
1314  *                      handle: Handle of this connection.
1315  *
1316  *                      label: Transaction label.  Must be the same value as
1317  *                      passed with the command message in the callback
1318  *                      function.
1319  *
1320  *                      p_msg: Pointer to PASS THROUGH message structure.
1321  *
1322  *                  Output Parameters:
1323  *                      None.
1324  *
1325  * Returns          AVRC_SUCCESS if successful.
1326  *                  AVRC_BAD_HANDLE if handle is invalid.
1327  *
1328  *****************************************************************************/
AVRC_PassRsp(uint8_t handle,uint8_t label,tAVRC_MSG_PASS * p_msg)1329 uint16_t AVRC_PassRsp(uint8_t handle, uint8_t label, tAVRC_MSG_PASS* p_msg) {
1330   BT_HDR* p_buf;
1331   if (!p_msg) return AVRC_BAD_PARAM;
1332 
1333   p_buf = avrc_pass_msg(p_msg);
1334   if (p_buf) return AVCT_MsgReq(handle, label, AVCT_RSP, p_buf);
1335   return AVRC_NO_RESOURCES;
1336 }
1337