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 interface_profile_gatt_server.h 29 * 30 * @brief Declares GATT server role interface profile functions. 31 * 32 * @since 6 33 * 34 */ 35 #ifndef INTERFACE_PROFILE_GATT_SERVER_H 36 #define INTERFACE_PROFILE_GATT_SERVER_H 37 38 #include "interface_profile.h" 39 #include "bt_def.h" 40 #include "gatt_data.h" 41 #include <vector> 42 43 namespace OHOS { 44 namespace bluetooth { 45 /** 46 * @brief Class for IGattServerCallback functions. 47 * 48 * @since 6 49 * 50 */ 51 class IGattServerCallback { 52 public: 53 /** 54 * @brief The callback function to notify characteristic read. 55 * 56 * @param device Remote device object. 57 * @param characteristic Characteristic object. 58 * @return int callback accept status. 59 * @since 6 60 * 61 */ OnCharacteristicReadRequest(const GattDevice & device,const Characteristic & characteristic)62 virtual void OnCharacteristicReadRequest(const GattDevice &device, const Characteristic &characteristic) {} 63 OnCharacteristicReadByUuidRequest(const GattDevice & device,const Characteristic & characteristic)64 virtual void OnCharacteristicReadByUuidRequest(const GattDevice &device, const Characteristic &characteristic) {} 65 66 /** 67 * @brief The callback function to notify characteristic write. 68 * 69 * @param device Remote device object. 70 * @param characteristic Characteristic object. 71 * @param needRespones Respones status. 72 * @return int callback accept status. 73 * @since 6 74 * 75 */ OnCharacteristicWriteRequest(const GattDevice & device,const Characteristic & characteristic,bool needRespones)76 virtual void OnCharacteristicWriteRequest( 77 const GattDevice &device, const Characteristic &characteristic, bool needRespones) {} 78 79 /** 80 * @brief The callback function to notify descriptor read. 81 * 82 * @param device Remote device object. 83 * @param descriptor Descriptor object. 84 * @return int callback accept status. 85 * @since 6 86 * 87 */ OnDescriptorReadRequest(const GattDevice & device,const Descriptor & descriptor)88 virtual void OnDescriptorReadRequest(const GattDevice &device, const Descriptor &descriptor) {} 89 90 /** 91 * @brief The callback function to notify descriptor write. 92 * 93 * @param device Remote device object. 94 * @param descriptor Descriptor object. 95 * @return int callback accept status. 96 * @since 6 97 * 98 */ OnDescriptorWriteRequest(const GattDevice & device,const Descriptor & descriptor)99 virtual void OnDescriptorWriteRequest(const GattDevice &device, const Descriptor &descriptor) {} 100 101 /** 102 * @brief The callback function to notify confirm notify status. 103 * 104 * @param device Remote device object. 105 * @param characteristic Characteristic object. 106 * @since 6 107 * 108 */ OnNotifyConfirm(const GattDevice & device,const Characteristic & characteristic,int result)109 virtual void OnNotifyConfirm(const GattDevice &device, const Characteristic &characteristic, int result) {} 110 /** 111 * @brief The callback function to notify connection state changed. 112 * 113 * @param device Remote device object. 114 * @param ret Result of the callback. 115 * @param state Current state. 116 * @since 6 117 * 118 */ 119 virtual void OnConnectionStateChanged(const GattDevice &device, int ret, int state) = 0; 120 /** 121 * @brief The callback function to notify mtu changed. 122 * 123 * @param device Remote device object. 124 * @param mtu Current mtu. 125 * @since 6 126 * 127 */ OnMtuChanged(const GattDevice & device,int mtu)128 virtual void OnMtuChanged(const GattDevice &device, int mtu) {} 129 /** 130 * @brief The callback function to notify service add. 131 * 132 * @param ret Result of the callback. 133 * @param services Service object. 134 * @since 6 135 * 136 */ OnAddService(int ret,const Service & services)137 virtual void OnAddService(int ret, const Service &services) {} 138 OnServiceChanged(const Service & services)139 virtual void OnServiceChanged(const Service &services) {} 140 OnConnectionParameterChanged(const GattDevice & device,int interval,int latency,int timeout,int status)141 virtual void OnConnectionParameterChanged( 142 const GattDevice &device, int interval, int latency, int timeout, int status) {} 143 144 /** 145 * @brief Destroy the IGattServerCallback. 146 * 147 * @since 6 148 * 149 */ ~IGattServerCallback()150 virtual ~IGattServerCallback() 151 {} 152 }; 153 154 /** 155 * @brief Class for IProfileGattServer functions. 156 * 157 * @since 6 158 * 159 */ 160 class IProfileGattServer : public IProfile { 161 public: 162 /** 163 * @brief The function to register application. 164 * 165 * @param callback Callback object. 166 * @return int api accept status. 167 * @since 6 168 * 169 */ 170 virtual int RegisterApplication(std::shared_ptr<IGattServerCallback> callback) = 0; 171 /** 172 * @brief The function to deregister application. 173 * 174 * @param appId Application id. 175 * @return int api accept status. 176 * @since 6 177 * 178 */ 179 virtual int DeregisterApplication(int appId) = 0; 180 /** 181 * @brief The function to add service. 182 * 183 * @param appId Application id. 184 * @param services Services object. 185 * @return int api accept status. 186 * @since 6 187 * 188 */ 189 virtual int AddService(int appId, Service &service) = 0; 190 /** 191 * @brief The function to remove service. 192 * 193 * @param appId Application id. 194 * @param services Services object. 195 * @return int api accept status. 196 * @since 6 197 * 198 */ 199 virtual int RemoveService(int appId, const Service &services) = 0; 200 /** 201 * @brief The function to clear services. 202 * 203 * @param appId Application id. 204 * @return int api accept status. 205 * @since 6 206 * 207 */ 208 virtual int ClearServices(int appId) = 0; 209 /** 210 * @brief The function to notify client. 211 * 212 * @param device Remote device object. 213 * @param characteristic Characteristic object. 214 * @param needConfirm Confirm need status. 215 * @return int api accept status. 216 * @since 6 217 * 218 */ 219 virtual int NotifyClient(const GattDevice &device, Characteristic &characteristic, bool needConfirm = false) = 0; 220 /** 221 * @brief The function to respond characteristic read. 222 * 223 * @param device Remote device object. 224 * @param characteristic Characteristic object. 225 * @param ret Result of the callback. 226 * @return int api accept status. 227 * @since 6 228 * 229 */ 230 virtual int RespondCharacteristicRead(const GattDevice &device, Characteristic &characteristic, int ret) = 0; 231 232 virtual int RespondCharacteristicReadByUuid(const GattDevice &device, Characteristic &characteristic, int ret) = 0; 233 /** 234 * @brief The function to respond characteristic write. 235 * 236 * @param device Remote device object. 237 * @param characteristic Characteristic object. 238 * @param ret Result of the callback. 239 * @return int api accept status. 240 * @since 6 241 * 242 */ 243 virtual int RespondCharacteristicWrite(const GattDevice &device, const Characteristic &characteristic, int ret) = 0; 244 /** 245 * @brief The function to respond descriptor read. 246 * 247 * @param device Remote device object. 248 * @param descriptor Descriptor object. 249 * @param ret Result of the callback. 250 * @return int api accept status. 251 * @since 6 252 * 253 */ 254 virtual int RespondDescriptorRead(const GattDevice &device, Descriptor &descriptor, int ret) = 0; 255 /** 256 * @brief The function to respond descriptor write. 257 * 258 * @param device Remote device object. 259 * @param descriptor Descriptor object. 260 * @param ret Result of the callback. 261 * @return int api accept status. 262 * @since 6 263 * 264 */ 265 virtual int RespondDescriptorWrite(const GattDevice &device, const Descriptor &descriptor, int ret) = 0; 266 /** 267 * @brief The function to cancel connection. 268 * 269 * @param device Remote device object. 270 * @return int api accept status. 271 * @since 6 272 * 273 */ 274 virtual int CancelConnection(const GattDevice &device) = 0; 275 276 private: 277 using IProfile::Connect; 278 using IProfile::Disconnect; 279 }; 280 } // namespace bluetooth 281 } // namespace OHOS 282 #endif // INTERFACE_PROFILE_GATT_SERVER_H