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_server.h 29 * 30 * @brief gatt server interface. 31 * 32 * @since 6 33 * 34 */ 35 36 #ifndef BLUETOOTH_GATT_SERVER_H 37 #define BLUETOOTH_GATT_SERVER_H 38 39 #include "bluetooth_def.h" 40 #include "bluetooth_gatt_service.h" 41 #include "bluetooth_remote_device.h" 42 #include <memory> 43 44 namespace OHOS { 45 namespace Bluetooth { 46 /** 47 * @brief Class for Gatt Server callback functions. 48 * 49 * @since 6 50 * 51 */ 52 class GattServerCallback { 53 public: 54 /** 55 * @brief The callback function to notify connection state update. 56 * 57 * @param device Remote device object. 58 * @param state Connection state. 59 * @since 6 60 * 61 */ OnConnectionStateUpdate(const BluetoothRemoteDevice & device,int state)62 virtual void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) 63 {} 64 65 /** 66 * @brief The callback function to notify connection state update. 67 * 68 * @param device Remote device object. 69 * @param state Connection state. 70 * @since 20 71 * 72 */ OnConnectionStateUpdateWithReason(const BluetoothRemoteDevice & device,int state,int disconnectReason)73 virtual void OnConnectionStateUpdateWithReason(const BluetoothRemoteDevice &device, int state, int disconnectReason) 74 {} 75 76 /** 77 * @brief The callback function to notify service add. 78 * 79 * @param Service Added service object. 80 * @param ret Result of service add. 81 * @since 6 82 * 83 */ OnServiceAdded(GattService service,int ret)84 virtual void OnServiceAdded(GattService service, int ret) 85 {} 86 87 /** 88 * @brief The callback function to notify characteristic read request. 89 * 90 * @param device Remote device object. 91 * @param characteristic Characteristic object. 92 * @param requestId Result of request. 93 * @since 6 94 * 95 */ OnCharacteristicReadRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)96 virtual void OnCharacteristicReadRequest( 97 const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId) 98 {} 99 100 /** 101 * @brief The callback function to notify characteristic write request. 102 * 103 * @param device Remote device object. 104 * @param characteristic Characteristic object. 105 * @param requestId Result of request. 106 * @since 6 107 * 108 */ OnCharacteristicWriteRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)109 virtual void OnCharacteristicWriteRequest( 110 const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId) 111 {} 112 113 /** 114 * @brief The callback function to notify descriptor read request. 115 * 116 * @param device Remote device object. 117 * @param characteristic Characteristic object. 118 * @param requestId Result of request. 119 * @since 6 120 * 121 */ OnDescriptorReadRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)122 virtual void OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId) 123 {} 124 125 /** 126 * @brief The callback function to notify descriptor write request. 127 * 128 * @param device Remote device object. 129 * @param characteristic Characteristic object. 130 * @param requestId Result of request. 131 * @since 6 132 * 133 */ OnDescriptorWriteRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)134 virtual void OnDescriptorWriteRequest( 135 const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId) 136 {} 137 138 /** 139 * @brief The callback function to notify mtu update. 140 * 141 * @param device Remote device object. 142 * @param mtu Current mtu. 143 * @since 6 144 * 145 */ OnMtuUpdate(const BluetoothRemoteDevice & device,int mtu)146 virtual void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) 147 {} 148 /** 149 * @brief The callback function to notify characteristic changed. 150 * 151 * @param device Remote device object. 152 * @since 6 153 * 154 */ OnNotificationCharacteristicChanged(const BluetoothRemoteDevice & device,int result)155 virtual void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int result) 156 {} 157 /** 158 * @brief The callback function to notify connection parameter changed 159 * 160 * @param device Remote device object. 161 * @param interval Interval object. 162 * @param latency Latency object. 163 * @param timeout Timeout object. 164 * @param status Status object. 165 * @since 6 166 * 167 */ OnConnectionParameterChanged(const BluetoothRemoteDevice & device,int interval,int latency,int timeout,int status)168 virtual void OnConnectionParameterChanged( 169 const BluetoothRemoteDevice &device, int interval, int latency, int timeout, int status) 170 {} 171 172 /** 173 * @brief A destructor of GattServerCallback. 174 * 175 * @since 6 176 * 177 */ ~GattServerCallback()178 virtual ~GattServerCallback() 179 {} 180 }; 181 182 /** 183 * @brief Class for Gatt Server API. 184 * 185 * @since 6 186 * 187 */ 188 class BLUETOOTH_API GattServer : public std::enable_shared_from_this<GattServer> { 189 public: 190 /** 191 * @brief A constructor of GattServerCallback. 192 * 193 * @param device GattServerCallback callback object. 194 * @since 6 195 * 196 */ 197 static std::shared_ptr<GattServer> CreateInstance(std::shared_ptr<GattServerCallback> callback); 198 /** 199 * @brief The function to add service. 200 * 201 * @param service Service object to add. 202 * @return int api accept status. 203 * @since 6 204 * 205 */ 206 int AddService(GattService &service); 207 /** 208 * @brief The function to remove service. 209 * 210 * @param service Service object to remove. 211 * @return int api accept status. 212 * @since 6 213 * 214 */ 215 int RemoveGattService(const GattService &service); 216 /** 217 * @brief The function to clear all services. 218 * 219 * @since 6 220 * 221 */ 222 void ClearServices(); 223 /** 224 * @brief The function to clear all services. 225 * 226 * @return int. 227 * @since 6 228 * 229 */ 230 int Close(); 231 /** 232 * @brief The function to get service by UUID. 233 * 234 * @param uuid UUID of service. 235 * @param isPrimary Type of service. 236 * @return service. 237 * @since 6 238 * 239 */ 240 std::optional<std::reference_wrapper<GattService>> GetService(const UUID &uuid, bool isPrimary); 241 /** 242 * @brief The function to get all services. 243 * 244 * @return list of services. 245 * @since 6 246 * 247 */ 248 std::list<GattService> &GetServices(); 249 /** 250 * @brief The function to notify characteristic change. 251 * 252 * @param device Remote device object. 253 * @param characteristic Characteristic object. 254 * @param confirm Confirm the change. 255 * @return int api accept status. 256 * @since 6 257 * 258 */ 259 int NotifyCharacteristicChanged( 260 const BluetoothRemoteDevice &device, const GattCharacteristic &characteristic, bool confirm); 261 /** 262 * @brief The function to send responce. 263 * 264 * @param device Remote device object. 265 * @param requestId Result of the request. 266 * @param status Current status. 267 * @param offset Offset object. 268 * @param value Value object. 269 * @param length Length of value. 270 * @return int api accept status. 271 * @since 6 272 * 273 */ 274 int SendResponse( 275 const BluetoothRemoteDevice &device, int requestId, int status, int offset, const uint8_t *value, int length); 276 277 /** 278 * @brief The function to connect client device. 279 * 280 * @param device Remote device object. 281 * @param isDirect Whether to directly connect to the remote device (true) 282 * or to automatically connect as soon as the remote device becomes available (false) 283 * @return int api accept status. 284 * @since 6 285 * 286 */ 287 int Connect(const BluetoothRemoteDevice &device, bool isDirect); 288 289 /** 290 * @brief The function to cancel connection. 291 * 292 * @param device Remote device object. 293 * @return int api accept status. 294 * @since 6 295 * 296 */ 297 int CancelConnection(const BluetoothRemoteDevice &device); 298 299 /** 300 * @brief A destructor of GattServer. 301 * 302 * @since 6 303 * 304 */ 305 ~GattServer(); 306 /** 307 * @brief The function to delete constructor of GattServer. 308 * 309 * @since 6 310 * 311 */ 312 GattServer() = delete; 313 314 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(GattServer); 315 316 private: 317 explicit GattServer(std::shared_ptr<GattServerCallback> callback); 318 BLUETOOTH_DECLARE_IMPL(); 319 320 // The passkey pattern of C++ 321 struct PassKey { PassKeyPassKey322 PassKey() {}; 323 }; 324 public: GattServer(PassKey,std::shared_ptr<GattServerCallback> callback)325 explicit GattServer(PassKey, std::shared_ptr<GattServerCallback> callback) : GattServer(callback) {}; 326 327 }; 328 } // namespace Bluetooth 329 } // namespace OHOS 330 #endif // BLUETOOTH_GATT_SERVER_H 331