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 //! Proxy for multi-device cooperation.
17 
18 #![allow(dead_code)]
19 #![allow(unused_variables)]
20 
21 use std::ffi::{ c_char, CString };
22 
23 use hilog_rust::{ debug, error, hilog, HiLogLabel, LogType };
24 use ipc_rust::{ MsgParcel, Deserialize };
25 
26 use fusion_data_rust::{
27     Intention, DefaultReply, GeneralCoordinationParam, StartCoordinationParam,
28     StopCoordinationParam, GetCoordinationStateParam
29 };
30 use fusion_utils_rust::{ call_debug_enter, FusionResult, FusionErrorCode };
31 use fusion_ipc_client_rust::FusionIpcClient;
32 
33 const LOG_LABEL: HiLogLabel = HiLogLabel {
34     log_type: LogType::LogCore,
35     domain: 0xD002220,
36     tag: "FusionCoordinationClient"
37 };
38 
39 /// Definition of proxy for multi-device cooperation.
40 #[derive(Default)]
41 pub struct FusionCoordinationClient(i32);
42 
43 impl FusionCoordinationClient {
44     /// Request to enable multi-device cooperation.
enable_coordination(&self, user_data: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>45     pub fn enable_coordination(&self, user_data: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>
46     {
47         call_debug_enter!("FusionCoordinationClient::enable_coordination");
48         match MsgParcel::new() {
49             Some(mut reply_parcel) => {
50                 let param = GeneralCoordinationParam {
51                     user_data
52                 };
53                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
54                 debug!(LOG_LABEL, "Call ipc_client::enable()");
55                 ipc_client.enable(Intention::Coordination, &param, &mut borrowed_reply_parcel)
56             }
57             None => {
58                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
59                 Err(FusionErrorCode::Fail)
60             }
61         }
62     }
63 
64     /// Request to disable multi-device cooperation.
disable_coordination(&self, user_data: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>65     pub fn disable_coordination(&self, user_data: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>
66     {
67         call_debug_enter!("FusionCoordinationClient::disable_coordination");
68         match MsgParcel::new() {
69             Some(mut reply_parcel) => {
70                 let param = GeneralCoordinationParam {
71                     user_data
72                 };
73                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
74                 debug!(LOG_LABEL, "Call ipc_client::disable()");
75                 ipc_client.disable(Intention::Coordination, &param, &mut borrowed_reply_parcel)
76             }
77             None => {
78                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
79                 Err(FusionErrorCode::Fail)
80             }
81         }
82     }
83 
84     /// Request to start multi-device cooperation.
start_coordination(&self, user_data: i32, remote_network_id: &str, start_device_id: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>85     pub fn start_coordination(&self, user_data: i32, remote_network_id: &str,
86         start_device_id: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>
87     {
88         call_debug_enter!("FusionCoordinationClient::start_coordination");
89         match MsgParcel::new() {
90             Some(mut reply_parcel) => {
91                 let param = StartCoordinationParam {
92                     user_data,
93                     remote_network_id: remote_network_id.to_string(),
94                     start_device_id,
95                 };
96                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
97                 debug!(LOG_LABEL, "Call ipc_client::start()");
98                 ipc_client.start(Intention::Coordination, &param, &mut borrowed_reply_parcel)
99             }
100             None => {
101                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
102                 Err(FusionErrorCode::Fail)
103             }
104         }
105     }
106 
107     /// Request to stop multi-device cooperation.
stop_coordination(&self, user_data: i32, is_unchained: i32, ipc_client: &FusionIpcClient) -> FusionResult<()>108     pub fn stop_coordination(&self, user_data: i32, is_unchained: i32,
109         ipc_client: &FusionIpcClient) -> FusionResult<()>
110     {
111         call_debug_enter!("FusionCoordinationClient::stop_coordination");
112         match MsgParcel::new() {
113             Some(mut reply_parcel) => {
114                 let param = StopCoordinationParam {
115                     user_data,
116                     is_unchained,
117                 };
118                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
119                 debug!(LOG_LABEL, "Call ipc_client::stop()");
120                 ipc_client.stop(Intention::Coordination, &param, &mut borrowed_reply_parcel)
121             }
122             None => {
123                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
124                 Err(FusionErrorCode::Fail)
125             }
126         }
127     }
128 
129     /// Request for current switch status of multi-device cooperation.
get_coordination_state(&self, user_data: i32, device_id: &str, ipc_client: &FusionIpcClient) -> FusionResult<i32>130     pub fn get_coordination_state(&self, user_data: i32, device_id: &str,
131         ipc_client: &FusionIpcClient) -> FusionResult<i32>
132     {
133         call_debug_enter!("FusionCoordinationClient::get_coordination_state");
134         match MsgParcel::new() {
135             Some(mut reply_parcel) => {
136                 let param = GetCoordinationStateParam {
137                     user_data,
138                     device_id: device_id.to_string(),
139                 };
140                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
141                 debug!(LOG_LABEL, "Call ipc_client::get_param()");
142                 ipc_client.get_param(Intention::Coordination, 0u32, &param, &mut borrowed_reply_parcel)?;
143 
144                 match DefaultReply::deserialize(&borrowed_reply_parcel) {
145                     Ok(x) => {
146                         Ok(x.reply)
147                     }
148                     Err(_) => {
149                         error!(LOG_LABEL, "Failed to deserialize DefaultReply");
150                         Err(FusionErrorCode::Fail)
151                     }
152                 }
153             }
154             None => {
155                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
156                 Err(FusionErrorCode::Fail)
157             }
158         }
159     }
160 
161     /// Request to listen for events of multi-device cooperation.
register_coordination_listener(&self, ipc_client: &FusionIpcClient) -> FusionResult<()>162     pub fn register_coordination_listener(&self, ipc_client: &FusionIpcClient) -> FusionResult<()>
163     {
164         call_debug_enter!("FusionCoordinationClient::register_coordination_listener");
165         match MsgParcel::new() {
166             Some(mut reply_parcel) => {
167                 let param = GeneralCoordinationParam::default();
168                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
169                 debug!(LOG_LABEL, "Call ipc_client::add_watch()");
170                 ipc_client.add_watch(Intention::Coordination, 0u32, &param, &mut borrowed_reply_parcel)
171             }
172             None => {
173                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
174                 Err(FusionErrorCode::Fail)
175             }
176         }
177     }
178 
179     /// Request to stop listening for events of multi-device cooperation.
unregister_coordination_listener(&self, ipc_client: &FusionIpcClient) -> FusionResult<()>180     pub fn unregister_coordination_listener(&self, ipc_client: &FusionIpcClient) -> FusionResult<()>
181     {
182         call_debug_enter!("FusionCoordinationClient::unregister_coordination_listener");
183         match MsgParcel::new() {
184             Some(mut reply_parcel) => {
185                 let param = GeneralCoordinationParam::default();
186                 let mut borrowed_reply_parcel = reply_parcel.borrowed();
187                 debug!(LOG_LABEL, "Call ipc_client::remove_watch()");
188                 ipc_client.remove_watch(Intention::Coordination, 0u32, &param, &mut borrowed_reply_parcel)
189             }
190             None => {
191                 error!(LOG_LABEL, "Can not instantiate MsgParcel");
192                 Err(FusionErrorCode::Fail)
193             }
194         }
195     }
196 }
197