1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
extern crate magenta;
extern crate magenta_sys;
extern crate mxruntime_sys;
use magenta::{Handle, HandleBase, Channel, ChannelOpts, Status};
use magenta_sys::{mx_handle_t, MX_OK};
use mxruntime_sys::{mxio_service_connect, mxio_service_connect_at};
use std::ffi::CString;
pub const MX_HANDLE_INVALID: mx_handle_t = 0;
#[repr(u32)]
pub enum HandleType {
Resource = mxruntime_sys::PA_RESOURCE,
ApplicationLauncher = mxruntime_sys::PA_APP_LAUNCHER,
OutgoingServices = mxruntime_sys::PA_APP_SERVICES,
User0 = mxruntime_sys::PA_USER0,
}
pub fn get_startup_handle(htype: HandleType) -> Option<Handle> {
unsafe {
let raw = mxruntime_sys::mx_get_startup_handle(htype as u32);
if raw == mxruntime_sys::MX_HANDLE_INVALID {
None
} else {
Some(Handle::from_raw(raw))
}
}
}
pub fn get_service_root() -> Result<Channel, Status> {
let (h1, h2) = Channel::create(ChannelOpts::Normal).unwrap();
let svc = CString::new("/svc/.").unwrap();
let connect_status = unsafe {
mxio_service_connect(svc.as_ptr(), h1.raw_handle())
};
if connect_status == MX_OK {
Ok(h2)
} else {
Err(Status::from_raw(connect_status))
}
}
pub fn connect_to_environment_service(service_root: Channel, path: &str) -> Result<Channel, Status> {
let (h1, h2) = Channel::create(ChannelOpts::Normal).unwrap();
let path_str = CString::new(path).unwrap();
let connect_status = unsafe {
mxio_service_connect_at(service_root.raw_handle(), path_str.as_ptr(), h1.raw_handle())
};
if connect_status == MX_OK {
Ok(h2)
} else {
Err(Status::from_raw(connect_status))
}
}