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