1 
2 #include "wifi_hal.h"
3 #include "gscan.h"
4 
5 #ifndef __WIFI_HAL_RTT_H__
6 #define __WIFI_HAL_RTT_H__
7 
8 /* Ranging status */
9 typedef enum {
10     RTT_STATUS_SUCCESS       = 0,
11     RTT_STATUS_FAILURE       = 1,           // general failure status
12     RTT_STATUS_FAIL_NO_RSP   = 2,           // target STA does not respond to request
13     RTT_STATUS_FAIL_REJECTED = 3,           // request rejected. Applies to 2-sided RTT only
14     RTT_STATUS_FAIL_NOT_SCHEDULED_YET  = 4,
15     RTT_STATUS_FAIL_TM_TIMEOUT         = 5, // timing measurement times out
16     RTT_STATUS_FAIL_AP_ON_DIFF_CHANNEL = 6, // Target on different channel, cannot range
17     RTT_STATUS_FAIL_NO_CAPABILITY  = 7,     // ranging not supported
18     RTT_STATUS_ABORTED             = 8,     // request aborted for unknown reason
19     RTT_STATUS_FAIL_INVALID_TS     = 9,     // Invalid T1-T4 timestamp
20     RTT_STATUS_FAIL_PROTOCOL       = 10,    // 11mc protocol failed
21     RTT_STATUS_FAIL_SCHEDULE       = 11,    // request could not be scheduled
22     RTT_STATUS_FAIL_BUSY_TRY_LATER = 12,    // responder cannot collaborate at time of request
23     RTT_STATUS_INVALID_REQ         = 13,    // bad request args
24     RTT_STATUS_NO_WIFI             = 14,    // WiFi not enabled
25     RTT_STATUS_FAIL_FTM_PARAM_OVERRIDE = 15, // Responder overrides param info, cannot range with new params
26     RTT_STATUS_NAN_RANGING_PROTOCOL_FAILURE =16, //Negotiation failure
27     RTT_STATUS_NAN_RANGING_CONCURRENCY_NOT_SUPPORTED=17, //concurrency not supported (NDP+RTT)
28 } wifi_rtt_status;
29 
30 /* RTT peer type */
31 typedef enum {
32     RTT_PEER_AP         = 0x1,
33     RTT_PEER_STA        = 0x2,
34     RTT_PEER_P2P_GO     = 0x3,
35     RTT_PEER_P2P_CLIENT = 0x4,
36     RTT_PEER_NAN        = 0x5
37 } rtt_peer_type;
38 
39 /* RTT Measurement Bandwidth */
40 typedef enum {
41     WIFI_RTT_BW_5   = 0x01,
42     WIFI_RTT_BW_10  = 0x02,
43     WIFI_RTT_BW_20  = 0x04,
44     WIFI_RTT_BW_40  = 0x08,
45     WIFI_RTT_BW_80  = 0x10,
46     WIFI_RTT_BW_160 = 0x20
47 } wifi_rtt_bw;
48 
49 /* RTT Measurement Preamble */
50 typedef enum {
51     WIFI_RTT_PREAMBLE_LEGACY = 0x1,
52     WIFI_RTT_PREAMBLE_HT     = 0x2,
53     WIFI_RTT_PREAMBLE_VHT    = 0x4
54 } wifi_rtt_preamble;
55 
56 /* RTT Type */
57 typedef enum {
58     RTT_TYPE_1_SIDED = 0x1,
59     RTT_TYPE_2_SIDED = 0x2,
60 } wifi_rtt_type;
61 
62 /* RTT configuration */
63 typedef struct {
64     mac_addr addr;                 // peer device mac address
65     wifi_rtt_type type;            // 1-sided or 2-sided RTT
66     rtt_peer_type peer;            // optional - peer device hint (STA, P2P, AP)
67     wifi_channel_info channel;     // Required for STA-AP mode, optional for P2P, NBD etc.
68     unsigned burst_period;         // Time interval between bursts (units: 100 ms).
69                                    // Applies to 1-sided and 2-sided RTT multi-burst requests.
70                                    // Range: 0-31, 0: no preference by initiator (2-sided RTT)
71     unsigned num_burst;            // Total number of RTT bursts to be executed. It will be
72                                    // specified in the same way as the parameter "Number of
73                                    // Burst Exponent" found in the FTM frame format. It
74                                    // applies to both: 1-sided RTT and 2-sided RTT. Valid
75                                    // values are 0 to 15 as defined in 802.11mc std.
76                                    // 0 means single shot
77                                    // The implication of this parameter on the maximum
78                                    // number of RTT results is the following:
79                                    // for 1-sided RTT: max num of RTT results = (2^num_burst)*(num_frames_per_burst)
80                                    // for 2-sided RTT: max num of RTT results = (2^num_burst)*(num_frames_per_burst - 1)
81     unsigned num_frames_per_burst; // num of frames per burst.
82                                    // Minimum value = 1, Maximum value = 31
83                                    // For 2-sided this equals the number of FTM frames
84                                    // to be attempted in a single burst. This also
85                                    // equals the number of FTM frames that the
86                                    // initiator will request that the responder send
87                                    // in a single frame.
88     unsigned num_retries_per_rtt_frame; // number of retries for a failed RTT frame. Applies
89                                         // to 1-sided RTT only. Minimum value = 0, Maximum value = 3
90 
91     //following fields are only valid for 2-side RTT
92     unsigned num_retries_per_ftmr; // Maximum number of retries that the initiator can
93                                    // retry an FTMR frame.
94                                    // Minimum value = 0, Maximum value = 3
95     byte LCI_request;              // 1: request LCI, 0: do not request LCI
96     byte LCR_request;              // 1: request LCR, 0: do not request LCR
97     unsigned burst_duration;       // Applies to 1-sided and 2-sided RTT. Valid values will
98                                    // be 2-11 and 15 as specified by the 802.11mc std for
99                                    // the FTM parameter burst duration. In a multi-burst
100                                    // request, if responder overrides with larger value,
101                                    // the initiator will return failure. In a single-burst
102                                    // request if responder overrides with larger value,
103                                    // the initiator will sent TMR_STOP to terminate RTT
104                                    // at the end of the burst_duration it requested.
105     wifi_rtt_preamble preamble;    // RTT preamble to be used in the RTT frames
106     wifi_rtt_bw bw;                // RTT BW to be used in the RTT frames
107 } wifi_rtt_config;
108 
109 /* RTT results */
110 typedef struct {
111     mac_addr addr;                // device mac address
112     unsigned burst_num;           // burst number in a multi-burst request
113     unsigned measurement_number;  // Total RTT measurement frames attempted
114     unsigned success_number;      // Total successful RTT measurement frames
115     byte  number_per_burst_peer;  // Maximum number of "FTM frames per burst" supported by
116                                   // the responder STA. Applies to 2-sided RTT only.
117                                   // If reponder overrides with larger value:
118                                   // - for single-burst request initiator will truncate the
119                                   // larger value and send a TMR_STOP after receiving as
120                                   // many frames as originally requested.
121                                   // - for multi-burst request, initiator will return
122                                   // failure right away.
123     wifi_rtt_status status;       // ranging status
124     byte retry_after_duration;    // When status == RTT_STATUS_FAIL_BUSY_TRY_LATER,
125                                   // this will be the time provided by the responder as to
126                                   // when the request can be tried again. Applies to 2-sided
127                                   // RTT only. In sec, 1-31sec.
128     wifi_rtt_type type;           // RTT type
129     wifi_rssi rssi;               // average rssi in 0.5 dB steps e.g. 143 implies -71.5 dB
130     wifi_rssi rssi_spread;        // rssi spread in 0.5 dB steps e.g. 5 implies 2.5 dB spread (optional)
131     wifi_rate tx_rate;            // 1-sided RTT: TX rate of RTT frame.
132                                   // 2-sided RTT: TX rate of initiator's Ack in response to FTM frame.
133     wifi_rate rx_rate;            // 1-sided RTT: TX rate of Ack from other side.
134                                   // 2-sided RTT: TX rate of FTM frame coming from responder.
135     wifi_timespan rtt;            // round trip time in picoseconds
136     wifi_timespan rtt_sd;         // rtt standard deviation in picoseconds
137     wifi_timespan rtt_spread;     // difference between max and min rtt times recorded in picoseconds
138     int distance_mm;              // distance in mm (optional)
139     int distance_sd_mm;           // standard deviation in mm (optional)
140     int distance_spread_mm;       // difference between max and min distance recorded in mm (optional)
141     wifi_timestamp ts;            // time of the measurement (in microseconds since boot)
142     int burst_duration;           // in ms, actual time taken by the FW to finish one burst
143                                   // measurement. Applies to 1-sided and 2-sided RTT.
144     int negotiated_burst_num;     // Number of bursts allowed by the responder. Applies
145                                   // to 2-sided RTT only.
146     wifi_information_element *LCI; // for 11mc only
147     wifi_information_element *LCR; // for 11mc only
148 } wifi_rtt_result;
149 
150 /* RTT result callback */
151 typedef struct {
152     void (*on_rtt_results) (wifi_request_id id, unsigned num_results, wifi_rtt_result *rtt_result[]);
153 } wifi_rtt_event_handler;
154 
155 /* API to request RTT measurement */
156 wifi_error wifi_rtt_range_request(wifi_request_id id, wifi_interface_handle iface,
157         unsigned num_rtt_config, wifi_rtt_config rtt_config[], wifi_rtt_event_handler handler);
158 
159 /* API to cancel RTT measurements */
160 wifi_error wifi_rtt_range_cancel(wifi_request_id id,  wifi_interface_handle iface,
161         unsigned num_devices, mac_addr addr[]);
162 
163 /* NBD ranging channel map */
164 typedef struct {
165     wifi_channel availablity[32]; // specifies the channel map for each of the 16 TU windows
166     // frequency of 0 => unspecified; which means firmware is
167     // free to do whatever it wants in this window.
168 } wifi_channel_map;
169 
170 /* API to start publishing the channel map on responder device in a NBD cluster.
171    Responder device will take this request and schedule broadcasting the channel map
172    in a NBD ranging attribute in a SDF. DE will automatically remove the ranging
173    attribute from the OTA queue after number of DW specified by num_dw
174    where Each DW is 512 TUs apart */
175 wifi_error wifi_rtt_channel_map_set(wifi_request_id id,
176         wifi_interface_handle iface, wifi_channel_map *params, unsigned num_dw);
177 
178 /* API to clear the channel map on the responder device in a NBD cluster.
179    Responder device will cancel future ranging channel request, starting from “next”
180    DW interval and will also stop broadcasting NBD ranging attribute in SDF */
181 wifi_error wifi_rtt_channel_map_clear(wifi_request_id id,  wifi_interface_handle iface);
182 
183 // Preamble definition for bit mask used in wifi_rtt_capabilities
184 #define PREAMBLE_LEGACY 0x1
185 #define PREAMBLE_HT     0x2
186 #define PREAMBLE_VHT    0x4
187 
188 // BW definition for bit mask used in wifi_rtt_capabilities
189 #define BW_5_SUPPORT   0x1
190 #define BW_10_SUPPORT  0x2
191 #define BW_20_SUPPORT  0x4
192 #define BW_40_SUPPORT  0x8
193 #define BW_80_SUPPORT  0x10
194 #define BW_160_SUPPORT 0x20
195 
196 /* RTT Capabilities */
197 typedef struct {
198     byte rtt_one_sided_supported;  // if 1-sided rtt data collection is supported
199     byte rtt_ftm_supported;        // if ftm rtt data collection is supported
200     byte lci_support;              // if initiator supports LCI request. Applies to 2-sided RTT
201     byte lcr_support;              // if initiator supports LCR request. Applies to 2-sided RTT
202     byte preamble_support;         // bit mask indicates what preamble is supported by initiator
203     byte bw_support;               // bit mask indicates what BW is supported by initiator
204     byte responder_supported;      // if 11mc responder mode is supported
205     byte mc_version;               // draft 11mc spec version supported by chip. For instance,
206                                    // version 4.0 should be 40 and version 4.3 should be 43 etc.
207 } wifi_rtt_capabilities;
208 
209 /*  RTT capabilities of the device */
210 wifi_error wifi_get_rtt_capabilities(wifi_interface_handle iface, wifi_rtt_capabilities *capabilities);
211 
212 /* debugging definitions */
213 enum {
214     RTT_DEBUG_DISABLE,
215     RTT_DEBUG_LOG,
216     RTT_DEBUG_PROTO,
217     RTT_DEBUG_BURST,
218     RTT_DEBUG_ACCURACY,
219     RTT_DEBUG_LOGDETAIL
220 };  //rtt debug type
221 
222 enum {
223     RTT_DEBUG_FORMAT_TXT,
224     RTT_DEBUG_FORMAT_BINARY
225 }; //rtt debug format
226 
227 typedef struct rtt_debug {
228     unsigned version;
229     unsigned len; // total length of after len field
230     unsigned type;  // rtt debug type
231     unsigned format; //rtt debug format
232     char dbuf[0]; // debug content
233 } rtt_debug_t;
234 
235 /* set configuration for debug */
236 wifi_error wifi_rtt_debug_cfg(wifi_interface_handle h, unsigned rtt_dbg_type, char *cfgbuf, unsigned cfg_buf_size);
237 /* get the debug information */
238 wifi_error wifi_rtt_debug_get(wifi_interface_handle h, rtt_debug_t **debugbuf);
239 /* free the debug buffer */
240 wifi_error wifi_rtt_debug_free(wifi_interface_handle h, rtt_debug_t *debugbuf);
241 
242 /* API for setting LCI/LCR information to be provided to a requestor */
243 typedef enum {
244     WIFI_MOTION_NOT_EXPECTED = 0, // Not expected to change location
245     WIFI_MOTION_EXPECTED = 1,     // Expected to change location
246     WIFI_MOTION_UNKNOWN  = 2,     // Movement pattern unknown
247 } wifi_motion_pattern;
248 
249 typedef struct {
250     long latitude;              // latitude in degrees * 2^25 , 2's complement
251     long longitude;             // latitude in degrees * 2^25 , 2's complement
252     int  altitude;              // Altitude in units of 1/256 m
253     byte latitude_unc;          // As defined in Section 2.3.2 of IETF RFC 6225
254     byte longitude_unc;         // As defined in Section 2.3.2 of IETF RFC 6225
255     byte altitude_unc;          // As defined in Section 2.4.5 from IETF RFC 6225:
256 
257     //Following element for configuring the Z subelement
258     wifi_motion_pattern motion_pattern;
259     int  floor;                 // floor in units of 1/16th of floor. 0x80000000 if unknown.
260     int  height_above_floor;    // in units of 1/64 m
261     int  height_unc;            // in units of 1/64 m. 0 if unknown
262 } wifi_lci_information;
263 
264 typedef struct {
265     char country_code[2];       // country code
266     int  length;                // length of the info field
267     char civic_info[256];       // Civic info to be copied in FTM frame
268 } wifi_lcr_information;
269 
270 // API to configure the LCI. Used in RTT Responder mode only
271 wifi_error wifi_set_lci(wifi_request_id id, wifi_interface_handle iface,
272                         wifi_lci_information *lci);
273 
274 // API to configure the LCR. Used in RTT Responder mode only.
275 wifi_error wifi_set_lcr(wifi_request_id id, wifi_interface_handle iface,
276                         wifi_lcr_information *lcr);
277 
278 /**
279  * RTT Responder information
280  */
281 typedef struct {
282     wifi_channel_info channel;
283     wifi_rtt_preamble preamble;
284 } wifi_rtt_responder;
285 
286 /**
287  * Get RTT responder information e.g. WiFi channel to enable responder on.
288  */
289 wifi_error wifi_rtt_get_responder_info(wifi_interface_handle iface,
290                                        wifi_rtt_responder *responder_info);
291 
292 /**
293  * Enable RTT responder mode.
294  * channel_hint - hint of the channel information where RTT responder should be enabled on.
295  * max_duration_seconds - timeout of responder mode.
296  * channel_used - channel used for RTT responder, NULL if responder is not enabled.
297  */
298 wifi_error wifi_enable_responder(wifi_request_id id, wifi_interface_handle iface,
299                                  wifi_channel_info channel_hint, unsigned max_duration_seconds,
300                                  wifi_rtt_responder *responder_info);
301 
302 /**
303  * Disable RTT responder mode.
304  */
305 wifi_error wifi_disable_responder(wifi_request_id id, wifi_interface_handle iface);
306 
307 #endif
308