1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define LL_SYNC_STATE_IDLE       0x00
8 #define LL_SYNC_STATE_ADDR_MATCH 0x01
9 #define LL_SYNC_STATE_CREATED    0x02
10 
11 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
12 struct ll_sync_iso_set;
13 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
14 
15 struct ll_sync_set {
16 	struct ull_hdr ull;
17 	struct lll_sync lll;
18 
19 	uint16_t skip;
20 	uint16_t timeout;
21 	/* Non-zero when sync is setup. It can be in two sub-stated:
22 	 * - Waiting for first AUX_SYNC_IND, before sync established was notified to Host.
23 	 *   If sync establishment is in progress node_rx_sync_estab is not NULL.
24 	 * - sync is already established, node_rx_sync_estab is NULL.
25 	 */
26 	uint16_t volatile timeout_reload;
27 	uint16_t timeout_expire;
28 
29 	/* Member to store periodic advertising sync prepare.
30 	 * Also serves as a flag to inform if sync established was
31 	 * already generated.
32 	 */
33 	void (*lll_sync_prepare)(void *param);
34 
35 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_SYNC) || \
36 	defined(CONFIG_BT_CTLR_SYNC_PERIODIC_ADI_SUPPORT)
37 	uint8_t peer_id_addr[BDADDR_SIZE];
38 	uint8_t peer_id_addr_type:1;
39 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_SYNC ||
40 	* CONFIG_BT_CTLR_SYNC_PERIODIC_ADI_SUPPORT
41 	*/
42 
43 	uint8_t rx_enable:1;
44 
45 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_ADI_SUPPORT)
46 	uint8_t nodups:1;
47 #endif
48 
49 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING) && \
50 	!defined(CONFIG_BT_CTLR_CTEINLINE_SUPPORT)
51 	/* Member used to notify event done handler to terminate sync scanning.
52 	 * Used only when no HW support for parsing PDU for CTEInfo.
53 	 */
54 	uint8_t is_term:1;
55 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING && !CONFIG_BT_CTLR_CTEINLINE_SUPPORT */
56 
57 	uint8_t is_stop:1; /* sync terminate or cancel requested */
58 	uint8_t sync_expire:3; /* countdown of 6 before fail to establish */
59 
60 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
61 	uint8_t enc : 1;
62 	uint8_t num_bis : 5;
63 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
64 
65 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_SYNC)
66 	uint8_t sid;
67 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_SYNC */
68 
69 	/* node rx type with memory aligned storage for sync lost reason.
70 	 * HCI will reference the value using the pdu member of
71 	 * struct node_rx_pdu.
72 	 */
73 	struct {
74 		struct node_rx_pdu rx;
75 		/* Dummy declaration to ensure space allocated to hold one pdu bytes */
76 		uint8_t dummy;
77 	} node_rx_lost;
78 
79 	/* Not-Null when sync was setup and Controller is waiting for first AUX_SYNC_IND PDU.
80 	 * It means the sync was not estalished yet.
81 	 */
82 	struct node_rx_pdu *node_rx_sync_estab;
83 
84 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
85 	struct {
86 		struct node_rx_pdu *node_rx_estab;
87 
88 		/* Non-Null when creating sync, reset in ISR context on
89 		 * synchronisation state and checked in Thread context when
90 		 * cancelling sync create, hence the volatile keyword.
91 		 */
92 		struct ll_sync_iso_set *volatile sync_iso;
93 	} iso;
94 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
95 
96 	uint16_t data_len;
97 };
98 
99 struct node_rx_sync {
100 	uint8_t status;
101 	uint8_t  phy;
102 	uint16_t interval;
103 	uint8_t  sca;
104 };
105 
106 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
107 struct ll_sync_iso_set {
108 	struct ull_hdr ull;
109 	struct lll_sync_iso lll;
110 
111 	/* Periodic Advertising Sync that contained the BIGInfo */
112 	struct ll_sync_set *sync;
113 
114 	/* Periodic Advertising Sync timeout */
115 	uint16_t timeout;
116 	uint16_t volatile timeout_reload; /* Non-zero when sync established */
117 	uint16_t timeout_expire; /* timeout countdown */
118 	uint8_t big_handle;
119 
120 	/* Encryption */
121 	uint8_t gltk[16];
122 
123 	/* node rx type with memory aligned storage for sync lost reason.
124 	 * HCI will reference the value using the pdu member of
125 	 * struct node_rx_pdu.
126 	 */
127 	struct {
128 		struct node_rx_pdu rx;
129 		/* Dummy declaration to ensure space allocated to hold two pdu bytes */
130 		uint8_t dummy[2];
131 	} node_rx_lost;
132 };
133 
134 struct node_rx_sync_iso {
135 	uint8_t status;
136 	uint16_t interval;
137 };
138 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
139