• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! This module offers a mocked version of UciManager for testing.
16 //!
17 //! The mocked version of UciManager mimics the behavior of the UCI manager and
18 //! stacks below it, such that tests can be run on a target without the UWB
19 //! hardware.
20 use std::collections::VecDeque;
21 use std::sync::{Arc, Mutex};
22 use std::time::Duration;
23 
24 use async_trait::async_trait;
25 use tokio::sync::{mpsc, Notify};
26 use tokio::time::timeout;
27 
28 use crate::error::{Error, Result};
29 use crate::params::uci_packets::{
30     app_config_tlvs_eq, device_config_tlvs_eq, radar_config_tlvs_eq, rf_test_config_tlvs_eq,
31     AndroidRadarConfigResponse, AppConfigTlv, AppConfigTlvType, CapTlv, ControleePhaseList,
32     Controlees, ControllerPhaseList, CoreSetConfigResponse, CountryCode, DeviceConfigId,
33     DeviceConfigTlv, GetDeviceInfoResponse, PowerStats, RadarConfigTlv, RadarConfigTlvType,
34     RawUciMessage, ResetConfig, RfTestConfigResponse, RfTestConfigTlv, SessionId, SessionState,
35     SessionToken, SessionType, SessionUpdateControllerMulticastResponse,
36     SessionUpdateDtTagRangingRoundsResponse, SetAppConfigResponse, UpdateMulticastListAction,
37 };
38 use crate::uci::notification::{
39     CoreNotification, DataRcvNotification, RadarDataRcvNotification, RfTestNotification,
40     SessionNotification, UciNotification,
41 };
42 use crate::uci::uci_logger::UciLoggerMode;
43 use crate::uci::uci_manager::UciManager;
44 
45 #[derive(Clone)]
46 /// Mock version of UciManager for testing.
47 pub struct MockUciManager {
48     expected_calls: Arc<Mutex<VecDeque<ExpectedCall>>>,
49     expect_call_consumed: Arc<Notify>,
50     core_notf_sender: mpsc::UnboundedSender<CoreNotification>,
51     session_notf_sender: mpsc::UnboundedSender<SessionNotification>,
52     vendor_notf_sender: mpsc::UnboundedSender<RawUciMessage>,
53     data_rcv_notf_sender: mpsc::UnboundedSender<DataRcvNotification>,
54     radar_data_rcv_notf_sender: mpsc::UnboundedSender<RadarDataRcvNotification>,
55     rf_test_notf_sender: mpsc::UnboundedSender<RfTestNotification>,
56 }
57 
58 #[allow(dead_code)]
59 impl MockUciManager {
60     /// Constructor.
new() -> Self61     pub fn new() -> Self {
62         Self {
63             expected_calls: Default::default(),
64             expect_call_consumed: Default::default(),
65             core_notf_sender: mpsc::unbounded_channel().0,
66             session_notf_sender: mpsc::unbounded_channel().0,
67             vendor_notf_sender: mpsc::unbounded_channel().0,
68             data_rcv_notf_sender: mpsc::unbounded_channel().0,
69             radar_data_rcv_notf_sender: mpsc::unbounded_channel().0,
70             rf_test_notf_sender: mpsc::unbounded_channel().0,
71         }
72     }
73 
74     /// Wait until expected calls are done.
75     ///
76     /// Returns false if calls are pending after 1 second.
wait_expected_calls_done(&mut self) -> bool77     pub async fn wait_expected_calls_done(&mut self) -> bool {
78         while !self.expected_calls.lock().unwrap().is_empty() {
79             if timeout(Duration::from_secs(1), self.expect_call_consumed.notified()).await.is_err()
80             {
81                 return false;
82             }
83         }
84         true
85     }
86 
87     /// Prepare Mock to expect for open_hal.
88     ///
89     /// MockUciManager expects call, returns out as response, followed by notfs sent.
expect_open_hal( &mut self, notfs: Vec<UciNotification>, out: Result<GetDeviceInfoResponse>, )90     pub fn expect_open_hal(
91         &mut self,
92         notfs: Vec<UciNotification>,
93         out: Result<GetDeviceInfoResponse>,
94     ) {
95         self.expected_calls.lock().unwrap().push_back(ExpectedCall::OpenHal { notfs, out });
96     }
97 
98     /// Prepare Mock to expect for close_call.
99     ///
100     /// MockUciManager expects call with parameters, returns out as response.
expect_close_hal(&mut self, expected_force: bool, out: Result<()>)101     pub fn expect_close_hal(&mut self, expected_force: bool, out: Result<()>) {
102         self.expected_calls
103             .lock()
104             .unwrap()
105             .push_back(ExpectedCall::CloseHal { expected_force, out });
106     }
107 
108     /// Prepare Mock to expect device_reset.
109     ///
110     /// MockUciManager expects call with parameters, returns out as response.
expect_device_reset(&mut self, expected_reset_config: ResetConfig, out: Result<()>)111     pub fn expect_device_reset(&mut self, expected_reset_config: ResetConfig, out: Result<()>) {
112         self.expected_calls
113             .lock()
114             .unwrap()
115             .push_back(ExpectedCall::DeviceReset { expected_reset_config, out });
116     }
117 
118     /// Prepare Mock to expect core_get_device_info.
119     ///
120     /// MockUciManager expects call, returns out as response.
expect_core_get_device_info(&mut self, out: Result<GetDeviceInfoResponse>)121     pub fn expect_core_get_device_info(&mut self, out: Result<GetDeviceInfoResponse>) {
122         self.expected_calls.lock().unwrap().push_back(ExpectedCall::CoreGetDeviceInfo { out });
123     }
124 
125     /// Prepare Mock to expect core_get_caps_info.
126     ///
127     /// MockUciManager expects call, returns out as response.
expect_core_get_caps_info(&mut self, out: Result<Vec<CapTlv>>)128     pub fn expect_core_get_caps_info(&mut self, out: Result<Vec<CapTlv>>) {
129         self.expected_calls.lock().unwrap().push_back(ExpectedCall::CoreGetCapsInfo { out });
130     }
131 
132     /// Prepare Mock to expect core_set_config.
133     ///
134     /// MockUciManager expects call with parameters, returns out as response.
expect_core_set_config( &mut self, expected_config_tlvs: Vec<DeviceConfigTlv>, out: Result<CoreSetConfigResponse>, )135     pub fn expect_core_set_config(
136         &mut self,
137         expected_config_tlvs: Vec<DeviceConfigTlv>,
138         out: Result<CoreSetConfigResponse>,
139     ) {
140         self.expected_calls
141             .lock()
142             .unwrap()
143             .push_back(ExpectedCall::CoreSetConfig { expected_config_tlvs, out });
144     }
145 
146     /// Prepare Mock to expect core_get_config.
147     ///
148     /// MockUciManager expects call with parameters, returns out as response.
expect_core_get_config( &mut self, expected_config_ids: Vec<DeviceConfigId>, out: Result<Vec<DeviceConfigTlv>>, )149     pub fn expect_core_get_config(
150         &mut self,
151         expected_config_ids: Vec<DeviceConfigId>,
152         out: Result<Vec<DeviceConfigTlv>>,
153     ) {
154         self.expected_calls
155             .lock()
156             .unwrap()
157             .push_back(ExpectedCall::CoreGetConfig { expected_config_ids, out });
158     }
159 
160     /// Prepare Mock to expect core_query_uwb_timestamp.
161     ///
162     /// MockUciManager expects call with parameters, returns out as response.
expect_core_query_uwb_timestamp(&mut self, out: Result<u64>)163     pub fn expect_core_query_uwb_timestamp(&mut self, out: Result<u64>) {
164         self.expected_calls.lock().unwrap().push_back(ExpectedCall::CoreQueryTimeStamp { out });
165     }
166 
167     /// Prepare Mock to expect session_init.
168     ///
169     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
170     /// sent.
expect_session_init( &mut self, expected_session_id: SessionId, expected_session_type: SessionType, notfs: Vec<UciNotification>, out: Result<()>, )171     pub fn expect_session_init(
172         &mut self,
173         expected_session_id: SessionId,
174         expected_session_type: SessionType,
175         notfs: Vec<UciNotification>,
176         out: Result<()>,
177     ) {
178         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionInit {
179             expected_session_id,
180             expected_session_type,
181             notfs,
182             out,
183         });
184     }
185 
186     /// Prepare Mock to expect session_deinit.
187     ///
188     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
189     /// sent.
expect_session_deinit( &mut self, expected_session_id: SessionId, notfs: Vec<UciNotification>, out: Result<()>, )190     pub fn expect_session_deinit(
191         &mut self,
192         expected_session_id: SessionId,
193         notfs: Vec<UciNotification>,
194         out: Result<()>,
195     ) {
196         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionDeinit {
197             expected_session_id,
198             notfs,
199             out,
200         });
201     }
202 
203     /// Prepare Mock to expect session_set_app_config.
204     ///
205     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
206     /// sent.
expect_session_set_app_config( &mut self, expected_session_id: SessionId, expected_config_tlvs: Vec<AppConfigTlv>, notfs: Vec<UciNotification>, out: Result<SetAppConfigResponse>, )207     pub fn expect_session_set_app_config(
208         &mut self,
209         expected_session_id: SessionId,
210         expected_config_tlvs: Vec<AppConfigTlv>,
211         notfs: Vec<UciNotification>,
212         out: Result<SetAppConfigResponse>,
213     ) {
214         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionSetAppConfig {
215             expected_session_id,
216             expected_config_tlvs,
217             notfs,
218             out,
219         });
220     }
221 
222     /// Prepare Mock to expect session_get_app_config.
223     ///
224     /// MockUciManager expects call with parameters, returns out as response.
expect_session_get_app_config( &mut self, expected_session_id: SessionId, expected_config_ids: Vec<AppConfigTlvType>, out: Result<Vec<AppConfigTlv>>, )225     pub fn expect_session_get_app_config(
226         &mut self,
227         expected_session_id: SessionId,
228         expected_config_ids: Vec<AppConfigTlvType>,
229         out: Result<Vec<AppConfigTlv>>,
230     ) {
231         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionGetAppConfig {
232             expected_session_id,
233             expected_config_ids,
234             out,
235         });
236     }
237 
238     /// Prepare Mock to expect session_get_count.
239     ///
240     /// MockUciManager expects call with parameters, returns out as response.
expect_session_get_count(&mut self, out: Result<u8>)241     pub fn expect_session_get_count(&mut self, out: Result<u8>) {
242         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionGetCount { out });
243     }
244 
245     /// Prepare Mock to expect session_get_state.
246     ///
247     /// MockUciManager expects call with parameters, returns out as response.
expect_session_get_state( &mut self, expected_session_id: SessionId, out: Result<SessionState>, )248     pub fn expect_session_get_state(
249         &mut self,
250         expected_session_id: SessionId,
251         out: Result<SessionState>,
252     ) {
253         self.expected_calls
254             .lock()
255             .unwrap()
256             .push_back(ExpectedCall::SessionGetState { expected_session_id, out });
257     }
258 
259     /// Prepare Mock to expect update_controller_multicast_list.
260     ///
261     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
262     /// sent.
expect_session_update_controller_multicast_list( &mut self, expected_session_id: SessionId, expected_action: UpdateMulticastListAction, expected_controlees: Controlees, notfs: Vec<UciNotification>, out: Result<SessionUpdateControllerMulticastResponse>, )263     pub fn expect_session_update_controller_multicast_list(
264         &mut self,
265         expected_session_id: SessionId,
266         expected_action: UpdateMulticastListAction,
267         expected_controlees: Controlees,
268         notfs: Vec<UciNotification>,
269         out: Result<SessionUpdateControllerMulticastResponse>,
270     ) {
271         self.expected_calls.lock().unwrap().push_back(
272             ExpectedCall::SessionUpdateControllerMulticastList {
273                 expected_session_id,
274                 expected_action,
275                 expected_controlees,
276                 notfs,
277                 out,
278             },
279         );
280     }
281 
282     /// Prepare Mock to expect session_update_active_rounds_dt_tag.
283     ///
284     /// MockUciManager expects call with parameters, returns out as response.
expect_session_update_dt_tag_ranging_rounds( &mut self, expected_session_id: u32, expected_ranging_round_indexes: Vec<u8>, out: Result<SessionUpdateDtTagRangingRoundsResponse>, )285     pub fn expect_session_update_dt_tag_ranging_rounds(
286         &mut self,
287         expected_session_id: u32,
288         expected_ranging_round_indexes: Vec<u8>,
289         out: Result<SessionUpdateDtTagRangingRoundsResponse>,
290     ) {
291         self.expected_calls.lock().unwrap().push_back(
292             ExpectedCall::SessionUpdateDtTagRangingRounds {
293                 expected_session_id,
294                 expected_ranging_round_indexes,
295                 out,
296             },
297         );
298     }
299 
300     /// Prepare Mock to expect for session_query_max_data_size.
301     ///
302     /// MockUciManager expects call, returns out as response.
expect_session_query_max_data_size( &mut self, expected_session_id: SessionId, out: Result<u16>, )303     pub fn expect_session_query_max_data_size(
304         &mut self,
305         expected_session_id: SessionId,
306         out: Result<u16>,
307     ) {
308         self.expected_calls
309             .lock()
310             .unwrap()
311             .push_back(ExpectedCall::SessionQueryMaxDataSize { expected_session_id, out });
312     }
313 
314     /// Prepare Mock to expect range_start.
315     ///
316     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
317     /// sent.
expect_range_start( &mut self, expected_session_id: SessionId, notfs: Vec<UciNotification>, out: Result<()>, )318     pub fn expect_range_start(
319         &mut self,
320         expected_session_id: SessionId,
321         notfs: Vec<UciNotification>,
322         out: Result<()>,
323     ) {
324         self.expected_calls.lock().unwrap().push_back(ExpectedCall::RangeStart {
325             expected_session_id,
326             notfs,
327             out,
328         });
329     }
330 
331     /// Prepare Mock to expect range_stop.
332     ///
333     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
334     /// sent.
expect_range_stop( &mut self, expected_session_id: SessionId, notfs: Vec<UciNotification>, out: Result<()>, )335     pub fn expect_range_stop(
336         &mut self,
337         expected_session_id: SessionId,
338         notfs: Vec<UciNotification>,
339         out: Result<()>,
340     ) {
341         self.expected_calls.lock().unwrap().push_back(ExpectedCall::RangeStop {
342             expected_session_id,
343             notfs,
344             out,
345         });
346     }
347 
348     /// Prepare Mock to expect range_get_ranging_count.
349     ///
350     /// MockUciManager expects call with parameters, returns out as response.
expect_range_get_ranging_count( &mut self, expected_session_id: SessionId, out: Result<usize>, )351     pub fn expect_range_get_ranging_count(
352         &mut self,
353         expected_session_id: SessionId,
354         out: Result<usize>,
355     ) {
356         self.expected_calls
357             .lock()
358             .unwrap()
359             .push_back(ExpectedCall::RangeGetRangingCount { expected_session_id, out });
360     }
361 
362     /// Prepare Mock to expect android_set_country_code.
363     ///
364     /// MockUciManager expects call with parameters, returns out as response.
expect_android_set_country_code( &mut self, expected_country_code: CountryCode, out: Result<()>, )365     pub fn expect_android_set_country_code(
366         &mut self,
367         expected_country_code: CountryCode,
368         out: Result<()>,
369     ) {
370         self.expected_calls
371             .lock()
372             .unwrap()
373             .push_back(ExpectedCall::AndroidSetCountryCode { expected_country_code, out });
374     }
375 
376     /// Prepare Mock to expect android_set_country_code.
377     ///
378     /// MockUciManager expects call with parameters, returns out as response.
expect_android_get_power_stats(&mut self, out: Result<PowerStats>)379     pub fn expect_android_get_power_stats(&mut self, out: Result<PowerStats>) {
380         self.expected_calls.lock().unwrap().push_back(ExpectedCall::AndroidGetPowerStats { out });
381     }
382 
383     /// Prepare Mock to expect android_set_radar_config.
384     ///
385     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
386     /// sent.
expect_android_set_radar_config( &mut self, expected_session_id: SessionId, expected_config_tlvs: Vec<RadarConfigTlv>, notfs: Vec<UciNotification>, out: Result<AndroidRadarConfigResponse>, )387     pub fn expect_android_set_radar_config(
388         &mut self,
389         expected_session_id: SessionId,
390         expected_config_tlvs: Vec<RadarConfigTlv>,
391         notfs: Vec<UciNotification>,
392         out: Result<AndroidRadarConfigResponse>,
393     ) {
394         self.expected_calls.lock().unwrap().push_back(ExpectedCall::AndroidSetRadarConfig {
395             expected_session_id,
396             expected_config_tlvs,
397             notfs,
398             out,
399         });
400     }
401 
402     /// Prepare Mock to expect android_get_app_config.
403     ///
404     /// MockUciManager expects call with parameters, returns out as response.
expect_android_get_radar_config( &mut self, expected_session_id: SessionId, expected_config_ids: Vec<RadarConfigTlvType>, out: Result<Vec<RadarConfigTlv>>, )405     pub fn expect_android_get_radar_config(
406         &mut self,
407         expected_session_id: SessionId,
408         expected_config_ids: Vec<RadarConfigTlvType>,
409         out: Result<Vec<RadarConfigTlv>>,
410     ) {
411         self.expected_calls.lock().unwrap().push_back(ExpectedCall::AndroidGetRadarConfig {
412             expected_session_id,
413             expected_config_ids,
414             out,
415         });
416     }
417 
418     /// Prepare Mock to expect raw_uci_cmd.
419     ///
420     /// MockUciManager expects call with parameters, returns out as response.
expect_raw_uci_cmd( &mut self, expected_mt: u32, expected_gid: u32, expected_oid: u32, expected_payload: Vec<u8>, out: Result<RawUciMessage>, )421     pub fn expect_raw_uci_cmd(
422         &mut self,
423         expected_mt: u32,
424         expected_gid: u32,
425         expected_oid: u32,
426         expected_payload: Vec<u8>,
427         out: Result<RawUciMessage>,
428     ) {
429         self.expected_calls.lock().unwrap().push_back(ExpectedCall::RawUciCmd {
430             expected_mt,
431             expected_gid,
432             expected_oid,
433             expected_payload,
434             out,
435         });
436     }
437 
438     /// Prepare Mock to expect send_data_packet.
439     ///
440     /// MockUciManager expects call with parameters, returns out as response.
expect_send_data_packet( &mut self, expected_session_id: SessionId, expected_address: Vec<u8>, expected_uci_sequence_num: u16, expected_app_payload_data: Vec<u8>, out: Result<()>, )441     pub fn expect_send_data_packet(
442         &mut self,
443         expected_session_id: SessionId,
444         expected_address: Vec<u8>,
445         expected_uci_sequence_num: u16,
446         expected_app_payload_data: Vec<u8>,
447         out: Result<()>,
448     ) {
449         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SendDataPacket {
450             expected_session_id,
451             expected_address,
452             expected_uci_sequence_num,
453             expected_app_payload_data,
454             out,
455         });
456     }
457 
458     /// Prepare Mock to expect session_set_hybrid_controller_config.
459     ///
460     /// MockUciManager expects call with parameters, returns out as response
expect_session_set_hybrid_controller_config( &mut self, expected_session_id: SessionId, expected_number_of_phases: u8, expected_phase_list: Vec<ControllerPhaseList>, out: Result<()>, )461     pub fn expect_session_set_hybrid_controller_config(
462         &mut self,
463         expected_session_id: SessionId,
464         expected_number_of_phases: u8,
465         expected_phase_list: Vec<ControllerPhaseList>,
466         out: Result<()>,
467     ) {
468         self.expected_calls.lock().unwrap().push_back(
469             ExpectedCall::SessionSetHybridControllerConfig {
470                 expected_session_id,
471                 expected_number_of_phases,
472                 expected_phase_list,
473                 out,
474             },
475         );
476     }
477 
478     /// Prepare Mock to expect session_set_hybrid_controlee_config.
479     ///
480     /// MockUciManager expects call with parameters, returns out as response
expect_session_set_hybrid_controlee_config( &mut self, expected_session_id: SessionId, expected_controlee_phase_list: Vec<ControleePhaseList>, out: Result<()>, )481     pub fn expect_session_set_hybrid_controlee_config(
482         &mut self,
483         expected_session_id: SessionId,
484         expected_controlee_phase_list: Vec<ControleePhaseList>,
485         out: Result<()>,
486     ) {
487         self.expected_calls.lock().unwrap().push_back(
488             ExpectedCall::SessionSetHybridControleeConfig {
489                 expected_session_id,
490                 expected_controlee_phase_list,
491                 out,
492             },
493         );
494     }
495 
496     /// Prepare Mock to expect session_data_transfer_phase_config
497     /// MockUciManager expects call with parameters, returns out as response
498     #[allow(clippy::too_many_arguments)]
expect_session_data_transfer_phase_config( &mut self, expected_session_id: SessionId, expected_dtpcm_repetition: u8, expected_data_transfer_control: u8, expected_dtpml_size: u8, expected_mac_address: Vec<u8>, expected_slot_bitmap: Vec<u8>, expected_stop_data_transfer: Vec<u8>, out: Result<()>, )499     pub fn expect_session_data_transfer_phase_config(
500         &mut self,
501         expected_session_id: SessionId,
502         expected_dtpcm_repetition: u8,
503         expected_data_transfer_control: u8,
504         expected_dtpml_size: u8,
505         expected_mac_address: Vec<u8>,
506         expected_slot_bitmap: Vec<u8>,
507         expected_stop_data_transfer: Vec<u8>,
508         out: Result<()>,
509     ) {
510         self.expected_calls.lock().unwrap().push_back(
511             ExpectedCall::SessionDataTransferPhaseConfig {
512                 expected_session_id,
513                 expected_dtpcm_repetition,
514                 expected_data_transfer_control,
515                 expected_dtpml_size,
516                 expected_mac_address,
517                 expected_slot_bitmap,
518                 expected_stop_data_transfer,
519                 out,
520             },
521         );
522     }
523 
524     /// Prepare Mock to expect session_set_rf_test_config.
525     ///
526     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
527     /// sent.
expect_session_set_rf_test_config( &mut self, expected_session_id: SessionId, expected_config_tlvs: Vec<RfTestConfigTlv>, notfs: Vec<UciNotification>, out: Result<RfTestConfigResponse>, )528     pub fn expect_session_set_rf_test_config(
529         &mut self,
530         expected_session_id: SessionId,
531         expected_config_tlvs: Vec<RfTestConfigTlv>,
532         notfs: Vec<UciNotification>,
533         out: Result<RfTestConfigResponse>,
534     ) {
535         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionSetRfTestConfig {
536             expected_session_id,
537             expected_config_tlvs,
538             notfs,
539             out,
540         });
541     }
542 
543     /// Prepare Mock to expect rf_test_periodic_tx.
544     ///
545     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
546     /// sent.
expect_test_periodic_tx( &mut self, expected_psdu_data: Vec<u8>, notfs: Vec<UciNotification>, out: Result<()>, )547     pub fn expect_test_periodic_tx(
548         &mut self,
549         expected_psdu_data: Vec<u8>,
550         notfs: Vec<UciNotification>,
551         out: Result<()>,
552     ) {
553         self.expected_calls.lock().unwrap().push_back(ExpectedCall::TestPeriodicTx {
554             expected_psdu_data,
555             notfs,
556             out,
557         });
558     }
559 
560     /// Prepare Mock to expect rf_test_per_rx.
561     ///
562     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
563     /// sent.
expect_test_per_rx( &mut self, expected_psdu_data: Vec<u8>, notfs: Vec<UciNotification>, out: Result<()>, )564     pub fn expect_test_per_rx(
565         &mut self,
566         expected_psdu_data: Vec<u8>,
567         notfs: Vec<UciNotification>,
568         out: Result<()>,
569     ) {
570         self.expected_calls.lock().unwrap().push_back(ExpectedCall::TestPerRx {
571             expected_psdu_data,
572             notfs,
573             out,
574         });
575     }
576 
577     /// Prepare Mock to expect StopRfTest.
578     ///
579     /// MockUciManager expects call with parameters, returns out as response
expect_stop_rf_test(&mut self, out: Result<()>)580     pub fn expect_stop_rf_test(&mut self, out: Result<()>) {
581         self.expected_calls.lock().unwrap().push_back(ExpectedCall::StopRfTest { out });
582     }
583 
584     /// Call Mock to send notifications.
send_notifications(&self, notfs: Vec<UciNotification>)585     fn send_notifications(&self, notfs: Vec<UciNotification>) {
586         for notf in notfs.into_iter() {
587             match notf {
588                 UciNotification::Core(notf) => {
589                     let _ = self.core_notf_sender.send(notf);
590                 }
591                 UciNotification::Session(notf) => {
592                     let _ = self.session_notf_sender.send(notf);
593                 }
594                 UciNotification::Vendor(notf) => {
595                     let _ = self.vendor_notf_sender.send(notf);
596                 }
597                 UciNotification::RfTest(notf) => {
598                     let _ = self.rf_test_notf_sender.send(notf);
599                 }
600             }
601         }
602     }
603 }
604 
605 impl Default for MockUciManager {
default() -> Self606     fn default() -> Self {
607         Self::new()
608     }
609 }
610 
611 #[async_trait]
612 impl UciManager for MockUciManager {
set_logger_mode(&self, _logger_mode: UciLoggerMode) -> Result<()>613     async fn set_logger_mode(&self, _logger_mode: UciLoggerMode) -> Result<()> {
614         Ok(())
615     }
set_core_notification_sender( &mut self, core_notf_sender: mpsc::UnboundedSender<CoreNotification>, )616     async fn set_core_notification_sender(
617         &mut self,
618         core_notf_sender: mpsc::UnboundedSender<CoreNotification>,
619     ) {
620         self.core_notf_sender = core_notf_sender;
621     }
set_session_notification_sender( &mut self, session_notf_sender: mpsc::UnboundedSender<SessionNotification>, )622     async fn set_session_notification_sender(
623         &mut self,
624         session_notf_sender: mpsc::UnboundedSender<SessionNotification>,
625     ) {
626         self.session_notf_sender = session_notf_sender;
627     }
set_vendor_notification_sender( &mut self, vendor_notf_sender: mpsc::UnboundedSender<RawUciMessage>, )628     async fn set_vendor_notification_sender(
629         &mut self,
630         vendor_notf_sender: mpsc::UnboundedSender<RawUciMessage>,
631     ) {
632         self.vendor_notf_sender = vendor_notf_sender;
633     }
set_data_rcv_notification_sender( &mut self, data_rcv_notf_sender: mpsc::UnboundedSender<DataRcvNotification>, )634     async fn set_data_rcv_notification_sender(
635         &mut self,
636         data_rcv_notf_sender: mpsc::UnboundedSender<DataRcvNotification>,
637     ) {
638         self.data_rcv_notf_sender = data_rcv_notf_sender;
639     }
set_radar_data_rcv_notification_sender( &mut self, radar_data_rcv_notf_sender: mpsc::UnboundedSender<RadarDataRcvNotification>, )640     async fn set_radar_data_rcv_notification_sender(
641         &mut self,
642         radar_data_rcv_notf_sender: mpsc::UnboundedSender<RadarDataRcvNotification>,
643     ) {
644         self.radar_data_rcv_notf_sender = radar_data_rcv_notf_sender;
645     }
646 
set_rf_test_notification_sender( &mut self, rf_test_notf_sender: mpsc::UnboundedSender<RfTestNotification>, )647     async fn set_rf_test_notification_sender(
648         &mut self,
649         rf_test_notf_sender: mpsc::UnboundedSender<RfTestNotification>,
650     ) {
651         self.rf_test_notf_sender = rf_test_notf_sender;
652     }
653 
open_hal(&self) -> Result<GetDeviceInfoResponse>654     async fn open_hal(&self) -> Result<GetDeviceInfoResponse> {
655         let mut expected_calls = self.expected_calls.lock().unwrap();
656         match expected_calls.pop_front() {
657             Some(ExpectedCall::OpenHal { notfs, out }) => {
658                 self.expect_call_consumed.notify_one();
659                 self.send_notifications(notfs);
660                 out
661             }
662             Some(call) => {
663                 expected_calls.push_front(call);
664                 Err(Error::MockUndefined)
665             }
666             None => Err(Error::MockUndefined),
667         }
668     }
669 
close_hal(&self, force: bool) -> Result<()>670     async fn close_hal(&self, force: bool) -> Result<()> {
671         let mut expected_calls = self.expected_calls.lock().unwrap();
672         match expected_calls.pop_front() {
673             Some(ExpectedCall::CloseHal { expected_force, out }) if expected_force == force => {
674                 self.expect_call_consumed.notify_one();
675                 out
676             }
677             Some(call) => {
678                 expected_calls.push_front(call);
679                 Err(Error::MockUndefined)
680             }
681             None => Err(Error::MockUndefined),
682         }
683     }
684 
device_reset(&self, reset_config: ResetConfig) -> Result<()>685     async fn device_reset(&self, reset_config: ResetConfig) -> Result<()> {
686         let mut expected_calls = self.expected_calls.lock().unwrap();
687         match expected_calls.pop_front() {
688             Some(ExpectedCall::DeviceReset { expected_reset_config, out })
689                 if expected_reset_config == reset_config =>
690             {
691                 self.expect_call_consumed.notify_one();
692                 out
693             }
694             Some(call) => {
695                 expected_calls.push_front(call);
696                 Err(Error::MockUndefined)
697             }
698             None => Err(Error::MockUndefined),
699         }
700     }
701 
core_get_device_info(&self) -> Result<GetDeviceInfoResponse>702     async fn core_get_device_info(&self) -> Result<GetDeviceInfoResponse> {
703         let mut expected_calls = self.expected_calls.lock().unwrap();
704         match expected_calls.pop_front() {
705             Some(ExpectedCall::CoreGetDeviceInfo { out }) => {
706                 self.expect_call_consumed.notify_one();
707                 out
708             }
709             Some(call) => {
710                 expected_calls.push_front(call);
711                 Err(Error::MockUndefined)
712             }
713             None => Err(Error::MockUndefined),
714         }
715     }
716 
core_get_caps_info(&self) -> Result<Vec<CapTlv>>717     async fn core_get_caps_info(&self) -> Result<Vec<CapTlv>> {
718         let mut expected_calls = self.expected_calls.lock().unwrap();
719         match expected_calls.pop_front() {
720             Some(ExpectedCall::CoreGetCapsInfo { out }) => {
721                 self.expect_call_consumed.notify_one();
722                 out
723             }
724             Some(call) => {
725                 expected_calls.push_front(call);
726                 Err(Error::MockUndefined)
727             }
728             None => Err(Error::MockUndefined),
729         }
730     }
731 
core_set_config( &self, config_tlvs: Vec<DeviceConfigTlv>, ) -> Result<CoreSetConfigResponse>732     async fn core_set_config(
733         &self,
734         config_tlvs: Vec<DeviceConfigTlv>,
735     ) -> Result<CoreSetConfigResponse> {
736         let mut expected_calls = self.expected_calls.lock().unwrap();
737         match expected_calls.pop_front() {
738             Some(ExpectedCall::CoreSetConfig { expected_config_tlvs, out })
739                 if device_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) =>
740             {
741                 self.expect_call_consumed.notify_one();
742                 out
743             }
744             Some(call) => {
745                 expected_calls.push_front(call);
746                 Err(Error::MockUndefined)
747             }
748             None => Err(Error::MockUndefined),
749         }
750     }
751 
core_get_config( &self, config_ids: Vec<DeviceConfigId>, ) -> Result<Vec<DeviceConfigTlv>>752     async fn core_get_config(
753         &self,
754         config_ids: Vec<DeviceConfigId>,
755     ) -> Result<Vec<DeviceConfigTlv>> {
756         let mut expected_calls = self.expected_calls.lock().unwrap();
757         match expected_calls.pop_front() {
758             Some(ExpectedCall::CoreGetConfig { expected_config_ids, out })
759                 if expected_config_ids == config_ids =>
760             {
761                 self.expect_call_consumed.notify_one();
762                 out
763             }
764             Some(call) => {
765                 expected_calls.push_front(call);
766                 Err(Error::MockUndefined)
767             }
768             None => Err(Error::MockUndefined),
769         }
770     }
771 
core_query_uwb_timestamp(&self) -> Result<u64>772     async fn core_query_uwb_timestamp(&self) -> Result<u64> {
773         let mut expected_calls = self.expected_calls.lock().unwrap();
774         match expected_calls.pop_front() {
775             Some(ExpectedCall::CoreQueryTimeStamp { out }) => {
776                 self.expect_call_consumed.notify_one();
777                 out
778             }
779             Some(call) => {
780                 expected_calls.push_front(call);
781                 Err(Error::MockUndefined)
782             }
783             None => Err(Error::MockUndefined),
784         }
785     }
786 
session_init(&self, session_id: SessionId, session_type: SessionType) -> Result<()>787     async fn session_init(&self, session_id: SessionId, session_type: SessionType) -> Result<()> {
788         let mut expected_calls = self.expected_calls.lock().unwrap();
789         match expected_calls.pop_front() {
790             Some(ExpectedCall::SessionInit {
791                 expected_session_id,
792                 expected_session_type,
793                 notfs,
794                 out,
795             }) if expected_session_id == session_id && expected_session_type == session_type => {
796                 self.expect_call_consumed.notify_one();
797                 self.send_notifications(notfs);
798                 out
799             }
800             Some(call) => {
801                 expected_calls.push_front(call);
802                 Err(Error::MockUndefined)
803             }
804             None => Err(Error::MockUndefined),
805         }
806     }
807 
session_deinit(&self, session_id: SessionId) -> Result<()>808     async fn session_deinit(&self, session_id: SessionId) -> Result<()> {
809         let mut expected_calls = self.expected_calls.lock().unwrap();
810         match expected_calls.pop_front() {
811             Some(ExpectedCall::SessionDeinit { expected_session_id, notfs, out })
812                 if expected_session_id == session_id =>
813             {
814                 self.expect_call_consumed.notify_one();
815                 self.send_notifications(notfs);
816                 out
817             }
818             Some(call) => {
819                 expected_calls.push_front(call);
820                 Err(Error::MockUndefined)
821             }
822             None => Err(Error::MockUndefined),
823         }
824     }
825 
session_set_app_config( &self, session_id: SessionId, config_tlvs: Vec<AppConfigTlv>, ) -> Result<SetAppConfigResponse>826     async fn session_set_app_config(
827         &self,
828         session_id: SessionId,
829         config_tlvs: Vec<AppConfigTlv>,
830     ) -> Result<SetAppConfigResponse> {
831         let mut expected_calls = self.expected_calls.lock().unwrap();
832         match expected_calls.pop_front() {
833             Some(ExpectedCall::SessionSetAppConfig {
834                 expected_session_id,
835                 expected_config_tlvs,
836                 notfs,
837                 out,
838             }) if expected_session_id == session_id
839                 && app_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) =>
840             {
841                 self.expect_call_consumed.notify_one();
842                 self.send_notifications(notfs);
843                 out
844             }
845             Some(call) => {
846                 expected_calls.push_front(call);
847                 Err(Error::MockUndefined)
848             }
849             None => Err(Error::MockUndefined),
850         }
851     }
852 
session_get_app_config( &self, session_id: SessionId, config_ids: Vec<AppConfigTlvType>, ) -> Result<Vec<AppConfigTlv>>853     async fn session_get_app_config(
854         &self,
855         session_id: SessionId,
856         config_ids: Vec<AppConfigTlvType>,
857     ) -> Result<Vec<AppConfigTlv>> {
858         let mut expected_calls = self.expected_calls.lock().unwrap();
859         match expected_calls.pop_front() {
860             Some(ExpectedCall::SessionGetAppConfig {
861                 expected_session_id,
862                 expected_config_ids,
863                 out,
864             }) if expected_session_id == session_id && expected_config_ids == config_ids => {
865                 self.expect_call_consumed.notify_one();
866                 out
867             }
868             Some(call) => {
869                 expected_calls.push_front(call);
870                 Err(Error::MockUndefined)
871             }
872             None => Err(Error::MockUndefined),
873         }
874     }
875 
session_get_count(&self) -> Result<u8>876     async fn session_get_count(&self) -> Result<u8> {
877         let mut expected_calls = self.expected_calls.lock().unwrap();
878         match expected_calls.pop_front() {
879             Some(ExpectedCall::SessionGetCount { out }) => {
880                 self.expect_call_consumed.notify_one();
881                 out
882             }
883             Some(call) => {
884                 expected_calls.push_front(call);
885                 Err(Error::MockUndefined)
886             }
887             None => Err(Error::MockUndefined),
888         }
889     }
890 
session_get_state(&self, session_id: SessionId) -> Result<SessionState>891     async fn session_get_state(&self, session_id: SessionId) -> Result<SessionState> {
892         let mut expected_calls = self.expected_calls.lock().unwrap();
893         match expected_calls.pop_front() {
894             Some(ExpectedCall::SessionGetState { expected_session_id, out })
895                 if expected_session_id == session_id =>
896             {
897                 self.expect_call_consumed.notify_one();
898                 out
899             }
900             Some(call) => {
901                 expected_calls.push_front(call);
902                 Err(Error::MockUndefined)
903             }
904             None => Err(Error::MockUndefined),
905         }
906     }
907 
session_update_controller_multicast_list( &self, session_id: SessionId, action: UpdateMulticastListAction, controlees: Controlees, _is_multicast_list_ntf_v2_supported: bool, _is_multicast_list_rsp_v2_supported: bool, ) -> Result<SessionUpdateControllerMulticastResponse>908     async fn session_update_controller_multicast_list(
909         &self,
910         session_id: SessionId,
911         action: UpdateMulticastListAction,
912         controlees: Controlees,
913         _is_multicast_list_ntf_v2_supported: bool,
914         _is_multicast_list_rsp_v2_supported: bool,
915     ) -> Result<SessionUpdateControllerMulticastResponse> {
916         let mut expected_calls = self.expected_calls.lock().unwrap();
917         match expected_calls.pop_front() {
918             Some(ExpectedCall::SessionUpdateControllerMulticastList {
919                 expected_session_id,
920                 expected_action,
921                 expected_controlees,
922                 notfs,
923                 out,
924             }) if expected_session_id == session_id
925                 && expected_action == action
926                 && expected_controlees == controlees =>
927             {
928                 self.expect_call_consumed.notify_one();
929                 self.send_notifications(notfs);
930                 out
931             }
932             Some(call) => {
933                 expected_calls.push_front(call);
934                 Err(Error::MockUndefined)
935             }
936             None => Err(Error::MockUndefined),
937         }
938     }
939 
session_data_transfer_phase_config( &self, session_id: SessionId, dtpcm_repetition: u8, data_transfer_control: u8, dtpml_size: u8, mac_address: Vec<u8>, slot_bitmap: Vec<u8>, stop_data_transfer: Vec<u8>, ) -> Result<()>940     async fn session_data_transfer_phase_config(
941         &self,
942         session_id: SessionId,
943         dtpcm_repetition: u8,
944         data_transfer_control: u8,
945         dtpml_size: u8,
946         mac_address: Vec<u8>,
947         slot_bitmap: Vec<u8>,
948         stop_data_transfer: Vec<u8>,
949     ) -> Result<()> {
950         let mut expected_calls = self.expected_calls.lock().unwrap();
951         match expected_calls.pop_front() {
952             Some(ExpectedCall::SessionDataTransferPhaseConfig {
953                 expected_session_id,
954                 expected_dtpcm_repetition,
955                 expected_data_transfer_control,
956                 expected_dtpml_size,
957                 expected_mac_address,
958                 expected_slot_bitmap,
959                 expected_stop_data_transfer,
960                 out,
961             }) if expected_session_id == session_id
962                 && expected_dtpcm_repetition == dtpcm_repetition
963                 && expected_data_transfer_control == data_transfer_control
964                 && expected_dtpml_size == dtpml_size
965                 && expected_mac_address == mac_address
966                 && expected_slot_bitmap == slot_bitmap
967                 && expected_stop_data_transfer == stop_data_transfer =>
968             {
969                 self.expect_call_consumed.notify_one();
970                 out
971             }
972             Some(call) => {
973                 expected_calls.push_front(call);
974                 Err(Error::MockUndefined)
975             }
976             None => Err(Error::MockUndefined),
977         }
978     }
979 
session_update_dt_tag_ranging_rounds( &self, session_id: u32, ranging_round_indexes: Vec<u8>, ) -> Result<SessionUpdateDtTagRangingRoundsResponse>980     async fn session_update_dt_tag_ranging_rounds(
981         &self,
982         session_id: u32,
983         ranging_round_indexes: Vec<u8>,
984     ) -> Result<SessionUpdateDtTagRangingRoundsResponse> {
985         let mut expected_calls = self.expected_calls.lock().unwrap();
986         match expected_calls.pop_front() {
987             Some(ExpectedCall::SessionUpdateDtTagRangingRounds {
988                 expected_session_id,
989                 expected_ranging_round_indexes,
990                 out,
991             }) if expected_session_id == session_id
992                 && expected_ranging_round_indexes == ranging_round_indexes =>
993             {
994                 self.expect_call_consumed.notify_one();
995                 out
996             }
997             Some(call) => {
998                 expected_calls.push_front(call);
999                 Err(Error::MockUndefined)
1000             }
1001             None => Err(Error::MockUndefined),
1002         }
1003     }
1004 
session_query_max_data_size(&self, session_id: SessionId) -> Result<u16>1005     async fn session_query_max_data_size(&self, session_id: SessionId) -> Result<u16> {
1006         let mut expected_calls = self.expected_calls.lock().unwrap();
1007         match expected_calls.pop_front() {
1008             Some(ExpectedCall::SessionQueryMaxDataSize { expected_session_id, out })
1009                 if expected_session_id == session_id =>
1010             {
1011                 self.expect_call_consumed.notify_one();
1012                 out
1013             }
1014             Some(call) => {
1015                 expected_calls.push_front(call);
1016                 Err(Error::MockUndefined)
1017             }
1018             None => Err(Error::MockUndefined),
1019         }
1020     }
1021 
range_start(&self, session_id: SessionId) -> Result<()>1022     async fn range_start(&self, session_id: SessionId) -> Result<()> {
1023         let mut expected_calls = self.expected_calls.lock().unwrap();
1024         match expected_calls.pop_front() {
1025             Some(ExpectedCall::RangeStart { expected_session_id, notfs, out })
1026                 if expected_session_id == session_id =>
1027             {
1028                 self.expect_call_consumed.notify_one();
1029                 self.send_notifications(notfs);
1030                 out
1031             }
1032             Some(call) => {
1033                 expected_calls.push_front(call);
1034                 Err(Error::MockUndefined)
1035             }
1036             None => Err(Error::MockUndefined),
1037         }
1038     }
1039 
range_stop(&self, session_id: SessionId) -> Result<()>1040     async fn range_stop(&self, session_id: SessionId) -> Result<()> {
1041         let mut expected_calls = self.expected_calls.lock().unwrap();
1042         match expected_calls.pop_front() {
1043             Some(ExpectedCall::RangeStop { expected_session_id, notfs, out })
1044                 if expected_session_id == session_id =>
1045             {
1046                 self.expect_call_consumed.notify_one();
1047                 self.send_notifications(notfs);
1048                 out
1049             }
1050             Some(call) => {
1051                 expected_calls.push_front(call);
1052                 Err(Error::MockUndefined)
1053             }
1054             None => Err(Error::MockUndefined),
1055         }
1056     }
1057 
range_get_ranging_count(&self, session_id: SessionId) -> Result<usize>1058     async fn range_get_ranging_count(&self, session_id: SessionId) -> Result<usize> {
1059         let mut expected_calls = self.expected_calls.lock().unwrap();
1060         match expected_calls.pop_front() {
1061             Some(ExpectedCall::RangeGetRangingCount { expected_session_id, out })
1062                 if expected_session_id == session_id =>
1063             {
1064                 self.expect_call_consumed.notify_one();
1065                 out
1066             }
1067             Some(call) => {
1068                 expected_calls.push_front(call);
1069                 Err(Error::MockUndefined)
1070             }
1071             None => Err(Error::MockUndefined),
1072         }
1073     }
1074 
android_set_country_code(&self, country_code: CountryCode) -> Result<()>1075     async fn android_set_country_code(&self, country_code: CountryCode) -> Result<()> {
1076         let mut expected_calls = self.expected_calls.lock().unwrap();
1077         match expected_calls.pop_front() {
1078             Some(ExpectedCall::AndroidSetCountryCode { expected_country_code, out })
1079                 if expected_country_code == country_code =>
1080             {
1081                 self.expect_call_consumed.notify_one();
1082                 out
1083             }
1084             Some(call) => {
1085                 expected_calls.push_front(call);
1086                 Err(Error::MockUndefined)
1087             }
1088             None => Err(Error::MockUndefined),
1089         }
1090     }
1091 
android_get_power_stats(&self) -> Result<PowerStats>1092     async fn android_get_power_stats(&self) -> Result<PowerStats> {
1093         let mut expected_calls = self.expected_calls.lock().unwrap();
1094         match expected_calls.pop_front() {
1095             Some(ExpectedCall::AndroidGetPowerStats { out }) => {
1096                 self.expect_call_consumed.notify_one();
1097                 out
1098             }
1099             Some(call) => {
1100                 expected_calls.push_front(call);
1101                 Err(Error::MockUndefined)
1102             }
1103             None => Err(Error::MockUndefined),
1104         }
1105     }
1106 
android_set_radar_config( &self, session_id: SessionId, config_tlvs: Vec<RadarConfigTlv>, ) -> Result<AndroidRadarConfigResponse>1107     async fn android_set_radar_config(
1108         &self,
1109         session_id: SessionId,
1110         config_tlvs: Vec<RadarConfigTlv>,
1111     ) -> Result<AndroidRadarConfigResponse> {
1112         let mut expected_calls = self.expected_calls.lock().unwrap();
1113         match expected_calls.pop_front() {
1114             Some(ExpectedCall::AndroidSetRadarConfig {
1115                 expected_session_id,
1116                 expected_config_tlvs,
1117                 notfs,
1118                 out,
1119             }) if expected_session_id == session_id
1120                 && radar_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) =>
1121             {
1122                 self.expect_call_consumed.notify_one();
1123                 self.send_notifications(notfs);
1124                 out
1125             }
1126             Some(call) => {
1127                 expected_calls.push_front(call);
1128                 Err(Error::MockUndefined)
1129             }
1130             None => Err(Error::MockUndefined),
1131         }
1132     }
1133 
android_get_radar_config( &self, session_id: SessionId, config_ids: Vec<RadarConfigTlvType>, ) -> Result<Vec<RadarConfigTlv>>1134     async fn android_get_radar_config(
1135         &self,
1136         session_id: SessionId,
1137         config_ids: Vec<RadarConfigTlvType>,
1138     ) -> Result<Vec<RadarConfigTlv>> {
1139         let mut expected_calls = self.expected_calls.lock().unwrap();
1140         match expected_calls.pop_front() {
1141             Some(ExpectedCall::AndroidGetRadarConfig {
1142                 expected_session_id,
1143                 expected_config_ids,
1144                 out,
1145             }) if expected_session_id == session_id && expected_config_ids == config_ids => {
1146                 self.expect_call_consumed.notify_one();
1147                 out
1148             }
1149             Some(call) => {
1150                 expected_calls.push_front(call);
1151                 Err(Error::MockUndefined)
1152             }
1153             None => Err(Error::MockUndefined),
1154         }
1155     }
1156 
raw_uci_cmd( &self, mt: u32, gid: u32, oid: u32, payload: Vec<u8>, ) -> Result<RawUciMessage>1157     async fn raw_uci_cmd(
1158         &self,
1159         mt: u32,
1160         gid: u32,
1161         oid: u32,
1162         payload: Vec<u8>,
1163     ) -> Result<RawUciMessage> {
1164         let mut expected_calls = self.expected_calls.lock().unwrap();
1165         match expected_calls.pop_front() {
1166             Some(ExpectedCall::RawUciCmd {
1167                 expected_mt,
1168                 expected_gid,
1169                 expected_oid,
1170                 expected_payload,
1171                 out,
1172             }) if expected_mt == mt
1173                 && expected_gid == gid
1174                 && expected_oid == oid
1175                 && expected_payload == payload =>
1176             {
1177                 self.expect_call_consumed.notify_one();
1178                 out
1179             }
1180             Some(call) => {
1181                 expected_calls.push_front(call);
1182                 Err(Error::MockUndefined)
1183             }
1184             None => Err(Error::MockUndefined),
1185         }
1186     }
1187 
send_data_packet( &self, session_id: SessionId, address: Vec<u8>, uci_sequence_num: u16, app_payload_data: Vec<u8>, ) -> Result<()>1188     async fn send_data_packet(
1189         &self,
1190         session_id: SessionId,
1191         address: Vec<u8>,
1192         uci_sequence_num: u16,
1193         app_payload_data: Vec<u8>,
1194     ) -> Result<()> {
1195         let mut expected_calls = self.expected_calls.lock().unwrap();
1196         match expected_calls.pop_front() {
1197             Some(ExpectedCall::SendDataPacket {
1198                 expected_session_id,
1199                 expected_address,
1200                 expected_uci_sequence_num,
1201                 expected_app_payload_data,
1202                 out,
1203             }) if expected_session_id == session_id
1204                 && expected_address == address
1205                 && expected_uci_sequence_num == uci_sequence_num
1206                 && expected_app_payload_data == app_payload_data =>
1207             {
1208                 self.expect_call_consumed.notify_one();
1209                 out
1210             }
1211             Some(call) => {
1212                 expected_calls.push_front(call);
1213                 Err(Error::MockUndefined)
1214             }
1215             None => Err(Error::MockUndefined),
1216         }
1217     }
1218 
get_session_token_from_session_id( &self, _session_id: SessionId, ) -> Result<SessionToken>1219     async fn get_session_token_from_session_id(
1220         &self,
1221         _session_id: SessionId,
1222     ) -> Result<SessionToken> {
1223         Ok(1) // No uci call here, no mock required.
1224     }
1225 
session_set_hybrid_controller_config( &self, session_id: SessionId, number_of_phases: u8, phase_lists: Vec<ControllerPhaseList>, ) -> Result<()>1226     async fn session_set_hybrid_controller_config(
1227         &self,
1228         session_id: SessionId,
1229         number_of_phases: u8,
1230         phase_lists: Vec<ControllerPhaseList>,
1231     ) -> Result<()> {
1232         let mut expected_calls = self.expected_calls.lock().unwrap();
1233         match expected_calls.pop_front() {
1234             Some(ExpectedCall::SessionSetHybridControllerConfig {
1235                 expected_session_id,
1236                 expected_number_of_phases,
1237                 expected_phase_list,
1238                 out,
1239             }) if expected_session_id == session_id
1240                 && expected_number_of_phases == number_of_phases
1241                 && expected_phase_list == phase_lists =>
1242             {
1243                 self.expect_call_consumed.notify_one();
1244                 out
1245             }
1246             Some(call) => {
1247                 expected_calls.push_front(call);
1248                 Err(Error::MockUndefined)
1249             }
1250             None => Err(Error::MockUndefined),
1251         }
1252     }
1253 
session_set_hybrid_controlee_config( &self, session_id: SessionId, controlee_phase_list: Vec<ControleePhaseList>, ) -> Result<()>1254     async fn session_set_hybrid_controlee_config(
1255         &self,
1256         session_id: SessionId,
1257         controlee_phase_list: Vec<ControleePhaseList>,
1258     ) -> Result<()> {
1259         let mut expected_calls = self.expected_calls.lock().unwrap();
1260         match expected_calls.pop_front() {
1261             Some(ExpectedCall::SessionSetHybridControleeConfig {
1262                 expected_session_id,
1263                 expected_controlee_phase_list,
1264                 out,
1265             }) if expected_session_id == session_id
1266                 && expected_controlee_phase_list.len() == controlee_phase_list.len()
1267                 && expected_controlee_phase_list == controlee_phase_list =>
1268             {
1269                 self.expect_call_consumed.notify_one();
1270                 out
1271             }
1272             Some(call) => {
1273                 expected_calls.push_front(call);
1274                 Err(Error::MockUndefined)
1275             }
1276             None => Err(Error::MockUndefined),
1277         }
1278     }
1279 
session_set_rf_test_config( &self, session_id: SessionId, config_tlvs: Vec<RfTestConfigTlv>, ) -> Result<RfTestConfigResponse>1280     async fn session_set_rf_test_config(
1281         &self,
1282         session_id: SessionId,
1283         config_tlvs: Vec<RfTestConfigTlv>,
1284     ) -> Result<RfTestConfigResponse> {
1285         let mut expected_calls = self.expected_calls.lock().unwrap();
1286         match expected_calls.pop_front() {
1287             Some(ExpectedCall::SessionSetRfTestConfig {
1288                 expected_session_id,
1289                 expected_config_tlvs,
1290                 notfs,
1291                 out,
1292             }) if expected_session_id == session_id
1293                 && rf_test_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) =>
1294             {
1295                 self.expect_call_consumed.notify_one();
1296                 self.send_notifications(notfs);
1297                 out
1298             }
1299             Some(call) => {
1300                 expected_calls.push_front(call);
1301                 Err(Error::MockUndefined)
1302             }
1303             None => Err(Error::MockUndefined),
1304         }
1305     }
1306 
rf_test_periodic_tx(&self, psdu_data: Vec<u8>) -> Result<()>1307     async fn rf_test_periodic_tx(&self, psdu_data: Vec<u8>) -> Result<()> {
1308         let mut expected_calls = self.expected_calls.lock().unwrap();
1309         match expected_calls.pop_front() {
1310             Some(ExpectedCall::TestPeriodicTx { expected_psdu_data, notfs, out })
1311                 if expected_psdu_data == psdu_data =>
1312             {
1313                 self.expect_call_consumed.notify_one();
1314                 self.send_notifications(notfs);
1315                 out
1316             }
1317             Some(call) => {
1318                 expected_calls.push_front(call);
1319                 Err(Error::MockUndefined)
1320             }
1321             None => Err(Error::MockUndefined),
1322         }
1323     }
1324 
rf_test_per_rx(&self, psdu_data: Vec<u8>) -> Result<()>1325     async fn rf_test_per_rx(&self, psdu_data: Vec<u8>) -> Result<()> {
1326         let mut expected_calls = self.expected_calls.lock().unwrap();
1327         match expected_calls.pop_front() {
1328             Some(ExpectedCall::TestPerRx { expected_psdu_data, notfs, out })
1329             if expected_psdu_data == psdu_data =>
1330                 {
1331                     self.expect_call_consumed.notify_one();
1332                     self.send_notifications(notfs);
1333                     out
1334                 }
1335             Some(call) => {
1336                 expected_calls.push_front(call);
1337                 Err(Error::MockUndefined)
1338             }
1339             None => Err(Error::MockUndefined),
1340         }
1341     }
1342 
stop_rf_test(&self) -> Result<()>1343     async fn stop_rf_test(&self) -> Result<()> {
1344         let mut expected_calls = self.expected_calls.lock().unwrap();
1345         match expected_calls.pop_front() {
1346             Some(ExpectedCall::StopRfTest { out }) => {
1347                 self.expect_call_consumed.notify_one();
1348                 out
1349             }
1350             Some(call) => {
1351                 expected_calls.push_front(call);
1352                 Err(Error::MockUndefined)
1353             }
1354             None => Err(Error::MockUndefined),
1355         }
1356     }
1357 }
1358 
1359 #[derive(Clone)]
1360 enum ExpectedCall {
1361     OpenHal {
1362         notfs: Vec<UciNotification>,
1363         out: Result<GetDeviceInfoResponse>,
1364     },
1365     CloseHal {
1366         expected_force: bool,
1367         out: Result<()>,
1368     },
1369     DeviceReset {
1370         expected_reset_config: ResetConfig,
1371         out: Result<()>,
1372     },
1373     CoreGetDeviceInfo {
1374         out: Result<GetDeviceInfoResponse>,
1375     },
1376     CoreGetCapsInfo {
1377         out: Result<Vec<CapTlv>>,
1378     },
1379     CoreSetConfig {
1380         expected_config_tlvs: Vec<DeviceConfigTlv>,
1381         out: Result<CoreSetConfigResponse>,
1382     },
1383     CoreGetConfig {
1384         expected_config_ids: Vec<DeviceConfigId>,
1385         out: Result<Vec<DeviceConfigTlv>>,
1386     },
1387     CoreQueryTimeStamp {
1388         out: Result<u64>,
1389     },
1390     SessionInit {
1391         expected_session_id: SessionId,
1392         expected_session_type: SessionType,
1393         notfs: Vec<UciNotification>,
1394         out: Result<()>,
1395     },
1396     SessionDeinit {
1397         expected_session_id: SessionId,
1398         notfs: Vec<UciNotification>,
1399         out: Result<()>,
1400     },
1401     SessionSetAppConfig {
1402         expected_session_id: SessionId,
1403         expected_config_tlvs: Vec<AppConfigTlv>,
1404         notfs: Vec<UciNotification>,
1405         out: Result<SetAppConfigResponse>,
1406     },
1407     SessionGetAppConfig {
1408         expected_session_id: SessionId,
1409         expected_config_ids: Vec<AppConfigTlvType>,
1410         out: Result<Vec<AppConfigTlv>>,
1411     },
1412     SessionGetCount {
1413         out: Result<u8>,
1414     },
1415     SessionGetState {
1416         expected_session_id: SessionId,
1417         out: Result<SessionState>,
1418     },
1419     SessionUpdateControllerMulticastList {
1420         expected_session_id: SessionId,
1421         expected_action: UpdateMulticastListAction,
1422         expected_controlees: Controlees,
1423         notfs: Vec<UciNotification>,
1424         out: Result<SessionUpdateControllerMulticastResponse>,
1425     },
1426     SessionUpdateDtTagRangingRounds {
1427         expected_session_id: u32,
1428         expected_ranging_round_indexes: Vec<u8>,
1429         out: Result<SessionUpdateDtTagRangingRoundsResponse>,
1430     },
1431     SessionQueryMaxDataSize {
1432         expected_session_id: SessionId,
1433         out: Result<u16>,
1434     },
1435     RangeStart {
1436         expected_session_id: SessionId,
1437         notfs: Vec<UciNotification>,
1438         out: Result<()>,
1439     },
1440     RangeStop {
1441         expected_session_id: SessionId,
1442         notfs: Vec<UciNotification>,
1443         out: Result<()>,
1444     },
1445     RangeGetRangingCount {
1446         expected_session_id: SessionId,
1447         out: Result<usize>,
1448     },
1449     AndroidSetCountryCode {
1450         expected_country_code: CountryCode,
1451         out: Result<()>,
1452     },
1453     AndroidGetPowerStats {
1454         out: Result<PowerStats>,
1455     },
1456     AndroidSetRadarConfig {
1457         expected_session_id: SessionId,
1458         expected_config_tlvs: Vec<RadarConfigTlv>,
1459         notfs: Vec<UciNotification>,
1460         out: Result<AndroidRadarConfigResponse>,
1461     },
1462     AndroidGetRadarConfig {
1463         expected_session_id: SessionId,
1464         expected_config_ids: Vec<RadarConfigTlvType>,
1465         out: Result<Vec<RadarConfigTlv>>,
1466     },
1467     RawUciCmd {
1468         expected_mt: u32,
1469         expected_gid: u32,
1470         expected_oid: u32,
1471         expected_payload: Vec<u8>,
1472         out: Result<RawUciMessage>,
1473     },
1474     SendDataPacket {
1475         expected_session_id: SessionId,
1476         expected_address: Vec<u8>,
1477         expected_uci_sequence_num: u16,
1478         expected_app_payload_data: Vec<u8>,
1479         out: Result<()>,
1480     },
1481     SessionSetHybridControllerConfig {
1482         expected_session_id: SessionId,
1483         expected_number_of_phases: u8,
1484         expected_phase_list: Vec<ControllerPhaseList>,
1485         out: Result<()>,
1486     },
1487     SessionSetHybridControleeConfig {
1488         expected_session_id: SessionId,
1489         expected_controlee_phase_list: Vec<ControleePhaseList>,
1490         out: Result<()>,
1491     },
1492     SessionDataTransferPhaseConfig {
1493         expected_session_id: SessionId,
1494         expected_dtpcm_repetition: u8,
1495         expected_data_transfer_control: u8,
1496         expected_dtpml_size: u8,
1497         expected_mac_address: Vec<u8>,
1498         expected_slot_bitmap: Vec<u8>,
1499         expected_stop_data_transfer: Vec<u8>,
1500         out: Result<()>,
1501     },
1502     SessionSetRfTestConfig {
1503         expected_session_id: SessionId,
1504         expected_config_tlvs: Vec<RfTestConfigTlv>,
1505         notfs: Vec<UciNotification>,
1506         out: Result<RfTestConfigResponse>,
1507     },
1508     TestPeriodicTx {
1509         expected_psdu_data: Vec<u8>,
1510         notfs: Vec<UciNotification>,
1511         out: Result<()>,
1512     },
1513     TestPerRx {
1514         expected_psdu_data: Vec<u8>,
1515         notfs: Vec<UciNotification>,
1516         out: Result<()>,
1517     },
1518     StopRfTest {
1519         out: Result<()>,
1520     },
1521 }
1522