• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "dm_anonymous.h"
19 #include "dm_auth_manager.h"
20 #include "dm_constants.h"
21 #include "dm_dfx_constants.h"
22 #include "dm_hitrace.h"
23 #include "dm_log.h"
24 #include "nlohmann/json.hpp"
25 #include "softbus_connector.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 
SoftbusSession()32 SoftbusSession::SoftbusSession()
33 {
34     LOGD("SoftbusSession constructor.");
35 }
36 
~SoftbusSession()37 SoftbusSession::~SoftbusSession()
38 {
39     LOGD("SoftbusSession destructor.");
40 }
41 
RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)42 int32_t SoftbusSession::RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)
43 {
44     sessionCallback_ = callback;
45     return DM_OK;
46 }
47 
UnRegisterSessionCallback()48 int32_t SoftbusSession::UnRegisterSessionCallback()
49 {
50     sessionCallback_ = nullptr;
51     return DM_OK;
52 }
53 
OpenAuthSession(const std::string & deviceId)54 int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId)
55 {
56     DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION));
57     int32_t sessionId = -1;
58     std::string connectAddr;
59     ConnectionAddr *addrInfo = SoftbusConnector::GetConnectAddr(deviceId, connectAddr);
60     if (addrInfo == nullptr) {
61         LOGE("[SOFTBUS]addrInfo is nullptr. sessionId: %d.", sessionId);
62         return sessionId;
63     }
64     sessionId = ::OpenAuthSession(DM_SESSION_NAME, addrInfo, 1, nullptr);
65     if (sessionId < 0) {
66         LOGE("[SOFTBUS]open session error, sessionId: %d.", sessionId);
67         return sessionId;
68     }
69     DmTraceEnd();
70     LOGI("OpenAuthSession success. sessionId: %d.", sessionId);
71     return sessionId;
72 }
73 
CloseAuthSession(int32_t sessionId)74 int32_t SoftbusSession::CloseAuthSession(int32_t sessionId)
75 {
76     LOGD("CloseAuthSession.");
77     ::CloseSession(sessionId);
78     return DM_OK;
79 }
80 
GetPeerDeviceId(int32_t sessionId,std::string & peerDevId)81 int32_t SoftbusSession::GetPeerDeviceId(int32_t sessionId, std::string &peerDevId)
82 {
83     char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
84     int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
85     if (ret == DM_OK) {
86         peerDevId = peerDeviceId;
87         LOGI("[SOFTBUS]GetPeerDeviceId success for session: %d, peerDeviceId: %s.", sessionId,
88             GetAnonyString(peerDevId).c_str());
89         return ERR_DM_FAILED;
90     }
91     LOGE("[SOFTBUS]GetPeerDeviceId failed for session: %d, ret: %d.", sessionId, ret);
92     peerDevId = "";
93     return DM_OK;
94 }
95 
SendData(int32_t sessionId,std::string & message)96 int32_t SoftbusSession::SendData(int32_t sessionId, std::string &message)
97 {
98     nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
99     if (jsonObject.is_discarded()) {
100         LOGE("extrasJson error, message: %s.", message.c_str());
101         return ERR_DM_FAILED;
102     }
103     if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
104         LOGE("SoftbusSession::SendData err json string.");
105         return ERR_DM_FAILED;
106     }
107     int32_t msgType = jsonObject[TAG_MSG_TYPE].get<int32_t>();
108     LOGI("start, msgType: %d.", msgType);
109     if (sessionCallback_->GetIsCryptoSupport()) {
110         LOGI("SendData Start encryption.");
111     }
112     if (SendBytes(sessionId, message.c_str(), strlen(message.c_str())) != DM_OK) {
113         LOGE("[SOFTBUS]SendBytes failed.");
114         return ERR_DM_FAILED;
115     }
116     return DM_OK;
117 }
118 
SendHeartbeatData(int32_t sessionId,std::string & message)119 int32_t SoftbusSession::SendHeartbeatData(int32_t sessionId, std::string &message)
120 {
121     nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
122     if (SendBytes(sessionId, message.c_str(), strlen(message.c_str())) != DM_OK) {
123         LOGE("[SOFTBUS]SendHeartbeatData failed.");
124         return ERR_DM_FAILED;
125     }
126     return DM_OK;
127 }
128 
OnSessionOpened(int sessionId,int result)129 int SoftbusSession::OnSessionOpened(int sessionId, int result)
130 {
131     int32_t sessionSide = GetSessionSide(sessionId);
132     sessionCallback_->OnSessionOpened(sessionId, sessionSide, result);
133     LOGD("OnSessionOpened, success, sessionId: %d.", sessionId);
134     return DM_OK;
135 }
136 
OnSessionClosed(int sessionId)137 void SoftbusSession::OnSessionClosed(int sessionId)
138 {
139     LOGD("OnSessionClosed, sessionId: %d.", sessionId);
140 }
141 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)142 void SoftbusSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
143 {
144     if (sessionId < 0 || data == nullptr || dataLen <= 0) {
145         LOGI("[SOFTBUS]fail to receive data from softbus with sessionId: %d, dataLen: %d.", sessionId, dataLen);
146         return;
147     }
148     LOGI("start, sessionId: %d, dataLen: %d.", sessionId, dataLen);
149     if (sessionCallback_->GetIsCryptoSupport()) {
150         LOGI("Start decryption.");
151     }
152     std::string message = std::string(reinterpret_cast<const char *>(data), dataLen);
153     nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
154     if (jsonObject.is_discarded()) {
155         LOGE("DecodeRequestAuth jsonStr error");
156         return;
157     }
158     if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
159         LOGE("err json string, first time");
160         return;
161     }
162     if (jsonObject[TAG_MSG_TYPE].get<int32_t>() == AUTH_DEVICE_REQ_NEGOTIATE ||
163         jsonObject[TAG_MSG_TYPE].get<int32_t>() == AUTH_DEVICE_RESP_NEGOTIATE) {
164         sessionCallback_->OnAuthDeviceDataReceived(sessionId, message);
165     } else {
166         sessionCallback_->OnDataReceived(sessionId, message);
167     }
168     LOGI("completed.");
169 }
170 
OnUnbindSessionOpened(int sessionId,int result)171 void SoftbusSession::OnUnbindSessionOpened(int sessionId, int result)
172 {
173     int32_t sessionSide = GetSessionSide(sessionId);
174     sessionCallback_->OnUnbindSessionOpened(sessionId, sessionSide, result);
175     LOGD("OnUnbindSessionOpened, success, sessionId: %d.", sessionId);
176 }
177 
OpenUnbindSession(const std::string & netWorkId)178 int32_t SoftbusSession::OpenUnbindSession(const std::string &netWorkId)
179 {
180     int dataType = TYPE_BYTES;
181     int streamType = INVALID;
182     SessionAttribute attr = { 0 };
183     attr.dataType = dataType;
184     attr.linkTypeNum = LINK_TYPE_MAX;
185     LinkType linkTypeList[LINK_TYPE_MAX] = {
186         LINK_TYPE_WIFI_P2P,
187         LINK_TYPE_WIFI_WLAN_5G,
188         LINK_TYPE_WIFI_WLAN_2G,
189         LINK_TYPE_BR,
190     };
191     if (memcpy_s(attr.linkType, sizeof(attr.linkType), linkTypeList, sizeof(linkTypeList)) != DM_OK) {
192         LOGE("OpenUnbindSession: Data copy failed.");
193         return ERR_DM_FAILED;
194     }
195     attr.attr.streamAttr.streamType = streamType;
196     int32_t sessionId = OpenSession(DM_UNBIND_SESSION_NAME, DM_UNBIND_SESSION_NAME, netWorkId.c_str(), "0", &attr);
197     if (sessionId < 0) {
198         LOGE("[SOFTBUS]open OpenUnbindSession error, sessionId: %d.", sessionId);
199         return sessionId;
200     }
201     LOGI("OpenUnbindSession success. sessionId: %d.", sessionId);
202     return sessionId;
203 }
204 } // namespace DistributedHardware
205 } // namespace OHOS
206