Andrea Ciliberti
3 years ago
26 changed files with 320 additions and 118 deletions
@ -1,35 +1,58 @@ |
|||||||
// TODO: Implement remaining functions
|
// TODO: Implement remaining functions
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering}; |
||||||
|
|
||||||
|
use crate::error::Error; |
||||||
|
|
||||||
|
#[non_exhaustive] |
||||||
pub struct SslC(()); |
pub struct SslC(()); |
||||||
|
|
||||||
|
static SSLC_ACTIVE: AtomicBool = AtomicBool::new(false); |
||||||
|
|
||||||
impl SslC { |
impl SslC { |
||||||
/// Initialize sslc
|
/// Initialize sslc
|
||||||
pub fn init() -> crate::Result<Self> { |
pub fn init() -> crate::Result<Self> { |
||||||
unsafe { |
match SSLC_ACTIVE.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { |
||||||
let r = ctru_sys::sslcInit(0); |
Ok(_) => { |
||||||
|
let r = unsafe { ctru_sys::sslcInit(0) }; |
||||||
if r < 0 { |
if r < 0 { |
||||||
Err(r.into()) |
Err(r.into()) |
||||||
} else { |
} else { |
||||||
Ok(SslC(())) |
Ok(Self(())) |
||||||
} |
} |
||||||
} |
} |
||||||
|
Err(_) => Err(Error::ServiceAlreadyActive("SslC")), |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
/// Fill `buf` with `buf.len()` random bytes
|
/// Fill `buf` with `buf.len()` random bytes
|
||||||
pub fn generate_random_data(&self, buf: &mut [u8]) -> crate::Result<()> { |
pub fn generate_random_data(&self, buf: &mut [u8]) -> crate::Result<()> { |
||||||
unsafe { |
let r = unsafe { ctru_sys::sslcGenerateRandomData(buf.as_ptr() as _, buf.len() as u32) }; |
||||||
let r = ctru_sys::sslcGenerateRandomData(buf.as_ptr() as _, buf.len() as u32); |
|
||||||
if r < 0 { |
if r < 0 { |
||||||
Err(r.into()) |
Err(r.into()) |
||||||
} else { |
} else { |
||||||
Ok(()) |
Ok(()) |
||||||
} |
} |
||||||
} |
} |
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
impl Drop for SslC { |
impl Drop for SslC { |
||||||
fn drop(&mut self) { |
fn drop(&mut self) { |
||||||
unsafe { ctru_sys::sslcExit() }; |
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 @@ |
|||||||
|
use std::sync::atomic::{AtomicBool, Ordering}; |
||||||
|
|
||||||
|
use crate::error::Error; |
||||||
|
|
||||||
|
#[non_exhaustive] |
||||||
pub struct Srv(()); |
pub struct Srv(()); |
||||||
|
|
||||||
|
static SRV_ACTIVE: AtomicBool = AtomicBool::new(false); |
||||||
|
|
||||||
impl Srv { |
impl Srv { |
||||||
pub fn init() -> crate::Result<Srv> { |
pub fn init() -> crate::Result<Self> { |
||||||
unsafe { |
match SRV_ACTIVE.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { |
||||||
let r = ctru_sys::srvInit(); |
Ok(_) => { |
||||||
|
let r = unsafe { ctru_sys::srvInit() }; |
||||||
if r < 0 { |
if r < 0 { |
||||||
Err(r.into()) |
Err(r.into()) |
||||||
} else { |
} else { |
||||||
Ok(Srv(())) |
Ok(Self(())) |
||||||
} |
} |
||||||
} |
} |
||||||
|
Err(_) => Err(Error::ServiceAlreadyActive("Srv")), |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
impl Drop for Srv { |
impl Drop for Srv { |
||||||
fn drop(&mut self) { |
fn drop(&mut self) { |
||||||
unsafe { ctru_sys::srvExit() }; |
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