• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //! fusion IPC client.
17 
18 #![allow(dead_code)]
19 #![allow(unused_variables)]
20 
21 extern crate ipc_rust;
22 extern crate hilog_rust;
23 extern crate fusion_data_rust;
24 extern crate fusion_utils_rust;
25 extern crate fusion_ipc_service_rust;
26 
27 use ipc_rust::{
28     BorrowedMsgParcel, FromRemoteObj, InterfaceToken, MsgParcel,
29     RemoteObjRef, Serialize, get_service,
30 };
31 
32 use std::ffi::{ c_char, CString };
33 use hilog_rust::{ info, error, hilog, HiLogLabel, LogType };
34 use fusion_data_rust::{ FusionResult, Intention };
35 use fusion_utils_rust::call_debug_enter;
36 use fusion_ipc_service_rust::{ IDeviceStatus, FusionIpcProxy, MSDP_DEVICESTATUS_SERVICE_ID };
37 
38 const LOG_LABEL: HiLogLabel = HiLogLabel {
39     log_type: LogType::LogCore,
40     domain: 0xD002220,
41     tag: "FusionIpcClient"
42 };
43 
44 /// struct FusionIpcClient
45 pub struct FusionIpcClient(RemoteObjRef<dyn IDeviceStatus>);
46 
47 impl FusionIpcClient {
48     /// TODO: add documentation.
connect() -> FusionResult<Self>49     pub fn connect() -> FusionResult<Self> {
50         call_debug_enter!("FusionIpcClient::connect");
51         match get_service(MSDP_DEVICESTATUS_SERVICE_ID) {
52             Ok(obj) => {
53                 match <dyn IDeviceStatus as FromRemoteObj>::try_from(obj) {
54                     Ok(obj_ref) => {
55                         info!(LOG_LABEL, "in FusionIpcClient::connect(): Connect to service successfully");
56                         Ok(FusionIpcClient(obj_ref))
57                     }
58                     Err(err) => {
59                         error!(LOG_LABEL, "in FusionIpcClient::connect(): Can not dereference remote object");
60                         Err(-1)
61                     }
62                 }
63             }
64             Err(err) => {
65                 error!(LOG_LABEL, "in FusionIpcClient::connect(): Can not connect to service");
66                 Err(-1)
67             }
68         }
69     }
70 
add_interface_token(&self, data_parcel: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>71     fn add_interface_token(&self, data_parcel: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
72         call_debug_enter!("FusionIpcClient::add_interface_token");
73         let token = InterfaceToken::new(FusionIpcProxy::get_descriptor());
74         match token.serialize(data_parcel) {
75             Ok(_) => {
76                 Ok(0)
77             }
78             Err(_) => {
79                 error!(LOG_LABEL, "Fail to serialize interface token");
80                 Err(-1)
81             }
82         }
83     }
84 
85     /// TODO: add documentation.
enable(&self, intention: Intention, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>86     pub fn enable(&self, intention: Intention, data: &dyn Serialize,
87         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
88         call_debug_enter!("FusionIpcClient::enable");
89         match MsgParcel::new() {
90             Some(mut data_parcel) => {
91                 let mut borrowed_data_parcel = data_parcel.borrowed();
92                 info!(LOG_LABEL, "Serialize interface token");
93                 self.add_interface_token(&mut borrowed_data_parcel)?;
94 
95                 if data.serialize(&mut borrowed_data_parcel).is_err() {
96                     return Err(-1);
97                 }
98                 info!(LOG_LABEL, "Call proxy.enable()");
99                 self.0.enable(intention, &borrowed_data_parcel, reply)
100             }
101             None => {
102                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
103                 Err(-1)
104             }
105         }
106     }
107 
108     /// TODO: add documentation.
disable(&self, intention: Intention, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>109     pub fn disable(&self, intention: Intention, data: &dyn Serialize,
110         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
111         call_debug_enter!("FusionIpcClient::disable");
112         match MsgParcel::new() {
113             Some(mut data_parcel) => {
114                 let mut borrowed_data_parcel = data_parcel.borrowed();
115                 info!(LOG_LABEL, "Serialize interface token");
116                 self.add_interface_token(&mut borrowed_data_parcel)?;
117 
118                 if data.serialize(&mut borrowed_data_parcel).is_err() {
119                     return Err(-1);
120                 }
121                 info!(LOG_LABEL, "Call proxy.disable()");
122                 self.0.disable(intention, &borrowed_data_parcel, reply)
123             }
124             None => {
125                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
126                 Err(-1)
127             }
128         }
129     }
130 
131     /// TODO: add documentation.
start(&self, intention: Intention, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>132     pub fn start(&self, intention: Intention, data: &dyn Serialize,
133         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
134         call_debug_enter!("FusionIpcClient::start");
135         match MsgParcel::new() {
136             Some(mut data_parcel) => {
137                 let mut borrowed_data_parcel = data_parcel.borrowed();
138                 info!(LOG_LABEL, "Serialize interface token");
139                 self.add_interface_token(&mut borrowed_data_parcel)?;
140 
141                 if data.serialize(&mut borrowed_data_parcel).is_err() {
142                     return Err(-1);
143                 }
144                 info!(LOG_LABEL, "Call proxy.start()");
145                 self.0.start(intention, &borrowed_data_parcel, reply)
146             }
147             None => {
148                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
149                 Err(-1)
150             }
151         }
152     }
153 
154     /// TODO: add documentation.
stop(&self, intention: Intention, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>155     pub fn stop(&self, intention: Intention, data: &dyn Serialize,
156         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
157         call_debug_enter!("FusionIpcClient::stop");
158         match MsgParcel::new() {
159             Some(mut data_parcel) => {
160                 let mut borrowed_data_parcel = data_parcel.borrowed();
161                 info!(LOG_LABEL, "Serialize interface token");
162                 self.add_interface_token(&mut borrowed_data_parcel)?;
163 
164                 if data.serialize(&mut borrowed_data_parcel).is_err() {
165                     return Err(-1);
166                 }
167                 info!(LOG_LABEL, "Call proxy.stop()");
168                 self.0.stop(intention, &borrowed_data_parcel, reply)
169             }
170             None => {
171                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
172                 Err(-1)
173             }
174         }
175     }
176 
177     /// TODO: add documentation.
add_watch(&self, intention: Intention, id: u32, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>178     pub fn add_watch(&self, intention: Intention, id: u32, data: &dyn Serialize,
179         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
180         call_debug_enter!("FusionIpcClient::add_watch");
181         match MsgParcel::new() {
182             Some(mut data_parcel) => {
183                 let mut borrowed_data_parcel = data_parcel.borrowed();
184                 info!(LOG_LABEL, "Serialize interface token");
185                 self.add_interface_token(&mut borrowed_data_parcel)?;
186 
187                 if data.serialize(&mut borrowed_data_parcel).is_err() {
188                     return Err(-1);
189                 }
190                 info!(LOG_LABEL, "Call proxy.add_watch()");
191                 self.0.add_watch(intention, id, &borrowed_data_parcel, reply)
192             }
193             None => {
194                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
195                 Err(-1)
196             }
197         }
198     }
199 
200     /// TODO: add documentation.
remove_watch(&self, intention: Intention, id: u32, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>201     pub fn remove_watch(&self, intention: Intention, id: u32, data: &dyn Serialize,
202         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
203         call_debug_enter!("FusionIpcClient::remove_watch");
204         match MsgParcel::new() {
205             Some(mut data_parcel) => {
206                 let mut borrowed_data_parcel = data_parcel.borrowed();
207                 info!(LOG_LABEL, "Serialize interface token");
208                 self.add_interface_token(&mut borrowed_data_parcel)?;
209 
210                 if data.serialize(&mut borrowed_data_parcel).is_err() {
211                     return Err(-1);
212                 }
213                 info!(LOG_LABEL, "Call proxy.remove_watch()");
214                 self.0.remove_watch(intention, id, &borrowed_data_parcel, reply)
215             }
216             None => {
217                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
218                 Err(-1)
219             }
220         }
221     }
222 
223     /// TODO: add documentation.
set_param(&self, intention: Intention, id: u32, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>224     pub fn set_param(&self, intention: Intention, id: u32, data: &dyn Serialize,
225         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
226         call_debug_enter!("FusionIpcClient::set_param");
227         match MsgParcel::new() {
228             Some(mut data_parcel) => {
229                 let mut borrowed_data_parcel = data_parcel.borrowed();
230                 info!(LOG_LABEL, "Serialize interface token");
231                 self.add_interface_token(&mut borrowed_data_parcel)?;
232 
233                 if data.serialize(&mut borrowed_data_parcel).is_err() {
234                     return Err(-1);
235                 }
236                 info!(LOG_LABEL, "Call proxy.set_param()");
237                 self.0.set_param(intention, id, &borrowed_data_parcel, reply)
238             }
239             None => {
240                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
241                 Err(-1)
242             }
243         }
244     }
245 
246     /// TODO: add documentation.
get_param(&self, intention: Intention, id: u32, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>247     pub fn get_param(&self, intention: Intention, id: u32, data: &dyn Serialize,
248         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
249         call_debug_enter!("FusionIpcClient::get_param");
250         match MsgParcel::new() {
251             Some(mut data_parcel) => {
252                 let mut borrowed_data_parcel = data_parcel.borrowed();
253                 info!(LOG_LABEL, "Serialize interface token");
254                 self.add_interface_token(&mut borrowed_data_parcel)?;
255 
256                 if data.serialize(&mut borrowed_data_parcel).is_err() {
257                     return Err(-1);
258                 }
259                 info!(LOG_LABEL, "Call proxy.get_param()");
260                 self.0.get_param(intention, id, &borrowed_data_parcel, reply)
261             }
262             None => {
263                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
264                 Err(-1)
265             }
266         }
267     }
268 
269     /// TODO: add documentation.
control(&self, intention: Intention, id: u32, data: &dyn Serialize, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>270     pub fn control(&self, intention: Intention, id: u32, data: &dyn Serialize,
271         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
272         call_debug_enter!("FusionIpcClient::control");
273         match MsgParcel::new() {
274             Some(mut data_parcel) => {
275                 let mut borrowed_data_parcel = data_parcel.borrowed();
276                 info!(LOG_LABEL, "Serialize interface token");
277                 self.add_interface_token(&mut borrowed_data_parcel)?;
278 
279                 if data.serialize(&mut borrowed_data_parcel).is_err() {
280                     return Err(-1);
281                 }
282                 info!(LOG_LABEL, "Call proxy.control()");
283                 self.0.control(intention, id, &borrowed_data_parcel, reply)
284             }
285             None => {
286                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
287                 Err(-1)
288             }
289         }
290     }
291 }
292