1 /*
2 * Copyright (c) 2022-2024 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 "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_dfx_constants.h"
21 #include "dm_hitrace.h"
22 #include "dm_log.h"
23 #include "json_object.h"
24 #include "softbus_connector.h"
25 #include "softbus_error_code.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 std::shared_ptr<ISoftbusSessionCallback> SoftbusSession::sessionCallback_ = nullptr;
30 constexpr const char* DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION";
31 constexpr int32_t MAX_DATA_LEN = 65535;
32
SoftbusSession()33 SoftbusSession::SoftbusSession()
34 {
35 LOGD("SoftbusSession constructor.");
36 }
37
~SoftbusSession()38 SoftbusSession::~SoftbusSession()
39 {
40 LOGD("SoftbusSession destructor.");
41 }
42
RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)43 int32_t SoftbusSession::RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)
44 {
45 sessionCallback_ = callback;
46 return DM_OK;
47 }
48
UnRegisterSessionCallback()49 int32_t SoftbusSession::UnRegisterSessionCallback()
50 {
51 sessionCallback_ = nullptr;
52 return DM_OK;
53 }
54
OpenAuthSession(const std::string & deviceId)55 int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId)
56 {
57 DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION));
58 int32_t sessionId = -1;
59 std::string connectAddr;
60 auto addrInfo = SoftbusConnector::GetConnectAddr(deviceId, connectAddr);
61 if (addrInfo == nullptr) {
62 LOGE("[SOFTBUS]addrInfo is nullptr. sessionId: %{public}d.", sessionId);
63 return sessionId;
64 }
65 sessionId = ::OpenAuthSession(DM_SESSION_NAME, addrInfo.get(), 1, nullptr);
66 if (sessionId < 0) {
67 LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId);
68 return sessionId;
69 }
70 DmTraceEnd();
71 LOGI("OpenAuthSession success. sessionId: %{public}d.", sessionId);
72 return sessionId;
73 }
74
CloseAuthSession(int32_t sessionId)75 int32_t SoftbusSession::CloseAuthSession(int32_t sessionId)
76 {
77 LOGI("CloseAuthSession.");
78 ::CloseSession(sessionId);
79 return DM_OK;
80 }
81
GetPeerDeviceId(int32_t sessionId,std::string & peerDevId)82 int32_t SoftbusSession::GetPeerDeviceId(int32_t sessionId, std::string &peerDevId)
83 {
84 char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
85 int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
86 if (ret == DM_OK) {
87 peerDevId = peerDeviceId;
88 LOGI("[SOFTBUS]GetPeerDeviceId success for session: %{public}d, peerDeviceId: %{public}s.", sessionId,
89 GetAnonyString(peerDevId).c_str());
90 return DM_OK;
91 }
92 LOGE("[SOFTBUS]GetPeerDeviceId failed for session: %{public}d, ret: %{public}d.", sessionId, ret);
93 peerDevId = "";
94 return ret;
95 }
96
SendData(int32_t sessionId,std::string & message)97 int32_t SoftbusSession::SendData(int32_t sessionId, std::string &message)
98 {
99 int32_t ret = SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
100 if (ret != DM_OK) {
101 LOGE("[SOFTBUS]SendBytes failed.");
102 return ret;
103 }
104 return DM_OK;
105 }
106
SendHeartbeatData(int32_t sessionId,std::string & message)107 int32_t SoftbusSession::SendHeartbeatData(int32_t sessionId, std::string &message)
108 {
109 int32_t ret = SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
110 if (ret != DM_OK) {
111 LOGE("[SOFTBUS]SendHeartbeatData failed.");
112 return ret;
113 }
114 return DM_OK;
115 }
116
OnSessionOpened(int sessionId,int result)117 int SoftbusSession::OnSessionOpened(int sessionId, int result)
118 {
119 LOGI("OnSessionOpened, success, sessionId: %{public}d, result: %{public}d.", sessionId, result);
120 if (sessionCallback_ == nullptr) {
121 LOGI("Session callback is not registered.");
122 return DM_OK;
123 }
124 int32_t sessionSide = GetSessionSide(sessionId);
125 sessionCallback_->OnSessionOpened(sessionId, sessionSide, result);
126 return DM_OK;
127 }
128
OnSessionClosed(int sessionId)129 void SoftbusSession::OnSessionClosed(int sessionId)
130 {
131 LOGI("OnSessionClosed, sessionId: %{public}d.", sessionId);
132 CHECK_NULL_VOID(sessionCallback_);
133 sessionCallback_->OnSessionClosed(sessionId);
134 return;
135 }
136 } // namespace DistributedHardware
137 } // namespace OHOS
138