1 /*
2 * Copyright (c) 2021-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 #include "ipc/daemon_stub.h"
17
18 #include "dfs_error.h"
19 #include "dfsu_access_token_helper.h"
20 #include "dm_device_info.h"
21 #include "ipc/distributed_file_daemon_ipc_interface_code.h"
22 #include "utils_log.h"
23
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 using namespace OHOS::FileManagement;
DaemonStub()28 DaemonStub::DaemonStub()
29 {
30 opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION)] =
31 &DaemonStub::HandleOpenP2PConnection;
32 opToInterfaceMap_[static_cast<uint32_t>(
33 DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION)] =
34 &DaemonStub::HandleCloseP2PConnection;
35 opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PREPARE_SESSION)] =
36 &DaemonStub::HandlePrepareSession;
37 opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE)] =
38 &DaemonStub::HandleRequestSendFile;
39 }
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t DaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43 if (data.ReadInterfaceToken() != GetDescriptor()) {
44 return DFS_DAEMON_DESCRIPTOR_IS_EMPTY;
45 }
46 auto interfaceIndex = opToInterfaceMap_.find(code);
47 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
48 LOGE("Cannot response request %d: unknown tranction", code);
49 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50 }
51 return (this->*(interfaceIndex->second))(data, reply);
52 }
53
HandleOpenP2PConnection(MessageParcel & data,MessageParcel & reply)54 int32_t DaemonStub::HandleOpenP2PConnection(MessageParcel &data, MessageParcel &reply)
55 {
56 LOGI("Begin OpenP2PConnection");
57 DistributedHardware::DmDeviceInfo deviceInfo;
58 auto ret = strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
59 if (ret != 0) {
60 LOGE("strcpy for source device id failed, ret is %{public}d", ret);
61 return -1;
62 }
63 ret = strcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, data.ReadCString());
64 if (ret != 0) {
65 LOGE("strcpy for source device name failed, ret is %{public}d", ret);
66 return -1;
67 }
68 ret = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
69 if (ret != 0) {
70 LOGE("strcpy for source network id failed, ret is %{public}d", ret);
71 return -1;
72 }
73 deviceInfo.deviceTypeId = data.ReadUint16();
74 deviceInfo.range = static_cast<int32_t>(data.ReadUint32());
75 deviceInfo.authForm = static_cast<DistributedHardware::DmAuthForm>(data.ReadInt32());
76
77 int32_t res = OpenP2PConnection(deviceInfo);
78 reply.WriteInt32(res);
79 LOGI("End OpenP2PConnection, res = %{public}d", res);
80 return res;
81 }
82
HandleCloseP2PConnection(MessageParcel & data,MessageParcel & reply)83 int32_t DaemonStub::HandleCloseP2PConnection(MessageParcel &data, MessageParcel &reply)
84 {
85 LOGI("Begin CloseP2PConnection");
86 DistributedHardware::DmDeviceInfo deviceInfo;
87 auto ret = strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
88 if (ret != 0) {
89 LOGE("strcpy for source device id failed, ret is %{public}d", ret);
90 return -1;
91 }
92 ret = strcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, data.ReadCString());
93 if (ret != 0) {
94 LOGE("strcpy for source device name failed, ret is %{public}d", ret);
95 return -1;
96 }
97 ret = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
98 if (ret != 0) {
99 LOGE("strcpy for source network id failed, ret is %{public}d", ret);
100 return -1;
101 }
102 deviceInfo.deviceTypeId = data.ReadUint16();
103 deviceInfo.range = static_cast<int32_t>(data.ReadUint32());
104 deviceInfo.authForm = static_cast<DistributedHardware::DmAuthForm>(data.ReadInt32());
105
106 int32_t res = CloseP2PConnection(deviceInfo);
107 reply.WriteInt32(res);
108 LOGI("End CloseP2PConnection");
109 return res;
110 }
111
HandlePrepareSession(MessageParcel & data,MessageParcel & reply)112 int32_t DaemonStub::HandlePrepareSession(MessageParcel &data, MessageParcel &reply)
113 {
114 std::string srcUri;
115 if (!data.ReadString(srcUri)) {
116 LOGE("read srcUri failed");
117 return E_IPC_READ_FAILED;
118 }
119 if (!DfsuAccessTokenHelper::CheckUriPermission(srcUri)) {
120 LOGE("permission verify failed");
121 return E_PERMISSION_DENIED;
122 }
123 std::string dstUri;
124 if (!data.ReadString(dstUri)) {
125 LOGE("read dstUri failed");
126 return E_IPC_READ_FAILED;
127 }
128 std::string srcDeviceId;
129 if (!data.ReadString(srcDeviceId)) {
130 LOGE("read srcDeviceId failed");
131 return E_IPC_READ_FAILED;
132 }
133 auto listener = data.ReadRemoteObject();
134 if (listener == nullptr) {
135 LOGE("read listener failed");
136 return E_IPC_READ_FAILED;
137 }
138
139 int32_t res = PrepareSession(srcUri, dstUri, srcDeviceId, listener);
140 reply.WriteInt32(res);
141 LOGD("End PrepareSession, ret = %{public}d.", res);
142 return res;
143 }
144
HandleRequestSendFile(MessageParcel & data,MessageParcel & reply)145 int32_t DaemonStub::HandleRequestSendFile(MessageParcel &data, MessageParcel &reply)
146 {
147 std::string srcUri;
148 if (!data.ReadString(srcUri)) {
149 LOGE("read srcUri failed");
150 return E_IPC_READ_FAILED;
151 }
152 std::string dstPath;
153 if (!data.ReadString(dstPath)) {
154 LOGE("read dstPath failed");
155 return E_IPC_READ_FAILED;
156 }
157 std::string dstDeviceId;
158 if (!data.ReadString(dstDeviceId)) {
159 LOGE("read remoteDeviceId failed");
160 return E_IPC_READ_FAILED;
161 }
162 std::string sessionName;
163 if (!data.ReadString(sessionName)) {
164 LOGE("read sessionName failed");
165 return E_IPC_READ_FAILED;
166 }
167 auto res = RequestSendFile(srcUri, dstPath, dstDeviceId, sessionName);
168 reply.WriteInt32(res);
169 LOGD("End RequestSendFile, ret = %{public}d.", res);
170 return res;
171 }
172 } // namespace DistributedFile
173 } // namespace Storage
174 } // namespace OHOS