xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/controller/src/ble_ll_conn_priv.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_CONN_PRIV_
21 #define H_BLE_LL_CONN_PRIV_
22 
23 #include "controller/ble_ll_conn.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /*
30  * Definitions for min/max RX/TX time/bytes values allowed for connections.
31  * Source: Core 5.0 specification, Vol 6, Part B, section 4.5.10
32  */
33 #define BLE_LL_CONN_SUPP_TIME_MIN           (328)   /* usecs */
34 #define BLE_LL_CONN_SUPP_TIME_MAX           (17040) /* usecs */
35 #define BLE_LL_CONN_SUPP_TIME_MIN_UNCODED   (328)   /* usecs */
36 #define BLE_LL_CONN_SUPP_TIME_MAX_UNCODED   (2120)  /* usecs */
37 #define BLE_LL_CONN_SUPP_TIME_MIN_CODED     (2704)  /* usecs */
38 #define BLE_LL_CONN_SUPP_TIME_MAX_CODED     (17040) /* usecs */
39 #define BLE_LL_CONN_SUPP_BYTES_MIN          (27)    /* bytes */
40 #define BLE_LL_CONN_SUPP_BYTES_MAX          (251)   /* bytes */
41 
42 /* Connection event timing */
43 #define BLE_LL_CONN_INITIAL_OFFSET          (1250)
44 #define BLE_LL_CONN_ITVL_USECS              (1250)
45 #define BLE_LL_CONN_TX_WIN_USECS            (1250)
46 #define BLE_LL_CONN_TX_OFF_USECS            (1250)
47 #define BLE_LL_CONN_CE_USECS                (625)
48 #define BLE_LL_CONN_TX_WIN_MIN              (1)         /* in tx win units */
49 #define BLE_LL_CONN_SLAVE_LATENCY_MAX       (499)
50 
51 /* Connection handle range */
52 #define BLE_LL_CONN_MAX_CONN_HANDLE         (0x0EFF)
53 
54 /* Offset (in bytes) of advertising address in connect request */
55 #define BLE_LL_CONN_REQ_ADVA_OFF    (BLE_LL_PDU_HDR_LEN + BLE_DEV_ADDR_LEN)
56 
57 /* Default authenticated payload timeout (30 seconds; in 10 msecs increments) */
58 #define BLE_LL_CONN_DEF_AUTH_PYLD_TMO       (3000)
59 #define BLE_LL_CONN_AUTH_PYLD_OS_TMO(x)     ble_npl_time_ms_to_ticks32((x) * 10)
60 
61 
62 typedef void (*ble_ll_hci_post_cmd_complete_cb)(void);
63 
64 /* Global Link Layer connection parameters */
65 struct ble_ll_conn_global_params
66 {
67     uint8_t master_chan_map[BLE_LL_CONN_CHMAP_LEN];
68     uint8_t num_used_chans;
69     uint8_t supp_max_tx_octets;
70     uint8_t supp_max_rx_octets;
71     uint8_t conn_init_max_tx_octets;
72     uint8_t sugg_tx_octets;
73     uint16_t sugg_tx_time;
74     uint16_t conn_init_max_tx_time;
75     uint16_t conn_init_max_tx_time_uncoded;
76     uint16_t conn_init_max_tx_time_coded;
77     uint16_t supp_max_tx_time;
78     uint16_t supp_max_rx_time;
79 };
80 extern struct ble_ll_conn_global_params g_ble_ll_conn_params;
81 
82 /* Some data structures used by other LL routines */
83 SLIST_HEAD(ble_ll_conn_active_list, ble_ll_conn_sm);
84 STAILQ_HEAD(ble_ll_conn_free_list, ble_ll_conn_sm);
85 extern struct ble_ll_conn_active_list g_ble_ll_conn_active_list;
86 extern struct ble_ll_conn_free_list g_ble_ll_conn_free_list;
87 
88 /* Pointer to connection state machine we are trying to create */
89 extern struct ble_ll_conn_sm *g_ble_ll_conn_create_sm;
90 
91 /* Generic interface */
92 struct ble_ll_len_req;
93 struct hci_create_conn;
94 struct ble_mbuf_hdr;
95 struct ble_ll_adv_sm;
96 
97 void ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm);
98 void ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err);
99 void ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
100                              uint8_t hdr_byte, uint8_t length);
101 struct ble_ll_conn_sm *ble_ll_conn_sm_get(void);
102 void ble_ll_conn_master_init(struct ble_ll_conn_sm *connsm,
103                              struct hci_create_conn *hcc);
104 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
105 void ble_ll_conn_ext_master_init(struct ble_ll_conn_sm *connsm,
106                                  struct hci_ext_create_conn *hcc);
107 
108 void ble_ll_conn_ext_set_params(struct ble_ll_conn_sm *connsm,
109                                 struct hci_ext_conn_params *hcc_params,
110                                 int phy);
111 #endif
112 
113 struct ble_ll_conn_sm *ble_ll_conn_find_active_conn(uint16_t handle);
114 void ble_ll_conn_datalen_update(struct ble_ll_conn_sm *connsm,
115                                 struct ble_ll_len_req *req);
116 
117 /* Advertising interface */
118 int ble_ll_conn_slave_start(uint8_t *rxbuf, uint8_t pat,
119                             struct ble_mbuf_hdr *rxhdr, bool force_csa2);
120 
121 /* Link Layer interface */
122 void ble_ll_conn_module_init(void);
123 void ble_ll_conn_set_global_chanmap(uint8_t num_used_chans, uint8_t *chanmap);
124 void ble_ll_conn_module_reset(void);
125 void ble_ll_conn_tx_pkt_in(struct os_mbuf *om, uint16_t handle, uint16_t len);
126 int ble_ll_conn_rx_isr_start(struct ble_mbuf_hdr *rxhdr, uint32_t aa);
127 int ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr);
128 void ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr);
129 void ble_ll_init_rx_pkt_in(uint8_t pdu_type, uint8_t *rxbuf,
130                            struct ble_mbuf_hdr *ble_hdr);
131 int ble_ll_init_rx_isr_start(uint8_t pdu_type, struct ble_mbuf_hdr *ble_hdr);
132 int ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
133                            struct ble_mbuf_hdr *ble_hdr);
134 void ble_ll_conn_wfr_timer_exp(void);
135 void ble_ll_conn_init_wfr_timer_exp(void);
136 int ble_ll_conn_is_lru(struct ble_ll_conn_sm *s1, struct ble_ll_conn_sm *s2);
137 uint32_t ble_ll_conn_get_ce_end_time(void);
138 void ble_ll_conn_event_halt(void);
139 uint8_t ble_ll_conn_calc_used_chans(uint8_t *chmap);
140 void ble_ll_conn_reset_pending_aux_conn_rsp(void);
141 bool ble_ll_conn_init_pending_aux_conn_rsp(void);
142 /* HCI */
143 void ble_ll_disconn_comp_event_send(struct ble_ll_conn_sm *connsm,
144                                     uint8_t reason);
145 void ble_ll_auth_pyld_tmo_event_send(struct ble_ll_conn_sm *connsm);
146 int ble_ll_conn_hci_disconnect_cmd(uint8_t *cmdbuf);
147 int ble_ll_conn_hci_rd_rem_ver_cmd(uint8_t *cmdbuf);
148 int ble_ll_conn_create(uint8_t *cmdbuf);
149 int ble_ll_conn_hci_update(uint8_t *cmdbuf);
150 int ble_ll_conn_hci_set_chan_class(uint8_t *cmdbuf);
151 int ble_ll_conn_hci_param_reply(uint8_t *cmdbuf, int negative_reply,
152                                 uint8_t *rspbuf, uint8_t *rsplen);
153 int ble_ll_conn_create_cancel(ble_ll_hci_post_cmd_complete_cb *post_cmd_cb);
154 void ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm);
155 void ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, uint8_t status,
156                                  uint8_t *evbuf, struct ble_ll_adv_sm *advsm);
157 void ble_ll_conn_timeout(struct ble_ll_conn_sm *connsm, uint8_t ble_err);
158 int ble_ll_conn_hci_chk_conn_params(uint16_t itvl_min, uint16_t itvl_max,
159                                     uint16_t latency, uint16_t spvn_tmo);
160 int ble_ll_conn_hci_read_rem_features(uint8_t *cmdbuf);
161 int ble_ll_conn_hci_read_rem_features_complete(void);
162 int ble_ll_conn_hci_rd_rssi(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen);
163 int ble_ll_conn_hci_rd_chan_map(uint8_t *cmdbuf, uint8_t *rspbuf,
164                                 uint8_t *rsplen);
165 int ble_ll_conn_hci_set_data_len(uint8_t *cmdbuf, uint8_t *rspbuf,
166                                  uint8_t *rsplen);
167 int ble_ll_conn_hci_le_start_encrypt(uint8_t *cmdbuf);
168 int ble_ll_conn_hci_le_ltk_reply(uint8_t *cmdbuf, uint8_t *rspbuf,
169                                  uint8_t *rsplen);
170 int ble_ll_conn_hci_le_ltk_neg_reply(uint8_t *cmdbuf, uint8_t *rspbuf,
171                                      uint8_t *rsplen);
172 int ble_ll_conn_hci_wr_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp,
173                                      uint8_t *rsplen);
174 int ble_ll_conn_hci_rd_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp,
175                                      uint8_t *rsplen);
176 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
177 void ble_ll_conn_auth_pyld_timer_start(struct ble_ll_conn_sm *connsm);
178 #else
179 #define ble_ll_conn_auth_pyld_timer_start(x)
180 #endif
181 
182 int ble_ll_hci_cmd_rx(uint8_t *cmd, void *arg);
183 int ble_ll_hci_acl_rx(struct os_mbuf *om, void *arg);
184 
185 int ble_ll_conn_hci_le_rd_phy(uint8_t *cmdbuf, uint8_t *rsp, uint8_t *rsplen);
186 int ble_ll_conn_hci_le_set_phy(uint8_t *cmdbuf);
187 int ble_ll_conn_chk_phy_upd_start(struct ble_ll_conn_sm *connsm);
188 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
189 int ble_ll_ext_conn_create(uint8_t *cmdbuf, uint8_t cmdlen);
190 #endif
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif /* H_BLE_LL_CONN_PRIV_ */
196