nrf_modem/
uicc_link.rs

1//! Implementation of [UiccLink]
2use crate::Error;
3
4/// An object that keeps the UICC (Universal Integrated Circuit Card).
5/// As long as there is an instance, the UICC will be kept on.
6/// The drop function disables the UICC if there is no link left.
7///
8/// You can use this object to power on the UICC and interact with the SIM card,
9/// without keeping other parts of the modem powered on.
10/// If you already use LTE, you do not need to explicitly power on the UICC.
11///
12/// You do not need to create a UICC link for any of the structs in the crate to work.
13/// However, you may need this if you are manually executing AT commands to interact with the SIM card.
14#[derive(Debug, PartialEq, Eq)]
15#[cfg_attr(feature = "defmt", derive(defmt::Format))]
16pub struct UiccLink(());
17
18impl UiccLink {
19    /// Create a new instance
20    pub async fn new() -> Result<Self, Error> {
21        if unsafe { !nrfxlib_sys::nrf_modem_is_initialized() } {
22            return Err(Error::ModemNotInitialized);
23        }
24
25        crate::MODEM_RUNTIME_STATE.activate_uicc().await?;
26
27        Ok(Self(()))
28    }
29
30    /// Deactivates the UICC if it is no longer in use.
31    ///
32    /// This does the same as dropping the instance, but in an async manner.
33    pub async fn deactivate(self) -> Result<(), Error> {
34        core::mem::forget(self);
35        let result = crate::MODEM_RUNTIME_STATE.deactivate_uicc().await;
36
37        if result.is_err() {
38            crate::MODEM_RUNTIME_STATE.set_error_active();
39        }
40
41        result
42    }
43}
44
45impl Drop for UiccLink {
46    fn drop(&mut self) {
47        #[cfg(feature = "defmt")]
48        defmt::warn!(
49            "Turning off UICC synchronously. Use async function `deactivate` to avoid blocking and to get more guarantees that the modem is actually shut off."
50        );
51
52        if let Err(_e) = crate::MODEM_RUNTIME_STATE.deactivate_uicc_blocking() {
53            #[cfg(feature = "defmt")]
54            defmt::error!("Could not turn off the UICC: {}", _e);
55            crate::MODEM_RUNTIME_STATE.set_error_active();
56        }
57    }
58}