1 /*
2 * Copyright (c) 2023-2024 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 #include "distributed_file_daemon_proxy.h"
17
18 #include <sstream>
19
20 #include "dfs_error.h"
21 #include "ipc/distributed_file_daemon_ipc_interface_code.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "utils_log.h"
25
26 #undef LOG_DOMAIN
27 #undef LOG_TAG
28 #define LOG_DOMAIN 0xD001600
29 #define LOG_TAG "distributedfile_daemon"
30
31 namespace OHOS {
32 namespace Storage {
33 namespace DistributedFile {
34 using namespace std;
35 using namespace OHOS::Storage;
36
OnRemoteSaDied(const wptr<IRemoteObject> & remote)37 void DistributedFileDaemonProxy::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
38 {
39 LOGI("dfs service death");
40 unique_lock<mutex> lock(proxyMutex_);
41 daemonProxy_ = nullptr;
42 }
43
SetDeathRecipient(RemoteDiedHandler handler)44 void DistributedFileDaemonProxy::DaemonDeathRecipient::SetDeathRecipient(RemoteDiedHandler handler)
45 {
46 handler_ = std::move(handler);
47 }
48
OnRemoteDied(const wptr<IRemoteObject> & remote)49 void DistributedFileDaemonProxy::DaemonDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
50 {
51 LOGI("start");
52 if (handler_ != nullptr) {
53 handler_(remote);
54 }
55 }
56
GetInstance()57 sptr<IDaemon> DistributedFileDaemonProxy::GetInstance()
58 {
59 LOGI("getinstance");
60 unique_lock<mutex> lock(proxyMutex_);
61 if (daemonProxy_ != nullptr) {
62 return daemonProxy_;
63 }
64
65 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
66 if (samgr == nullptr) {
67 LOGE("Samgr is nullptr");
68 return nullptr;
69 }
70
71 auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID);
72 if (object == nullptr) {
73 LOGE("object == nullptr");
74 return nullptr;
75 }
76
77 if (deathRecipient_ == nullptr) {
78 deathRecipient_ = new (std::nothrow) DaemonDeathRecipient();
79 if (deathRecipient_ == nullptr) {
80 LOGE("new death recipient failed");
81 return nullptr;
82 }
83 }
84 deathRecipient_->SetDeathRecipient([](const wptr<IRemoteObject> &remote) { OnRemoteSaDied(remote); });
85 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
86 LOGE("failed to add death recipient.");
87 return nullptr;
88 }
89
90 daemonProxy_ = iface_cast<IDaemon>(object);
91 if (daemonProxy_ == nullptr) {
92 LOGE("service == nullptr");
93 return nullptr;
94 }
95 return daemonProxy_;
96 }
97
OpenP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)98 int32_t DistributedFileDaemonProxy::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
99 {
100 LOGI("Open p2p connection");
101 MessageParcel data;
102 MessageParcel reply;
103 MessageOption option;
104 if (!data.WriteInterfaceToken(GetDescriptor())) {
105 LOGE("Failed to write interface token");
106 return OHOS::FileManagement::E_BROKEN_IPC;
107 }
108 if (!data.WriteCString(deviceInfo.deviceId)) {
109 LOGE("Failed to send device id");
110 return OHOS::FileManagement::E_INVAL_ARG;
111 }
112 if (!data.WriteCString(deviceInfo.deviceName)) {
113 LOGE("Failed to send device name");
114 return OHOS::FileManagement::E_INVAL_ARG;
115 }
116 if (!data.WriteCString(deviceInfo.networkId)) {
117 LOGE("Failed to send network id");
118 return OHOS::FileManagement::E_INVAL_ARG;
119 }
120 if (!data.WriteUint16(deviceInfo.deviceTypeId)) {
121 LOGE("Failed to send deviceTypeId");
122 return OHOS::FileManagement::E_INVAL_ARG;
123 }
124 if (!data.WriteUint32(deviceInfo.range)) {
125 LOGE("Failed to send range");
126 return OHOS::FileManagement::E_INVAL_ARG;
127 }
128 if (!data.WriteInt32(deviceInfo.authForm)) {
129 LOGE("Failed to send user id");
130 return OHOS::FileManagement::E_INVAL_ARG;
131 }
132 auto remote = Remote();
133 if (!remote) {
134 LOGE("remote is nullptr");
135 return OHOS::FileManagement::E_BROKEN_IPC;
136 }
137 int32_t ret = remote->SendRequest(
138 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION),
139 data, reply, option);
140 if (ret != 0) {
141 stringstream ss;
142 ss << "Failed to send out the requeset, errno:" << ret;
143 LOGE("%{public}s", ss.str().c_str());
144 return OHOS::FileManagement::E_BROKEN_IPC;
145 }
146 LOGI("Open p2p connection Success");
147 return reply.ReadInt32();
148 }
149
CloseP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)150 int32_t DistributedFileDaemonProxy::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
151 {
152 LOGI("Close p2p connection");
153 MessageParcel data;
154 MessageParcel reply;
155 MessageOption option;
156 if (!data.WriteInterfaceToken(GetDescriptor())) {
157 LOGE("Failed to write interface token");
158 return OHOS::FileManagement::E_BROKEN_IPC;
159 }
160 if (!data.WriteCString(deviceInfo.deviceId)) {
161 LOGE("Failed to send device id");
162 return OHOS::FileManagement::E_INVAL_ARG;
163 }
164 if (!data.WriteCString(deviceInfo.deviceName)) {
165 LOGE("Failed to send device name");
166 return OHOS::FileManagement::E_INVAL_ARG;
167 }
168 if (!data.WriteCString(deviceInfo.networkId)) {
169 LOGE("Failed to send network id");
170 return OHOS::FileManagement::E_INVAL_ARG;
171 }
172 if (!data.WriteUint16(deviceInfo.deviceTypeId)) {
173 LOGE("Failed to send deviceTypeId");
174 return OHOS::FileManagement::E_INVAL_ARG;
175 }
176 if (!data.WriteUint32(deviceInfo.range)) {
177 LOGE("Failed to send range");
178 return OHOS::FileManagement::E_INVAL_ARG;
179 }
180 if (!data.WriteInt32(deviceInfo.authForm)) {
181 LOGE("Failed to send user id");
182 return OHOS::FileManagement::E_INVAL_ARG;
183 }
184 auto remote = Remote();
185 if (!remote) {
186 LOGE("remote is nullptr");
187 return OHOS::FileManagement::E_BROKEN_IPC;
188 }
189 int32_t ret = remote->SendRequest(
190 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION),
191 data, reply, option);
192 if (ret != 0) {
193 stringstream ss;
194 ss << "Failed to send out the requeset, errno:" << ret;
195 LOGE("%{public}s", ss.str().c_str());
196 return OHOS::FileManagement::E_BROKEN_IPC;
197 }
198 LOGI("Close p2p connection Success");
199 return reply.ReadInt32();
200 }
201
OpenP2PConnectionEx(const std::string & networkId,sptr<IFileDfsListener> remoteReverseObj)202 int32_t DistributedFileDaemonProxy::OpenP2PConnectionEx(const std::string &networkId,
203 sptr<IFileDfsListener> remoteReverseObj)
204 {
205 LOGI("DistributedFileDaemonProxy::OpenP2PConnectionEx start.");
206 MessageParcel data;
207 MessageParcel reply;
208 MessageOption option;
209 if (!data.WriteInterfaceToken(GetDescriptor())) {
210 LOGE("Failed to write interface token.");
211 return OHOS::FileManagement::E_BROKEN_IPC;
212 }
213 if (!data.WriteString(networkId)) {
214 LOGE("Failed to send network id.");
215 return OHOS::FileManagement::E_INVAL_ARG;
216 }
217 if (remoteReverseObj == nullptr) {
218 LOGE("remoteReverseObj is nullptr.");
219 return OHOS::FileManagement::E_BROKEN_IPC;
220 }
221 if (!data.WriteRemoteObject(remoteReverseObj->AsObject())) {
222 LOGE("fail to WriteRemoteObject remoteReverseObj");
223 return OHOS::FileManagement::E_BROKEN_IPC;
224 }
225
226 auto remote = Remote();
227 if (!remote) {
228 LOGE("remote is nullptr.");
229 return OHOS::FileManagement::E_BROKEN_IPC;
230 }
231 int32_t ret = remote->SendRequest(
232 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION_EX),
233 data, reply, option);
234 if (ret != 0) {
235 LOGE("SendRequest failed, ret = %{public}d", ret);
236 return OHOS::FileManagement::E_BROKEN_IPC;
237 }
238 LOGI("DistributedFileDaemonProxy::OpenP2PConnectionEx success.");
239 return reply.ReadInt32();
240 }
241
CloseP2PConnectionEx(const std::string & networkId)242 int32_t DistributedFileDaemonProxy::CloseP2PConnectionEx(const std::string &networkId)
243 {
244 LOGI("Close p2p connection");
245 MessageParcel data;
246 MessageParcel reply;
247 MessageOption option;
248 if (!data.WriteInterfaceToken(GetDescriptor())) {
249 LOGE("Failed to write interface token.");
250 return OHOS::FileManagement::E_BROKEN_IPC;
251 }
252 if (!data.WriteString(networkId)) {
253 LOGE("Failed to send device id.");
254 return OHOS::FileManagement::E_INVAL_ARG;
255 }
256 auto remote = Remote();
257 if (!remote) {
258 LOGE("remote is nullptr.");
259 return OHOS::FileManagement::E_BROKEN_IPC;
260 }
261 int32_t ret = remote->SendRequest(
262 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION_EX),
263 data, reply, option);
264 if (ret != 0) {
265 LOGE("SendRequest failed, ret = %{public}d", ret);
266 return OHOS::FileManagement::E_BROKEN_IPC;
267 }
268 LOGI("DistributedFileDaemonProxy::Close p2p connection Success");
269 return reply.ReadInt32();
270 }
271
PrepareSession(const std::string & srcUri,const std::string & dstUri,const std::string & srcDeviceId,const sptr<IRemoteObject> & listener,HmdfsInfo & info)272 int32_t DistributedFileDaemonProxy::PrepareSession(const std::string &srcUri,
273 const std::string &dstUri,
274 const std::string &srcDeviceId,
275 const sptr<IRemoteObject> &listener,
276 HmdfsInfo &info)
277 {
278 MessageParcel data;
279 MessageParcel reply;
280 MessageOption option;
281 if (!data.WriteInterfaceToken(GetDescriptor())) {
282 LOGE("Failed to write interface token");
283 return OHOS::FileManagement::E_BROKEN_IPC;
284 }
285 if (!data.WriteString(srcUri)) {
286 LOGE("Failed to send srcUri");
287 return OHOS::FileManagement::E_INVAL_ARG;
288 }
289 if (!data.WriteString(dstUri)) {
290 LOGE("Failed to send dstUri");
291 return OHOS::FileManagement::E_INVAL_ARG;
292 }
293 if (!data.WriteString(srcDeviceId)) {
294 LOGE("Failed to send remoteDeviceId");
295 return OHOS::FileManagement::E_INVAL_ARG;
296 }
297 if (!data.WriteRemoteObject(listener)) {
298 LOGE("Failed to send the listener callback stub");
299 return OHOS::FileManagement::E_INVAL_ARG;
300 }
301 if (!data.WriteString(info.copyPath)) {
302 LOGE("Failed to send info.copyPath");
303 return OHOS::FileManagement::E_INVAL_ARG;
304 }
305 if (!data.WriteBool(info.dirExistFlag)) {
306 LOGE("Failed to send info.dirExistFlag");
307 return OHOS::FileManagement::E_INVAL_ARG;
308 }
309 auto remote = Remote();
310 if (remote == nullptr) {
311 LOGE("remote is nullptr");
312 return OHOS::FileManagement::E_BROKEN_IPC;
313 }
314 int32_t ret =
315 remote->SendRequest(static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PREPARE_SESSION),
316 data, reply, option);
317 if (!reply.ReadString(info.sessionName)) {
318 LOGE("Failed to receive info.sessionName");
319 return OHOS::FileManagement::E_INVAL_ARG;
320 }
321 if (ret != 0) {
322 LOGE("SendRequest failed, ret = %{public}d", ret);
323 return OHOS::FileManagement::E_BROKEN_IPC;
324 }
325 return reply.ReadInt32();
326 }
327
RequestSendFile(const std::string & srcUri,const std::string & dstPath,const std::string & remoteDeviceId,const std::string & sessionName)328 int32_t DistributedFileDaemonProxy::RequestSendFile(const std::string &srcUri,
329 const std::string &dstPath,
330 const std::string &remoteDeviceId,
331 const std::string &sessionName)
332 {
333 MessageParcel data;
334 MessageParcel reply;
335 MessageOption option;
336 if (!data.WriteInterfaceToken(GetDescriptor())) {
337 LOGE("Failed to write interface token");
338 return OHOS::FileManagement::E_BROKEN_IPC;
339 }
340 if (!data.WriteString(srcUri)) {
341 LOGE("Failed to send srcUri");
342 return OHOS::FileManagement::E_INVAL_ARG;
343 }
344 if (!data.WriteString(dstPath)) {
345 LOGE("Failed to send dstPath");
346 return OHOS::FileManagement::E_INVAL_ARG;
347 }
348 if (!data.WriteString(remoteDeviceId)) {
349 LOGE("Failed to send remoteDeviceId");
350 return OHOS::FileManagement::E_INVAL_ARG;
351 }
352 if (!data.WriteString(sessionName)) {
353 LOGE("Failed to send sessionName");
354 return OHOS::FileManagement::E_INVAL_ARG;
355 }
356 auto remote = Remote();
357 if (remote == nullptr) {
358 LOGE("remote is nullptr");
359 return OHOS::FileManagement::E_BROKEN_IPC;
360 }
361 int32_t ret = remote->SendRequest(
362 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE), data, reply,
363 option);
364 if (ret != 0) {
365 LOGE("SendRequest failed, ret = %{public}d", ret);
366 return OHOS::FileManagement::E_BROKEN_IPC;
367 }
368 return reply.ReadInt32();
369 }
370
GetRemoteCopyInfo(const std::string & srcUri,bool & isFile,bool & isDir)371 int32_t DistributedFileDaemonProxy::GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir)
372 {
373 MessageParcel data;
374 MessageParcel reply;
375 MessageOption option;
376 if (!data.WriteInterfaceToken(GetDescriptor())) {
377 LOGE("Failed to write interface token");
378 return OHOS::FileManagement::E_BROKEN_IPC;
379 }
380 if (!data.WriteString(srcUri)) {
381 LOGE("Failed to send srcUri");
382 return OHOS::FileManagement::E_INVAL_ARG;
383 }
384 auto remote = Remote();
385 if (remote == nullptr) {
386 LOGE("remote is nullptr");
387 return OHOS::FileManagement::E_BROKEN_IPC;
388 }
389 auto ret = remote->SendRequest(
390 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO), data, reply,
391 option);
392 if (ret != 0) {
393 LOGE("SendRequest failed, ret = %{public}d", ret);
394 return OHOS::FileManagement::E_BROKEN_IPC;
395 }
396 if (!reply.ReadBool(isFile)) {
397 LOGE("read isFile failed");
398 return OHOS::FileManagement::E_INVAL_ARG;
399 }
400 if (!reply.ReadBool(isDir)) {
401 LOGE("read isDir failed");
402 return OHOS::FileManagement::E_INVAL_ARG;
403 }
404 if (!reply.ReadInt32(ret)) {
405 LOGE("read res failed");
406 return OHOS::FileManagement::E_INVAL_ARG;
407 }
408 return ret;
409 }
410
CancelCopyTask(const std::string & sessionName)411 int32_t DistributedFileDaemonProxy::CancelCopyTask(const std::string &sessionName)
412 {
413 MessageParcel data;
414 MessageParcel reply;
415 MessageOption option;
416 if (!data.WriteInterfaceToken(GetDescriptor())) {
417 LOGE("Failed to write interface token");
418 return OHOS::FileManagement::E_BROKEN_IPC;
419 }
420 if (!data.WriteString(sessionName)) {
421 LOGE("Failed to send sessionName");
422 return OHOS::FileManagement::E_INVAL_ARG;
423 }
424 auto remote = Remote();
425 if (remote == nullptr) {
426 LOGE("remote is nullptr");
427 return OHOS::FileManagement::E_BROKEN_IPC;
428 }
429 int32_t ret = remote->SendRequest(
430 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CANCEL_COPY_TASK), data, reply,
431 option);
432 if (ret != 0) {
433 LOGE("SendRequest failed, ret = %{public}d", ret);
434 return OHOS::FileManagement::E_BROKEN_IPC;
435 }
436 return reply.ReadInt32();
437 }
438
PushAsset(int32_t userId,const sptr<AssetObj> & assetObj,const sptr<IAssetSendCallback> & sendCallback)439 int32_t DistributedFileDaemonProxy::PushAsset(int32_t userId,
440 const sptr<AssetObj> &assetObj,
441 const sptr<IAssetSendCallback> &sendCallback)
442 {
443 MessageParcel data;
444 MessageParcel reply;
445 MessageOption option;
446 if (!data.WriteInterfaceToken(GetDescriptor())) {
447 LOGE("Failed to write interface token");
448 return OHOS::FileManagement::E_BROKEN_IPC;
449 }
450
451 if (!data.WriteInt32(userId)) {
452 LOGE("Failed to send the user id");
453 return OHOS::FileManagement::E_INVAL_ARG;
454 }
455 if (!data.WriteParcelable(assetObj)) {
456 LOGE("Failed to send the assetInfoObj");
457 return OHOS::FileManagement::E_INVAL_ARG;
458 }
459 if (sendCallback == nullptr) {
460 LOGE("sendCallback is nullptr.");
461 return OHOS::FileManagement::E_INVAL_ARG;
462 }
463 if (!data.WriteRemoteObject(sendCallback->AsObject())) {
464 LOGE("Failed to send the listener callback stub");
465 return OHOS::FileManagement::E_INVAL_ARG;
466 }
467 auto remote = Remote();
468 if (remote == nullptr) {
469 LOGE("remote is nullptr");
470 return OHOS::FileManagement::E_BROKEN_IPC;
471 }
472 int32_t ret = remote->SendRequest(
473 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PUSH_ASSET), data, reply,
474 option);
475 if (ret != 0) {
476 LOGE("UnRegisterAssetCallback failed, ret = %{public}d", ret);
477 return OHOS::FileManagement::E_BROKEN_IPC;
478 }
479 return reply.ReadInt32();
480 }
481
RegisterAssetCallback(const sptr<IAssetRecvCallback> & recvCallback)482 int32_t DistributedFileDaemonProxy::RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
483 {
484 MessageParcel data;
485 MessageParcel reply;
486 MessageOption option;
487 if (!data.WriteInterfaceToken(GetDescriptor())) {
488 LOGE("Failed to write interface token");
489 return OHOS::FileManagement::E_BROKEN_IPC;
490 }
491 if (recvCallback == nullptr) {
492 LOGE("recvCallback is nullptr.");
493 return OHOS::FileManagement::E_INVAL_ARG;
494 }
495 if (!data.WriteRemoteObject(recvCallback->AsObject())) {
496 LOGE("Failed to send the listener callback stub");
497 return OHOS::FileManagement::E_INVAL_ARG;
498 }
499 auto remote = Remote();
500 if (remote == nullptr) {
501 LOGE("remote is nullptr");
502 return OHOS::FileManagement::E_BROKEN_IPC;
503 }
504 int32_t ret = remote->SendRequest(
505 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REGISTER_ASSET_CALLBACK), data,
506 reply, option);
507 if (ret != 0) {
508 LOGE("RegisterAssetCallback failed, ret = %{public}d", ret);
509 return OHOS::FileManagement::E_BROKEN_IPC;
510 }
511 return reply.ReadInt32();
512 }
513
UnRegisterAssetCallback(const sptr<IAssetRecvCallback> & recvCallback)514 int32_t DistributedFileDaemonProxy::UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
515 {
516 MessageParcel data;
517 MessageParcel reply;
518 MessageOption option;
519 if (!data.WriteInterfaceToken(GetDescriptor())) {
520 LOGE("Failed to write interface token");
521 return OHOS::FileManagement::E_BROKEN_IPC;
522 }
523 if (recvCallback == nullptr) {
524 LOGE("recvCallback is nullptr.");
525 return OHOS::FileManagement::E_INVAL_ARG;
526 }
527 if (!data.WriteRemoteObject(recvCallback->AsObject())) {
528 LOGE("Failed to send the listener callback stub");
529 return OHOS::FileManagement::E_INVAL_ARG;
530 }
531 auto remote = Remote();
532 if (remote == nullptr) {
533 LOGE("remote is nullptr");
534 return OHOS::FileManagement::E_BROKEN_IPC;
535 }
536 int32_t ret = remote->SendRequest(
537 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_UN_REGISTER_ASSET_CALLBACK), data,
538 reply, option);
539 if (ret != 0) {
540 LOGE("UnRegisterAssetCallback failed, ret = %{public}d", ret);
541 return OHOS::FileManagement::E_BROKEN_IPC;
542 }
543 return reply.ReadInt32();
544 }
545 } // namespace DistributedFile
546 } // namespace Storage
547 } // namespace OHOS