xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/controller/include/controller/ble_ll_ctrl.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef H_BLE_LL_CTRL_
21 #define H_BLE_LL_CTRL_
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /*
28  * LL control procedures. This "enumeration" is not in the specification;
29  * It is used to determine which LL control procedure is currently running
30  * in a connection and which ones may be pending.
31  */
32 #define BLE_LL_CTRL_PROC_CONN_UPDATE    (0)
33 #define BLE_LL_CTRL_PROC_CHAN_MAP_UPD   (1)
34 #define BLE_LL_CTRL_PROC_ENCRYPT        (2)
35 #define BLE_LL_CTRL_PROC_FEATURE_XCHG   (3)
36 #define BLE_LL_CTRL_PROC_VERSION_XCHG   (4)
37 #define BLE_LL_CTRL_PROC_TERMINATE      (5)
38 #define BLE_LL_CTRL_PROC_CONN_PARAM_REQ (6)
39 #define BLE_LL_CTRL_PROC_LE_PING        (7)
40 #define BLE_LL_CTRL_PROC_DATA_LEN_UPD   (8)
41 #define BLE_LL_CTRL_PROC_PHY_UPDATE     (9)
42 #define BLE_LL_CTRL_PROC_NUM            (10)
43 #define BLE_LL_CTRL_PROC_IDLE           (255)
44 
45 /* Checks if a particular control procedure is running */
46 #define IS_PENDING_CTRL_PROC(sm, proc)  (sm->pending_ctrl_procs & (1 << proc))
47 #define CLR_PENDING_CTRL_PROC(sm, proc) (sm->pending_ctrl_procs &= ~(1 << proc))
48 
49 /* LL control procedure timeout */
50 #define BLE_LL_CTRL_PROC_TIMEOUT_MS     (40000) /* ms */
51 
52 /*
53  * LL CTRL PDU format
54  *  -> Opcode   (1 byte)
55  *  -> Data     (0 - 26 bytes)
56  */
57 #define BLE_LL_CTRL_CONN_UPDATE_IND     (0)
58 #define BLE_LL_CTRL_CHANNEL_MAP_REQ     (1)
59 #define BLE_LL_CTRL_TERMINATE_IND       (2)
60 #define BLE_LL_CTRL_ENC_REQ             (3)
61 #define BLE_LL_CTRL_ENC_RSP             (4)
62 #define BLE_LL_CTRL_START_ENC_REQ       (5)
63 #define BLE_LL_CTRL_START_ENC_RSP       (6)
64 #define BLE_LL_CTRL_UNKNOWN_RSP         (7)
65 #define BLE_LL_CTRL_FEATURE_REQ         (8)
66 #define BLE_LL_CTRL_FEATURE_RSP         (9)
67 #define BLE_LL_CTRL_PAUSE_ENC_REQ       (10)
68 #define BLE_LL_CTRL_PAUSE_ENC_RSP       (11)
69 #define BLE_LL_CTRL_VERSION_IND         (12)
70 #define BLE_LL_CTRL_REJECT_IND          (13)
71 #define BLE_LL_CTRL_SLAVE_FEATURE_REQ   (14)
72 #define BLE_LL_CTRL_CONN_PARM_REQ       (15)
73 #define BLE_LL_CTRL_CONN_PARM_RSP       (16)
74 #define BLE_LL_CTRL_REJECT_IND_EXT      (17)
75 #define BLE_LL_CTRL_PING_REQ            (18)
76 #define BLE_LL_CTRL_PING_RSP            (19)
77 #define BLE_LL_CTRL_LENGTH_REQ          (20)
78 #define BLE_LL_CTRL_LENGTH_RSP          (21)
79 #define BLE_LL_CTRL_PHY_REQ             (22)
80 #define BLE_LL_CTRL_PHY_RSP             (23)
81 #define BLE_LL_CTRL_PHY_UPDATE_IND      (24)
82 #define BLE_LL_CTRL_MIN_USED_CHAN_IND   (25)
83 
84 /* Maximum opcode value */
85 #define BLE_LL_CTRL_OPCODES             (BLE_LL_CTRL_MIN_USED_CHAN_IND + 1)
86 
87 extern const uint8_t g_ble_ll_ctrl_pkt_lengths[BLE_LL_CTRL_OPCODES];
88 
89 /* Maximum # of payload bytes in a LL control PDU */
90 #define BLE_LL_CTRL_MAX_PAYLOAD         (26)
91 #define BLE_LL_CTRL_MAX_PDU_LEN         (27)
92 
93 /* LL control connection update request */
94 struct ble_ll_conn_upd_req
95 {
96     uint8_t winsize;
97     uint16_t winoffset;
98     uint16_t interval;
99     uint16_t latency;
100     uint16_t timeout;
101     uint16_t instant;
102 };
103 #define BLE_LL_CTRL_CONN_UPD_REQ_LEN        (11)
104 
105 /* LL control channel map request */
106 struct ble_ll_chan_map_req
107 {
108     uint8_t chmap[5];
109     uint16_t instant;
110 };
111 #define BLE_LL_CTRL_CHAN_MAP_LEN            (7)
112 
113 /*
114  * LL control terminate ind
115  *  -> error code (1 byte)
116  */
117 #define BLE_LL_CTRL_TERMINATE_IND_LEN      (1)
118 
119 /* LL control enc req */
120 struct ble_ll_enc_req
121 {
122     uint8_t rand[8];
123     uint16_t ediv;
124     uint8_t skdm[8];
125     uint32_t ivm;
126 };
127 
128 #define BLE_LL_CTRL_ENC_REQ_LEN             (22)
129 
130 /* LL control enc rsp */
131 struct ble_ll_enc_rsp
132 {
133     uint8_t skds[8];
134     uint32_t ivs;
135 };
136 
137 #define BLE_LL_CTRL_ENC_RSP_LEN             (12)
138 
139 /* LL control start/pause enc request and response */
140 #define BLE_LL_CTRL_START_ENC_REQ_LEN       (0)
141 #define BLE_LL_CTRL_START_ENC_RSP_LEN       (0)
142 #define BLE_LL_CTRL_PAUSE_ENC_REQ_LEN       (0)
143 #define BLE_LL_CTRL_PAUSE_ENC_RSP_LEN       (0)
144 
145 /*
146  * LL control unknown response
147  *  -> 1 byte which contains the unknown or un-supported opcode.
148  */
149 #define BLE_LL_CTRL_UNK_RSP_LEN             (1)
150 
151 /*
152  * LL control feature req and LL control feature rsp
153  *  -> 8 bytes of data containing features supported by device.
154  */
155 #define BLE_LL_CTRL_FEATURE_LEN             (8)
156 
157 /*
158  * LL control version ind
159  *  -> version (1 byte):
160  *      Contains the version number of the bluetooth controller specification.
161  *  -> comp_id (2 bytes)
162  *      Contains the company identifier of the manufacturer of the controller.
163  *  -> sub_ver_num: Contains a unique value for implementation or revision of
164  *      the bluetooth controller.
165  */
166 struct ble_ll_version_ind
167 {
168     uint8_t ble_ctrlr_ver;
169     uint16_t company_id;
170     uint16_t sub_ver_num;
171 };
172 
173 #define BLE_LL_CTRL_VERSION_IND_LEN         (5)
174 
175 /*
176  * LL control reject ind
177  *  -> error code (1 byte): contains reason why request was rejected.
178  */
179 #define BLE_LL_CTRL_REJ_IND_LEN             (1)
180 
181 /*
182  * LL control slave feature req
183  *  -> 8 bytes of data containing features supported by device.
184  */
185 #define BLE_LL_CTRL_SLAVE_FEATURE_REQ_LEN   (8)
186 
187 /* LL control connection param req and connection param rsp */
188 struct ble_ll_conn_params
189 {
190     uint16_t interval_min;
191     uint16_t interval_max;
192     uint16_t latency;
193     uint16_t timeout;
194     uint8_t pref_periodicity;
195     uint16_t ref_conn_event_cnt;
196     uint16_t offset0;
197     uint16_t offset1;
198     uint16_t offset2;
199     uint16_t offset3;
200     uint16_t offset4;
201     uint16_t offset5;
202 };
203 
204 #define BLE_LL_CTRL_CONN_PARAMS_LEN     (23)
205 
206 /* LL control reject ind ext */
207 struct ble_ll_reject_ind_ext
208 {
209     uint8_t reject_opcode;
210     uint8_t err_code;
211 };
212 
213 #define BLE_LL_CTRL_REJECT_IND_EXT_LEN  (2)
214 
215 /* LL control ping req and ping rsp (contain no data) */
216 #define BLE_LL_CTRL_PING_LEN            (0)
217 
218 /*
219  * LL control length req and length rsp
220  *  -> max_rx_bytes (2 bytes): defines connMaxRxOctets. Range 27 to 251
221  *  -> max_rx_time (2 bytes): defines connMaxRxTime. Range 328 to 2120 usecs.
222  *  -> max_tx_bytes (2 bytes): defines connMaxTxOctets. Range 27 to 251
223  *  -> max_tx_time (2 bytes): defines connMaxTxTime. Range 328 to 2120 usecs.
224  */
225 struct ble_ll_len_req
226 {
227     uint16_t max_rx_bytes;
228     uint16_t max_rx_time;
229     uint16_t max_tx_bytes;
230     uint16_t max_tx_time;
231 };
232 
233 #define BLE_LL_CTRL_LENGTH_REQ_LEN      (8)
234 
235 /* PHY request/response */
236 #define BLE_LL_CTRL_PHY_REQ_LEN         (2)
237 #define BLE_LL_CTRL_PHY_RSP_LEN         (2)
238 #define BLE_LL_CTRL_PHY_UPD_IND_LEN     (4)
239 
240 /* Min used channels */
241 #define BLE_LL_CTRL_MIN_USED_CHAN_LEN   (2)
242 
243 /* API */
244 struct ble_ll_conn_sm;
245 void ble_ll_ctrl_proc_start(struct ble_ll_conn_sm *connsm, int ctrl_proc);
246 void ble_ll_ctrl_proc_stop(struct ble_ll_conn_sm *connsm, int ctrl_proc);
247 int ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om);
248 void ble_ll_ctrl_chk_proc_start(struct ble_ll_conn_sm *connsm);
249 void ble_ll_ctrl_terminate_start(struct ble_ll_conn_sm *connsm);
250 int ble_ll_ctrl_is_terminate_ind(uint8_t hdr, uint8_t opcode);
251 uint8_t ble_ll_ctrl_conn_param_reply(struct ble_ll_conn_sm *connsm,
252                                      uint8_t *rsp,
253                                      struct ble_ll_conn_params *req);
254 int ble_ll_ctrl_reject_ind_send(struct ble_ll_conn_sm *connsm,
255                                 uint8_t rej_opcode, uint8_t err);
256 int ble_ll_ctrl_start_enc_send(struct ble_ll_conn_sm *connsm);
257 int ble_ll_ctrl_enc_allowed_pdu_rx(struct os_mbuf *rxpdu);
258 int ble_ll_ctrl_enc_allowed_pdu_tx(struct os_mbuf_pkthdr *pkthdr);
259 int ble_ll_ctrl_tx_done(struct os_mbuf *txpdu, struct ble_ll_conn_sm *connsm);
260 int ble_ll_ctrl_is_start_enc_rsp(struct os_mbuf *txpdu);
261 
262 void ble_ll_hci_ev_datalen_chg(struct ble_ll_conn_sm *connsm);
263 void ble_ll_hci_ev_rem_conn_parm_req(struct ble_ll_conn_sm *connsm,
264                                      struct ble_ll_conn_params *cp);
265 void ble_ll_hci_ev_conn_update(struct ble_ll_conn_sm *connsm, uint8_t status);
266 void ble_ll_hci_ev_rd_rem_used_feat(struct ble_ll_conn_sm *connsm,
267                                       uint8_t status);
268 void ble_ll_hci_ev_rd_rem_ver(struct ble_ll_conn_sm *connsm, uint8_t status);
269 void ble_ll_hci_ev_encrypt_chg(struct ble_ll_conn_sm *connsm, uint8_t status);
270 int ble_ll_hci_ev_ltk_req(struct ble_ll_conn_sm *connsm);
271 int ble_ll_hci_ev_hw_err(uint8_t hw_err);
272 void ble_ll_hci_ev_databuf_overflow(void);
273 void ble_ll_hci_ev_le_csa(struct ble_ll_conn_sm *connsm);
274 void ble_ll_hci_ev_send_scan_req_recv(uint8_t adv_handle, const uint8_t *peer,
275                                       uint8_t peer_addr_type);
276 void ble_ll_hci_ev_send_scan_timeout(void);
277 void ble_ll_hci_ev_send_adv_set_terminated(uint8_t status, uint8_t adv_handle,
278                                            uint16_t conn_handle, uint8_t events);
279 int ble_ll_hci_ev_phy_update(struct ble_ll_conn_sm *connsm, uint8_t status);
280 void ble_ll_calc_session_key(struct ble_ll_conn_sm *connsm);
281 void ble_ll_ctrl_phy_update_proc_complete(struct ble_ll_conn_sm *connsm);
282 void ble_ll_ctrl_initiate_dle(struct ble_ll_conn_sm *connsm);
283 void ble_ll_hci_ev_send_vendor_err(char *file, uint32_t line);
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 #endif /* H_BLE_LL_CTRL_ */
290