Andrea Ciliberti
3 years ago
26 changed files with 320 additions and 118 deletions
@ -1,35 +1,58 @@
@@ -1,35 +1,58 @@
|
||||
// TODO: Implement remaining functions
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering}; |
||||
|
||||
use crate::error::Error; |
||||
|
||||
#[non_exhaustive] |
||||
pub struct SslC(()); |
||||
|
||||
static SSLC_ACTIVE: AtomicBool = AtomicBool::new(false); |
||||
|
||||
impl SslC { |
||||
/// Initialize sslc
|
||||
pub fn init() -> crate::Result<Self> { |
||||
unsafe { |
||||
let r = ctru_sys::sslcInit(0); |
||||
match SSLC_ACTIVE.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { |
||||
Ok(_) => { |
||||
let r = unsafe { ctru_sys::sslcInit(0) }; |
||||
if r < 0 { |
||||
Err(r.into()) |
||||
} else { |
||||
Ok(SslC(())) |
||||
Ok(Self(())) |
||||
} |
||||
} |
||||
Err(_) => Err(Error::ServiceAlreadyActive("SslC")), |
||||
} |
||||
} |
||||
|
||||
/// Fill `buf` with `buf.len()` random bytes
|
||||
pub fn generate_random_data(&self, buf: &mut [u8]) -> crate::Result<()> { |
||||
unsafe { |
||||
let r = ctru_sys::sslcGenerateRandomData(buf.as_ptr() as _, buf.len() as u32); |
||||
let r = unsafe { ctru_sys::sslcGenerateRandomData(buf.as_ptr() as _, buf.len() as u32) }; |
||||
if r < 0 { |
||||
Err(r.into()) |
||||
} else { |
||||
Ok(()) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl Drop for SslC { |
||||
fn drop(&mut self) { |
||||
unsafe { ctru_sys::sslcExit() }; |
||||
|
||||
SSLC_ACTIVE.store(false, Ordering::Release); |
||||
} |
||||
} |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn sslc_duplicate() { |
||||
let _sslc = SslC::init().unwrap(); |
||||
|
||||
assert!(SslC::init().is_err()); |
||||
} |
||||
} |
||||
|
||||
|
@ -1,20 +1,44 @@
@@ -1,20 +1,44 @@
|
||||
use std::sync::atomic::{AtomicBool, Ordering}; |
||||
|
||||
use crate::error::Error; |
||||
|
||||
#[non_exhaustive] |
||||
pub struct Srv(()); |
||||
|
||||
static SRV_ACTIVE: AtomicBool = AtomicBool::new(false); |
||||
|
||||
impl Srv { |
||||
pub fn init() -> crate::Result<Srv> { |
||||
unsafe { |
||||
let r = ctru_sys::srvInit(); |
||||
pub fn init() -> crate::Result<Self> { |
||||
match SRV_ACTIVE.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { |
||||
Ok(_) => { |
||||
let r = unsafe { ctru_sys::srvInit() }; |
||||
if r < 0 { |
||||
Err(r.into()) |
||||
} else { |
||||
Ok(Srv(())) |
||||
Ok(Self(())) |
||||
} |
||||
} |
||||
Err(_) => Err(Error::ServiceAlreadyActive("Srv")), |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl Drop for Srv { |
||||
fn drop(&mut self) { |
||||
unsafe { ctru_sys::srvExit() }; |
||||
|
||||
SRV_ACTIVE.store(false, Ordering::Release); |
||||
} |
||||
} |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn srv_duplicate() { |
||||
let _srv = Srv::init().unwrap(); |
||||
|
||||
assert!(Srv::init().is_err()); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue