1 /*
2 * Copyright (c) 2022 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 "nlohmann/json.hpp"
24 #include "softbus_connector.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 std::shared_ptr<ISoftbusSessionCallback> SoftbusSession::sessionCallback_ = nullptr;
29 constexpr const char* DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION";
30
SoftbusSession()31 SoftbusSession::SoftbusSession()
32 {
33 LOGD("SoftbusSession constructor.");
34 }
35
~SoftbusSession()36 SoftbusSession::~SoftbusSession()
37 {
38 LOGD("SoftbusSession destructor.");
39 }
40
RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)41 int32_t SoftbusSession::RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)
42 {
43 sessionCallback_ = callback;
44 return DM_OK;
45 }
46
UnRegisterSessionCallback()47 int32_t SoftbusSession::UnRegisterSessionCallback()
48 {
49 sessionCallback_ = nullptr;
50 return DM_OK;
51 }
52
OpenAuthSession(const std::string & deviceId)53 int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId)
54 {
55 DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION));
56 int32_t sessionId = -1;
57 std::string connectAddr;
58 ConnectionAddr *addrInfo = SoftbusConnector::GetConnectAddr(deviceId, connectAddr);
59 if (addrInfo == nullptr) {
60 LOGE("[SOFTBUS]addrInfo is nullptr. sessionId: %d.", sessionId);
61 return sessionId;
62 }
63 sessionId = ::OpenAuthSession(DM_SESSION_NAME, addrInfo, 1, nullptr);
64 if (sessionId < 0) {
65 LOGE("[SOFTBUS]open session error, sessionId: %d.", sessionId);
66 return sessionId;
67 }
68 DmTraceEnd();
69 LOGI("OpenAuthSession success. sessionId: %d.", sessionId);
70 return sessionId;
71 }
72
CloseAuthSession(int32_t sessionId)73 int32_t SoftbusSession::CloseAuthSession(int32_t sessionId)
74 {
75 LOGD("CloseAuthSession.");
76 ::CloseSession(sessionId);
77 return DM_OK;
78 }
79
GetPeerDeviceId(int32_t sessionId,std::string & peerDevId)80 int32_t SoftbusSession::GetPeerDeviceId(int32_t sessionId, std::string &peerDevId)
81 {
82 char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
83 int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
84 if (ret == DM_OK) {
85 peerDevId = peerDeviceId;
86 LOGI("[SOFTBUS]GetPeerDeviceId success for session: %d, peerDeviceId: %s.", sessionId,
87 GetAnonyString(peerDevId).c_str());
88 return ERR_DM_FAILED;
89 }
90 LOGE("[SOFTBUS]GetPeerDeviceId failed for session: %d, ret: %d.", sessionId, ret);
91 peerDevId = "";
92 return DM_OK;
93 }
94
SendData(int32_t sessionId,std::string & message)95 int32_t SoftbusSession::SendData(int32_t sessionId, std::string &message)
96 {
97 nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
98 if (jsonObject.is_discarded()) {
99 LOGE("extrasJson error, message: %s.", message.c_str());
100 return ERR_DM_FAILED;
101 }
102 if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
103 LOGE("SoftbusSession::SendData err json string.");
104 return ERR_DM_FAILED;
105 }
106 int32_t msgType = jsonObject[TAG_MSG_TYPE].get<int32_t>();
107 LOGI("start, msgType: %d.", msgType);
108 if (sessionCallback_->GetIsCryptoSupport()) {
109 LOGI("SendData Start encryption.");
110 }
111 int32_t ret = SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
112 if (ret != DM_OK) {
113 LOGE("[SOFTBUS]SendBytes failed, ret: %d.", ret);
114 return ERR_DM_FAILED;
115 }
116 return ret;
117 }
118
OnSessionOpened(int sessionId,int result)119 int SoftbusSession::OnSessionOpened(int sessionId, int result)
120 {
121 int32_t sessionSide = GetSessionSide(sessionId);
122 sessionCallback_->OnSessionOpened(sessionId, sessionSide, result);
123 LOGD("OnSessionOpened, success, sessionId: %d.", sessionId);
124 return DM_OK;
125 }
126
OnSessionClosed(int sessionId)127 void SoftbusSession::OnSessionClosed(int sessionId)
128 {
129 LOGD("OnSessionClosed, sessionId: %d.", sessionId);
130 }
131
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)132 void SoftbusSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
133 {
134 if (sessionId < 0 || data == nullptr || dataLen <= 0) {
135 LOGI("[SOFTBUS]fail to receive data from softbus with sessionId: %d, dataLen: %d.", sessionId, dataLen);
136 return;
137 }
138 LOGI("start, sessionId: %d, dataLen: %d.", sessionId, dataLen);
139 if (sessionCallback_->GetIsCryptoSupport()) {
140 LOGI("Start decryption.");
141 }
142 std::string message = std::string(reinterpret_cast<const char *>(data), dataLen);
143 sessionCallback_->OnDataReceived(sessionId, message);
144 LOGI("completed.");
145 }
146 } // namespace DistributedHardware
147 } // namespace OHOS
148