1 /******************************************************************************
2 *
3 * Copyright (c) 2014 The Android Open Source Project
4 * Copyright 2003-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /******************************************************************************
21 *
22 * This is the implementation of the API for the handsfree (HF role)
23 * subsystem of BTA
24 *
25 ******************************************************************************/
26
27 #include <cstdint>
28
29 #include "bt_trace.h" // Legacy trace logging
30
31 #include "bta/hf_client/bta_hf_client_int.h"
32 #include "bta/include/bta_hf_client_api.h"
33 #include "bta/sys/bta_sys.h"
34 #include "osi/include/allocator.h"
35 #include "osi/include/compat.h"
36 #include "stack/include/bt_types.h"
37 #include "types/raw_address.h"
38
39 /*****************************************************************************
40 * External Function Declarations
41 ****************************************************************************/
42
43 /*******************************************************************************
44 *
45 * Function BTA_HfClientEnable
46 *
47 * Description Enable the HF CLient service. It does the following:
48 * 1. Sets the state to initialized (control blocks)
49 * 2. Starts the SDP for the client role (HF)
50 * 3. Starts the RFCOMM server to accept incoming connections
51 * The function is synchronous and returns with an error code
52 * if anything went wrong. This should be the first call to the
53 * API before doing an BTA_HfClientOpen
54 *
55 * Returns BTA_SUCCESS if OK, BTA_FAILURE otherwise.
56 *
57 ******************************************************************************/
BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK * p_cback,tBTA_HF_CLIENT_FEAT features,const char * p_service_name)58 tBTA_STATUS BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK* p_cback,
59 tBTA_HF_CLIENT_FEAT features,
60 const char* p_service_name) {
61 return bta_hf_client_api_enable(p_cback, features, p_service_name);
62 }
63
64 /*******************************************************************************
65 *
66 * Function BTA_HfClientDisable
67 *
68 * Description Disable the HF Client service
69 *
70 * Returns void
71 *
72 ******************************************************************************/
BTA_HfClientDisable(void)73 void BTA_HfClientDisable(void) { bta_hf_client_api_disable(); }
74
75 /*******************************************************************************
76 *
77 * Function BTA_HfClientOpen
78 *
79 * Description Opens up a RF connection to the remote device and
80 * subsequently set it up for a HF SLC
81 *
82 * Returns void
83 *
84 ******************************************************************************/
BTA_HfClientOpen(const RawAddress & bd_addr,uint16_t * p_handle)85 void BTA_HfClientOpen(const RawAddress& bd_addr, uint16_t* p_handle) {
86 APPL_TRACE_DEBUG("%s", __func__);
87 tBTA_HF_CLIENT_API_OPEN* p_buf =
88 (tBTA_HF_CLIENT_API_OPEN*)osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN));
89
90 if (!bta_hf_client_allocate_handle(bd_addr, p_handle)) {
91 APPL_TRACE_ERROR("%s: could not allocate handle", __func__);
92 return;
93 }
94
95 p_buf->hdr.event = BTA_HF_CLIENT_API_OPEN_EVT;
96 p_buf->hdr.layer_specific = *p_handle;
97 p_buf->bd_addr = bd_addr;
98
99 bta_sys_sendmsg(p_buf);
100 }
101
102 /*******************************************************************************
103 *
104 * Function BTA_HfClientClose
105 *
106 * Description Close the current connection to an audio gateway.
107 * Any current audio connection will also be closed
108 *
109 *
110 * Returns void
111 *
112 ******************************************************************************/
BTA_HfClientClose(uint16_t handle)113 void BTA_HfClientClose(uint16_t handle) {
114 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
115
116 p_buf->event = BTA_HF_CLIENT_API_CLOSE_EVT;
117 p_buf->layer_specific = handle;
118
119 bta_sys_sendmsg(p_buf);
120 }
121
122 /*******************************************************************************
123 *
124 * Function BTA_HfCllientAudioOpen
125 *
126 * Description Opens an audio connection to the currently connected
127 * audio gateway
128 *
129 *
130 * Returns void
131 *
132 ******************************************************************************/
BTA_HfClientAudioOpen(uint16_t handle)133 void BTA_HfClientAudioOpen(uint16_t handle) {
134 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
135
136 p_buf->event = BTA_HF_CLIENT_API_AUDIO_OPEN_EVT;
137 p_buf->layer_specific = handle;
138
139 bta_sys_sendmsg(p_buf);
140 }
141
142 /*******************************************************************************
143 *
144 * Function BTA_HfClientAudioClose
145 *
146 * Description Close the currently active audio connection to an audio
147 * gateway. The data connection remains open
148 *
149 *
150 * Returns void
151 *
152 ******************************************************************************/
BTA_HfClientAudioClose(uint16_t handle)153 void BTA_HfClientAudioClose(uint16_t handle) {
154 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
155
156 p_buf->event = BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT;
157 p_buf->layer_specific = handle;
158
159 bta_sys_sendmsg(p_buf);
160 }
161
162 /*******************************************************************************
163 *
164 * Function BTA_HfClientSendAT
165 *
166 * Description send AT command
167 *
168 *
169 * Returns void
170 *
171 ******************************************************************************/
BTA_HfClientSendAT(uint16_t handle,tBTA_HF_CLIENT_AT_CMD_TYPE at,uint32_t val1,uint32_t val2,const char * str)172 void BTA_HfClientSendAT(uint16_t handle, tBTA_HF_CLIENT_AT_CMD_TYPE at,
173 uint32_t val1, uint32_t val2, const char* str) {
174 tBTA_HF_CLIENT_DATA_VAL* p_buf =
175 (tBTA_HF_CLIENT_DATA_VAL*)osi_malloc(sizeof(tBTA_HF_CLIENT_DATA_VAL));
176
177 p_buf->hdr.event = BTA_HF_CLIENT_SEND_AT_CMD_EVT;
178 p_buf->uint8_val = at;
179 p_buf->uint32_val1 = val1;
180 p_buf->uint32_val2 = val2;
181
182 if (str) {
183 strlcpy(p_buf->str, str, BTA_HF_CLIENT_NUMBER_LEN + 1);
184 p_buf->str[BTA_HF_CLIENT_NUMBER_LEN] = '\0';
185 } else {
186 p_buf->str[0] = '\0';
187 }
188
189 p_buf->hdr.layer_specific = handle;
190
191 bta_sys_sendmsg(p_buf);
192 }
193
194 /*******************************************************************************
195 *
196 * Function BTA_HfClientDumpStatistics
197 *
198 * Description Dump statistics about the various control blocks
199 * and other relevant connection statistics
200 *
201 * Returns Void
202 *
203 ******************************************************************************/
BTA_HfClientDumpStatistics(int fd)204 void BTA_HfClientDumpStatistics(int fd) { bta_hf_client_dump_statistics(fd); }
205