• 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_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) const156 int32_t SoftbusAdapter::OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName,
157     const std::string &peerDevId) const
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_STREAM;
162     int streamType = COMMON_VIDEO_STREAM;
163     SessionAttribute attr = { 0 };
164     attr.dataType = dataType;
165     attr.linkTypeNum = LINK_TYPE_MAX;
166     LinkType linkTypeList[LINK_TYPE_MAX] = {
167         LINK_TYPE_WIFI_P2P,
168         LINK_TYPE_WIFI_WLAN_5G,
169         LINK_TYPE_WIFI_WLAN_2G,
170         LINK_TYPE_BR,
171     };
172     int32_t ret = memcpy_s(attr.linkType, sizeof(attr.linkType), linkTypeList, sizeof(linkTypeList));
173     if (ret != EOK) {
174         DHLOGE("%s: Data copy failed.", LOG_TAG);
175         return ERR_DH_SCREEN_ADAPTER_PARA_ERROR;
176     }
177     attr.attr.streamAttr.streamType = streamType;
178     int32_t sessionId = OpenSession(mySessionName.c_str(), peerSessionName.c_str(), peerDevId.c_str(), "0", &attr);
179     if (sessionId < 0) {
180         DHLOGE("%s: OpenSession failed sessionId: %." PRId32, LOG_TAG, sessionId);
181         return ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL;
182     }
183 
184     DHLOGI("%s: OpenSoftbusSession success sessionId: %." PRId32, LOG_TAG, sessionId);
185     return sessionId;
186 }
187 
CloseSoftbusSession(const int32_t sessionId)188 int32_t SoftbusAdapter::CloseSoftbusSession(const int32_t sessionId)
189 {
190     DHLOGI("%s: CloseSoftbusSession, sessid:%" PRId32, LOG_TAG, sessionId);
191     CloseSession(sessionId);
192 
193     std::lock_guard<std::mutex> lisLock(listenerMtx_);
194     mapSessListeners_.erase(sessionId);
195 
196     DHLOGI("%s: CloseSoftbusSession success.", LOG_TAG);
197     return DH_SUCCESS;
198 }
199 
SendSoftbusBytes(int32_t sessionId,const void * data,int32_t dataLen) const200 int32_t SoftbusAdapter::SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen) const
201 {
202     DHLOGD("%s: SendSoftbusBytes, sessid:%" PRId32, LOG_TAG, sessionId);
203     int32_t ret = SendBytes(sessionId, data, dataLen);
204     if (ret != DH_SUCCESS) {
205         DHLOGE("%s: SendBytes failed ret:%" PRId32, LOG_TAG, ret);
206         return ERR_DH_SCREEN_TRANS_ERROR;
207     }
208 
209     return DH_SUCCESS;
210 }
211 
SendSoftbusStream(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param) const212 int32_t SoftbusAdapter::SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext,
213     const StreamFrameInfo *param) const
214 {
215     DHLOGD("%s: SendSoftbusStream, sessid:%" PRId32, LOG_TAG, sessionId);
216     int32_t ret = SendStream(sessionId, data, ext, param);
217     if (ret != DH_SUCCESS) {
218         DHLOGE("%s: SendStream failed ret:%" PRId32, LOG_TAG, ret);
219         return ERR_DH_SCREEN_TRANS_ERROR;
220     }
221 
222     return DH_SUCCESS;
223 }
224 
GetSoftbusListenerByName(int32_t sessionId)225 std::shared_ptr<ISoftbusListener> &SoftbusAdapter::GetSoftbusListenerByName(int32_t sessionId)
226 {
227     char sessionName[DSCREEN_MAX_SESSION_NAME_LEN] = "";
228     char peerDevId[DSCREEN_MAX_DEVICE_ID_LEN] = "";
229     int32_t ret = GetPeerSessionName(sessionId, sessionName, sizeof(sessionName));
230     if (ret != DH_SUCCESS) {
231         DHLOGE("%s: GetPeerSessionName failed ret:%" PRId32, LOG_TAG, ret);
232         return nullListener_;
233     }
234     ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId));
235     if (ret != DH_SUCCESS) {
236         DHLOGE("%s: GetPeerDeviceId failed ret:%" PRId32, LOG_TAG, ret);
237         return nullListener_;
238     }
239 
240     std::string sessionNameStr(sessionName);
241     std::string peerDevIdStr(peerDevId);
242     DHLOGD("%s: GetSoftbusListenerByName sessionName:%s, peerDevId:%s.", LOG_TAG, sessionNameStr.c_str(),
243         GetAnonyString(peerDevIdStr).c_str());
244     std::string strListenerKey = sessionNameStr + "_" + peerDevIdStr;
245 
246     std::lock_guard<std::mutex> lisLock(listenerMtx_);
247     if (mapListeners_.find(strListenerKey) == mapListeners_.end()) {
248         DHLOGE("%s: Find listener failed.", LOG_TAG);
249         return nullListener_;
250     }
251     return mapListeners_[strListenerKey];
252 }
253 
GetSoftbusListenerById(int32_t sessionId)254 std::shared_ptr<ISoftbusListener> &SoftbusAdapter::GetSoftbusListenerById(int32_t sessionId)
255 {
256     std::lock_guard<std::mutex> lisLock(listenerMtx_);
257     if (mapSessListeners_.find(sessionId) == mapSessListeners_.end()) {
258         DHLOGE("%s: Find listener failed.", LOG_TAG);
259         return nullListener_;
260     }
261 
262     return mapSessListeners_[sessionId];
263 }
264 
OnSoftbusSessionOpened(int32_t sessionId,int32_t result)265 int32_t SoftbusAdapter::OnSoftbusSessionOpened(int32_t sessionId, int32_t result)
266 {
267     DHLOGI("%s: OnSessionOpened session:%" PRId32", result:%" PRId32, LOG_TAG, sessionId, result);
268     if (result != DH_SUCCESS) {
269         DHLOGE("%s: OnSessionOpened failed.", LOG_TAG);
270         return ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL;
271     }
272 
273     std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId);
274     if (listener == nullptr) {
275         DHLOGE("Get softbus listener failed.");
276         return ERR_DH_SCREEN_TRANS_ERROR;
277     }
278     listener->OnSessionOpened(sessionId, result);
279 
280     std::lock_guard<std::mutex> lisLock(listenerMtx_);
281     mapSessListeners_.insert(std::make_pair(sessionId, listener));
282 
283     return DH_SUCCESS;
284 }
285 
OnSoftbusSessionClosed(int32_t sessionId)286 void SoftbusAdapter::OnSoftbusSessionClosed(int32_t sessionId)
287 {
288     DHLOGI("%s: OnSessionClosed sessionId:%" PRId32, LOG_TAG, sessionId);
289     std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerById(sessionId);
290     if (listener == nullptr) {
291         DHLOGE("Get softbus listener failed.");
292         return;
293     }
294     listener->OnSessionClosed(sessionId);
295 
296     std::lock_guard<std::mutex> lisLock(listenerMtx_);
297     mapSessListeners_.erase(sessionId);
298 }
299 
OnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)300 void SoftbusAdapter::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
301 {
302     DHLOGD("%s: OnBytesReceived, sessionId:%" PRId32, LOG_TAG, sessionId);
303     if (data == nullptr) {
304         DHLOGE("BytesData is null.");
305         return;
306     }
307     if (dataLen == 0 || dataLen > DSCREEN_MAX_RECV_DATA_LEN) {
308         DHLOGE("BytesData length is too large, dataLen:%u.", dataLen);
309         return;
310     }
311 
312     std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId);
313     if (listener == nullptr) {
314         DHLOGE("Get softbus listener failed.");
315         return;
316     }
317     listener->OnBytesReceived(sessionId, data, dataLen);
318 }
319 
OnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frameInfo)320 void SoftbusAdapter::OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
321     const StreamFrameInfo *frameInfo)
322 {
323     DHLOGD("%s OnStreamReceived, sessionId:%" PRId32, LOG_TAG, sessionId);
324     if (data == nullptr) {
325         DHLOGE("StreamData is null.");
326         return;
327     }
328     if (data->bufLen <= 0 || data->bufLen > static_cast<int32_t>(DSCREEN_MAX_RECV_DATA_LEN)) {
329         DHLOGE("StreamData length is too large, dataLen:%" PRId32, data->bufLen);
330         return;
331     }
332 
333     std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId);
334     if (listener == nullptr) {
335         DHLOGE("Get softbus listener failed.");
336         return;
337     }
338     listener->OnStreamReceived(sessionId, data, ext, frameInfo);
339 }
340 
OnMessageReceived(int sessionId,const void * data,unsigned int dataLen) const341 void SoftbusAdapter::OnMessageReceived(int sessionId, const void *data, unsigned int dataLen) const
342 {
343     (void)data;
344     (void)dataLen;
345     DHLOGD("%s OnMessageReceived, sessionId:%" PRId32, LOG_TAG, sessionId);
346 }
347 
OnQosEvent(int sessionId,int eventId,int tvCount,const QosTv * tvList) const348 void SoftbusAdapter::OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList) const
349 {
350     (void)eventId;
351     (void)tvCount;
352     (void)tvList;
353     DHLOGD("%s OnQosEvent, sessionId:%" PRId32, LOG_TAG, sessionId);
354 }
355 } // namespace DistributedHardware
356 } // namespace OHOS