1 /******************************************************************************
3 * Copyright (C) 2009-2013 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
20 #include <hardware/bluetooth.h>
21 #include <hardware/bt_gatt.h>
27 #define LOG_TAG "bt_btif_gatt"
29 #include "btif_common.h"
30 #include "btif_util.h"
32 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
35 #include "bta_gatt_api.h"
36 #include "btif_storage.h"
39 #include "btif_gatt.h"
40 #include "btif_gatt_util.h"
44 #include "osi/include/log.h"
46 /*******************************************************************************
48 *******************************************************************************/
56 /*******************************************************************************
58 *******************************************************************************/
60 static const char * disc_name[GATT_DISC_MAX] =
64 "GATT_DISC_SRVC_BY_UUID",
67 "GATT_DISC_CHAR_DSCPT"
70 static btif_test_cb_t test_cb;
72 /*******************************************************************************
74 *******************************************************************************/
76 static char * format_uuid(tBT_UUID bt_uuid, char *str_buf)
80 if (bt_uuid.len == LEN_UUID_16)
82 sprintf(str_buf, "0x%04x", bt_uuid.uu.uuid16);
84 else if (bt_uuid.len == LEN_UUID_128)
86 x += sprintf(&str_buf[x], "%02x%02x%02x%02x-%02x%02x-%02x%02x",
87 bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14],
88 bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12],
89 bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10],
90 bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]);
91 sprintf(&str_buf[x], "%02x%02x-%02x%02x%02x%02x%02x%02x",
92 bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6],
93 bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4],
94 bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2],
95 bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]);
98 sprintf(str_buf, "Unknown (len=%d)", bt_uuid.len);
103 static void btif_test_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
104 BOOLEAN connected, tGATT_DISCONN_REASON reason, tBT_TRANSPORT transport)
111 LOG_DEBUG("%s: conn_id=%d, connected=%d", __FUNCTION__, conn_id, connected);
112 test_cb.conn_id = connected ? conn_id : 0;
115 static void btif_test_command_complete_cback(UINT16 conn_id, tGATTC_OPTYPE op,
116 tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
118 LOG_DEBUG ("%s: op_code=0x%02x, conn_id=0x%x. status=0x%x",
119 __FUNCTION__, op, conn_id, status);
123 case GATTC_OPTYPE_READ:
124 case GATTC_OPTYPE_WRITE:
125 case GATTC_OPTYPE_CONFIG:
126 case GATTC_OPTYPE_EXE_WRITE:
127 case GATTC_OPTYPE_NOTIFICATION:
130 case GATTC_OPTYPE_INDICATION:
131 GATTC_SendHandleValueConfirm(conn_id, p_data->handle);
135 LOG_DEBUG ("%s: Unknown op_code (0x%02x)", __FUNCTION__, op);
141 static void btif_test_discovery_result_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type,
142 tGATT_DISC_RES *p_data)
147 LOG_DEBUG("------ GATT Discovery result %-22s -------", disc_name[disc_type]);
148 LOG_DEBUG(" Attribute handle: 0x%04x (%d)", p_data->handle, p_data->handle);
150 if (disc_type != GATT_DISC_CHAR_DSCPT) {
151 LOG_DEBUG(" Attribute type: %s", format_uuid(p_data->type, str_buf));
156 case GATT_DISC_SRVC_ALL:
157 LOG_DEBUG(" Handle range: 0x%04x ~ 0x%04x (%d ~ %d)",
158 p_data->handle, p_data->value.group_value.e_handle,
159 p_data->handle, p_data->value.group_value.e_handle);
160 LOG_DEBUG(" Service UUID: %s",
161 format_uuid(p_data->value.group_value.service_type, str_buf));
164 case GATT_DISC_SRVC_BY_UUID:
165 LOG_DEBUG(" Handle range: 0x%04x ~ 0x%04x (%d ~ %d)",
166 p_data->handle, p_data->value.handle,
167 p_data->handle, p_data->value.handle);
170 case GATT_DISC_INC_SRVC:
171 LOG_DEBUG(" Handle range: 0x%04x ~ 0x%04x (%d ~ %d)",
172 p_data->value.incl_service.s_handle, p_data->value.incl_service.e_handle,
173 p_data->value.incl_service.s_handle, p_data->value.incl_service.e_handle);
174 LOG_DEBUG(" Service UUID: %s",
175 format_uuid(p_data->value.incl_service.service_type, str_buf));
179 LOG_DEBUG(" Properties: 0x%02x",
180 p_data->value.dclr_value.char_prop);
181 LOG_DEBUG(" Characteristic UUID: %s",
182 format_uuid(p_data->value.dclr_value.char_uuid, str_buf));
185 case GATT_DISC_CHAR_DSCPT:
186 LOG_DEBUG(" Descriptor UUID: %s", format_uuid(p_data->type, str_buf));
190 LOG_DEBUG("-----------------------------------------------------------");
193 static void btif_test_discovery_complete_cback(UINT16 conn_id,
194 tGATT_DISC_TYPE disc_type,
199 LOG_DEBUG("%s: status=%d", __FUNCTION__, status);
202 static tGATT_CBACK btif_test_callbacks =
204 btif_test_connect_cback ,
205 btif_test_command_complete_cback,
206 btif_test_discovery_result_cback,
207 btif_test_discovery_complete_cback,
213 /*******************************************************************************
215 *******************************************************************************/
217 bt_status_t btif_gattc_test_command_impl(uint16_t command, btgatt_test_params_t* params)
220 case 0x01: /* Enable */
222 LOG_DEBUG("%s: ENABLE - enable=%d", __FUNCTION__, params->u1);
225 tBT_UUID app_uuid = {LEN_UUID_128,{0xAE}};
226 test_cb.gatt_if = GATT_Register(&app_uuid, &btif_test_callbacks);
227 GATT_StartIf(test_cb.gatt_if);
229 GATT_Deregister(test_cb.gatt_if);
235 case 0x02: /* Connect */
237 LOG_DEBUG("%s: CONNECT - device=%02x:%02x:%02x:%02x:%02x:%02x (dev_type=%d, addr_type=%d)",
239 params->bda1->address[0], params->bda1->address[1],
240 params->bda1->address[2], params->bda1->address[3],
241 params->bda1->address[4], params->bda1->address[5],
242 params->u1, params->u2);
244 if (params->u1 == BT_DEVICE_TYPE_BLE)
245 BTM_SecAddBleDevice(params->bda1->address, NULL, BT_DEVICE_TYPE_BLE, params->u2);
247 if ( !GATT_Connect(test_cb.gatt_if, params->bda1->address, TRUE, BT_TRANSPORT_LE) )
249 LOG_ERROR("%s: GATT_Connect failed!", __FUNCTION__);
254 case 0x03: /* Disconnect */
256 LOG_DEBUG("%s: DISCONNECT - conn_id=%d", __FUNCTION__, test_cb.conn_id);
257 GATT_Disconnect(test_cb.conn_id);
261 case 0x04: /* Discover */
264 tGATT_DISC_PARAM param;
265 memset(¶m, 0, sizeof(tGATT_DISC_PARAM));
267 if (params->u1 >= GATT_DISC_MAX)
269 LOG_ERROR("%s: DISCOVER - Invalid type (%d)!", __FUNCTION__, params->u1);
273 param.s_handle = params->u2;
274 param.e_handle = params->u3;
275 btif_to_bta_uuid(¶m.service, params->uuid1);
277 LOG_DEBUG("%s: DISCOVER (%s), conn_id=%d, uuid=%s, handles=0x%04x-0x%04x",
278 __FUNCTION__, disc_name[params->u1], test_cb.conn_id,
279 format_uuid(param.service, buf), params->u2, params->u3);
280 GATTC_Discover(test_cb.conn_id, params->u1, ¶m);
284 case 0xF0: /* Pairing configuration */
285 LOG_DEBUG("%s: Setting pairing config auth=%d, iocaps=%d, keys=%d/%d/%d",
286 __FUNCTION__, params->u1, params->u2, params->u3, params->u4,
289 bte_appl_cfg.ble_auth_req = params->u1;
290 bte_appl_cfg.ble_io_cap = params->u2;
291 bte_appl_cfg.ble_init_key = params->u3;
292 bte_appl_cfg.ble_resp_key = params->u4;
293 bte_appl_cfg.ble_max_key_size = params->u5;
297 LOG_ERROR("%s: UNKNOWN TEST COMMAND 0x%02x", __FUNCTION__, command);