ble_gatts.h
Go to the documentation of this file.
1 /**
2 ****************************************************************************************
3 *
4 * @file ble_gatts.h
5 *
6 * @brief BLE GATTS API
7 *
8 ****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 *****************************************************************************************
36 */
37
38 /**
39 * @addtogroup BLE
40 * @{
41 */
42
43 /**
44 * @addtogroup BLE_GATT Generic Attribute Profile (GATT)
45 * @{
46 * @brief Definitions and prototypes for the GATT interface.
47 */
48
49 /**
50 @addtogroup BLE_SDK_GATTS Generic Attribute Profile (GATT) Server
51 @{
52 @brief Definitions and prototypes for the GATT server interface.
53 */
54
55
56 #ifndef __BLE_GATTS_H__
57 #define __BLE_GATTS_H__
58
59 #include "ble_error.h"
60 #include "ble_att.h"
61 #include "ble_gatt.h"
62 #include "ble_gapc.h"
63
64 #include <stdint.h>
65
66 /** @addtogroup BLE_GATTS_DEFINES Defines
67 * @{ */
68
69 /** @defgroup BLE_GATTS_MAX_INC_SRVC_NUM Max Number of Included Services
70 * @{ */
71 #define BLE_GATTS_MAX_INC_SRVC_NUM (5) /**< The max number of Included Services a Primary/Secondary service can have. Used by @ref ble_gatts_create_db_t. */
72 /** @} */
73
74 /** @defgroup BLE_GATTS_ATTR_PERM_BIT Attribute Permission Bit
75 * @{ */
76 #define BLE_GATTS_BROADCAST (0x01) /**< In one byte, bit0 means: Broadcast bit. Used by @ref BLE_GATTS_BROADCAST_ENABLE. */
77 #define BLE_GATTS_READ (0x02) /**< In one byte, bit1 means: Read bit. Used by @ref BLE_GATTS_READ_PERM_UNSEC, @ref BLE_GATTS_READ_PERM */
78 #define BLE_GATTS_WRITE_CMD (0x04) /**< In one byte, bit2 means: Write_cmd bit. Used by @ref BLE_GATTS_WRITE_CMD_PERM_UNSEC, @ref BLE_GATTS_WRITE_CMD_PERM. */
79 #define BLE_GATTS_WRITE_REQ (0x08) /**< In one byte, bit3 means: Write_req bit. Used by @ref BLE_GATTS_WRITE_REQ_PERM_UNSEC, @ref BLE_GATTS_WRITE_REQ_PERM. */
80 #define BLE_GATTS_NOTIFY (0x10) /**< In one byte, bit4 means: Notify bit. Used by @ref BLE_GATTS_NOTIFY_PERM_UNSEC, @ref BLE_GATTS_NOTIFY_PERM. */
81 #define BLE_GATTS_INDICATE (0x20) /**< In one byte, bit5 means: Indicate bit. Used by @ref BLE_GATTS_INDICATE_PERM_UNSEC, @ref BLE_GATTS_INDICATE_PERM */
82 #define BLE_GATTS_WRITE_SIGNED (0x40) /**< In one byte, bit6 means: Write_signed bit. Used by @ref BLE_GATTS_WRITE_SIGNED_PERM_UNSEC, @ref BLE_GATTS_WRITE_SIGNED_PERM. */
83 #define BLE_GATTS_EXT_PROP (0x80) /**< In one byte, bit7 means: Ext_property bit. Used by @ref BLE_GATTS_EXT_PROP_ENABLE. */
84 /** @} */
85
86 /** @defgroup BLE_GATTS_ATTR_PERM_POS Attribute Permission Value Position
87 * @{ */
88 #define BLE_GATTS_READ_POS (0x00) /**< Bit position of read permission. Used by @ref BLE_GATTS_READ_PERM. */
89 #define BLE_GATTS_WRITE_POS (0x02) /**< Bit position of write permission. Used by @ref BLE_GATTS_WRITE_CMD_PERM, @ref BLE_GATTS_WRITE_REQ_PERM, @ref BLE_GATTS_WRITE_SIGNED_PERM. */
90 #define BLE_GATTS_INDICATE_POS (0x04) /**< Bit position of indicate bit. Used by @ref BLE_GATTS_INDICATE_PERM. */
91 #define BLE_GATTS_NOTIFY_POS (0x06) /**< Bit position of notify bit. Used by @ref BLE_GATTS_NOTIFY_PERM. */
92 /** @} */
93
94 /** @defgroup BLE_GATTS_SEC_LEVEL Attribute and Service Access Rights
95 * @{ */
96
97 #define BLE_GATTS_NOAUTH (0x00) /**< LE security mode 1, level 1. Link does not need to be encrypted or authenticated.
98 Parameter of @ref BLE_GATTS_SRVC_PERM, @ref BLE_GATTS_READ_PERM, @ref BLE_GATTS_WRITE_REQ_PERM, @ref BLE_GATTS_WRITE_CMD_PERM, @ref BLE_GATTS_WRITE_SIGNED_PERM, @ref BLE_GATTS_INDICATE_PERM, @ref BLE_GATTS_NOTIFY_PERM. */
99 #define BLE_GATTS_UNAUTH (0x01) /**< LE security mode 1, level 2. Link needs to be encrypted, but not to be authenticated.
100 Parameter of @ref BLE_GATTS_SRVC_PERM, @ref BLE_GATTS_READ_PERM, @ref BLE_GATTS_WRITE_REQ_PERM, @ref BLE_GATTS_WRITE_CMD_PERM, @ref BLE_GATTS_WRITE_SIGNED_PERM, @ref BLE_GATTS_INDICATE_PERM, @ref BLE_GATTS_NOTIFY_PERM. */
101 #define BLE_GATTS_AUTH (0x02) /**< LE security mode 1, level 3. Link needs to be encrypted and authenticated (MITM).
102 Parameter of @ref BLE_GATTS_SRVC_PERM, @ref BLE_GATTS_READ_PERM, @ref BLE_GATTS_WRITE_REQ_PERM, @ref BLE_GATTS_WRITE_CMD_PERM, @ref BLE_GATTS_WRITE_SIGNED_PERM, @ref BLE_GATTS_INDICATE_PERM, @ref BLE_GATTS_NOTIFY_PERM. */
103 #define BLE_GATTS_SEC_CON (0x03) /**< LE security mode 1, level 4. Link needs to be encrypted and authenticateBLE_GATTS_d (secure connections).
104 Parameter of @ref BLE_GATTS_SRVC_PERM, @ref BLE_GATTS_READ_PERM, @ref BLE_GATTS_WRITE_REQ_PERM, @ref BLE_GATTS_WRITE_CMD_PERM, @ref BLE_GATTS_WRITE_SIGNED_PERM, @ref BLE_GATTS_INDICATE_PERM, @ref BLE_GATTS_NOTIFY_PERM. */
105 /** @} */
106
107 /** @defgroup BLE_GATTS_SEC_LEVEL_MASK Attribute and Service Security Level Mask
108 * @{ */
109 #define BLE_GATTS_SEC_LEVEL_MASK (0x03) /**< Security level mask.
110 Used by @ref BLE_GATTS_SRVC_PERM, @ref BLE_GATTS_READ_PERM, @ref BLE_GATTS_WRITE_REQ_PERM, @ref BLE_GATTS_WRITE_CMD_PERM, @ref BLE_GATTS_WRITE_SIGNED_PERM, @ref BLE_GATTS_INDICATE_PERM, @ref BLE_GATTS_NOTIFY_PERM. */
111 /** @} */
112
113 /** @defgroup BLE_GATTS_UUID_TYPE Attribute and Service UUID Type
114 * @{ */
115 #define BLE_GATTS_UUID_TYPE_16 (0x00) /**< 16-bit UUID length. */
116 #define BLE_GATTS_UUID_TYPE_128 (0x02) /**< 128-bit UUID length. */
117 /** @} */
118
119 /**
120 * Service permissions
121 *
122 * | 7 | 6--5 | 4 | 3--2 | 1 | 0 |
123 * |----|--------|----|--------|----|----|
124 * |SEC |UUID_LEN|DIS | AUTH |EKS | MI |
125 *
126 * Bit [0] : Service is multi-instantiated (0 = not support; 1 = support) \n
127 * Bit [1] : Encryption key size must be 16 bytes (0 = not need; 1 = need) \n
128 * Bit [2-3]: Service permission (0 = NOAUTH; 1 = UNAUTH; 2 = AUTH; 3 = SEC_CON) \n
129 * Bit [4] : Disable the service (0 = no; 1 = yes) \n
130 * Bit [5-6]: UUID Length Type (0 = 16 bits; 2 = 128 bits) \n
131 * Bit [7] : Secondary Service (0 = Primary Service; 1 = Secondary Service) \n
132 */
133
134 /** @defgroup BLE_GATTS_SRVC_PERM Service Permission
135 * @{ */
136 #define BLE_GATTS_SRVC_SECONDARY_SET (0x80) /**< Secondary service set. */
137 #define BLE_GATTS_SRVC_UUID_TYPE_SET(uuid_len) ((uuid_len) << 5) /**< Service UUID length set. See @ref BLE_GATTS_UUID_TYPE. */
138 #define BLE_GATTS_SRVC_DISABLE (0x10) /**< Service disable. */
139 #define BLE_GATTS_SRVC_PERM(sec_level) (((sec_level) & SEC_LEVEL_MASK) << 2) /**< Service permission authentication. See @ref BLE_GATTS_SEC_LEVEL. */
140 #define BLE_GATTS_SRVC_ENCRP_KEY_SIZE_16 (0x02) /**< 16 bytes service encryption key size . */
141 #define BLE_GATTS_SRVC_MULTI_ENABLE (0x01) /**< Service is multi-instantiated. */
142 /** @} */
143
144 /**
145 * Attribute permission
146 *
147 * | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7--6 | 5--4 | 3--2 | 1--0 |
148 * |----|----|----|----|----|----|----|----|--------|--------|--------|--------|
149 * |EXT | WS | I | N | WR | WC | RD | B | NP | IP | WP | RP |
150 *
151 * Bit [0-1]: Read permission (0 = NOAUTH; 1 = UNAUTH; 2 = AUTH; 3 = SEC_CON) \n
152 * Bit [2-3]: Write permission (0 = NOAUTH; 1 = UNAUTH; 2 = AUTH; 3 = SEC_CON) \n
153 * Bit [4-5]: Indicate permission (0 = NOAUTH; 1 = UNAUTH; 2 = AUTH; 3 = SEC_CON) \n
154 * Bit [6-7]: Notify permission (0 = NOAUTH; 1 = UNAUTH; 2 = AUTH; 3 = SEC_CON) \n
155 *
156 * Bit [8] : Broadcast permission \n
157 * Bit [9] : Read accepted \n
158 * Bit [10] : Write Command accepted \n
159 * Bit [11] : Write Request accepted \n
160 * Bit [12] : Notify accepted \n
161 * Bit [13] : Indicate accepted \n
162 * Bit [14] : Write Signed accepted \n
163 * Bit [15] : Extended Properties present \n
164 */
165
166 /** @defgroup BLE_GATTS_ATTR_PERM Attribute Permission
167 * @{ */
168 #define BLE_GATTS_READ_PERM_UNSEC (BLE_GATTS_READ << 8) /**< Default Read permission. */
169 #define BLE_GATTS_READ_PERM(sec_level) (BLE_GATTS_READ << 8 | (((sec_level) & BLE_GATTS_SEC_LEVEL_MASK) << BLE_GATTS_READ_POS)) /**< Read permission set. See @ref BLE_GATTS_SEC_LEVEL. */
170 #define BLE_GATTS_WRITE_REQ_PERM_UNSEC (BLE_GATTS_WRITE_REQ << 8) /**< Default Write Permission. */
171 #define BLE_GATTS_WRITE_REQ_PERM(sec_level) (BLE_GATTS_WRITE_REQ << 8 | (((sec_level) & BLE_GATTS_SEC_LEVEL_MASK) << BLE_GATTS_WRITE_POS)) /**< Write permission set. See @ref BLE_GATTS_SEC_LEVEL. */
172 #define BLE_GATTS_WRITE_CMD_PERM_UNSEC (BLE_GATTS_WRITE_CMD << 8) /**< Default Write without Response Permission. */
173 #define BLE_GATTS_WRITE_CMD_PERM(sec_level) (BLE_GATTS_WRITE_CMD << 8 | (((sec_level) & BLE_GATTS_SEC_LEVEL_MASK) << BLE_GATTS_WRITE_POS)) /**< Write without Response permission set. See @ref BLE_GATTS_SEC_LEVEL. */
174 #define BLE_GATTS_WRITE_SIGNED_PERM_UNSEC (BLE_GATTS_WRITE_SIGNED << 8) /**< Default Authenticated Signed Write Permission. */
175 #define BLE_GATTS_WRITE_SIGNED_PERM(sec_level) (BLE_GATTS_WRITE_SIGNED << 8 | (((sec_level) & BLE_GATTS_SEC_LEVEL_MASK) << BLE_GATTS_WRITE_POS)) /**< Authenticated Signed Write permission set. See @ref BLE_GATTS_SEC_LEVEL. */
176 #define BLE_GATTS_INDICATE_PERM_UNSEC (BLE_GATTS_INDICATE << 8) /**< Default Indicate Permission. */
177 #define BLE_GATTS_INDICATE_PERM(sec_level) (BLE_GATTS_INDICATE << 8 | (((sec_level) & BLE_GATTS_SEC_LEVEL_MASK) << BLE_GATTS_INDICATE_POS)) /**< Indicate permission set. See @ref BLE_GATTS_SEC_LEVEL. */
178 #define BLE_GATTS_NOTIFY_PERM_UNSEC (BLE_GATTS_NOTIFY << 8) /**< Default Notify Permission. */
179 #define BLE_GATTS_NOTIFY_PERM(sec_level) (BLE_GATTS_NOTIFY << 8 | (((sec_level) & BLE_GATTS_SEC_LEVEL_MASK) << BLE_GATTS_NOTIFY_POS)) /**< Notify permission set. See @ref BLE_GATTS_SEC_LEVEL. */
180 #define BLE_GATTS_BROADCAST_ENABLE (BLE_GATTS_BROADCAST << 8) /**< Broadcast enable. */
181 #define BLE_GATTS_EXT_PROP_ENABLE (BLE_GATTS_EXT_PROP << 8) /**< Extended Properties enable. */
182 /** @} */
183
184 /**
185 * Attribute extend permission
186 *
187 * | 15 | 14--13 | 12 | 11--0 |
188 * |----|---------|----|-----------------------------------------------------------|
189 * | VL |UUID_LEN |EKS | RESERVED |
190 *
191 * Bit [0-11] : Reserved \n
192 * Bit [12] : Encryption key size must be 16 bytes (0 = not need; 1 = need) \n
193 * Bit [14-13]: UUID length type (0 = 16 bits; 2 = 128 bits) \n
194 * Bit [15] : Value location (0 = value saved in BLE Stack; 1 = value saved in user space) \n
195 */
196
197 /** @defgroup BLE_GATTS_ATTR_EXT_PERM Attribute Extend Permission
198 * @{ */
199 #define BLE_GATTS_ATT_VAL_LOC_USER (1 << 15) /**< Value location which means value saved in user space, the profile's read/write callback will be called. */
200 #define BLE_GATTS_ATT_VAL_LOC_STACK (0 << 15) /**< Value location which means value saved in BLE Stack. */
201 #define BLE_GATTS_ATT_UUID_TYPE_SET(uuid_len) ((uuid_len) << 13) /**< Attribute UUID length set. See @ref BLE_GATTS_UUID_TYPE */
202 #define BLE_GATTS_ATT_ENC_KEY_SIZE_16 (0x1000) /**< 16 bytes attribute encryption key size . */
203 /** @} */
204
205
206 /** @} */
207
208 /** @addtogroup BLE_GATTS_ENUMERATIONS Enumerations
209 * @{ */
210
211 /**
212 * @brief Service table type.
213 */
214 typedef enum
215 {
216 BLE_GATTS_SERVICE_TABLE_TYPE_16 = 0x00, /**< 16-bit service table type. */
217 BLE_GATTS_SERVICE_TABLE_TYPE_128, /**< 128-bit service table type. */
218 } ble_gatts_service_type_t;
219
220
221 /** @} */
222
223
224 /** @addtogroup BLE_GATTS_STRUCTURES Structures
225 * @{ */
226
227 /**
228 * @brief Service(16-bit UUID) description.
229 */
230 typedef struct
231 {
232 uint16_t uuid; /**< 16-bit LSB-first UUID */
233 uint16_t perm; /**< Attribute permissions, see @ref BLE_GATTS_ATTR_PERM. \n
234 - For Primary/Secondary/Included Services, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
235 - For Characteristic Declaration, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
236 - For Characteristic Extended Properties, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
237 - For Characteristic Presentation Format, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
238 - For Characteristic Aggregate Format, must be @ref BLE_GATTS_READ_PERM_UNSEC. */
239
240 uint16_t ext_perm; /**< Attribute extended permissions, see @ref BLE_GATTS_ATTR_EXT_PERM. \n
241 - For Primary/Secondary/Included Services, this field is not used and should be set to 0. \n
242 - For Characteristic Declaration, this field is not used and should be set to 0. \n
243 - For Characteristic Extended Properties, this field is not used and should be set to 0. \n
244 - For Client Characteristic Configuration and Server Characteristic Configuration, value must be saved in user space,
245 user needn't to set this value location bit. The UUID length type must be set to 0.*/
246
247 uint16_t max_size; /**< Attribute max size. \n
248 - For Primary/Secondary/Included Services, this field is not used, set to 0. \n
249 - For Characteristic Declaration, this field is not used, set to 0. \n
250 - For Characteristic Extended Properties, this field contains 2-byte value. \n
251 - For Client Characteristic Configuration and Server Characteristic Configuration, this field is not used, set to 0. \n
252 - For others, this field is attribute max size. */
253 } ble_gatts_attm_desc_t;
254
255 /**
256 * @brief Service(128 bits UUID) description.
257 */
258 typedef struct
259 {
260 uint8_t uuid[16]; /**< 128 bits UUID LSB First. */
261 uint16_t perm; /**< Attribute permissions, see @ref BLE_GATTS_ATTR_PERM. \n
262 - For Primary/Secondary/Included Services, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
263 - For Characteristic Declaration, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
264 - For Characteristic Extended Properties, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
265 - For Characteristic Presentation Format, must be @ref BLE_GATTS_READ_PERM_UNSEC. \n
266 - For Characteristic Aggregate Format, must be @ref BLE_GATTS_READ_PERM_UNSEC. */
267
268 uint16_t ext_perm; /**< Attribute extended permissions, see @ref BLE_GATTS_ATTR_EXT_PERM. \n
269 - For Primary/Secondary/Included Services, this field is not used, set to 0. \n
270 - For Characteristic Declaration, this field is not used, set to 0. \n
271 - For Characteristic Extended Properties, this field is not used, set to 0. \n
272 - For Client Characteristic Configuration and Server Characteristic Configuration, value must be saved in user space,
273 user needn't to set this value location bit. The UUID length type must be set to 0.*/
274
275 uint16_t max_size; /**< Attribute max size. \n
276 - For Primary/Secondary/Included Services, this field is not used, set to 0. \n
277 - For Characteristic Declaration, this field is not used, set to 0. \n
278 - For Characteristic Extended Properties, this field contains 2-byte value. \n
279 - For Client Characteristic Configuration and Server Characteristic Configuration, this field is not used, set to 0. \n
280 - For others, this field is attribute max size. */
281 } ble_gatts_attm_desc_128_t;
282
283 /**
284 * @brief Parameter of Added service description.
285 */
286 typedef struct
287 {
288
289 uint16_t *shdl; /**< Service start handle pointer. If *shdl = 0, it returns a handle using the first available handle (*shdl is modified); otherwise it verifies if the given start handle can be used to allocate handle range. */
290 const uint8_t *uuid; /**< Service UUID pointer. The pointer points to the Service UUID LSB. */
291 uint8_t *attr_tab_cfg; /**< Attribute table selector pointer. It can be set to NULL to select all items of attribute table. Each bit matches with an attribute of attribute table. \n EXAMPLE:if attr_tab_cfg points to array {0x3F, 0x03}, it means that the 0.1.2.3.4.5.8.9 items of attribute table will be added to database. */
292 uint8_t max_nb_attr; /**< Number of attributes in attribute table. */
293 union attribute_table /**< Attribute table. */
294 {
295 const ble_gatts_attm_desc_t *attr_tab_16; /**< 16 bits service description. The pointer point to attribute table of 16 bits service. See @ref ble_gatts_attm_desc_t. */
296 const ble_gatts_attm_desc_128_t *attr_tab_128; /**< 128 bits service description. The pointer point to attribute table of 128 bits service. See @ref ble_gatts_attm_desc_128_t. */
297 } attr_tab; /**< Attribute table. */
298 uint16_t *inc_srvc_handle[BLE_GATTS_MAX_INC_SRVC_NUM]; /**< Pointer array of Included Service start handle's address. */
299 uint16_t inc_srvc_num; /**< Included Service number for this service. */
300 uint8_t srvc_perm; /**< Service permission. See @ref BLE_GATTS_SRVC_PERM. */
301 ble_gatts_service_type_t attr_tab_type; /**< Service table type. See @ref ble_gatts_service_type_t. */
302 } ble_gatts_create_db_t;
303
304 /**
305 * @brief GATT read attribute result description.
306 */
307 typedef struct
308 {
309 uint16_t handle; /**< Handle of the read attribute. */
310 uint16_t length; /**< Length of read data. */
311 uint8_t status; /**< Status of read operation by upper layers. See @ref BLE_STACK_ERROR_CODES.*/
312 uint8_t *value; /**< Attribute value pointer. */
313 } ble_gatts_read_cfm_t;
314
315 /**
316 * @brief GATT write attribute result description.
317 */
318 typedef struct
319 {
320 uint16_t handle; /**< Handle of the attribute written. */
321 uint8_t status; /**< Status of write operation by upper layers. See @ref BLE_STACK_ERROR_CODES.*/
322 } ble_gatts_write_cfm_t;
323
324 /**
325 * @brief GATT prepare write result description.
326 */
327 typedef struct
328 {
329 uint16_t handle; /**< Handle of the attribute in prepare write operation. */
330 uint16_t length; /**< Current length of the attribute. */
331 uint8_t status; /**< Status of prepare write operation by upper layers. See @ref BLE_STACK_ERROR_CODES.*/
332 } ble_gatts_prep_write_cfm_t;
333
334
335 /**
336 * @brief GATT sending Notification or Indication event param description.
337 */
338 typedef struct
339 {
340 ble_gatt_evt_type_t type; /**< Request type (Notification/Indication). see @ref ble_gatt_evt_type_t. */
341 uint16_t handle; /**< Characteristic Value handle to be notified or indicated. */
342 uint16_t length; /**< Length of Characteristic Value to be sent. */
343 uint8_t *value; /**< Characteristic Value pointer. */
344 } ble_gatts_noti_ind_t;
345
346 /**
347 * @brief GATT read request event for @ref BLE_GATTS_EVT_READ_REQUEST.
348 */
349 typedef struct
350 {
351 uint16_t handle; /**< Handle of the attribute to be read. */
352 } ble_gatts_evt_read_t;
353
354 /**
355 * @brief GATT write request event for @ref BLE_GATTS_EVT_WRITE_REQUEST.
356 */
357 typedef struct
358 {
359 uint16_t handle; /**< Handle of the attribute to be written. */
360 uint16_t offset; /**< Offset at which the data has to be written. */
361 uint16_t length; /**< Data length to be written. */
362 uint8_t *value; /**< Data to be written to characteristic value. */
363 } ble_gatts_evt_write_t;
364
365 /**
366 * @brief GATT prepare write request event for @ref BLE_GATTS_EVT_PREP_WRITE_REQUEST.
367 */
368 typedef struct
369 {
370 uint16_t handle; /**< Handle of the attribute for whose value is requested. */
371 } ble_gatts_evt_prep_write_t;
372
373 /**@brief Gatt Notification or indication event for @ref BLE_GATTS_EVT_NTF_IND. */
374 typedef struct
375 {
376 ble_gatt_evt_type_t type; /**< Notification or indication event type. */
377 uint16_t handle; /**< Handle of the write operation, or notification/indication operation. */
378 } ble_gatts_evt_ntf_ind_t;
379
380 /**@brief Gatt cccd recovery event for @ref BLE_GATTS_EVT_CCCD_RECOVERY. */
381 typedef struct
382 {
383 ble_gap_bdaddr_t peer_addr; /**< Pointer to peer address. */
384 uint16_t handle; /**< Handle of attribute. */
385 uint16_t cccd_val; /**< CCCD value. */
386 } ble_gatts_evt_cccd_rec_t;
387
388 /**@brief BLE GATTS event structure. */
389 typedef struct
390 {
391 uint8_t index; /**< Index of connection. */
392 union
393 {
394 ble_gatts_evt_read_t read_req; /**< Read request event. */
395 ble_gatts_evt_write_t write_req; /**< Write request event. */
396 ble_gatts_evt_prep_write_t prep_wr_req; /**< Prepare write request event. */
397 ble_gatts_evt_ntf_ind_t ntf_ind_sended; /**< Notification or indication sened event. */
398 ble_gatts_evt_cccd_rec_t cccd_recovery; /**< Gatt cccd recovery event . */
399 } params; /**< Event Parameters. */
400 } ble_gatts_evt_t;
401
402 /** @} */
403
404
405 /** @addtogroup BLE_GATTS_FUNCTIONS Functions
406 * @{ */
407 /**
408 ****************************************************************************************
409 * @brief Register a service's attribute list.
410 *
411 * @param[in] p_param: Pointer to the parameter used in creating databases. see @ref ble_gatts_create_db_t.
412 *
413 * @retval ::SDK_SUCCESS: Database has been registered successfully.
414 * @retval ::SDK_ERR_POINTER_NULL: Param is NULL or param's members are NULL.
415 * @retval ::SDK_ERR_INVALID_PARAM: The member of param is invalid.
416 * @retval ::SDK_ERR_INVALID_HANDLE: The service handles can not be allocated.
417 * @retval ::SDK_ERR_NO_RESOURCES: There is not enough memory to allocate service buffer.
418 * @retval ::SDK_ERR_INVALID_PERM: Permissions of Client Characteristic Configuration or Server Characteristic Configuration are not set correctly.
419 ****************************************************************************************
420 */
421 uint16_t ble_gatts_srvc_db_create(ble_gatts_create_db_t *p_param);
422
423 /**
424 ****************************************************************************************
425 * @brief Update attribute value only if the attribute value is saved in the BLE Stack space.
426 *
427 * @param[in] handle: Attribute handle.
428 * @param[in] length: Size of the value to set.
429 * @param[in] offset: Data offset of the value in attribute value.
430 * @param[in] p_value: The value to set. If offset = 0, the value is the new attribute value; otherwise, the value is part of the new attribute value.
431 *
432 * @retval ::SDK_SUCCESS: Successfully update the attribute value.
433 * @retval ::SDK_ERR_POINTER_NULL: Value is NULL.
434 * @retval ::SDK_ERR_INVALID_HANDLE: Handle not exist in database.
435 * @retval ::SDK_ERR_REQ_NOT_SUPPORTED: Attribute data is not present in database or cannot be modified.
436 * @retval ::SDK_ERR_INVALID_ATT_VAL_LEN: New value length exceeds maximum attribute value length.
437 * @retval ::SDK_ERR_INVALID_OFFSET: Offset exceeds current attribute value length.
438 ****************************************************************************************
439 */
440 uint16_t ble_gatts_value_set(uint16_t handle, uint16_t length, uint16_t offset, const uint8_t* p_value);
441
442 /**
443 ****************************************************************************************
444 * @brief Retrieve attribute value only if the attribute value is saved in the BLE Stack space.
445 *
446 * @param[in] handle: Attribute handle.
447 * @param[in,out] p_length: Input as buffer size and output as attribute value length.
448 * @param[out] p_value: Buffer to store attribute value when buffer size is enough.
449 *
450 * @retval ::SDK_SUCCESS: Attribute value retrieved successfully.
451 * @retval ::SDK_ERR_POINTER_NULL: The pointer to input buffer size or input buffer is NULL.
452 * @retval ::SDK_ERR_INVALID_HANDLE: Handle not exist in the database.
453 * @retval ::SDK_ERR_REQ_NOT_SUPPORTED: Attribute data is not present in database.
454 * @retval ::SDK_ERR_INVALID_ATT_VAL_LEN: Attribute data value size is bigger than buffer size.
455 * @retval ::SDK_ERR_APP_ERROR: Database is not correctly initialized by application.
456 ****************************************************************************************
457 */
458 uint16_t ble_gatts_value_get(uint16_t handle, uint16_t* p_length, uint8_t* p_value);
459
460 /**
461 ****************************************************************************************
462 * @brief Update attribute permission.
463 * @note The modifications of attribute permission to service/character/include/character_extended_properties_descriptor declaration: not supported. \n
464 * The modifications of attribute permission to others: perm can be updated and EKS of ext_perm can be updated.See @ref BLE_GATTS_ATTR_PERM.
465 *
466 * @param[in] handle: Attribute handle.
467 * @param[in] perm: New attribute permission.
468 * @param[in] ext_perm: New attribute extended permission.
469 *
470 * @retval ::SDK_SUCCESS: Update attribute permission successfully.
471 * @retval ::SDK_ERR_INVALID_HANDLE: Handle not exist in the database.
472 * @retval ::SDK_ERR_REQ_NOT_SUPPORTED: Attribute permission can't be modified.
473 ****************************************************************************************
474 */
475 uint16_t ble_gatts_attr_permission_set(uint16_t handle, uint16_t perm, uint16_t ext_perm);
476
477 /**
478 ****************************************************************************************
479 * @brief Retrieve attribute permission.
480 *
481 * @param[in] handle: Attribute handle.
482 * @param[out] p_perm: Attribute permission value to be returned.
483 * @param[out] p_ext_perm: Attribute extended permission value to be returned.
484 *
485 * @retval ::SDK_SUCCESS: Retrieve attribute permission successfully.
486 * @retval ::SDK_ERR_POINTER_NULL: Perm or ext_perm is NULL.
487 * @retval ::SDK_ERR_INVALID_HANDLE: Handle not exist in the database.
488 ****************************************************************************************
489 */
490 uint16_t ble_gatts_attr_permission_get(uint16_t handle, uint16_t *p_perm, uint16_t *p_ext_perm);
491
492 /**
493 ****************************************************************************************
494 * @brief Respond to an attribute read request..
495 *
496 * @note The status member gatts_read_cfm_t::status should be set to @ref BLE_ATT_ERR_INSUFF_AUTHOR
497 * to control the authorization of particular read operations of a client.
498 *
499 * @param[in] conn_idx: Current connection index.
500 * @param[in] p_param: Pointer to the parameters filled by profile. See @ref ble_gatts_read_cfm_t.
501 *
502 * @retval ::SDK_SUCCESS: Send read confirm value to stack successfully.
503 * @retval ::SDK_ERR_POINTER_NULL: Param is NULL.
504 * @retval ::SDK_ERR_INVALID_CONN_IDX: Conidx is invalid.
505 * @retval ::SDK_ERR_NO_RESOURCES: Not enough resources.
506 ****************************************************************************************
507 */
508 uint16_t ble_gatts_read_cfm(uint8_t conn_idx, const ble_gatts_read_cfm_t *p_param);
509
510 /**
511 ****************************************************************************************
512 * @brief Respond to an attribute write request.
513 *
514 * @note The status member gatts_write_cfm_t::status should be set to @ref BLE_ATT_ERR_INSUFF_AUTHOR
515 * to control the authorization of particular client's write operation.
516 *
517 * @param[in] conn_idx: Current connection index.
518 * @param[in] p_param: Pointer to the parameters filled by profile. see @ref ble_gatts_write_cfm_t.
519 *
520 * @retval ::SDK_SUCCESS: Send write confirm status to stack successfully.
521 * @retval ::SDK_ERR_POINTER_NULL: Param is NULL.
522 * @retval ::SDK_ERR_INVALID_CONN_IDX: Conidx is invalid.
523 * @retval ::SDK_ERR_NO_RESOURCES: Not enough resources.
524 ****************************************************************************************
525 */
526 uint16_t ble_gatts_write_cfm(uint8_t conn_idx, const ble_gatts_write_cfm_t *p_param);
527
528 /**
529 ****************************************************************************************
530 * @brief Respond to an attribute prepare write request.
531 *
532 * @note The status member gatts_prep_write_cfm_t::status should be set to @ref BLE_ATT_ERR_INSUFF_AUTHOR
533 * to control the authorization of particular client's write operation.
534 *
535 * @param[in] conn_idx: Current connection index.
536 * @param[in] p_param: Pointer to the parameters filled by profile. see @ref ble_gatts_prep_write_cfm_t.
537 *
538 * @retval ::SDK_SUCCESS: Send prepare write confirm status to stack successfully.
539 * @retval ::SDK_ERR_POINTER_NULL: Param is NULL.
540 * @retval ::SDK_ERR_INVALID_CONN_IDX: Conidx is invalid.
541 * @retval ::SDK_ERR_NO_RESOURCES: Not enough resources.
542 ****************************************************************************************
543 */
544 uint16_t ble_gatts_prepare_write_cfm(uint8_t conn_idx, const ble_gatts_prep_write_cfm_t *p_param);
545
546 /**
547 ****************************************************************************************
548 * @brief Send out a notification or an indication.
549 *
550 * @note Check whether the relevant Client Characteristic Configuration Descriptor is enabled before using this API.
551 *
552 * @param[in] conn_idx: Current connection index.
553 * @param[in] p_param: Pointer to the parameters filled by profile. see @ref ble_gatts_noti_ind_t.
554 *
555 * @retval ::SDK_SUCCESS: Send Notification or Indication event to stack successfully.
556 * @retval ::SDK_ERR_POINTER_NULL: Param is NULL.
557 * @retval ::SDK_ERR_INVALID_CONN_IDX: Conidx is invalid.
558 * @retval ::SDK_ERR_INVALID_PARAM: Type is invalid.
559 * @retval ::SDK_ERR_INVALID_HANDLE: Handle not exist in the database.
560 * @retval ::SDK_ERR_NO_RESOURCES: Not enough resources.
561 ****************************************************************************************
562 */
563 uint16_t ble_gatts_noti_ind(uint8_t conn_idx, const ble_gatts_noti_ind_t *p_param);
564
565 /**
566 ****************************************************************************************
567 * @brief When service on local device finishes upgrade, call this API to send service upgrade to stack.
568 * If the bonded device connects again, the stack will send service change to the bonded device until the bonded
569 * device has sent back indication confirmation.
570 ****************************************************************************************
571 */
572 void ble_gatts_service_changed(void);
573 /** @} */
574
575 #endif // BLE_SDK_GATTS_H_
576 /** @} */
577
578 /** @} */
579 /** @} */
580
ble_gatts_create_db_t::attribute_table::attr_tab_128
const ble_gatts_attm_desc_128_t * attr_tab_128
Definition: ble_gatts.h:296
ble_gatts_attm_desc_t::max_size
uint16_t max_size
Definition: ble_gatts.h:247
ble_gatts_create_db_t::srvc_perm
uint8_t srvc_perm
Definition: ble_gatts.h:300
ble_gatts_attm_desc_128_t::max_size
uint16_t max_size
Definition: ble_gatts.h:275
ble_gatts_evt_read_t
GATT read request event for BLE_GATTS_EVT_READ_REQUEST.
Definition: ble_gatts.h:350
ble_gap_bdaddr_t
The struct of broadcast address with broadcast type.
Definition: ble_gapc.h:176
ble_gatts_write_cfm_t::status
uint8_t status
Definition: ble_gatts.h:321
ble_gatts_read_cfm_t::handle
uint16_t handle
Definition: ble_gatts.h:309
ble_gatts_evt_write_t::value
uint8_t * value
Definition: ble_gatts.h:362
ble_gatts_write_cfm_t
GATT write attribute result description.
Definition: ble_gatts.h:319
ble_gatts_evt_write_t::offset
uint16_t offset
Definition: ble_gatts.h:360
ble_gatts_create_db_t
Parameter of Added service description.
Definition: ble_gatts.h:287
BLE_GATTS_SERVICE_TABLE_TYPE_16
@ BLE_GATTS_SERVICE_TABLE_TYPE_16
Definition: ble_gatts.h:216
ble_gatts_attm_desc_t
Service(16-bit UUID) description.
Definition: ble_gatts.h:231
ble_gatts_evt_cccd_rec_t::cccd_val
uint16_t cccd_val
Definition: ble_gatts.h:385
ble_gatts_noti_ind_t
GATT sending Notification or Indication event param description.
Definition: ble_gatts.h:339
ble_gatt.h
BLE GATT.
ble_gatts_evt_ntf_ind_t::type
ble_gatt_evt_type_t type
Definition: ble_gatts.h:376
ble_gatts_evt_cccd_rec_t::peer_addr
ble_gap_bdaddr_t peer_addr
Definition: ble_gatts.h:383
ble_gatts_evt_prep_write_t
GATT prepare write request event for BLE_GATTS_EVT_PREP_WRITE_REQUEST.
Definition: ble_gatts.h:369
BLE_GATTS_SERVICE_TABLE_TYPE_128
@ BLE_GATTS_SERVICE_TABLE_TYPE_128
Definition: ble_gatts.h:217
ble_gatts_attr_permission_set
uint16_t ble_gatts_attr_permission_set(uint16_t handle, uint16_t perm, uint16_t ext_perm)
Update attribute permission.
ble_gatts_noti_ind_t::value
uint8_t * value
Definition: ble_gatts.h:343
ble_gatts_evt_t::index
uint8_t index
Definition: ble_gatts.h:391
ble_gatts_attm_desc_t::uuid
uint16_t uuid
Definition: ble_gatts.h:232
ble_gatts_noti_ind_t::type
ble_gatt_evt_type_t type
Definition: ble_gatts.h:340
ble_gatts_create_db_t::uuid
const uint8_t * uuid
Definition: ble_gatts.h:290
ble_gatts_service_type_t
ble_gatts_service_type_t
Service table type.
Definition: ble_gatts.h:215
ble_gatts_evt_write_t::handle
uint16_t handle
Definition: ble_gatts.h:359
ble_gatts_value_set
uint16_t ble_gatts_value_set(uint16_t handle, uint16_t length, uint16_t offset, const uint8_t *p_value)
Update attribute value only if the attribute value is saved in the BLE Stack space.
ble_error.h
File that contains error codes.
ble_gatts_evt_t::read_req
ble_gatts_evt_read_t read_req
Definition: ble_gatts.h:394
ble_gapc.h
BLE GAPC API.
ble_gatts_evt_t::write_req
ble_gatts_evt_write_t write_req
Definition: ble_gatts.h:395
ble_gatts_create_db_t::attribute_table::attr_tab_16
const ble_gatts_attm_desc_t * attr_tab_16
Definition: ble_gatts.h:295
ble_gatts_create_db_t::attr_tab_type
ble_gatts_service_type_t attr_tab_type
Definition: ble_gatts.h:301
ble_gatts_create_db_t::max_nb_attr
uint8_t max_nb_attr
Definition: ble_gatts.h:292
ble_gatts_noti_ind_t::handle
uint16_t handle
Definition: ble_gatts.h:341
ble_gatts_attm_desc_128_t::perm
uint16_t perm
Definition: ble_gatts.h:261
ble_gatts_evt_cccd_rec_t::handle
uint16_t handle
Definition: ble_gatts.h:384
ble_gatts_write_cfm
uint16_t ble_gatts_write_cfm(uint8_t conn_idx, const ble_gatts_write_cfm_t *p_param)
Respond to an attribute write request.
ble_gatts_prep_write_cfm_t::handle
uint16_t handle
Definition: ble_gatts.h:329
ble_gatts_create_db_t::shdl
uint16_t * shdl
Definition: ble_gatts.h:289
ble_gatts_evt_t::cccd_recovery
ble_gatts_evt_cccd_rec_t cccd_recovery
Definition: ble_gatts.h:398
ble_gatts_read_cfm_t::length
uint16_t length
Definition: ble_gatts.h:310
ble_gatts_write_cfm_t::handle
uint16_t handle
Definition: ble_gatts.h:320
ble_gatts_evt_ntf_ind_t::handle
uint16_t handle
Definition: ble_gatts.h:377
ble_gatt_evt_type_t
ble_gatt_evt_type_t
GATT common events.
Definition: ble_gatt.h:67
ble_gatts_srvc_db_create
uint16_t ble_gatts_srvc_db_create(ble_gatts_create_db_t *p_param)
Register a service's attribute list.
ble_gatts_prep_write_cfm_t::status
uint8_t status
Definition: ble_gatts.h:331
ble_gatts_prepare_write_cfm
uint16_t ble_gatts_prepare_write_cfm(uint8_t conn_idx, const ble_gatts_prep_write_cfm_t *p_param)
Respond to an attribute prepare write request.
ble_gatts_attr_permission_get
uint16_t ble_gatts_attr_permission_get(uint16_t handle, uint16_t *p_perm, uint16_t *p_ext_perm)
Retrieve attribute permission.
ble_gatts_read_cfm_t::status
uint8_t status
Definition: ble_gatts.h:311
ble_gatts_evt_t
BLE GATTS event structure.
Definition: ble_gatts.h:390
ble_gatts_evt_write_t
GATT write request event for BLE_GATTS_EVT_WRITE_REQUEST.
Definition: ble_gatts.h:358
ble_gatts_create_db_t::inc_srvc_num
uint16_t inc_srvc_num
Definition: ble_gatts.h:299
ble_gatts_evt_prep_write_t::handle
uint16_t handle
Definition: ble_gatts.h:370
ble_gatts_prep_write_cfm_t::length
uint16_t length
Definition: ble_gatts.h:330
BLE_GATTS_MAX_INC_SRVC_NUM
#define BLE_GATTS_MAX_INC_SRVC_NUM
Definition: ble_gatts.h:71
ble_gatts_create_db_t::attr_tab_cfg
uint8_t * attr_tab_cfg
Definition: ble_gatts.h:291
ble_gatts_evt_t::ntf_ind_sended
ble_gatts_evt_ntf_ind_t ntf_ind_sended
Definition: ble_gatts.h:397
ble_att.h
Attribute Protocol.
ble_gatts_value_get
uint16_t ble_gatts_value_get(uint16_t handle, uint16_t *p_length, uint8_t *p_value)
Retrieve attribute value only if the attribute value is saved in the BLE Stack space.
ble_gatts_evt_read_t::handle
uint16_t handle
Definition: ble_gatts.h:351
ble_gatts_prep_write_cfm_t
GATT prepare write result description.
Definition: ble_gatts.h:328
ble_gatts_attm_desc_t::perm
uint16_t perm
Definition: ble_gatts.h:233
ble_gatts_noti_ind_t::length
uint16_t length
Definition: ble_gatts.h:342
ble_gatts_service_changed
void ble_gatts_service_changed(void)
When service on local device finishes upgrade, call this API to send service upgrade to stack....
ble_gatts_evt_t::prep_wr_req
ble_gatts_evt_prep_write_t prep_wr_req
Definition: ble_gatts.h:396
ble_gatts_read_cfm_t::value
uint8_t * value
Definition: ble_gatts.h:312
ble_gatts_create_db_t::attribute_table
Definition: ble_gatts.h:294
ble_gatts_evt_write_t::length
uint16_t length
Definition: ble_gatts.h:361
ble_gatts_attm_desc_128_t::ext_perm
uint16_t ext_perm
Definition: ble_gatts.h:268
ble_gatts_read_cfm_t
GATT read attribute result description.
Definition: ble_gatts.h:308
ble_gatts_attm_desc_t::ext_perm
uint16_t ext_perm
Definition: ble_gatts.h:240
ble_gatts_noti_ind
uint16_t ble_gatts_noti_ind(uint8_t conn_idx, const ble_gatts_noti_ind_t *p_param)
Send out a notification or an indication.
ble_gatts_attm_desc_128_t
Service(128 bits UUID) description.
Definition: ble_gatts.h:259
ble_gatts_evt_ntf_ind_t
Gatt Notification or indication event for BLE_GATTS_EVT_NTF_IND.
Definition: ble_gatts.h:375
ble_gatts_read_cfm
uint16_t ble_gatts_read_cfm(uint8_t conn_idx, const ble_gatts_read_cfm_t *p_param)
Respond to an attribute read request..
ble_gatts_evt_cccd_rec_t
Gatt cccd recovery event for BLE_GATTS_EVT_CCCD_RECOVERY.
Definition: ble_gatts.h:382