1 /*
2 * Copyright (C) 2021 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 "service_proxy.h"
17 #include "utils_log.h"
18
19 namespace OHOS {
20 namespace Storage {
21 namespace DistributedFile {
SendFile(const std::string & cid,const std::vector<std::string> & sourceFileList,const std::vector<std::string> & destinationFileList,const uint32_t fileCount)22 int32_t ServiceProxy::SendFile(const std::string &cid,
23 const std::vector<std::string> &sourceFileList,
24 const std::vector<std::string> &destinationFileList,
25 const uint32_t fileCount)
26 {
27 MessageParcel data;
28 if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) {
29 return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL;
30 }
31 data.WriteString(cid);
32 int32_t sourceListNumber = static_cast<int32_t>(sourceFileList.size());
33 if (sourceListNumber != 1) {
34 return DFS_PARAM_FILE_COUNT_ERROR;
35 }
36 data.WriteInt32(sourceListNumber);
37 for (int32_t index = 0; index < sourceListNumber; ++index) {
38 data.WriteString(sourceFileList.at(index));
39 }
40
41 int32_t destinationListNumber = static_cast<int32_t>(destinationFileList.size());
42 if (destinationListNumber > 1) {
43 return DFS_PARAM_FILE_COUNT_ERROR;
44 }
45 data.WriteInt32(destinationListNumber);
46 for (int32_t index = 0; index < destinationListNumber; ++index) {
47 data.WriteString(destinationFileList.at(index));
48 }
49
50 if (fileCount != 1) {
51 return DFS_PARAM_FILE_COUNT_ERROR;
52 }
53 data.WriteUint32(fileCount);
54
55 if (Remote() == nullptr) {
56 LOGE("unregister sendfile remote object address is null");
57 return DFS_REMOTE_ADDRESS_IS_NULL;
58 }
59
60 MessageParcel reply;
61 MessageOption option;
62 int32_t result = Remote()->SendRequest(SEND_FILE_DISTRIBUTED, data, reply, option);
63 if (result != DFS_SENDFILE_SUCCESS) {
64 LOGE("sendfile error code : %{public}d", result);
65 return result;
66 }
67
68 return reply.ReadInt32();
69 }
70
OpenFile(int32_t fd,const std::string & fileName,int32_t mode)71 int32_t ServiceProxy::OpenFile(int32_t fd, const std::string &fileName, int32_t mode)
72 {
73 MessageParcel data;
74 if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) {
75 return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL;
76 }
77
78 data.WriteFileDescriptor(fd);
79 data.WriteString(fileName);
80 data.WriteInt32(mode);
81
82 if (Remote() == nullptr) {
83 LOGE("openfile remote object address is null");
84 return DFS_REMOTE_ADDRESS_IS_NULL;
85 }
86
87 MessageParcel reply;
88 MessageOption option;
89 Remote()->SendRequest(OPEN_FILE_FD, data, reply, option);
90
91 return reply.ReadInt32();
92 }
93
RegisterNotifyCallback(sptr<IFileTransferCallback> & callback)94 int32_t ServiceProxy::RegisterNotifyCallback(sptr<IFileTransferCallback> &callback)
95 {
96 MessageParcel data;
97 if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) {
98 return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL;
99 }
100
101 if (callback == nullptr) {
102 LOGE("The parameter of callback is nullptr");
103 return DFS_CALLBACK_PARAM_ERROR;
104 }
105
106 LOGD("ServiceProxy::RegisterNotifyCallback: cb[%{public}p]", callback->AsObject().GetRefPtr());
107 if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
108 LOGE("write remote object failed");
109 return DFS_WRITE_REMOTE_OBJECT_FAIL;
110 }
111
112 if (Remote() == nullptr) {
113 LOGE("register filetransfer callback remote object address is null");
114 return DFS_REMOTE_ADDRESS_IS_NULL;
115 }
116
117 MessageParcel reply;
118 MessageOption option;
119 Remote()->SendRequest(REGISTER_NOTIFY_CALLBACK, data, reply, option);
120
121 return reply.ReadInt32();
122 }
123
UnRegisterNotifyCallback()124 int32_t ServiceProxy::UnRegisterNotifyCallback()
125 {
126 MessageParcel data;
127 if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) {
128 return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL;
129 }
130
131 if (Remote() == nullptr) {
132 LOGE("unregister filetransfer callback remote object address is null");
133 return DFS_REMOTE_ADDRESS_IS_NULL;
134 }
135
136 MessageParcel reply;
137 MessageOption option;
138 Remote()->SendRequest(UN_REGISTER_NOTIFY_CALLBACK, data, reply, option);
139
140 return reply.ReadInt32();
141 }
142
IsDeviceOnline(const std::string & cid)143 int32_t ServiceProxy::IsDeviceOnline(const std::string &cid)
144 {
145 MessageParcel data;
146 if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) {
147 return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL;
148 }
149 data.WriteString(cid);
150
151 if (Remote() == nullptr) {
152 LOGE("Remote object address is null");
153 return DFS_REMOTE_ADDRESS_IS_NULL;
154 }
155
156 MessageParcel reply;
157 MessageOption option;
158 int32_t result = Remote()->SendRequest(IS_DEVICE_ONLINE, data, reply, option);
159 if (result != DFS_NO_ERROR) {
160 LOGE("Function RemoveBundleDistributedDirs! errCode:%{public}d", result);
161 return DFS_NO_DEVICE_ONLINE;
162 }
163
164 return reply.ReadInt32();
165 }
166 } // namespace DistributedFile
167 } // namespace Storage
168 } // namespace OHOS