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 <assert.h>
21 #include <stdlib.h>
22 #include "os/os.h"
23 #include "ble_hs_priv.h"
24
25 const uint8_t ble_hs_misc_null_addr[6];
26
ble_hs_misc_conn_chan_find(uint16_t conn_handle,uint16_t cid,struct ble_hs_conn ** out_conn,struct ble_l2cap_chan ** out_chan)27 int ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
28 struct ble_hs_conn **out_conn,
29 struct ble_l2cap_chan **out_chan)
30 {
31 struct ble_l2cap_chan *chan;
32 struct ble_hs_conn *conn;
33 int rc;
34 conn = ble_hs_conn_find(conn_handle);
35 if (conn == NULL) {
36 chan = NULL;
37 rc = BLE_HS_ENOTCONN;
38 } else {
39 chan = ble_hs_conn_chan_find_by_scid(conn, cid);
40 if (chan == NULL) {
41 rc = BLE_HS_ENOTCONN;
42 } else {
43 rc = 0;
44 }
45 }
46
47 if (out_conn != NULL) {
48 *out_conn = conn;
49 }
50
51 if (out_chan != NULL) {
52 *out_chan = chan;
53 }
54
55 return rc;
56 }
57
ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle,uint16_t cid,struct ble_hs_conn ** out_conn,struct ble_l2cap_chan ** out_chan)58 void ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
59 struct ble_hs_conn **out_conn,
60 struct ble_l2cap_chan **out_chan)
61 {
62 struct ble_l2cap_chan *chan;
63 struct ble_hs_conn *conn;
64 int rc;
65 rc = ble_hs_misc_conn_chan_find(conn_handle, cid, &conn, &chan);
66 BLE_HS_DBG_ASSERT_EVAL(rc == 0);
67
68 if (out_conn != NULL) {
69 *out_conn = conn;
70 }
71
72 if (out_chan != NULL) {
73 *out_chan = chan;
74 }
75 }
76
ble_hs_misc_own_addr_type_to_id(uint8_t addr_type)77 uint8_t ble_hs_misc_own_addr_type_to_id(uint8_t addr_type)
78 {
79 switch (addr_type) {
80 case BLE_OWN_ADDR_PUBLIC:
81 case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT:
82 return BLE_ADDR_PUBLIC;
83
84 case BLE_OWN_ADDR_RANDOM:
85 case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
86 return BLE_ADDR_RANDOM;
87
88 default:
89 BLE_HS_DBG_ASSERT(0);
90 return BLE_ADDR_PUBLIC;
91 }
92 }
93
ble_hs_misc_peer_addr_type_to_id(uint8_t addr_type)94 uint8_t ble_hs_misc_peer_addr_type_to_id(uint8_t addr_type)
95 {
96 switch (addr_type) {
97 case BLE_ADDR_PUBLIC:
98 case BLE_ADDR_PUBLIC_ID:
99 return BLE_ADDR_PUBLIC;
100
101 case BLE_ADDR_RANDOM:
102 case BLE_ADDR_RANDOM_ID:
103 return BLE_ADDR_RANDOM;
104
105 default:
106 BLE_HS_DBG_ASSERT(0);
107 return BLE_ADDR_PUBLIC;
108 }
109 }
110
ble_hs_misc_restore_one_irk(int obj_type,union ble_store_value * val,void * cookie)111 static int ble_hs_misc_restore_one_irk(int obj_type, union ble_store_value *val, void *cookie)
112 {
113 const struct ble_store_value_sec *sec;
114 BLE_HS_DBG_ASSERT(obj_type == BLE_STORE_OBJ_TYPE_PEER_SEC);
115 sec = &val->sec;
116 if (sec->irk_present) {
117 int rc = ble_hs_pvcy_add_entry(sec->peer_addr.val, sec->peer_addr.type, sec->irk);
118 if (rc != 0) {
119 BLE_HS_LOG(ERROR, "failed to configure restored IRK\n");
120 }
121 }
122 return 0;
123 }
124
ble_hs_misc_restore_irks(void)125 int ble_hs_misc_restore_irks(void)
126 {
127 int rc;
128 rc = ble_store_iterate(BLE_STORE_OBJ_TYPE_PEER_SEC,
129 ble_hs_misc_restore_one_irk,
130 NULL);
131 return rc;
132 }