1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2013 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 #define LOG_TAG "bt_btif_gatt"
20 
21 #include "btif_gatt_util.h"
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <hardware/bluetooth.h>
29 #include <hardware/bt_gatt.h>
30 
31 #include "bdaddr.h"
32 #include "bt_common.h"
33 #include "bta_api.h"
34 #include "bta_gatt_api.h"
35 #include "bta_jv_api.h"
36 #include "btif_common.h"
37 #include "btif_config.h"
38 #include "btif_dm.h"
39 #include "btif_gatt.h"
40 #include "btif_storage.h"
41 #include "btif_util.h"
42 #include "osi/include/osi.h"
43 
44 #define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */
45 #define GATTC_READ_VALUE_TYPE_AGG_FORMAT \
46   0x2905 /* Characteristic Aggregate Format*/
47 
48 static unsigned char BASE_UUID[16] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00,
49                                       0x00, 0x80, 0x00, 0x10, 0x00, 0x00,
50                                       0x00, 0x00, 0x00, 0x00};
51 
uuidType(const unsigned char * p_uuid)52 int uuidType(const unsigned char* p_uuid) {
53   int i = 0;
54   int match = 0;
55   int all_zero = 1;
56 
57   for (i = 0; i != 16; ++i) {
58     if (i == 12 || i == 13) continue;
59 
60     if (p_uuid[i] == BASE_UUID[i]) ++match;
61 
62     if (p_uuid[i] != 0) all_zero = 0;
63   }
64   if (all_zero) return 0;
65   if (match == 12) return LEN_UUID_32;
66   if (match == 14) return LEN_UUID_16;
67   return LEN_UUID_128;
68 }
69 
70 /*******************************************************************************
71  * BTIF -> BTA conversion functions
72  ******************************************************************************/
73 
btif_to_bta_uuid(tBT_UUID * p_dest,const bt_uuid_t * p_src)74 void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src) {
75   char* p_byte = (char*)p_src;
76   int i = 0;
77 
78   p_dest->len = uuidType(p_src->uu);
79 
80   switch (p_dest->len) {
81     case LEN_UUID_16:
82       p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
83       break;
84 
85     case LEN_UUID_32:
86       p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12];
87       p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
88       break;
89 
90     case LEN_UUID_128:
91       for (i = 0; i != 16; ++i) p_dest->uu.uuid128[i] = p_byte[i];
92       break;
93 
94     default:
95       LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_dest->len);
96       break;
97   }
98 }
99 
btif_to_bta_response(tBTA_GATTS_RSP * p_dest,btgatt_response_t * p_src)100 void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src) {
101   p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
102   p_dest->attr_value.handle = p_src->attr_value.handle;
103   p_dest->attr_value.len = p_src->attr_value.len;
104   p_dest->attr_value.offset = p_src->attr_value.offset;
105   memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
106 }
107 
btif_to_bta_uuid_mask(tBTM_BLE_PF_COND_MASK * p_mask,const bt_uuid_t * uuid_mask,const bt_uuid_t * svc_uuid)108 void btif_to_bta_uuid_mask(tBTM_BLE_PF_COND_MASK* p_mask,
109                            const bt_uuid_t* uuid_mask,
110                            const bt_uuid_t* svc_uuid) {
111   char* p_byte = (char*)uuid_mask;
112   int uuid_len = uuidType(svc_uuid->uu);
113   int i = 0;
114 
115   switch (uuid_len) {
116     case LEN_UUID_16:
117       p_mask->uuid16_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12];
118       break;
119 
120     case LEN_UUID_32:
121       p_mask->uuid32_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12];
122       p_mask->uuid32_mask +=
123           (uuid_mask->uu[15] << 24) + (uuid_mask->uu[14] << 16);
124       break;
125 
126     case LEN_UUID_128:
127       for (i = 0; i != 16; ++i) p_mask->uuid128_mask[i] = p_byte[i];
128       break;
129 
130     default:
131       break;
132   }
133 }
134 
135 /*******************************************************************************
136  * BTA -> BTIF conversion functions
137  ******************************************************************************/
138 
bta_to_btif_uuid(bt_uuid_t * p_dest,tBT_UUID * p_src)139 void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src) {
140   int i = 0;
141 
142   if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32) {
143     for (i = 0; i != 16; ++i) p_dest->uu[i] = BASE_UUID[i];
144   }
145 
146   switch (p_src->len) {
147     case 0:
148       break;
149 
150     case LEN_UUID_16:
151       p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
152       p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
153       break;
154 
155     case LEN_UUID_32:
156       p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
157       p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
158       p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
159       p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
160       break;
161 
162     case LEN_UUID_128:
163       for (i = 0; i != 16; ++i) p_dest->uu[i] = p_src->uu.uuid128[i];
164       break;
165 
166     default:
167       LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_src->len);
168       break;
169   }
170 }
171 
172 /*******************************************************************************
173  * Utility functions
174  ******************************************************************************/
175 
get_uuid16(tBT_UUID * p_uuid)176 uint16_t get_uuid16(tBT_UUID* p_uuid) {
177   if (p_uuid->len == LEN_UUID_16) {
178     return p_uuid->uu.uuid16;
179   } else if (p_uuid->len == LEN_UUID_128) {
180     uint16_t u16;
181     uint8_t* p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
182     STREAM_TO_UINT16(u16, p);
183     return u16;
184   } else /* p_uuid->len == LEN_UUID_32 */
185   {
186     return (uint16_t)p_uuid->uu.uuid32;
187   }
188 }
189 
set_read_value(btgatt_read_params_t * p_dest,tBTA_GATTC_READ * p_src)190 uint16_t set_read_value(btgatt_read_params_t* p_dest, tBTA_GATTC_READ* p_src) {
191   uint16_t len = 0;
192 
193   p_dest->status = p_src->status;
194   p_dest->handle = p_src->handle;
195 
196   if ((p_src->status == BTA_GATT_OK) && (p_src->len != 0)) {
197     LOG_INFO(LOG_TAG, "%s len = %d ", __func__, p_src->len);
198     p_dest->value.len = p_src->len;
199     memcpy(p_dest->value.value, p_src->value, p_src->len);
200 
201     len += p_src->len;
202   } else {
203     p_dest->value.len = 0;
204   }
205 
206   p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE;
207   return len;
208 }
209 
210 /*******************************************************************************
211  * Encrypted link map handling
212  ******************************************************************************/
213 
214 #if (BLE_DELAY_REQUEST_ENC == FALSE)
btif_gatt_is_link_encrypted(BD_ADDR bd_addr)215 static bool btif_gatt_is_link_encrypted(BD_ADDR bd_addr) {
216   if (bd_addr == NULL) return false;
217 
218   return BTA_JvIsEncrypted(bd_addr);
219 }
220 
btif_gatt_set_encryption_cb(UNUSED_ATTR BD_ADDR bd_addr,UNUSED_ATTR tBTA_TRANSPORT transport,tBTA_STATUS result)221 static void btif_gatt_set_encryption_cb(UNUSED_ATTR BD_ADDR bd_addr,
222                                         UNUSED_ATTR tBTA_TRANSPORT transport,
223                                         tBTA_STATUS result) {
224   if (result != BTA_SUCCESS && result != BTA_BUSY) {
225     BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result);
226   }
227 }
228 #endif
229 
230 #if (BLE_DELAY_REQUEST_ENC == FALSE)
btif_gatt_check_encrypted_link(BD_ADDR bd_addr,tBTA_GATT_TRANSPORT transport_link)231 void btif_gatt_check_encrypted_link(BD_ADDR bd_addr,
232                                     tBTA_GATT_TRANSPORT transport_link) {
233   char buf[100];
234 
235   bt_bdaddr_t bda;
236   bdcpy(bda.address, bd_addr);
237 
238   if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC, buf,
239                                         sizeof(tBTM_LE_PENC_KEYS)) ==
240        BT_STATUS_SUCCESS) &&
241       !btif_gatt_is_link_encrypted(bd_addr)) {
242     BTIF_TRACE_DEBUG("%s: transport = %d", __func__, transport_link);
243     BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb,
244                         BTM_BLE_SEC_ENCRYPT);
245   }
246 }
247 #else
btif_gatt_check_encrypted_link(UNUSED_ATTR BD_ADDR bd_addr,UNUSED_ATTR tBTA_GATT_TRANSPORT transport_link)248 void btif_gatt_check_encrypted_link(UNUSED_ATTR BD_ADDR bd_addr,
249                                     UNUSED_ATTR tBTA_GATT_TRANSPORT
250                                         transport_link) {}
251 #endif
252 
btif_gatt_move_track_adv_data(btgatt_track_adv_info_t * p_dest,btgatt_track_adv_info_t * p_src)253 void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest,
254                                    btgatt_track_adv_info_t* p_src) {
255   memset(p_dest, 0, sizeof(btgatt_track_adv_info_t));
256 
257   memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t));
258 
259   if (p_src->adv_pkt_len > 0) {
260     p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len);
261     memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, p_src->adv_pkt_len);
262     osi_free_and_reset((void**)&p_src->p_adv_pkt_data);
263   }
264 
265   if (p_src->scan_rsp_len > 0) {
266     p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len);
267     memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data,
268            p_src->scan_rsp_len);
269     osi_free_and_reset((void**)&p_src->p_scan_rsp_data);
270   }
271 }
272