1 /* 2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd. 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 __BLUETOOTH_DEVICE_H__ 16 #define __BLUETOOTH_DEVICE_H__ 17 #include "securec.h" 18 #include "blegap.h" 19 #include "esp_gattc_api.h" 20 #define GATTC_TAG "BLUETOOTH_DEMO" 21 22 #define BleGapParam esp_ble_gap_cb_param_t 23 #define BleGattcParam esp_ble_gattc_cb_param_t 24 #define BleScanParams esp_ble_scan_params_t 25 #define BleGattcCharElem esp_gattc_char_elem_t 26 #define BleGattcDescrElem esp_gattc_descr_elem_t 27 #define BtLogBufferHex ESP_LOG_BUFFER_HEX 28 #define BtLogBufferChar esp_log_buffer_char 29 #define ScanTime 30 30 31 typedef enum { 32 OHOS_GATTC_REG_EVT = 0, 33 OHOS_GATTC_UNREG_EVT = 1, 34 OHOS_GATTC_OPEN_EVT = 2, 35 OHOS_GATTC_READ_CHAR_EVT = 3, 36 OHOS_GATTC_WRITE_CHAR_EVT = 4, 37 OHOS_GATTC_CLOSE_EVT = 5, 38 OHOS_GATTC_SEARCH_CMPL_EVT = 6, 39 OHOS_GATTC_SEARCH_RES_EVT = 7, 40 OHOS_GATTC_READ_DESCR_EVT = 8, 41 OHOS_GATTC_WRITE_DESCR_EVT = 9, 42 OHOS_GATTC_NOTIFY_EVT = 10, 43 OHOS_GATTC_PREP_WRITE_EVT = 11, 44 OHOS_GATTC_EXEC_EVT = 12, 45 OHOS_GATTC_ACL_EVT = 13, 46 OHOS_GATTC_CANCEL_OPEN_EVT = 14, 47 OHOS_GATTC_SRVC_CHG_EVT = 15, 48 OHOS_GATTC_ENC_CMPL_CB_EVT = 17, 49 OHOS_GATTC_CFG_MTU_EVT = 18, 50 OHOS_GATTC_ADV_DATA_EVT = 19, 51 OHOS_GATTC_MULT_ADV_ENB_EVT = 20, 52 OHOS_GATTC_MULT_ADV_UPD_EVT = 21, 53 OHOS_GATTC_MULT_ADV_DATA_EVT = 22, 54 OHOS_GATTC_MULT_ADV_DIS_EVT = 23, 55 OHOS_GATTC_CONGEST_EVT = 24, 56 OHOS_GATTC_BTH_SCAN_ENB_EVT = 25, 57 OHOS_GATTC_BTH_SCAN_CFG_EVT = 26, 58 OHOS_GATTC_BTH_SCAN_RD_EVT = 27, 59 OHOS_GATTC_BTH_SCAN_THR_EVT = 28, 60 OHOS_GATTC_BTH_SCAN_PARAM_EVT = 29, 61 OHOS_GATTC_BTH_SCAN_DIS_EVT = 30, 62 OHOS_GATTC_SCAN_FLT_CFG_EVT = 31, 63 OHOS_GATTC_SCAN_FLT_PARAM_EVT = 32, 64 OHOS_GATTC_SCAN_FLT_STATUS_EVT = 33, 65 OHOS_GATTC_ADV_VSC_EVT = 34, 66 OHOS_GATTC_REG_FOR_NOTIFY_EVT = 38, 67 OHOS_GATTC_UNREG_FOR_NOTIFY_EVT = 39, 68 OHOS_GATTC_CONNECT_EVT = 40, 69 OHOS_GATTC_DISCONNECT_EVT = 41, 70 OHOS_GATTC_READ_MULTIPLE_EVT = 42, 71 OHOS_GATTC_QUEUE_FULL_EVT = 43, 72 OHOS_GATTC_SET_ASSOC_EVT = 44, 73 OHOS_GATTC_GET_ADDR_LIST_EVT = 45, 74 OHOS_GATTC_DIS_SRVC_CMPL_EVT = 46, 75 } GattcBleCallbackEvent; 76 77 typedef enum { 78 OHOS_GATT_SERVICE_FROM_REMOTE_DEVICE = 0, 79 OHOS_GATT_SERVICE_FROM_NVS_FLASH = 1, 80 OHOS_GATT_SERVICE_FROM_UNKNOWN = 2, 81 } GattServeiceSource; 82 83 typedef enum { 84 OHOS_GATT_DB_PRIMARY_SERVICE, 85 OHOS_GATT_DB_SECONDARY_SERVICE, 86 OHOS_GATT_DB_CHARACTERISTIC, 87 OHOS_GATT_DB_DESCRIPTOR, 88 OHOS_GATT_DB_INCLUDED_SERVICE, 89 OHOS_GATT_DB_ALL, 90 } GattDbAttrType; 91 92 typedef void (* GattcBleCallback)(GattcBleCallbackEvent event, GattInterfaceType gattc_if, BleGattcParam *param); 93 94 typedef struct { 95 GapBleCallback gap_callback; 96 GattcBleCallback gattc_callback; 97 uint16_t PROFILE_APP_ID; 98 } BtGattClientCallbacks; 99 100 typedef enum { 101 OHOS_GATT_AUTH_REQ_NONE = 0, 102 OHOS_GATT_AUTH_REQ_NO_MITM = 1, 103 OHOS_GATT_AUTH_REQ_MITM = 2, 104 OHOS_GATT_AUTH_REQ_SIGNED_NO_MITM = 3, 105 OHOS_GATT_AUTH_REQ_SIGNED_MITM = 4, 106 } GattBleAuthReq; 107 108 typedef struct { 109 GattInterfaceType gattc_if; 110 uint16_t conn_id; 111 uint16_t char_handle; 112 BtUuids descr_uuid; 113 } GattcGetDescr; 114 115 typedef struct { 116 GattInterfaceType gattc_if; 117 uint16_t conn_id; 118 uint16_t handle; 119 uint16_t value_len; 120 GattBleWriteType write_type; 121 } GattcWriteChar; 122 123 typedef struct { 124 GattInterfaceType gattc_if; 125 uint16_t conn_id; 126 esp_gatt_db_attr_type_t type; 127 uint16_t start_handle; 128 uint16_t end_handle; 129 } GattcGetAttr; 130 131 typedef struct { 132 GattInterfaceType gattc_if; 133 uint16_t conn_id; 134 uint16_t start_handle; 135 uint16_t end_handle; 136 } GattcGetChar; 137 138 BtError BleGattcConnect(int clientId, VOID *func, const BdAddr *bdAddr, bool isAutoConnect, 139 BtTransportType transport); 140 141 BtError BleGattcDisconnect(int clientId, int conn_id); 142 143 BtError BleGattcRegister(BtGattClientCallbacks func); 144 145 BtError BleGattcUnRegister(int clientId); 146 147 BtError BleGattcConfigureMtuSize(int mtuSize); 148 149 BtError BleGattcSearchServices(int clientId, int conn_id, BtUuid *filter_uuid); 150 151 BtError BleGattcWriteCharacteristic(GattcWriteChar write_char, uint8_t *value, 152 GattBleAuthReq auth_req); 153 154 BtError BleGattcSendMtuReq(GattInterfaceType gattc_if, uint16_t conn_id); 155 156 BtError BleGattcGetAttrCount(GattcGetAttr get_attr, 157 uint16_t char_handle, 158 uint16_t *count); 159 160 GattStatus BleGattcGetCharByUuid(GattcGetChar get_char, BtUuids char_uuid, 161 BleGattcCharElem *result, uint16_t *count); 162 163 BtError BleGattcRegisterForNotify(GattInterfaceType gattc_if, 164 BdAddr server_bda, 165 uint16_t handle); 166 167 GattStatus BleGattcGetDescrByCharHandle(GattcGetDescr get_descr, BleGattcDescrElem *result, 168 uint16_t *count); 169 170 BtError BleGattcWriteCharDescr(GattcWriteChar write_char, 171 uint8_t *value, 172 GattAttributePermission auth_req); 173 #endif