1 /* 2 * Copyright (c) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED. 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 * Description: GATT server interfaces 16 */ 17 18 #ifndef OHOS_BT_GATT_SERVER_H 19 #define OHOS_BT_GATT_SERVER_H 20 21 #include "ohos_bt_def.h" 22 23 typedef enum { 24 OHOS_BLE_SEC_NONE = 0x00, 25 OHOS_BLE_SEC_ENCRYPT, 26 OHOS_BLE_SEC_ENCRYPT_NO_MITM, 27 OHOS_BLE_SEC_ENCRYPT_MITM 28 } BleSecAct; 29 30 typedef struct { 31 int connectId; /* connection index */ 32 int status; /* read/write character status, GattStatus */ 33 int attrHandle; /* attribute handle */ 34 int valueLen; 35 char *value; /* response data from host */ 36 } GattsSendRspParam; 37 38 typedef struct { 39 int connectId; /* connection index */ 40 int attrHandle; /* attribute handle */ 41 int confirm; /* true(indication)-the client shall respond a conformation, false(notification)-send a notification */ 42 int valueLen; /* response data from host */ 43 char *value; 44 } GattsSendIndParam; 45 46 typedef struct { 47 int connId; /* connect index */ 48 int transId; /* BtTransportId */ 49 BdAddr *bdAddr; 50 int attrHandle; /* The handle of the attribute to be read */ 51 int offset; /* The offset of the first octet to be read */ 52 bool isLong; /* If isLong is true, then this request is part of a Long Read procedure */ 53 } BtReqReadCbPara; 54 55 typedef struct { 56 int connId; /* connect index */ 57 int transId; /* BtTransportId */ 58 BdAddr *bdAddr; 59 int attrHandle; /* The handle of the attribute to be written */ 60 int offset; /* The offset of the first octet to be written */ 61 int length; 62 bool needRsp; /* if the remote device requires a response */ 63 bool isPrep; /* if this write operation should be queued for later execution */ 64 unsigned char *value; 65 } BtReqWriteCbPara; 66 67 /* Callback invoked in response to register_server */ 68 typedef void (*RegisterServerCallback)(int status, int serverId, BtUuid *appUuid); 69 70 /* Callback indicating that a remote device has connected */ 71 typedef void (*ConnectServerCallback)(int connId, int serverId, const BdAddr *bdAddr); 72 73 /* Callback indicating that a remote device has disconnected */ 74 typedef void (*DisconnectServerCallback)(int connId, int serverId, const BdAddr *bdAddr); 75 76 /* Callback invoked in response to add service */ 77 typedef void (*ServiceAddCallback)(int status, int serverId, BtUuid *uuid, int srvcHandle); 78 79 /* Callback indicating that an included service has been added to a service */ 80 typedef void (*IncludeServiceAddCallback)(int status, int serverId, int srvcHandle, int includeSrvcHandle); 81 82 /* Callback invoked when a characteristic has been added to a service */ 83 typedef void (*CharacteristicAddCallback)(int status, int serverId, BtUuid *uuid, 84 int srvcHandle, int characteristicHandle); 85 86 /* Callback invoked when a descriptor has been added to a characteristic */ 87 typedef void (*DescriptorAddCallback)(int status, int serverId, BtUuid *uuid, 88 int srvcHandle, int descriptorHandle); 89 90 /* Callback invoked in response to start_service */ 91 typedef void (*ServiceStartCallback)(int status, int serverId, int srvcHandle); 92 93 /* Callback invoked in response to stop_service */ 94 typedef void (*ServiceStopCallback)(int status, int serverId, int srvcHandle); 95 96 /* Callback triggered when a service has been deleted */ 97 typedef void (*ServiceDeleteCallback)(int status, int serverId, int srvcHandle); 98 99 /* Callback invoked when a remote device has requested to read a characteristicor descriptor. 100 * The application must respond by calling send_response 101 */ 102 typedef void (*RequestReadCallback)(BtReqReadCbPara readCbPara); 103 104 /* Callback invoked when a remote device has requested to write to a characteristic or descriptor. */ 105 typedef void (*RequestWriteCallback)(BtReqWriteCbPara writeCbPara); 106 107 /* Callback triggered in response to send_response if the remote device sends a confirmation */ 108 typedef void (*ResponseConfirmationCallback)(int status, int handle); 109 110 /* Callback confirming that a notification or indication has been sent to a remote device */ 111 typedef void (*IndicationSentCallback)(int connId, int status); 112 113 /* Callback invoked when the MTU for a given connection changes */ 114 typedef void (*MtuChangeCallback)(int connId, int mtu); 115 116 typedef struct { 117 RegisterServerCallback registerServerCb; 118 ConnectServerCallback connectServerCb; 119 DisconnectServerCallback disconnectServerCb; 120 ServiceAddCallback serviceAddCb; 121 IncludeServiceAddCallback includeServiceAddCb; 122 CharacteristicAddCallback characteristicAddCb; 123 DescriptorAddCallback descriptorAddCb; 124 ServiceStartCallback serviceStartCb; 125 ServiceStopCallback serviceStopCb; 126 ServiceDeleteCallback serviceDeleteCb; 127 RequestReadCallback requestReadCb; 128 RequestWriteCallback requestWriteCb; 129 ResponseConfirmationCallback responseConfirmationCb; 130 IndicationSentCallback indicationSentCb; 131 MtuChangeCallback mtuChangeCb; 132 } BtGattServerCallbacks; 133 134 typedef int (*BleGattServiceRead)(unsigned char *buff, unsigned int *len); 135 136 typedef int (*BleGattServiceWrite)(unsigned char *buff, unsigned int len); 137 138 typedef int (*BleGattServiceIndicate)(unsigned char *buff, unsigned int len); 139 140 typedef struct { 141 BleGattServiceRead read; 142 BleGattServiceWrite write; 143 BleGattServiceIndicate indicate; 144 } BleGattOperateFunc; 145 146 typedef struct { 147 BleAttribType attrType; 148 unsigned int permission; /* e.g. (OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE) */ 149 UuidType uuidType; 150 unsigned char uuid[OHOS_BLE_UUID_MAX_LEN]; 151 unsigned char *value; 152 unsigned char valLen; 153 /* e.g. (OHOS_GATT_CHARACTER_PROPERTY_BIT_BROADCAST | OHOS_GATT_CHARACTER_PROPERTY_BIT_READ) */ 154 unsigned char properties; 155 BleGattOperateFunc func; 156 } BleGattAttr; 157 158 typedef struct { 159 unsigned int attrNum; 160 BleGattAttr *attrList; 161 } BleGattService; 162 163 /* 164 * @brief gatt server application register, callback return serverId 165 * @param[in] <appUuid> specified by upper layer 166 * @return 0-success, other-fail 167 */ 168 int BleGattsRegister(BtUuid appUuid); 169 170 /* 171 * @brief gatt server deregister 172 * @param[in] <clientId> server interface Id 173 * @return 0-success, other-fail 174 */ 175 int BleGattsUnRegister(int serverId); 176 177 /* 178 * @brief Cancel connection with remote device 179 * @param[in] <serverId> server interface id 180 * @param[in] <bdAddr> remote address 181 * @param[in] <connId> connection index. 182 * @return 0-success, other-fail 183 */ 184 int BleGattsDisconnect(int serverId, BdAddr bdAddr, int connId); 185 186 /* 187 * @brief add service 188 * @param[in] <serverId> server interface id 189 * @param[in] <srvcUuid> service uuid and uuid length 190 * @param[in] <isPrimary> is primary or secondary service. 191 * @param[in] <number> service characther attribute number. 192 * @return 0-success, other-fail 193 */ 194 int BleGattsAddService(int serverId, BtUuid srvcUuid, bool isPrimary, int number); 195 196 /* 197 * @brief add characteristic 198 * @param[in] <serverId> server interface id 199 * @param[in] <srvcHandle> service handle 200 * @param[in] <characUuid> characteristic uuid and uuid length 201 * @param[in] <properties> e.g. (OHOS_GATT_CHARACTER_PROPERTY_BIT_BROADCAST | OHOS_GATT_CHARACTER_PROPERTY_BIT_READ) 202 * @param[in] <permissions> e.g. (OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE) 203 * @return 0-success, other-fail 204 */ 205 int BleGattsAddCharacteristic(int serverId, int srvcHandle, BtUuid characUuid, 206 int properties, int permissions); 207 208 /* 209 * @brief add descriptor 210 * @param[in] <serverId> server interface id 211 * @param[in] <srvcHandle> service handle 212 * @param[in] <descUuid> descriptor uuid and uuid length 213 * @param[in] <permissions> e.g. (OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE) 214 * @return 0-success, other-fail 215 */ 216 int BleGattsAddDescriptor(int serverId, int srvcHandle, BtUuid descUuid, int permissions); 217 218 /* 219 * @brief start service 220 * @param[in] <serverId> server interface id 221 * @param[in] <srvcHandle> service handle 222 * @return 0-success, other-fail 223 */ 224 int BleGattsStartService(int serverId, int srvcHandle); 225 226 /* 227 * @brief start service 228 * @param[in] <serverId> server interface id 229 * @param[in] <srvcHandle> service handle 230 * @return 0-success, other-fail 231 */ 232 int BleGattsStopService(int serverId, int srvcHandle); 233 234 /* 235 * @brief remove a service from the list of provided services 236 * @param[in] <serverId> server interface id 237 * @param[in] <srvcHandle> service handle 238 * @return 0-success, other-fail 239 */ 240 int BleGattsDeleteService(int serverId, int srvcHandle); 241 242 /* 243 * @brief remove all services from the list of provided services 244 * @param[in] <serverId> server interface id 245 * @return 0-success, other-fail 246 */ 247 int BleGattsClearServices(int serverId); 248 249 /* 250 * @brief Send a response to a read or write request to a remote device. 251 * @param[in] <serverId> server interface id 252 * @param[in] <GattsSendRspParam> response param 253 * @return 0-success, other-fail 254 */ 255 int BleGattsSendResponse(int serverId, GattsSendRspParam *param); 256 257 /* 258 * @brief Send a notification or indication that a local characteristic has been updated 259 * @param[in] <serverId> server interface id 260 * @param[in] <GattsSendIndParam> indication param 261 * @return 0-success, other-fail 262 */ 263 int BleGattsSendIndication(int serverId, GattsSendIndParam *param); 264 265 /* 266 * @brief Set the encryption level of the data transmission channel during connection 267 * @param[in] <bdAddr> remote address 268 * @param[in] <secAct> BleSecAct 269 * @return 0-success, other-fail 270 */ 271 int BleGattsSetEncryption(BdAddr bdAddr, BleSecAct secAct); 272 273 /* 274 * @brief Callback invoked for gatt server function 275 * @param[in] <BtGattServerCallbacks> Callback funcs 276 * @return 0-success, other-fail 277 */ 278 int BleGattsRegisterCallbacks(BtGattServerCallbacks *func); 279 280 /* 281 * @brief Start sevice include add service/characteristic/Descriptor option. 282 * This API will not described in the development manual, only for Hilink. 283 * @return 0-success, other-fail 284 */ 285 int BleGattsStartServiceEx(int *srvcHandle, BleGattService *srvcInfo); 286 287 /* 288 * @brief Stop service. 289 * This API will not described in the development manual, only for Hilink. 290 * @return 0-success, other-fail 291 */ 292 int BleGattsStopServiceEx(int srvcHandle); 293 #endif 294