NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_ll_sched.h
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_SCHED_
21#define H_BLE_LL_SCHED_
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* Time per BLE scheduler slot */
28#define BLE_LL_SCHED_USECS_PER_SLOT (1250)
29
30/*
31 * Worst case time needed for scheduled advertising item. This is the longest
32 * possible time to receive a scan request and send a scan response (with the
33 * appropriate IFS time between them). This number is calculated using the
34 * following formula: IFS + SCAN_REQ + IFS + SCAN_RSP = 150 + 176 + 150 + 376.
35 * Note: worst case time to tx adv, rx scan req and send scan rsp is 1228 usecs.
36 * This assumes maximum sized advertising PDU and scan response PDU.
37 *
38 * For connectable advertising events no scan request is allowed. In this case
39 * we just need to receive a connect request PDU: IFS + CONNECT_REQ = 150 + 352.
40 * Note: worst-case is 376 + 150 + 352 = 878 usecs
41 *
42 * NOTE: The advertising PDU transmit time is NOT included here since we know
43 * how long that will take (worst-case is 376 usecs).
44 */
45#define BLE_LL_SCHED_ADV_MAX_USECS (852)
46#define BLE_LL_SCHED_DIRECT_ADV_MAX_USECS (502)
47#define BLE_LL_SCHED_MAX_ADV_PDU_USECS (376)
48
49/*
50 * This is the offset from the start of the scheduled item until the actual
51 * tx/rx should occur, in ticks.
52 */
53extern uint8_t g_ble_ll_sched_offset_ticks;
54
55/*
56 * This is the number of slots needed to transmit and receive a maximum
57 * size PDU, including an IFS time before each. The actual time is
58 * 2120 usecs for tx/rx and 150 for IFS = 4540 usecs.
59 */
60#define BLE_LL_SCHED_MAX_TXRX_SLOT (4 * BLE_LL_SCHED_USECS_PER_SLOT)
61
62/* BLE scheduler errors */
63#define BLE_LL_SCHED_ERR_OVERLAP (1)
64
65/* Types of scheduler events */
66#define BLE_LL_SCHED_TYPE_ADV (1)
67#define BLE_LL_SCHED_TYPE_SCAN (2)
68#define BLE_LL_SCHED_TYPE_CONN (3)
69#define BLE_LL_SCHED_TYPE_DTM (5)
70#define BLE_LL_SCHED_TYPE_PERIODIC (6)
71#define BLE_LL_SCHED_TYPE_SYNC (7)
72#define BLE_LL_SCHED_TYPE_SCAN_AUX (8)
73
74/* Return values for schedule callback. */
75#define BLE_LL_SCHED_STATE_RUNNING (0)
76#define BLE_LL_SCHED_STATE_DONE (1)
77
78/* Callback function */
79struct ble_ll_sched_item;
80typedef int (*sched_cb_func)(struct ble_ll_sched_item *sch);
81typedef void (*sched_remove_cb_func)(struct ble_ll_sched_item *sch);
82
83/*
84 * Schedule item
85 * sched_type: This is the type of the schedule item.
86 * enqueued: Flag denoting if item is on the scheduler list. 0: no, 1:yes
87 * remainder: # of usecs from offset till tx/rx should occur
88 * txrx_offset: Number of ticks from start time until tx/rx should occur.
89 *
90 */
91struct ble_ll_sched_item
92{
93 uint8_t sched_type;
94 uint8_t enqueued;
95 uint8_t remainder;
96 uint32_t start_time;
97 uint32_t end_time;
98 void *cb_arg;
99 sched_cb_func sched_cb;
100 TAILQ_ENTRY(ble_ll_sched_item) link;
101};
102
103/* Initialize the scheduler */
104int ble_ll_sched_init(void);
105
106/* Remove item(s) from schedule */
107int ble_ll_sched_rmv_elem(struct ble_ll_sched_item *sch);
108
109void ble_ll_sched_rmv_elem_type(uint8_t type, sched_remove_cb_func remove_cb);
110
111/* Schedule a new master connection */
112struct ble_ll_conn_sm;
113int ble_ll_sched_conn_central_new(struct ble_ll_conn_sm *connsm,
114 struct ble_mbuf_hdr *ble_hdr, uint8_t pyld_len);
115
116/* Schedule a new slave connection */
117int ble_ll_sched_conn_periph_new(struct ble_ll_conn_sm *connsm);
118
119struct ble_ll_adv_sm;
120typedef void ble_ll_sched_adv_new_cb(struct ble_ll_adv_sm *advsm,
121 uint32_t sch_start, void *arg);
122
123/* Schedule a new advertising event */
124int ble_ll_sched_adv_new(struct ble_ll_sched_item *sch,
125 ble_ll_sched_adv_new_cb cb, void *arg);
126
127/* Schedule periodic advertising event */
128int ble_ll_sched_periodic_adv(struct ble_ll_sched_item *sch, bool first_event);
129
130int ble_ll_sched_sync_reschedule(struct ble_ll_sched_item *sch,
131 uint32_t anchor_point,
132 uint8_t anchor_point_usecs,
133 uint32_t window_widening, int8_t phy_mode);
134int ble_ll_sched_sync(struct ble_ll_sched_item *sch,
135 uint32_t beg_cputime, uint32_t rem_usecs, uint32_t offset,
136 int8_t phy_mode);
137
138/* Reschedule an advertising event */
139int ble_ll_sched_adv_reschedule(struct ble_ll_sched_item *sch,
140 uint32_t max_delay_ticks);
141
142/* Reschedule and advertising pdu */
143int ble_ll_sched_adv_resched_pdu(struct ble_ll_sched_item *sch);
144
145/* Reschedule a connection that had previously been scheduled or that is over */
146int ble_ll_sched_conn_reschedule(struct ble_ll_conn_sm * connsm);
147
158int ble_ll_sched_next_time(uint32_t *next_event_time);
159
160#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
161struct ble_ll_scan_sm;
162struct ble_ll_aux_data;
163int ble_ll_sched_aux_scan(struct ble_mbuf_hdr *ble_hdr,
164 struct ble_ll_scan_sm *scansm,
165 struct ble_ll_aux_data *aux_scan);
166
167int ble_ll_sched_scan_aux(struct ble_ll_sched_item *sch, uint32_t pdu_time,
168 uint8_t pdu_time_rem, uint32_t offset_us);
169#endif
170
171/* Stop the scheduler */
172void ble_ll_sched_stop(void);
173
174#if MYNEWT_VAL(BLE_LL_DTM)
175int ble_ll_sched_dtm(struct ble_ll_sched_item *sch);
176#endif
177
178#if MYNEWT_VAL(BLE_LL_CONN_STRICT_SCHED)
179#if !MYNEWT_VAL(BLE_LL_CONN_STRICT_SCHED_FIXED)
180void ble_ll_sched_css_set_params(uint32_t slot_us, uint32_t period_slots);
181#endif
182void ble_ll_sched_css_set_conn_anchor(struct ble_ll_conn_sm *connsm);
183#if MYNEWT_VAL(BLE_LL_CONN_STRICT_SCHED_FIXED)
184static inline uint32_t
185ble_ll_sched_css_get_slot_us(void)
186{
187 return MYNEWT_VAL(BLE_LL_CONN_STRICT_SCHED_SLOT_US);
188}
189
190static inline uint32_t
191ble_ll_sched_css_get_period_slots(void)
192{
193 return MYNEWT_VAL(BLE_LL_CONN_STRICT_SCHED_PERIOD_SLOTS);
194}
195
196static inline uint32_t
197ble_ll_sched_css_get_conn_interval_us(void)
198{
199 return ble_ll_sched_css_get_period_slots() *
200 ble_ll_sched_css_get_slot_us() / 1250;
201}
202#else
203uint32_t ble_ll_sched_css_get_slot_us(void);
204uint32_t ble_ll_sched_css_get_period_slots(void);
205uint32_t ble_ll_sched_css_get_conn_interval_us(void);
206#endif
207#endif
208
209#ifdef __cplusplus
210}
211#endif
212
213#endif /* H_LL_SCHED_ */