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