• 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 //! IPC service definition.
17 
18 #![allow(unused_variables)]
19 
20 extern crate hilog_rust;
21 extern crate ipc_rust;
22 extern crate fusion_data_rust;
23 extern crate fusion_utils_rust;
24 
25 mod identity;
26 
27 use std::ffi::{ c_char, CString };
28 use hilog_rust::{ info, error, hilog, HiLogLabel, LogType };
29 use crate::ipc_rust::{
30     BorrowedMsgParcel, IRemoteBroker, IRemoteObj, MsgParcel, IMsgParcel,
31     RemoteObj, RemoteStub, define_remote_object
32 };
33 use crate::fusion_data_rust::{ FusionResult, Intention };
34 use crate::fusion_utils_rust::call_debug_enter;
35 use crate::identity::{ CommonAction, compose_param_id, split_action, split_intention, split_param };
36 
37 const LOG_LABEL: HiLogLabel = HiLogLabel {
38     log_type: LogType::LogCore,
39     domain: 0xD002220,
40     tag: "FusionIpcService"
41 };
42 
43 /// SA ID for "ohos.msdp.Idevicestatus"
44 pub const MSDP_DEVICESTATUS_SERVICE_ID: i32 = 2902;
45 
46 /// trait IDeviceStatus
47 pub trait IDeviceStatus: IRemoteBroker {
48     /// TODO:
enable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>49     fn enable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
50     /// TODO:
disable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>51     fn disable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
52     /// TODO:
start(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>53     fn start(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
54     /// TODO:
stop(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>55     fn stop(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
56     /// TODO:
add_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>57     fn add_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
58     /// TODO:
remove_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>59     fn remove_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
60     /// TODO:
set_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>61     fn set_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
62     /// TODO:
get_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>63     fn get_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
64     /// TODO:
control(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>65     fn control(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>;
66 }
67 
on_remote_request(stub: &dyn IDeviceStatus, code: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>68 fn on_remote_request(stub: &dyn IDeviceStatus, code: u32, data: &BorrowedMsgParcel<'_>,
69     reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
70     call_debug_enter!("FusionIpcService::on_remote_request");
71     let intention = split_intention(code)?;
72     let id = split_param(code);
73 
74     match split_action(code)? {
75         CommonAction::Enable => {
76             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.enable()");
77             stub.enable(intention, data, reply)
78         }
79         CommonAction::Disable => {
80             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.disable()");
81             stub.disable(intention, data, reply)
82         }
83         CommonAction::Start => {
84             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.start()");
85             stub.start(intention, data, reply)
86         }
87         CommonAction::Stop => {
88             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.stop()");
89             stub.stop(intention, data, reply)
90         }
91         CommonAction::AddWatch => {
92             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.add_watch()");
93             stub.add_watch(intention, id, data, reply)
94         }
95         CommonAction::RemoveWatch => {
96             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.remove_watch()");
97             stub.remove_watch(intention, id, data, reply)
98         }
99         CommonAction::SetParam => {
100             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.set_param()");
101             stub.set_param(intention, id, data, reply)
102         }
103         CommonAction::GetParam => {
104             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.get_param()");
105             stub.get_param(intention, id, data, reply)
106         }
107         CommonAction::Control => {
108             info!(LOG_LABEL, "in FusionIpcService::on_remote_request(): call stub.control()");
109             stub.control(intention, id, data, reply)
110         }
111     }
112 }
113 
114 define_remote_object!(
115     IDeviceStatus["ohos.msdp.Idevicestatus"] {
116         stub: FusionIpcStub(on_remote_request),
117         proxy: FusionIpcProxy,
118     }
119 );
120 
121 impl FusionIpcProxy {
transfer_data(&self, src: &dyn IMsgParcel, target: &mut dyn IMsgParcel) -> FusionResult<i32>122     fn transfer_data(&self, src: &dyn IMsgParcel, target: &mut dyn IMsgParcel) -> FusionResult<i32>
123     {
124         call_debug_enter!("FusionIpcProxy::transfer_data");
125         let data_size = src.get_data_size();
126         match src.read_buffer(data_size) {
127             Ok(data_vec) => {
128                 if target.write_buffer(data_vec.as_slice()) {
129                     Ok(0)
130                 } else {
131                     error!(LOG_LABEL, "write_buffer() failed");
132                     Err(-1)
133                 }
134             }
135             Err(_) => {
136                 error!(LOG_LABEL, "read_buffer() failed");
137                 Err(-1)
138             }
139         }
140     }
141 
send_request(&self, action: CommonAction, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>142     fn send_request(&self, action: CommonAction, intention: Intention, id: u32,
143         data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>
144     {
145         call_debug_enter!("FusionIpcProxy::send_request");
146         match MsgParcel::new() {
147             Some(mut data_parcel) => {
148                 self.transfer_data(data, &mut data_parcel)?;
149                 let code = compose_param_id(action, intention, id);
150                 let rep = {
151                     match self.remote.send_request(code, &data_parcel, false) {
152                         Ok(tr) => {
153                             tr
154                         }
155                         Err(_) => {
156                             error!(LOG_LABEL, "Fail to send request");
157                             return Err(-1);
158                         }
159                     }
160                 };
161 
162                 self.transfer_data(&rep, reply)
163             }
164             None => {
165                 error!(LOG_LABEL, "Can not deref data");
166                 Err(-1)
167             }
168         }
169     }
170 }
171 
172 impl IDeviceStatus for FusionIpcProxy {
enable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>173     fn enable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>,
174         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
175         call_debug_enter!("FusionIpcProxy::enable");
176         self.send_request(CommonAction::Enable, intention, 0u32, data, reply)
177     }
178 
disable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>179     fn disable(&self, intention: Intention, data: &BorrowedMsgParcel<'_>,
180         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
181         call_debug_enter!("FusionIpcProxy::disable");
182         self.send_request(CommonAction::Disable, intention, 0u32, data, reply)
183     }
184 
start(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>185     fn start(&self, intention: Intention, data: &BorrowedMsgParcel<'_>,
186         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
187         call_debug_enter!("FusionIpcProxy::start");
188         self.send_request(CommonAction::Start, intention, 0u32, data, reply)
189     }
190 
stop(&self, intention: Intention, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>191     fn stop(&self, intention: Intention, data: &BorrowedMsgParcel<'_>,
192         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
193         call_debug_enter!("FusionIpcProxy::stop");
194         self.send_request(CommonAction::Stop, intention, 0u32, data, reply)
195     }
196 
add_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>197     fn add_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>,
198         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
199         call_debug_enter!("FusionIpcProxy::add_watch");
200         self.send_request(CommonAction::AddWatch, intention, id, data, reply)
201     }
202 
remove_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>203     fn remove_watch(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>,
204         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
205         call_debug_enter!("FusionIpcProxy::remove_watch");
206         self.send_request(CommonAction::RemoveWatch, intention, id, data, reply)
207     }
208 
set_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>209     fn set_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>,
210         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
211         call_debug_enter!("FusionIpcProxy::set_param");
212         self.send_request(CommonAction::SetParam, intention, id, data, reply)
213     }
214 
get_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>215     fn get_param(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>,
216         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
217         call_debug_enter!("FusionIpcProxy::get_param");
218         self.send_request(CommonAction::GetParam, intention, id, data, reply)
219     }
220 
control(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>, reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32>221     fn control(&self, intention: Intention, id: u32, data: &BorrowedMsgParcel<'_>,
222         reply: &mut BorrowedMsgParcel<'_>) -> FusionResult<i32> {
223         call_debug_enter!("FusionIpcProxy::control");
224         self.send_request(CommonAction::Control, intention, id, data, reply)
225     }
226 }
227