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//! General functionalities.
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#![allow(dead_code)]
19f857971dSopenharmony_ci#![allow(unused_variables)]
20f857971dSopenharmony_ci
21f857971dSopenharmony_ciuse std::ffi::{ c_char, CString };
22f857971dSopenharmony_ciuse std::fs::File;
23f857971dSopenharmony_ciuse std::os::fd::{ FromRawFd, RawFd };
24f857971dSopenharmony_ci
25f857971dSopenharmony_ciuse hilog_rust::{ info, error, hilog, HiLogLabel, LogType };
26f857971dSopenharmony_ciuse ipc_rust::{ BorrowedMsgParcel, Deserialize, FileDesc, Serialize };
27f857971dSopenharmony_ci
28f857971dSopenharmony_ciuse fusion_data_rust::{ IPlugin, AllocSocketPairParam, BasicParamID, CallingContext };
29f857971dSopenharmony_ciuse fusion_services_rust::FusionService;
30f857971dSopenharmony_ciuse fusion_utils_rust::{ call_debug_enter, FusionResult, FusionErrorCode };
31f857971dSopenharmony_ci
32f857971dSopenharmony_ciconst LOG_LABEL: HiLogLabel = HiLogLabel {
33f857971dSopenharmony_ci    log_type: LogType::LogCore,
34f857971dSopenharmony_ci    domain: 0xD002220,
35f857971dSopenharmony_ci    tag: "FusionBasicServer"
36f857971dSopenharmony_ci};
37f857971dSopenharmony_ci
38f857971dSopenharmony_ci/// Module-level interface of general functionalities.
39f857971dSopenharmony_ci#[derive(Default)]
40f857971dSopenharmony_cipub struct FusionBasicServer(i32);
41f857971dSopenharmony_ci
42f857971dSopenharmony_ciimpl FusionBasicServer {
43f857971dSopenharmony_ci    fn alloc_socket_pair(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> FusionResult<()>
44f857971dSopenharmony_ci    {
45f857971dSopenharmony_ci        let call_param = AllocSocketPairParam::deserialize(data).or(Err(FusionErrorCode::Fail))?;
46f857971dSopenharmony_ci
47f857971dSopenharmony_ci        if let Some(proxy) = FusionService::get_instance() {
48f857971dSopenharmony_ci            let mut client_fd: RawFd = 0;
49f857971dSopenharmony_ci            let mut token_type: i32 = 0;
50f857971dSopenharmony_ci
51f857971dSopenharmony_ci            proxy.alloc_socket_fd(call_param.program_name.as_str(),
52f857971dSopenharmony_ci                call_param.module_type, &mut client_fd, &mut token_type)?;
53f857971dSopenharmony_ci
54f857971dSopenharmony_ci            let f = unsafe { File::from_raw_fd(client_fd) };
55f857971dSopenharmony_ci            FileDesc::new(f).serialize(reply).or(Err(FusionErrorCode::Fail))?;
56f857971dSopenharmony_ci            token_type.serialize(reply).or(Err(FusionErrorCode::Fail))?;
57f857971dSopenharmony_ci            Ok(())
58f857971dSopenharmony_ci        } else {
59f857971dSopenharmony_ci            error!(LOG_LABEL, "No proxy");
60f857971dSopenharmony_ci            Err(FusionErrorCode::Fail)
61f857971dSopenharmony_ci        }
62f857971dSopenharmony_ci    }
63f857971dSopenharmony_ci}
64f857971dSopenharmony_ci
65f857971dSopenharmony_ciimpl IPlugin for FusionBasicServer {
66f857971dSopenharmony_ci    fn enable(&self, context: &CallingContext, data: &BorrowedMsgParcel,
67f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
68f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::enable");
69f857971dSopenharmony_ci        Ok(())
70f857971dSopenharmony_ci    }
71f857971dSopenharmony_ci
72f857971dSopenharmony_ci    fn disable(&self, context: &CallingContext, data: &BorrowedMsgParcel,
73f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
74f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::disable");
75f857971dSopenharmony_ci        Ok(())
76f857971dSopenharmony_ci    }
77f857971dSopenharmony_ci
78f857971dSopenharmony_ci    fn start(&self, context: &CallingContext, data: &BorrowedMsgParcel,
79f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
80f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::start");
81f857971dSopenharmony_ci        error!(LOG_LABEL, "FusionBasicServer::start");
82f857971dSopenharmony_ci        Err(FusionErrorCode::Fail)
83f857971dSopenharmony_ci    }
84f857971dSopenharmony_ci
85f857971dSopenharmony_ci    fn stop(&self, context: &CallingContext, data: &BorrowedMsgParcel,
86f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
87f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::stop");
88f857971dSopenharmony_ci        Ok(())
89f857971dSopenharmony_ci    }
90f857971dSopenharmony_ci
91f857971dSopenharmony_ci    fn add_watch(&self, context: &CallingContext, id: u32, data: &BorrowedMsgParcel,
92f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
93f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::add_watch");
94f857971dSopenharmony_ci        Ok(())
95f857971dSopenharmony_ci    }
96f857971dSopenharmony_ci
97f857971dSopenharmony_ci    fn remove_watch(&self, context: &CallingContext, id: u32, data: &BorrowedMsgParcel,
98f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
99f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::remove_watch");
100f857971dSopenharmony_ci        Ok(())
101f857971dSopenharmony_ci    }
102f857971dSopenharmony_ci
103f857971dSopenharmony_ci    fn set_param(&self, context: &CallingContext, id: u32, data: &BorrowedMsgParcel,
104f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
105f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::set_param");
106f857971dSopenharmony_ci        Ok(())
107f857971dSopenharmony_ci    }
108f857971dSopenharmony_ci
109f857971dSopenharmony_ci    fn get_param(&self, context: &CallingContext, id: u32, data: &BorrowedMsgParcel,
110f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
111f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::get_param");
112f857971dSopenharmony_ci        Ok(())
113f857971dSopenharmony_ci    }
114f857971dSopenharmony_ci
115f857971dSopenharmony_ci    fn control(&self, context: &CallingContext, id: u32, data: &BorrowedMsgParcel,
116f857971dSopenharmony_ci        reply: &mut BorrowedMsgParcel) -> FusionResult<()> {
117f857971dSopenharmony_ci        call_debug_enter!("FusionBasicServer::control");
118f857971dSopenharmony_ci        match BasicParamID::try_from(id) {
119f857971dSopenharmony_ci            Ok(param) => {
120f857971dSopenharmony_ci                match param {
121f857971dSopenharmony_ci                    BasicParamID::AllocSocketPair => {
122f857971dSopenharmony_ci                        info!(LOG_LABEL, "Alloc socket pair");
123f857971dSopenharmony_ci                        self.alloc_socket_pair(data, reply)
124f857971dSopenharmony_ci                    }
125f857971dSopenharmony_ci                }
126f857971dSopenharmony_ci            }
127f857971dSopenharmony_ci            Err(_) => {
128f857971dSopenharmony_ci                error!(LOG_LABEL, "Invalid param id: {}", id);
129f857971dSopenharmony_ci                Err(FusionErrorCode::Fail)
130f857971dSopenharmony_ci            }
131f857971dSopenharmony_ci        }
132f857971dSopenharmony_ci    }
133f857971dSopenharmony_ci}
134