summaryrefslogtreecommitdiff
path: root/src/rust/uwb_core/src/session/session_manager.rs
blob: 3c05a1ebd92ccdfc1a3a0402f8bb4b3b21355ae9 (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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
// Copyright 2022, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeMap;

use log::{debug, error, warn};
use tokio::sync::{mpsc, oneshot};

use crate::error::{Error, Result};
use crate::params::app_config_params::AppConfigParams;
use crate::params::uci_packets::{
    Controlee, ReasonCode, SessionId, SessionState, SessionType, UpdateMulticastListAction,
};
use crate::session::uwb_session::{Response as SessionResponse, ResponseSender, UwbSession};
use crate::uci::notification::{SessionNotification as UciSessionNotification, SessionRangeData};
use crate::uci::uci_manager::UciManager;
use crate::utils::clean_mpsc_receiver;

const MAX_SESSION_COUNT: usize = 5;

/// The notifications that are sent from SessionManager to its caller.
#[derive(Debug, PartialEq)]
pub(crate) enum SessionNotification {
    SessionState { session_id: SessionId, session_state: SessionState, reason_code: ReasonCode },
    RangeData { session_id: SessionId, range_data: SessionRangeData },
}

/// The SessionManager organizes the state machine of the existing UWB ranging sessions, sends
/// the session-related requests to the UciManager, and handles the session notifications from the
/// UciManager.
/// Using the actor model, SessionManager delegates the requests to SessionManagerActor.
pub(crate) struct SessionManager {
    cmd_sender: mpsc::UnboundedSender<(SessionCommand, ResponseSender)>,
}

impl SessionManager {
    pub fn new<T: UciManager>(
        uci_manager: T,
        uci_notf_receiver: mpsc::UnboundedReceiver<UciSessionNotification>,
        session_notf_sender: mpsc::UnboundedSender<SessionNotification>,
    ) -> Self {
        let (cmd_sender, cmd_receiver) = mpsc::unbounded_channel();
        let mut actor = SessionManagerActor::new(
            cmd_receiver,
            uci_manager,
            uci_notf_receiver,
            session_notf_sender,
        );
        tokio::spawn(async move { actor.run().await });

        Self { cmd_sender }
    }

    pub async fn init_session(
        &mut self,
        session_id: SessionId,
        session_type: SessionType,
        params: AppConfigParams,
    ) -> Result<()> {
        let result = self
            .send_cmd(SessionCommand::InitSession { session_id, session_type, params })
            .await
            .map(|_| ());
        if result.is_err() && result != Err(Error::DuplicatedSessionId) {
            let _ = self.deinit_session(session_id).await;
        }
        result
    }

    pub async fn deinit_session(&mut self, session_id: SessionId) -> Result<()> {
        self.send_cmd(SessionCommand::DeinitSession { session_id }).await?;
        Ok(())
    }

    pub async fn start_ranging(&mut self, session_id: SessionId) -> Result<AppConfigParams> {
        match self.send_cmd(SessionCommand::StartRanging { session_id }).await? {
            SessionResponse::AppConfigParams(params) => Ok(params),
            _ => panic!("start_ranging() should reply AppConfigParams result"),
        }
    }

    pub async fn stop_ranging(&mut self, session_id: SessionId) -> Result<()> {
        self.send_cmd(SessionCommand::StopRanging { session_id }).await?;
        Ok(())
    }

    pub async fn reconfigure(
        &mut self,
        session_id: SessionId,
        params: AppConfigParams,
    ) -> Result<()> {
        self.send_cmd(SessionCommand::Reconfigure { session_id, params }).await?;
        Ok(())
    }

    pub async fn update_controller_multicast_list(
        &mut self,
        session_id: SessionId,
        action: UpdateMulticastListAction,
        controlees: Vec<Controlee>,
    ) -> Result<()> {
        self.send_cmd(SessionCommand::UpdateControllerMulticastList {
            session_id,
            action,
            controlees,
        })
        .await?;
        Ok(())
    }

    pub async fn session_params(&mut self, session_id: SessionId) -> Result<AppConfigParams> {
        match self.send_cmd(SessionCommand::GetParams { session_id }).await? {
            SessionResponse::AppConfigParams(params) => Ok(params),
            _ => panic!("session_params() should reply AppConfigParams result"),
        }
    }

    // Send the |cmd| to the SessionManagerActor.
    async fn send_cmd(&self, cmd: SessionCommand) -> Result<SessionResponse> {
        let (result_sender, result_receiver) = oneshot::channel();
        self.cmd_sender.send((cmd, result_sender)).map_err(|cmd| {
            error!("Failed to send cmd: {:?}", cmd.0);
            Error::Unknown
        })?;
        result_receiver.await.unwrap_or_else(|e| {
            error!("Failed to receive the result for cmd: {:?}", e);
            Err(Error::Unknown)
        })
    }
}

struct SessionManagerActor<T: UciManager> {
    // Receive the commands and the corresponding response senders from SessionManager.
    cmd_receiver: mpsc::UnboundedReceiver<(SessionCommand, ResponseSender)>,
    // Send the notification to SessionManager's caller.
    session_notf_sender: mpsc::UnboundedSender<SessionNotification>,

    // The UciManager for delegating UCI requests.
    uci_manager: T,
    // Receive the notification from |uci_manager|.
    uci_notf_receiver: mpsc::UnboundedReceiver<UciSessionNotification>,

    active_sessions: BTreeMap<SessionId, UwbSession>,
}

impl<T: UciManager> SessionManagerActor<T> {
    fn new(
        cmd_receiver: mpsc::UnboundedReceiver<(SessionCommand, ResponseSender)>,
        uci_manager: T,
        uci_notf_receiver: mpsc::UnboundedReceiver<UciSessionNotification>,
        session_notf_sender: mpsc::UnboundedSender<SessionNotification>,
    ) -> Self {
        Self {
            cmd_receiver,
            session_notf_sender,
            uci_manager,
            uci_notf_receiver,
            active_sessions: BTreeMap::new(),
        }
    }

    async fn run(&mut self) {
        loop {
            tokio::select! {
                cmd = self.cmd_receiver.recv() => {
                    match cmd {
                        None => {
                            debug!("SessionManager is about to drop.");
                            break;
                        },
                        Some((cmd, result_sender)) => {
                            self.handle_cmd(cmd, result_sender);
                        }
                    }
                }

                Some(notf) = self.uci_notf_receiver.recv() => {
                    self.handle_uci_notification(notf);
                }
            }
        }
    }

    fn handle_cmd(&mut self, cmd: SessionCommand, result_sender: ResponseSender) {
        match cmd {
            SessionCommand::InitSession { session_id, session_type, params } => {
                if self.active_sessions.contains_key(&session_id) {
                    warn!("Session {} already exists", session_id);
                    let _ = result_sender.send(Err(Error::DuplicatedSessionId));
                    return;
                }
                if self.active_sessions.len() == MAX_SESSION_COUNT {
                    warn!("The amount of active sessions already reached {}", MAX_SESSION_COUNT);
                    let _ = result_sender.send(Err(Error::MaxSessionsExceeded));
                    return;
                }

                if !params.is_type_matched(session_type) {
                    warn!(
                        "session_type {:?} doesn't match with the params {:?}",
                        session_type, params
                    );
                    let _ = result_sender.send(Err(Error::BadParameters));
                    return;
                }

                let mut session =
                    UwbSession::new(self.uci_manager.clone(), session_id, session_type);
                session.initialize(params, result_sender);

                // We store the session first. If the initialize() fails, then SessionManager will
                // call deinit_session() to remove it.
                self.active_sessions.insert(session_id, session);
            }
            SessionCommand::DeinitSession { session_id } => {
                match self.active_sessions.remove(&session_id) {
                    None => {
                        warn!("Session {} doesn't exist", session_id);
                        let _ = result_sender.send(Err(Error::BadParameters));
                    }
                    Some(mut session) => {
                        session.deinitialize(result_sender);
                    }
                }
            }
            SessionCommand::StartRanging { session_id } => {
                match self.active_sessions.get_mut(&session_id) {
                    None => {
                        warn!("Session {} doesn't exist", session_id);
                        let _ = result_sender.send(Err(Error::BadParameters));
                    }
                    Some(session) => {
                        session.start_ranging(result_sender);
                    }
                }
            }
            SessionCommand::StopRanging { session_id } => {
                match self.active_sessions.get_mut(&session_id) {
                    None => {
                        warn!("Session {} doesn't exist", session_id);
                        let _ = result_sender.send(Err(Error::BadParameters));
                    }
                    Some(session) => {
                        session.stop_ranging(result_sender);
                    }
                }
            }
            SessionCommand::Reconfigure { session_id, params } => {
                match self.active_sessions.get_mut(&session_id) {
                    None => {
                        warn!("Session {} doesn't exist", session_id);
                        let _ = result_sender.send(Err(Error::BadParameters));
                    }
                    Some(session) => {
                        session.reconfigure(params, result_sender);
                    }
                }
            }
            SessionCommand::UpdateControllerMulticastList { session_id, action, controlees } => {
                match self.active_sessions.get_mut(&session_id) {
                    None => {
                        warn!("Session {} doesn't exist", session_id);
                        let _ = result_sender.send(Err(Error::BadParameters));
                    }
                    Some(session) => {
                        session.update_controller_multicast_list(action, controlees, result_sender);
                    }
                }
            }
            SessionCommand::GetParams { session_id } => {
                match self.active_sessions.get_mut(&session_id) {
                    None => {
                        warn!("Session {} doesn't exist", session_id);
                        let _ = result_sender.send(Err(Error::BadParameters));
                    }
                    Some(session) => {
                        session.params(result_sender);
                    }
                }
            }
        }
    }

    fn handle_uci_notification(&mut self, notf: UciSessionNotification) {
        match notf {
            UciSessionNotification::Status { session_token, session_state, reason_code } => {
                let reason_code = match ReasonCode::try_from(reason_code) {
                    Ok(r) => r,
                    Err(_) => {
                        error!(
                            "Received unknown reason_code {:?} in UciSessionNotification",
                            reason_code
                        );
                        return;
                    }
                };
                if session_state == SessionState::SessionStateDeinit {
                    debug!("Session {} is deinitialized", session_token);
                    let _ = self.active_sessions.remove(&session_token);
                    let _ = self.session_notf_sender.send(SessionNotification::SessionState {
                        session_id: session_token,
                        session_state,
                        reason_code,
                    });
                    return;
                }

                match self.active_sessions.get_mut(&session_token) {
                    Some(session) => {
                        session.on_session_status_changed(session_state);
                        let _ = self.session_notf_sender.send(SessionNotification::SessionState {
                            session_id: session_token,
                            session_state,
                            reason_code,
                        });
                    }
                    None => {
                        warn!(
                            "Received notification of the unknown Session {}: {:?}, {:?}",
                            session_token, session_state, reason_code
                        );
                    }
                }
            }
            UciSessionNotification::UpdateControllerMulticastList {
                session_token,
                remaining_multicast_list_size: _,
                status_list,
            } => match self.active_sessions.get_mut(&session_token) {
                Some(session) => session.on_controller_multicast_list_udpated(status_list),
                None => {
                    warn!(
                        "Received the notification of the unknown Session {}: {:?}",
                        session_token, status_list
                    );
                }
            },
            UciSessionNotification::SessionInfo(range_data) => {
                if self.active_sessions.get(&range_data.session_token).is_some() {
                    let _ = self.session_notf_sender.send(SessionNotification::RangeData {
                        session_id: range_data.session_token,
                        range_data,
                    });
                } else {
                    warn!("Received range data of the unknown Session: {:?}", range_data);
                }
            }
            UciSessionNotification::DataCredit { session_token, credit_availability: _ } => {
                match self.active_sessions.get(&session_token) {
                    Some(_) => {
                        /*
                         * TODO(b/270443790): Handle the DataCredit notification in the new
                         * code flow.
                         */
                    }
                    None => {
                        warn!(
                            "Received the Data Credit notification for an unknown Session {}",
                            session_token
                        );
                    }
                }
            }
            UciSessionNotification::DataTransferStatus {
                session_token,
                uci_sequence_number: _,
                status: _,
                tx_count: _,
            } => {
                match self.active_sessions.get(&session_token) {
                    Some(_) => {
                        /*
                         * TODO(b/270443790): Handle the DataTransferStatus notification in the
                         * new code flow.
                         */
                    }
                    None => {
                        warn!(
                            "Received a Data Transfer Status notification for unknown Session {}",
                            session_token
                        );
                    }
                }
            }
        }
    }
}

impl<T: UciManager> Drop for SessionManagerActor<T> {
    fn drop(&mut self) {
        // mpsc receiver is about to be dropped. Clean shutdown the mpsc message.
        clean_mpsc_receiver(&mut self.uci_notf_receiver);
    }
}

#[derive(Debug)]
enum SessionCommand {
    InitSession {
        session_id: SessionId,
        session_type: SessionType,
        params: AppConfigParams,
    },
    DeinitSession {
        session_id: SessionId,
    },
    StartRanging {
        session_id: SessionId,
    },
    StopRanging {
        session_id: SessionId,
    },
    Reconfigure {
        session_id: SessionId,
        params: AppConfigParams,
    },
    UpdateControllerMulticastList {
        session_id: SessionId,
        action: UpdateMulticastListAction,
        controlees: Vec<Controlee>,
    },
    GetParams {
        session_id: SessionId,
    },
}

#[cfg(test)]
pub(crate) mod test_utils {
    use super::*;

    use crate::params::ccc_app_config_params::*;
    use crate::params::fira_app_config_params::*;
    use crate::params::uci_packets::{
        RangingMeasurementType, ReasonCode, ShortAddressTwoWayRangingMeasurement, StatusCode,
    };
    use crate::params::GetDeviceInfoResponse;
    use crate::uci::mock_uci_manager::MockUciManager;
    use crate::uci::notification::{RangingMeasurements, UciNotification};
    use crate::utils::init_test_logging;
    use uwb_uci_packets::StatusCode::UciStatusOk;

    const GET_DEVICE_INFO_RSP: GetDeviceInfoResponse = GetDeviceInfoResponse {
        status: UciStatusOk,
        uci_version: 0,
        mac_version: 0,
        phy_version: 0,
        uci_test_version: 0,
        vendor_spec_info: vec![],
    };

    pub(crate) fn generate_params() -> AppConfigParams {
        FiraAppConfigParamsBuilder::new()
            .device_type(DeviceType::Controller)
            .multi_node_mode(MultiNodeMode::Unicast)
            .device_mac_address(UwbAddress::Short([1, 2]))
            .dst_mac_address(vec![UwbAddress::Short([3, 4])])
            .device_role(DeviceRole::Initiator)
            .vendor_id([0xFE, 0xDC])
            .static_sts_iv([0xDF, 0xCE, 0xAB, 0x12, 0x34, 0x56])
            .build()
            .unwrap()
    }

    pub(crate) fn generate_ccc_params() -> AppConfigParams {
        CccAppConfigParamsBuilder::new()
            .protocol_version(CccProtocolVersion { major: 2, minor: 1 })
            .uwb_config(CccUwbConfig::Config0)
            .pulse_shape_combo(CccPulseShapeCombo {
                initiator_tx: PulseShape::PrecursorFree,
                responder_tx: PulseShape::PrecursorFreeSpecial,
            })
            .ran_multiplier(3)
            .channel_number(CccUwbChannel::Channel9)
            .chaps_per_slot(ChapsPerSlot::Value9)
            .num_responder_nodes(1)
            .slots_per_rr(3)
            .sync_code_index(12)
            .hopping_mode(CccHoppingMode::ContinuousAes)
            .build()
            .unwrap()
    }

    // TODO(b/321757248): Add a unit test generate_aliro_params().

    pub(crate) fn session_range_data(session_id: SessionId) -> SessionRangeData {
        SessionRangeData {
            sequence_number: 1,
            session_token: session_id,
            current_ranging_interval_ms: 3,
            ranging_measurement_type: RangingMeasurementType::TwoWay,
            ranging_measurements: RangingMeasurements::ShortAddressTwoWay(vec![
                ShortAddressTwoWayRangingMeasurement {
                    mac_address: 0x123,
                    status: StatusCode::UciStatusOk,
                    nlos: 0,
                    distance: 4,
                    aoa_azimuth: 5,
                    aoa_azimuth_fom: 6,
                    aoa_elevation: 7,
                    aoa_elevation_fom: 8,
                    aoa_destination_azimuth: 9,
                    aoa_destination_azimuth_fom: 10,
                    aoa_destination_elevation: 11,
                    aoa_destination_elevation_fom: 12,
                    slot_index: 0,
                    rssi: u8::MAX,
                },
            ]),
            rcr_indicator: 0,
            raw_ranging_data: vec![0x12, 0x34],
        }
    }

    pub(crate) fn session_status_notf(
        session_id: SessionId,
        session_state: SessionState,
    ) -> UciNotification {
        UciNotification::Session(UciSessionNotification::Status {
            session_token: session_id,
            session_state,
            reason_code: ReasonCode::StateChangeWithSessionManagementCommands.into(),
        })
    }

    pub(crate) fn range_data_notf(range_data: SessionRangeData) -> UciNotification {
        UciNotification::Session(UciSessionNotification::SessionInfo(range_data))
    }

    pub(super) async fn setup_session_manager<F>(
        setup_uci_manager_fn: F,
    ) -> (SessionManager, MockUciManager, mpsc::UnboundedReceiver<SessionNotification>)
    where
        F: FnOnce(&mut MockUciManager),
    {
        init_test_logging();
        let (uci_notf_sender, uci_notf_receiver) = mpsc::unbounded_channel();
        let (session_notf_sender, session_notf_receiver) = mpsc::unbounded_channel();
        let mut uci_manager = MockUciManager::new();
        uci_manager.expect_open_hal(vec![], Ok(GET_DEVICE_INFO_RSP));
        setup_uci_manager_fn(&mut uci_manager);
        uci_manager.set_session_notification_sender(uci_notf_sender).await;
        let _ = uci_manager.open_hal().await;

        (
            SessionManager::new(uci_manager.clone(), uci_notf_receiver, session_notf_sender),
            uci_manager,
            session_notf_receiver,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::test_utils::*;
    use super::*;

    use std::collections::HashMap;

    use crate::params::ccc_started_app_config_params::CccStartedAppConfigParams;
    use crate::params::uci_packets::{
        AppConfigTlv, AppConfigTlvType, ControleeStatus, Controlees, MulticastUpdateStatusCode,
        ReasonCode, SetAppConfigResponse, StatusCode,
    };
    use crate::params::utils::{u32_to_bytes, u64_to_bytes, u8_to_bytes};
    use crate::params::{FiraAppConfigParamsBuilder, KeyRotation};
    use crate::uci::notification::UciNotification;

    #[tokio::test]
    async fn test_init_deinit_session() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;
        let params = generate_params();

        let tlvs = params.generate_tlvs();
        let (mut session_manager, mut mock_uci_manager, mut session_notf_receiver) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_session_deinit(
                    session_id,
                    vec![session_status_notf(session_id, SessionState::SessionStateDeinit)],
                    Ok(()),
                );
            })
            .await;

        // Deinit a session before initialized should fail.
        let result = session_manager.deinit_session(session_id).await;
        assert_eq!(result, Err(Error::BadParameters));

        // Initialize a normal session should be successful.
        let result = session_manager.init_session(session_id, session_type, params.clone()).await;
        assert_eq!(result, Ok(()));
        let session_notf = session_notf_receiver.recv().await.unwrap();
        assert_eq!(
            session_notf,
            SessionNotification::SessionState {
                session_id,
                session_state: SessionState::SessionStateInit,
                reason_code: ReasonCode::StateChangeWithSessionManagementCommands
            }
        );
        let session_notf = session_notf_receiver.recv().await.unwrap();
        assert_eq!(
            session_notf,
            SessionNotification::SessionState {
                session_id,
                session_state: SessionState::SessionStateIdle,
                reason_code: ReasonCode::StateChangeWithSessionManagementCommands
            }
        );

        // Initialize a session multiple times without deinitialize should fail.
        let result = session_manager.init_session(session_id, session_type, params).await;
        assert_eq!(result, Err(Error::DuplicatedSessionId));

        // Deinitialize the session should be successful, and should receive the deinitialized
        // notification.
        let result = session_manager.deinit_session(session_id).await;
        assert_eq!(result, Ok(()));
        let session_notf = session_notf_receiver.recv().await.unwrap();
        assert_eq!(
            session_notf,
            SessionNotification::SessionState {
                session_id,
                session_state: SessionState::SessionStateDeinit,
                reason_code: ReasonCode::StateChangeWithSessionManagementCommands
            }
        );

        // Deinit a session after deinitialized should fail.
        let result = session_manager.deinit_session(session_id).await;
        assert_eq!(result, Err(Error::BadParameters));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_init_session_timeout() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;
        let params = generate_params();

        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                let notfs = vec![]; // Not sending SessionStatus notification.
                uci_manager.expect_session_init(session_id, session_type, notfs, Ok(()));
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params).await;
        assert_eq!(result, Err(Error::Timeout));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_start_stop_ranging() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;
        let params = generate_params();
        let tlvs = params.generate_tlvs();

        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_range_start(
                    session_id,
                    vec![session_status_notf(session_id, SessionState::SessionStateActive)],
                    Ok(()),
                );
                uci_manager.expect_range_stop(
                    session_id,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(()),
                );
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params.clone()).await;
        assert_eq!(result, Ok(()));
        let result = session_manager.start_ranging(session_id).await;
        assert_eq!(result, Ok(params));
        let result = session_manager.stop_ranging(session_id).await;
        assert_eq!(result, Ok(()));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_ccc_start_ranging() {
        let session_id = 0x123;
        let session_type = SessionType::Ccc;
        // params that is passed to UciManager::session_set_app_config().
        let params = generate_ccc_params();
        let tlvs = params.generate_tlvs();
        // The params that is received from UciManager::session_get_app_config().
        let received_config_map = HashMap::from([
            (AppConfigTlvType::StsIndex, u32_to_bytes(3)),
            (AppConfigTlvType::CccHopModeKey, u32_to_bytes(5)),
            (AppConfigTlvType::CccUwbTime0, u64_to_bytes(7)),
            (AppConfigTlvType::RangingDuration, u32_to_bytes(96)),
            (AppConfigTlvType::PreambleCodeIndex, u8_to_bytes(9)),
        ]);
        let received_tlvs = received_config_map
            .iter()
            .map(|(key, value)| AppConfigTlv::new(*key, value.clone()))
            .collect();
        let started_params =
            CccStartedAppConfigParams::from_config_map(received_config_map).unwrap();

        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_range_start(
                    session_id,
                    vec![session_status_notf(session_id, SessionState::SessionStateActive)],
                    Ok(()),
                );
                uci_manager.expect_session_get_app_config(session_id, vec![], Ok(received_tlvs));
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params.clone()).await;
        assert_eq!(result, Ok(()));
        let result = session_manager.start_ranging(session_id).await;
        assert_eq!(result, Ok(AppConfigParams::CccStarted(started_params)));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_update_controller_multicast_list() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;
        let params = generate_params();
        let tlvs = params.generate_tlvs();
        let action = UpdateMulticastListAction::AddControlee;
        let short_address: [u8; 2] = [0x12, 0x34];
        let controlees = vec![Controlee { short_address, subsession_id: 0x24 }];

        let controlees_clone = controlees.clone();
        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                let multicast_list_notf = vec![UciNotification::Session(
                    UciSessionNotification::UpdateControllerMulticastList {
                        session_token: session_id,
                        remaining_multicast_list_size: 1,
                        status_list: vec![ControleeStatus {
                            mac_address: [0x34, 0x12],
                            subsession_id: 0x24,
                            status: MulticastUpdateStatusCode::StatusOkMulticastListUpdate,
                        }],
                    },
                )];
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_session_update_controller_multicast_list(
                    session_id,
                    action,
                    Controlees::NoSessionKey(controlees_clone),
                    multicast_list_notf,
                    Ok(()),
                );
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params).await;
        assert_eq!(result, Ok(()));
        let result =
            session_manager.update_controller_multicast_list(session_id, action, controlees).await;
        assert_eq!(result, Ok(()));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_ccc_update_controller_multicast_list() {
        let session_id = 0x123;
        let session_type = SessionType::Ccc;
        let params = generate_ccc_params();
        let tlvs = params.generate_tlvs();
        let action = UpdateMulticastListAction::AddControlee;
        let short_address: [u8; 2] = [0x12, 0x34];
        let controlees = vec![Controlee { short_address, subsession_id: 0x24 }];

        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params).await;
        assert_eq!(result, Ok(()));
        // CCC session doesn't support update_controller_multicast_list.
        let result =
            session_manager.update_controller_multicast_list(session_id, action, controlees).await;
        assert_eq!(result, Err(Error::BadParameters));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_update_controller_multicast_list_without_notification() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;
        let params = generate_params();
        let tlvs = params.generate_tlvs();
        let action = UpdateMulticastListAction::AddControlee;
        let short_address: [u8; 2] = [0x12, 0x34];
        let controlees = vec![Controlee { short_address, subsession_id: 0x24 }];

        let controlees_clone = controlees.clone();
        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_session_update_controller_multicast_list(
                    session_id,
                    action,
                    uwb_uci_packets::Controlees::NoSessionKey(controlees_clone),
                    vec![], // Not sending notification.
                    Ok(()),
                );
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params).await;
        assert_eq!(result, Ok(()));
        // This method should timeout waiting for the notification.
        let result =
            session_manager.update_controller_multicast_list(session_id, action, controlees).await;
        assert_eq!(result, Err(Error::Timeout));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_receive_session_range_data() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;
        let params = generate_params();
        let tlvs = params.generate_tlvs();
        let range_data = session_range_data(session_id);
        let range_data_clone = range_data.clone();

        let (mut session_manager, mut mock_uci_manager, mut session_notf_receiver) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![
                        session_status_notf(session_id, SessionState::SessionStateIdle),
                        range_data_notf(range_data_clone),
                    ],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
            })
            .await;

        let result = session_manager.init_session(session_id, session_type, params).await;
        assert_eq!(result, Ok(()));
        let session_notf = session_notf_receiver.recv().await.unwrap();
        assert_eq!(
            session_notf,
            SessionNotification::SessionState {
                session_id,
                session_state: SessionState::SessionStateInit,
                reason_code: ReasonCode::StateChangeWithSessionManagementCommands
            }
        );
        let session_notf = session_notf_receiver.recv().await.unwrap();
        assert_eq!(
            session_notf,
            SessionNotification::SessionState {
                session_id,
                session_state: SessionState::SessionStateIdle,
                reason_code: ReasonCode::StateChangeWithSessionManagementCommands
            }
        );

        let session_notf = session_notf_receiver.recv().await.unwrap();
        assert_eq!(session_notf, SessionNotification::RangeData { session_id, range_data });

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_reconfigure_app_config() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;

        let initial_params = generate_params();
        let initial_tlvs = initial_params.generate_tlvs();

        let non_default_key_rotation_val = KeyRotation::Enable;
        let idle_params = FiraAppConfigParamsBuilder::from_params(&initial_params)
            .unwrap()
            .key_rotation(non_default_key_rotation_val)
            .build()
            .unwrap();
        let idle_tlvs = idle_params
            .generate_updated_tlvs(&initial_params, SessionState::SessionStateIdle)
            .unwrap();

        let non_default_block_stride_val = 2u8;
        let active_params = FiraAppConfigParamsBuilder::from_params(&idle_params)
            .unwrap()
            .block_stride_length(non_default_block_stride_val)
            .build()
            .unwrap();
        let active_tlvs = active_params
            .generate_updated_tlvs(&idle_params, SessionState::SessionStateIdle)
            .unwrap();

        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    initial_tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    idle_tlvs,
                    vec![],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
                uci_manager.expect_range_start(
                    session_id,
                    vec![session_status_notf(session_id, SessionState::SessionStateActive)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    active_tlvs,
                    vec![],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
            })
            .await;

        // Reconfiguring without first initing a session should fail.
        let result = session_manager.reconfigure(session_id, initial_params.clone()).await;
        assert_eq!(result, Err(Error::BadParameters));

        let result =
            session_manager.init_session(session_id, session_type, initial_params.clone()).await;
        assert_eq!(result, Ok(()));

        // Reconfiguring any parameters during idle state should succeed.
        let result = session_manager.reconfigure(session_id, idle_params.clone()).await;
        assert_eq!(result, Ok(()));

        let result = session_manager.start_ranging(session_id).await;
        assert_eq!(result, Ok(idle_params));

        // Reconfiguring most parameters during active state should fail.
        let result = session_manager.reconfigure(session_id, initial_params).await;
        assert_eq!(result, Err(Error::BadParameters));

        // Only some parameters are allowed to be reconfigured during active state.
        let result = session_manager.reconfigure(session_id, active_params).await;
        assert_eq!(result, Ok(()));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }

    #[tokio::test]
    async fn test_session_params() {
        let session_id = 0x123;
        let session_type = SessionType::FiraRangingSession;

        let params = generate_params();
        let tlvs = params.generate_tlvs();

        let (mut session_manager, mut mock_uci_manager, _) =
            setup_session_manager(move |uci_manager| {
                uci_manager.expect_session_init(
                    session_id,
                    session_type,
                    vec![session_status_notf(session_id, SessionState::SessionStateInit)],
                    Ok(()),
                );
                uci_manager.expect_session_set_app_config(
                    session_id,
                    tlvs,
                    vec![session_status_notf(session_id, SessionState::SessionStateIdle)],
                    Ok(SetAppConfigResponse {
                        status: StatusCode::UciStatusOk,
                        config_status: vec![],
                    }),
                );
            })
            .await;

        // Getting session params without initing a session should fail
        let result = session_manager.session_params(session_id).await;
        assert_eq!(result, Err(Error::BadParameters));

        let result = session_manager.init_session(session_id, session_type, params.clone()).await;
        result.unwrap();

        // Getting session params after they've been properly set should succeed
        let result = session_manager.session_params(session_id).await;
        assert_eq!(result, Ok(params));

        assert!(mock_uci_manager.wait_expected_calls_done().await);
    }
}