(ds: &internal::PassiveRoamingDeviceSession)
| 14 | use lrwn::{AES128Key, DevAddr, EUI64}; |
| 15 | |
| 16 | pub async fn save(ds: &internal::PassiveRoamingDeviceSession) -> Result<()> { |
| 17 | let sess_id = Uuid::from_slice(&ds.session_id)?; |
| 18 | let dev_addr = DevAddr::from_slice(&ds.dev_addr)?; |
| 19 | let dev_eui = if ds.dev_eui.is_empty() { |
| 20 | EUI64::default() |
| 21 | } else { |
| 22 | EUI64::from_slice(&ds.dev_eui)? |
| 23 | }; |
| 24 | |
| 25 | let lifetime: DateTime<Utc> = match ds.lifetime { |
| 26 | Some(v) => v.try_into().map_err(anyhow::Error::msg)?, |
| 27 | None => { |
| 28 | debug!("Not saving passive-roaming device-session, no passive-roaming lifetime set"); |
| 29 | return Ok(()); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | let lifetime = lifetime - Utc::now(); |
| 34 | if lifetime <= Duration::zero() { |
| 35 | debug!( |
| 36 | "Not saving passive-roaming device-session, lifetime of passive-roaming session expired" |
| 37 | ); |
| 38 | return Ok(()); |
| 39 | } |
| 40 | |
| 41 | let conf = config::get(); |
| 42 | |
| 43 | let dev_addr_key = redis_key(format!("pr:devaddr:{{{}}}", dev_addr)); |
| 44 | let dev_eui_key = redis_key(format!("pr:dev:{{{}}}", dev_eui)); |
| 45 | let sess_key = redis_key(format!("pr:sess:{{{}}}", sess_id)); |
| 46 | let b = ds.encode_to_vec(); |
| 47 | let ttl = conf.network.device_session_ttl.as_millis() as usize; |
| 48 | let pr_ttl = lifetime.num_milliseconds() as usize; |
| 49 | |
| 50 | // We need to store a pointer from both the DevAddr and DevEUI to the |
| 51 | // passive-roaming device-session ID. This is needed: |
| 52 | // * Because the DevAddr is not guaranteed to be unique |
| 53 | // * Because the DevEUI might not be given (thus is also not guaranteed |
| 54 | // to be an unique identifier). |
| 55 | // |
| 56 | // But: |
| 57 | // * We need to be able to lookup the session using the DevAddr (potentially |
| 58 | // using the MIC validation). |
| 59 | // * We need to be able to stop a passive-roaming session given a DevEUI. |
| 60 | () = redis::pipe() |
| 61 | .atomic() |
| 62 | .cmd("SADD") |
| 63 | .arg(&dev_addr_key) |
| 64 | .arg(sess_id.to_string()) |
| 65 | .ignore() |
| 66 | .cmd("SADD") |
| 67 | .arg(&dev_eui_key) |
| 68 | .arg(sess_id.to_string()) |
| 69 | .ignore() |
| 70 | .cmd("PEXPIRE") |
| 71 | .arg(&dev_addr_key) |
| 72 | .arg(ttl) |
| 73 | .ignore() |
nothing calls this directly
no test coverage detected