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 "network/softbus/softbus_agent.h"
17
18 #include <sstream>
19
20 #include "dfsu_exception.h"
21 #include "ipc/i_daemon.h"
22 #include "ipc_skeleton.h"
23 #include "network/softbus/softbus_session.h"
24 #include "network/softbus/softbus_session_dispatcher.h"
25 #include "network/softbus/softbus_session_name.h"
26 #include "session.h"
27 #include "utils_log.h"
28
29 namespace OHOS {
30 namespace Storage {
31 namespace DistributedFile {
32 namespace {
33 constexpr int MAX_RETRY_COUNT = 7;
34 constexpr uint8_t LINK_TYPE_AP_MAX = 2;
35 constexpr uint8_t LINK_TYPE_P2P_MAX = 4;
36 constexpr uint8_t LINK_TYPE_WIFI_IDX = 2;
37 constexpr uint8_t LINK_TYPE_BR_IDX = 3;
38 }
39 using namespace std;
SoftbusAgent(weak_ptr<MountPoint> mountPoint)40 SoftbusAgent::SoftbusAgent(weak_ptr<MountPoint> mountPoint) : NetworkAgentTemplate(mountPoint)
41 {
42 auto spt = mountPoint.lock();
43 if (spt == nullptr) {
44 LOGE("mountPoint is not exist! bad weak_ptr");
45 sessionName_ = "";
46 return;
47 }
48
49 string path = spt->GetMountArgument().GetFullDst();
50 SoftbusSessionName sessionName(path);
51 sessionName_ = sessionName.ToString();
52 }
53
JoinDomain()54 void SoftbusAgent::JoinDomain()
55 {
56 ISessionListener sessionListener = {
57 .OnSessionOpened = SoftbusSessionDispatcher::OnSessionOpened,
58 .OnSessionClosed = SoftbusSessionDispatcher::OnSessionClosed,
59 .OnBytesReceived = nullptr,
60 .OnMessageReceived = nullptr,
61 .OnStreamReceived = nullptr,
62 };
63
64 SoftbusSessionDispatcher::RegisterSessionListener(sessionName_, shared_from_this());
65 int ret = ::CreateSessionServer(IDaemon::SERVICE_NAME.c_str(), sessionName_.c_str(), &sessionListener);
66 if (ret != 0) {
67 stringstream ss;
68 ss << "Failed to CreateSessionServer, errno:" << ret;
69 LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str());
70 throw runtime_error(ss.str());
71 }
72 LOGD("Succeed to JoinDomain, busName:%{public}s", sessionName_.c_str());
73 }
74
QuitDomain()75 void SoftbusAgent::QuitDomain()
76 {
77 int ret = ::RemoveSessionServer(IDaemon::SERVICE_NAME.c_str(), sessionName_.c_str());
78 if (ret != 0) {
79 stringstream ss;
80 ss << "Failed to RemoveSessionServer, errno:" << ret;
81 LOGE("%{public}s", ss.str().c_str());
82 throw runtime_error(ss.str());
83 }
84
85 SoftbusSessionDispatcher::UnregisterSessionListener(sessionName_.c_str());
86 LOGD("Succeed to QuitDomain, busName:%{public}s", sessionName_.c_str());
87 }
88
StopTopHalf()89 void SoftbusAgent::StopTopHalf()
90 {
91 QuitDomain();
92 }
93
StopBottomHalf()94 void SoftbusAgent::StopBottomHalf() {}
95
OpenSession(const DeviceInfo & info,const uint8_t & linkType)96 void SoftbusAgent::OpenSession(const DeviceInfo &info, const uint8_t &linkType)
97 {
98 LOGI("Start to OpenSession, cid:%{public}s, linkType:%{public}d", info.GetCid().c_str(), linkType);
99 SessionAttribute attr;
100 attr.dataType = TYPE_BYTES;
101 string groupId;
102 if (linkType == LINK_TYPE_AP) {
103 groupId = GROUP_TYPE_AP;
104 attr.linkTypeNum = LINK_TYPE_AP_MAX;
105 attr.linkType[0] = LINK_TYPE_WIFI_WLAN_5G;
106 attr.linkType[1] = LINK_TYPE_WIFI_WLAN_2G;
107 } else if (linkType == LINK_TYPE_P2P) {
108 groupId = GROUP_TYPE_P2P;
109 attr.linkTypeNum = LINK_TYPE_P2P_MAX;
110 attr.linkType[0] = LINK_TYPE_WIFI_P2P;
111 attr.linkType[1] = LINK_TYPE_WIFI_WLAN_5G;
112 attr.linkType[LINK_TYPE_WIFI_IDX] = LINK_TYPE_WIFI_WLAN_2G;
113 attr.linkType[LINK_TYPE_BR_IDX] = LINK_TYPE_BR;
114 } else {
115 LOGE("Fail to OpenSession, cid:%{public}s, linkType:%{public}d", info.GetCid().c_str(), linkType);
116 THROW_EXCEPTION(ERR_CONNECT_LINK_TYPE, "Fail to OpenSession");
117 }
118 int sessionId =
119 ::OpenSession(sessionName_.c_str(), sessionName_.c_str(), info.GetCid().c_str(), groupId.c_str(), &attr);
120 if (sessionId < 0) {
121 LOGE("Fail to OpenSession, cid:%{public}s, sessionId:%{public}d, linkType:%{public}d", info.GetCid().c_str(),
122 sessionId, linkType);
123 THROW_EXCEPTION(ERR_SOFTBUS_AGENT_ON_SESSION_OPENED_FAIL, "Fail to OpenSession");
124 }
125 LOGI("Success to OpenSession, cid:%{public}s, sessionId:%{public}d, linkType:%{public}d", info.GetCid().c_str(),
126 sessionId, linkType);
127 }
128
CloseSession(shared_ptr<BaseSession> session)129 void SoftbusAgent::CloseSession(shared_ptr<BaseSession> session)
130 {
131 if (session == nullptr) {
132 LOGE("Failed to close session, error:invalid session");
133 return;
134 }
135 session->Release();
136 }
137
IsContinueRetry(const string & cid)138 bool SoftbusAgent::IsContinueRetry(const string &cid)
139 {
140 auto retriedTimesMap = OpenSessionRetriedTimesMap_.find(cid);
141 if (retriedTimesMap != OpenSessionRetriedTimesMap_.end()) {
142 if (retriedTimesMap->second >= MAX_RETRY_COUNT) {
143 return false;
144 }
145 } else {
146 OpenSessionRetriedTimesMap_[cid] = 0;
147 }
148 OpenSessionRetriedTimesMap_[cid]++;
149 return true;
150 }
151
OnSessionOpened(const int sessionId,const int result)152 int SoftbusAgent::OnSessionOpened(const int sessionId, const int result)
153 {
154 auto session = make_shared<SoftbusSession>(sessionId);
155 auto cid = session->GetCid();
156
157 DeviceInfo info;
158 info.SetCid(cid);
159 if (result != 0) {
160 LOGE("OnSessionOpened failed, Is %{public}s Side, result:%{public}d",
161 (session->IsFromServer() == true) ? "Server" : "Client", result);
162 if (!session->IsFromServer()) { // client retry
163 if (IsContinueRetry(cid)) {
164 auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, const DeviceInfo>>(
165 &NetworkAgentTemplate::ConnectDeviceAsync, info);
166 cmd->UpdateOption({.tryTimes_ = 1});
167 Recv(move(cmd));
168 } else {
169 LOGE("Exceeded the maximum number of retries, not retry");
170 }
171 }
172 return result;
173 }
174
175 auto retriedTimesMap = OpenSessionRetriedTimesMap_.find(cid);
176 if (retriedTimesMap != OpenSessionRetriedTimesMap_.end()) {
177 OpenSessionRetriedTimesMap_.erase(cid);
178 }
179
180 int socket_fd = session->GetHandle();
181 LOGI(
182 "accept sesion, sessionid:%{public}d, Is %{public}s Side, fd %{public}d, from cid %{public}s, result "
183 "%{public}d",
184 sessionId, (session->IsFromServer() == true) ? "Server" : "Client", socket_fd, cid.c_str(), result);
185 session->DisableSessionListener();
186 AcceptSession(session);
187 return 0;
188 }
189
OnSessionClosed(int sessionId)190 void SoftbusAgent::OnSessionClosed(int sessionId)
191 {
192 auto session = make_shared<SoftbusSession>(sessionId);
193 auto cid = session->GetCid();
194 LOGI("Session to %{public}s closed by unknown reason, Is %{public}s Side", cid.c_str(),
195 (session->IsFromServer() == true) ? "Server" : "Client");
196 }
197 } // namespace DistributedFile
198 } // namespace Storage
199 } // namespace OHOS
200