1 /* 2 * Copyright (C) 2021 Huawei Device 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 16 #ifndef OHOS_BT_GATT_CLIENT_H 17 #define OHOS_BT_GATT_CLIENT_H 18 19 #include "ohos_bt_def.h" 20 21 /* Refer to the definition of "HCI_LE_Connection_Update" in core 5.2|Vol 4,Part E */ 22 typedef struct { 23 int connIntervalMin; /* Minimum value for the connection interval. [N * 1.25ms] */ 24 int connIntervalMax; /* Maximum value for the connection interval. [N * 1.25ms] */ 25 int connLatency; /* Slave latency for the connection in number of connection events. [Range: 0x0000 to 0x01F3] */ 26 int supervisionTimeout; /* Supervision timeout for the LE Link. [N * 10 ms] */ 27 int minConnectionEventLen; /* The minimum length of connection event needed for LE connection. [N * 0.625ms] */ 28 int maxConnectionEventLen; /* [N * 0.625ms] */ 29 } BtGattcConnPara; 30 31 /* notification/indication data */ 32 typedef struct { 33 unsigned short handle; 34 unsigned char isNotify; /* 1: notification, 0: indication */ 35 unsigned short dataLen; 36 unsigned char *data; 37 } BtGattNotifyData; 38 39 /* Parameters for GATT read operations */ 40 typedef struct { 41 unsigned short handle; 42 unsigned short dataLen; 43 unsigned char *data; 44 } BtGattReadData; 45 46 /* Callback invoked in response to BleGattcRegister */ 47 typedef void (*RegisterClientCallback)(int status, int clientId, const BtUuid *appUuid); 48 49 /* Callback invoked in response to BleGattcConnect */ 50 typedef void (*ConnectClientCallback)(int connId, int status, int clientId, const BdAddr *bdAddr); 51 52 /* Callback invoked in response to BleGattcDisconnect */ 53 typedef void (*DisconnectClientCallback)(int connId, int status, int clientId, const BdAddr *bdAddr); 54 55 /* Callback invoked when the connection parameters for a given connection changed */ 56 typedef void (*ConnectParaUpdateCallback)(int connId, int interval, int latency, int timeout, int status); 57 58 /* Invoked in response to BleGattcSearchServices when the GATT service discovery has been completed */ 59 typedef void (*SearchServiceCompleteCallback)(int connId, int status); 60 61 /* Reports result of a GATT read operation */ 62 typedef void (*ReadCharacteristicCallback)(int connId, int status, BtGattReadData *readData); 63 64 /* GATT write characteristic operation callback */ 65 typedef void (*WriteCharacteristicCallback)(int connId, int status, unsigned short handle); 66 67 /* Callback invoked in response to BleGattcReadDescriptor */ 68 typedef void (*ReadDescriptorCallback)(int connId, int status, BtGattReadData *readData); 69 70 /* Callback invoked in response to BleGattcWriteDescriptor */ 71 typedef void (*WriteDescriptorCallback)(int connId, int status, unsigned short handle); 72 73 /* GATT execute prepared write callback */ 74 typedef void (*ExecuteWriteCallback)(int connId, int status); 75 76 /* Callback invoked when the MTU size for a given connection changes */ 77 typedef void (*ConfigureMtuSizeCallback)(int connId, int status, int mtuSize); 78 79 /* Callback invoked in response to BleGattcReadRemoteRssi */ 80 typedef void (*ReadRemoteRssiCallback)(int clientId, const BdAddr *bdAddr, int rssi, int status); 81 82 /* Callback invoked in response to BleGattcRegisterNotifications, register/deregister */ 83 typedef void (*RegisterNotificationCallback)(int connId, int registered, int status, unsigned short handle); 84 85 /* Callback invoked when a remote device sends a notification/indication that a client has registered for */ 86 typedef void (*NotificationCallback)(int connId, BtGattNotifyData notifyData); 87 88 typedef struct { 89 RegisterClientCallback registerClientCb; 90 ConnectClientCallback connectClientCb; 91 DisconnectClientCallback disconnectCb; 92 ConnectParaUpdateCallback connectParaUpdateCb; 93 SearchServiceCompleteCallback searchServiceCompleteCb; 94 ReadCharacteristicCallback readCharacteristicCb; 95 WriteCharacteristicCallback writeCharacteristicCb; 96 ReadDescriptorCallback readDescriptorCb; 97 WriteDescriptorCallback writeDescriptorCb; 98 ExecuteWriteCallback executeWriteCb; 99 ConfigureMtuSizeCallback configureMtuSizeCb; 100 ReadRemoteRssiCallback readRemoteRssiCb; 101 RegisterNotificationCallback registerNotificationCb; 102 NotificationCallback notificationCb; 103 } BtGattClientCallbacks; 104 105 /* 106 * @brief gatt client register, callback return clientId 107 * @param[in] <appUuid> specified by upper layer 108 * @return 0-success, other-fail 109 */ 110 int BleGattcRegister( BtUuid appUuid); 111 112 /* 113 * @brief gatt client deregister 114 * @param[in] <clientId> client Id 115 * @return 0-success, other-fail 116 */ 117 int BleGattcUnRegister(int clientId); 118 119 /* 120 * @brief Create a connection to a remote LE or dual-mode device 121 * @param[in] <clientId> client Id 122 * @param[in] <bdAddr> remote address 123 * @param[in] <isDirect> is a direct connection or a background auto connection 124 * @param[in] <transport> BtTransportId 125 * @return 0-success, other-fail 126 */ 127 int BleGattcConnect(int clientId, const BdAddr *bdAddr, bool isDirect, int transport); 128 129 /* 130 * @brief Disconnect a remote device or cancel a pending connection 131 * @param[in] <clientId> client Id 132 * @param[in] <bdAddr> remote address 133 * @param[in] <connId> connection index. 134 * @return 0-success, other-fail 135 */ 136 int BleGattcDisconnect(int clientId, const BdAddr *bdAddr, int connId); 137 138 /* 139 * @brief Send a connection parameter update request to the remote device. 140 * @param[in] <bdAddr> remote address 141 * @param[in] <BtGattcConnPara> connection param refer to "HCI_LE_Connection_Update". 142 * @return 0-success, other-fail 143 */ 144 int BleGattcConnectParaUpdate(const BdAddr *bdAddr, BtGattcConnPara connPara); 145 146 /* 147 * @brief This function is called to request a GATT service discovery on a GATT server. 148 Optionally, the results can be filtered for a given UUID. 149 * @param[in] <bdAddr> remote address 150 * @param[in] <filterUuid> a UUID of the service application is interested in. If Null, discover for all services 151 * @return 0-success, other-fail 152 */ 153 int BleGattcSearchServices(int connId, BtUuid filterUuid); 154 155 /* 156 * @brief This function is called to read a characteristics value from the server. 157 * @param[in] <connId> connection ID 158 * @param[in] <handle> characteritic handle to read 159 * @return 0-success, other-fail 160 */ 161 int BleGattcReadCharacteristic(int connId, int handle); 162 163 /* 164 * @brief This function is called to write a characteristics value to the server. 165 * @param[in] <connId> connection ID 166 * @param[in] <handle> characteritic handle to read 167 * @param[in] <writeType> BtGattWriteType, default: write need rsp 168 * @param[in] <len> the data length 169 * @param[in] <value> the data to be writen 170 * @return 0-success, other-fail 171 */ 172 int BleGattcWriteCharacteristic(int connId, int handle, int writeType, int len, char *value); 173 174 /* 175 * @brief This function is called to read a characteristics value from the server. 176 * @param[in] <connId> connection ID 177 * @param[in] <handle> descriptor handle to read 178 * @return 0-success, other-fail 179 */ 180 int BleGattcReadDescriptor(int connId, int handle); 181 182 /* 183 * @brief This function is called to write a descriptor value to the server. 184 * @param[in] <connId> connection ID 185 * @param[in] <handle> descriptor handle to read 186 * @param[in] <writeType> BtGattWriteType, default: write need rsp 187 * @param[in] <len> the data length 188 * @param[in] <value> the data to be writen 189 * @return 0-success, other-fail 190 */ 191 int BleGattcWriteDescriptor(int connId, int handle, int writeType, int len, char *value); 192 193 /* 194 * @brief This function is called to send an execute write request to the server(or cancel the prepare write). 195 * @param[in] <connId> connection ID 196 * @param[in] <execute> [1-execute, 0-cancel], to execute or cancel the prepare write request(s). 197 * @return 0-success, other-fail 198 */ 199 int BleGattcExecuteWrite(int connId, int execute); 200 201 /* 202 * @brief This function is called to configure the ATT MTU size for a connection on an LE transport. 203 * @param[in] <connId> connection ID 204 * @param[in] <mtuSize> attribute MTU size. 205 * @return 0-success, other-fail 206 */ 207 int BleGattcConfigureMtuSize(int connId, int mtuSize); 208 209 /* 210 * @brief Read the RSSI for a connected remote device. 211 * @param[in] <clientId> client Id 212 * @param[in] <bdAddr> remote address 213 * @return 0-success, other-fail 214 */ 215 int BleGattcReadRemoteRssi(int clientId, const BdAddr *bdAddr); 216 217 /* 218 * @brief Enable or disable notifications/indications for a given characteristic. 219 * @param[in] <clientId> client Id 220 * @param[in] <bdAddr> remote address 221 * @param[in] <handle> characteristic handle 222 * @param[in] <enable> 1-register, 0-deregister 223 * @return 0-success, other-fail 224 */ 225 int BleGattcRegisterNotifications(int clientId, const BdAddr *bdAddr, int handle, int enable); 226 227 /* 228 * @brief Callback invoked for gatt client function 229 * @param[in] <BtGattClientCallbacks> Callback funcs 230 * @return 0-success, other-fail 231 */ 232 int BleGattcRegisterCallbacks(BtGattClientCallbacks *func); 233 #endif 234