1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci//! fusion IPC client.
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#![allow(dead_code)]
19f857971dSopenharmony_ci#![allow(unused_variables)]
20f857971dSopenharmony_ci
21f857971dSopenharmony_cimod frameworks;
22f857971dSopenharmony_ci
23f857971dSopenharmony_ciuse std::ffi::{ c_char, CStr, CString };
24f857971dSopenharmony_ciuse std::os::fd::AsRawFd;
25f857971dSopenharmony_ciuse hilog_rust::{ error, hilog, HiLogLabel, LogType };
26f857971dSopenharmony_ciuse fusion_utils_rust::call_debug_enter;
27f857971dSopenharmony_ciuse fusion_data_rust::{ AllocSocketPairParam, CDragData, DragData };
28f857971dSopenharmony_ciuse frameworks::FusionFrameworks;
29f857971dSopenharmony_ci
30f857971dSopenharmony_ciconst LOG_LABEL: HiLogLabel = HiLogLabel {
31f857971dSopenharmony_ci    log_type: LogType::LogCore,
32f857971dSopenharmony_ci    domain: 0xD002220,
33f857971dSopenharmony_ci    tag: "fusion_client"
34f857971dSopenharmony_ci};
35f857971dSopenharmony_ci
36f857971dSopenharmony_ci/// # Safety
37f857971dSopenharmony_ci#[no_mangle]
38f857971dSopenharmony_ciunsafe extern "C" fn fusion_alloc_socket_fd(program_name: *const c_char, module_type: i32,
39f857971dSopenharmony_ci    client_fd: *mut i32, token_type: *mut i32) -> i32
40f857971dSopenharmony_ci{
41f857971dSopenharmony_ci    call_debug_enter!("fusion_alloc_socket_fd");
42f857971dSopenharmony_ci    if program_name.is_null() {
43f857971dSopenharmony_ci        error!(LOG_LABEL, "program_name is null");
44f857971dSopenharmony_ci        return -1;
45f857971dSopenharmony_ci    };
46f857971dSopenharmony_ci    let param = match AllocSocketPairParam::from_c(program_name, module_type) {
47f857971dSopenharmony_ci        Ok(param) => { param }
48f857971dSopenharmony_ci        Err(err) => {
49f857971dSopenharmony_ci            error!(LOG_LABEL, "Failed parsing AllocSocketPairParam");
50f857971dSopenharmony_ci            return i32::from(err);
51f857971dSopenharmony_ci        }
52f857971dSopenharmony_ci    };
53f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
54f857971dSopenharmony_ci    fw.alloc_socket_pair(&param).map_or_else(
55f857971dSopenharmony_ci        |err| { i32::from(err) },
56f857971dSopenharmony_ci        |(fdesc, t)| {
57f857971dSopenharmony_ci            *client_fd = fdesc.as_raw_fd();
58f857971dSopenharmony_ci            *token_type = t;
59f857971dSopenharmony_ci            0
60f857971dSopenharmony_ci        }
61f857971dSopenharmony_ci    )
62f857971dSopenharmony_ci}
63f857971dSopenharmony_ci
64f857971dSopenharmony_ci/// # Safety
65f857971dSopenharmony_ci#[no_mangle]
66f857971dSopenharmony_ciunsafe extern "C" fn fusion_start_drag(c_drag_data: *mut CDragData) -> i32
67f857971dSopenharmony_ci{
68f857971dSopenharmony_ci    c_drag_data.as_mut().map_or(-1, |c_drag_data_ref| {
69f857971dSopenharmony_ci        let drag_data = DragData::from_c(c_drag_data_ref);
70f857971dSopenharmony_ci        let fw = FusionFrameworks::get_instance().unwrap();
71f857971dSopenharmony_ci        fw.start_drag(&drag_data).map_or_else(|err| { i32::from(err) }, |_| { 0 })
72f857971dSopenharmony_ci    })
73f857971dSopenharmony_ci}
74f857971dSopenharmony_ci
75f857971dSopenharmony_ci/// # Safety
76f857971dSopenharmony_ci#[no_mangle]
77f857971dSopenharmony_ciunsafe extern "C" fn fusion_register_coordination_listener() -> i32
78f857971dSopenharmony_ci{
79f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
80f857971dSopenharmony_ci    fw.register_coordination_listener().map_or_else(|err| { i32::from(err) }, |_| { 0 })
81f857971dSopenharmony_ci}
82f857971dSopenharmony_ci
83f857971dSopenharmony_ci/// # Safety
84f857971dSopenharmony_ci#[no_mangle]
85f857971dSopenharmony_ciunsafe extern "C" fn fusion_unregister_coordination_listener() -> i32
86f857971dSopenharmony_ci{
87f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
88f857971dSopenharmony_ci    fw.unregister_coordination_listener().map_or_else(|err| { i32::from(err) }, |_| { 0 })
89f857971dSopenharmony_ci}
90f857971dSopenharmony_ci
91f857971dSopenharmony_ci/// # Safety
92f857971dSopenharmony_ci#[no_mangle]
93f857971dSopenharmony_ciunsafe extern "C" fn fusion_enable_coordination(user_data: i32) -> i32
94f857971dSopenharmony_ci{
95f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
96f857971dSopenharmony_ci    fw.enable_coordination(user_data).map_or_else(|err| { i32::from(err) }, |_| { 0 })
97f857971dSopenharmony_ci}
98f857971dSopenharmony_ci
99f857971dSopenharmony_ci/// # Safety
100f857971dSopenharmony_ci#[no_mangle]
101f857971dSopenharmony_ciunsafe extern "C" fn fusion_disable_coordination(user_data: i32) -> i32
102f857971dSopenharmony_ci{
103f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
104f857971dSopenharmony_ci    fw.disable_coordination(user_data).map_or_else(|err| { i32::from(err) }, |_| { 0 })
105f857971dSopenharmony_ci}
106f857971dSopenharmony_ci
107f857971dSopenharmony_ci/// # Safety
108f857971dSopenharmony_ci#[no_mangle]
109f857971dSopenharmony_ciunsafe extern "C" fn fusion_start_coordination(user_data: i32,
110f857971dSopenharmony_ci    remote_network_id: *const c_char, start_device_id: i32) -> i32
111f857971dSopenharmony_ci{
112f857971dSopenharmony_ci    if remote_network_id.is_null() {
113f857971dSopenharmony_ci        error!(LOG_LABEL, "remote_network_id is null");
114f857971dSopenharmony_ci        return -1;
115f857971dSopenharmony_ci    }
116f857971dSopenharmony_ci    let remote_network_id: String = match CStr::from_ptr(remote_network_id).to_str() {
117f857971dSopenharmony_ci        Ok(id) => { id.to_string() }
118f857971dSopenharmony_ci        Err(_) => {
119f857971dSopenharmony_ci            error!(LOG_LABEL, "Invalid network id");
120f857971dSopenharmony_ci            return -1;
121f857971dSopenharmony_ci        }
122f857971dSopenharmony_ci    };
123f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
124f857971dSopenharmony_ci    fw.start_coordination(user_data, &remote_network_id,
125f857971dSopenharmony_ci        start_device_id).map_or_else(|err| { i32::from(err) }, |_| { 0 })
126f857971dSopenharmony_ci}
127f857971dSopenharmony_ci
128f857971dSopenharmony_ci/// # Safety
129f857971dSopenharmony_ci#[no_mangle]
130f857971dSopenharmony_ciunsafe extern "C" fn fusion_stop_coordination(user_data: i32, is_unchained: i32) -> i32
131f857971dSopenharmony_ci{
132f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
133f857971dSopenharmony_ci    fw.stop_coordination(user_data, is_unchained).map_or_else(|err| { i32::from(err) }, |_| { 0 })
134f857971dSopenharmony_ci}
135f857971dSopenharmony_ci
136f857971dSopenharmony_ci/// # Safety
137f857971dSopenharmony_ci#[no_mangle]
138f857971dSopenharmony_ciunsafe extern "C" fn fusion_get_coordination_state(user_data: i32, device_id: *const c_char) -> i32
139f857971dSopenharmony_ci{
140f857971dSopenharmony_ci    if device_id.is_null() {
141f857971dSopenharmony_ci        error!(LOG_LABEL, "device_id is null");
142f857971dSopenharmony_ci        return -1;
143f857971dSopenharmony_ci    }
144f857971dSopenharmony_ci    let device_id: String = match CStr::from_ptr(device_id).to_str() {
145f857971dSopenharmony_ci        Ok(id) => { id.to_string() }
146f857971dSopenharmony_ci        Err(_) => {
147f857971dSopenharmony_ci            error!(LOG_LABEL, "Invalid device id");
148f857971dSopenharmony_ci            return -1;
149f857971dSopenharmony_ci        }
150f857971dSopenharmony_ci    };
151f857971dSopenharmony_ci    let fw = FusionFrameworks::get_instance().unwrap();
152f857971dSopenharmony_ci    fw.get_coordination_state(user_data, &device_id).map_or_else(|err| { i32::from(err) }, |_| { 0 })
153f857971dSopenharmony_ci}
154