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