Skip to main content

systemconfiguration/
network_protocol.rs

1use crate::{bridge, error::Result, ffi, PropertyList};
2
3#[derive(Clone, Debug)]
4/// Wraps `SCNetworkProtocolRef`.
5pub struct NetworkProtocol {
6    raw: bridge::OwnedHandle,
7}
8
9impl NetworkProtocol {
10    /// Wraps `SCNetworkProtocolGetTypeID`.
11    pub fn type_id() -> u64 {
12        unsafe { ffi::network_protocol::sc_network_protocol_get_type_id() }
13    }
14
15    /// Wraps `SCNetworkProtocolCopyConfiguration`.
16    pub fn configuration(&self) -> Option<PropertyList> {
17        unsafe {
18            bridge::OwnedHandle::from_raw(
19                ffi::network_protocol::sc_network_protocol_copy_configuration(self.raw.as_ptr()),
20            )
21        }
22        .map(PropertyList::from_owned_handle)
23    }
24
25    /// Wraps `SCNetworkProtocolGetEnabled`.
26    pub fn is_enabled(&self) -> bool {
27        unsafe { ffi::network_protocol::sc_network_protocol_get_enabled(self.raw.as_ptr()) != 0 }
28    }
29
30    /// Wraps `SCNetworkProtocolCopyProtocolType`.
31    pub fn protocol_type(&self) -> Option<String> {
32        bridge::take_optional_string(unsafe {
33            ffi::network_protocol::sc_network_protocol_copy_protocol_type(self.raw.as_ptr())
34        })
35    }
36
37    /// Wraps `SCNetworkProtocolSetConfiguration`.
38    pub fn set_configuration(&self, value: &PropertyList) -> Result<()> {
39        let ok = unsafe {
40            ffi::network_protocol::sc_network_protocol_set_configuration(
41                self.raw.as_ptr(),
42                value.as_ptr(),
43            )
44        };
45        bridge::bool_result("sc_network_protocol_set_configuration", ok)
46    }
47
48    /// Wraps `SCNetworkProtocolSetEnabled`.
49    pub fn set_enabled(&self, enabled: bool) -> Result<()> {
50        let ok = unsafe {
51            ffi::network_protocol::sc_network_protocol_set_enabled(
52                self.raw.as_ptr(),
53                u8::from(enabled),
54            )
55        };
56        bridge::bool_result("sc_network_protocol_set_enabled", ok)
57    }
58
59    pub(crate) fn from_owned_handle(raw: bridge::OwnedHandle) -> Self {
60        Self { raw }
61    }
62}