btm_ble_addr.c revision ad2d45b15aae80ba254277c3d1fa036207d8b926
1/*****************************************************************************
2**
3**  Name:          btm_ble_addr.c
4**
5**  Description:   This file contains functions for BLE address management.
6**
7**
8**
9**  Copyright (c) 1999-2010, Broadcom Corp., All Rights Reserved.
10**  WIDCOMM Bluetooth Core. Proprietary and confidential.
11******************************************************************************/
12
13#include <string.h>
14
15#include "bt_types.h"
16#include "hcimsgs.h"
17#include "btu.h"
18#include "btm_int.h"
19
20
21#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
22    #include "smp_api.h"
23    #define BTM_BLE_PRIVATE_ADDR_INT    900             /* 15 minutes minimum for random address refreshing */
24
25/*******************************************************************************
26**
27** Function         btm_gen_resolve_paddr_cmpl
28**
29** Description      This is callback functioin when resolvable private address
30**                  generation is complete.
31**
32** Returns          void
33**
34*******************************************************************************/
35static void btm_gen_resolve_paddr_cmpl(tSMP_ENC *p)
36{
37    tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
38    tBTM_BLE_INQ_CB  *p_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
39    BTM_TRACE_EVENT0 ("btm_gen_resolve_paddr_cmpl");
40    if (p && p->param_buf)
41    {
42        /* get the high bytes of the random address */
43        p_cb->private_addr[2] = p->param_buf[0];
44        p_cb->private_addr[1] = p->param_buf[1];
45        p_cb->private_addr[0] = p->param_buf[2];
46        /* mask off the 1st MSB */
47        p_cb->private_addr[0] &= 0xfe;
48        /* set the 2nd MSB to be 1 */
49        p_cb->private_addr[0] |= 0x02;
50        /* set it to controller */
51        btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
52
53        p_inq_cb->own_addr_type = BLE_ADDR_RANDOM;
54
55        /* start a periodical timer to refresh random addr */
56        btu_stop_timer(&p_cb->raddr_timer_ent);
57        btu_start_timer (&p_cb->raddr_timer_ent, BTU_TTYPE_BLE_RANDOM_ADDR,
58                         BTM_BLE_PRIVATE_ADDR_INT);
59
60        /* if adv is active, restart adv with new private addr */
61        if (p_inq_cb->adv_mode == BTM_BLE_ADV_ENABLE)
62        {
63            btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE);
64
65            btsnd_hcic_ble_write_adv_params (p_inq_cb->adv_interval_min,
66                                             p_inq_cb->adv_interval_max,
67                                             p_inq_cb->evt_type,
68                                             p_inq_cb->own_addr_type,
69                                             p_inq_cb->direct_bda.type,
70                                             p_inq_cb->direct_bda.bda,
71                                             p_inq_cb->adv_chnl_map,
72                                             p_inq_cb->afp);
73        }
74    }
75    else
76    {
77        /* random address set failure */
78        BTM_TRACE_DEBUG0("set random address failed");
79    }
80}
81/*******************************************************************************
82**
83** Function         btm_gen_resolve_paddr_low
84**
85** Description      This function is called when random address has generate the
86**                  random number base for low 3 byte bd address.
87**
88** Returns          void
89**
90*******************************************************************************/
91static void btm_gen_resolve_paddr_low(tBTM_RAND_ENC *p)
92{
93#if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
94    tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
95    tSMP_ENC    output;
96
97    BTM_TRACE_EVENT0 ("btm_gen_resolve_paddr_low");
98    if (p && p->param_buf)
99    {
100        p_cb->private_addr[5] = p->param_buf[0];
101        p_cb->private_addr[4] = p->param_buf[1];
102        p_cb->private_addr[3] = p->param_buf[2];
103
104        /* encrypt with ur IRK */
105        if (!SMP_Encrypt(btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN, p->param_buf, 3, &output))
106        {
107            btm_gen_resolve_paddr_cmpl(NULL);
108        }
109        else
110        {
111            btm_gen_resolve_paddr_cmpl(&output);
112        }
113    }
114#endif
115}
116/*******************************************************************************
117**
118** Function         btm_gen_resolvable_private_addr
119**
120** Description      This function generate a resolvable private address.
121**
122** Returns          void
123**
124*******************************************************************************/
125void btm_gen_resolvable_private_addr (void)
126{
127    BTM_TRACE_EVENT0 ("btm_gen_resolvable_private_addr");
128    /* generate 3B rand as BD LSB, SRK with it, get BD MSB */
129    if (!btsnd_hcic_ble_rand((void *)btm_gen_resolve_paddr_low))
130        btm_gen_resolve_paddr_cmpl(NULL);
131}
132/*******************************************************************************
133**
134** Function         btm_gen_non_resolve_paddr_cmpl
135**
136** Description      This is the callback function when non-resolvable private
137**                  function is generated and write to controller.
138**
139** Returns          void
140**
141*******************************************************************************/
142static void btm_gen_non_resolve_paddr_cmpl(tBTM_RAND_ENC *p)
143{
144    tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
145    UINT8   *pp;
146    BTM_TRACE_EVENT0 ("btm_gen_non_resolve_paddr_cmpl");
147    if (p && p->param_buf)
148    {
149        pp = p->param_buf;
150        STREAM_TO_BDADDR(p_cb->private_addr, pp);
151        /* mask off the 2 MSB */
152        p_cb->private_addr[0] &= 0xfc;
153        /* write to controller */
154        btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
155
156        btm_cb.ble_ctr_cb.inq_var.own_addr_type = BLE_ADDR_RANDOM;
157    }
158    else
159    {
160        BTM_TRACE_DEBUG0("btm_gen_non_resolvable_private_addr failed");
161    }
162}
163/*******************************************************************************
164**
165** Function         btm_gen_non_resolvable_private_addr
166**
167** Description      This function generate a non-resolvable private address.
168**
169**
170** Returns          void
171**
172*******************************************************************************/
173void btm_gen_non_resolvable_private_addr (void)
174{
175    BTM_TRACE_EVENT0 ("btm_gen_non_resolvable_private_addr");
176    if (!btsnd_hcic_ble_rand((void *)btm_gen_non_resolve_paddr_cmpl))
177    {
178        btm_gen_non_resolve_paddr_cmpl(NULL);
179    }
180}
181    #if SMP_INCLUDED == TRUE
182/*******************************************************************************
183**  Utility functions for Random address resolving
184*******************************************************************************/
185/*******************************************************************************
186**
187** Function         btm_ble_resolve_address_cmpl
188**
189** Description      This function sends the random address resolving complete
190**                  callback.
191**
192** Returns          None.
193**
194*******************************************************************************/
195static void btm_ble_resolve_address_cmpl(void)
196{
197    tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
198    tBTM_SEC_DEV_REC    *p_dev_rec = NULL;
199
200    BTM_TRACE_EVENT0 ("btm_ble_resolve_address_cmpl");
201    if (p_mgnt_cb->index < BTM_SEC_MAX_DEVICE_RECORDS)
202        p_dev_rec = &btm_cb.sec_dev_rec[p_mgnt_cb->index];
203
204    p_mgnt_cb->busy = FALSE;
205
206    (* p_mgnt_cb->p_resolve_cback)(p_dev_rec, p_mgnt_cb->p);
207}
208/*******************************************************************************
209**
210** Function         btm_ble_proc_resolve_x
211**
212** Description      This function compares the X with random address 3 MSO bytes
213**                  to find a match, if not match, continue for next record.
214**
215** Returns          None.
216**
217*******************************************************************************/
218static BOOLEAN btm_ble_proc_resolve_x(tSMP_ENC *p)
219{
220    tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
221    UINT8    comp[3];
222    BTM_TRACE_EVENT0 ("btm_ble_proc_resolve_x");
223    /* compare the hash with 3 LSB of bd address */
224    comp[0] = p_mgnt_cb->random_bda[5];
225    comp[1] = p_mgnt_cb->random_bda[4];
226    comp[2] = p_mgnt_cb->random_bda[3];
227
228    if (p && p->param_buf)
229    {
230        if (!memcmp(p->param_buf, &comp[0], 3))
231        {
232            /* match is found */
233            BTM_TRACE_EVENT0 ("match is found");
234            btm_ble_resolve_address_cmpl();
235            return TRUE;
236        }
237    }
238    return FALSE;
239}
240/*******************************************************************************
241**
242** Function         btm_ble_match_random_bda
243**
244** Description      This function match the random address to the appointed device
245**                  record, starting from calculating IRK. If record index exceed
246**                  the maximum record number, matching failed and send callback.
247**
248** Returns          None.
249**
250*******************************************************************************/
251static BOOLEAN btm_ble_match_random_bda(UINT16 rec_index)
252{
253#if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
254    tBTM_SEC_DEV_REC    *p_dev_rec;
255    tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
256    UINT8       rand[3];
257    tSMP_ENC    output;
258
259    /* use the 3 MSB of bd address as prand */
260    rand[0] = p_mgnt_cb->random_bda[2];
261    rand[1] = p_mgnt_cb->random_bda[1];
262    rand[2] = p_mgnt_cb->random_bda[0];
263
264    BTM_TRACE_EVENT1("btm_ble_match_random_bda rec_index = %d", rec_index);
265
266    if (rec_index < BTM_SEC_MAX_DEVICE_RECORDS)
267    {
268        p_dev_rec = &btm_cb.sec_dev_rec[rec_index];
269
270        BTM_TRACE_ERROR2("sec_flags = %02x device_type = %d", p_dev_rec->sec_flags, p_dev_rec->device_type);
271
272        if ((p_dev_rec->device_type == BT_DEVICE_TYPE_BLE) &&
273            (p_dev_rec->ble.key_type & BTM_LE_KEY_PID))
274        {
275            /* generate X = E irk(R0, R1, R2) and R is random address 3 LSO */
276            SMP_Encrypt(p_dev_rec->ble.keys.irk, BT_OCTET16_LEN,
277                        &rand[0], 3, &output);
278            return btm_ble_proc_resolve_x(&output);
279        }
280        else
281        {
282            // not completed
283            return FALSE;
284        }
285    }
286    else /* no  match found */
287    {
288        btm_ble_resolve_address_cmpl();
289        return TRUE;
290    }
291#endif
292}
293
294/*******************************************************************************
295**
296** Function         btm_ble_resolve_random_addr
297**
298** Description      This function is called to resolve a random address.
299**
300** Returns          pointer to the security record of the device whom a random
301**                  address is matched to.
302**
303*******************************************************************************/
304void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p)
305{
306    tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
307
308    BTM_TRACE_EVENT0 ("btm_ble_resolve_random_addr");
309    if ( !p_mgnt_cb->busy)
310    {
311        p_mgnt_cb->p = p;
312        p_mgnt_cb->busy = TRUE;
313        p_mgnt_cb->index = 0;
314        p_mgnt_cb->p_resolve_cback = p_cback;
315        memcpy(p_mgnt_cb->random_bda, random_bda, BD_ADDR_LEN);
316        /* start to resolve random address */
317        /* check for next security record */
318        while (TRUE)
319        {
320            if (btm_ble_match_random_bda(p_mgnt_cb->index++))
321            {
322                // match found or went through the list
323                break;
324            }
325        }
326    }
327    else
328        (*p_cback)(NULL, p);
329}
330    #endif
331/*******************************************************************************
332**  address mapping between pseudo address and real connection address
333*******************************************************************************/
334/*******************************************************************************
335**
336** Function         btm_ble_map_bda_to_conn_bda
337**
338** Description      This function map a BD address to the real connection address
339**                  and return the connection address type.
340*******************************************************************************/
341tBLE_ADDR_TYPE btm_ble_map_bda_to_conn_bda(BD_ADDR bd_addr)
342{
343    tBTM_SEC_DEV_REC    *p_dev_rec = NULL;
344    BTM_TRACE_EVENT0 ("btm_ble_map_bda_to_conn_bda");
345    if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL &&
346        p_dev_rec->device_type == BT_DEVICE_TYPE_BLE)
347    {
348        if (p_dev_rec->ble.ble_addr_type != BLE_ADDR_PUBLIC)
349        {
350            memcpy(bd_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
351        }
352        return p_dev_rec->ble.ble_addr_type;
353    }
354    else
355        return BLE_ADDR_PUBLIC;
356}
357/*******************************************************************************
358**
359** Function         btm_ble_map_bda_to_pseudo_bda
360**
361** Description      This function map a BD address to a pseudo address when the
362**                  address given is a random address.
363**
364*******************************************************************************/
365void btm_ble_map_bda_to_pseudo_bda(BD_ADDR bd_addr)
366{
367    BTM_TRACE_EVENT0 ("btm_ble_map_bda_to_pseudo_bda");
368}
369#endif
370
371
372