1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 //! rust distributed hardware sys.
17 
18 #![allow(dead_code)]
19 
20 use std::ffi::{ c_char, CString };
21 
22 use hilog_rust::{ error, hilog, HiLogLabel, LogType };
23 
24 use fusion_utils_rust::call_info_trace;
25 
26 use crate::DmDeviceInfo;
27 
28 const LOG_LABEL: HiLogLabel = HiLogLabel {
29     log_type: LogType::LogCore,
30     domain: 0xD002220,
31     tag: "RustDmBinding"
32 };
33 
34 /// C representation of [`DmAuthForm`].
35 #[repr(C)]
36 #[derive(Clone, Copy, Debug)]
37 pub enum CDmAuthForm {
38     /// Invalid type.
39     INVALIDTYPE = -1,
40     /// Peer to peer.
41     PEERTOPEER = 0,
42     /// Identical account.
43     IDENTICALACCOUNT = 1,
44     /// Across account.
45     ACROSSACCOUNT = 2,
46 }
47 
48 /// C representation of [`DmDeviceInfo`].
49 #[repr(C)]
50 pub struct CDmDeviceInfo {
51     /// Device Id of the device.
52     pub device_id: *const c_char,
53     /// Device name of the device.
54     pub device_name: *const c_char,
55     /// Device type of the device.
56     pub device_type_id: u16,
57     /// NetworkId of the device.
58     pub network_id: *const c_char,
59     /// The distance of discovered device.
60     pub range: i32,
61     /// Device authentication form.
62     pub auth_form: CDmAuthForm,
63 }
64 
65 /// Used to hold callbacks.
66 #[repr(C)]
67 pub struct CRegisterDevStateCallback {
68     /// Device online callback of the device.
69     pub on_device_online: OnRegisterDevState,
70     /// Device changed callback of the device.
71     pub on_device_changed: OnRegisterDevState,
72     /// Device ready callback of the device.
73     pub on_device_ready: OnRegisterDevState,
74     /// Device offline callback of the device.
75     pub on_device_offline: OnRegisterDevState,
76 }
77 
78 // Callback function type for OnDeviceInit() from native, this
79 // callback will be called when device manager init.
80 pub type OnDeviceInit = unsafe extern "C" fn ();
81 
82 /// Callback when the remote died.
83 ///
84 /// # Safety
85 ///
86 /// The function pointer passed to the c-side is legal.
on_remote_died()87 pub unsafe extern "C" fn on_remote_died() {
88     call_info_trace!("dm_binding::on_remote_died");
89 }
90 
91 // Callback function type for OnRegisterDevState() from native, this
92 // callback will be called when register device state.
93 pub type OnRegisterDevState = unsafe extern "C" fn (
94     device_info: *const CDmDeviceInfo
95 );
96 
97 /// Callback when the device online.
98 ///
99 /// # Safety
100 ///
101 /// The function pointer passed to the c-side is legal.
on_device_online(device_info_ptr: *const CDmDeviceInfo)102 pub unsafe extern "C" fn on_device_online(device_info_ptr: *const CDmDeviceInfo) {
103     call_info_trace!("dm_binding::on_device_online");
104     let _device_info = match DmDeviceInfo::from_raw(device_info_ptr) {
105         Some(dev_info) => dev_info,
106         None => {
107             error!(LOG_LABEL, "Crate DmDeviceInfo failed");
108             return;
109         }
110     };
111 
112     CDestroyDmDeviceInfo(device_info_ptr);
113 }
114 
115 /// Callback when the device changed.
116 ///
117 /// # Safety
118 ///
119 /// The function pointer passed to the c-side is legal.
on_device_changed(_device_info_ptr: *const CDmDeviceInfo)120 pub unsafe extern "C" fn on_device_changed(_device_info_ptr: *const CDmDeviceInfo) {
121     call_info_trace!("dm_binding::on_device_changed");
122 }
123 
124 /// Callback when the device ready.
125 ///
126 /// # Safety
127 ///
128 /// The function pointer passed to the c-side is legal.
on_device_ready(_device_info_ptr: *const CDmDeviceInfo)129 pub unsafe extern "C" fn on_device_ready(_device_info_ptr: *const CDmDeviceInfo) {
130     call_info_trace!("dm_binding::on_device_ready");
131 }
132 
133 /// Callback when the device offline.
134 ///
135 /// # Safety
136 ///
137 /// The function pointer passed to the c-side is legal.
on_device_offline(_device_info_ptr: *const CDmDeviceInfo)138 pub unsafe extern "C" fn on_device_offline(_device_info_ptr: *const CDmDeviceInfo) {
139     call_info_trace!("dm_binding::on_device_offline");
140 }
141 
142 extern "C" {
CInitDeviceManager(pkg_name: *const c_char, on_device_init: OnDeviceInit) -> bool143     pub fn CInitDeviceManager(pkg_name: *const c_char, on_device_init: OnDeviceInit) -> bool;
CRegisterDevState( pkg_name: *const c_char, extra: *const c_char, on_register_dev_state: CRegisterDevStateCallback, ) -> bool144     pub fn CRegisterDevState(
145         pkg_name: *const c_char,
146         extra: *const c_char,
147         on_register_dev_state: CRegisterDevStateCallback,
148     ) -> bool;
CUnRegisterDevState(pkg_name: *const c_char, extra: *const c_char) -> bool149     pub fn CUnRegisterDevState(pkg_name: *const c_char, extra: *const c_char) -> bool;
CDestroyDmDeviceInfo(device_info: *const CDmDeviceInfo)150     pub fn CDestroyDmDeviceInfo(device_info: *const CDmDeviceInfo);
151 }