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