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 "softbus_session.h"
17
18 #include <mutex>
19 #include <thread>
20
21 #include "dfs_error.h"
22 #include "softbus_adapter.h"
23 #include "utils_log.h"
24
25 namespace OHOS::FileManagement::CloudSync {
26 using namespace std;
27 const std::string GROUP_TYPE_P2P = "CloudSyncService_P2PGroup";
28 constexpr int OPEN_SESSION_RETRY_TIMES = 10 * 30;
29 constexpr int OPEN_SESSION_RETRY_INTERVAL_MS = 100;
30 constexpr int LOG_SPAN = 10;
31 constexpr int32_t HEAD_SIZE = 3;
32 constexpr int32_t END_SIZE = 3;
33 constexpr int32_t MIN_SIZE = HEAD_SIZE + END_SIZE + 3;
34 constexpr const char *REPLACE_CHAIN = "***";
35 constexpr const char *DEFAULT_ANONYMOUS = "******";
36
SoftbusSession(const std::string & peerDeviceId,const std::string & sessionName,DataType type)37 SoftbusSession::SoftbusSession(const std::string &peerDeviceId, const std::string &sessionName, DataType type)
38 : peerDeviceId_(peerDeviceId), sessionName_(sessionName), type_(type)
39 {
40 }
41
Start()42 int32_t SoftbusSession::Start()
43 {
44 CancelReleaseSessionIfNeeded();
45 if (sessionId_ != INVALID_SESSION_ID) {
46 LOGI("session is exist, no need open again");
47 return E_OK;
48 }
49
50 std::unique_lock<mutex> lock(sessionMutex_);
51 if (sessionId_ == INVALID_SESSION_ID) {
52 LOGI("open session with device: %{public}s", ToBeAnonymous(peerDeviceId_).c_str());
53 int session = SoftbusAdapter::GetInstance().OpenSessionByP2P(
54 const_cast<char *>(sessionName_.c_str()), const_cast<char *>(peerDeviceId_.c_str()),
55 const_cast<char *>(GROUP_TYPE_P2P.c_str()), (type_ == DataType::TYPE_FILE));
56 if (session < 0) {
57 LOGE("open session failed");
58 return E_OPEN_SESSION;
59 }
60
61 auto ret = WaitSessionOpened(session);
62 if (ret != E_OK) {
63 return ret;
64 }
65 sessionId_ = session;
66 }
67 return E_OK;
68 }
69
Stop()70 int32_t SoftbusSession::Stop()
71 {
72 LOGD("stop connection");
73 std::unique_lock<mutex> lock(sessionMutex_);
74 SoftbusAdapter::GetInstance().CloseSession(sessionId_);
75 sessionId_ = INVALID_SESSION_ID;
76 return E_OK;
77 }
78
SendData(const void * data,uint32_t dataLen)79 int32_t SoftbusSession::SendData(const void *data, uint32_t dataLen)
80 {
81 return SoftbusAdapter::GetInstance().SendBytes(sessionId_, data, dataLen);
82 }
83
SendFile(const std::vector<std::string> & sFileList,const std::vector<std::string> & dFileList)84 int32_t SoftbusSession::SendFile(const std::vector<std::string> &sFileList, const std::vector<std::string> &dFileList)
85 {
86 return SoftbusAdapter::GetInstance().SendFile(sessionId_, sFileList, dFileList);
87 }
88
GetSessionId()89 int32_t SoftbusSession::GetSessionId()
90 {
91 return sessionId_;
92 }
93
GetDataType()94 SoftbusSession::DataType SoftbusSession::GetDataType()
95 {
96 return type_;
97 }
98
GetPeerDeviceId()99 std::string SoftbusSession::GetPeerDeviceId()
100 {
101 return peerDeviceId_;
102 }
103
CancelReleaseSessionIfNeeded()104 void SoftbusSession::CancelReleaseSessionIfNeeded() {}
105
CancelDelayReleaseSessionTask()106 void SoftbusSession::CancelDelayReleaseSessionTask() {}
107
WaitSessionOpened(int sessionId)108 int32_t SoftbusSession::WaitSessionOpened(int sessionId)
109 {
110 int retryTimes = 0;
111 int logSpan = LOG_SPAN;
112 while (retryTimes++ < OPEN_SESSION_RETRY_TIMES) {
113 if (!SoftbusAdapter::GetInstance().IsSessionOpened(sessionId)) {
114 std::this_thread::sleep_for(std::chrono::milliseconds(OPEN_SESSION_RETRY_INTERVAL_MS));
115 if (retryTimes % logSpan == 0) {
116 LOGI("openSession, waiting for:%{public}d ms", retryTimes * OPEN_SESSION_RETRY_INTERVAL_MS);
117 }
118 continue;
119 }
120 return E_OK;
121 }
122 LOGE("wait session opened timeout");
123 return E_WAIT_SESSION_OPENED;
124 }
125
ToBeAnonymous(const std::string & name)126 std::string SoftbusSession::ToBeAnonymous(const std::string &name)
127 {
128 if (name.length() <= HEAD_SIZE) {
129 return DEFAULT_ANONYMOUS;
130 }
131
132 if (name.length() < MIN_SIZE) {
133 return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
134 }
135
136 return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN + name.substr(name.length() - END_SIZE, END_SIZE));
137 }
138 } // namespace OHOS::FileManagement::CloudSync
139