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 "distributedfile_service_stub.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21
22 #include "filetransfer_callback_proxy.h"
23 #include "ipc_skeleton.h"
24 #include "utils_log.h"
25
26 namespace OHOS {
27 namespace Storage {
28 namespace DistributedFile {
DistributedFileServiceStub()29 DistributedFileServiceStub::DistributedFileServiceStub()
30 {
31 memberFuncMap_[SEND_FILE_DISTRIBUTED] = &DistributedFileServiceStub::SendFileStub;
32 memberFuncMap_[OPEN_FILE_FD] = &DistributedFileServiceStub::OpenFileStub;
33 memberFuncMap_[REGISTER_NOTIFY_CALLBACK] = &DistributedFileServiceStub::CmdRegisterNotifyCallback;
34 memberFuncMap_[IS_DEVICE_ONLINE] = &DistributedFileServiceStub::CmdIsDeviceOnline;
35 }
36
~DistributedFileServiceStub()37 DistributedFileServiceStub::~DistributedFileServiceStub()
38 {
39 memberFuncMap_.clear();
40 }
41
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int DistributedFileServiceStub::OnRemoteRequest(uint32_t code,
43 MessageParcel &data,
44 MessageParcel &reply,
45 MessageOption &option)
46 {
47 std::u16string myDescriptor = DistributedFileServiceStub::GetDescriptor();
48 std::u16string remoteDescriptor = data.ReadInterfaceToken();
49 if (myDescriptor != remoteDescriptor) {
50 LOGE("DistributedFileServiceStub descriptor information");
51 return DFS_DESCRIPTOR_IS_EMPTY;
52 }
53
54 auto itFunc = memberFuncMap_.find(code);
55 if (itFunc != memberFuncMap_.end()) {
56 auto memberFunc = itFunc->second;
57 if (memberFunc != nullptr) {
58 return (this->*memberFunc)(data, reply);
59 }
60 }
61 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63
SendFileStub(MessageParcel & data,MessageParcel & reply)64 int32_t DistributedFileServiceStub::SendFileStub(MessageParcel &data, MessageParcel &reply)
65 {
66 std::string cid = data.ReadString();
67 if (cid.empty()) {
68 LOGE("SendFileStub : Failed to get app device id, error: invalid device id");
69 return DFS_SESSION_ID_IS_EMPTY;
70 }
71
72 int32_t sourceListNumber = data.ReadInt32();
73 if (sourceListNumber != 1) {
74 return DFS_PARAM_FILE_COUNT_ERROR;
75 }
76 std::vector<std::string> srcList;
77 for (int32_t index = 0; index < sourceListNumber; ++index) {
78 srcList.push_back(data.ReadString());
79 }
80
81 int32_t destinationListNumber = data.ReadInt32();
82 if (destinationListNumber > 1) {
83 return DFS_PARAM_FILE_COUNT_ERROR;
84 }
85 std::vector<std::string> dstList;
86 for (int32_t index = 0; index < destinationListNumber; ++index) {
87 dstList.push_back(data.ReadString());
88 }
89
90 uint32_t fileCount = data.ReadUint32();
91 if (fileCount != 1) {
92 return DFS_PARAM_FILE_COUNT_ERROR;
93 }
94
95 int32_t result = SendFile(cid, srcList, dstList, fileCount);
96 if (!reply.WriteInt32(result)) {
97 LOGE("sendfilestub fail to write parcel");
98 return DFS_WRITE_REPLY_FAIL;
99 }
100
101 return result;
102 }
103
OpenFileStub(MessageParcel & data,MessageParcel & reply)104 int32_t DistributedFileServiceStub::OpenFileStub(MessageParcel &data, MessageParcel &reply)
105 {
106 int32_t fileData = data.ReadFileDescriptor();
107 int32_t fd = fcntl(fileData, F_GETFD, 0);
108 if (fd < 0) {
109 reply.WriteInt32(DFS_FILE_OP_ERROR);
110 LOGE("DistributedFileServiceStub : Failed to get app device id, error: invalid device id");
111 return DFS_FILE_OP_ERROR;
112 }
113
114 std::string fileName = data.ReadString();
115 if (fileName.empty()) {
116 LOGE("DistributedFileServiceStub : Failed to get app device id, error: invalid device id");
117 return DFS_NO_SUCH_FILE;
118 }
119
120 int32_t modeData = data.ReadInt32();
121 int32_t result = OpenFile(fileData, fileName, modeData);
122 if (!reply.WriteInt32(result)) {
123 LOGE("fail to write parcel");
124 return DFS_WRITE_REPLY_FAIL;
125 }
126
127 return result;
128 }
129
CmdRegisterNotifyCallback(MessageParcel & data,MessageParcel & reply)130 int32_t DistributedFileServiceStub::CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)
131 {
132 sptr<IRemoteObject> remote = data.ReadRemoteObject();
133 if (remote == nullptr) {
134 LOGE("Callback ptr is nullptr.");
135 return DFS_REMOTE_ADDRESS_IS_NULL;
136 }
137
138 sptr<IFileTransferCallback> callback = iface_cast<IFileTransferCallback>(remote);
139 LOGD("DistributedFileServiceStub::RegisterNotifyCallback: cb[%{public}p]", callback->AsObject().GetRefPtr());
140 int32_t result = RegisterNotifyCallback(callback);
141 reply.WriteInt32(result);
142 return result;
143 }
144
CmdIsDeviceOnline(MessageParcel & data,MessageParcel & reply)145 int32_t DistributedFileServiceStub::CmdIsDeviceOnline(MessageParcel &data, MessageParcel &reply)
146 {
147 std::string cid = data.ReadString();
148 if (cid.empty()) {
149 LOGE("DistributedFileServiceStub : Failed to get app device id, error: invalid device id");
150 return DFS_SESSION_ID_IS_EMPTY;
151 }
152
153 int32_t result = IsDeviceOnline(cid);
154 if (!reply.WriteInt32(result)) {
155 LOGE("fail to write parcel");
156 return DFS_WRITE_REPLY_FAIL;
157 }
158
159 return result;
160 }
161 } // namespace DistributedFile
162 } // namespace Storage
163 } // namespace OHOS