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 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions, 21 * including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc. 22 * 23 * @since 6 24 * 25 */ 26 27 /** 28 * @file bluetooth_gatt_client.h 29 * 30 * @brief Bluetooth gatt client interface. 31 * 32 * @since 6 33 * 34 */ 35 36 #ifndef BLUETOOTH_GATT_CLIENT_H 37 #define BLUETOOTH_GATT_CLIENT_H 38 39 #include "bluetooth_def.h" 40 #include "bluetooth_gatt_service.h" 41 #include "bluetooth_remote_device.h" 42 43 namespace OHOS { 44 namespace Bluetooth { 45 /** 46 * @brief Class for GattClientCallback functions. 47 * 48 * @since 6 49 * 50 */ 51 class GattClientCallback { 52 public: 53 /** 54 * @brief The function to OnConnectionStateChanged. 55 * 56 * @param connectionState callback of gattClientCallback. 57 * @param ret ret of GattClientCallback. 58 * @since 6 59 * 60 */ 61 virtual void OnConnectionStateChanged(int connectionState, int ret) = 0; 62 63 /** 64 * @brief The function to OnCharacteristicChanged. 65 * 66 * @param characteristic Characteristic object to changed. 67 * @since 6 68 * 69 */ OnCharacteristicChanged(const GattCharacteristic & characteristic)70 virtual void OnCharacteristicChanged(const GattCharacteristic &characteristic) 71 {} 72 73 /** 74 * @brief The function to OnCharacteristicReadResult. 75 * 76 * @param characteristic Characteristic object. 77 * @param ret ret of GattClientCallback. 78 * @since 6 79 * 80 */ OnCharacteristicReadResult(const GattCharacteristic & characteristic,int ret)81 virtual void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) 82 {} 83 84 /** 85 * @brief The function to OnCharacteristicWriteResult. 86 * 87 * @param characteristic Characteristic object. 88 * @param ret ret of GattClientCallback. 89 * @since 6 90 * 91 */ OnCharacteristicWriteResult(const GattCharacteristic & characteristic,int ret)92 virtual void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) 93 {} 94 95 /** 96 * @brief The function to OnDescriptorReadResult. 97 * 98 * @param descriptor descriptor object. 99 * @param ret ret of GattClientCallback. 100 * @since 6 101 * 102 */ OnDescriptorReadResult(const GattDescriptor & descriptor,int ret)103 virtual void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) 104 {} 105 106 /** 107 * @brief The function to OnDescriptorWriteResult. 108 * 109 * @param descriptor descriptor object. 110 * @param ret ret of GattClientCallback. 111 * @since 6 112 * 113 */ OnDescriptorWriteResult(const GattDescriptor & descriptor,int ret)114 virtual void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) 115 {} 116 117 /** 118 * @brief The function to OnMtuUpdate. 119 * 120 * @param mtu mtu to update. 121 * @param ret ret of GattClientCallback. 122 * @since 6 123 * 124 */ OnMtuUpdate(int mtu,int ret)125 virtual void OnMtuUpdate(int mtu, int ret) 126 {} 127 128 /** 129 * @brief The function to OnServicesDiscovered. 130 * 131 * @param status Status object. 132 * @since 6 133 * 134 */ OnServicesDiscovered(int status)135 virtual void OnServicesDiscovered(int status) 136 {} 137 138 /** 139 * @brief The function to OnConnectionParameterChanged. 140 * 141 * @param interval interval object. 142 * @param latency latency object. 143 * @param timeout timeout object. 144 * @param status status object. 145 * @since 6 146 * 147 */ OnConnectionParameterChanged(int interval,int latency,int timeout,int status)148 virtual void OnConnectionParameterChanged(int interval, int latency, int timeout, int status) 149 {} 150 151 /** 152 * @brief The function to OnSetNotifyCharacteristic. 153 * 154 * @param status status object. 155 * @since 6 156 * 157 */ OnSetNotifyCharacteristic(int status)158 virtual void OnSetNotifyCharacteristic(int status) 159 {} 160 161 /** 162 * @brief A destructor of GattClientCallback. 163 * 164 * @since 6 165 * 166 */ ~GattClientCallback()167 virtual ~GattClientCallback() 168 {} 169 }; 170 171 /** 172 * @brief Class for GattClient functions. 173 * 174 * @since 6 175 * 176 */ 177 class BLUETOOTH_API GattClient { 178 public: 179 /** 180 * @brief The function to Connect. 181 * 182 * @param callback callback of gattClientCallback. 183 * @param isAutoConnect isAutoConnect of GattClient. 184 * @param transport transport of GattClient. 185 * @return int api accept status. 186 * @since 6 187 * 188 */ 189 int Connect(GattClientCallback &callback, bool isAutoConnect, int transport); 190 191 /** 192 * @brief The function to request connection priority. 193 * 194 * @param connPriority connPriority of GattClient. 195 * @return int api accept status. 196 * @since 6 197 * 198 */ 199 int RequestConnectionPriority(int connPriority); 200 201 /** 202 * @brief The function to disconnect. 203 * 204 * @return int api accept status. 205 * @since 6 206 * 207 */ 208 int Disconnect(); 209 210 /** 211 * @brief The function to close. 212 * 213 * @return int. 214 * @since 6 215 * 216 */ 217 int Close(); 218 219 /** 220 * @brief The function to discover services. 221 * 222 * @return int api accept status. 223 * @since 6 224 * 225 */ 226 int DiscoverServices(); 227 228 /** 229 * @brief The function to get service. 230 * 231 * @param uuid uuid of GattClient. 232 * @return service. 233 * @since 6 234 * 235 */ 236 std::optional<std::reference_wrapper<GattService>> GetService(const UUID &uuid); 237 238 /** 239 * @brief The function to get service. 240 * 241 * @return list of services. 242 * @since 6 243 * 244 */ 245 std::vector<GattService> &GetService(); 246 247 /** 248 * @brief The function to read characteristic. 249 * 250 * @param characteristic Characteristic object. 251 * @return int read characteristic. 252 * @since 6 253 * 254 */ 255 int ReadCharacteristic(GattCharacteristic &characteristic); 256 257 /** 258 * @brief The function to read descriptor. 259 * 260 * @param descriptor descriptor object. 261 * @return int read descriptor. 262 * @since 6 263 * 264 */ 265 int ReadDescriptor(GattDescriptor &descriptor); 266 267 /** 268 * @brief The function to RequestBleMtuSize. 269 * 270 * @param mtu mtu of GattClient. 271 * @return int request ble mtu size. 272 * @since 6 273 * 274 */ 275 int RequestBleMtuSize(int mtu); 276 277 /** 278 * @brief The function to SetNotifyCharacteristic. 279 * 280 * @param characteristic characteristic object. 281 * @param enable enable of GattClient. 282 * @return result of #GATT_STATUS. 283 * @since 6 284 * 285 */ 286 int SetNotifyCharacteristic(GattCharacteristic &characteristic, bool enable); 287 288 /** 289 * @brief The function to write characteristic. 290 * 291 * @param characteristic characteristic object. 292 * @return int write characteristic. 293 * @since 6 294 * 295 */ 296 int WriteCharacteristic(GattCharacteristic &characteristic); 297 298 /** 299 * @brief The function to write characteristic. 300 * 301 * @param descriptor descriptor object. 302 * @return int write descriptor. 303 * @since 6 304 * 305 */ 306 int WriteDescriptor(GattDescriptor &descriptor); 307 308 /** 309 * @brief A constructor of GattClient. 310 * 311 * @param device Remote device object. 312 * @since 6 313 * 314 */ 315 explicit GattClient(const BluetoothRemoteDevice &device); 316 317 /** 318 * @brief A destructor of GattClient. 319 * 320 * @since 6 321 * 322 */ 323 ~GattClient(); 324 325 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(GattClient); 326 327 private: 328 BLUETOOTH_DECLARE_IMPL(); 329 }; 330 } // namespace Bluetooth 331 } // namespace OHOS 332 #endif // BLUETOOTH_GATT_CLIENT_H 333