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_adapter.h"
17
18 #include <securec.h>
19 #include <unistd.h>
20
21 #include "softbus_bus_center.h"
22 #include "softbus_common.h"
23
24 #include "dscreen_errcode.h"
25 #include "dscreen_hisysevent.h"
26 #include "dscreen_util.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 IMPLEMENT_SINGLE_INSTANCE(SoftbusAdapter);
ScreenOnSoftbusSessionOpened(int32_t sessionId,int32_t result)31 static int32_t ScreenOnSoftbusSessionOpened(int32_t sessionId, int32_t result)
32 {
33 return SoftbusAdapter::GetInstance().OnSoftbusSessionOpened(sessionId, result);
34 }
35
ScreenOnSoftbusSessionClosed(int32_t sessionId)36 static void ScreenOnSoftbusSessionClosed(int32_t sessionId)
37 {
38 SoftbusAdapter::GetInstance().OnSoftbusSessionClosed(sessionId);
39 }
40
ScreenOnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)41 static void ScreenOnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
42 {
43 SoftbusAdapter::GetInstance().OnBytesReceived(sessionId, data, dataLen);
44 }
45
ScreenOnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frameInfo)46 static void ScreenOnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
47 const StreamFrameInfo *frameInfo)
48 {
49 SoftbusAdapter::GetInstance().OnStreamReceived(sessionId, data, ext, frameInfo);
50 }
51
ScreenOnMessageReceived(int sessionId,const void * data,unsigned int dataLen)52 static void ScreenOnMessageReceived(int sessionId, const void *data, unsigned int dataLen)
53 {
54 SoftbusAdapter::GetInstance().OnMessageReceived(sessionId, data, dataLen);
55 }
56
ScreenOnQosEvent(int sessionId,int eventId,int tvCount,const QosTv * tvList)57 static void ScreenOnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList)
58 {
59 SoftbusAdapter::GetInstance().OnQosEvent(sessionId, eventId, tvCount, tvList);
60 }
61
SoftbusAdapter()62 SoftbusAdapter::SoftbusAdapter()
63 {
64 DHLOGI("SoftbusAdapter");
65 sessListener_.OnSessionOpened = ScreenOnSoftbusSessionOpened;
66 sessListener_.OnSessionClosed = ScreenOnSoftbusSessionClosed;
67 sessListener_.OnBytesReceived = ScreenOnBytesReceived;
68 sessListener_.OnStreamReceived = ScreenOnStreamReceived;
69 sessListener_.OnMessageReceived = ScreenOnMessageReceived;
70 sessListener_.OnQosEvent = ScreenOnQosEvent;
71 }
72
~SoftbusAdapter()73 SoftbusAdapter::~SoftbusAdapter()
74 {
75 DHLOGI("~SoftbusAdapter");
76 }
77
RegisterSoftbusListener(const std::shared_ptr<ISoftbusListener> & listener,const std::string & sessionName,const std::string & peerDevId)78 int32_t SoftbusAdapter::RegisterSoftbusListener(const std::shared_ptr<ISoftbusListener> &listener,
79 const std::string &sessionName, const std::string &peerDevId)
80 {
81 if (listener == nullptr) {
82 DHLOGE("%s: listener is nullptr.", LOG_TAG);
83 return ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL;
84 }
85 DHLOGI("%s: RegisterListener sess:%s id:%s.", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str());
86 std::string strListenerKey = sessionName + "_" + peerDevId;
87
88 std::lock_guard<std::mutex> lisLock(listenerMtx_);
89 if (mapListeners_.find(strListenerKey) != mapListeners_.end()) {
90 DHLOGE("%s: Session listener already register.", LOG_TAG);
91 return ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL;
92 }
93 mapListeners_.insert(std::make_pair(strListenerKey, listener));
94
95 return DH_SUCCESS;
96 }
97
UnRegisterSoftbusListener(const std::string & sessionName,const std::string & peerDevId)98 int32_t SoftbusAdapter::UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId)
99 {
100 DHLOGI("%s: UnRegisterListener sess:%s id:%s.", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str());
101 std::string strListenerKey = sessionName + "_" + peerDevId;
102
103 std::lock_guard<std::mutex> lisLock(listenerMtx_);
104 mapListeners_.erase(strListenerKey);
105
106 return DH_SUCCESS;
107 }
108
CreateSoftbusSessionServer(const std::string & pkgname,const std::string & sessionName,const std::string & peerDevId)109 int32_t SoftbusAdapter::CreateSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName,
110 const std::string &peerDevId)
111 {
112 DHLOGI("%s: CreateSessionServer sess:%s id:%s.", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str());
113 std::lock_guard<std::mutex> setLock(sessSetMtx_);
114 if (mapSessionSet_.find(sessionName) == mapSessionSet_.end()) {
115 int32_t ret = CreateSessionServer(pkgname.c_str(), sessionName.c_str(), &sessListener_);
116 if (ret != DH_SUCCESS) {
117 DHLOGE("%s: CreateSessionServer failed.", LOG_TAG);
118 return ret;
119 }
120 } else {
121 DHLOGD("%s: Session already create.", sessionName.c_str());
122 }
123
124 mapSessionSet_[sessionName].insert(peerDevId);
125 DHLOGI("%s: CreateSessionServer success.", LOG_TAG);
126 return DH_SUCCESS;
127 }
128
RemoveSoftbusSessionServer(const std::string & pkgname,const std::string & sessionName,const std::string & peerDevId)129 int32_t SoftbusAdapter::RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName,
130 const std::string &peerDevId)
131 {
132 DHLOGI("%s: RemoveSessionServer sess:%s id:%s", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str());
133 std::lock_guard<std::mutex> setLock(sessSetMtx_);
134 if (mapSessionSet_.find(sessionName) == mapSessionSet_.end()) {
135 DHLOGE("%s: Session name can not find.", LOG_TAG);
136 return ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION;
137 } else if (mapSessionSet_[sessionName].find(peerDevId) == mapSessionSet_[sessionName].end()) {
138 DHLOGE("%s: PeerDevId can not find.", LOG_TAG);
139 return ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION;
140 }
141
142 int32_t ret = RemoveSessionServer(pkgname.c_str(), sessionName.c_str());
143 if (ret != DH_SUCCESS) {
144 DHLOGE("%s: RemoveSessionServer failed.", LOG_TAG);
145 return ret;
146 }
147
148 mapSessionSet_[sessionName].erase(peerDevId);
149 if (mapSessionSet_[sessionName].empty()) {
150 mapSessionSet_.erase(sessionName);
151 }
152 DHLOGI("%s: RemoveSessionServer success.", LOG_TAG);
153 return DH_SUCCESS;
154 }
155
OpenSoftbusSession(const std::string & mySessionName,const std::string & peerSessionName,const std::string & peerDevId)156 int32_t SoftbusAdapter::OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName,
157 const std::string &peerDevId)
158 {
159 DHLOGI("%s: OpenSoftbusSession mysess:%s peersess:%s id:%s.", LOG_TAG, mySessionName.c_str(),
160 peerSessionName.c_str(), GetAnonyString(peerDevId).c_str());
161 int dataType = TYPE_BYTES;
162 int streamType = -1;
163 if (mySessionName == DATA_SESSION_NAME) {
164 dataType = TYPE_STREAM;
165 streamType = RAW_STREAM;
166 }
167
168 SessionAttribute attr = { 0 };
169 attr.dataType = dataType;
170 attr.linkTypeNum = LINK_TYPE_MAX;
171 LinkType linkTypeList[LINK_TYPE_MAX] = {
172 LINK_TYPE_WIFI_P2P,
173 LINK_TYPE_WIFI_WLAN_5G,
174 LINK_TYPE_WIFI_WLAN_2G,
175 LINK_TYPE_BR,
176 };
177 int32_t ret = memcpy_s(attr.linkType, sizeof(attr.linkType), linkTypeList, sizeof(linkTypeList));
178 if (ret != EOK) {
179 DHLOGE("%s: Data copy failed.", LOG_TAG);
180 return ERR_DH_SCREEN_ADAPTER_PARA_ERROR;
181 }
182 attr.attr.streamAttr.streamType = streamType;
183 int32_t sessionId = OpenSession(mySessionName.c_str(), peerSessionName.c_str(), peerDevId.c_str(), "0", &attr);
184 if (sessionId < 0) {
185 DHLOGE("%s: OpenSession failed sessionId:%d.", LOG_TAG, sessionId);
186 return ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL;
187 }
188
189 DHLOGI("%s: OpenSoftbusSession success sessionId:%d.", LOG_TAG, sessionId);
190 return sessionId;
191 }
192
CloseSoftbusSession(const int32_t sessionId)193 int32_t SoftbusAdapter::CloseSoftbusSession(const int32_t sessionId)
194 {
195 DHLOGI("%s: CloseSoftbusSession, sessid:%d.", LOG_TAG, sessionId);
196 CloseSession(sessionId);
197
198 std::lock_guard<std::mutex> lisLock(listenerMtx_);
199 mapSessListeners_.erase(sessionId);
200
201 DHLOGI("%s: CloseSoftbusSession success.", LOG_TAG);
202 return DH_SUCCESS;
203 }
204
SendSoftbusBytes(int32_t sessionId,const void * data,int32_t dataLen)205 int32_t SoftbusAdapter::SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen)
206 {
207 DHLOGD("%s: SendSoftbusBytes, sessid:%d.", LOG_TAG, sessionId);
208 int32_t ret = SendBytes(sessionId, data, dataLen);
209 if (ret != DH_SUCCESS) {
210 DHLOGE("%s: SendBytes failed ret:%d.", LOG_TAG, ret);
211 return ERR_DH_SCREEN_TRANS_ERROR;
212 }
213
214 return DH_SUCCESS;
215 }
216
SendSoftbusStream(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)217 int32_t SoftbusAdapter::SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext,
218 const StreamFrameInfo *param)
219 {
220 DHLOGD("%s: SendSoftbusStream, sessid:%d.", LOG_TAG, sessionId);
221 int32_t ret = SendStream(sessionId, data, ext, param);
222 if (ret != DH_SUCCESS) {
223 DHLOGE("%s: SendStream failed ret:%d.", LOG_TAG, ret);
224 return ERR_DH_SCREEN_TRANS_ERROR;
225 }
226
227 return DH_SUCCESS;
228 }
229
GetSoftbusListenerByName(int32_t sessionId)230 std::shared_ptr<ISoftbusListener> &SoftbusAdapter::GetSoftbusListenerByName(int32_t sessionId)
231 {
232 char sessionName[DSCREEN_MAX_SESSION_NAME_LEN] = "";
233 char peerDevId[DSCREEN_MAX_DEVICE_ID_LEN] = "";
234 int32_t ret = GetPeerSessionName(sessionId, sessionName, sizeof(sessionName));
235 if (ret != DH_SUCCESS) {
236 DHLOGE("%s: GetPeerSessionName failed ret:%d.", LOG_TAG, ret);
237 return nullListener_;
238 }
239 ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId));
240 if (ret != DH_SUCCESS) {
241 DHLOGE("%s: GetPeerDeviceId failed ret:%d.", LOG_TAG, ret);
242 return nullListener_;
243 }
244
245 std::string sessionNameStr(sessionName);
246 std::string peerDevIdStr(peerDevId);
247 DHLOGD("%s: GetSoftbusListenerByName sessionName:%s, peerDevId:%s.", LOG_TAG, sessionNameStr.c_str(),
248 GetAnonyString(peerDevIdStr).c_str());
249 std::string strListenerKey = sessionNameStr + "_" + peerDevIdStr;
250
251 std::lock_guard<std::mutex> lisLock(listenerMtx_);
252 if (mapListeners_.find(strListenerKey) == mapListeners_.end()) {
253 DHLOGE("%s: Find listener failed.", LOG_TAG);
254 return nullListener_;
255 }
256 return mapListeners_[strListenerKey];
257 }
258
GetSoftbusListenerById(int32_t sessionId)259 std::shared_ptr<ISoftbusListener> &SoftbusAdapter::GetSoftbusListenerById(int32_t sessionId)
260 {
261 std::lock_guard<std::mutex> lisLock(listenerMtx_);
262 if (mapSessListeners_.find(sessionId) == mapSessListeners_.end()) {
263 DHLOGE("%s: Find listener failed.", LOG_TAG);
264 return nullListener_;
265 }
266
267 return mapSessListeners_[sessionId];
268 }
269
OnSoftbusSessionOpened(int32_t sessionId,int32_t result)270 int32_t SoftbusAdapter::OnSoftbusSessionOpened(int32_t sessionId, int32_t result)
271 {
272 DHLOGI("%s: OnSessionOpened session:%d, result:%d.", LOG_TAG, sessionId, result);
273 if (result != DH_SUCCESS) {
274 DHLOGE("%s: OnSessionOpened failed.", LOG_TAG);
275 return ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL;
276 }
277
278 std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId);
279 if (listener == nullptr) {
280 DHLOGE("Get softbus listener failed.");
281 return ERR_DH_SCREEN_TRANS_ERROR;
282 }
283 listener->OnSessionOpened(sessionId, result);
284
285 std::lock_guard<std::mutex> lisLock(listenerMtx_);
286 mapSessListeners_.insert(std::make_pair(sessionId, listener));
287
288 return DH_SUCCESS;
289 }
290
OnSoftbusSessionClosed(int32_t sessionId)291 void SoftbusAdapter::OnSoftbusSessionClosed(int32_t sessionId)
292 {
293 DHLOGI("%s: OnSessionClosed sessionId:%d.", LOG_TAG, sessionId);
294 std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerById(sessionId);
295 if (listener == nullptr) {
296 DHLOGE("Get softbus listener failed.");
297 return;
298 }
299 listener->OnSessionClosed(sessionId);
300
301 std::lock_guard<std::mutex> lisLock(listenerMtx_);
302 mapSessListeners_.erase(sessionId);
303 }
304
OnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)305 void SoftbusAdapter::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
306 {
307 DHLOGD("%s: OnBytesReceived, sessionId:%d.", LOG_TAG, sessionId);
308 if (data == nullptr) {
309 DHLOGE("BytesData is null.");
310 return;
311 }
312 if (dataLen == 0 || dataLen > DSCREEN_MAX_RECV_DATA_LEN) {
313 DHLOGE("BytesData length is too large, dataLen:%u.", dataLen);
314 return;
315 }
316
317 std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId);
318 if (listener == nullptr) {
319 DHLOGE("Get softbus listener failed.");
320 return;
321 }
322 listener->OnBytesReceived(sessionId, data, dataLen);
323 }
324
OnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frameInfo)325 void SoftbusAdapter::OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
326 const StreamFrameInfo *frameInfo)
327 {
328 DHLOGD("%s OnStreamReceived, sessionId:%d.", LOG_TAG, sessionId);
329 if (data == nullptr) {
330 DHLOGE("StreamData is null.");
331 return;
332 }
333 if (data->bufLen <= 0 || data->bufLen > (int32_t)DSCREEN_MAX_RECV_DATA_LEN) {
334 DHLOGE("StreamData length is too large, dataLen:%d.", data->bufLen);
335 return;
336 }
337
338 std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId);
339 if (listener == nullptr) {
340 DHLOGE("Get softbus listener failed.");
341 return;
342 }
343 listener->OnStreamReceived(sessionId, data, ext, frameInfo);
344 }
345
OnMessageReceived(int sessionId,const void * data,unsigned int dataLen)346 void SoftbusAdapter::OnMessageReceived(int sessionId, const void *data, unsigned int dataLen)
347 {
348 DHLOGD("%s OnMessageReceived, sessionId:%d.", LOG_TAG, sessionId);
349 }
350
OnQosEvent(int sessionId,int eventId,int tvCount,const QosTv * tvList)351 void SoftbusAdapter::OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList)
352 {
353 DHLOGD("%s OnQosEvent, sessionId:%d.", LOG_TAG, sessionId);
354 }
355 } // namespace DistributedHardware
356 } // namespace OHOS