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 #ifndef _HAL_PPROFILE_UTILS_H 16 #define _HAL_PPROFILE_UTILS_H 17 18 #include "ble_ip_config.h" 19 #if (BLE_SERVER_PRF || BLE_CLIENT_PRF) 20 #include "hal_prf_types.h" 21 #include "hal_gattc_task.h" 22 #include "hal_gapc.h" 23 #include "hal_gapc_task.h" 24 #include "hal_attm.h" 25 #include "hal_profile.h" 26 #endif /* (BLE_SERVER_PRF || BLE_CLIENT_PRF) */ 27 28 /** 29 **************************************************************************************** 30 * @brief Macro used to called the prf_client_get_env function. 31 * 32 * @param prf_id Profile Task ID (In Upper case, ex: HTPC, DISC, ...) 33 * @param type Profile task type (In lower case, ex: htpc, disc, ...) 34 **************************************************************************************** 35 */ 36 #define PRF_ENV_GET(prf_id, type) \ 37 ((struct type ## _env_tag *)prf_env_get((TASK_ID_##prf_id))) 38 39 40 41 // 16 bits attributes in a 128 bits array 42 #define ATT_16_TO_128_ARRAY(uuid) { (uuid) & 0xFF, (uuid >> 8) & 0xFF, 0,0,0,0,0,0,0,0,0,0,0,0,0,0 } 43 // 44 #define ATT_128_PRIMARY_SERVICE ATT_16_TO_128_ARRAY(ATT_DECL_PRIMARY_SERVICE) 45 #define ATT_128_CHARACTERISTIC ATT_16_TO_128_ARRAY(ATT_DECL_CHARACTERISTIC) 46 #define ATT_128_CLIENT_CHAR_CFG ATT_16_TO_128_ARRAY(ATT_DESC_CLIENT_CHAR_CFG) 47 48 49 /* 50 * FUNCTION DECLARATIONS 51 **************************************************************************************** 52 */ 53 54 #if (BLE_BATT_SERVER) 55 /** 56 **************************************************************************************** 57 * @brief Pack Characteristic Presentation Format descriptor value 58 **************************************************************************************** 59 */ 60 void prf_pack_char_pres_fmt(uint8_t *packed_val, const struct prf_char_pres_fmt* char_pres_fmt); 61 #endif // (BLE_BATT_SERVER) 62 63 #if (BLE_BATT_CLIENT) 64 /** 65 **************************************************************************************** 66 * @brief Unpack Characteristic Presentation Format descriptor value 67 **************************************************************************************** 68 */ 69 void prf_unpack_char_pres_fmt(const uint8_t *packed_val, struct prf_char_pres_fmt* char_pres_fmt); 70 #endif // (BLE_BATT_CLIENT) 71 72 #if (BLE_CLIENT_PRF) 73 /** 74 **************************************************************************************** 75 * @brief Request peer device to read an attribute 76 * 77 * @param[in] prf_env Pointer to profile information 78 * 79 * @param conidx Connection index 80 * @param shdl Search Start Handle 81 * @param ehdl Search End Handle 82 * 83 * @param valhdl Value Handle 84 * 85 * @note: if attribute is invalid, nothing is registered 86 **************************************************************************************** 87 */ 88 void prf_read_char_send(prf_env_t *prf_env, uint8_t conidx, 89 uint16_t shdl, uint16_t ehdl, uint16_t valhdl); 90 91 92 /** 93 **************************************************************************************** 94 * @brief register attribute handle in GATT 95 * 96 * @param[in] prf_env Pointer to profile information 97 * 98 * @param conidx Connection index 99 * @param svc Service to register 100 * 101 * @note: if attribute is invalid, nothing is registered 102 **************************************************************************************** 103 */ 104 void prf_register_atthdl2gatt(prf_env_t *prf_env, uint8_t conidx, struct prf_svc *svc); 105 106 /** 107 **************************************************************************************** 108 * @brief Unregister attribute handle in GATT 109 * 110 * @param[in] prf_env Pointer to profile information 111 * 112 * @param conidx Connection index 113 * @param svc Service to register 114 * 115 * @note: if attribute is invalid, nothing is registered 116 **************************************************************************************** 117 */ 118 void prf_unregister_atthdl2gatt(prf_env_t *prf_env, uint8_t conidx, struct prf_svc *svc); 119 120 /** 121 **************************************************************************************** 122 * @brief Request service discovery on peer device. 123 * 124 * This request will be used to retrieve start and end handles of the service. 125 * 126 * @param[in] prf_env Pointer to profile information 127 * 128 * @param conidx Connection index 129 * @param uuid Service UUID 130 **************************************************************************************** 131 */ 132 void prf_disc_svc_send(prf_env_t *prf_env,uint8_t conidx, uint16_t uuid); 133 134 135 /** 136 **************************************************************************************** 137 * @brief Write peer characteristic using GATT. 138 * 139 * It will request write modification of peer handle 140 * 141 * @param[in] prf_env Pointer to profile information 142 * @param[in] conidx Connection index 143 * @param[in] handle Peer handle to modify 144 * @param[in] value New Peer handle value 145 * @param[in] length Value length 146 **************************************************************************************** 147 */ 148 void prf_gatt_write(prf_env_t *prf_env, uint8_t conidx, 149 uint16_t handle, uint8_t* value, uint16_t length, uint8_t operation); 150 151 /** 152 **************************************************************************************** 153 * @brief Modify peer client configuration descriptor using GATT 154 * 155 * It will request write modification of peer client configuration descriptor handle 156 * 157 * @param[in] prf_env Pointer to profile information 158 * 159 * @param[in] conidx Connection index 160 * @param[in] handle Peer client configuration descriptor handle to modify 161 * 162 * @param[in] ntf_ind_cfg Indication/Notification configuration 163 **************************************************************************************** 164 */ 165 void prf_gatt_write_ntf_ind(prf_env_t *prf_env, uint8_t conidx, uint16_t handle, 166 uint16_t ntf_ind_cfg); 167 168 /** 169 **************************************************************************************** 170 * @brief Check service characteristic validity 171 * 172 * For each characteristic in service it verifies handles. 173 * 174 * If some handles are not present, it checks if they shall be present or they are optional. 175 * 176 * @param nb_chars Number of Characteristics in the service 177 * @param chars Characteristics values (char handles, val handles, properties) 178 * @param chars_req Characteristics requirements. 179 * 180 * @return 0x1 if service is valid, 0x00 else. 181 **************************************************************************************** 182 */ 183 uint8_t prf_check_svc_char_validity(uint8_t nb_chars, 184 const struct prf_char_inf* chars, 185 const struct prf_char_def* chars_req); 186 187 /** 188 **************************************************************************************** 189 * @brief Check service characteristic descriptors validity 190 * 191 * For each characteristic descriptors in service it verifies handles. 192 * 193 * If some handles are not present, according to characteristic properties it verify if 194 * descriptor is optional or not. 195 * 196 * @param descs_size Number of Characteristic descriptors in the service 197 * @param descs Characteristic descriptors values (handles) 198 * @param descs_req Characteristics descriptors requirements. 199 * 200 * @return 0x1 if service is valid, 0x00 else. 201 **************************************************************************************** 202 */ 203 uint8_t prf_check_svc_char_desc_validity(uint8_t descs_size, 204 const struct prf_char_desc_inf* descs, 205 const struct prf_char_desc_def* descs_req, 206 const struct prf_char_inf* chars); 207 208 /** 209 **************************************************************************************** 210 * @brief Extract information of the service according to the service description 211 * 212 * @param param Service information 213 * @param nb_chars Length of provided arrays (chars and chars_req) 214 * @param chars_req Characteristics requirements 215 * @param chars Characteristics 216 * @param nb_descs Length of provided arrays (descs and descs_req) 217 * @param descs_req Descriptors requirements 218 * @param descs Descriptors 219 **************************************************************************************** 220 */ 221 void prf_extract_svc_info(const struct gattc_sdp_svc_ind* param, 222 uint8_t nb_chars, const struct prf_char_def* chars_req, struct prf_char_inf* chars, 223 uint8_t nb_descs, const struct prf_char_desc_def* descs_req, struct prf_char_desc_inf* descs); 224 225 #endif //(BLE_CLIENT_PRF) 226 227 228 #if (BLE_CLIENT_PRF || BLE_TIP_SERVER || BLE_AN_SERVER || BLE_PAS_SERVER) 229 230 /** 231 **************************************************************************************** 232 * @brief The function is used to send information about peer attribute value 233 * 234 * @param[in] prf_env Pointer to the profile environment variable 235 * @param[in] conidx Connection index 236 * @param[in] msg_id Profile message ID to trigger 237 * @param[in] status Response status code 238 * @param[in] read_ind GATT read message indication 239 **************************************************************************************** 240 */ 241 void prf_client_att_info_rsp(prf_env_t *prf_env, uint8_t conidx, uint16_t msg_id, 242 uint8_t status, struct gattc_read_ind const* read_ind); 243 244 #endif //(BLE_CLIENT_PRF || BLE_TIP_SERVER || BLE_AN_SERVER || BLE_PAS_SERVER) 245 246 #if (BLE_SERVER_PRF || BLE_CLIENT_PRF) 247 /** 248 **************************************************************************************** 249 * @brief Pack date time value 250 * 251 * @param[out] packed_date packed date time 252 * @param[in] date_time structure date time 253 * 254 * @return size of packed value 255 **************************************************************************************** 256 */ 257 uint8_t prf_pack_date_time(uint8_t *packed_date, const struct prf_date_time* date_time); 258 259 /** 260 **************************************************************************************** 261 * @brief Unpack date time value 262 * 263 * @param[in] packed_date packed date time 264 * @param[out] date_time structure date time 265 * 266 * @return size of packed value 267 **************************************************************************************** 268 */ 269 uint8_t prf_unpack_date_time(uint8_t *packed_date, struct prf_date_time* date_time); 270 271 #endif /* (BLE_SERVER_PRF || BLE_CLIENT_PRF) */ 272 273 #if ((BLE_CGM_SERVER || BLE_CGM_CLIENT)) 274 /** 275 **************************************************************************************** 276 * @brief calculate E2E-CRC according to the profile specification 277 * @param[in] buf Pointer to the byte buffer for the crc calculation. 278 * @param[in] length Length of the buffer for calculation. 279 * @return E2E-CRC according to the specification. 280 **************************************************************************************** 281 */ 282 uint16_t prf_e2e_crc(const uint8_t * buf, uint16_t length); 283 284 #endif /* ((BLE_CGM_SERVER || BLE_CGM_CLIENT)) */ 285 286 #endif // _HAL_PPROFILE_UTILS_H 287