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