1 /****************************************************************************** 2 * 3 * Copyright (c) 2014 The Android Open Source Project 4 * Copyright (C) 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 <string.h> 28 #include "bta_hf_client_api.h" 29 #include "bta_hf_client_int.h" 30 #include "osi/include/compat.h" 31 32 /***************************************************************************** 33 ** Constants and data types 34 *****************************************************************************/ 35 static const tBTA_SYS_REG bta_hf_client_reg = 36 { 37 bta_hf_client_hdl_event, 38 BTA_HfClientDisable 39 }; 40 41 42 /***************************************************************************** 43 ** External Function Declarations 44 *****************************************************************************/ 45 46 /******************************************************************************* 47 ** 48 ** Function BTA_HfClientEnable 49 ** 50 ** Description Enable the HF CLient service. When the enable 51 ** operation is complete the callback function will be 52 ** called with a BTA_HF_CLIENT_ENABLE_EVT. This function must 53 ** be called before other function in the HF CLient API are 54 ** called. 55 ** 56 ** Returns BTA_SUCCESS if OK, BTA_FAILURE otherwise. 57 ** 58 *******************************************************************************/ 59 tBTA_STATUS BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK *p_cback) 60 { 61 if (bta_sys_is_register (BTA_ID_HS)) 62 { 63 APPL_TRACE_ERROR("BTA HF Client is already enabled, ignoring ..."); 64 return BTA_FAILURE; 65 } 66 67 /* register with BTA system manager */ 68 bta_sys_register(BTA_ID_HS, &bta_hf_client_reg); 69 70 tBTA_HF_CLIENT_API_ENABLE *p_buf = 71 (tBTA_HF_CLIENT_API_ENABLE *)osi_malloc(sizeof(tBTA_HF_CLIENT_API_ENABLE)); 72 p_buf->hdr.event = BTA_HF_CLIENT_API_ENABLE_EVT; 73 p_buf->p_cback = p_cback; 74 75 bta_sys_sendmsg(p_buf); 76 77 return BTA_SUCCESS; 78 } 79 80 /******************************************************************************* 81 ** 82 ** Function BTA_HfClientDisable 83 ** 84 ** Description Disable the HF Client service 85 ** 86 ** 87 ** Returns void 88 ** 89 *******************************************************************************/ 90 void BTA_HfClientDisable(void) 91 { 92 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR)); 93 94 p_buf->event = BTA_HF_CLIENT_API_DISABLE_EVT; 95 96 bta_sys_sendmsg(p_buf); 97 } 98 99 /******************************************************************************* 100 ** 101 ** Function BTA_HfClientRegister 102 ** 103 ** Description Register an HF Client service. 104 ** 105 ** 106 ** Returns void 107 ** 108 *******************************************************************************/ 109 void BTA_HfClientRegister(tBTA_SEC sec_mask, tBTA_HF_CLIENT_FEAT features, 110 char *p_service_name) 111 { 112 tBTA_HF_CLIENT_API_REGISTER *p_buf = 113 (tBTA_HF_CLIENT_API_REGISTER *)osi_malloc(sizeof(tBTA_HF_CLIENT_API_REGISTER)); 114 115 p_buf->hdr.event = BTA_HF_CLIENT_API_REGISTER_EVT; 116 p_buf->features = features; 117 p_buf->sec_mask = sec_mask; 118 if (p_service_name) 119 strlcpy(p_buf->name, p_service_name, BTA_SERVICE_NAME_LEN); 120 else 121 p_buf->name[0] = 0; 122 123 bta_sys_sendmsg(p_buf); 124 } 125 126 /******************************************************************************* 127 ** 128 ** Function BTA_HfClientDeregister 129 ** 130 ** Description Deregister an HF Client service. 131 ** 132 ** 133 ** Returns void 134 ** 135 *******************************************************************************/ 136 void BTA_HfClientDeregister(UINT16 handle) 137 { 138 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR)); 139 140 p_buf->event = BTA_HF_CLIENT_API_DEREGISTER_EVT; 141 p_buf->layer_specific = handle; 142 143 bta_sys_sendmsg(p_buf); 144 } 145 146 /******************************************************************************* 147 ** 148 ** Function BTA_HfClientOpen 149 ** 150 ** Description Opens a connection to an audio gateway. 151 ** When connection is open callback function is called 152 ** with a BTA_AG_OPEN_EVT. Only the data connection is 153 ** opened. The audio connection is not opened. 154 ** 155 ** 156 ** Returns void 157 ** 158 *******************************************************************************/ 159 void BTA_HfClientOpen(UINT16 handle, BD_ADDR bd_addr, tBTA_SEC sec_mask) 160 { 161 tBTA_HF_CLIENT_API_OPEN *p_buf = 162 (tBTA_HF_CLIENT_API_OPEN *)osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN)); 163 164 p_buf->hdr.event = BTA_HF_CLIENT_API_OPEN_EVT; 165 p_buf->hdr.layer_specific = handle; 166 bdcpy(p_buf->bd_addr, bd_addr); 167 p_buf->sec_mask = sec_mask; 168 169 bta_sys_sendmsg(p_buf); 170 } 171 172 /******************************************************************************* 173 ** 174 ** Function BTA_HfClientClose 175 ** 176 ** Description Close the current connection to an audio gateway. 177 ** Any current audio connection will also be closed 178 ** 179 ** 180 ** Returns void 181 ** 182 *******************************************************************************/ 183 void BTA_HfClientClose(UINT16 handle) 184 { 185 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR)); 186 187 p_buf->event = BTA_HF_CLIENT_API_CLOSE_EVT; 188 p_buf->layer_specific = handle; 189 190 bta_sys_sendmsg(p_buf); 191 } 192 193 /******************************************************************************* 194 ** 195 ** Function BTA_HfCllientAudioOpen 196 ** 197 ** Description Opens an audio connection to the currently connected 198 ** audio gateway 199 ** 200 ** 201 ** Returns void 202 ** 203 *******************************************************************************/ 204 void BTA_HfClientAudioOpen(UINT16 handle) 205 { 206 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR)); 207 208 p_buf->event = BTA_HF_CLIENT_API_AUDIO_OPEN_EVT; 209 p_buf->layer_specific = handle; 210 211 bta_sys_sendmsg(p_buf); 212 } 213 214 /******************************************************************************* 215 ** 216 ** Function BTA_HfClientAudioClose 217 ** 218 ** Description Close the currently active audio connection to an audio 219 ** gateway. The data connection remains open 220 ** 221 ** 222 ** Returns void 223 ** 224 *******************************************************************************/ 225 void BTA_HfClientAudioClose(UINT16 handle) 226 { 227 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR)); 228 229 p_buf->event = BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT; 230 p_buf->layer_specific = handle; 231 232 bta_sys_sendmsg(p_buf); 233 } 234 235 /******************************************************************************* 236 ** 237 ** Function BTA_HfClientSendAT 238 ** 239 ** Description send AT command 240 ** 241 ** 242 ** Returns void 243 ** 244 *******************************************************************************/ 245 void BTA_HfClientSendAT(UINT16 handle, tBTA_HF_CLIENT_AT_CMD_TYPE at, UINT32 val1, UINT32 val2, const char *str) 246 { 247 tBTA_HF_CLIENT_DATA_VAL *p_buf = 248 (tBTA_HF_CLIENT_DATA_VAL *)osi_malloc(sizeof(tBTA_HF_CLIENT_DATA_VAL)); 249 250 p_buf->hdr.event = BTA_HF_CLIENT_SEND_AT_CMD_EVT; 251 p_buf->uint8_val = at; 252 p_buf->uint32_val1 = val1; 253 p_buf->uint32_val2 = val2; 254 255 if (str) { 256 strlcpy(p_buf->str, str, BTA_HF_CLIENT_NUMBER_LEN + 1); 257 p_buf->str[BTA_HF_CLIENT_NUMBER_LEN] = '\0'; 258 } else { 259 p_buf->str[0] = '\0'; 260 } 261 262 p_buf->hdr.layer_specific = handle; 263 264 bta_sys_sendmsg(p_buf); 265 } 266