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