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