xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/util/src/addr.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "host/ble_hs.h"
21 #include "host/util/util.h"
22 
23 #if MYNEWT_VAL(BLE_DEVICE)
24 #include "controller/ble_hw.h"
25 #endif
26 
27 static int
ble_hs_util_load_rand_addr(ble_addr_t * addr)28 ble_hs_util_load_rand_addr(ble_addr_t *addr)
29 {
30     /* XXX: It is unfortunate that the function to retrieve the random address
31      * is in the controller package.  A host-only device ought to be able to
32      * automically restore a random address.
33      */
34 #if MYNEWT_VAL(BLE_DEVICE)
35     int rc;
36 
37     rc = ble_hw_get_static_addr(addr);
38     if (rc == 0) {
39         return 0;
40     }
41 #endif
42 
43     return BLE_HS_ENOADDR;
44 }
45 
46 static int
ble_hs_util_ensure_rand_addr(void)47 ble_hs_util_ensure_rand_addr(void)
48 {
49     ble_addr_t addr;
50     int rc;
51 
52     /* If we already have a random address, then we are done. */
53     rc = ble_hs_id_copy_addr(BLE_ADDR_RANDOM, NULL, NULL);
54     if (rc == 0) {
55         return 0;
56     }
57 
58     /* Otherwise, try to load a random address. */
59     rc = ble_hs_util_load_rand_addr(&addr);
60     if (rc != 0) {
61         return rc;
62     }
63 
64     /* Configure nimble to use the random address. */
65     rc = ble_hs_id_set_rnd(addr.val);
66     if (rc != 0) {
67         return rc;
68     }
69 
70     return 0;
71 }
72 
73 int
ble_hs_util_ensure_addr(int prefer_random)74 ble_hs_util_ensure_addr(int prefer_random)
75 {
76     int rc;
77 
78     if (prefer_random) {
79         /* Try to load a random address. */
80         rc = ble_hs_util_ensure_rand_addr();
81         if (rc == BLE_HS_ENOADDR) {
82             /* No random address; try to load a public address. */
83             rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, NULL, NULL);
84         }
85     } else {
86         /* Try to load a public address. */
87         rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, NULL, NULL);
88         if (rc == BLE_HS_ENOADDR) {
89             /* No public address; try to load a random address. */
90             rc = ble_hs_util_ensure_rand_addr();
91         }
92     }
93 
94     return rc;
95 }
96