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 
68     return E_OK;
69 }
70 
Stop()71 int32_t SoftbusSession::Stop()
72 {
73     LOGD("stop connection");
74     std::unique_lock<mutex> lock(sessionMutex_);
75     SoftbusAdapter::GetInstance().CloseSession(sessionId_);
76     sessionId_ = INVALID_SESSION_ID;
77     return E_OK;
78 }
79 
SendData(const void *data, uint32_t dataLen)80 int32_t SoftbusSession::SendData(const void *data, uint32_t dataLen)
81 {
82     return SoftbusAdapter::GetInstance().SendBytes(sessionId_, data, dataLen);
83 }
84 
SendFile(const std::vector<std::string> &sFileList, const std::vector<std::string> &dFileList)85 int32_t SoftbusSession::SendFile(const std::vector<std::string> &sFileList, const std::vector<std::string> &dFileList)
86 {
87     return SoftbusAdapter::GetInstance().SendFile(sessionId_, sFileList, dFileList);
88 }
89 
GetSessionId()90 int32_t SoftbusSession::GetSessionId()
91 {
92     return sessionId_;
93 }
94 
GetDataType()95 SoftbusSession::DataType SoftbusSession::GetDataType()
96 {
97     return type_;
98 }
99 
GetPeerDeviceId()100 std::string SoftbusSession::GetPeerDeviceId()
101 {
102     return peerDeviceId_;
103 }
104 
CancelReleaseSessionIfNeeded()105 void SoftbusSession::CancelReleaseSessionIfNeeded() {}
106 
CancelDelayReleaseSessionTask()107 void SoftbusSession::CancelDelayReleaseSessionTask() {}
108 
WaitSessionOpened(int sessionId)109 int32_t SoftbusSession::WaitSessionOpened(int sessionId)
110 {
111     int retryTimes = 0;
112     int logSpan = LOG_SPAN;
113     while (retryTimes++ < OPEN_SESSION_RETRY_TIMES) {
114         if (!SoftbusAdapter::GetInstance().IsSessionOpened(sessionId)) {
115             std::this_thread::sleep_for(std::chrono::milliseconds(OPEN_SESSION_RETRY_INTERVAL_MS));
116             if (retryTimes % logSpan == 0) {
117                 LOGI("openSession, waiting for:%{public}d ms", retryTimes * OPEN_SESSION_RETRY_INTERVAL_MS);
118             }
119             continue;
120         }
121         return E_OK;
122     }
123     LOGE("wait session opened timeout");
124     return E_WAIT_SESSION_OPENED;
125 }
126 
ToBeAnonymous(const std::string &name)127 std::string SoftbusSession::ToBeAnonymous(const std::string &name)
128 {
129     if (name.length() <= HEAD_SIZE) {
130         return DEFAULT_ANONYMOUS;
131     }
132 
133     if (name.length() < MIN_SIZE) {
134         return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
135     }
136 
137     return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN + name.substr(name.length() - END_SIZE, END_SIZE));
138 }
139 } // namespace OHOS::FileManagement::CloudSync
140