1 /**
2  ****************************************************************************************
3  *
4  * @file gattc_task.h
5  *
6  * @brief Header file - GATTCTASK.
7  *
8  * Copyright (C) RivieraWaves 2009-2016
9  *
10  *
11  ****************************************************************************************
12  */
13 
14 #ifndef GATTC_TASK_H_
15 #define GATTC_TASK_H_
16 
17 /**
18  ****************************************************************************************
19  * @addtogroup GATTCTASK Task
20  * @ingroup GATTC
21  * @brief Handles ALL messages to/from GATT Controller block.
22  *
23  * The GATTCTASK is responsible for managing the messages coming from
24  * the attribute layer and host-level layers for dedicated connection.
25  * The task includes services and characteristic discovery, configuration exchanges
26  * and attribute value access operations (reading, writing, notification and indication).
27  *
28  * Messages may originate from @ref ATTC "ATTC", @ref ATTS "ATTS", @ref GAP "GAP"
29  * and Application.
30  *
31  * @{
32  ****************************************************************************************
33  */
34 /*
35  * INCLUDE FILES
36  ****************************************************************************************
37  */
38 #include "att.h"
39 #include "rwip_task.h" // Task definitions
40 //#include "compiler.h"
41 #include "ble_arch.h"
42 #include <stdbool.h>
43 /*
44  * DEFINES
45  ****************************************************************************************
46  */
47 
48 /// GATT Task messages
49 enum gattc_msg_id
50 {
51     /* Default event */
52     /// Command Complete event
53     GATTC_CMP_EVT = TASK_FIRST_MSG(TASK_ID_GATTC),
54 
55     /* ATTRIBUTE CLIENT */
56     /// Server configuration request
57     GATTC_EXC_MTU_CMD,
58     /// Indicate that the ATT MTU has been updated (negotiated)
59     GATTC_MTU_CHANGED_IND,
60 
61     /*Discover All Services */
62     /*Discover Services by Service UUID*/
63     /*Find Included Services*/
64     /*Discover Characteristics by UUID*/
65     /*Discover All Characteristics of a Service*/
66     /*Discover All Characteristic Descriptors*/
67     /// Discovery command
68     GATTC_DISC_CMD,
69     /* GATT -> HL: Events to Upper layer */
70     /*Discover All Services*/
71     /// Discovery services indication
72     GATTC_DISC_SVC_IND,
73     /*Find Included Services*/
74     /// Discover included services indication
75     GATTC_DISC_SVC_INCL_IND,
76     /*Discover All Characteristics of a Service*/
77     /// Discover characteristic indication
78     GATTC_DISC_CHAR_IND,
79     /*Discover All Characteristic Descriptors*/
80     /// Discovery characteristic descriptor indication
81     GATTC_DISC_CHAR_DESC_IND,
82 
83     /*Read Value*/
84     /*Read Using UUID*/
85     /*Read Long Value*/
86     /*Read Multiple Values*/
87     /// Read command
88     GATTC_READ_CMD,
89     /// Read response
90     GATTC_READ_IND,
91 
92     /*Write without response*/
93     /*Write without response with Authentication*/
94     /*Write Characteristic Value*/
95     /*Signed Write Characteristic Value*/
96     /*Write Long Characteristic Value*/
97     /*Characteristic Value Reliable Write*/
98     /*Write Characteristic Descriptors*/
99     /*Write Long Characteristic Descriptors*/
100     /*Characteristic Value Reliable Write*/
101     /// Write command request
102     GATTC_WRITE_CMD,
103 
104     /* Cancel / Execute pending write operations */
105     /// Execute write characteristic request
106     GATTC_EXECUTE_WRITE_CMD,
107 
108     /* Reception of an indication or notification from peer device. */
109     /// peer device triggers an event (notification)
110     GATTC_EVENT_IND,
111     /// peer device triggers an event that requires a confirmation (indication)
112     GATTC_EVENT_REQ_IND,
113     /// Confirm reception of event (trigger a confirmation message)
114     GATTC_EVENT_CFM,
115 
116     /// Registration to peer device events (Indication/Notification).
117     GATTC_REG_TO_PEER_EVT_CMD,
118 
119     /* -------------------------- ATTRIBUTE SERVER ------------------------------- */
120     /*Notify Characteristic*/
121     /*Indicate Characteristic*/
122     /// send an event to peer device
123     GATTC_SEND_EVT_CMD,
124 
125     /* Service Changed Characteristic Indication */
126     /**
127      * Send a Service Changed indication to a device
128      * (message structure is struct gattm_svc_changed_ind_req)
129      */
130     GATTC_SEND_SVC_CHANGED_CMD,
131     /**
132      * Inform the application when sending of Service Changed indications has been
133      * enabled or disabled
134      */
135     GATTC_SVC_CHANGED_CFG_IND,
136 
137     /* Indicate that read operation is requested. */
138     /// Read command indicated to upper layers.
139     GATTC_READ_REQ_IND,
140     /// REad command confirmation from upper layers.
141     GATTC_READ_CFM,
142 
143     /* Indicate that write operation is requested. */
144     /// Write command indicated to upper layers.
145     GATTC_WRITE_REQ_IND,
146     /// Write command confirmation from upper layers.
147     GATTC_WRITE_CFM,
148 
149     /* Indicate that write operation is requested. */
150     /// Request Attribute info to upper layer - could be trigger during prepare write
151     GATTC_ATT_INFO_REQ_IND,
152     /// Attribute info from upper layer confirmation
153     GATTC_ATT_INFO_CFM,
154 
155     /* ----------------------- SERVICE DISCOVERY PROCEDURE  --------------------------- */
156     /// Service Discovery command
157     GATTC_SDP_SVC_DISC_CMD,
158     /// Service Discovery indicate that a service has been found.
159     GATTC_SDP_SVC_IND,
160 
161     /* -------------------------- TRANSACTION ERROR EVENT ----------------------------- */
162     /// Transaction Timeout Error Event no more transaction will be accepted
163     GATTC_TRANSACTION_TO_ERROR_IND,
164 
165     /* ------------------------------- Internal API ----------------------------------- */
166     /// Client Response timeout indication
167     GATTC_CLIENT_RTX_IND,
168     /// Server indication confirmation timeout indication
169     GATTC_SERVER_RTX_IND,
170 };
171 
172 
173 /// request operation type - application interface
174 enum gattc_operation
175 {
176     /*              Attribute Client Flags              */
177     /* No Operation (if nothing has been requested)     */
178     /* ************************************************ */
179     /// No operation
180     GATTC_NO_OP                                    = 0x00,
181 
182     /* Operation flags for MTU Exchange                 */
183     /* ************************************************ */
184     /// Perform MTU exchange
185     GATTC_MTU_EXCH,
186 
187     /*      Operation flags for discovery operation     */
188     /* ************************************************ */
189     /// Discover all services
190     GATTC_DISC_ALL_SVC,
191     /// Discover services by UUID
192     GATTC_DISC_BY_UUID_SVC,
193     /// Discover included services
194     GATTC_DISC_INCLUDED_SVC,
195     /// Discover all characteristics
196     GATTC_DISC_ALL_CHAR,
197     /// Discover characteristic by UUID
198     GATTC_DISC_BY_UUID_CHAR,
199     /// Discover characteristic descriptor
200     GATTC_DISC_DESC_CHAR,
201 
202     /* Operation flags for reading attributes           */
203     /* ************************************************ */
204     /// Read attribute
205     GATTC_READ,
206     /// Read long attribute
207     GATTC_READ_LONG,
208     /// Read attribute by UUID
209     GATTC_READ_BY_UUID,
210     /// Read multiple attribute
211     GATTC_READ_MULTIPLE,
212 
213     /* Operation flags for writing/modifying attributes */
214     /* ************************************************ */
215     /// Write attribute
216     GATTC_WRITE,
217     /// Write no response
218     GATTC_WRITE_NO_RESPONSE,
219     /// Write signed
220     GATTC_WRITE_SIGNED,
221     /// Execute write
222     GATTC_EXEC_WRITE,
223 
224     /* Operation flags for registering to peer device   */
225     /* events                                           */
226     /* ************************************************ */
227     /// Register to peer device events
228     GATTC_REGISTER,
229     /// Unregister from peer device events
230     GATTC_UNREGISTER,
231 
232     /* Operation flags for sending events to peer device*/
233     /* ************************************************ */
234     /// Send an attribute notification
235     GATTC_NOTIFY,
236     /// Send an attribute indication
237     GATTC_INDICATE,
238     /// Send a service changed indication
239     GATTC_SVC_CHANGED,
240 
241     /* Service Discovery Procedure                      */
242     /* ************************************************ */
243     /// Search specific service
244     GATTC_SDP_DISC_SVC,
245     /// Search for all services
246     GATTC_SDP_DISC_SVC_ALL,
247     /// Cancel Service Discovery Procedure
248     GATTC_SDP_DISC_CANCEL,
249 };
250 
251 /// Service Discovery Attribute type
252 enum gattc_sdp_att_type
253 {
254     /// No Attribute Information
255     GATTC_SDP_NONE,
256     /// Included Service Information
257     GATTC_SDP_INC_SVC,
258     /// Characteristic Declaration
259     GATTC_SDP_ATT_CHAR,
260     /// Attribute Value
261     GATTC_SDP_ATT_VAL,
262     /// Attribute Descriptor
263     GATTC_SDP_ATT_DESC,
264 };
265 
266 /// Command complete event data structure
267 struct gattc_op_cmd
268 {
269     /// GATT request type
270     uint8_t operation;
271     /// operation sequence number
272     uint16_t seq_num;
273 };
274 
275 /// Command complete event data structure
276 struct gattc_cmp_evt
277 {
278     /// GATT request type
279     uint8_t operation;
280     /// Status of the request
281     uint8_t status;
282     /// operation sequence number - provided when operation is started
283     uint16_t seq_num;
284 };
285 
286 
287 /// Service Discovery Command Structure
288 struct gattc_exc_mtu_cmd
289 {
290     /// GATT request type
291     uint8_t operation;
292     /// operation sequence number
293     uint16_t seq_num;
294 };
295 
296 /// Indicate that the ATT MTU has been updated (negotiated)
297 struct gattc_mtu_changed_ind
298 {
299     /// Exchanged MTU value
300     uint16_t mtu;
301     /// operation sequence number
302     uint16_t seq_num;
303 };
304 
305 /// Service Discovery Command Structure
306 struct gattc_disc_cmd
307 {
308     /// GATT request type
309     uint8_t  operation;
310     /// UUID length
311     uint8_t  uuid_len;
312     /// operation sequence number
313     uint16_t seq_num;
314     /// start handle range
315     uint16_t start_hdl;
316     /// start handle range
317     uint16_t end_hdl;
318     /// UUID
319     uint8_t  uuid[__ARRAY_EMPTY];
320 };
321 
322 
323 /// Discover Service indication Structure
324 struct gattc_disc_svc_ind
325 {
326     /// start handle
327     uint16_t start_hdl;
328     /// end handle
329     uint16_t end_hdl;
330     /// UUID length
331     uint8_t  uuid_len;
332     /// service UUID
333     uint8_t  uuid[__ARRAY_EMPTY];
334 };
335 
336 /// Discover Service indication Structure
337 struct gattc_disc_svc_incl_ind
338 {
339     /// element handle
340     uint16_t attr_hdl;
341     /// start handle
342     uint16_t start_hdl;
343     /// end handle
344     uint16_t end_hdl;
345     /// UUID length
346     uint8_t uuid_len;
347     /// included service UUID
348     uint8_t uuid[__ARRAY_EMPTY];
349 };
350 
351 /// Discovery All Characteristic indication Structure
352 struct gattc_disc_char_ind
353 {
354     /// database element handle
355     uint16_t attr_hdl;
356     /// pointer attribute handle to UUID
357     uint16_t pointer_hdl;
358     /// properties
359     uint8_t prop;
360     /// UUID length
361     uint8_t uuid_len;
362     /// characteristic UUID
363     uint8_t uuid[__ARRAY_EMPTY];
364 };
365 
366 /// Discovery Characteristic Descriptor indication Structure
367 struct gattc_disc_char_desc_ind
368 {
369     /// database element handle
370     uint16_t attr_hdl;
371     /// UUID length
372     uint8_t uuid_len;
373     /// Descriptor UUID
374     uint8_t uuid[__ARRAY_EMPTY];
375 };
376 
377 
378 /// Simple Read (GATTC_READ or GATTC_READ_LONG)
379 struct gattc_read_simple
380 {
381     /// attribute handle
382     uint16_t handle;
383     /// start offset in data payload
384     uint16_t offset;
385     /// Length of data to read (0 = read all)
386     uint16_t length;
387 };
388 
389 /// Read by UUID: search UUID and read it's characteristic value (GATTC_READ_BY_UUID)
390 /// Note: it doesn't perform an automatic read long.
391 struct gattc_read_by_uuid
392 {
393     /// Start handle
394     uint16_t start_hdl;
395     /// End handle
396     uint16_t end_hdl;
397     /// Size of UUID
398     uint8_t uuid_len;
399     /// UUID value
400     uint8_t uuid[__ARRAY_EMPTY];
401 };
402 
403 /// Read Multiple short characteristic (GATTC_READ_MULTIPLE)
404 struct gattc_read_multiple
405 {
406     /// attribute handle
407     uint16_t handle;
408     /// Known Handle length (shall be != 0)
409     uint16_t len;
410 };
411 
412 /// Read command (Simple, Long, Multiple, or by UUID)
413 struct gattc_read_cmd
414 {
415     /// request type
416     uint8_t operation;
417     /// number of read (only used for multiple read)
418     uint8_t nb;
419     /// operation sequence number
420     uint16_t seq_num;
421 
422     /// request union according to read type
423     union gattc_read_req
424     {
425         /// Simple Read (GATTC_READ or GATTC_READ_LONG)
426         struct gattc_read_simple simple;
427         /// Read by UUID (GATTC_READ_BY_UUID)
428         struct gattc_read_by_uuid by_uuid;
429         /// Read Multiple short characteristic (GATTC_READ_MULTIPLE)
430         struct gattc_read_multiple multiple[1];
431     } req;
432 };
433 
434 /// Attribute value read indication
435 struct gattc_read_ind
436 {
437     /// Attribute handle
438     uint16_t handle;
439     /// Read offset
440     uint16_t offset;
441     /// Read length
442     uint16_t length;
443     /// Handle value
444     uint8_t value[__ARRAY_EMPTY];
445 };
446 
447 /// Write peer attribute value command
448 struct gattc_write_cmd
449 {
450     /// Request type
451     uint8_t operation;
452     /// Perform automatic execution
453     /// (if false, an ATT Prepare Write will be used this shall be use for reliable write)
454     bool auto_execute;
455     /// operation sequence number
456     uint16_t seq_num;
457     /// Attribute handle
458     uint16_t handle;
459     /// Write offset
460     uint16_t offset;
461     /// Write length
462     uint16_t length;
463     /// Internal write cursor shall be initialized to 0
464     uint16_t cursor;
465     /// Value to write
466     uint8_t value[__ARRAY_EMPTY];
467 };
468 
469 /// Write peer attribute value command
470 struct gattc_execute_write_cmd
471 {
472     /// Request type
473     uint8_t operation;
474 
475     /// [True = perform/False cancel] pending write operations
476     bool execute;
477     /// operation sequence number
478     uint16_t seq_num;
479 };
480 /// peer device triggers an event (notification)
481 struct gattc_event_ind
482 {
483     /// Event Type
484     uint8_t type;
485     /// Data length
486     uint16_t length;
487     /// Attribute handle
488     uint16_t handle;
489     /// Event Value
490     uint8_t value[__ARRAY_EMPTY];
491 };
492 
493 /// peer device triggers an event that requires a confirmation (indication)
494 struct gattc_event_req_ind
495 {
496     /// Event Type
497     uint8_t type;
498     /// Data length
499     uint16_t length;
500     /// Attribute handle
501     uint16_t handle;
502     /// Event Value
503     uint8_t value[__ARRAY_EMPTY];
504 };
505 
506 /// Confirm reception of event (trigger a confirmation message)
507 struct gattc_event_cfm
508 {
509     /// Attribute handle
510     uint16_t handle;
511 };
512 
513 /// Register to peer device events command
514 struct gattc_reg_to_peer_evt_cmd
515 {
516     /// Request type
517     uint8_t operation;
518     /// operation sequence number
519     uint16_t seq_num;
520     /// attribute start handle
521     uint16_t start_hdl;
522     /// attribute end handle
523     uint16_t end_hdl;
524 };
525 
526 /// Send an event to peer device
527 struct gattc_send_evt_cmd
528 {
529     /// Request type (notification / indication)
530     uint8_t operation;
531     /// operation sequence number
532     uint16_t seq_num;
533     /// characteristic handle
534     uint16_t handle;
535     /// length of packet to send
536     uint16_t length;
537     /// data value
538     uint8_t  value[__ARRAY_EMPTY];
539 };
540 
541 /// Inform that attribute value is requested by lower layers.
542 struct gattc_read_req_ind
543 {
544     /// Handle of the attribute that has to be read
545     uint16_t handle;
546 };
547 
548 /// Confirm Read Request requested by GATT to profile
549 struct gattc_read_cfm
550 {
551     /// Handle of the attribute read
552     uint16_t handle;
553     /// Data length read
554     uint16_t length;
555     /// Status of read command execution by upper layers
556     uint8_t status;
557     /// attribute data value
558     uint8_t  value[__ARRAY_EMPTY];
559 };
560 
561 /// Inform that a modification of database has been requested by peer device.
562 struct gattc_write_req_ind
563 {
564     /// Handle of the attribute that has to be written
565     uint16_t handle;
566     /// offset at which the data has to be written
567     uint16_t offset;
568     /// Data length to be written
569     uint16_t length;
570     /// Data to be written in attribute database
571     uint8_t  value[__ARRAY_EMPTY];
572 };
573 
574 /// Confirm modification of database from upper layer when requested by peer device.
575 struct gattc_write_cfm
576 {
577     /// Handle of the attribute written
578     uint16_t handle;
579     /// Status of write command execution by upper layers
580     uint8_t status;
581 };
582 
583 /// Parameters for @ref GATTC_SEND_SVC_CHANGED_CMD message
584 struct gattc_send_svc_changed_cmd
585 {
586     /// Request Type
587     uint8_t operation;
588     /// operation sequence number
589     uint16_t seq_num;
590     /// Start of Affected Attribute Handle Range
591     uint16_t svc_shdl;
592     /// End of Affected Attribute Handle Range
593     uint16_t svc_ehdl;
594 };
595 
596 /// Parameters for @ref GATTC_SVC_CHANGED_CFG_IND and @ref GATTC_SVC_CHANGED_SET_CFG_REQ message
597 struct gattc_svc_changed_cfg
598 {
599     /**
600      * Current value of the Client Characteristic Configuration descriptor for the Service
601      * Changed characteristic
602      */
603     uint16_t ind_cfg;
604 };
605 
606 
607 /// Request Attribute info to upper layer - could be trigger during prepare write
608 struct gattc_att_info_req_ind
609 {
610     /// Handle of the attribute for which info are requested
611     uint16_t handle;
612 };
613 
614 /// Attribute info from upper layer confirmation
615 struct gattc_att_info_cfm
616 {
617     /// Handle of the attribute
618     uint16_t handle;
619     /// Current length of the attribute
620     uint16_t length;
621     /// use to know if it's possible to modify the attribute
622     /// can contains authorization or application error code.
623     uint8_t  status;
624 };
625 
626 
627 /// Service Discovery command
628 struct gattc_sdp_svc_disc_cmd
629 {
630     /// GATT Request Type
631     /// - GATTC_SDP_DISC_SVC Search specific service
632     /// - GATTC_SDP_DISC_SVC_ALL Search for all services
633     /// - GATTC_SDP_DISC_CANCEL Cancel Service Discovery Procedure
634     uint8_t operation;
635     /// Service UUID Length
636     uint8_t  uuid_len;
637     /// operation sequence number
638     uint16_t seq_num;
639     /// Search start handle
640     uint16_t start_hdl;
641     /// Search end handle
642     uint16_t end_hdl;
643     /// Service UUID
644     uint8_t  uuid[ATT_UUID_128_LEN];
645 };
646 
647 
648 /// Information about included service
649 struct gattc_sdp_include_svc
650 {
651     /// Attribute Type
652     /// - GATTC_SDP_INC_SVC: Included Service Information
653     uint8_t att_type;
654     /// Included service UUID Length
655     uint8_t uuid_len;
656     /// Included Service UUID
657     uint8_t  uuid[ATT_UUID_128_LEN];
658     /// Included service Start Handle
659     uint16_t start_hdl;
660     /// Included service End Handle
661     uint16_t end_hdl;
662 };
663 
664 /// Information about attribute characteristic
665 struct gattc_sdp_att_char
666 {
667     /// Attribute Type
668     /// - GATTC_SDP_ATT_CHAR: Characteristic Declaration
669     uint8_t att_type;
670     /// Value property
671     uint8_t prop;
672     /// Value Handle
673     uint16_t handle;
674 };
675 
676 /// Information about attribute
677 struct gattc_sdp_att
678 {
679     /// Attribute Type
680     /// - GATTC_SDP_ATT_VAL: Attribute Value
681     /// - GATTC_SDP_ATT_DESC: Attribute Descriptor
682     uint8_t  att_type;
683     /// Attribute UUID Length
684     uint8_t  uuid_len;
685     /// Attribute UUID
686     uint8_t  uuid[ATT_UUID_128_LEN];
687 };
688 
689 /// Attribute information
690 union gattc_sdp_att_info
691 {
692     /// Attribute Type
693     uint8_t att_type;
694     /// Information about attribute characteristic
695     struct gattc_sdp_att_char att_char;
696     /// Information about included service
697     struct gattc_sdp_include_svc inc_svc;
698     /// Information about attribute
699     struct gattc_sdp_att att;
700 };
701 
702 
703 /// Service Discovery indicate that a service has been found.
704 struct gattc_sdp_svc_ind
705 {
706     /// Service UUID Length
707     uint8_t  uuid_len;
708     /// Service UUID
709     uint8_t  uuid[ATT_UUID_128_LEN];
710     /// Service start handle
711     uint16_t start_hdl;
712     /// Service end handle
713     uint16_t end_hdl;
714     /// attribute information present in the service
715     /// (length = end_hdl - start_hdl)
716     union gattc_sdp_att_info info[__ARRAY_EMPTY];
717 };
718 
719 
720 
721 /// @} GATTCTASK
722 #endif // GATTC_TASK_H_
723