use std::{ffi::*, ptr};
use crate::Lib;
use super::{
types::{HcnetLevel, NetDvrPreviewInfo, RealDataCallback, TagPdcParamKey},
LpNetDvrJpegpara, LpnetDvrDeviceInfoV40, NetDvrUserLoginInfo, NetLastError, NetSdkInitCfgType,
};
use e_utils::{
auto_any_res_c,
parse::{AutoParse, CAutoParse},
CResult,
};
use libloading::{Library, Symbol};
#[repr(C)]
#[derive(Default)]
pub struct HcNetCoreSdk {
handle: c_long,
lib: Lib,
uid: c_long,
}
unsafe impl Send for HcNetCoreSdk {}
unsafe impl Sync for HcNetCoreSdk {}
impl HcNetCoreSdk {
pub fn set_lib(&mut self, lib: Lib) {
self.lib = lib;
}
pub fn lib(&self) -> &Library {
self.lib.get()
}
pub fn free(&mut self) -> Option<()> {
self.lib.free()
}
}
impl HcNetCoreSdk {
pub unsafe extern "C" fn login_v40(
&mut self,
login_info: &mut NetDvrUserLoginInfo,
dev_info: &mut LpnetDvrDeviceInfoV40,
) -> CResult<c_long> {
let func: Symbol<
'_,
unsafe extern "C" fn(
pLoginInfo: *mut NetDvrUserLoginInfo,
lpDeviceInfo: *mut LpnetDvrDeviceInfoV40,
) -> c_long,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_Login_V40\0"));
let uid = func(login_info, dev_info);
if uid != -1 {
self.uid = uid;
}
CResult::Ok(uid)
}
pub unsafe extern "C" fn init_cfg(&self, etype: NetSdkInitCfgType) -> CResult<bool> {
let func = auto_any_res_c!(self
.lib()
.get::<Symbol<'_, unsafe extern "C" fn(enumType: c_uint, lpInBuff: *const c_void) -> bool>>(
b"NET_DVR_SetSDKInitCfg\0",
));
let et: c_uint = (&etype).into();
let ibuff = match &etype {
NetSdkInitCfgType::NetSdkInitCfgAbility(x) => x as *const _ as *const c_void,
NetSdkInitCfgType::NetSdkInitCfgSdkPath(x) => x as *const _ as *const c_void,
NetSdkInitCfgType::NetSdkInitCfgLibeayPath(x) => *x as *const c_void,
NetSdkInitCfgType::NetSdkInitCfgSsleayPath(x) => *x as *const c_void,
};
let res = func(et, ibuff);
CResult::Ok(res)
}
pub unsafe extern "C" fn set_capture_mode(&self, mode: TagPdcParamKey) -> CResult<bool> {
unsafe {
let func: Symbol<'_, unsafe extern "C" fn(dwCaptureMode: c_uint) -> bool> =
auto_any_res_c!(self.lib().get(b"NET_DVR_SetCapturePictureMode\0"));
let res = func(mode as c_uint);
CResult::Ok(res)
}
}
pub unsafe extern "C" fn capture_picture_block(
&self,
fname: *const c_char,
timeout: c_uint,
) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(
lRealHandle: c_long,
sPicFileName: *const c_char,
dwTimeOut: c_uint,
) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_CapturePictureBlock\0"));
let res = func(self.handle, fname, timeout);
CResult::Ok(res)
}
pub unsafe extern "C" fn capture_picture_block_new(
&self,
pic_buf: *mut c_char,
pic_size: c_uint,
size_returned: *mut c_uint,
) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(
lRealHandle: c_long,
pPicBuf: *mut c_char,
dwPicSize: c_uint,
lpSizeReturned: *mut c_uint,
) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_CapturePictureBlock_New\0"));
let res = func(self.handle, pic_buf, pic_size, size_returned);
CResult::Ok(res)
}
pub unsafe extern "C" fn capture_picture(&self, fname: *const c_char) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(lRealHandle: c_long, sPicFileName: *const c_char) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_CapturePicture\0"));
let res = func(self.handle, fname);
CResult::Ok(res)
}
pub unsafe extern "C" fn logout_sdk(&mut self) -> CResult<bool> {
let func: Symbol<'_, unsafe extern "C" fn(lUserID: c_long) -> bool> =
auto_any_res_c!(self.lib().get(b"NET_DVR_Logout\0"));
let res = func(self.uid);
if res {
self.uid = -1
}
CResult::Ok(res)
}
pub unsafe extern "C" fn stop_real_play_sdk(&mut self) -> CResult<bool> {
let func: Symbol<'_, unsafe extern "C" fn(lRealHandle: c_long) -> bool> =
auto_any_res_c!(self.lib().get(b"NET_DVR_StopRealPlay\0"));
let res = func(self.handle);
if res {
self.handle = -1;
}
CResult::Ok(res)
}
pub unsafe extern "C" fn open_preview_sdk(
&mut self,
preview_info: NetDvrPreviewInfo,
callback: Option<RealDataCallback>,
) -> CResult<bool> {
println!("1 uid {}", self.uid);
let func: Symbol<
'_,
unsafe extern "C" fn(
lUserID: c_long,
lpPreviewInfo: NetDvrPreviewInfo,
fRealDataCallBack_V30: *const c_void,
pUser: *mut c_void,
) -> c_long,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_RealPlay_V40\0"));
let cback = callback
.and_then(|x| Some(x as *const RealDataCallback as *const c_void))
.unwrap_or(ptr::null());
let handle = func(self.uid, preview_info, cback, ptr::null_mut());
self.handle = handle;
CResult::Ok(handle != -1)
}
pub unsafe extern "C" fn cleanup_sdk(&self) -> CResult<bool> {
let func: Symbol<'_, unsafe extern "C" fn() -> bool> =
auto_any_res_c!(self.lib().get(b"NET_DVR_Cleanup\0"));
let res = func();
CResult::Ok(res)
}
pub unsafe extern "C" fn get_last_error_sdk(&self, msg: *const c_char) -> CResult<NetLastError> {
let func: Symbol<'_, unsafe extern "C" fn() -> c_long> =
auto_any_res_c!(self.lib().get(b"NET_DVR_GetLastError\0"));
let err = func();
CResult::Ok(NetLastError {
code: err,
msg: format!("NetLastError<{}<{}>>", msg.c_to_string(), err).to_c_char(),
})
}
pub unsafe extern "C" fn set_log_to_file_sdk(
&self,
log_path: *const c_char,
level: HcnetLevel,
auto_del: bool,
) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(nLogLevel: c_uint, strLogDir: *const c_char, bAutoDel: bool) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_SetLogToFile\0"));
let res = func(level as c_uint, log_path, auto_del);
CResult::Ok(res)
}
pub unsafe extern "C" fn init_sdk(&self) -> CResult<bool> {
let func: Symbol<'_, unsafe extern "C" fn() -> bool> =
auto_any_res_c!(self.lib().get(b"NET_DVR_Init\0"));
let res = func();
CResult::Ok(res)
}
pub unsafe extern "C" fn capture_jpeg(
&self,
uid: c_long,
channel: c_long,
jpeg_conf: LpNetDvrJpegpara,
fname: *const c_char,
) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(
lUserID: c_long,
lChannel: c_long,
lpJpegPara: LpNetDvrJpegpara,
sPicFileName: *const c_char,
) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_CaptureJPEGPicture\0"));
let res = func(uid, channel, jpeg_conf, fname);
CResult::Ok(res)
}
pub unsafe extern "C" fn get_real_player_index(&self, preview_handle: c_long) -> CResult<c_long> {
let func: Symbol<'_, unsafe extern "C" fn(lRealHandle: c_long) -> c_long> =
auto_any_res_c!(self.lib().get(b"NET_DVR_GetRealPlayerIndex\0"));
let res = func(preview_handle);
CResult::Ok(res)
}
pub unsafe extern "C" fn get_local_ip(
&self,
str_ip: *mut *mut c_char,
valid_num: *mut c_ulong,
enable_bind: *mut bool,
) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(
strIP: *mut *mut c_char,
pValidNum: *mut c_ulong,
pEnableBind: *mut bool,
) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_GetLocalIP\0"));
let res = func(str_ip, valid_num, enable_bind);
CResult::Ok(res)
}
pub unsafe extern "C" fn ip_by_resolve_svr_ex(
&self,
server_ip: *const c_uchar,
server_port: c_ushort,
dvr_name: *const c_uchar,
dvr_name_len: c_ushort,
dvr_serial_number: *const c_uchar,
dvr_serial_len: c_ushort,
get_ip: *mut c_char,
port: *mut c_uint,
) -> CResult<bool> {
let func: Symbol<
'_,
unsafe extern "C" fn(
sServerIP: *const c_uchar,
wServerPort: c_ushort,
sDVRName: *const c_uchar,
wDVRNameLen: c_ushort,
sDVRSerialNumber: *const c_uchar,
wDVRSerialLen: c_ushort,
sGetIP: *mut c_char,
dwPort: *mut c_uint,
) -> bool,
> = auto_any_res_c!(self.lib().get(b"NET_DVR_GetDVRIPByResolveSvr_EX\0"));
let res = func(
server_ip,
server_port,
dvr_name,
dvr_name_len,
dvr_serial_number,
dvr_serial_len,
get_ip,
port,
);
CResult::Ok(res)
}
}