• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef DEVICE_MANAGER_COMMON_FLAG
27 #include "session_ex.h"
28 #endif
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 std::shared_ptr<ISoftbusSessionCallback> SoftbusSession::sessionCallback_ = nullptr;
33 constexpr const char* DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION";
34 constexpr int32_t MAX_DATA_LEN = 65535;
35 
OnShutdown(int32_t socket,ShutdownReason reason)36 static void OnShutdown(int32_t socket, ShutdownReason reason)
37 {
38     LOGI("[SOFTBUS]OnShutdown socket : %{public}d, reason: %{public}d", socket, (int32_t)reason);
39     SoftbusSession::OnSessionClosed(socket);
40 }
41 
OnBytes(int32_t socket,const void * data,uint32_t dataLen)42 static void OnBytes(int32_t socket, const void *data, uint32_t dataLen)
43 {
44     LOGI("[SOFTBUS]OnBytes socket : %{public}d", socket);
45     SoftbusSession::OnBytesReceived(socket, data, dataLen);
46 }
47 
OnQos(int32_t socket,QoSEvent eventId,const QosTV * qos,uint32_t qosCount)48 static void OnQos(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount)
49 {
50     LOGI("[SOFTBUS]OnQos, socket: %{public}d, QoSEvent: %{public}d, qosCount: %{public}u", socket, (int32_t)eventId,
51         qosCount);
52     if (qosCount > QOS_TYPE_BUTT) {
53         LOGE("OnQos invalid qosCount");
54         return;
55     }
56     for (uint32_t idx = 0; idx < qosCount; idx++) {
57         LOGI("QosTV: type: %{public}d, value: %{public}d", (int32_t)qos[idx].qos, qos[idx].value);
58     }
59 }
60 
SoftbusSession()61 SoftbusSession::SoftbusSession()
62 {
63     LOGD("SoftbusSession constructor.");
64     if (memset_s(&iSocketListener_, sizeof(ISocketListener), 0, sizeof(ISocketListener)) != DM_OK) {
65         LOGE("SoftbusSession::SoftbusSession memset_s failed.");
66         return;
67     }
68 
69     iSocketListener_.OnShutdown = OnShutdown;
70     iSocketListener_.OnBytes = OnBytes;
71     iSocketListener_.OnQos = OnQos;
72     iSocketListener_.OnFile = nullptr;
73     iSocketListener_.OnMessage = nullptr;
74     iSocketListener_.OnBind = nullptr;
75     iSocketListener_.OnStream = nullptr;
76     iSocketListener_.OnError = nullptr;
77     iSocketListener_.OnNegotiate = nullptr;
78 }
79 
~SoftbusSession()80 SoftbusSession::~SoftbusSession()
81 {
82     LOGD("SoftbusSession destructor.");
83 }
84 
RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)85 int32_t SoftbusSession::RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback> callback)
86 {
87     sessionCallback_ = callback;
88     return DM_OK;
89 }
90 
UnRegisterSessionCallback()91 int32_t SoftbusSession::UnRegisterSessionCallback()
92 {
93     sessionCallback_ = nullptr;
94     return DM_OK;
95 }
96 
OpenAuthSession(const std::string & deviceId)97 int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId)
98 {
99     DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION));
100     int32_t sessionId = -1;
101     std::string connectAddr;
102     ConnectionAddr *addrInfo = SoftbusConnector::GetConnectAddr(deviceId, connectAddr);
103     if (addrInfo == nullptr) {
104         LOGE("[SOFTBUS]addrInfo is nullptr. sessionId: %{public}d.", sessionId);
105         return sessionId;
106     }
107     sessionId = ::OpenAuthSession(DM_SESSION_NAME, addrInfo, 1, nullptr);
108     if (sessionId < 0) {
109         LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId);
110         return sessionId;
111     }
112     DmTraceEnd();
113     LOGI("OpenAuthSession success. sessionId: %{public}d.", sessionId);
114     return sessionId;
115 }
116 
OpenAuthSessionWithPara(const std::string & deviceId,int32_t actionId,bool isEnable160m)117 int32_t SoftbusSession::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m)
118 {
119 #ifdef DEVICE_MANAGER_COMMON_FLAG
120     LOGE("[SOFTBUS] OpenAuthSessionWithPara no implement");
121     return SOFTBUS_NOT_IMPLEMENT;
122 #else
123     DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION));
124     LinkPara para;
125     para.type = PARA_ACTION;
126     para.action.actionId = actionId;
127     para.enable160M = isEnable160m;
128     para.accountInfo = false;
129     int32_t sessionId = ::OpenAuthSessionWithPara(DM_SESSION_NAME, &para);
130     if (sessionId < 0) {
131         LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId);
132         return sessionId;
133     }
134     DmTraceEnd();
135     LOGI("OpenAuthSessionWithPara success. sessionId: %{public}d.", sessionId);
136     return sessionId;
137 #endif
138 }
139 
CloseAuthSession(int32_t sessionId)140 int32_t SoftbusSession::CloseAuthSession(int32_t sessionId)
141 {
142     LOGI("CloseAuthSession.");
143     ::CloseSession(sessionId);
144     return DM_OK;
145 }
146 
GetPeerDeviceId(int32_t sessionId,std::string & peerDevId)147 int32_t SoftbusSession::GetPeerDeviceId(int32_t sessionId, std::string &peerDevId)
148 {
149     char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
150     int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
151     if (ret == DM_OK) {
152         peerDevId = peerDeviceId;
153         LOGI("[SOFTBUS]GetPeerDeviceId success for session: %{public}d, peerDeviceId: %{public}s.", sessionId,
154             GetAnonyString(peerDevId).c_str());
155         return DM_OK;
156     }
157     LOGE("[SOFTBUS]GetPeerDeviceId failed for session: %{public}d, ret: %{public}d.", sessionId, ret);
158     peerDevId = "";
159     return ret;
160 }
161 
SendData(int32_t sessionId,std::string & message)162 int32_t SoftbusSession::SendData(int32_t sessionId, std::string &message)
163 {
164     JsonObject jsonObject(message);
165     if (jsonObject.IsDiscarded()) {
166         LOGE("extrasJson error, message: %{public}s.", GetAnonyString(message).c_str());
167         return ERR_DM_FAILED;
168     }
169     if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
170         LOGE("SoftbusSession::SendData err json string.");
171         return ERR_DM_FAILED;
172     }
173     int32_t msgType = jsonObject[TAG_MSG_TYPE].Get<int32_t>();
174     LOGI("start, msgType: %{public}d.", msgType);
175     if (sessionCallback_->GetIsCryptoSupport()) {
176         LOGI("SendData Start encryption.");
177     }
178     int32_t ret = SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
179     if (ret != DM_OK) {
180         LOGE("[SOFTBUS]SendBytes failed.");
181         return ret;
182     }
183     return DM_OK;
184 }
185 
SendHeartbeatData(int32_t sessionId,std::string & message)186 int32_t SoftbusSession::SendHeartbeatData(int32_t sessionId, std::string &message)
187 {
188     int32_t ret = SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
189     if (ret != DM_OK) {
190         LOGE("[SOFTBUS]SendHeartbeatData failed.");
191         return ret;
192     }
193     return DM_OK;
194 }
195 
OnSessionOpened(int sessionId,int result)196 int SoftbusSession::OnSessionOpened(int sessionId, int result)
197 {
198     int32_t sessionSide = GetSessionSide(sessionId);
199     sessionCallback_->OnSessionOpened(sessionId, sessionSide, result);
200     LOGD("OnSessionOpened, success, sessionId: %{public}d.", sessionId);
201     return DM_OK;
202 }
203 
OnSessionClosed(int sessionId)204 void SoftbusSession::OnSessionClosed(int sessionId)
205 {
206     LOGI("OnSessionClosed, sessionId: %{public}d.", sessionId);
207     CHECK_NULL_VOID(sessionCallback_);
208     sessionCallback_->OnSessionClosed(sessionId);
209 }
210 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)211 void SoftbusSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
212 {
213     if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > MAX_DATA_LEN) {
214         LOGI("[SOFTBUS]fail to receive data from softbus with sessionId: %{public}d, dataLen: %{public}d.", sessionId,
215             dataLen);
216         return;
217     }
218     LOGI("start, sessionId: %{public}d, dataLen: %{public}d.", sessionId, dataLen);
219     if (sessionCallback_->GetIsCryptoSupport()) {
220         LOGI("Start decryption.");
221     }
222     std::string message = std::string(reinterpret_cast<const char *>(data), dataLen);
223     JsonObject jsonObject(message);
224     if (jsonObject.IsDiscarded()) {
225         LOGE("DecodeRequestAuth jsonStr error");
226         return;
227     }
228     if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
229         LOGE("err json string, first time");
230         return;
231     }
232     if (jsonObject[TAG_MSG_TYPE].Get<int32_t>() == AUTH_DEVICE_REQ_NEGOTIATE ||
233         jsonObject[TAG_MSG_TYPE].Get<int32_t>() == AUTH_DEVICE_RESP_NEGOTIATE) {
234         sessionCallback_->OnAuthDeviceDataReceived(sessionId, message);
235     } else {
236         sessionCallback_->OnDataReceived(sessionId, message);
237     }
238     LOGI("completed.");
239 }
240 } // namespace DistributedHardware
241 } // namespace OHOS
242