From 745dd51df02918791834b48f602a9cb76b9ab1a0 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Sun, 11 Feb 2018 15:52:24 -0700 Subject: [PATCH] Add Soc service --- ctru-rs/Cargo.toml | 3 +++ ctru-rs/src/lib.rs | 1 + ctru-rs/src/services/mod.rs | 1 + ctru-rs/src/services/soc.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 ctru-rs/src/services/soc.rs diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index f93902c..1484b64 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -13,6 +13,9 @@ name = "ctru" path = "../ctru-sys" version = "0.4" +[dependencies.libc] +version = "0.2" + [dependencies.bitflags] version = "1.0.0" diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 10132a0..57635f6 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -3,6 +3,7 @@ #[macro_use] extern crate bitflags; +extern crate libc; extern crate widestring; extern crate ctru_sys as libctru; diff --git a/ctru-rs/src/services/mod.rs b/ctru-rs/src/services/mod.rs index f8c99d2..8a179b4 100644 --- a/ctru-rs/src/services/mod.rs +++ b/ctru-rs/src/services/mod.rs @@ -2,6 +2,7 @@ pub mod apt; pub mod fs; pub mod hid; pub mod gspgpu; +pub mod soc; pub mod sslc; pub use self::hid::Hid; diff --git a/ctru-rs/src/services/soc.rs b/ctru-rs/src/services/soc.rs new file mode 100644 index 0000000..4d4666a --- /dev/null +++ b/ctru-rs/src/services/soc.rs @@ -0,0 +1,33 @@ +use libctru::{socInit, socExit}; + +use libc::{memalign, free}; + +pub struct Soc { + soc_mem: *mut u32, +} + +impl Soc { + pub fn init() -> ::Result { + const SOC_MEM_SIZE: usize = 0x100000; + + unsafe { + let soc_mem = memalign(0x1000, SOC_MEM_SIZE) as *mut u32; + + let r = socInit(soc_mem, SOC_MEM_SIZE as u32); + if r < 0 { + Err(r.into()) + } else { + Ok(Soc { soc_mem, }) + } + } + } +} + +impl Drop for Soc { + fn drop(&mut self) { + unsafe { + socExit(); + free(self.soc_mem as *mut _); + } + } +}