blob: 549cf002e6dd000fcf5b809ccb5cfd86389119e7 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2010-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 GATT module of BTA.
22 *
23 ******************************************************************************/
24
25#include "bt_target.h"
26
27#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
28
29#include <string.h>
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070030#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include "bta_sys.h"
32#include "bta_gatt_api.h"
33#include "bta_gattc_int.h"
34
35/*****************************************************************************
36** Constants
37*****************************************************************************/
38
Andre Eisenbache1202ca2013-05-15 04:55:08 -070039static const tBTA_SYS_REG bta_gattc_reg =
The Android Open Source Project5738f832012-12-12 16:00:35 -080040{
41 bta_gattc_hdl_event,
Andre Eisenbache1202ca2013-05-15 04:55:08 -070042 BTA_GATTC_Disable
The Android Open Source Project5738f832012-12-12 16:00:35 -080043};
44
Andre Eisenbache1202ca2013-05-15 04:55:08 -070045
The Android Open Source Project5738f832012-12-12 16:00:35 -080046/*******************************************************************************
47**
Andre Eisenbache1202ca2013-05-15 04:55:08 -070048** Function BTA_GATTC_Disable
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080049**
Andre Eisenbache1202ca2013-05-15 04:55:08 -070050** Description This function is called to disable GATTC module
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080051**
Andre Eisenbache1202ca2013-05-15 04:55:08 -070052** Parameters None.
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080053**
54** Returns None
55**
56*******************************************************************************/
Andre Eisenbache1202ca2013-05-15 04:55:08 -070057void BTA_GATTC_Disable(void)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080058{
Andre Eisenbache1202ca2013-05-15 04:55:08 -070059 if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
60 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -070061 APPL_TRACE_WARNING("GATTC Module not enabled/already disabled");
Andre Eisenbache1202ca2013-05-15 04:55:08 -070062 return;
63 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -070064
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -080065 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
66 p_buf->event = BTA_GATTC_API_DISABLE_EVT;
67
68 bta_sys_sendmsg(p_buf);
69 bta_sys_deregister(BTA_ID_GATTC);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080070}
71
72/*******************************************************************************
73**
The Android Open Source Project5738f832012-12-12 16:00:35 -080074** Function BTA_GATTC_AppRegister
75**
76** Description This function is called to register application callbacks
77** with BTA GATTC module.
78**
79** Parameters p_app_uuid - applicaiton UUID
80** p_client_cb - pointer to the application callback function.
81**
82** Returns None
83**
84*******************************************************************************/
85void BTA_GATTC_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTC_CBACK *p_client_cb)
86{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -080087 tBTA_GATTC_API_REG *p_buf =
88 (tBTA_GATTC_API_REG *)osi_malloc(sizeof(tBTA_GATTC_API_REG));
The Android Open Source Project5738f832012-12-12 16:00:35 -080089
Andre Eisenbache1202ca2013-05-15 04:55:08 -070090 if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
Andre Eisenbache1202ca2013-05-15 04:55:08 -070091 bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080092
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -080093 p_buf->hdr.event = BTA_GATTC_API_REG_EVT;
94 if (p_app_uuid != NULL)
95 memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
96 p_buf->p_cback = p_client_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -080097
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -080098 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -080099}
100
101/*******************************************************************************
102**
103** Function BTA_GATTC_AppDeregister
104**
105** Description This function is called to deregister an application
106** from BTA GATTC module.
107**
108** Parameters client_if - client interface identifier.
109**
110** Returns None
111**
112*******************************************************************************/
113void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)
114{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800115 tBTA_GATTC_API_DEREG *p_buf =
116 (tBTA_GATTC_API_DEREG *)osi_malloc(sizeof(tBTA_GATTC_API_DEREG));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800117
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800118 p_buf->hdr.event = BTA_GATTC_API_DEREG_EVT;
119 p_buf->client_if = client_if;
120
121 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800122}
123
124/*******************************************************************************
125**
126** Function BTA_GATTC_Open
127**
128** Description Open a direct connection or add a background auto connection
129** bd address
130**
131** Parameters client_if: server interface.
132** remote_bda: remote device BD address.
133** is_direct: direct connection or background auto connection
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700134** transport: Transport to be used for GATT connection (BREDR/LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800135**
136** Returns void
137**
138*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700139void BTA_GATTC_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
140 BOOLEAN is_direct, tBTA_GATT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800142 tBTA_GATTC_API_OPEN *p_buf =
143 (tBTA_GATTC_API_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_OPEN));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800145 p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
146 p_buf->client_if = client_if;
147 p_buf->is_direct = is_direct;
148 p_buf->transport = transport;
149 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800150
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800151 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152}
153
154/*******************************************************************************
155**
156** Function BTA_GATTC_CancelOpen
157**
158** Description Cancel a direct open connection or remove a background auto connection
159** bd address
160**
161** Parameters client_if: server interface.
162** remote_bda: remote device BD address.
163** is_direct: direct connection or background auto connection
164**
165** Returns void
166**
167*******************************************************************************/
168void BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
169{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800170 tBTA_GATTC_API_CANCEL_OPEN *p_buf =
171 (tBTA_GATTC_API_CANCEL_OPEN *)osi_malloc(sizeof(tBTA_GATTC_API_CANCEL_OPEN));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800172
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800173 p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
174 p_buf->client_if = client_if;
175 p_buf->is_direct = is_direct;
176 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800177
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800178 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179}
180
181/*******************************************************************************
182**
183** Function BTA_GATTC_Close
184**
185** Description Close a connection to a GATT server.
186**
187** Parameters conn_id: connectino ID to be closed.
188**
189** Returns void
190**
191*******************************************************************************/
192void BTA_GATTC_Close(UINT16 conn_id)
193{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800194 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800195
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800196 p_buf->event = BTA_GATTC_API_CLOSE_EVT;
197 p_buf->layer_specific = conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800198
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800199 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800201
The Android Open Source Project5738f832012-12-12 16:00:35 -0800202/*******************************************************************************
203**
Andre Eisenbach9426d532014-03-25 06:31:17 -0700204** Function BTA_GATTC_ConfigureMTU
205**
206** Description Configure the MTU size in the GATT channel. This can be done
207** only once per connection.
208**
209** Parameters conn_id: connection ID.
210** mtu: desired MTU size to use.
211**
212** Returns void
213**
214*******************************************************************************/
215void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu)
216{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800217 tBTA_GATTC_API_CFG_MTU *p_buf =
218 (tBTA_GATTC_API_CFG_MTU *)osi_malloc(sizeof(tBTA_GATTC_API_CFG_MTU));
Andre Eisenbach9426d532014-03-25 06:31:17 -0700219
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800220 p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
221 p_buf->hdr.layer_specific = conn_id;
222 p_buf->mtu = mtu;
Andre Eisenbach9426d532014-03-25 06:31:17 -0700223
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800224 bta_sys_sendmsg(p_buf);
Andre Eisenbach9426d532014-03-25 06:31:17 -0700225}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800226
Andre Eisenbach9426d532014-03-25 06:31:17 -0700227/*******************************************************************************
228**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229** Function BTA_GATTC_ServiceSearchRequest
230**
231** Description This function is called to request a GATT service discovery
232** on a GATT server. This function report service search result
233** by a callback event, and followed by a service search complete
234** event.
235**
236** Parameters conn_id: connection ID.
237** p_srvc_uuid: a UUID of the service application is interested in.
238** If Null, discover for all services.
239**
240** Returns None
241**
242*******************************************************************************/
243void BTA_GATTC_ServiceSearchRequest (UINT16 conn_id, tBT_UUID *p_srvc_uuid)
244{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800245 const size_t len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID);
246 tBTA_GATTC_API_SEARCH *p_buf = (tBTA_GATTC_API_SEARCH *)osi_calloc(len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800247
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800248 p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
249 p_buf->hdr.layer_specific = conn_id;
250 if (p_srvc_uuid) {
251 p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1);
252 memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID));
253 } else {
254 p_buf->p_srvc_uuid = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800255 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800256
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800257 bta_sys_sendmsg(p_buf);
258}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259
260/*******************************************************************************
261**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700262** Function BTA_GATTC_GetServices
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700264** Description This function is called to find the services on the given server.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265**
266** Parameters conn_id: connection ID which identify the server.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700268** Returns returns list_t of tBTA_GATTC_SERVICE or NULL.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800269**
270*******************************************************************************/
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700271const list_t* BTA_GATTC_GetServices(UINT16 conn_id) {
272 return bta_gattc_get_services(conn_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273}
274
275/*******************************************************************************
276**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700277** Function BTA_GATTC_GetCharacteristic
The Android Open Source Project5738f832012-12-12 16:00:35 -0800278**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700279** Description This function is called to find the characteristic on the given server.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700281** Parameters conn_id - connection ID which identify the server.
282** handle - characteristic handle
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700284** Returns returns pointer to tBTA_GATTC_CHARACTERISTIC or NULL.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285**
286*******************************************************************************/
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700287const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(UINT16 conn_id, UINT16 handle) {
288 return bta_gattc_get_characteristic(conn_id, handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800289}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800290
291/*******************************************************************************
292**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700293** Function BTA_GATTC_GetDescriptor
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700295** Description This function is called to find the characteristic on the given server.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700297** Parameters conn_id - connection ID which identify the server.
298** handle - descriptor handle
The Android Open Source Project5738f832012-12-12 16:00:35 -0800299**
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700300** Returns returns pointer to tBTA_GATTC_DESCRIPTOR or NULL.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800301**
302*******************************************************************************/
Jakub Pawlowski5204fd72016-03-15 15:35:00 -0700303const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 handle) {
304 return bta_gattc_get_descriptor(conn_id, handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800306
307/*******************************************************************************
308**
Jakub Pawlowski533fdbf2016-01-21 23:26:11 -0800309** Function BTA_GATTC_GetGattDb
310**
311** Description This function is called to get the GATT database.
312**
313** Parameters conn_id: connection ID which identify the server.
314** db: output parameter which will contain the GATT database copy.
315** Caller is responsible for freeing it.
316** count: number of elements in database.
317**
318*******************************************************************************/
Jakub Pawlowski9fd552b2016-02-10 15:26:14 -0800319void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
320 btgatt_db_element_t **db, int *count)
Jakub Pawlowski533fdbf2016-01-21 23:26:11 -0800321{
Jakub Pawlowski9fd552b2016-02-10 15:26:14 -0800322 bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count);
Jakub Pawlowski533fdbf2016-01-21 23:26:11 -0800323}
324
325/*******************************************************************************
326**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800327** Function BTA_GATTC_ReadCharacteristic
328**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700329** Description This function is called to read a characteristics value
The Android Open Source Project5738f832012-12-12 16:00:35 -0800330**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700331** Parameters conn_id - connection ID.
332** handle - characteritic handle to read.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800333**
334** Returns None
335**
336*******************************************************************************/
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700337void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800339 tBTA_GATTC_API_READ *p_buf =
340 (tBTA_GATTC_API_READ *)osi_calloc(sizeof(tBTA_GATTC_API_READ));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800341
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800342 p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
343 p_buf->hdr.layer_specific = conn_id;
344 p_buf->auth_req = auth_req;
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700345 p_buf->handle = handle;
346 p_buf->cmpl_evt = BTA_GATTC_READ_CHAR_EVT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800348 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349}
350
351/*******************************************************************************
352**
353** Function BTA_GATTC_ReadCharDescr
354**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700355** Description This function is called to read a descriptor value.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356**
357** Parameters conn_id - connection ID.
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700358** handle - descriptor handle to read.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359**
360** Returns None
361**
362*******************************************************************************/
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700363void BTA_GATTC_ReadCharDescr (UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364{
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700365 tBTA_GATTC_API_READ *p_buf =
366 (tBTA_GATTC_API_READ *)osi_calloc(sizeof(tBTA_GATTC_API_READ));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800367
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800368 p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
369 p_buf->hdr.layer_specific = conn_id;
370 p_buf->auth_req = auth_req;
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700371 p_buf->handle = handle;
372 p_buf->cmpl_evt = BTA_GATTC_READ_DESCR_EVT;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700373
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800374 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800375}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800376
The Android Open Source Project5738f832012-12-12 16:00:35 -0800377/*******************************************************************************
378**
379** Function BTA_GATTC_ReadMultiple
380**
381** Description This function is called to read multiple characteristic or
382** characteristic descriptors.
383**
384** Parameters conn_id - connectino ID.
385** p_read_multi - pointer to the read multiple parameter.
386**
387** Returns None
388**
389*******************************************************************************/
390void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
391 tBTA_GATT_AUTH_REQ auth_req)
392{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800393 tBTA_GATTC_API_READ_MULTI *p_buf =
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700394 (tBTA_GATTC_API_READ_MULTI *)osi_calloc(sizeof(tBTA_GATTC_API_READ_MULTI));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800395
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800396 p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
397 p_buf->hdr.layer_specific = conn_id;
398 p_buf->auth_req = auth_req;
399 p_buf->num_attr = p_read_multi->num_attr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700401 if (p_buf->num_attr > 0)
402 memcpy(p_buf->handles, p_read_multi->handles, sizeof(UINT16) * p_read_multi->num_attr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800403
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800404 bta_sys_sendmsg(p_buf);
405}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800406
407/*******************************************************************************
408**
409** Function BTA_GATTC_WriteCharValue
410**
411** Description This function is called to write characteristic value.
412**
413** Parameters conn_id - connection ID.
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700414** handle - characteristic handle to write.
415** write_type - type of write.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416** len: length of the data to be written.
417** p_value - the value to be written.
418**
419** Returns None
420**
421*******************************************************************************/
422void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700423 UINT16 handle,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424 tBTA_GATTC_WRITE_TYPE write_type,
425 UINT16 len,
426 UINT8 *p_value,
427 tBTA_GATT_AUTH_REQ auth_req)
428{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800429 tBTA_GATTC_API_WRITE *p_buf =
430 (tBTA_GATTC_API_WRITE *)osi_calloc(sizeof(tBTA_GATTC_API_WRITE) + len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800431
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800432 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
433 p_buf->hdr.layer_specific = conn_id;
434 p_buf->auth_req = auth_req;
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700435 p_buf->handle = handle;
436 p_buf->cmpl_evt = BTA_GATTC_WRITE_CHAR_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800437 p_buf->write_type = write_type;
438 p_buf->len = len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800439
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800440 if (p_value && len > 0) {
441 p_buf->p_value = (UINT8 *)(p_buf + 1);
442 memcpy(p_buf->p_value, p_value, len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800443 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800444
445 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800447
The Android Open Source Project5738f832012-12-12 16:00:35 -0800448/*******************************************************************************
449**
450** Function BTA_GATTC_WriteCharDescr
451**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700452** Description This function is called to write descriptor value.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800453**
454** Parameters conn_id - connection ID
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700455** handle - descriptor hadle to write.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800456** write_type - write type.
457** p_value - the value to be written.
458**
459** Returns None
460**
461*******************************************************************************/
462void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700463 UINT16 handle,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800464 tBTA_GATTC_WRITE_TYPE write_type,
465 tBTA_GATT_UNFMT *p_data,
466 tBTA_GATT_AUTH_REQ auth_req)
467{
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700468 size_t len = sizeof(tBTA_GATTC_API_WRITE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800469
470 if (p_data != NULL)
471 len += p_data->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800472
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800473 tBTA_GATTC_API_WRITE *p_buf = (tBTA_GATTC_API_WRITE *)osi_calloc(len);
474 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
475 p_buf->hdr.layer_specific = conn_id;
476 p_buf->auth_req = auth_req;
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700477 p_buf->handle = handle;
478 p_buf->cmpl_evt = BTA_GATTC_WRITE_DESCR_EVT;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800479 p_buf->write_type = write_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800481 if (p_data && p_data->len != 0) {
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700482 p_buf->p_value = (UINT8 *)(p_buf + 1);
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800483 p_buf->len = p_data->len;
484 /* pack the descr data */
485 memcpy(p_buf->p_value, p_data->p_value, p_data->len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800486 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800487
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800488 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800489}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800490
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491/*******************************************************************************
492**
493** Function BTA_GATTC_PrepareWrite
494**
495** Description This function is called to prepare write a characteristic value.
496**
497** Parameters conn_id - connection ID.
498** p_char_id - GATT characteritic ID of the service.
499** offset - offset of the write value.
500** len: length of the data to be written.
501** p_value - the value to be written.
502**
503** Returns None
504**
505*******************************************************************************/
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700506void BTA_GATTC_PrepareWrite (UINT16 conn_id, UINT16 handle,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507 UINT16 offset, UINT16 len, UINT8 *p_value,
508 tBTA_GATT_AUTH_REQ auth_req)
509{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800510 tBTA_GATTC_API_WRITE *p_buf =
511 (tBTA_GATTC_API_WRITE *)osi_calloc(sizeof(tBTA_GATTC_API_WRITE) + len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800513 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
514 p_buf->hdr.layer_specific = conn_id;
515 p_buf->auth_req = auth_req;
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700516 p_buf->handle = handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800517
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800518 p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
519 p_buf->offset = offset;
520 p_buf->len = len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800521
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800522 if (p_value && len > 0) {
523 p_buf->p_value = (UINT8 *)(p_buf + 1);
524 memcpy(p_buf->p_value, p_value, len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800527 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800528}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800529
The Android Open Source Project5738f832012-12-12 16:00:35 -0800530/*******************************************************************************
531**
532** Function BTA_GATTC_ExecuteWrite
533**
534** Description This function is called to execute write a prepare write sequence.
535**
536** Parameters conn_id - connection ID.
537** is_execute - execute or cancel.
538**
539** Returns None
540**
541*******************************************************************************/
542void BTA_GATTC_ExecuteWrite (UINT16 conn_id, BOOLEAN is_execute)
543{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800544 tBTA_GATTC_API_EXEC *p_buf =
545 (tBTA_GATTC_API_EXEC *)osi_calloc(sizeof(tBTA_GATTC_API_EXEC));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800547 p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
548 p_buf->hdr.layer_specific = conn_id;
549 p_buf->is_execute = is_execute;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800550
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800551 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800553
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554/*******************************************************************************
555**
556** Function BTA_GATTC_SendIndConfirm
557**
558** Description This function is called to send handle value confirmation.
559**
560** Parameters conn_id - connection ID.
561** p_char_id - characteristic ID to confirm.
562**
563** Returns None
564**
565*******************************************************************************/
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700566void BTA_GATTC_SendIndConfirm (UINT16 conn_id, UINT16 handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800568 tBTA_GATTC_API_CONFIRM *p_buf =
569 (tBTA_GATTC_API_CONFIRM *)osi_calloc(sizeof(tBTA_GATTC_API_CONFIRM));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700571 APPL_TRACE_API("%s conn_id=%d handle=0x%04x", __func__, conn_id, handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800572
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800573 p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
574 p_buf->hdr.layer_specific = conn_id;
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700575 p_buf->handle = handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800577 bta_sys_sendmsg(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800578}
579
580/*******************************************************************************
581**
582** Function BTA_GATTC_RegisterForNotifications
583**
584** Description This function is called to register for notification of a service.
585**
586** Parameters client_if - client interface.
587** bda - target GATT server.
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700588** handle - GATT characteristic handle.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800589**
590** Returns OK if registration succeed, otherwise failed.
591**
592*******************************************************************************/
593tBTA_GATT_STATUS BTA_GATTC_RegisterForNotifications (tBTA_GATTC_IF client_if,
Jakub Pawlowski063ca022016-04-25 10:43:02 -0700594 const BD_ADDR bda, UINT16 handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595{
596 tBTA_GATTC_RCB *p_clreg;
597 tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
598 UINT8 i;
599
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700600 if (!handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800601 {
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700602 APPL_TRACE_ERROR("deregistration failed, handle is 0");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800603 return status;
604 }
605
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606 if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
607 {
608 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
609 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800610 if ( p_clreg->notif_reg[i].in_use &&
611 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700612 p_clreg->notif_reg[i].handle == handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800613 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700614 APPL_TRACE_WARNING("notification already registered");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615 status = BTA_GATT_OK;
616 break;
617 }
618 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800619 if (status != BTA_GATT_OK)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800620 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800621 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
622 {
623 if (!p_clreg->notif_reg[i].in_use)
624 {
625 memset((void *)&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
626
627 p_clreg->notif_reg[i].in_use = TRUE;
628 memcpy(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN);
629
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700630 p_clreg->notif_reg[i].handle = handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800631 status = BTA_GATT_OK;
632 break;
633 }
634 }
635 if (i == BTA_GATTC_NOTIF_REG_MAX)
636 {
637 status = BTA_GATT_NO_RESOURCES;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700638 APPL_TRACE_ERROR("Max Notification Reached, registration failed.");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800639 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640 }
641 }
642 else
643 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700644 APPL_TRACE_ERROR("Client_if: %d Not Registered", client_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645 }
646
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647 return status;
648}
649
650/*******************************************************************************
651**
652** Function BTA_GATTC_DeregisterForNotifications
653**
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700654** Description This function is called to de-register for notification of a servbice.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655**
656** Parameters client_if - client interface.
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700657** remote_bda - target GATT server.
658** handle - GATT characteristic handle.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800659**
660** Returns OK if deregistration succeed, otherwise failed.
661**
662*******************************************************************************/
663tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF client_if,
Jakub Pawlowski063ca022016-04-25 10:43:02 -0700664 const BD_ADDR bda, UINT16 handle)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800665{
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700666 if (!handle) {
667 APPL_TRACE_ERROR("%s: deregistration failed, handle is 0", __func__);
668 return BTA_GATT_ILLEGAL_PARAMETER;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800669 }
670
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700671 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(client_if);
672 if (p_clreg == NULL) {
Chris Mantonf8027002015-03-12 09:22:48 -0700673 APPL_TRACE_ERROR("%s client_if: %d not registered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
674 __func__, client_if, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700675 return BTA_GATT_ILLEGAL_PARAMETER;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800676 }
677
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700678 for (int i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
679 if (p_clreg->notif_reg[i].in_use &&
680 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
681 p_clreg->notif_reg[i].handle == handle) {
682 APPL_TRACE_DEBUG("%s deregistered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
683 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
684 memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
685 return BTA_GATT_OK;
686 }
687 }
688
689 APPL_TRACE_ERROR("%s registration not found bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
690 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
691 return BTA_GATT_ERROR;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800692}
693
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800694/*******************************************************************************
695**
696** Function BTA_GATTC_Refresh
697**
698** Description Refresh the server cache of the remote device
699**
700** Parameters remote_bda: remote device BD address.
701**
702** Returns void
703**
704*******************************************************************************/
Jakub Pawlowski063ca022016-04-25 10:43:02 -0700705void BTA_GATTC_Refresh(const BD_ADDR remote_bda)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800706{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800707 tBTA_GATTC_API_OPEN *p_buf =
708 (tBTA_GATTC_API_OPEN *)osi_malloc(sizeof(tBTA_GATTC_API_OPEN));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800709
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800710 p_buf->hdr.event = BTA_GATTC_API_REFRESH_EVT;
711 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800712
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800713 bta_sys_sendmsg(p_buf);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800714}
Andre Eisenbach5c44e452013-08-06 18:19:37 -0700715
716/*******************************************************************************
717**
718** Function BTA_GATTC_Listen
719**
720** Description Start advertisement to listen for connection request for a GATT
721** client application.
722**
723** Parameters client_if: server interface.
724** start: to start or stop listening for connection
725** remote_bda: remote device BD address, if listen to all device
726** use NULL.
727**
728** Returns void
729**
730*******************************************************************************/
731void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
732{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800733 tBTA_GATTC_API_LISTEN *p_buf =
734 (tBTA_GATTC_API_LISTEN *)osi_malloc(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN);
Andre Eisenbach5c44e452013-08-06 18:19:37 -0700735
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800736 p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
737 p_buf->client_if = client_if;
738 p_buf->start = start;
739 if (target_bda) {
740 p_buf->remote_bda = (UINT8*)(p_buf + 1);
741 memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
742 } else {
743 p_buf->remote_bda = NULL;
Andre Eisenbach5c44e452013-08-06 18:19:37 -0700744 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800745
746 bta_sys_sendmsg(p_buf);
Andre Eisenbach5c44e452013-08-06 18:19:37 -0700747}
748
Andre Eisenbacha4611ac2014-03-05 15:41:53 -0800749/*******************************************************************************
750**
751** Function BTA_GATTC_Broadcast
752**
753** Description Start broadcasting (non-connectable advertisements)
754**
755** Parameters client_if: client interface.
756** start: to start or stop listening for connection
757**
758** Returns void
759**
760*******************************************************************************/
761void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
762{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800763 tBTA_GATTC_API_LISTEN *p_buf =
764 (tBTA_GATTC_API_LISTEN *)osi_malloc(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN);
Andre Eisenbacha4611ac2014-03-05 15:41:53 -0800765
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800766 p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
767 p_buf->client_if = client_if;
768 p_buf->start = start;
769
770 bta_sys_sendmsg(p_buf);
Andre Eisenbacha4611ac2014-03-05 15:41:53 -0800771}
772
The Android Open Source Project5738f832012-12-12 16:00:35 -0800773#endif /* BTA_GATT_INCLUDED */