OSDN Git Service

Replace BT_HDR => BT_HDR_RIGID
[android-x86/system-bt.git] / bta / av / bta_av_api.cc
1 /******************************************************************************
2  *
3  *  Copyright 2011-2012 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  *  This is the implementation of the API for the advanced audio/video (AV)
22  *  subsystem of BTA, Broadcom's Bluetooth application layer for mobile
23  *  phones.
24  *
25  ******************************************************************************/
26
27 #define LOG_TAG "bt_bta_av"
28
29 #include "bt_target.h"  // Must be first to define build configuration
30
31 #include "bta/av/bta_av_int.h"
32 #include "osi/include/allocator.h"
33 #include "osi/include/compat.h"
34 #include "osi/include/log.h"
35
36 /*****************************************************************************
37  *  Constants
38  ****************************************************************************/
39
40 static const tBTA_SYS_REG bta_av_reg = {bta_av_hdl_event, BTA_AvDisable};
41
42 /*******************************************************************************
43  *
44  * Function         BTA_AvEnable
45  *
46  * Description      Enable the advanced audio/video service. When the enable
47  *                  operation is complete the callback function will be
48  *                  called with a BTA_AV_ENABLE_EVT. This function must
49  *                  be called before other function in the AV API are
50  *                  called.
51  *
52  * Returns          void
53  *
54  ******************************************************************************/
55 void BTA_AvEnable(tBTA_AV_FEAT features, tBTA_AV_CBACK* p_cback) {
56   tBTA_AV_API_ENABLE* p_buf =
57       (tBTA_AV_API_ENABLE*)osi_malloc(sizeof(tBTA_AV_API_ENABLE));
58
59   /* register with BTA system manager */
60   bta_sys_register(BTA_ID_AV, &bta_av_reg);
61
62   p_buf->hdr.event = BTA_AV_API_ENABLE_EVT;
63   p_buf->p_cback = p_cback;
64   p_buf->features = features;
65
66   bta_sys_sendmsg(p_buf);
67 }
68
69 /*******************************************************************************
70  *
71  * Function         BTA_AvDisable
72  *
73  * Description      Disable the advanced audio/video service.
74  *
75  * Returns          void
76  *
77  ******************************************************************************/
78 void BTA_AvDisable(void) {
79   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
80
81   bta_sys_deregister(BTA_ID_AV);
82   p_buf->event = BTA_AV_API_DISABLE_EVT;
83
84   bta_sys_sendmsg(p_buf);
85 }
86
87 /*******************************************************************************
88  *
89  * Function         BTA_AvRegister
90  *
91  * Description      Register the audio or video service to stack. When the
92  *                  operation is complete the callback function will be
93  *                  called with a BTA_AV_REGISTER_EVT. This function must
94  *                  be called before AVDT stream is open.
95  *
96  *
97  * Returns          void
98  *
99  ******************************************************************************/
100 void BTA_AvRegister(tBTA_AV_CHNL chnl, const char* p_service_name,
101                     uint8_t app_id, tBTA_AV_SINK_DATA_CBACK* p_sink_data_cback,
102                     uint16_t service_uuid) {
103   tBTA_AV_API_REG* p_buf =
104       (tBTA_AV_API_REG*)osi_malloc(sizeof(tBTA_AV_API_REG));
105
106   p_buf->hdr.layer_specific = chnl;
107   p_buf->hdr.event = BTA_AV_API_REGISTER_EVT;
108   if (p_service_name)
109     strlcpy(p_buf->p_service_name, p_service_name, BTA_SERVICE_NAME_LEN);
110   else
111     p_buf->p_service_name[0] = 0;
112   p_buf->app_id = app_id;
113   p_buf->p_app_sink_data_cback = p_sink_data_cback;
114   p_buf->service_uuid = service_uuid;
115
116   bta_sys_sendmsg(p_buf);
117 }
118
119 /*******************************************************************************
120  *
121  * Function         BTA_AvDeregister
122  *
123  * Description      Deregister the audio or video service
124  *
125  * Returns          void
126  *
127  ******************************************************************************/
128 void BTA_AvDeregister(tBTA_AV_HNDL hndl) {
129   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
130
131   p_buf->layer_specific = hndl;
132   p_buf->event = BTA_AV_API_DEREGISTER_EVT;
133
134   bta_sys_sendmsg(p_buf);
135 }
136
137 /*******************************************************************************
138  *
139  * Function         BTA_AvOpen
140  *
141  * Description      Opens an advanced audio/video connection to a peer device.
142  *                  When connection is open callback function is called
143  *                  with a BTA_AV_OPEN_EVT.
144  *
145  * Returns          void
146  *
147  ******************************************************************************/
148 void BTA_AvOpen(const RawAddress& bd_addr, tBTA_AV_HNDL handle, bool use_rc,
149                 uint16_t uuid) {
150   LOG_INFO("%s: peer %s bta_handle:0x%x use_rc=%s uuid=0x%x", __func__,
151            bd_addr.ToString().c_str(), handle, (use_rc) ? "true" : "false",
152            uuid);
153
154   tBTA_AV_API_OPEN* p_buf =
155       (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
156
157   p_buf->hdr.event = BTA_AV_API_OPEN_EVT;
158   p_buf->hdr.layer_specific = handle;
159   p_buf->bd_addr = bd_addr;
160   p_buf->use_rc = use_rc;
161   p_buf->switch_res = BTA_AV_RS_NONE;
162   p_buf->uuid = uuid;
163
164   bta_sys_sendmsg(p_buf);
165 }
166
167 /*******************************************************************************
168  *
169  * Function         BTA_AvClose
170  *
171  * Description      Close the current streams.
172  *
173  * Returns          void
174  *
175  ******************************************************************************/
176 void BTA_AvClose(tBTA_AV_HNDL handle) {
177   LOG_INFO("%s: bta_handle:0x%x", __func__, handle);
178
179   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
180
181   p_buf->event = BTA_AV_API_CLOSE_EVT;
182   p_buf->layer_specific = handle;
183
184   bta_sys_sendmsg(p_buf);
185 }
186
187 /*******************************************************************************
188  *
189  * Function         BTA_AvDisconnect
190  *
191  * Description      Close the connection to the address.
192  *
193  * Returns          void
194  *
195  ******************************************************************************/
196 void BTA_AvDisconnect(const RawAddress& bd_addr) {
197   LOG_INFO("%s: peer %s", __func__, bd_addr.ToString().c_str());
198
199   tBTA_AV_API_DISCNT* p_buf =
200       (tBTA_AV_API_DISCNT*)osi_malloc(sizeof(tBTA_AV_API_DISCNT));
201
202   p_buf->hdr.event = BTA_AV_API_DISCONNECT_EVT;
203   p_buf->bd_addr = bd_addr;
204
205   bta_sys_sendmsg(p_buf);
206 }
207
208 /*******************************************************************************
209  *
210  * Function         BTA_AvStart
211  *
212  * Description      Start audio/video stream data transfer.
213  *
214  * Returns          void
215  *
216  ******************************************************************************/
217 void BTA_AvStart(tBTA_AV_HNDL handle) {
218   LOG_INFO("Starting audio/video stream data transfer bta_handle:%hhu", handle);
219
220   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
221
222   p_buf->event = BTA_AV_API_START_EVT;
223   p_buf->layer_specific = handle;
224
225   bta_sys_sendmsg(p_buf);
226 }
227
228 /*******************************************************************************
229  *
230  * Function         BTA_AvOffloadStart
231  *
232  * Description      Start a2dp audio offloading.
233  *
234  * Returns          void
235  *
236  ******************************************************************************/
237 void BTA_AvOffloadStart(tBTA_AV_HNDL hndl) {
238   LOG_INFO("%s: bta_handle=0x%x", __func__, hndl);
239
240   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
241
242   p_buf->event = BTA_AV_API_OFFLOAD_START_EVT;
243   p_buf->layer_specific = hndl;
244
245   bta_sys_sendmsg(p_buf);
246 }
247
248 /*******************************************************************************
249  *
250  * Function         BTA_AvOffloadStartRsp
251  *
252  * Description      Response from vendor lib for A2DP Offload Start request.
253  *
254  * Returns          void
255  *
256  ******************************************************************************/
257 void BTA_AvOffloadStartRsp(tBTA_AV_HNDL hndl, tBTA_AV_STATUS status) {
258   tBTA_AV_API_STATUS_RSP* p_buf =
259       (tBTA_AV_API_STATUS_RSP*)osi_malloc(sizeof(tBTA_AV_API_STATUS_RSP));
260
261   p_buf->hdr.event = BTA_AV_API_OFFLOAD_START_RSP_EVT;
262   p_buf->hdr.layer_specific = hndl;
263   p_buf->status = status;
264
265   bta_sys_sendmsg(p_buf);
266 }
267
268 /*******************************************************************************
269  *
270  * Function         BTA_AvStop
271  *
272  * Description      Stop audio/video stream data transfer.
273  *                  If suspend is true, this function sends AVDT suspend signal
274  *                  to the connected peer(s).
275  *
276  * Returns          void
277  *
278  ******************************************************************************/
279 void BTA_AvStop(tBTA_AV_HNDL handle, bool suspend) {
280   LOG_INFO("%s: bta_handle=0x%x suspend=%s", __func__, handle,
281            logbool(suspend).c_str());
282
283   tBTA_AV_API_STOP* p_buf =
284       (tBTA_AV_API_STOP*)osi_malloc(sizeof(tBTA_AV_API_STOP));
285
286   p_buf->hdr.event = BTA_AV_API_STOP_EVT;
287   p_buf->hdr.layer_specific = handle;
288   p_buf->flush = true;
289   p_buf->suspend = suspend;
290   p_buf->reconfig_stop = false;
291
292   bta_sys_sendmsg(p_buf);
293 }
294
295 /*******************************************************************************
296  *
297  * Function         BTA_AvReconfig
298  *
299  * Description      Reconfigure the audio/video stream.
300  *                  If suspend is true, this function tries the
301  *                  suspend/reconfigure procedure first.
302  *                  If suspend is false or when suspend/reconfigure fails,
303  *                  this function closes and re-opens the AVDT connection.
304  *
305  * Returns          void
306  *
307  ******************************************************************************/
308 void BTA_AvReconfig(tBTA_AV_HNDL hndl, bool suspend, uint8_t sep_info_idx,
309                     uint8_t* p_codec_info, uint8_t num_protect,
310                     const uint8_t* p_protect_info) {
311   LOG_INFO("%s: bta_handle=0x%x suspend=%s sep_info_idx=%d", __func__, hndl,
312            logbool(suspend).c_str(), sep_info_idx);
313
314   tBTA_AV_API_RCFG* p_buf =
315       (tBTA_AV_API_RCFG*)osi_malloc(sizeof(tBTA_AV_API_RCFG) + num_protect);
316
317   p_buf->hdr.layer_specific = hndl;
318   p_buf->hdr.event = BTA_AV_API_RECONFIG_EVT;
319   p_buf->num_protect = num_protect;
320   p_buf->suspend = suspend;
321   p_buf->sep_info_idx = sep_info_idx;
322   p_buf->p_protect_info = (uint8_t*)(p_buf + 1);
323   memcpy(p_buf->codec_info, p_codec_info, AVDT_CODEC_SIZE);
324   memcpy(p_buf->p_protect_info, p_protect_info, num_protect);
325
326   bta_sys_sendmsg(p_buf);
327 }
328
329 /*******************************************************************************
330  *
331  * Function         BTA_AvProtectReq
332  *
333  * Description      Send a content protection request.  This function can only
334  *                  be used if AV is enabled with feature BTA_AV_FEAT_PROTECT.
335  *
336  * Returns          void
337  *
338  ******************************************************************************/
339 void BTA_AvProtectReq(tBTA_AV_HNDL hndl, uint8_t* p_data, uint16_t len) {
340   tBTA_AV_API_PROTECT_REQ* p_buf = (tBTA_AV_API_PROTECT_REQ*)osi_malloc(
341       sizeof(tBTA_AV_API_PROTECT_REQ) + len);
342
343   p_buf->hdr.layer_specific = hndl;
344   p_buf->hdr.event = BTA_AV_API_PROTECT_REQ_EVT;
345   p_buf->len = len;
346   if (p_data == NULL) {
347     p_buf->p_data = NULL;
348   } else {
349     p_buf->p_data = (uint8_t*)(p_buf + 1);
350     memcpy(p_buf->p_data, p_data, len);
351   }
352
353   bta_sys_sendmsg(p_buf);
354 }
355
356 /*******************************************************************************
357  *
358  * Function         BTA_AvProtectRsp
359  *
360  * Description      Send a content protection response.  This function must
361  *                  be called if a BTA_AV_PROTECT_REQ_EVT is received.
362  *                  This function can only be used if AV is enabled with
363  *                  feature BTA_AV_FEAT_PROTECT.
364  *
365  * Returns          void
366  *
367  ******************************************************************************/
368 void BTA_AvProtectRsp(tBTA_AV_HNDL hndl, uint8_t error_code, uint8_t* p_data,
369                       uint16_t len) {
370   tBTA_AV_API_PROTECT_RSP* p_buf = (tBTA_AV_API_PROTECT_RSP*)osi_malloc(
371       sizeof(tBTA_AV_API_PROTECT_RSP) + len);
372
373   p_buf->hdr.layer_specific = hndl;
374   p_buf->hdr.event = BTA_AV_API_PROTECT_RSP_EVT;
375   p_buf->len = len;
376   p_buf->error_code = error_code;
377   if (p_data == NULL) {
378     p_buf->p_data = NULL;
379   } else {
380     p_buf->p_data = (uint8_t*)(p_buf + 1);
381     memcpy(p_buf->p_data, p_data, len);
382   }
383
384   bta_sys_sendmsg(p_buf);
385 }
386
387 /*******************************************************************************
388  *
389  * Function         BTA_AvRemoteCmd
390  *
391  * Description      Send a remote control command.  This function can only
392  *                  be used if AV is enabled with feature BTA_AV_FEAT_RCCT.
393  *
394  * Returns          void
395  *
396  ******************************************************************************/
397 void BTA_AvRemoteCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_RC rc_id,
398                      tBTA_AV_STATE key_state) {
399   tBTA_AV_API_REMOTE_CMD* p_buf =
400       (tBTA_AV_API_REMOTE_CMD*)osi_malloc(sizeof(tBTA_AV_API_REMOTE_CMD));
401
402   p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
403   p_buf->hdr.layer_specific = rc_handle;
404   p_buf->msg.op_id = rc_id;
405   p_buf->msg.state = key_state;
406   p_buf->msg.p_pass_data = NULL;
407   p_buf->msg.pass_len = 0;
408   p_buf->label = label;
409
410   bta_sys_sendmsg(p_buf);
411 }
412
413 /*******************************************************************************
414  *
415  * Function         BTA_AvRemoteVendorUniqueCmd
416  *
417  * Description      Send a remote control command with Vendor Unique rc_id.
418  *                  This function can only be used if AV is enabled with
419  *                  feature BTA_AV_FEAT_RCCT.
420  *
421  * Returns          void
422  *
423  ******************************************************************************/
424 void BTA_AvRemoteVendorUniqueCmd(uint8_t rc_handle, uint8_t label,
425                                  tBTA_AV_STATE key_state, uint8_t* p_msg,
426                                  uint8_t buf_len) {
427   tBTA_AV_API_REMOTE_CMD* p_buf = (tBTA_AV_API_REMOTE_CMD*)osi_malloc(
428       sizeof(tBTA_AV_API_REMOTE_CMD) + buf_len);
429
430   p_buf->label = label;
431   p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
432   p_buf->hdr.layer_specific = rc_handle;
433   p_buf->msg.op_id = AVRC_ID_VENDOR;
434   p_buf->msg.state = key_state;
435   p_buf->msg.pass_len = buf_len;
436   if (p_msg == NULL) {
437     p_buf->msg.p_pass_data = NULL;
438   } else {
439     p_buf->msg.p_pass_data = (uint8_t*)(p_buf + 1);
440     memcpy(p_buf->msg.p_pass_data, p_msg, buf_len);
441   }
442   bta_sys_sendmsg(p_buf);
443 }
444
445 /*******************************************************************************
446  *
447  * Function         BTA_AvVendorCmd
448  *
449  * Description      Send a vendor dependent remote control command.  This
450  *                  function can only be used if AV is enabled with feature
451  *                  BTA_AV_FEAT_VENDOR.
452  *
453  * Returns          void
454  *
455  ******************************************************************************/
456 void BTA_AvVendorCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE cmd_code,
457                      uint8_t* p_data, uint16_t len) {
458   tBTA_AV_API_VENDOR* p_buf =
459       (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
460
461   p_buf->hdr.event = BTA_AV_API_VENDOR_CMD_EVT;
462   p_buf->hdr.layer_specific = rc_handle;
463   p_buf->msg.hdr.ctype = cmd_code;
464   p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
465   p_buf->msg.hdr.subunit_id = 0;
466   p_buf->msg.company_id = p_bta_av_cfg->company_id;
467   p_buf->label = label;
468   p_buf->msg.vendor_len = len;
469   if (p_data == NULL) {
470     p_buf->msg.p_vendor_data = NULL;
471   } else {
472     p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
473     memcpy(p_buf->msg.p_vendor_data, p_data, len);
474   }
475
476   bta_sys_sendmsg(p_buf);
477 }
478
479 /*******************************************************************************
480  *
481  * Function         BTA_AvVendorRsp
482  *
483  * Description      Send a vendor dependent remote control response.
484  *                  This function must be called if a BTA_AV_VENDOR_CMD_EVT
485  *                  is received. This function can only be used if AV is
486  *                  enabled with feature BTA_AV_FEAT_VENDOR.
487  *
488  * Returns          void
489  *
490  ******************************************************************************/
491 void BTA_AvVendorRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
492                      uint8_t* p_data, uint16_t len, uint32_t company_id) {
493   tBTA_AV_API_VENDOR* p_buf =
494       (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
495
496   p_buf->hdr.event = BTA_AV_API_VENDOR_RSP_EVT;
497   p_buf->hdr.layer_specific = rc_handle;
498   p_buf->msg.hdr.ctype = rsp_code;
499   p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
500   p_buf->msg.hdr.subunit_id = 0;
501   if (company_id)
502     p_buf->msg.company_id = company_id;
503   else
504     p_buf->msg.company_id = p_bta_av_cfg->company_id;
505   p_buf->label = label;
506   p_buf->msg.vendor_len = len;
507   if (p_data == NULL) {
508     p_buf->msg.p_vendor_data = NULL;
509   } else {
510     p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
511     memcpy(p_buf->msg.p_vendor_data, p_data, len);
512   }
513
514   bta_sys_sendmsg(p_buf);
515 }
516
517 /*******************************************************************************
518  *
519  * Function         BTA_AvOpenRc
520  *
521  * Description      Open an AVRCP connection toward the device with the
522  *                  specified handle
523  *
524  * Returns          void
525  *
526  ******************************************************************************/
527 void BTA_AvOpenRc(tBTA_AV_HNDL handle) {
528   tBTA_AV_API_OPEN_RC* p_buf =
529       (tBTA_AV_API_OPEN_RC*)osi_malloc(sizeof(tBTA_AV_API_OPEN_RC));
530
531   p_buf->hdr.event = BTA_AV_API_RC_OPEN_EVT;
532   p_buf->hdr.layer_specific = handle;
533
534   bta_sys_sendmsg(p_buf);
535 }
536
537 /*******************************************************************************
538  *
539  * Function         BTA_AvCloseRc
540  *
541  * Description      Close an AVRCP connection
542  *
543  * Returns          void
544  *
545  ******************************************************************************/
546 void BTA_AvCloseRc(uint8_t rc_handle) {
547   tBTA_AV_API_CLOSE_RC* p_buf =
548       (tBTA_AV_API_CLOSE_RC*)osi_malloc(sizeof(tBTA_AV_API_CLOSE_RC));
549
550   p_buf->hdr.event = BTA_AV_API_RC_CLOSE_EVT;
551   p_buf->hdr.layer_specific = rc_handle;
552
553   bta_sys_sendmsg(p_buf);
554 }
555
556 /*******************************************************************************
557  *
558  * Function         BTA_AvMetaRsp
559  *
560  * Description      Send a Metadata/Advanced Control response. The message
561  *                  contained in p_pkt can be composed with AVRC utility
562  *                  functions.
563  *                  This function can only be used if AV is enabled with feature
564  *                  BTA_AV_FEAT_METADATA.
565  *
566  * Returns          void
567  *
568  ******************************************************************************/
569 void BTA_AvMetaRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
570                    BT_HDR* p_pkt) {
571   tBTA_AV_API_META_RSP* p_buf =
572       (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
573
574   p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
575   p_buf->hdr.layer_specific = rc_handle;
576   p_buf->rsp_code = rsp_code;
577   p_buf->p_pkt = p_pkt;
578   p_buf->is_rsp = true;
579   p_buf->label = label;
580
581   bta_sys_sendmsg(p_buf);
582 }
583
584 /*******************************************************************************
585  *
586  * Function         BTA_AvMetaCmd
587  *
588  * Description      Send a Metadata/Advanced Control command. The message
589 *contained
590  *                  in p_pkt can be composed with AVRC utility functions.
591  *                  This function can only be used if AV is enabled with feature
592  *                  BTA_AV_FEAT_METADATA.
593  *                  This message is sent only when the peer supports the TG
594 *role.
595 *8                  The only command makes sense right now is the absolute
596 *volume command.
597  *
598  * Returns          void
599  *
600  ******************************************************************************/
601 void BTA_AvMetaCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CMD cmd_code,
602                    BT_HDR* p_pkt) {
603   tBTA_AV_API_META_RSP* p_buf =
604       (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
605
606   p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
607   p_buf->hdr.layer_specific = rc_handle;
608   p_buf->p_pkt = p_pkt;
609   p_buf->rsp_code = cmd_code;
610   p_buf->is_rsp = false;
611   p_buf->label = label;
612
613   bta_sys_sendmsg(p_buf);
614 }