1 /**
2  ****************************************************************************************
3  *
4  * @file gattm_task.h
5  *
6  * @brief Header file - GATTMTASK.
7  *
8  * Copyright (C) RivieraWaves 2009-2016
9  *
10  *
11  ****************************************************************************************
12  */
13 
14 #ifndef GATTM_TASK_H_
15 #define GATTM_TASK_H_
16 
17 /**
18  ****************************************************************************************
19  * @addtogroup GATTMTASK Task
20  * @ingroup GATTM
21  * @brief Handles ALL GATT block operations not related to a connection.
22  *
23  * The GATTMTASK is responsible for managing internal attribute database and state of
24  * GATT controller which manage GATT block operations related to a connection.
25  *
26  * Messages may originate from @ref ATTM "ATTM", @ref GAP "GAP" and Application.
27  *
28  * @{
29  ****************************************************************************************
30  */
31 /*
32  * INCLUDE FILES
33  ****************************************************************************************
34  */
35 #include "rwip_task.h" // Task definitions
36 #include "att.h"
37 
38 /*
39  * DEFINES
40  ****************************************************************************************
41  */
42 
43 
44 /// GATT Task messages
45 enum gattm_msg_id
46 {
47     /* Database Management */
48     /// Add service in database request
49     GATTM_ADD_SVC_REQ = TASK_FIRST_MSG(TASK_ID_GATTM),
50     /// Add service in database response
51     GATTM_ADD_SVC_RSP,
52 
53     /* Service management */
54     /// Get permission settings of service request
55     GATTM_SVC_GET_PERMISSION_REQ,
56     /// Get permission settings of service response
57     GATTM_SVC_GET_PERMISSION_RSP,
58     /// Set permission settings of service request
59     GATTM_SVC_SET_PERMISSION_REQ,
60     /// Set permission settings of service response
61     GATTM_SVC_SET_PERMISSION_RSP,
62 
63     /* Attribute Manipulation */
64     /// Get permission settings of attribute request
65     GATTM_ATT_GET_PERMISSION_REQ,
66     /// Get permission settings of attribute response
67     GATTM_ATT_GET_PERMISSION_RSP,
68     /// Set permission settings of attribute request
69     GATTM_ATT_SET_PERMISSION_REQ,
70     /// Set permission settings of attribute response
71     GATTM_ATT_SET_PERMISSION_RSP,
72 
73     /// Get attribute value request
74     GATTM_ATT_GET_VALUE_REQ,
75     /// Get attribute value response
76     GATTM_ATT_GET_VALUE_RSP,
77     /// Set attribute value request
78     GATTM_ATT_SET_VALUE_REQ,
79     /// Set attribute value response
80     GATTM_ATT_SET_VALUE_RSP,
81 
82     /* Debug messages */
83     /// DEBUG ONLY: Destroy Attribute database request
84     GATTM_DESTROY_DB_REQ,
85     /// DEBUG ONLY: Destroy Attribute database response
86     GATTM_DESTROY_DB_RSP,
87     /// DEBUG ONLY: Retrieve list of services request
88     GATTM_SVC_GET_LIST_REQ,
89     /// DEBUG ONLY: Retrieve list of services response
90     GATTM_SVC_GET_LIST_RSP,
91     /// DEBUG ONLY: Retrieve information of attribute request
92     GATTM_ATT_GET_INFO_REQ,
93     /// DEBUG ONLY: Retrieve information of attribute response
94     GATTM_ATT_GET_INFO_RSP,
95 };
96 
97 
98 /**
99  * Attribute Description
100  */
101 struct gattm_att_desc
102 {
103     /** Attribute UUID (LSB First) */
104     uint8_t uuid[ATT_UUID_128_LEN];
105 
106     /**
107      *  Attribute Permission (@see attm_perm_mask)
108      */
109     uint16_t perm;
110 
111 
112     /**
113      * Maximum length of the attribute
114      *
115      * Note:
116      * For Included Services and Characteristic Declarations, this field contains targeted
117      * handle.
118      *
119      * For Characteristic Extended Properties, this field contains 2 byte value
120      *
121      * Not used Client Characteristic Configuration and Server Characteristic Configuration,
122      * this field is not used.
123      */
124     uint16_t max_len;
125 
126     /**
127      * Attribute Extended permissions
128      *
129      * Extended Value permission bit field
130      *
131      *   15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
132      * +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
133      * | RI |UUID_LEN |EKS |                       Reserved                            |
134      * +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
135     *
136      * Bit [0-11] : Reserved
137      * Bit [12]   : Encryption key Size must be 16 bytes
138      * Bit [13-14]: UUID Length             (0 = 16 bits, 1 = 32 bits, 2 = 128 bits, 3 = RFU)
139      * Bit [15]   : Trigger Read Indication (0 = Value present in Database, 1 = Value not present in Database)
140      */
141     uint16_t ext_perm;
142 };
143 
144 /**
145  * Service description
146  */
147 struct gattm_svc_desc
148 {
149     /// Attribute Start Handle (0 = dynamically allocated)
150     uint16_t start_hdl;
151     /// Task identifier that manages service
152     uint16_t task_id;
153 
154     /**
155      *    7    6    5    4    3    2    1    0
156      * +----+----+----+----+----+----+----+----+
157      * |SEC |UUID_LEN |DIS |  AUTH   |EKS | MI |
158      * +----+----+----+----+----+----+----+----+
159      *
160      * Bit [0]  : Task that manage service is multi-instantiated (Connection index is conveyed)
161      * Bit [1]  : Encryption key Size must be 16 bytes
162      * Bit [2-3]: Service Permission      (0 = NO_AUTH, 1 = UNAUTH, 2 = AUTH, 3 = Secure Connect)
163      * Bit [4]  : Disable the service
164      * Bit [5-6]: UUID Length             (0 = 16 bits, 1 = 32 bits, 2 = 128 bits, 3 = RFU)
165      * Bit [7]  : Secondary Service       (0 = Primary Service, 1 = Secondary Service)
166      */
167     uint8_t perm;
168 
169     /// Number of attributes
170     uint8_t nb_att;
171 
172     /** Service  UUID */
173     uint8_t uuid[ATT_UUID_128_LEN];
174     /**
175      * List of attribute description present in service.
176      */
177     struct gattm_att_desc atts[__ARRAY_EMPTY];
178 };
179 
180 
181 /// Add service in database request
182 struct gattm_add_svc_req
183 {
184     /// service description
185     struct gattm_svc_desc svc_desc;
186 };
187 
188 /// Add service in database response
189 struct gattm_add_svc_rsp
190 {
191     /// Start handle of allocated service in attribute database
192     uint16_t start_hdl;
193     /// Return status of service allocation in attribute database.
194     uint8_t status;
195 };
196 
197 /* Service management */
198 /// Get permission settings of service request
199 struct gattm_svc_get_permission_req
200 {
201     /// Service start attribute handle
202     uint16_t start_hdl;
203 };
204 
205 /// Get permission settings of service response
206 struct gattm_svc_get_permission_rsp
207 {
208     /// Service start attribute handle
209     uint16_t start_hdl;
210     /// service permission
211     uint8_t perm;
212     /// Return status
213     uint8_t status;
214 };
215 
216 /// Set permission settings of service request
217 struct gattm_svc_set_permission_req
218 {
219     /// Service start attribute handle
220     uint16_t start_hdl;
221     /// service permission
222     uint8_t perm;
223 };
224 
225 /// Set permission settings of service response
226 struct gattm_svc_set_permission_rsp
227 {
228     /// Service start attribute handle
229     uint16_t start_hdl;
230     /// Return status
231     uint8_t status;
232 };
233 
234 
235 /* Attribute management */
236 /// Get permission settings of attribute request
237 struct gattm_att_get_permission_req
238 {
239     /// Handle of the attribute
240     uint16_t handle;
241 };
242 
243 /// Get permission settings of attribute response
244 struct gattm_att_get_permission_rsp
245 {
246     /// Handle of the attribute
247     uint16_t handle;
248     /// Attribute permission
249     uint16_t perm;
250     /// Extended Attribute permission
251     uint16_t ext_perm;
252     /// Return status
253     uint8_t status;
254 };
255 
256 /// Set permission settings of attribute request
257 struct gattm_att_set_permission_req
258 {
259     /// Handle of the attribute
260     uint16_t handle;
261     /// Attribute permission
262     uint16_t perm;
263     /// Extended Attribute permission
264     uint16_t ext_perm;
265 };
266 
267 /// Set permission settings of attribute response
268 struct gattm_att_set_permission_rsp
269 {
270     /// Handle of the attribute
271     uint16_t handle;
272     /// Return status
273     uint8_t status;
274 };
275 
276 
277 /// Get attribute value request
278 struct gattm_att_get_value_req
279 {
280     /// Handle of the attribute
281     uint16_t handle;
282 };
283 
284 /// Get attribute value response
285 struct gattm_att_get_value_rsp
286 {
287     /// Handle of the attribute
288     uint16_t handle;
289     /// Attribute value length
290     uint16_t length;
291     /// Return status
292     uint8_t status;
293     /// Attribute value
294     uint8_t value[__ARRAY_EMPTY];
295 };
296 
297 /// Set attribute value request
298 struct gattm_att_set_value_req
299 {
300     /// Handle of the attribute
301     uint16_t handle;
302     /// Attribute value length
303     uint16_t length;
304     /// Attribute value
305     uint8_t value[__ARRAY_EMPTY];
306 };
307 
308 /// Set attribute value response
309 struct gattm_att_set_value_rsp
310 {
311     /// Handle of the attribute
312     uint16_t handle;
313     /// Return status
314     uint8_t status;
315 };
316 
317 /// DEBUG ONLY: Destroy Attribute database request
318 struct gattm_destroy_db_req
319 {
320     /// New Gap Start Handle
321     uint16_t gap_hdl;
322     /// New Gatt Start Handle
323     uint16_t gatt_hdl;
324 };
325 
326 /// DEBUG ONLY: Destroy Attribute database Response
327 struct gattm_destroy_db_rsp
328 {
329     /// Return status
330     uint8_t status;
331 };
332 
333 
334 /// Service information
335 struct gattm_svc_info
336 {
337     /// Service start handle
338     uint16_t start_hdl;
339     /// Service end handle
340     uint16_t end_hdl;
341     /// Service task_id
342     uint16_t task_id;
343     /// Service permission
344     uint8_t perm;
345 };
346 
347 /// DEBUG ONLY: Retrieve list of services response
348 struct gattm_svc_get_list_rsp
349 {
350     /// Return status
351     uint8_t status;
352     /// Number of services
353     uint8_t nb_svc;
354     /// Array of information about services
355     struct gattm_svc_info svc[__ARRAY_EMPTY];
356 };
357 
358 /// DEBUG ONLY: Retrieve information of attribute request
359 struct  gattm_att_get_info_req
360 {
361     /// Attribute Handle
362     uint16_t handle;
363 };
364 
365 /// DEBUG ONLY: Retrieve information of attribute response
366 struct  gattm_att_get_info_rsp
367 {
368     /// Return status
369     uint8_t status;
370     /// UUID Length
371     uint8_t uuid_len;
372     /// Attribute Handle
373     uint16_t handle;
374     /// Attribute Permissions
375     uint16_t perm;
376     /// Extended Attribute permission
377     uint16_t ext_perm;
378     /// UUID value
379     uint8_t uuid[ATT_UUID_128_LEN];
380 };
381 
382 /*
383  * FUNCTION DECLARATIONS
384  ****************************************************************************************
385  */
386 
387 /// @} GATTMTASK
388 #endif // GATTM_TASK_H_
389