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 //! fusion IPC client.
17
18 #![allow(dead_code)]
19 #![allow(unused_variables)]
20
21 extern crate hilog_rust;
22 extern crate ipc_rust;
23 extern crate fusion_utils_rust;
24 extern crate fusion_ipc_client_rust;
25 extern crate fusion_data_rust;
26 extern crate fusion_basic_client_rust;
27 extern crate fusion_drag_client_rust;
28 extern crate fusion_coordination_client_rust;
29
30 mod frameworks;
31
32 use std::ffi::{ c_char, CStr, CString };
33 use std::os::fd::AsRawFd;
34 use hilog_rust::{ error, info, hilog, HiLogLabel, LogType };
35 use fusion_utils_rust::call_debug_enter;
36 use fusion_data_rust::{ AllocSocketPairParam, CDragData, DragData };
37 use frameworks::FusionFrameworks;
38
39 const LOG_LABEL: HiLogLabel = HiLogLabel {
40 log_type: LogType::LogCore,
41 domain: 0xD002220,
42 tag: "fusion_client"
43 };
44
45 /// fusion_start_drag()
46 /// # Safety
47 #[no_mangle]
fusion_alloc_socket_fd(program_name: *const c_char, module_type: i32, client_fd: *mut i32, token_type: *mut i32) -> i3248 pub unsafe extern "C" fn fusion_alloc_socket_fd(program_name: *const c_char, module_type: i32,
49 client_fd: *mut i32, token_type: *mut i32) -> i32
50 {
51 call_debug_enter!("fusion_alloc_socket_fd");
52 match FusionFrameworks::get_mut_instance() {
53 Some(fw_mut) => {
54 fw_mut.set_ipc_connect();
55 }
56 None => {
57 error!(LOG_LABEL, "Fail dereferencing mutable FusionFrameworks instance");
58 return -1;
59 }
60 }
61
62 let param_result = unsafe {
63 if program_name.is_null() {
64 error!(LOG_LABEL, "program_name is null");
65 return -1;
66 } else {
67 AllocSocketPairParam::from_c(program_name, module_type)
68 }
69 };
70 let param = match param_result {
71 Ok(param) => {
72 param
73 }
74 Err(err) => {
75 error!(LOG_LABEL, "Fail parsing AllocSocketPairParam");
76 return err;
77 }
78 };
79 match FusionFrameworks::get_instance() {
80 Some(fw) => {
81 info!(LOG_LABEL, "Call alloc_socket_pair()");
82 match fw.alloc_socket_pair(¶m) {
83 Ok((fdesc, t)) => {
84 *client_fd = fdesc.as_raw_fd();
85 *token_type = t;
86 0
87 }
88 Err(_) => {
89 error!(LOG_LABEL, "alloc_socket_pair() fail");
90 -1
91 }
92 }
93 }
94 None => {
95 error!(LOG_LABEL, "Fail dereferencing FusionFrameworks instance");
96 -1
97 }
98 }
99 }
100
101 /// fusion_start_drag()
102 /// # Safety
103 #[no_mangle]
fusion_start_drag(c_drag_data: *mut CDragData) -> i32104 pub unsafe extern "C" fn fusion_start_drag(c_drag_data: *mut CDragData) -> i32
105 {
106 c_drag_data.as_mut().map_or(-1, |c_drag_data_ref| {
107 info!(LOG_LABEL, "enter fusion_start_drag()");
108 match FusionFrameworks::get_mut_instance() {
109 Some(fw_mut) => {
110 fw_mut.set_ipc_connect();
111 }
112 None => {
113 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
114 return -1;
115 }
116 }
117
118 let drag_data = DragData::from_c(c_drag_data_ref);
119 match FusionFrameworks::get_instance() {
120 Some(fw) => {
121 info!(LOG_LABEL, "Call start_drag()");
122 match fw.start_drag(&drag_data) {
123 Ok(_) => {
124 0
125 }
126 Err(err) => {
127 error!(LOG_LABEL, "Error happened when starting drag");
128 err
129 }
130 }
131 }
132 None => {
133 error!(LOG_LABEL, "Can not dereference FusionFrameworks instance");
134 -1
135 }
136 }
137 })
138 }
139
140 /// TODO: add documentation.
141 /// # Safety
142 #[no_mangle]
fusion_register_coordination_listener() -> i32143 pub unsafe extern "C" fn fusion_register_coordination_listener() -> i32
144 {
145 match FusionFrameworks::get_mut_instance() {
146 Some(fw_mut) => {
147 fw_mut.set_ipc_connect();
148 match fw_mut.register_coordination_listener() {
149 Ok(_) => {
150 0
151 }
152 Err(err) => {
153 error!(LOG_LABEL, "Fail to register coordination listener: {}", @public(err));
154 err
155 }
156 }
157 }
158 None => {
159 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
160 -1
161 }
162 }
163 }
164
165 /// TODO: add documentation.
166 /// # Safety
167 #[no_mangle]
fusion_unregister_coordination_listener() -> i32168 pub unsafe extern "C" fn fusion_unregister_coordination_listener() -> i32
169 {
170 match FusionFrameworks::get_mut_instance() {
171 Some(fw_mut) => {
172 fw_mut.set_ipc_connect();
173 match fw_mut.unregister_coordination_listener() {
174 Ok(_) => {
175 0
176 }
177 Err(err) => {
178 error!(LOG_LABEL, "Fail to unregister coordination listener: {}", @public(err));
179 err
180 }
181 }
182 }
183 None => {
184 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
185 -1
186 }
187 }
188 }
189
190 /// TODO: add documentation.
191 /// # Safety
192 #[no_mangle]
fusion_enable_coordination(user_data: i32) -> i32193 pub unsafe extern "C" fn fusion_enable_coordination(user_data: i32) -> i32
194 {
195 match FusionFrameworks::get_mut_instance() {
196 Some(fw_mut) => {
197 fw_mut.set_ipc_connect();
198 match fw_mut.enable_coordination(user_data) {
199 Ok(_) => {
200 0
201 }
202 Err(err) => {
203 error!(LOG_LABEL, "Error in enable coordination");
204 err
205 }
206 }
207 }
208 None => {
209 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
210 -1
211 }
212 }
213 }
214
215 /// TODO: add documentation.
216 /// # Safety
217 #[no_mangle]
fusion_disable_coordination(user_data: i32) -> i32218 pub unsafe extern "C" fn fusion_disable_coordination(user_data: i32) -> i32
219 {
220 match FusionFrameworks::get_mut_instance() {
221 Some(fw_mut) => {
222 fw_mut.set_ipc_connect();
223 match fw_mut.disable_coordination(user_data) {
224 Ok(_) => {
225 0
226 }
227 Err(err) => {
228 error!(LOG_LABEL, "Error in enable coordination");
229 err
230 }
231 }
232 }
233 None => {
234 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
235 -1
236 }
237 }
238 }
239
240 /// TODO: add documentation.
241 /// # Safety
242 #[no_mangle]
fusion_start_coordination(user_data: i32, remote_network_id: *const c_char, start_device_id: i32) -> i32243 pub unsafe extern "C" fn fusion_start_coordination(user_data: i32,
244 remote_network_id: *const c_char, start_device_id: i32) -> i32
245 {
246 if remote_network_id.is_null() {
247 error!(LOG_LABEL, "remote_network_id is null");
248 return -1;
249 }
250 let remote_network_id: String = match CStr::from_ptr(remote_network_id).to_str() {
251 Ok(id) => {
252 id.to_string()
253 }
254 Err(_) => {
255 error!(LOG_LABEL, "Invalid network id");
256 return -1;
257 }
258 };
259 match FusionFrameworks::get_mut_instance() {
260 Some(fw_mut) => {
261 fw_mut.set_ipc_connect();
262 match fw_mut.start_coordination(user_data, remote_network_id, start_device_id) {
263 Ok(_) => {
264 0
265 }
266 Err(err) => {
267 error!(LOG_LABEL, "Error happened when starting coordination");
268 err
269 }
270 }
271 }
272 None => {
273 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
274 -1
275 }
276 }
277 }
278
279 /// TODO: add documentation.
280 /// # Safety
281 #[no_mangle]
fusion_stop_coordination(user_data: i32, is_unchained: i32) -> i32282 pub unsafe extern "C" fn fusion_stop_coordination(user_data: i32, is_unchained: i32) -> i32
283 {
284 match FusionFrameworks::get_mut_instance() {
285 Some(fw_mut) => {
286 fw_mut.set_ipc_connect();
287 match fw_mut.stop_coordination(user_data, is_unchained) {
288 Ok(_) => {
289 0
290 }
291 Err(err) => {
292 error!(LOG_LABEL, "Fail to stop coordination");
293 err
294 }
295 }
296 }
297 None => {
298 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
299 -1
300 }
301 }
302 }
303
304 /// TODO: add documentation.
305 /// # Safety
306 #[no_mangle]
fusion_get_coordination_state(user_data: i32, device_id: *const c_char) -> i32307 pub unsafe extern "C" fn fusion_get_coordination_state(user_data: i32, device_id: *const c_char) -> i32
308 {
309 if device_id.is_null() {
310 error!(LOG_LABEL, "device_id is null");
311 return -1;
312 }
313 let device_id: String = match CStr::from_ptr(device_id).to_str() {
314 Ok(id) => {
315 id.to_string()
316 }
317 Err(_) => {
318 error!(LOG_LABEL, "Invalid device id");
319 return -1;
320 }
321 };
322 match FusionFrameworks::get_mut_instance() {
323 Some(fw_mut) => {
324 fw_mut.set_ipc_connect();
325 match fw_mut.get_coordination_state(user_data, device_id) {
326 Ok(_) => {
327 0
328 }
329 Err(err) => {
330 error!(LOG_LABEL, "Fail to get coordination state: {}", @public(err));
331 err
332 }
333 }
334 }
335 None => {
336 error!(LOG_LABEL, "Can not dereference mutable FusionFrameworks instance");
337 -1
338 }
339 }
340 }
341