summaryrefslogtreecommitdiff
path: root/mac/fira_session.h
blob: d890f3104c33fef4546af4a3bcf0f2d363095cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
 * This file is part of the UWB stack for linux.
 *
 * Copyright (c) 2020-2022 Qorvo US, Inc.
 *
 * This software is provided under the GNU General Public License, version 2
 * (GPLv2), as well as under a Qorvo commercial license.
 *
 * You may choose to use this software under the terms of the GPLv2 License,
 * version 2 ("GPLv2"), as published by the Free Software Foundation.
 * You should have received a copy of the GPLv2 along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * This program is distributed under the GPLv2 in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GPLv2 for more
 * details.
 *
 * If you cannot meet the requirements of the GPLv2, you may not use this
 * software for any purpose without first obtaining a commercial license from
 * Qorvo. Please contact Qorvo to inquire about licensing terms.
 */

#ifndef NET_MCPS802154_FIRA_SESSION_H
#define NET_MCPS802154_FIRA_SESSION_H

#include "fira_session_fsm.h"
#include "fira_region.h"
#include "fira_sts.h"
#include "fira_crypto.h"
#include "fira_round_hopping_crypto_impl.h"

/**
 * enum fira_controlee_state - State of controlee.
 * @FIRA_CONTROLEE_STATE_PENDING_RUN: The controlee will be set to running state
 * at the end of round.
 * @FIRA_CONTROLEE_STATE_RUNNING: The controlee is running.
 * @FIRA_CONTROLEE_STATE_PENDING_STOP: The controlee will be set to stopping
 * state at the end of round.
 * @FIRA_CONTROLEE_STATE_STOPPING: The controlee is stopping.
 * @FIRA_CONTROLEE_STATE_PENDING_DEL: The controlee will be set to deleting
 * state at the end of round.
 * @FIRA_CONTROLEE_STATE_DELETING: The controlee is being deleted.
 */
enum fira_controlee_state {
	FIRA_CONTROLEE_STATE_PENDING_RUN,
	FIRA_CONTROLEE_STATE_RUNNING,
	FIRA_CONTROLEE_STATE_PENDING_STOP,
	FIRA_CONTROLEE_STATE_STOPPING,
	FIRA_CONTROLEE_STATE_PENDING_DEL,
	FIRA_CONTROLEE_STATE_DELETING,
};

/**
 * struct fira_controlee - Represent a controlee.
 */
struct fira_controlee {
	/**
	 * @sub_session_id: Sub-session ID used by the controlee.
	 */
	__u32 sub_session_id;
	/**
	 * @short_addr: Short address of the controlee.
	 */
	__le16 short_addr;
	/**
	 * @sub_session_key_len: Length of the sub-session key used by
	 * the controlee.
	 */
	__u8 sub_session_key_len;
	/**
	 * @sub_session_key: Sub-session key used by the controlee.
	 */
	char sub_session_key[FIRA_KEY_SIZE_MAX];
	/**
	 * @sub_session: Is the controlee using a sub-session.
	 */
	bool sub_session;
	/**
	 * @state: Current state of the controlee.
	 */
	enum fira_controlee_state state;
	/**
	 * @range_data_ntf_status: range_data_ntf status of the controlee.
	 */
	enum fira_range_data_ntf_status range_data_ntf_status;
	/**
	 * @entry: Entry in list of controlees.
	 */
	struct list_head entry;
};

struct fira_measurement_sequence_step {
	enum fira_measurement_type type;
	u8 n_measurements;
	s8 rx_ant_set_nonranging;
	s8 rx_ant_sets_ranging[2];
	s8 tx_ant_set_nonranging;
	s8 tx_ant_set_ranging;
};

struct fira_measurement_sequence {
	struct fira_measurement_sequence_step
		steps[FIRA_MEASUREMENT_SEQUENCE_STEP_MAX];
	size_t n_steps;
};

struct fira_session_params {
	/* Main parameters. */
	enum fira_device_type device_type;
	enum fira_ranging_round_usage ranging_round_usage;
	enum fira_multi_node_mode multi_node_mode;
	__le16 short_addr;
	__le16 controller_short_addr;
	/* Timings parameters. */
	int initiation_time_ms;
	int slot_duration_dtu;
	int block_duration_dtu;
	int round_duration_slots;
	/* Behaviour parameters. */
	u32 block_stride_len;
	u32 max_number_of_measurements;
	u32 max_rr_retry;
	bool round_hopping;
	u8 priority;
	bool result_report_phase;
	/* Radio. */
	int channel_number;
	int preamble_code_index;
	enum fira_rframe_config rframe_config;
	enum fira_preambule_duration preamble_duration;
	enum fira_sfd_id sfd_id;
	enum fira_sts_segments number_of_sts_segments;
	enum fira_psdu_data_rate psdu_data_rate;
	enum fira_mac_fcs_type mac_fcs_type;
	enum fira_prf_mode prf_mode;
	enum fira_phr_data_rate phr_data_rate;
	/* STS and crypto. */
	enum fira_sts_mode sts_config;
	u8 vupper64[FIRA_VUPPER64_SIZE];
	u8 session_key_len;
	u8 session_key[FIRA_KEY_SIZE_MAX];
	bool key_rotation;
	u8 key_rotation_rate;
	bool aoa_result_req;
	bool report_tof;
	bool report_aoa_azimuth;
	bool report_aoa_elevation;
	bool report_aoa_fom;
	enum fira_rssi_report_type report_rssi;
	struct fira_measurement_sequence meas_seq;
	u32 data_vendor_oui;
	u8 data_payload[FIRA_DATA_PAYLOAD_SIZE_MAX];
	u32 data_payload_seq;
	int data_payload_len;
	bool report_diagnostics;
	enum fira_ranging_diagnostics_frame_report_flags diagnostic_report_flags;
	/* Misc */
	enum fira_sts_length sts_length;
	enum fira_range_data_ntf_config range_data_ntf_config;
	u32 range_data_ntf_proximity_near_rctu;
	u32 range_data_ntf_proximity_far_rctu;
	s16 range_data_ntf_lower_bound_aoa_azimuth_2pi;
	s16 range_data_ntf_upper_bound_aoa_azimuth_2pi;
	s16 range_data_ntf_lower_bound_aoa_elevation_2pi;
	s16 range_data_ntf_upper_bound_aoa_elevation_2pi;
};

/**
 * struct fira_session - Session information.
 */
struct fira_session {
	/**
	 * @id: Session identifier.
	 */
	u32 id;
	/**
	 * @sequence_number: Session notification counter.
	 */
	u32 sequence_number;
	/**
	 * @entry: Entry in list of sessions.
	 */
	struct list_head entry;
	/**
	 * @state: State of the session.
	 */
	const struct fira_session_fsm_state *state;
	/**
	 * @params: Session parameters, mostly read only while the session is
	 * active.
	 */
	struct fira_session_params params;
	/**
	 * @hrp_uwb_params: HRP UWB parameters, read only while the session is
	 * active.
	 */
	struct mcps802154_hrp_uwb_params hrp_uwb_params;
	/**
	 * @event_portid: Port identifier to use for notifications.
	 */
	u32 event_portid;
	/**
	 * @block_start_valid: True when block_start_dtu is valid.
	 * It's false on the first access wo initiation delay.
	 */
	bool block_start_valid;
	/**
	 * @block_start_dtu: Block start timestamp in dtu of the last
	 * get_access.
	 */
	u32 block_start_dtu;
	/**
	 * @next_access_timestamp_dtu: Next access timestamp in dtu.
	 * It's equal to block_start_dtu when the hopping is disabled.
	 * Otherwise it's beyond the block_start_dtu.
	 * It's updated after each good or missed ranging round.
	 */
	u32 next_access_timestamp_dtu;
	/**
	 * @last_access_timestamp_dtu: Last timestamp where the session got
	 * the access.
	 * It's used only on session's election, when a negotiation between
	 * two session fails.
	 */
	u32 last_access_timestamp_dtu;
	/**
	 * @block_index: Block index used on the last access.
	 */
	u32 block_index;
	/**
	 * @block_stride_len: Stride length indicates how many ranging blocks
	 * will be skipped.
	 * The value is updated at the beginning of an access.
	 */
	int block_stride_len;
	/**
	 * @round_index: Round index used on the last access.
	 */
	int round_index;
	/**
	 * @next_round_index: Next round index a announced in measurement
	 * report message.
	 */
	int next_round_index;
	/**
	* @stop_request: Session has been requested to stop.
	*/
	bool stop_request;
	/**
	 * @stop_inband: Session has been requested to stop by controller. This
	 * field is only used for controlee sessions.
	 */
	bool stop_inband;
	/**
	 * @stop_no_response: Session has been requested to stop because ranging
	 * failed for max_rr_retry consecutive rounds.
	 */
	bool stop_no_response;
	/**
	 * @n_ranging_round_retry: Number of ranging round failed.
	 * Counter reset on ranging round success.
	 */
	int n_ranging_round_retry;

	/**
	 * @round_hopping_sequence: Round hopping sequence generation context.
	 */
	struct fira_round_hopping_sequence round_hopping_sequence;
	/**
	 * @controlee: Group of persistent variable(s) used when session
	 * is a controlee.
	 */
	struct {
		/**
		 * @synchronised: Whether a controlee session was synchronised.
		 */
		bool synchronised;
		/**
		 * @block_index_sync: Last block index received.
		 */
		int block_index_sync;
		/**
		 * @hopping_mode: True when hopping is enabled on last
		 * measurement frame.
		 */
		bool hopping_mode;
		/**
		 * @next_round_index_valid: True when the next round index
		 * is present in measurement report frame.
		 */
		bool next_round_index_valid;
		/**
		* @ctlr_range_data_ntf_status: range_data_ntf status of the
		* controller.
		*/
		enum fira_range_data_ntf_status ctlr_range_data_ntf_status;
	} controlee;
	/**
	 * @controller: Group of persistent variable(s) used when session
	 * is a controller.
	 */
	struct {
		/**
		 * @next_block_index: Next block index built on get access with
		 * next round index.
		 * It's only to avoid to rebuild the next round index on next
		 * access, when this last occur in time as block index will
		 * match.
		 */
		u32 next_block_index;
	} controller;
	/**
	 * @data_payload: Local context for data_payload feature.
	 */
	struct {
		/**
		 * @seq: Sequence number of last sent data.
		 */
		u32 seq;
		/**
		 * @sent: True when data have been send during ranging round.
		 */
		bool sent;
	} data_payload;
	/**
	 * @current_controlees: Current list of controlees.
	 */
	struct list_head current_controlees;
	/**
	 * @n_current_controlees: Number of elements in the list of current
	 * controlees.
	 */
	size_t n_current_controlees;
	/**
	 * @measurements: Measurement configurations which influence diagnostics.
	 */
	struct {
		/**
		 * @sequence: Copy of the meas_seq parameter on get_access
		 * event.
		 */
		struct fira_measurement_sequence sequence;
		/**
		 * @index: Index of the step in sequence array.
		 */
		int index;
		/**
		 * @n_achieved: Number of measurements done inside a step.
		 */
		int n_achieved;
		/**
		 * @n_total_achieved: Total number of measurements done.
		 */
		int n_total_achieved;
		/**
		 * @reset: True when new parameters have to be retrieved.
		 */
		bool reset;
	} measurements;
	/**
	 * @rx_ctx: Custom rx context for all controlees.
	 */
	void *rx_ctx[FIRA_CONTROLEES_MAX];
	/**
	 * @crypto: crypto related variables.
	 */
	struct fira_crypto *crypto;
	/**
	 * @sts: sts related variables.
	 */
	struct {
		/**
		 * @phy_sts_index_init: Initial phy_sts_index deduced at context init.
		 */
		u32 phy_sts_index_init;

		/**
		 * @last_rotation_block_index: index to the last block where the
		 * rotation occurred.
		 */
		u32 last_rotation_block_index;
	} sts;
	/*
	 * @last_error: last error that occurred during the active session.
	 */
	int last_error;
};

/**
 * struct fira_session_demand - Next access information for one FiRa session.
 */
struct fira_session_demand {
	/**
	 * @block_start_dtu: Block start in dtu.
	 */
	u32 block_start_dtu;
	/**
	 * @timestamp_dtu: Access timestamp in dtu.
	 */
	u32 timestamp_dtu;
	/**
	 * @max_duration_dtu: Maximum duration for the access.
	 */
	int max_duration_dtu;
	/**
	 * @add_blocks: Number of block to add.
	 */
	int add_blocks;
	/**
	 * @round_index: Round index to apply for the access.
	 */
	int round_index;
	/**
	 * @rx_timeout_dtu: timeout to apply when first frame of the controlee.
	 */
	int rx_timeout_dtu;
};

/**
 * struct fira_report_info - Report information for all peer.
 */
struct fira_report_info {
	/**
	 * @ranging_data: Base address of ranging data per peer, or null
	 * pointer.
	 */
	struct fira_ranging_info *ranging_data;
	/**
	 * @n_ranging_data: Number of entry in ranging_data above.
	 */
	size_t n_ranging_data;
	/**
	 * @stopped_controlees: NULL, or short address of all stopped controlees.
	 */
	const __le16 *stopped_controlees;
	/**
	 * @n_stopped_controlees: Number of controlees stopped in array above.
	 */
	size_t n_stopped_controlees;
	/**
	 * @diagnostics: Array of diagnostic collected per slots.
	 */
	const struct fira_diagnostic *diagnostics;
	/**
	 * @slots: Array of information slots.
	 */
	const struct fira_slot *slots;
	/**
	 * @n_slots: Number of slots above.
	 */
	size_t n_slots;
	/**
	 * @stopped: True when the session is stopped.
	 */
	bool stopped;
};

/**
 * fira_session_new() - Create a new session.
 * @local: FiRa context.
 * @session_id: Session identifier, must be unique.
 *
 * Return: The new session or NULL on error.
 */
struct fira_session *fira_session_new(struct fira_local *local, u32 session_id);

/**
 * fira_session_free() - Remove a session.
 * @local: FiRa context.
 * @session: Session to remove, must be inactive.
 */
void fira_session_free(struct fira_local *local, struct fira_session *session);

/**
 * fira_session_set_controlees() - Set controlees.
 * @local: FiRa context.
 * @session: Session.
 * @controlees: List of controlees.
 * @n_controlees: Number of controlees.
 *
 * Return: 0 or error.
 */
int fira_session_set_controlees(struct fira_local *local,
				struct fira_session *session,
				struct list_head *controlees, int n_controlees);

/**
 * fira_session_new_controlees() - Add new controlees.
 * @session: Session.
 * @controlees: List of controlees to add.
 * @n_controlees: Number of controlees.
 * @async: True is the controlees must be added asynchronously.
 *
 * Return: 0 or error.
 */
int fira_session_new_controlees(struct fira_session *session,
				struct list_head *controlees, int n_controlees,
				bool async);

/**
 * fira_session_restart_controlees() - Restart controlee and erase pending del.
 * @session: FiRa session context.
 *
 * Return: Number of controlee removed.
 */
void fira_session_restart_controlees(struct fira_session *session);

/**
 * fira_session_del_controlees() - Delete controlees.
 * @session: Session.
 * @controlees: List of controlees to delete.
 * @async: True is the controlees must be deleted asynchronously.
 *
 * Return: 0 or error.
 */
int fira_session_del_controlees(struct fira_session *session,
				struct list_head *controlees, bool async);

/**
 * fira_session_get_controlee() - Get controlee info from short address.
 * @session: Session.
 * @short_addr: Short address of the controlee.
 *
 * Return: The corresponding controlee object or NULL.
 */
static inline struct fira_controlee *
fira_session_get_controlee(struct fira_session *session, u16 short_addr)
{
        struct fira_controlee *controlee;
        list_for_each_entry (controlee, &session->current_controlees, entry) {
                if (controlee->short_addr == short_addr)
                        return controlee;
        }
        return NULL;
}

/**
 * fira_session_stop_controlees() - Stop controlees.
 * @session: Session.
 */
void fira_session_stop_controlees(struct fira_session *session);

/**
 * fira_session_controlees_running_count() - Get the number of running controlees.
 * @session: Session.
 *
 * Return: Number of running controlees.
 */
int fira_session_controlees_running_count(const struct fira_session *session);

/**
 * fira_session_update_controlees() - Update controlee's states.
 * @local: FiRa context.
 * @session: Session to test.
 */
void fira_session_update_controlees(struct fira_local *local,
				    struct fira_session *session);

/**
 * fira_session_is_ready() - Test whether a session is ready to be started.
 * @local: FiRa context.
 * @session: Session to test.
 *
 * Return: true if the session can be started.
 */
bool fira_session_is_ready(const struct fira_local *local,
			   const struct fira_session *session);

/**
 * fira_session_get_meas_seq_step() - Get current measurement step.
 * @session: Session.
 *
 * Return: Current Measurement Sequence step for given session.
 */
static inline const struct fira_measurement_sequence_step *
fira_session_get_meas_seq_step(const struct fira_session *session)
{
	const struct fira_measurement_sequence *seq =
		&session->measurements.sequence;

	return &seq->steps[session->measurements.index];
}

/**
 * fira_session_get_rx_ant_set() - Get Rx antenna set for a given message ID.
 * @session: Session.
 * @message_id: Message ID of FiRa frame.
 *
 * Return: Adequate antenna set id for given frame and session parameters.
 */
static inline int
fira_session_get_rx_ant_set(const struct fira_session *session,
			    enum fira_message_id message_id)
{
	const struct fira_session_params *params = &session->params;
	const struct fira_measurement_sequence_step *step =
		fira_session_get_meas_seq_step(session);

	if (message_id > FIRA_MESSAGE_ID_RFRAME_MAX)
		return step->rx_ant_set_nonranging;

	/* TODO: replace this test by device_role == FIRA_DEVICE_ROLE_INITIATOR
	* as soon as this feature is supported */
	if (params->device_type == FIRA_DEVICE_TYPE_CONTROLLER)
		return step->rx_ant_sets_ranging[0];
	else
		switch (step->type) {
		case FIRA_MEASUREMENT_TYPE_RANGE:
		case FIRA_MEASUREMENT_TYPE_AOA:
		case FIRA_MEASUREMENT_TYPE_AOA_AZIMUTH:
		case FIRA_MEASUREMENT_TYPE_AOA_ELEVATION:
			return step->rx_ant_sets_ranging[0];
		case FIRA_MEASUREMENT_TYPE_AOA_AZIMUTH_ELEVATION:
			return step->rx_ant_sets_ranging
				[message_id == FIRA_MESSAGE_ID_RANGING_FINAL];
		default:
			return -1;
		}
	return -1;
}

/**
 * fira_session_set_range_data_ntf_status() - Update range_data_ntf_status
 * for a given ranging.
 * @session: FiRa session.
 * @ranging_info: ranging data to be evaluated.
 */
void fira_session_set_range_data_ntf_status(
	const struct fira_session *session,
	struct fira_ranging_info *ranging_info);

/**
 * fira_session_report() - Report state change and ranging result for a session.
 * @local: FiRa context.
 * @session: Session to report.
 * @report_info: report information to exploit for the reporting.
 */
void fira_session_report(struct fira_local *local, struct fira_session *session,
			 struct fira_report_info *report_info);

/**
 * fira_session_controlee_active() - Return whether the controlee is currently active.
 * @controlee: Controlee.
 *
 * Return: True if the controlee is currently active.
 */
static inline bool
fira_session_controlee_active(struct fira_controlee *controlee)
{
	switch (controlee->state) {
	case FIRA_CONTROLEE_STATE_RUNNING:
	case FIRA_CONTROLEE_STATE_PENDING_STOP:
	case FIRA_CONTROLEE_STATE_PENDING_DEL:
		return true;
	default:
		return false;
	}
}

#endif /* NET_MCPS802154_FIRA_SESSION_H */