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 #include "filetransfer_callback_stub.h"
16
17 #include <fcntl.h>
18 #include "utils_log.h"
19
20 namespace OHOS {
21 namespace Storage {
22 namespace DistributedFile {
FileTransferCallbackStub()23 FileTransferCallbackStub::FileTransferCallbackStub()
24 {
25 memberFuncMap_[ON_SEND_FINISHED] = &FileTransferCallbackStub::CmdOnSendFinished;
26 memberFuncMap_[ON_SEND_ERROR] = &FileTransferCallbackStub::CmdOnSendError;
27 memberFuncMap_[ON_RECEIVE_FINISHED] = &FileTransferCallbackStub::CmdOnReceiveFinished;
28 memberFuncMap_[ON_RECEIVE_ERROR] = &FileTransferCallbackStub::CmdOnReceiveError;
29 memberFuncMap_[ON_SESSION_OPENED] = &FileTransferCallbackStub::CmdOnSessionOpened;
30 memberFuncMap_[ON_SESSION_CLOSED] = &FileTransferCallbackStub::CmdOnSessionClosed;
31 memberFuncMap_[ON_WRITE_FILE] = &FileTransferCallbackStub::CmdWriteFile;
32 }
33
~FileTransferCallbackStub()34 FileTransferCallbackStub::~FileTransferCallbackStub() {}
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t FileTransferCallbackStub::OnRemoteRequest(
37 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39 std::u16string myDescripter = FileTransferCallbackStub::GetDescriptor();
40 std::u16string remoteDescripter = data.ReadInterfaceToken();
41 if (myDescripter != remoteDescripter) {
42 LOGE("Descriptor checked failed");
43 return DFS_CALLBACK_DESCRIPTOR_IS_EMPTY;
44 }
45
46 auto itFunc = memberFuncMap_.find(code);
47 if (itFunc != memberFuncMap_.end()) {
48 auto requestFunc = itFunc->second;
49 if (requestFunc != nullptr) {
50 return (this->*requestFunc)(data, reply);
51 }
52 }
53
54 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55 }
56
CmdOnSessionOpened(MessageParcel & data,MessageParcel & reply)57 int32_t FileTransferCallbackStub::CmdOnSessionOpened(MessageParcel &data, MessageParcel &reply)
58 {
59 std::string cid = data.ReadString();
60 int32_t result = SessionOpened(cid);
61 if (!reply.WriteInt32(result)) {
62 LOGE("Write parcel failed");
63 return result;
64 }
65
66 return ERR_NONE;
67 }
68
CmdOnSessionClosed(MessageParcel & data,MessageParcel & reply)69 int32_t FileTransferCallbackStub::CmdOnSessionClosed(MessageParcel &data, MessageParcel &reply)
70 {
71 std::string cid = data.ReadString();
72 int32_t result = SessionClosed(cid);
73 if (!reply.WriteInt32(result)) {
74 LOGE("Write parcel failed");
75 return result;
76 }
77
78 return ERR_NONE;
79 }
80
CmdOnSendFinished(MessageParcel & data,MessageParcel & reply)81 int32_t FileTransferCallbackStub::CmdOnSendFinished(MessageParcel &data, MessageParcel &reply)
82 {
83 std::string cid = data.ReadString();
84 std::string fileName = data.ReadString();
85 int32_t result = SendFinished(cid, fileName);
86 if (!reply.WriteInt32(result)) {
87 LOGE("Write parcel failed");
88 return result;
89 }
90
91 return ERR_NONE;
92 }
93
CmdOnSendError(MessageParcel & data,MessageParcel & reply)94 int32_t FileTransferCallbackStub::CmdOnSendError(MessageParcel &data, MessageParcel &reply)
95 {
96 std::string cid = data.ReadString();
97 int32_t result = SendError(cid);
98 if (!reply.WriteInt32(result)) {
99 LOGE("Write parcel failed");
100 return result;
101 }
102
103 return ERR_NONE;
104 }
105
CmdOnReceiveFinished(MessageParcel & data,MessageParcel & reply)106 int32_t FileTransferCallbackStub::CmdOnReceiveFinished(MessageParcel &data, MessageParcel &reply)
107 {
108 std::string cid = data.ReadString();
109 std::string fileName = data.ReadString();
110 uint32_t num = data.ReadUint32();
111 int32_t result = ReceiveFinished(cid, fileName, num);
112 if (!reply.WriteInt32(result)) {
113 LOGE("Write parcel failed");
114 return result;
115 }
116
117 return ERR_NONE;
118 }
119
CmdOnReceiveError(MessageParcel & data,MessageParcel & reply)120 int32_t FileTransferCallbackStub::CmdOnReceiveError(MessageParcel &data, MessageParcel &reply)
121 {
122 std::string cid = data.ReadString();
123 int32_t result = ReceiveError(cid);
124 if (!reply.WriteInt32(result)) {
125 LOGE("Write parcel failed");
126 return result;
127 }
128
129 return ERR_NONE;
130 }
131
CmdWriteFile(MessageParcel & data,MessageParcel & reply)132 int32_t FileTransferCallbackStub::CmdWriteFile(MessageParcel &data, MessageParcel &reply)
133 {
134 int32_t fileData = data.ReadFileDescriptor();
135 int32_t fd = fcntl(fileData, F_GETFD, 0);
136 if (fd < 0) {
137 reply.WriteInt32(DFS_CALLBACK_ERR_SET_FD_FAIL);
138 LOGE("FileTransferCallbackStub : Failed to get app device id, error: invalid device id");
139 return DFS_CALLBACK_ERR_SET_FD_FAIL;
140 }
141
142 std::string fileName = data.ReadString();
143 if (fileName.empty()) {
144 LOGE("FileTransferCallbackStub : Failed to get app device id, error: invalid device id");
145 return DFS_CALLBACK_DESCRIPTOR_IS_EMPTY;
146 }
147
148 int32_t result = WriteFile(fileData, fileName);
149 if (!reply.WriteInt32(result)) {
150 LOGE("fail to write parcel");
151 return DFS_CALLBACK_WRITE_REPLY_FAIL;
152 }
153
154 return ERR_NONE;
155 }
156 } // namespace DistributedFile
157 } // namespace Storage
158 } // namespace OHOS