use super::gateway_instance_ctx::GCGatewayMockInstanceCtx;
use crate::GCDatapointValue;
use gc_abi::GCAbiConversion;
use gc_abi::raw;
use std::ffi::CStr;
pub unsafe extern "C" fn publish_datapoint_value(ctx: raw::GCCoreCtx, dp_value: raw::GCDatapointValue) -> bool {
unsafe {
let gateway_mock: &GCGatewayMockInstanceCtx = ctx.into();
gateway_mock.publish_datapoint_callback.as_ref()(GCDatapointValue::from_c_ptr(dp_value))
}
}
pub unsafe extern "C" fn system_log(ctx: raw::GCCoreCtx, level: raw::GCLogLevel, log: *const ::std::os::raw::c_char) {
unsafe {
let gateway_mock: &GCGatewayMockInstanceCtx = ctx.into();
let c_str = CStr::from_ptr(log);
gateway_mock.log_callback.as_ref()(level, c_str)
}
}
pub unsafe extern "C" fn audit_log(ctx: raw::GCCoreCtx, log: *const ::std::os::raw::c_char) {
unsafe {
let gateway_mock: &GCGatewayMockInstanceCtx = ctx.into();
let c_str = CStr::from_ptr(log);
gateway_mock.log_callback.as_ref()(raw::eGCLogLevel_INFO, c_str)
}
}
pub unsafe extern "C" fn release_datapoint_value(dp_value: raw::GCDatapointValue) -> bool {
unsafe {
let ptr_conversion: *mut GCDatapointValue = dp_value as *mut GCDatapointValue;
drop(Box::from_raw(ptr_conversion));
true
}
}
pub unsafe extern "C" fn get_last_datapoint_value(ctx: raw::GCCoreCtx, datapoint_id: raw::GCDatapointIdentifier) -> raw::GCDatapointValue {
let gateway_mock: &GCGatewayMockInstanceCtx = ctx.into();
let result = gateway_mock.get_last_datapoint_callback.as_ref()(datapoint_id);
let leaked_ptr = Box::into_raw(Box::new(result));
leaked_ptr as raw::GCDatapointValue
}
pub unsafe extern "C" fn get_ethernet_interface(
ctx: raw::GCCoreCtx,
name: *const ::std::os::raw::c_char,
out: *mut raw::sGCEthernetInterface,
) -> bool {
unsafe {
let gateway_mock: &GCGatewayMockInstanceCtx = ctx.into();
let name_cstr = CStr::from_ptr(name);
match gateway_mock.get_ethernet_interface_callback.as_ref()(name_cstr) {
Some(iface) => {
std::ptr::replace(out, iface.into_c());
true
}
None => false,
}
}
}
pub unsafe extern "C" fn get_serial_interface(ctx: raw::GCCoreCtx, name: *const ::std::os::raw::c_char, out: *mut raw::sGCSerialInterface) -> bool {
unsafe {
let gateway_mock: &GCGatewayMockInstanceCtx = ctx.into();
let name_cstr = CStr::from_ptr(name);
match gateway_mock.get_serial_interface_callback.as_ref()(name_cstr) {
Some(iface) => {
std::ptr::replace(out, iface.into_c());
true
}
None => false,
}
}
}