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 "dm_device_info.h"
20 #include "ipc/distributed_file_daemon_ipc_interface_code.h"
21 #include "utils_log.h"
22
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
DaemonStub()26 DaemonStub::DaemonStub()
27 {
28 opToInterfaceMap_[
29 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION)
30 ] = &DaemonStub::HandleOpenP2PConnection;
31 opToInterfaceMap_[
32 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION)
33 ] = &DaemonStub::HandleCloseP2PConnection;
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t DaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38 if (data.ReadInterfaceToken() != GetDescriptor()) {
39 return DFS_DAEMON_DESCRIPTOR_IS_EMPTY;
40 }
41 auto interfaceIndex = opToInterfaceMap_.find(code);
42 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
43 LOGE("Cannot response request %d: unknown tranction", code);
44 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45 }
46 return (this->*(interfaceIndex->second))(data, reply);
47 }
48
HandleOpenP2PConnection(MessageParcel & data,MessageParcel & reply)49 int32_t DaemonStub::HandleOpenP2PConnection(MessageParcel &data, MessageParcel &reply)
50 {
51 LOGI("Begin OpenP2PConnection");
52 DistributedHardware::DmDeviceInfo deviceInfo;
53 auto ret = strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
54 if (ret != 0) {
55 LOGE("strcpy for source device id failed, ret is %{public}d", ret);
56 return -1;
57 }
58 ret = strcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, data.ReadCString());
59 if (ret != 0) {
60 LOGE("strcpy for source device name failed, ret is %{public}d", ret);
61 return -1;
62 }
63 ret = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
64 if (ret != 0) {
65 LOGE("strcpy for source network id failed, ret is %{public}d", ret);
66 return -1;
67 }
68 deviceInfo.deviceTypeId = data.ReadUint16();
69 deviceInfo.range = static_cast<int32_t>(data.ReadUint32());
70 deviceInfo.authForm = static_cast<DistributedHardware::DmAuthForm>(data.ReadInt32());
71
72 int32_t res = OpenP2PConnection(deviceInfo);
73 reply.WriteInt32(res);
74 LOGI("End OpenP2PConnection");
75 return res;
76 }
77
HandleCloseP2PConnection(MessageParcel & data,MessageParcel & reply)78 int32_t DaemonStub::HandleCloseP2PConnection(MessageParcel &data, MessageParcel &reply)
79 {
80 LOGI("Begin CloseP2PConnection");
81 DistributedHardware::DmDeviceInfo deviceInfo;
82 auto ret = strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
83 if (ret != 0) {
84 LOGE("strcpy for source device id failed, ret is %{public}d", ret);
85 return -1;
86 }
87 ret = strcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, data.ReadCString());
88 if (ret != 0) {
89 LOGE("strcpy for source device name failed, ret is %{public}d", ret);
90 return -1;
91 }
92 ret = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
93 if (ret != 0) {
94 LOGE("strcpy for source network id failed, ret is %{public}d", ret);
95 return -1;
96 }
97 deviceInfo.deviceTypeId = data.ReadUint16();
98 deviceInfo.range = static_cast<int32_t>(data.ReadUint32());
99 deviceInfo.authForm = static_cast<DistributedHardware::DmAuthForm>(data.ReadInt32());
100
101 int32_t res = CloseP2PConnection(deviceInfo);
102 reply.WriteInt32(res);
103 LOGI("End CloseP2PConnection");
104 return res;
105 }
106 } // namespace DistributedFile
107 } // namespace Storage
108 } // namespace OHOS