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 	uint8_t peer_id_addr[BDADDR_SIZE];
36 	uint8_t peer_id_addr_type:1;
37 	uint8_t peer_addr_resolved:1;
38 
39 	uint8_t rx_enable:1;
40 
41 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_ADI_SUPPORT)
42 	uint8_t nodups:1;
43 #endif
44 
45 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING) && \
46 	!defined(CONFIG_BT_CTLR_CTEINLINE_SUPPORT)
47 	/* Member used to notify event done handler to terminate sync scanning.
48 	 * Used only when no HW support for parsing PDU for CTEInfo.
49 	 */
50 	uint8_t is_term:1;
51 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING && !CONFIG_BT_CTLR_CTEINLINE_SUPPORT */
52 
53 	uint8_t is_stop:1; /* sync terminate or cancel requested */
54 	uint8_t sync_expire:3; /* countdown of 6 before fail to establish */
55 
56 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
57 	uint8_t enc : 1;
58 	uint8_t num_bis : 5;
59 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
60 
61 	uint8_t sid;
62 
63 	/* node rx type with memory aligned storage for sync lost reason.
64 	 * HCI will reference the value using the pdu member of
65 	 * struct node_rx_pdu.
66 	 */
67 	struct {
68 		struct node_rx_pdu rx;
69 		/* Dummy declaration to ensure space allocated to hold one pdu bytes */
70 		uint8_t dummy;
71 	} node_rx_lost;
72 
73 	/* Not-Null when sync was setup and Controller is waiting for first AUX_SYNC_IND PDU.
74 	 * It means the sync was not estalished yet.
75 	 */
76 	struct node_rx_pdu *node_rx_sync_estab;
77 
78 #if defined(CONFIG_BT_CTLR_SCAN_AUX_USE_CHAINS)
79 	/* Extra node_rx for generating incomplete report */
80 	struct node_rx_pdu *rx_incomplete;
81 #endif /* CONFIG_BT_CTLR_SCAN_AUX_USE_CHAINS */
82 
83 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
84 	struct {
85 		struct node_rx_pdu *node_rx_estab;
86 
87 		/* Non-Null when creating sync, reset in ISR context on
88 		 * synchronisation state and checked in Thread context when
89 		 * cancelling sync create, hence the volatile keyword.
90 		 */
91 		struct ll_sync_iso_set *volatile sync_iso;
92 	} iso;
93 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
94 
95 #if !defined(CONFIG_BT_CTLR_SCAN_AUX_USE_CHAINS)
96 	uint16_t data_len;
97 #endif /* !CONFIG_BT_CTLR_SCAN_AUX_USE_CHAINS */
98 #if defined(CONFIG_BT_CTLR_SYNC_TRANSFER_SENDER)
99 	uint16_t interval;
100 #endif /* CONFIG_BT_CTLR_SYNC_TRANSFER_SENDER */
101 };
102 
103 struct node_rx_sync {
104 	uint8_t status;
105 	uint8_t  phy;
106 	uint16_t interval;
107 	uint8_t  sca;
108 };
109 
110 struct node_rx_past_received {
111 	struct node_rx_sync rx_sync;
112 	uint16_t conn_handle;
113 	uint16_t service_data;
114 };
115 
116 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
117 struct ll_sync_iso_set {
118 	struct ull_hdr ull;
119 	struct lll_sync_iso lll;
120 
121 	/* Periodic Advertising Sync that contained the BIGInfo */
122 	struct ll_sync_set *sync;
123 
124 	/* Pointer to semaphore used for LLL flushing */
125 	struct k_sem *flush_sem;
126 
127 	/* Periodic Advertising Sync timeout */
128 	uint16_t timeout;
129 	uint16_t volatile timeout_reload; /* Non-zero when sync established */
130 	uint16_t timeout_expire; /* timeout countdown */
131 	uint8_t big_handle;
132 
133 	/* Encryption */
134 	uint8_t gltk[16];
135 
136 	/* node rx type with memory aligned storage for sync lost reason.
137 	 * HCI will reference the value using the pdu member of
138 	 * struct node_rx_pdu.
139 	 */
140 	struct {
141 		struct node_rx_pdu rx;
142 		/* Dummy declaration to ensure space allocated to hold two pdu bytes */
143 		uint8_t dummy[2];
144 	} node_rx_lost;
145 };
146 
147 struct node_rx_sync_iso {
148 	uint8_t status;
149 	uint16_t interval;
150 };
151 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
152