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 #![allow(unused_variables)]
16 
17 //! IPC test server
18 
19 extern crate ipc_rust;
20 extern crate rust_idl_ipc_test_service;
21 
22 use std::ptr;
23 use std::ffi::{c_char, CString};
24 
25 use ipc_rust::{IRemoteBroker, join_work_thread, Result, add_service};
26 use rust_idl_ipc_test_service::{IIdlTestService, IdlTestServiceStub};
27 
28 const IPC_TEST_SERVICE_ID: i32 = 1118;
29 
30 #[repr(C)]
31 struct TokenInfoParams {
32     dcaps_num: i32,
33     perms_num: i32,
34     acls_num: i32,
35     dcaps: *const *const c_char,
36     perms: *const *const c_char,
37     acls: *const *const c_char,
38     process_name: *const c_char,
39     apl_str: *const c_char,
40 }
41 
init_access_token()42 fn init_access_token()
43 {
44     let name = CString::new("com.ipc.test").expect("process name is invalid");
45     let apl = CString::new("normal").expect("apl string is invalid");
46     let mut param = TokenInfoParams {
47         dcaps_num: 0,
48         perms_num: 0,
49         acls_num: 0,
50         dcaps: ptr::null(),
51         perms: ptr::null(),
52         acls: ptr::null(),
53         process_name: name.as_ptr(),
54         apl_str: apl.as_ptr(),
55     };
56 
57     unsafe {
58         let token_id = GetAccessTokenId(&mut param as *mut TokenInfoParams);
59         SetSelfTokenID(token_id);
60     }
61 }
62 
63 extern "C" {
GetAccessTokenId(tokenInfo: *mut TokenInfoParams) -> u6464     fn GetAccessTokenId(tokenInfo: *mut TokenInfoParams) -> u64;
SetSelfTokenID(tokenID: u64) -> i3265     fn SetSelfTokenID(tokenID: u64) -> i32;
66 }
67 
68 /// test.ipc.ITestService type
69 pub struct TestService;
70 
71 impl IIdlTestService for TestService {
idl_ipc_test_basic_001(&self) -> Result<()>72     fn idl_ipc_test_basic_001(&self) -> Result<()> {
73         println!("idl_ipc_test_basic_001 start");
74         Ok(())
75     }
76 
idl_ipc_test_basic_002(&self) -> Result<()>77     fn idl_ipc_test_basic_002(&self) -> Result<()> {
78         println!("idl_ipc_test_basic_002 start");
79         Ok(())
80     }
81 
idl_ipc_test_basic_101(&self, i_bool: &bool) -> Result<bool>82     fn idl_ipc_test_basic_101(&self, i_bool: &bool) -> Result<bool>{
83         println!("idl_ipc_test_basic_101 start -> {}", i_bool);
84         Ok(!i_bool)
85     }
86 
idl_ipc_test_basic_102(&self, i_byte: &i8) -> Result<i8>87     fn idl_ipc_test_basic_102(&self, i_byte: &i8) -> Result<i8>{
88         println!("idl_ipc_test_basic_102 start -> {}", i_byte);
89         Ok(i_byte * 2)
90     }
91 
idl_ipc_test_basic_103(&self, i_short: &i16) -> Result<i16>92     fn idl_ipc_test_basic_103(&self, i_short: &i16) -> Result<i16>{
93         println!("idl_ipc_test_basic_103 start -> {}", i_short);
94         Ok(i_short * 2)
95     }
96 
idl_ipc_test_basic_104(&self, i_int: &i32) -> Result<i32>97     fn idl_ipc_test_basic_104(&self, i_int: &i32) -> Result<i32>{
98         println!("idl_ipc_test_basic_104 start -> {}", i_int);
99         Ok(i_int * 2)
100     }
101 
idl_ipc_test_basic_105(&self, i_long: &i64) -> Result<i64>102     fn idl_ipc_test_basic_105(&self, i_long: &i64) -> Result<i64>{
103         println!("idl_ipc_test_basic_105 start -> {}", i_long);
104         Ok(i_long * 2)
105     }
106 
idl_ipc_test_basic_106(&self, i_float: &f32) -> Result<f32>107     fn idl_ipc_test_basic_106(&self, i_float: &f32) -> Result<f32>{
108         println!("idl_ipc_test_basic_106 start -> {}", i_float);
109         Ok(*i_float * 2.0)
110     }
111 
idl_ipc_test_basic_107(&self, i_double: &f64) -> Result<f64>112     fn idl_ipc_test_basic_107(&self, i_double: &f64) -> Result<f64>{
113         println!("idl_ipc_test_basic_107 start -> {}", i_double);
114         Ok(*i_double * 2.0)
115     }
116 
idl_ipc_test_basic_108(&self, i_string: &str) -> Result<String>117     fn idl_ipc_test_basic_108(&self, i_string: &str) -> Result<String>{
118         println!("idl_ipc_test_basic_108 start -> {}", i_string);
119         let mut result = String::from(i_string);
120         result.push_str(i_string);
121         Ok(result.to_string())
122     }
123 
idl_ipc_test_basic_199(&self, i_bool: &bool, i_long: &i64, i_float: &f32, i_double: &f64, i_string: &str) -> Result<bool>124     fn idl_ipc_test_basic_199(&self, i_bool: &bool, i_long: &i64, i_float: &f32,
125         i_double: &f64, i_string: &str) -> Result<bool>{
126         println!("idl_ipc_test_basic_199 start -> {} - {} - {} - {} - {}", i_bool, i_long, i_float, i_double, i_string);
127         Ok(true)
128     }
129 }
130 
131 impl IRemoteBroker for TestService {}
132 
main()133 fn main() {
134     init_access_token();
135     // create stub
136     let service = IdlTestServiceStub::new_remote_stub(TestService).expect("create TestService success");
137     add_service(&service.as_object().expect("get IIdlTestService service failed"),
138         IPC_TEST_SERVICE_ID).expect("add server to samgr failed");
139     println!("join to ipc work thread");
140     join_work_thread();
141 }
142