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