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_channel_adapter.h"
17
18 #include <securec.h>
19 #include <thread>
20 #include <unistd.h>
21
22 #include "av_trans_constants.h"
23 #include "av_trans_errno.h"
24 #include "av_trans_log.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "SoftbusChannelAdapter"
30
31 IMPLEMENT_SINGLE_INSTANCE(SoftbusChannelAdapter);
32
OnSessionOpened(int32_t sessionId,int32_t result)33 static int32_t OnSessionOpened(int32_t sessionId, int32_t result)
34 {
35 return SoftbusChannelAdapter::GetInstance().OnSoftbusChannelOpened(sessionId, result);
36 }
37
OnSessionClosed(int32_t sessionId)38 static void OnSessionClosed(int32_t sessionId)
39 {
40 SoftbusChannelAdapter::GetInstance().OnSoftbusChannelClosed(sessionId);
41 }
42
OnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)43 static void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
44 {
45 SoftbusChannelAdapter::GetInstance().OnSoftbusBytesReceived(sessionId, data, dataLen);
46 }
47
OnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frameInfo)48 static void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
49 const StreamFrameInfo *frameInfo)
50 {
51 SoftbusChannelAdapter::GetInstance().OnSoftbusStreamReceived(sessionId, data, ext, frameInfo);
52 }
53
onDevTimeSyncResult(const TimeSyncResultInfo * info,int32_t result)54 static void onDevTimeSyncResult(const TimeSyncResultInfo *info, int32_t result)
55 {
56 SoftbusChannelAdapter::GetInstance().OnSoftbusTimeSyncResult(info, result);
57 }
58
SoftbusChannelAdapter()59 SoftbusChannelAdapter::SoftbusChannelAdapter()
60 {
61 sessListener_.OnSessionOpened = OnSessionOpened;
62 sessListener_.OnSessionClosed = OnSessionClosed;
63 sessListener_.OnBytesReceived = OnBytesReceived;
64 sessListener_.OnStreamReceived = OnStreamReceived;
65 sessListener_.OnMessageReceived = nullptr;
66 sessListener_.OnQosEvent = nullptr;
67 }
68
~SoftbusChannelAdapter()69 SoftbusChannelAdapter::~SoftbusChannelAdapter()
70 {
71 listenerMap_.clear();
72 sessionNameSet_.clear();
73 timeSyncSessNames_.clear();
74 devId2SessIdMap_.clear();
75 }
76
CreateChannelServer(const std::string & pkgName,const std::string & sessName)77 int32_t SoftbusChannelAdapter::CreateChannelServer(const std::string& pkgName, const std::string &sessName)
78 {
79 AVTRANS_LOGI("Create session server for sessionName:%s.", sessName.c_str());
80 TRUE_RETURN_V_MSG_E(pkgName.empty(), ERR_DH_AVT_INVALID_PARAM, "input pkgName is empty.");
81 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessionName is empty.");
82
83 std::lock_guard<std::mutex> setLock(name2IdMtx_);
84 if (sessionNameSet_.find(sessName) == sessionNameSet_.end()) {
85 int32_t ret = CreateSessionServer(pkgName.c_str(), sessName.c_str(), &sessListener_);
86 TRUE_RETURN_V_MSG_E(ret != DH_AVT_SUCCESS, ERR_DH_AVT_CREATE_CHANNEL_FAILED, "create session server failed");
87 } else {
88 AVTRANS_LOGE("Session has already created for name:%s", sessName.c_str());
89 }
90 sessionNameSet_.insert(sessName);
91
92 AVTRANS_LOGI("Create session server success for sessionName:%s.", sessName.c_str());
93 return DH_AVT_SUCCESS;
94 }
95
RemoveChannelServer(const std::string & pkgName,const std::string & sessName)96 int32_t SoftbusChannelAdapter::RemoveChannelServer(const std::string& pkgName, const std::string &sessName)
97 {
98 AVTRANS_LOGI("Remove session server for sessionName:%s.", sessName.c_str());
99 TRUE_RETURN_V_MSG_E(pkgName.empty(), ERR_DH_AVT_INVALID_PARAM, "input pkgName is empty.");
100 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessionName is empty.");
101
102 std::lock_guard<std::mutex> setLock(name2IdMtx_);
103 if (sessionNameSet_.find(sessName) == sessionNameSet_.end()) {
104 AVTRANS_LOGE("Can not found session name:%s", sessName.c_str());
105 return ERR_DH_AVT_INVALID_PARAM_VALUE;
106 }
107 RemoveSessionServer(pkgName.c_str(), sessName.c_str());
108 sessionNameSet_.erase(sessName);
109
110 AVTRANS_LOGI("Remove session server success for sessionName:%s.", sessName.c_str());
111 return DH_AVT_SUCCESS;
112 }
113
OpenSoftbusChannel(const std::string & mySessName,const std::string & peerSessName,const std::string & peerDevId)114 int32_t SoftbusChannelAdapter::OpenSoftbusChannel(const std::string& mySessName, const std::string &peerSessName,
115 const std::string &peerDevId)
116 {
117 AVTRANS_LOGI("Open softbus channel for mySessName:%s, peerSessName:%s, peerDevId:%s.",
118 mySessName.c_str(), peerSessName.c_str(), GetAnonyString(peerDevId).c_str());
119 TRUE_RETURN_V_MSG_E(mySessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input mySessName is empty.");
120 TRUE_RETURN_V_MSG_E(peerSessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerSessName is empty.");
121 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
122
123 int32_t existSessId = GetSessIdBySessName(mySessName, peerDevId);
124 if (existSessId > 0) {
125 AVTRANS_LOGI("Softbus channel already opened, sessionId:%" PRId32, existSessId);
126 return ERR_DH_AVT_SESSION_HAS_OPENED;
127 }
128
129 int dataType = TYPE_BYTES;
130 int streamType = INVALID;
131 if (mySessName.find("avtrans.data") != std::string::npos) {
132 dataType = TYPE_STREAM;
133 streamType = COMMON_VIDEO_STREAM;
134 }
135
136 SessionAttribute attr = { 0 };
137 attr.dataType = dataType;
138 attr.linkTypeNum = LINK_TYPE_MAX;
139 LinkType linkTypeList[LINK_TYPE_MAX] = {
140 LINK_TYPE_WIFI_P2P,
141 LINK_TYPE_WIFI_WLAN_5G,
142 LINK_TYPE_WIFI_WLAN_2G,
143 LINK_TYPE_BR,
144 };
145 int32_t ret = memcpy_s(attr.linkType, sizeof(attr.linkType), linkTypeList, sizeof(linkTypeList));
146 if (ret != EOK) {
147 AVTRANS_LOGE("Data copy failed.");
148 return ERR_DH_AVT_NO_MEMORY;
149 }
150 attr.attr.streamAttr.streamType = streamType;
151 int32_t sessionId = OpenSession(mySessName.c_str(), peerSessName.c_str(), peerDevId.c_str(), "0", &attr);
152 if (sessionId < 0) {
153 AVTRANS_LOGE("Open softbus session failed for sessionId:%" PRId32, sessionId);
154 return ERR_DH_AVT_SESSION_ERROR;
155 }
156 {
157 std::lock_guard<std::mutex> lock(idMapMutex_);
158 devId2SessIdMap_.insert(std::make_pair(mySessName + "_" + peerDevId, sessionId));
159 }
160 AVTRANS_LOGI("Open softbus channel finished, sessionId:%" PRId32, sessionId);
161 return DH_AVT_SUCCESS;
162 }
163
CloseSoftbusChannel(const std::string & sessName,const std::string & peerDevId)164 int32_t SoftbusChannelAdapter::CloseSoftbusChannel(const std::string& sessName, const std::string &peerDevId)
165 {
166 AVTRANS_LOGI("Close softbus channel for sessName:%s, peerDevId:%s.", sessName.c_str(),
167 GetAnonyString(peerDevId).c_str());
168 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessName is empty.");
169 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
170
171 int32_t sessionId = GetSessIdBySessName(sessName, peerDevId);
172 CloseSession(sessionId);
173 {
174 std::lock_guard<std::mutex> lock(idMapMutex_);
175 devId2SessIdMap_.erase(sessName + "_" + peerDevId);
176 }
177
178 AVTRANS_LOGI("Close softbus channel success, sessionId:%" PRId32, sessionId);
179 return DH_AVT_SUCCESS;
180 }
181
SendBytesData(const std::string & sessName,const std::string & peerDevId,const std::string & data)182 int32_t SoftbusChannelAdapter::SendBytesData(const std::string& sessName, const std::string &peerDevId,
183 const std::string &data)
184 {
185 AVTRANS_LOGI("Send bytes data for sessName:%s, peerDevId:%s.", sessName.c_str(), GetAnonyString(peerDevId).c_str());
186 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessName is empty.");
187 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
188 TRUE_RETURN_V_MSG_E(data.empty(), ERR_DH_AVT_INVALID_PARAM, "input data string is empty.");
189
190 int32_t ret = SendBytes(GetSessIdBySessName(sessName, peerDevId), data.c_str(), strlen(data.c_str()));
191 if (ret != DH_AVT_SUCCESS) {
192 AVTRANS_LOGE("Send bytes data failed ret:%" PRId32, ret);
193 return ERR_DH_AVT_SEND_DATA_FAILED;
194 }
195 return DH_AVT_SUCCESS;
196 }
197
SendStreamData(const std::string & sessName,const std::string & peerDevId,const StreamData * data,const StreamData * ext)198 int32_t SoftbusChannelAdapter::SendStreamData(const std::string& sessName, const std::string &peerDevId,
199 const StreamData *data, const StreamData *ext)
200 {
201 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessName is empty.");
202 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
203 TRUE_RETURN_V_MSG_E(data == nullptr, ERR_DH_AVT_INVALID_PARAM, "input data is nullptr.");
204 TRUE_RETURN_V_MSG_E(ext == nullptr, ERR_DH_AVT_INVALID_PARAM, "input ext data is nullptr.");
205
206 StreamFrameInfo frameInfo = {0};
207 int32_t ret = SendStream(GetSessIdBySessName(sessName, peerDevId), data, ext, &frameInfo);
208 if (ret != DH_AVT_SUCCESS) {
209 AVTRANS_LOGE("Send stream data failed ret:%" PRId32, ret);
210 return ERR_DH_AVT_SEND_DATA_FAILED;
211 }
212 return DH_AVT_SUCCESS;
213 }
214
RegisterChannelListener(const std::string & sessName,const std::string & peerDevId,ISoftbusChannelListener * listener)215 int32_t SoftbusChannelAdapter::RegisterChannelListener(const std::string& sessName, const std::string &peerDevId,
216 ISoftbusChannelListener *listener)
217 {
218 AVTRANS_LOGI("Register channel listener for sessName:%s, peerDevId:%s.",
219 sessName.c_str(), GetAnonyString(peerDevId).c_str());
220 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessName is empty.");
221 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
222 TRUE_RETURN_V_MSG_E(listener == nullptr, ERR_DH_AVT_INVALID_PARAM, "input callback is nullptr.");
223
224 std::lock_guard<std::mutex> lock(listenerMtx_);
225 listenerMap_.insert(std::make_pair(sessName + "_" + peerDevId, listener));
226
227 return DH_AVT_SUCCESS;
228 }
229
UnRegisterChannelListener(const std::string & sessName,const std::string & peerDevId)230 int32_t SoftbusChannelAdapter::UnRegisterChannelListener(const std::string& sessName, const std::string &peerDevId)
231 {
232 AVTRANS_LOGI("Unregister channel listener for sessName:%s, peerDevId:%s.",
233 sessName.c_str(), GetAnonyString(peerDevId).c_str());
234 TRUE_RETURN_V_MSG_E(sessName.empty(), ERR_DH_AVT_INVALID_PARAM, "input sessName is empty.");
235 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
236
237 std::lock_guard<std::mutex> lock(listenerMtx_);
238 listenerMap_.erase(sessName + "_" + peerDevId);
239
240 return DH_AVT_SUCCESS;
241 }
242
StartDeviceTimeSync(const std::string & pkgName,const std::string & sessName,const std::string & peerDevId)243 int32_t SoftbusChannelAdapter::StartDeviceTimeSync(const std::string &pkgName, const std::string& sessName,
244 const std::string &peerDevId)
245 {
246 AVTRANS_LOGI("Start device time sync for peerDeviceId:%s.", GetAnonyString(peerDevId).c_str());
247 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
248
249 ITimeSyncCb timeSyncCbk = {.onTimeSyncResult = onDevTimeSyncResult};
250 int32_t ret = StartTimeSync(pkgName.c_str(), peerDevId.c_str(), TimeSyncAccuracy::SUPER_HIGH_ACCURACY,
251 TimeSyncPeriod::SHORT_PERIOD, &timeSyncCbk);
252 if (ret != 0) {
253 AVTRANS_LOGE("StartTimeSync failed ret:%" PRId32, ret);
254 return ERR_DH_AVT_TIME_SYNC_FAILED;
255 }
256
257 std::lock_guard<std::mutex> lock(timeSyncMtx_);
258 timeSyncSessNames_.insert(sessName + "_" + peerDevId);
259
260 return DH_AVT_SUCCESS;
261 }
262
StopDeviceTimeSync(const std::string & pkgName,const std::string & sessName,const std::string & peerDevId)263 int32_t SoftbusChannelAdapter::StopDeviceTimeSync(const std::string &pkgName, const std::string& sessName,
264 const std::string &peerDevId)
265 {
266 AVTRANS_LOGI("Stop device time sync for peerDeviceId:%s.", GetAnonyString(peerDevId).c_str());
267 TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty.");
268
269 int32_t ret = StopTimeSync(pkgName.c_str(), peerDevId.c_str());
270 if (ret != 0) {
271 AVTRANS_LOGE("StopTimeSync failed ret:%" PRId32, ret);
272 return ERR_DH_AVT_TIME_SYNC_FAILED;
273 }
274
275 std::lock_guard<std::mutex> lock(timeSyncMtx_);
276 timeSyncSessNames_.erase(sessName + "_" + peerDevId);
277
278 return DH_AVT_SUCCESS;
279 }
280
GetSessIdBySessName(const std::string & sessName,const std::string & peerDevId)281 int32_t SoftbusChannelAdapter::GetSessIdBySessName(const std::string& sessName, const std::string &peerDevId)
282 {
283 std::lock_guard<std::mutex> lock(idMapMutex_);
284 std::string idMapKey = sessName + "_" + peerDevId;
285 if (devId2SessIdMap_.find(idMapKey) == devId2SessIdMap_.end()) {
286 AVTRANS_LOGE("Can not find sessionId for sessName:%s, peerDevId:%s.",
287 sessName.c_str(), GetAnonyString(peerDevId).c_str());
288 return -1;
289 }
290 return devId2SessIdMap_[idMapKey];
291 }
292
GetSessionNameById(int32_t sessionId)293 std::string SoftbusChannelAdapter::GetSessionNameById(int32_t sessionId)
294 {
295 std::lock_guard<std::mutex> lock(idMapMutex_);
296 for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end(); it++) {
297 if (it->second == sessionId) {
298 return it->first;
299 }
300 }
301
302 AVTRANS_LOGE("No available channel or invalid sessionId:%" PRId32, sessionId);
303 return EMPTY_STRING;
304 }
305
OnSoftbusChannelOpened(int32_t sessionId,int32_t result)306 int32_t SoftbusChannelAdapter::OnSoftbusChannelOpened(int32_t sessionId, int32_t result)
307 {
308 AVTRANS_LOGI("On softbus channel opened, sessionId:%" PRId32", result:%" PRId32, sessionId, result);
309
310 char peerDevIdChar[MAX_DEVICE_ID_LEN] = "";
311 int32_t ret = GetPeerDeviceId(sessionId, peerDevIdChar, sizeof(peerDevIdChar));
312 TRUE_RETURN_V_MSG_E(ret != 0, ret, "Get peer device id from softbus failed.");
313 std::string peerDevId(peerDevIdChar);
314
315 char sessNameChar[MAX_SESSION_NAME_LEN] = "";
316 ret = GetMySessionName(sessionId, sessNameChar, sizeof(sessNameChar));
317 TRUE_RETURN_V_MSG_E(ret != 0, ret, "Get my session name from softbus failed.");
318 std::string sessName(sessNameChar);
319
320 EventType type = (result == 0) ? EventType::EVENT_CHANNEL_OPENED : EventType::EVENT_CHANNEL_OPEN_FAIL;
321 AVTransEvent event = {type, sessName, peerDevId};
322
323 {
324 std::lock_guard<std::mutex> lock(listenerMtx_);
325 for (auto it = listenerMap_.begin(); it != listenerMap_.end(); it++) {
326 if (((it->first).find(sessName) != std::string::npos) && (it->second != nullptr)) {
327 std::thread(&SoftbusChannelAdapter::SendChannelEvent, this, it->second, event).detach();
328 {
329 std::lock_guard<std::mutex> lock(idMapMutex_);
330 devId2SessIdMap_.erase(it->first);
331 devId2SessIdMap_.insert(std::make_pair(it->first, sessionId));
332 }
333 }
334 }
335 }
336
337 int32_t existSessId = GetSessIdBySessName(sessName, peerDevId);
338 if (existSessId == -1) {
339 std::lock_guard<std::mutex> lock(idMapMutex_);
340 devId2SessIdMap_.insert(std::make_pair(sessName + "_" + peerDevId, sessionId));
341 }
342
343 return DH_AVT_SUCCESS;
344 }
345
OnSoftbusChannelClosed(int32_t sessionId)346 void SoftbusChannelAdapter::OnSoftbusChannelClosed(int32_t sessionId)
347 {
348 AVTRANS_LOGI("On softbus channel closed, sessionId:%" PRId32, sessionId);
349
350 std::string peerDevId = GetPeerDevIdBySessId(sessionId);
351 AVTransEvent event = {EventType::EVENT_CHANNEL_CLOSED, "", peerDevId};
352
353 std::lock_guard<std::mutex> lock(idMapMutex_);
354 for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end();) {
355 if (it->second == sessionId) {
356 event.content = GetOwnerFromSessName(it->first);
357 std::lock_guard<std::mutex> lock(listenerMtx_);
358 std::thread(&SoftbusChannelAdapter::SendChannelEvent, this, listenerMap_[it->first], event).detach();
359 devId2SessIdMap_.erase(it++);
360 } else {
361 it++;
362 }
363 }
364 }
365
OnSoftbusBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)366 void SoftbusChannelAdapter::OnSoftbusBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
367 {
368 AVTRANS_LOGI("On softbus channel bytes received, sessionId:%" PRId32, sessionId);
369 TRUE_RETURN(data == nullptr, "input data is nullptr.");
370 TRUE_RETURN(dataLen == 0, "input dataLen is 0.");
371
372 char peerDevIdChar[MAX_DEVICE_ID_LEN] = "";
373 int32_t ret = GetPeerDeviceId(sessionId, peerDevIdChar, sizeof(peerDevIdChar));
374 TRUE_RETURN(ret != 0, "Get peer device id from softbus failed.");
375 std::string peerDevId(peerDevIdChar);
376
377 std::string dataStr = std::string(reinterpret_cast<const char *>(data), dataLen);
378 AVTransEvent event = {EventType::EVENT_DATA_RECEIVED, dataStr, peerDevId};
379
380 std::lock_guard<std::mutex> lock(idMapMutex_);
381 for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end(); it++) {
382 if (it->second == sessionId) {
383 std::lock_guard<std::mutex> lock(listenerMtx_);
384 std::thread(&SoftbusChannelAdapter::SendChannelEvent, this, listenerMap_[it->first], event).detach();
385 }
386 }
387 }
388
OnSoftbusStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frameInfo)389 void SoftbusChannelAdapter::OnSoftbusStreamReceived(int32_t sessionId, const StreamData *data,
390 const StreamData *ext, const StreamFrameInfo *frameInfo)
391 {
392 (void)frameInfo;
393 TRUE_RETURN(data == nullptr, "input data is nullptr.");
394 TRUE_RETURN(ext == nullptr, "input ext data is nullptr.");
395
396 std::lock_guard<std::mutex> lock(idMapMutex_);
397 for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end(); it++) {
398 if (it->second == sessionId) {
399 std::lock_guard<std::mutex> lock(listenerMtx_);
400 ISoftbusChannelListener *listener = listenerMap_[it->first];
401 TRUE_RETURN(listener == nullptr, "Can not find channel listener.");
402 listener->OnStreamReceived(data, ext);
403 }
404 }
405 }
406
OnSoftbusTimeSyncResult(const TimeSyncResultInfo * info,int32_t result)407 void SoftbusChannelAdapter::OnSoftbusTimeSyncResult(const TimeSyncResultInfo *info, int32_t result)
408 {
409 AVTRANS_LOGI("On softbus channel time sync result:%" PRId32, result);
410 TRUE_RETURN(result == 0, "On softbus channel time sync failed");
411
412 int32_t millisecond = info->result.millisecond;
413 int32_t microsecond = info->result.microsecond;
414 TimeSyncAccuracy accuracy = info->result.accuracy;
415 AVTRANS_LOGI("Time sync success, flag:%" PRId32", millisecond:%" PRId32 ", microsecond:%" PRId32
416 ", accuracy:%" PRId32, info->flag, millisecond, microsecond, accuracy);
417
418 std::string targetDevId(info->target.targetNetworkId);
419 std::string masterDevId(info->target.masterNetworkId);
420 std::lock_guard<std::mutex> lock(timeSyncMtx_);
421 for (auto sessName : timeSyncSessNames_) {
422 std::lock_guard<std::mutex> lock(listenerMtx_);
423 ISoftbusChannelListener *listener = listenerMap_[sessName];
424 if (listener != nullptr) {
425 listener->OnChannelEvent({EventType::EVENT_TIME_SYNC_RESULT, std::to_string(millisecond), targetDevId});
426 }
427 }
428 }
429
GetPeerDevIdBySessId(int32_t sessionId)430 std::string SoftbusChannelAdapter::GetPeerDevIdBySessId(int32_t sessionId)
431 {
432 std::lock_guard<std::mutex> lock(idMapMutex_);
433 for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end(); it++) {
434 if (it->second != sessionId) {
435 continue;
436 }
437 std::string::size_type position = (it->first).find_last_of("_");
438 if (position == std::string::npos) {
439 continue;
440 }
441 std::string peerDevId = (it->first).substr(position + 1);
442 if (peerDevId != AV_TRANS_SPECIAL_DEVICE_ID) {
443 return peerDevId;
444 }
445 }
446 return EMPTY_STRING;
447 }
448
GetOwnerFromSessName(const std::string & sessName)449 std::string SoftbusChannelAdapter::GetOwnerFromSessName(const std::string &sessName)
450 {
451 std::string::size_type position = sessName.find_first_of("_");
452 if (position != std::string::npos) {
453 return sessName.substr(0, position);
454 }
455 return EMPTY_STRING;
456 }
457
SendChannelEvent(ISoftbusChannelListener * listener,const AVTransEvent & event)458 void SoftbusChannelAdapter::SendChannelEvent(ISoftbusChannelListener *listener, const AVTransEvent &event)
459 {
460 pthread_setname_np(pthread_self(), SEND_CHANNEL_EVENT);
461
462 TRUE_RETURN(listener == nullptr, "input listener is nullptr.");
463 listener->OnChannelEvent(event);
464 }
465 } // namespace DistributedHardware
466 } // namespace OHOS