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