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