• 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, AppConfigTlv, AppConfigTlvType, CapTlv, Controlees,
31     CoreSetConfigResponse, CountryCode, DeviceConfigId, DeviceConfigTlv, FiraComponent,
32     GetDeviceInfoResponse, PowerStats, RawUciMessage, ResetConfig, SessionId, SessionState,
33     SessionToken, SessionType, SessionUpdateDtTagRangingRoundsResponse, SetAppConfigResponse,
34     UpdateMulticastListAction,
35 };
36 use crate::uci::notification::{
37     CoreNotification, DataRcvNotification, SessionNotification, UciNotification,
38 };
39 use crate::uci::uci_logger::UciLoggerMode;
40 use crate::uci::uci_manager::UciManager;
41 
42 #[derive(Clone)]
43 /// Mock version of UciManager for testing.
44 pub struct MockUciManager {
45     expected_calls: Arc<Mutex<VecDeque<ExpectedCall>>>,
46     expect_call_consumed: Arc<Notify>,
47     core_notf_sender: mpsc::UnboundedSender<CoreNotification>,
48     session_notf_sender: mpsc::UnboundedSender<SessionNotification>,
49     vendor_notf_sender: mpsc::UnboundedSender<RawUciMessage>,
50     data_rcv_notf_sender: mpsc::UnboundedSender<DataRcvNotification>,
51 }
52 
53 #[allow(dead_code)]
54 impl MockUciManager {
55     /// Constructor.
new() -> Self56     pub fn new() -> Self {
57         Self {
58             expected_calls: Default::default(),
59             expect_call_consumed: Default::default(),
60             core_notf_sender: mpsc::unbounded_channel().0,
61             session_notf_sender: mpsc::unbounded_channel().0,
62             vendor_notf_sender: mpsc::unbounded_channel().0,
63             data_rcv_notf_sender: mpsc::unbounded_channel().0,
64         }
65     }
66 
67     /// Wait until expected calls are done.
68     ///
69     /// Returns false if calls are pending after 1 second.
wait_expected_calls_done(&mut self) -> bool70     pub async fn wait_expected_calls_done(&mut self) -> bool {
71         while !self.expected_calls.lock().unwrap().is_empty() {
72             if timeout(Duration::from_secs(1), self.expect_call_consumed.notified()).await.is_err()
73             {
74                 return false;
75             }
76         }
77         true
78     }
79 
80     /// Prepare Mock to expect for open_hal.
81     ///
82     /// MockUciManager expects call, returns out as response, followed by notfs sent.
expect_open_hal(&mut self, notfs: Vec<UciNotification>, out: Result<()>)83     pub fn expect_open_hal(&mut self, notfs: Vec<UciNotification>, out: Result<()>) {
84         self.expected_calls.lock().unwrap().push_back(ExpectedCall::OpenHal { notfs, out });
85     }
86 
87     /// Prepare Mock to expect for close_call.
88     ///
89     /// MockUciManager expects call with parameters, returns out as response.
expect_close_hal(&mut self, expected_force: bool, out: Result<()>)90     pub fn expect_close_hal(&mut self, expected_force: bool, out: Result<()>) {
91         self.expected_calls
92             .lock()
93             .unwrap()
94             .push_back(ExpectedCall::CloseHal { expected_force, out });
95     }
96 
97     /// Prepare Mock to expect device_reset.
98     ///
99     /// MockUciManager expects call with parameters, returns out as response.
expect_device_reset(&mut self, expected_reset_config: ResetConfig, out: Result<()>)100     pub fn expect_device_reset(&mut self, expected_reset_config: ResetConfig, out: Result<()>) {
101         self.expected_calls
102             .lock()
103             .unwrap()
104             .push_back(ExpectedCall::DeviceReset { expected_reset_config, out });
105     }
106 
107     /// Prepare Mock to expect core_get_device_info.
108     ///
109     /// MockUciManager expects call, returns out as response.
expect_core_get_device_info(&mut self, out: Result<GetDeviceInfoResponse>)110     pub fn expect_core_get_device_info(&mut self, out: Result<GetDeviceInfoResponse>) {
111         self.expected_calls.lock().unwrap().push_back(ExpectedCall::CoreGetDeviceInfo { out });
112     }
113 
114     /// Prepare Mock to expect core_get_caps_info.
115     ///
116     /// MockUciManager expects call, returns out as response.
expect_core_get_caps_info(&mut self, out: Result<Vec<CapTlv>>)117     pub fn expect_core_get_caps_info(&mut self, out: Result<Vec<CapTlv>>) {
118         self.expected_calls.lock().unwrap().push_back(ExpectedCall::CoreGetCapsInfo { out });
119     }
120 
121     /// Prepare Mock to expect core_set_config.
122     ///
123     /// MockUciManager expects call with parameters, returns out as response.
expect_core_set_config( &mut self, expected_config_tlvs: Vec<DeviceConfigTlv>, out: Result<CoreSetConfigResponse>, )124     pub fn expect_core_set_config(
125         &mut self,
126         expected_config_tlvs: Vec<DeviceConfigTlv>,
127         out: Result<CoreSetConfigResponse>,
128     ) {
129         self.expected_calls
130             .lock()
131             .unwrap()
132             .push_back(ExpectedCall::CoreSetConfig { expected_config_tlvs, out });
133     }
134 
135     /// Prepare Mock to expect core_get_config.
136     ///
137     /// MockUciManager expects call with parameters, returns out as response.
expect_core_get_config( &mut self, expected_config_ids: Vec<DeviceConfigId>, out: Result<Vec<DeviceConfigTlv>>, )138     pub fn expect_core_get_config(
139         &mut self,
140         expected_config_ids: Vec<DeviceConfigId>,
141         out: Result<Vec<DeviceConfigTlv>>,
142     ) {
143         self.expected_calls
144             .lock()
145             .unwrap()
146             .push_back(ExpectedCall::CoreGetConfig { expected_config_ids, out });
147     }
148 
149     /// Prepare Mock to expect session_init.
150     ///
151     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
152     /// sent.
expect_session_init( &mut self, expected_session_id: SessionId, expected_session_type: SessionType, notfs: Vec<UciNotification>, out: Result<()>, )153     pub fn expect_session_init(
154         &mut self,
155         expected_session_id: SessionId,
156         expected_session_type: SessionType,
157         notfs: Vec<UciNotification>,
158         out: Result<()>,
159     ) {
160         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionInit {
161             expected_session_id,
162             expected_session_type,
163             notfs,
164             out,
165         });
166     }
167 
168     /// Prepare Mock to expect session_deinit.
169     ///
170     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
171     /// sent.
expect_session_deinit( &mut self, expected_session_id: SessionId, notfs: Vec<UciNotification>, out: Result<()>, )172     pub fn expect_session_deinit(
173         &mut self,
174         expected_session_id: SessionId,
175         notfs: Vec<UciNotification>,
176         out: Result<()>,
177     ) {
178         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionDeinit {
179             expected_session_id,
180             notfs,
181             out,
182         });
183     }
184 
185     /// Prepare Mock to expect session_set_app_config.
186     ///
187     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
188     /// sent.
expect_session_set_app_config( &mut self, expected_session_id: SessionId, expected_config_tlvs: Vec<AppConfigTlv>, notfs: Vec<UciNotification>, out: Result<SetAppConfigResponse>, )189     pub fn expect_session_set_app_config(
190         &mut self,
191         expected_session_id: SessionId,
192         expected_config_tlvs: Vec<AppConfigTlv>,
193         notfs: Vec<UciNotification>,
194         out: Result<SetAppConfigResponse>,
195     ) {
196         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionSetAppConfig {
197             expected_session_id,
198             expected_config_tlvs,
199             notfs,
200             out,
201         });
202     }
203 
204     /// Prepare Mock to expect session_get_app_config.
205     ///
206     /// 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>>, )207     pub fn expect_session_get_app_config(
208         &mut self,
209         expected_session_id: SessionId,
210         expected_config_ids: Vec<AppConfigTlvType>,
211         out: Result<Vec<AppConfigTlv>>,
212     ) {
213         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionGetAppConfig {
214             expected_session_id,
215             expected_config_ids,
216             out,
217         });
218     }
219 
220     /// Prepare Mock to expect session_get_count.
221     ///
222     /// MockUciManager expects call with parameters, returns out as response.
expect_session_get_count(&mut self, out: Result<u8>)223     pub fn expect_session_get_count(&mut self, out: Result<u8>) {
224         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SessionGetCount { out });
225     }
226 
227     /// Prepare Mock to expect session_get_state.
228     ///
229     /// MockUciManager expects call with parameters, returns out as response.
expect_session_get_state( &mut self, expected_session_id: SessionId, out: Result<SessionState>, )230     pub fn expect_session_get_state(
231         &mut self,
232         expected_session_id: SessionId,
233         out: Result<SessionState>,
234     ) {
235         self.expected_calls
236             .lock()
237             .unwrap()
238             .push_back(ExpectedCall::SessionGetState { expected_session_id, out });
239     }
240 
241     /// Prepare Mock to expect update_controller_multicast_list.
242     ///
243     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
244     /// sent.
expect_session_update_controller_multicast_list( &mut self, expected_session_id: SessionId, expected_action: UpdateMulticastListAction, expected_controlees: Controlees, notfs: Vec<UciNotification>, out: Result<()>, )245     pub fn expect_session_update_controller_multicast_list(
246         &mut self,
247         expected_session_id: SessionId,
248         expected_action: UpdateMulticastListAction,
249         expected_controlees: Controlees,
250         notfs: Vec<UciNotification>,
251         out: Result<()>,
252     ) {
253         self.expected_calls.lock().unwrap().push_back(
254             ExpectedCall::SessionUpdateControllerMulticastList {
255                 expected_session_id,
256                 expected_action,
257                 expected_controlees,
258                 notfs,
259                 out,
260             },
261         );
262     }
263 
264     /// Prepare Mock to expect session_update_active_rounds_dt_tag.
265     ///
266     /// 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>, )267     pub fn expect_session_update_dt_tag_ranging_rounds(
268         &mut self,
269         expected_session_id: u32,
270         expected_ranging_round_indexes: Vec<u8>,
271         out: Result<SessionUpdateDtTagRangingRoundsResponse>,
272     ) {
273         self.expected_calls.lock().unwrap().push_back(
274             ExpectedCall::SessionUpdateDtTagRangingRounds {
275                 expected_session_id,
276                 expected_ranging_round_indexes,
277                 out,
278             },
279         );
280     }
281 
282     /// Prepare Mock to expect for session_query_max_data_size.
283     ///
284     /// MockUciManager expects call, returns out as response.
expect_session_query_max_data_size( &mut self, expected_session_id: SessionId, out: Result<u16>, )285     pub fn expect_session_query_max_data_size(
286         &mut self,
287         expected_session_id: SessionId,
288         out: Result<u16>,
289     ) {
290         self.expected_calls
291             .lock()
292             .unwrap()
293             .push_back(ExpectedCall::SessionQueryMaxDataSize { expected_session_id, out });
294     }
295 
296     /// Prepare Mock to expect range_start.
297     ///
298     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
299     /// sent.
expect_range_start( &mut self, expected_session_id: SessionId, notfs: Vec<UciNotification>, out: Result<()>, )300     pub fn expect_range_start(
301         &mut self,
302         expected_session_id: SessionId,
303         notfs: Vec<UciNotification>,
304         out: Result<()>,
305     ) {
306         self.expected_calls.lock().unwrap().push_back(ExpectedCall::RangeStart {
307             expected_session_id,
308             notfs,
309             out,
310         });
311     }
312 
313     /// Prepare Mock to expect range_stop.
314     ///
315     /// MockUciManager expects call with parameters, returns out as response, followed by notfs
316     /// sent.
expect_range_stop( &mut self, expected_session_id: SessionId, notfs: Vec<UciNotification>, out: Result<()>, )317     pub fn expect_range_stop(
318         &mut self,
319         expected_session_id: SessionId,
320         notfs: Vec<UciNotification>,
321         out: Result<()>,
322     ) {
323         self.expected_calls.lock().unwrap().push_back(ExpectedCall::RangeStop {
324             expected_session_id,
325             notfs,
326             out,
327         });
328     }
329 
330     /// Prepare Mock to expect range_get_ranging_count.
331     ///
332     /// MockUciManager expects call with parameters, returns out as response.
expect_range_get_ranging_count( &mut self, expected_session_id: SessionId, out: Result<usize>, )333     pub fn expect_range_get_ranging_count(
334         &mut self,
335         expected_session_id: SessionId,
336         out: Result<usize>,
337     ) {
338         self.expected_calls
339             .lock()
340             .unwrap()
341             .push_back(ExpectedCall::RangeGetRangingCount { expected_session_id, out });
342     }
343 
344     /// Prepare Mock to expect android_set_country_code.
345     ///
346     /// MockUciManager expects call with parameters, returns out as response.
expect_android_set_country_code( &mut self, expected_country_code: CountryCode, out: Result<()>, )347     pub fn expect_android_set_country_code(
348         &mut self,
349         expected_country_code: CountryCode,
350         out: Result<()>,
351     ) {
352         self.expected_calls
353             .lock()
354             .unwrap()
355             .push_back(ExpectedCall::AndroidSetCountryCode { expected_country_code, out });
356     }
357 
358     /// Prepare Mock to expect android_set_country_code.
359     ///
360     /// MockUciManager expects call with parameters, returns out as response.
expect_android_get_power_stats(&mut self, out: Result<PowerStats>)361     pub fn expect_android_get_power_stats(&mut self, out: Result<PowerStats>) {
362         self.expected_calls.lock().unwrap().push_back(ExpectedCall::AndroidGetPowerStats { out });
363     }
364 
365     /// Prepare Mock to expect raw_uci_cmd.
366     ///
367     /// 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>, )368     pub fn expect_raw_uci_cmd(
369         &mut self,
370         expected_mt: u32,
371         expected_gid: u32,
372         expected_oid: u32,
373         expected_payload: Vec<u8>,
374         out: Result<RawUciMessage>,
375     ) {
376         self.expected_calls.lock().unwrap().push_back(ExpectedCall::RawUciCmd {
377             expected_mt,
378             expected_gid,
379             expected_oid,
380             expected_payload,
381             out,
382         });
383     }
384 
385     /// Prepare Mock to expect send_data_packet.
386     ///
387     /// MockUciManager expects call with parameters, returns out as response.
expect_send_data_packet( &mut self, expected_session_id: SessionId, expected_address: Vec<u8>, expected_dest_end_point: FiraComponent, expected_uci_sequence_num: u8, expected_app_payload_data: Vec<u8>, out: Result<()>, )388     pub fn expect_send_data_packet(
389         &mut self,
390         expected_session_id: SessionId,
391         expected_address: Vec<u8>,
392         expected_dest_end_point: FiraComponent,
393         expected_uci_sequence_num: u8,
394         expected_app_payload_data: Vec<u8>,
395         out: Result<()>,
396     ) {
397         self.expected_calls.lock().unwrap().push_back(ExpectedCall::SendDataPacket {
398             expected_session_id,
399             expected_address,
400             expected_dest_end_point,
401             expected_uci_sequence_num,
402             expected_app_payload_data,
403             out,
404         });
405     }
406 
407     /// Call Mock to send notifications.
send_notifications(&self, notfs: Vec<UciNotification>)408     fn send_notifications(&self, notfs: Vec<UciNotification>) {
409         for notf in notfs.into_iter() {
410             match notf {
411                 UciNotification::Core(notf) => {
412                     let _ = self.core_notf_sender.send(notf);
413                 }
414                 UciNotification::Session(notf) => {
415                     let _ = self.session_notf_sender.send(notf);
416                 }
417                 UciNotification::Vendor(notf) => {
418                     let _ = self.vendor_notf_sender.send(notf);
419                 }
420             }
421         }
422     }
423 }
424 
425 impl Default for MockUciManager {
default() -> Self426     fn default() -> Self {
427         Self::new()
428     }
429 }
430 
431 #[async_trait]
432 impl UciManager for MockUciManager {
set_logger_mode(&self, _logger_mode: UciLoggerMode) -> Result<()>433     async fn set_logger_mode(&self, _logger_mode: UciLoggerMode) -> Result<()> {
434         Ok(())
435     }
set_core_notification_sender( &mut self, core_notf_sender: mpsc::UnboundedSender<CoreNotification>, )436     async fn set_core_notification_sender(
437         &mut self,
438         core_notf_sender: mpsc::UnboundedSender<CoreNotification>,
439     ) {
440         self.core_notf_sender = core_notf_sender;
441     }
set_session_notification_sender( &mut self, session_notf_sender: mpsc::UnboundedSender<SessionNotification>, )442     async fn set_session_notification_sender(
443         &mut self,
444         session_notf_sender: mpsc::UnboundedSender<SessionNotification>,
445     ) {
446         self.session_notf_sender = session_notf_sender;
447     }
set_vendor_notification_sender( &mut self, vendor_notf_sender: mpsc::UnboundedSender<RawUciMessage>, )448     async fn set_vendor_notification_sender(
449         &mut self,
450         vendor_notf_sender: mpsc::UnboundedSender<RawUciMessage>,
451     ) {
452         self.vendor_notf_sender = vendor_notf_sender;
453     }
set_data_rcv_notification_sender( &mut self, data_rcv_notf_sender: mpsc::UnboundedSender<DataRcvNotification>, )454     async fn set_data_rcv_notification_sender(
455         &mut self,
456         data_rcv_notf_sender: mpsc::UnboundedSender<DataRcvNotification>,
457     ) {
458         self.data_rcv_notf_sender = data_rcv_notf_sender;
459     }
460 
open_hal(&self) -> Result<()>461     async fn open_hal(&self) -> Result<()> {
462         let mut expected_calls = self.expected_calls.lock().unwrap();
463         match expected_calls.pop_front() {
464             Some(ExpectedCall::OpenHal { notfs, out }) => {
465                 self.expect_call_consumed.notify_one();
466                 self.send_notifications(notfs);
467                 out
468             }
469             Some(call) => {
470                 expected_calls.push_front(call);
471                 Err(Error::MockUndefined)
472             }
473             None => Err(Error::MockUndefined),
474         }
475     }
476 
close_hal(&self, force: bool) -> Result<()>477     async fn close_hal(&self, force: bool) -> Result<()> {
478         let mut expected_calls = self.expected_calls.lock().unwrap();
479         match expected_calls.pop_front() {
480             Some(ExpectedCall::CloseHal { expected_force, out }) if expected_force == force => {
481                 self.expect_call_consumed.notify_one();
482                 out
483             }
484             Some(call) => {
485                 expected_calls.push_front(call);
486                 Err(Error::MockUndefined)
487             }
488             None => Err(Error::MockUndefined),
489         }
490     }
491 
device_reset(&self, reset_config: ResetConfig) -> Result<()>492     async fn device_reset(&self, reset_config: ResetConfig) -> Result<()> {
493         let mut expected_calls = self.expected_calls.lock().unwrap();
494         match expected_calls.pop_front() {
495             Some(ExpectedCall::DeviceReset { expected_reset_config, out })
496                 if expected_reset_config == reset_config =>
497             {
498                 self.expect_call_consumed.notify_one();
499                 out
500             }
501             Some(call) => {
502                 expected_calls.push_front(call);
503                 Err(Error::MockUndefined)
504             }
505             None => Err(Error::MockUndefined),
506         }
507     }
508 
core_get_device_info(&self) -> Result<GetDeviceInfoResponse>509     async fn core_get_device_info(&self) -> Result<GetDeviceInfoResponse> {
510         let mut expected_calls = self.expected_calls.lock().unwrap();
511         match expected_calls.pop_front() {
512             Some(ExpectedCall::CoreGetDeviceInfo { out }) => {
513                 self.expect_call_consumed.notify_one();
514                 out
515             }
516             Some(call) => {
517                 expected_calls.push_front(call);
518                 Err(Error::MockUndefined)
519             }
520             None => Err(Error::MockUndefined),
521         }
522     }
523 
core_get_caps_info(&self) -> Result<Vec<CapTlv>>524     async fn core_get_caps_info(&self) -> Result<Vec<CapTlv>> {
525         let mut expected_calls = self.expected_calls.lock().unwrap();
526         match expected_calls.pop_front() {
527             Some(ExpectedCall::CoreGetCapsInfo { out }) => {
528                 self.expect_call_consumed.notify_one();
529                 out
530             }
531             Some(call) => {
532                 expected_calls.push_front(call);
533                 Err(Error::MockUndefined)
534             }
535             None => Err(Error::MockUndefined),
536         }
537     }
538 
core_set_config( &self, config_tlvs: Vec<DeviceConfigTlv>, ) -> Result<CoreSetConfigResponse>539     async fn core_set_config(
540         &self,
541         config_tlvs: Vec<DeviceConfigTlv>,
542     ) -> Result<CoreSetConfigResponse> {
543         let mut expected_calls = self.expected_calls.lock().unwrap();
544         match expected_calls.pop_front() {
545             Some(ExpectedCall::CoreSetConfig { expected_config_tlvs, out })
546                 if device_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) =>
547             {
548                 self.expect_call_consumed.notify_one();
549                 out
550             }
551             Some(call) => {
552                 expected_calls.push_front(call);
553                 Err(Error::MockUndefined)
554             }
555             None => Err(Error::MockUndefined),
556         }
557     }
558 
core_get_config( &self, config_ids: Vec<DeviceConfigId>, ) -> Result<Vec<DeviceConfigTlv>>559     async fn core_get_config(
560         &self,
561         config_ids: Vec<DeviceConfigId>,
562     ) -> Result<Vec<DeviceConfigTlv>> {
563         let mut expected_calls = self.expected_calls.lock().unwrap();
564         match expected_calls.pop_front() {
565             Some(ExpectedCall::CoreGetConfig { expected_config_ids, out })
566                 if expected_config_ids == config_ids =>
567             {
568                 self.expect_call_consumed.notify_one();
569                 out
570             }
571             Some(call) => {
572                 expected_calls.push_front(call);
573                 Err(Error::MockUndefined)
574             }
575             None => Err(Error::MockUndefined),
576         }
577     }
578 
session_init(&self, session_id: SessionId, session_type: SessionType) -> Result<()>579     async fn session_init(&self, session_id: SessionId, session_type: SessionType) -> Result<()> {
580         let mut expected_calls = self.expected_calls.lock().unwrap();
581         match expected_calls.pop_front() {
582             Some(ExpectedCall::SessionInit {
583                 expected_session_id,
584                 expected_session_type,
585                 notfs,
586                 out,
587             }) if expected_session_id == session_id && expected_session_type == session_type => {
588                 self.expect_call_consumed.notify_one();
589                 self.send_notifications(notfs);
590                 out
591             }
592             Some(call) => {
593                 expected_calls.push_front(call);
594                 Err(Error::MockUndefined)
595             }
596             None => Err(Error::MockUndefined),
597         }
598     }
599 
session_deinit(&self, session_id: SessionId) -> Result<()>600     async fn session_deinit(&self, session_id: SessionId) -> Result<()> {
601         let mut expected_calls = self.expected_calls.lock().unwrap();
602         match expected_calls.pop_front() {
603             Some(ExpectedCall::SessionDeinit { expected_session_id, notfs, out })
604                 if expected_session_id == session_id =>
605             {
606                 self.expect_call_consumed.notify_one();
607                 self.send_notifications(notfs);
608                 out
609             }
610             Some(call) => {
611                 expected_calls.push_front(call);
612                 Err(Error::MockUndefined)
613             }
614             None => Err(Error::MockUndefined),
615         }
616     }
617 
session_set_app_config( &self, session_id: SessionId, config_tlvs: Vec<AppConfigTlv>, ) -> Result<SetAppConfigResponse>618     async fn session_set_app_config(
619         &self,
620         session_id: SessionId,
621         config_tlvs: Vec<AppConfigTlv>,
622     ) -> Result<SetAppConfigResponse> {
623         let mut expected_calls = self.expected_calls.lock().unwrap();
624         match expected_calls.pop_front() {
625             Some(ExpectedCall::SessionSetAppConfig {
626                 expected_session_id,
627                 expected_config_tlvs,
628                 notfs,
629                 out,
630             }) if expected_session_id == session_id
631                 && app_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) =>
632             {
633                 self.expect_call_consumed.notify_one();
634                 self.send_notifications(notfs);
635                 out
636             }
637             Some(call) => {
638                 expected_calls.push_front(call);
639                 Err(Error::MockUndefined)
640             }
641             None => Err(Error::MockUndefined),
642         }
643     }
644 
session_get_app_config( &self, session_id: SessionId, config_ids: Vec<AppConfigTlvType>, ) -> Result<Vec<AppConfigTlv>>645     async fn session_get_app_config(
646         &self,
647         session_id: SessionId,
648         config_ids: Vec<AppConfigTlvType>,
649     ) -> Result<Vec<AppConfigTlv>> {
650         let mut expected_calls = self.expected_calls.lock().unwrap();
651         match expected_calls.pop_front() {
652             Some(ExpectedCall::SessionGetAppConfig {
653                 expected_session_id,
654                 expected_config_ids,
655                 out,
656             }) if expected_session_id == session_id && expected_config_ids == config_ids => {
657                 self.expect_call_consumed.notify_one();
658                 out
659             }
660             Some(call) => {
661                 expected_calls.push_front(call);
662                 Err(Error::MockUndefined)
663             }
664             None => Err(Error::MockUndefined),
665         }
666     }
667 
session_get_count(&self) -> Result<u8>668     async fn session_get_count(&self) -> Result<u8> {
669         let mut expected_calls = self.expected_calls.lock().unwrap();
670         match expected_calls.pop_front() {
671             Some(ExpectedCall::SessionGetCount { out }) => {
672                 self.expect_call_consumed.notify_one();
673                 out
674             }
675             Some(call) => {
676                 expected_calls.push_front(call);
677                 Err(Error::MockUndefined)
678             }
679             None => Err(Error::MockUndefined),
680         }
681     }
682 
session_get_state(&self, session_id: SessionId) -> Result<SessionState>683     async fn session_get_state(&self, session_id: SessionId) -> Result<SessionState> {
684         let mut expected_calls = self.expected_calls.lock().unwrap();
685         match expected_calls.pop_front() {
686             Some(ExpectedCall::SessionGetState { expected_session_id, out })
687                 if expected_session_id == session_id =>
688             {
689                 self.expect_call_consumed.notify_one();
690                 out
691             }
692             Some(call) => {
693                 expected_calls.push_front(call);
694                 Err(Error::MockUndefined)
695             }
696             None => Err(Error::MockUndefined),
697         }
698     }
699 
session_update_controller_multicast_list( &self, session_id: SessionId, action: UpdateMulticastListAction, controlees: Controlees, ) -> Result<()>700     async fn session_update_controller_multicast_list(
701         &self,
702         session_id: SessionId,
703         action: UpdateMulticastListAction,
704         controlees: Controlees,
705     ) -> Result<()> {
706         let mut expected_calls = self.expected_calls.lock().unwrap();
707         match expected_calls.pop_front() {
708             Some(ExpectedCall::SessionUpdateControllerMulticastList {
709                 expected_session_id,
710                 expected_action,
711                 expected_controlees,
712                 notfs,
713                 out,
714             }) if expected_session_id == session_id
715                 && expected_action == action
716                 && expected_controlees == controlees =>
717             {
718                 self.expect_call_consumed.notify_one();
719                 self.send_notifications(notfs);
720                 out
721             }
722             Some(call) => {
723                 expected_calls.push_front(call);
724                 Err(Error::MockUndefined)
725             }
726             None => Err(Error::MockUndefined),
727         }
728     }
729 
session_update_dt_tag_ranging_rounds( &self, session_id: u32, ranging_round_indexes: Vec<u8>, ) -> Result<SessionUpdateDtTagRangingRoundsResponse>730     async fn session_update_dt_tag_ranging_rounds(
731         &self,
732         session_id: u32,
733         ranging_round_indexes: Vec<u8>,
734     ) -> Result<SessionUpdateDtTagRangingRoundsResponse> {
735         let mut expected_calls = self.expected_calls.lock().unwrap();
736         match expected_calls.pop_front() {
737             Some(ExpectedCall::SessionUpdateDtTagRangingRounds {
738                 expected_session_id,
739                 expected_ranging_round_indexes,
740                 out,
741             }) if expected_session_id == session_id
742                 && expected_ranging_round_indexes == ranging_round_indexes =>
743             {
744                 self.expect_call_consumed.notify_one();
745                 out
746             }
747             Some(call) => {
748                 expected_calls.push_front(call);
749                 Err(Error::MockUndefined)
750             }
751             None => Err(Error::MockUndefined),
752         }
753     }
754 
session_query_max_data_size(&self, session_id: SessionId) -> Result<u16>755     async fn session_query_max_data_size(&self, session_id: SessionId) -> Result<u16> {
756         let mut expected_calls = self.expected_calls.lock().unwrap();
757         match expected_calls.pop_front() {
758             Some(ExpectedCall::SessionQueryMaxDataSize { expected_session_id, out })
759                 if expected_session_id == session_id =>
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 
range_start(&self, session_id: SessionId) -> Result<()>772     async fn range_start(&self, session_id: SessionId) -> Result<()> {
773         let mut expected_calls = self.expected_calls.lock().unwrap();
774         match expected_calls.pop_front() {
775             Some(ExpectedCall::RangeStart { expected_session_id, notfs, out })
776                 if expected_session_id == session_id =>
777             {
778                 self.expect_call_consumed.notify_one();
779                 self.send_notifications(notfs);
780                 out
781             }
782             Some(call) => {
783                 expected_calls.push_front(call);
784                 Err(Error::MockUndefined)
785             }
786             None => Err(Error::MockUndefined),
787         }
788     }
789 
range_stop(&self, session_id: SessionId) -> Result<()>790     async fn range_stop(&self, session_id: SessionId) -> Result<()> {
791         let mut expected_calls = self.expected_calls.lock().unwrap();
792         match expected_calls.pop_front() {
793             Some(ExpectedCall::RangeStop { expected_session_id, notfs, out })
794                 if expected_session_id == session_id =>
795             {
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 
range_get_ranging_count(&self, session_id: SessionId) -> Result<usize>808     async fn range_get_ranging_count(&self, session_id: SessionId) -> Result<usize> {
809         let mut expected_calls = self.expected_calls.lock().unwrap();
810         match expected_calls.pop_front() {
811             Some(ExpectedCall::RangeGetRangingCount { expected_session_id, out })
812                 if expected_session_id == session_id =>
813             {
814                 self.expect_call_consumed.notify_one();
815                 out
816             }
817             Some(call) => {
818                 expected_calls.push_front(call);
819                 Err(Error::MockUndefined)
820             }
821             None => Err(Error::MockUndefined),
822         }
823     }
824 
android_set_country_code(&self, country_code: CountryCode) -> Result<()>825     async fn android_set_country_code(&self, country_code: CountryCode) -> Result<()> {
826         let mut expected_calls = self.expected_calls.lock().unwrap();
827         match expected_calls.pop_front() {
828             Some(ExpectedCall::AndroidSetCountryCode { expected_country_code, out })
829                 if expected_country_code == country_code =>
830             {
831                 self.expect_call_consumed.notify_one();
832                 out
833             }
834             Some(call) => {
835                 expected_calls.push_front(call);
836                 Err(Error::MockUndefined)
837             }
838             None => Err(Error::MockUndefined),
839         }
840     }
841 
android_get_power_stats(&self) -> Result<PowerStats>842     async fn android_get_power_stats(&self) -> Result<PowerStats> {
843         let mut expected_calls = self.expected_calls.lock().unwrap();
844         match expected_calls.pop_front() {
845             Some(ExpectedCall::AndroidGetPowerStats { out }) => {
846                 self.expect_call_consumed.notify_one();
847                 out
848             }
849             Some(call) => {
850                 expected_calls.push_front(call);
851                 Err(Error::MockUndefined)
852             }
853             None => Err(Error::MockUndefined),
854         }
855     }
856 
raw_uci_cmd( &self, mt: u32, gid: u32, oid: u32, payload: Vec<u8>, ) -> Result<RawUciMessage>857     async fn raw_uci_cmd(
858         &self,
859         mt: u32,
860         gid: u32,
861         oid: u32,
862         payload: Vec<u8>,
863     ) -> Result<RawUciMessage> {
864         let mut expected_calls = self.expected_calls.lock().unwrap();
865         match expected_calls.pop_front() {
866             Some(ExpectedCall::RawUciCmd {
867                 expected_mt,
868                 expected_gid,
869                 expected_oid,
870                 expected_payload,
871                 out,
872             }) if expected_mt == mt
873                 && expected_gid == gid
874                 && expected_oid == oid
875                 && expected_payload == payload =>
876             {
877                 self.expect_call_consumed.notify_one();
878                 out
879             }
880             Some(call) => {
881                 expected_calls.push_front(call);
882                 Err(Error::MockUndefined)
883             }
884             None => Err(Error::MockUndefined),
885         }
886     }
887 
send_data_packet( &self, session_id: SessionId, address: Vec<u8>, dest_end_point: FiraComponent, uci_sequence_num: u8, app_payload_data: Vec<u8>, ) -> Result<()>888     async fn send_data_packet(
889         &self,
890         session_id: SessionId,
891         address: Vec<u8>,
892         dest_end_point: FiraComponent,
893         uci_sequence_num: u8,
894         app_payload_data: Vec<u8>,
895     ) -> Result<()> {
896         let mut expected_calls = self.expected_calls.lock().unwrap();
897         match expected_calls.pop_front() {
898             Some(ExpectedCall::SendDataPacket {
899                 expected_session_id,
900                 expected_address,
901                 expected_dest_end_point,
902                 expected_uci_sequence_num,
903                 expected_app_payload_data,
904                 out,
905             }) if expected_session_id == session_id
906                 && expected_address == address
907                 && expected_dest_end_point == dest_end_point
908                 && expected_uci_sequence_num == uci_sequence_num
909                 && expected_app_payload_data == app_payload_data =>
910             {
911                 self.expect_call_consumed.notify_one();
912                 out
913             }
914             Some(call) => {
915                 expected_calls.push_front(call);
916                 Err(Error::MockUndefined)
917             }
918             None => Err(Error::MockUndefined),
919         }
920     }
921 
get_session_token_from_session_id( &self, _session_id: SessionId, ) -> Result<SessionToken>922     async fn get_session_token_from_session_id(
923         &self,
924         _session_id: SessionId,
925     ) -> Result<SessionToken> {
926         Ok(1) // No uci call here, no mock required.
927     }
928 }
929 
930 #[derive(Clone)]
931 enum ExpectedCall {
932     OpenHal {
933         notfs: Vec<UciNotification>,
934         out: Result<()>,
935     },
936     CloseHal {
937         expected_force: bool,
938         out: Result<()>,
939     },
940     DeviceReset {
941         expected_reset_config: ResetConfig,
942         out: Result<()>,
943     },
944     CoreGetDeviceInfo {
945         out: Result<GetDeviceInfoResponse>,
946     },
947     CoreGetCapsInfo {
948         out: Result<Vec<CapTlv>>,
949     },
950     CoreSetConfig {
951         expected_config_tlvs: Vec<DeviceConfigTlv>,
952         out: Result<CoreSetConfigResponse>,
953     },
954     CoreGetConfig {
955         expected_config_ids: Vec<DeviceConfigId>,
956         out: Result<Vec<DeviceConfigTlv>>,
957     },
958     SessionInit {
959         expected_session_id: SessionId,
960         expected_session_type: SessionType,
961         notfs: Vec<UciNotification>,
962         out: Result<()>,
963     },
964     SessionDeinit {
965         expected_session_id: SessionId,
966         notfs: Vec<UciNotification>,
967         out: Result<()>,
968     },
969     SessionSetAppConfig {
970         expected_session_id: SessionId,
971         expected_config_tlvs: Vec<AppConfigTlv>,
972         notfs: Vec<UciNotification>,
973         out: Result<SetAppConfigResponse>,
974     },
975     SessionGetAppConfig {
976         expected_session_id: SessionId,
977         expected_config_ids: Vec<AppConfigTlvType>,
978         out: Result<Vec<AppConfigTlv>>,
979     },
980     SessionGetCount {
981         out: Result<u8>,
982     },
983     SessionGetState {
984         expected_session_id: SessionId,
985         out: Result<SessionState>,
986     },
987     SessionUpdateControllerMulticastList {
988         expected_session_id: SessionId,
989         expected_action: UpdateMulticastListAction,
990         expected_controlees: Controlees,
991         notfs: Vec<UciNotification>,
992         out: Result<()>,
993     },
994     SessionUpdateDtTagRangingRounds {
995         expected_session_id: u32,
996         expected_ranging_round_indexes: Vec<u8>,
997         out: Result<SessionUpdateDtTagRangingRoundsResponse>,
998     },
999     SessionQueryMaxDataSize {
1000         expected_session_id: SessionId,
1001         out: Result<u16>,
1002     },
1003     RangeStart {
1004         expected_session_id: SessionId,
1005         notfs: Vec<UciNotification>,
1006         out: Result<()>,
1007     },
1008     RangeStop {
1009         expected_session_id: SessionId,
1010         notfs: Vec<UciNotification>,
1011         out: Result<()>,
1012     },
1013     RangeGetRangingCount {
1014         expected_session_id: SessionId,
1015         out: Result<usize>,
1016     },
1017     AndroidSetCountryCode {
1018         expected_country_code: CountryCode,
1019         out: Result<()>,
1020     },
1021     AndroidGetPowerStats {
1022         out: Result<PowerStats>,
1023     },
1024     RawUciCmd {
1025         expected_mt: u32,
1026         expected_gid: u32,
1027         expected_oid: u32,
1028         expected_payload: Vec<u8>,
1029         out: Result<RawUciMessage>,
1030     },
1031     SendDataPacket {
1032         expected_session_id: SessionId,
1033         expected_address: Vec<u8>,
1034         expected_dest_end_point: FiraComponent,
1035         expected_uci_sequence_num: u8,
1036         expected_app_payload_data: Vec<u8>,
1037         out: Result<()>,
1038     },
1039 }
1040