1 /* 2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef _HAL_GAPC_H 17 #define _HAL_GAPC_H 18 19 #include "ble_ip_config.h" 20 21 #if (BLE_GAPC) 22 23 #include "ble_ke_task.h" 24 #include "hal_gap.h" 25 #include "hal_smpc.h" 26 27 /// Operation type 28 enum gapc_op_type 29 { 30 /// Operation used to manage Link (update params, get peer info) 31 GAPC_OP_LINK_INFO = 0x00, 32 33 /// Operation used to manage SMP 34 GAPC_OP_SMP = 0x01, 35 36 /// Operation used to manage connection update 37 GAPC_OP_LINK_UPD = 0x02, 38 39 /// Max number of operations 40 GAPC_OP_MAX 41 }; 42 43 /// Link security status. This status represents the authentication/authorization/bonding levels of the connection 44 enum gapc_lk_sec_req 45 { 46 /// Link is bonded 47 GAPC_LK_BONDED, 48 /// Link is Encrypted 49 GAPC_LK_ENCRYPTED, 50 /// Link LTK Exchanged during pairing 51 GAPC_LK_LTK_PRESENT, 52 }; 53 54 /* 55 * TYPE DEFINITIONS 56 **************************************************************************************** 57 */ 58 59 /// GAP controller environment variable structure. 60 struct gapc_env_tag 61 { 62 /// Request operation Kernel message 63 void* operation[GAPC_OP_MAX]; 64 /// Source task id of requested disconnection 65 ke_task_id_t disc_requester; 66 /// Destination task ID for asynchronous events not linked to an operation 67 ke_task_id_t dest_task_id; 68 69 /* Connection parameters to keep */ 70 71 /// Security Management Protocol environment variables 72 struct smpc_env smpc; 73 74 /// connection handle 75 uint16_t conhdl; 76 77 /// Configuration fields (@see enum gapc_fields) 78 uint8_t fields; 79 80 /// BD Address used for the link that should be kept 81 struct gap_bdaddr src[SMPC_INFO_MAX]; 82 83 /// Relevant information of peer LE features 8-byte array 84 uint8_t features; 85 /// Channel Selection Algorithm 86 uint8_t chan_sel_algo; 87 }; 88 89 90 91 /* 92 * MACROS 93 **************************************************************************************** 94 */ 95 96 /* 97 * FUNCTION DECLARATIONS 98 **************************************************************************************** 99 */ 100 101 /** 102 **************************************************************************************** 103 * @brief Retrieve connection index from connection handle. 104 * 105 * @param[in] conhdl Connection handle 106 * 107 * @return Return found connection index, GAP_INVALID_CONIDX if not found. 108 **************************************************************************************** 109 */ 110 uint8_t gapc_get_conidx(uint16_t conhdl); 111 112 /** 113 **************************************************************************************** 114 * @brief Retrieve connection handle from connection index. 115 * 116 * @param[in] conidx Connection index 117 * 118 * @return Return found connection handle, GAP_INVALID_CONHDL if not found. 119 **************************************************************************************** 120 */ 121 uint16_t gapc_get_conhdl(uint8_t conidx); 122 123 /** 124 **************************************************************************************** 125 * @brief Retrieve connection role from connection index. 126 * 127 * @param[in] conidx Connection index 128 * 129 * @return Return found connection role 130 **************************************************************************************** 131 */ 132 uint8_t gapc_get_role(uint8_t conidx); 133 134 /** 135 **************************************************************************************** 136 * @brief Set resolvable address used for connection establishment as local address. 137 * 138 * @brief conidx Connection index 139 * @brief p_addr Pointer to the resolvable address used by either advertising activity or 140 * resolvable activity. 141 **************************************************************************************** 142 */ 143 void gapc_set_local_addr(uint8_t conidx, uint8_t *p_addr); 144 145 /** 146 **************************************************************************************** 147 * @brief Retrieve connection address information on current link. 148 * 149 * @param[in] conidx Connection index 150 * @param[in] src Connection information source 151 * 152 * @return Return found connection address 153 **************************************************************************************** 154 */ 155 struct gap_bdaddr* gapc_get_bdaddr(uint8_t conidx, uint8_t src); 156 157 /** 158 **************************************************************************************** 159 * @brief Get destination task id for asynchronous event, meaning events that are not 160 * linked to an operation. 161 * Note the provided connection index shall be valid (gapc_env[conidx] is not NULL) 162 * 163 * @param[in] conidx Connection Index 164 * 165 * @return ID of the destination task. 166 **************************************************************************************** 167 */ 168 ke_task_id_t gapc_get_dest_task(uint8_t conidx); 169 170 /** 171 **************************************************************************************** 172 * @brief Check if current link support security requirements. 173 * 174 * @param[in] conidx Connection index 175 * @param[in] sec_req Link security requirement to test 176 * 177 * @return True if link requirement is supported, False else. 178 **************************************************************************************** 179 */ 180 bool gapc_is_sec_set(uint8_t conidx, uint8_t sec_req); 181 182 /** 183 **************************************************************************************** 184 * @brief Retrieve Link Security level 185 * 186 * @param[in] conidx Connection index 187 * 188 * @return Link Security level. 189 **************************************************************************************** 190 */ 191 uint8_t gapc_lk_sec_lvl_get(uint8_t conidx); 192 193 /** 194 **************************************************************************************** 195 * @brief Retrieve the encryption key size of the connection 196 * 197 * @param[in] conidx Connection index 198 * 199 * @return encryption key size (size is 7 - 16 byte range) 200 * 201 **************************************************************************************** 202 */ 203 uint8_t gapc_enc_keysize_get(uint8_t conidx); 204 205 /** 206 **************************************************************************************** 207 * @brief Get Service Change Client Configuration 208 * 209 * @param[in] conidx Connection index 210 * 211 * @return Service Change Client Configuration 212 **************************************************************************************** 213 */ 214 bool gapc_svc_chg_ccc_get(uint8_t conidx); 215 216 /** 217 **************************************************************************************** 218 * @brief Set Service Change Client Configuration 219 * 220 * @param[in] conidx Connection index 221 * @param[in] enable True if CCC is enabled, False else 222 * 223 **************************************************************************************** 224 */ 225 void gapc_svc_chg_ccc_set(uint8_t conidx, bool enable); 226 227 /** 228 **************************************************************************************** 229 * Retrieve if current connection index is used for a discovery purpose such as 230 * Name discovery 231 * 232 * @param conidx Index of the specific connection 233 * 234 * @return true if connection has a discovery purpose, False else 235 **************************************************************************************** 236 */ 237 bool gapc_is_disc_connection(uint8_t conidx); 238 239 #endif // (BLE_GAPC) 240 241 #endif // _HAL_GAPC_H 242