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 //! IPC data definitions of BASIC module. 17 18 use std::ffi::{ c_char, CStr, CString }; 19 use std::fmt::{ Display, Formatter, Error }; 20 use crate::fusion_utils_rust::{ call_debug_enter, define_enum }; 21 use crate::hilog_rust::{ info, error, hilog, HiLogLabel, LogType }; 22 use crate::ipc_rust::{ BorrowedMsgParcel, Serialize, Deserialize, IpcResult }; 23 use crate::FusionResult; 24 25 const LOG_LABEL: HiLogLabel = HiLogLabel { 26 log_type: LogType::LogCore, 27 domain: 0xD002220, 28 tag: "FusionBasicData" 29 }; 30 31 define_enum! { 32 BasicParamID { 33 AllocSocketPair 34 } 35 } 36 37 impl From<BasicParamID> for u32 { from(id: BasicParamID) -> Self38 fn from(id: BasicParamID) -> Self 39 { 40 match id { 41 BasicParamID::AllocSocketPair => { 0u32 }, 42 } 43 } 44 } 45 46 /// Parameters for AllocSocketPair request. 47 pub struct AllocSocketPairParam { 48 /// Represent program name of calling. 49 pub program_name: String, 50 /// Represent module type of calling. 51 pub module_type: i32 52 } 53 54 impl AllocSocketPairParam { 55 /// Construct AllocSocketPairParam from raw data. 56 /// 57 /// # Safety 58 /// The 'program_name' must be some valid pointer to null-terminated string. 59 /// from_c(program_name: *const c_char, module_type: i32) -> FusionResult<Self>60 pub unsafe fn from_c(program_name: *const c_char, module_type: i32) -> FusionResult<Self> 61 { 62 call_debug_enter!("AllocSocketPairParam::from_c"); 63 let cs = unsafe { 64 CStr::from_ptr(program_name) 65 }; 66 match cs.to_str() { 67 Ok(sref) => { 68 Ok(Self { 69 program_name: sref.to_string(), 70 module_type 71 }) 72 } 73 Err(_) => { 74 error!(LOG_LABEL, "Can not convert \'program_name\' from CStr to String"); 75 Err(-1) 76 } 77 } 78 } 79 } 80 81 impl Serialize for AllocSocketPairParam { serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()>82 fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> 83 { 84 info!(LOG_LABEL, "serialize AllocSocketPairParam"); 85 self.program_name.serialize(parcel)?; 86 self.module_type.serialize(parcel)?; 87 Ok(()) 88 } 89 } 90 91 impl Deserialize for AllocSocketPairParam { deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self>92 fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> 93 { 94 let param = Self { 95 program_name: String::deserialize(parcel)?, 96 module_type: i32::deserialize(parcel)?, 97 }; 98 Ok(param) 99 } 100 } 101 102 impl Display for AllocSocketPairParam { fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>103 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> 104 { 105 writeln!(f, "\nAllocSocketPairParam {{")?; 106 writeln!(f, " program_name: {}", self.program_name)?; 107 writeln!(f, " module_type: {}", self.module_type)?; 108 writeln!(f, "}}")?; 109 Ok(()) 110 } 111 } 112