• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "distributed_file_daemon_proxy.h"
17 
18 #include <sstream>
19 
20 #include "dfs_error.h"
21 #include "ipc/distributed_file_daemon_ipc_interface_code.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "utils_log.h"
25 
26 namespace OHOS {
27 namespace Storage {
28 namespace DistributedFile {
29 using namespace std;
30 using namespace OHOS::Storage;
31 
GetInstance()32 sptr<IDaemon> DistributedFileDaemonProxy::GetInstance()
33 {
34     LOGI("getinstance");
35     unique_lock<mutex> lock(proxyMutex_);
36     if (daemonProxy_ != nullptr) {
37         return daemonProxy_;
38     }
39 
40     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (samgr == nullptr) {
42         LOGE("Samgr is nullptr");
43         return nullptr;
44     }
45 
46     auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID);
47     if (object == nullptr) {
48         LOGE("object == nullptr");
49         return nullptr;
50     }
51 
52     daemonProxy_ = iface_cast<IDaemon>(object);
53     if (daemonProxy_ == nullptr) {
54         LOGE("service == nullptr");
55         return nullptr;
56     }
57     return daemonProxy_;
58 }
59 
OpenP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)60 int32_t DistributedFileDaemonProxy::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
61 {
62     LOGI("Open p2p connection");
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option;
66     if (!data.WriteInterfaceToken(GetDescriptor())) {
67         LOGE("Failed to write interface token");
68         return OHOS::FileManagement::E_BROKEN_IPC;
69     }
70     if (!data.WriteCString(deviceInfo.deviceId)) {
71         LOGE("Failed to send device id");
72         return OHOS::FileManagement::E_INVAL_ARG;
73     }
74     if (!data.WriteCString(deviceInfo.deviceName)) {
75         LOGE("Failed to send device name");
76         return OHOS::FileManagement::E_INVAL_ARG;
77     }
78     if (!data.WriteCString(deviceInfo.networkId)) {
79         LOGE("Failed to send network id");
80         return OHOS::FileManagement::E_INVAL_ARG;
81     }
82     if (!data.WriteUint16(deviceInfo.deviceTypeId)) {
83         LOGE("Failed to send deviceTypeId");
84         return OHOS::FileManagement::E_INVAL_ARG;
85     }
86     if (!data.WriteUint32(deviceInfo.range)) {
87         LOGE("Failed to send range");
88         return OHOS::FileManagement::E_INVAL_ARG;
89     }
90     if (!data.WriteInt32(deviceInfo.authForm)) {
91         LOGE("Failed to send user id");
92         return OHOS::FileManagement::E_INVAL_ARG;
93     }
94     auto remote = Remote();
95     if (!remote) {
96         LOGE("remote is nullptr");
97         return OHOS::FileManagement::E_BROKEN_IPC;
98     }
99     int32_t ret = remote->SendRequest(
100         static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION),
101         data, reply, option);
102     if (ret != 0) {
103         stringstream ss;
104         ss << "Failed to send out the requeset, errno:" << ret;
105         LOGE("%{public}s", ss.str().c_str());
106         return OHOS::FileManagement::E_BROKEN_IPC;
107     }
108     LOGI("Open p2p connection Success");
109     return reply.ReadInt32();
110 }
111 
CloseP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)112 int32_t DistributedFileDaemonProxy::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
113 {
114     LOGI("Close p2p connection");
115     MessageParcel data;
116     MessageParcel reply;
117     MessageOption option;
118     if (!data.WriteInterfaceToken(GetDescriptor())) {
119         LOGE("Failed to write interface token");
120         return OHOS::FileManagement::E_BROKEN_IPC;
121     }
122     if (!data.WriteCString(deviceInfo.deviceId)) {
123         LOGE("Failed to send device id");
124         return OHOS::FileManagement::E_INVAL_ARG;
125     }
126     if (!data.WriteCString(deviceInfo.deviceName)) {
127         LOGE("Failed to send device name");
128         return OHOS::FileManagement::E_INVAL_ARG;
129     }
130     if (!data.WriteCString(deviceInfo.networkId)) {
131         LOGE("Failed to send network id");
132         return OHOS::FileManagement::E_INVAL_ARG;
133     }
134     if (!data.WriteUint16(deviceInfo.deviceTypeId)) {
135         LOGE("Failed to send deviceTypeId");
136         return OHOS::FileManagement::E_INVAL_ARG;
137     }
138     if (!data.WriteUint32(deviceInfo.range)) {
139         LOGE("Failed to send range");
140         return OHOS::FileManagement::E_INVAL_ARG;
141     }
142     if (!data.WriteInt32(deviceInfo.authForm)) {
143         LOGE("Failed to send user id");
144         return OHOS::FileManagement::E_INVAL_ARG;
145     }
146     auto remote = Remote();
147     if (!remote) {
148         LOGE("remote is nullptr");
149         return OHOS::FileManagement::E_BROKEN_IPC;
150     }
151     int32_t ret = remote->SendRequest(
152         static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION),
153         data, reply, option);
154     if (ret != 0) {
155         stringstream ss;
156         ss << "Failed to send out the requeset, errno:" << ret;
157         LOGE("%{public}s", ss.str().c_str());
158         return OHOS::FileManagement::E_BROKEN_IPC;
159     }
160     LOGI("Close p2p connection Success");
161     return reply.ReadInt32();
162 }
163 
164 } // namespace DistributedFile
165 } // namespace Storage
166 } // namespace OHOS