166f3657fSopenharmony_ci/* 266f3657fSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 366f3657fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 466f3657fSopenharmony_ci * you may not use this file except in compliance with the License. 566f3657fSopenharmony_ci * You may obtain a copy of the License at 666f3657fSopenharmony_ci * 766f3657fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 866f3657fSopenharmony_ci * 966f3657fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1066f3657fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1166f3657fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1266f3657fSopenharmony_ci * See the License for the specific language governing permissions and 1366f3657fSopenharmony_ci * limitations under the License. 1466f3657fSopenharmony_ci */ 1566f3657fSopenharmony_ci 1666f3657fSopenharmony_ci#include "softbus_adapter.h" 1766f3657fSopenharmony_ci 1866f3657fSopenharmony_ci#include <securec.h> 1966f3657fSopenharmony_ci#include <unistd.h> 2066f3657fSopenharmony_ci 2166f3657fSopenharmony_ci#include "softbus_bus_center.h" 2266f3657fSopenharmony_ci#include "softbus_common.h" 2366f3657fSopenharmony_ci 2466f3657fSopenharmony_ci#include "dscreen_errcode.h" 2566f3657fSopenharmony_ci#include "dscreen_hisysevent.h" 2666f3657fSopenharmony_ci#include "dscreen_util.h" 2766f3657fSopenharmony_ci 2866f3657fSopenharmony_cinamespace OHOS { 2966f3657fSopenharmony_cinamespace DistributedHardware { 3066f3657fSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(SoftbusAdapter); 3166f3657fSopenharmony_cistatic void ScreenOnSoftbusSessionOpened(int32_t sessionId, PeerSocketInfo info) 3266f3657fSopenharmony_ci{ 3366f3657fSopenharmony_ci SoftbusAdapter::GetInstance().OnSoftbusSessionOpened(sessionId, info); 3466f3657fSopenharmony_ci} 3566f3657fSopenharmony_ci 3666f3657fSopenharmony_cistatic void ScreenOnSoftbusSessionClosed(int32_t sessionId, ShutdownReason reason) 3766f3657fSopenharmony_ci{ 3866f3657fSopenharmony_ci SoftbusAdapter::GetInstance().OnSoftbusSessionClosed(sessionId, reason); 3966f3657fSopenharmony_ci} 4066f3657fSopenharmony_ci 4166f3657fSopenharmony_cistatic void ScreenOnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) 4266f3657fSopenharmony_ci{ 4366f3657fSopenharmony_ci SoftbusAdapter::GetInstance().OnBytesReceived(sessionId, data, dataLen); 4466f3657fSopenharmony_ci} 4566f3657fSopenharmony_ci 4666f3657fSopenharmony_cistatic void ScreenOnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, 4766f3657fSopenharmony_ci const StreamFrameInfo *frameInfo) 4866f3657fSopenharmony_ci{ 4966f3657fSopenharmony_ci SoftbusAdapter::GetInstance().OnStreamReceived(sessionId, data, ext, frameInfo); 5066f3657fSopenharmony_ci} 5166f3657fSopenharmony_ci 5266f3657fSopenharmony_cistatic void ScreenOnMessageReceived(int sessionId, const void *data, unsigned int dataLen) 5366f3657fSopenharmony_ci{ 5466f3657fSopenharmony_ci SoftbusAdapter::GetInstance().OnMessageReceived(sessionId, data, dataLen); 5566f3657fSopenharmony_ci} 5666f3657fSopenharmony_ci 5766f3657fSopenharmony_ciSoftbusAdapter::SoftbusAdapter() 5866f3657fSopenharmony_ci{ 5966f3657fSopenharmony_ci DHLOGI("SoftbusAdapter"); 6066f3657fSopenharmony_ci sessListener_.OnBind = ScreenOnSoftbusSessionOpened; 6166f3657fSopenharmony_ci sessListener_.OnShutdown = ScreenOnSoftbusSessionClosed; 6266f3657fSopenharmony_ci sessListener_.OnBytes = ScreenOnBytesReceived; 6366f3657fSopenharmony_ci sessListener_.OnStream = ScreenOnStreamReceived; 6466f3657fSopenharmony_ci sessListener_.OnMessage = ScreenOnMessageReceived; 6566f3657fSopenharmony_ci sessListener_.OnFile = nullptr; 6666f3657fSopenharmony_ci sessListener_.OnQos = nullptr; 6766f3657fSopenharmony_ci sessListener_.OnError = nullptr; 6866f3657fSopenharmony_ci sessListener_.OnNegotiate = nullptr; 6966f3657fSopenharmony_ci} 7066f3657fSopenharmony_ci 7166f3657fSopenharmony_ciSoftbusAdapter::~SoftbusAdapter() 7266f3657fSopenharmony_ci{ 7366f3657fSopenharmony_ci DHLOGI("~SoftbusAdapter"); 7466f3657fSopenharmony_ci} 7566f3657fSopenharmony_ci 7666f3657fSopenharmony_ciint32_t SoftbusAdapter::RegisterSoftbusListener(const std::shared_ptr<ISoftbusListener> &listener, 7766f3657fSopenharmony_ci const std::string &sessionName, const std::string &peerDevId) 7866f3657fSopenharmony_ci{ 7966f3657fSopenharmony_ci if (listener == nullptr) { 8066f3657fSopenharmony_ci DHLOGE("%{public}s: listener is nullptr.", DSCREEN_LOG_TAG); 8166f3657fSopenharmony_ci return ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL; 8266f3657fSopenharmony_ci } 8366f3657fSopenharmony_ci DHLOGI("%{public}s: RegisterListener sess:%{public}s id:%{public}s.", DSCREEN_LOG_TAG, sessionName.c_str(), 8466f3657fSopenharmony_ci GetAnonyString(peerDevId).c_str()); 8566f3657fSopenharmony_ci std::string strListenerKey = sessionName + "_" + peerDevId; 8666f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 8766f3657fSopenharmony_ci if (mapListeners_.find(strListenerKey) != mapListeners_.end()) { 8866f3657fSopenharmony_ci DHLOGE("%{public}s: Session listener already register.", DSCREEN_LOG_TAG); 8966f3657fSopenharmony_ci return ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL; 9066f3657fSopenharmony_ci } 9166f3657fSopenharmony_ci mapListeners_.insert(std::make_pair(strListenerKey, listener)); 9266f3657fSopenharmony_ci 9366f3657fSopenharmony_ci return DH_SUCCESS; 9466f3657fSopenharmony_ci} 9566f3657fSopenharmony_ci 9666f3657fSopenharmony_ciint32_t SoftbusAdapter::UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId) 9766f3657fSopenharmony_ci{ 9866f3657fSopenharmony_ci DHLOGI("%{public}s: UnRegisterListener sess:%{public}s id:%{public}s.", DSCREEN_LOG_TAG, sessionName.c_str(), 9966f3657fSopenharmony_ci GetAnonyString(peerDevId).c_str()); 10066f3657fSopenharmony_ci std::string strListenerKey = sessionName + "_" + peerDevId; 10166f3657fSopenharmony_ci 10266f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 10366f3657fSopenharmony_ci mapListeners_.erase(strListenerKey); 10466f3657fSopenharmony_ci 10566f3657fSopenharmony_ci return DH_SUCCESS; 10666f3657fSopenharmony_ci} 10766f3657fSopenharmony_ci 10866f3657fSopenharmony_ciint32_t SoftbusAdapter::CreateSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, 10966f3657fSopenharmony_ci const std::string &peerDevId) 11066f3657fSopenharmony_ci{ 11166f3657fSopenharmony_ci DHLOGI("%{public}s: CreateSessionServer sess:%{public}s id:%{public}s.", DSCREEN_LOG_TAG, sessionName.c_str(), 11266f3657fSopenharmony_ci GetAnonyString(peerDevId).c_str()); 11366f3657fSopenharmony_ci { 11466f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(serverIdMapMutex_); 11566f3657fSopenharmony_ci std::string idMapValue = sessionName + "_" + peerDevId; 11666f3657fSopenharmony_ci for (auto it = serverIdMap_.begin(); it != serverIdMap_.end(); it++) { 11766f3657fSopenharmony_ci if (((it->second).find(idMapValue) != std::string::npos)) { 11866f3657fSopenharmony_ci DHLOGI("%{public}s: Session already create.", sessionName.c_str()); 11966f3657fSopenharmony_ci return DH_SUCCESS; 12066f3657fSopenharmony_ci } 12166f3657fSopenharmony_ci } 12266f3657fSopenharmony_ci } 12366f3657fSopenharmony_ci 12466f3657fSopenharmony_ci SocketInfo serverInfo = { 12566f3657fSopenharmony_ci .name = const_cast<char*>(sessionName.c_str()), 12666f3657fSopenharmony_ci .pkgName = const_cast<char*>(pkgname.c_str()), 12766f3657fSopenharmony_ci .dataType = DATA_TYPE_VIDEO_STREAM, 12866f3657fSopenharmony_ci }; 12966f3657fSopenharmony_ci int32_t socketId = Socket(serverInfo); 13066f3657fSopenharmony_ci if (socketId < 0) { 13166f3657fSopenharmony_ci DHLOGE("Create Socket fail socketId:%{public}" PRId32, socketId); 13266f3657fSopenharmony_ci return ERR_DH_SCREEN_ADAPTER_BAD_VALUE; 13366f3657fSopenharmony_ci } 13466f3657fSopenharmony_ci QosTV qos[] = { 13566f3657fSopenharmony_ci {.qos = QOS_TYPE_MIN_BW, .value = 40 * 1024 * 1024}, 13666f3657fSopenharmony_ci {.qos = QOS_TYPE_MAX_LATENCY, .value = 8000}, 13766f3657fSopenharmony_ci {.qos = QOS_TYPE_MIN_LATENCY, .value = 2000}, 13866f3657fSopenharmony_ci }; 13966f3657fSopenharmony_ci 14066f3657fSopenharmony_ci int32_t ret = Listen(socketId, qos, sizeof(qos) / sizeof(qos[0]), &sessListener_); 14166f3657fSopenharmony_ci if (ret != DH_SUCCESS) { 14266f3657fSopenharmony_ci DHLOGE("Listen socket error for sessionName:%{public}s", sessionName.c_str()); 14366f3657fSopenharmony_ci return ERR_DH_SCREEN_ADAPTER_BAD_VALUE; 14466f3657fSopenharmony_ci } 14566f3657fSopenharmony_ci { 14666f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(serverIdMapMutex_); 14766f3657fSopenharmony_ci serverIdMap_.insert(std::make_pair(socketId, sessionName + "_" + peerDevId)); 14866f3657fSopenharmony_ci } 14966f3657fSopenharmony_ci DHLOGI("%{public}s: CreateSessionServer success sessionId. %{public}" PRId32, DSCREEN_LOG_TAG, socketId); 15066f3657fSopenharmony_ci return DH_SUCCESS; 15166f3657fSopenharmony_ci} 15266f3657fSopenharmony_ci 15366f3657fSopenharmony_ciint32_t SoftbusAdapter::RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, 15466f3657fSopenharmony_ci const std::string &peerDevId) 15566f3657fSopenharmony_ci{ 15666f3657fSopenharmony_ci (void)pkgname; 15766f3657fSopenharmony_ci if (sessionName.empty() || peerDevId.empty()) { 15866f3657fSopenharmony_ci return ERR_DH_SCREEN_TRANS_NULL_VALUE; 15966f3657fSopenharmony_ci } 16066f3657fSopenharmony_ci DHLOGI("%{public}s: RemoveSessionServer sess:%{public}s id:%{public}s", DSCREEN_LOG_TAG, sessionName.c_str(), 16166f3657fSopenharmony_ci GetAnonyString(peerDevId).c_str()); 16266f3657fSopenharmony_ci int32_t serverSocketId = INVALID_SESSION_ID; 16366f3657fSopenharmony_ci { 16466f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(serverIdMapMutex_); 16566f3657fSopenharmony_ci std::string idMapValue = sessionName + "_" + peerDevId; 16666f3657fSopenharmony_ci for (auto it = serverIdMap_.begin(); it != serverIdMap_.end();) { 16766f3657fSopenharmony_ci if (((it->second).find(idMapValue) != std::string::npos)) { 16866f3657fSopenharmony_ci serverSocketId = it->first; 16966f3657fSopenharmony_ci it = serverIdMap_.erase(it); 17066f3657fSopenharmony_ci } else { 17166f3657fSopenharmony_ci ++it; 17266f3657fSopenharmony_ci } 17366f3657fSopenharmony_ci } 17466f3657fSopenharmony_ci } 17566f3657fSopenharmony_ci Shutdown(serverSocketId); 17666f3657fSopenharmony_ci DHLOGI("%{public}s: RemoveSessionServer success.", DSCREEN_LOG_TAG); 17766f3657fSopenharmony_ci return DH_SUCCESS; 17866f3657fSopenharmony_ci} 17966f3657fSopenharmony_ci 18066f3657fSopenharmony_ciint32_t SoftbusAdapter::OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName, 18166f3657fSopenharmony_ci const std::string &peerDevId) 18266f3657fSopenharmony_ci{ 18366f3657fSopenharmony_ci DHLOGI("%{public}s: OpenSoftbusSession mysess:%{public}s peersess:%{public}s id:%{public}s.", DSCREEN_LOG_TAG, 18466f3657fSopenharmony_ci mySessionName.c_str(), peerSessionName.c_str(), GetAnonyString(peerDevId).c_str()); 18566f3657fSopenharmony_ci 18666f3657fSopenharmony_ci QosTV qos[] = { 18766f3657fSopenharmony_ci {.qos = QOS_TYPE_MIN_BW, .value = 40 * 1024 * 1024}, 18866f3657fSopenharmony_ci {.qos = QOS_TYPE_MAX_LATENCY, .value = 8000}, 18966f3657fSopenharmony_ci {.qos = QOS_TYPE_MIN_LATENCY, .value = 2000}, 19066f3657fSopenharmony_ci }; 19166f3657fSopenharmony_ci std::string localSesionName = mySessionName + "_" + std::to_string(GetCurrentTimeUs()); 19266f3657fSopenharmony_ci SocketInfo clientInfo = { 19366f3657fSopenharmony_ci .name = const_cast<char*>((localSesionName.c_str())), 19466f3657fSopenharmony_ci .peerName = const_cast<char*>(peerSessionName.c_str()), 19566f3657fSopenharmony_ci .peerNetworkId = const_cast<char*>(peerDevId.c_str()), 19666f3657fSopenharmony_ci .pkgName = const_cast<char*>(PKG_NAME.c_str()), 19766f3657fSopenharmony_ci .dataType = DATA_TYPE_VIDEO_STREAM, 19866f3657fSopenharmony_ci }; 19966f3657fSopenharmony_ci int32_t socketId = Socket(clientInfo); 20066f3657fSopenharmony_ci if (socketId < 0) { 20166f3657fSopenharmony_ci DHLOGE("Create OpenSoftbusChannel Socket error"); 20266f3657fSopenharmony_ci return ERR_DH_SCREEN_ADAPTER_PARA_ERROR; 20366f3657fSopenharmony_ci } 20466f3657fSopenharmony_ci int32_t ret = Bind(socketId, qos, sizeof(qos) / sizeof(qos[0]), &sessListener_); 20566f3657fSopenharmony_ci if (ret != DH_SUCCESS) { 20666f3657fSopenharmony_ci DHLOGE("Bind SocketClient error"); 20766f3657fSopenharmony_ci return ERR_DH_SCREEN_ADAPTER_PARA_ERROR; 20866f3657fSopenharmony_ci } 20966f3657fSopenharmony_ci { 21066f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(idMapMutex_); 21166f3657fSopenharmony_ci devId2SessIdMap_.insert(std::make_pair(socketId, mySessionName + "_" + peerDevId)); 21266f3657fSopenharmony_ci } 21366f3657fSopenharmony_ci std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(socketId); 21466f3657fSopenharmony_ci if (listener == nullptr) { 21566f3657fSopenharmony_ci DHLOGE("Get softbus listener failed."); 21666f3657fSopenharmony_ci return ERR_DH_SCREEN_TRANS_ERROR; 21766f3657fSopenharmony_ci } 21866f3657fSopenharmony_ci PeerSocketInfo info; 21966f3657fSopenharmony_ci ret = OnSoftbusSessionOpened(socketId, info); 22066f3657fSopenharmony_ci if (ret != DH_SUCCESS) { 22166f3657fSopenharmony_ci return ret; 22266f3657fSopenharmony_ci } 22366f3657fSopenharmony_ci DHLOGI("%{public}s: OpenSoftbusSession success sessionId: %{public}" PRId32, DSCREEN_LOG_TAG, socketId); 22466f3657fSopenharmony_ci return socketId; 22566f3657fSopenharmony_ci} 22666f3657fSopenharmony_ci 22766f3657fSopenharmony_ciint32_t SoftbusAdapter::CloseSoftbusSession(const int32_t sessionId) 22866f3657fSopenharmony_ci{ 22966f3657fSopenharmony_ci DHLOGI("%{public}s: CloseSoftbusSession, sessid:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 23066f3657fSopenharmony_ci Shutdown(sessionId); 23166f3657fSopenharmony_ci { 23266f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(idMapMutex_); 23366f3657fSopenharmony_ci devId2SessIdMap_.erase(sessionId); 23466f3657fSopenharmony_ci } 23566f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 23666f3657fSopenharmony_ci mapSessListeners_.erase(sessionId); 23766f3657fSopenharmony_ci 23866f3657fSopenharmony_ci DHLOGI("%{public}s: CloseSoftbusSession success.", DSCREEN_LOG_TAG); 23966f3657fSopenharmony_ci return DH_SUCCESS; 24066f3657fSopenharmony_ci} 24166f3657fSopenharmony_ci 24266f3657fSopenharmony_ciint32_t SoftbusAdapter::SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen) const 24366f3657fSopenharmony_ci{ 24466f3657fSopenharmony_ci DHLOGD("%{public}s: SendSoftbusBytes, sessid:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 24566f3657fSopenharmony_ci int32_t ret = SendBytes(sessionId, data, dataLen); 24666f3657fSopenharmony_ci if (ret != DH_SUCCESS) { 24766f3657fSopenharmony_ci DHLOGE("%{public}s: SendBytes failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret); 24866f3657fSopenharmony_ci return ERR_DH_SCREEN_TRANS_ERROR; 24966f3657fSopenharmony_ci } 25066f3657fSopenharmony_ci 25166f3657fSopenharmony_ci return DH_SUCCESS; 25266f3657fSopenharmony_ci} 25366f3657fSopenharmony_ci 25466f3657fSopenharmony_ciint32_t SoftbusAdapter::SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext, 25566f3657fSopenharmony_ci const StreamFrameInfo *param) const 25666f3657fSopenharmony_ci{ 25766f3657fSopenharmony_ci DHLOGD("%{public}s: SendSoftbusStream, sessid:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 25866f3657fSopenharmony_ci int32_t ret = SendStream(sessionId, data, ext, param); 25966f3657fSopenharmony_ci if (ret != DH_SUCCESS) { 26066f3657fSopenharmony_ci DHLOGE("%{public}s: SendStream failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret); 26166f3657fSopenharmony_ci return ERR_DH_SCREEN_TRANS_ERROR; 26266f3657fSopenharmony_ci } 26366f3657fSopenharmony_ci 26466f3657fSopenharmony_ci return DH_SUCCESS; 26566f3657fSopenharmony_ci} 26666f3657fSopenharmony_ci 26766f3657fSopenharmony_cistd::shared_ptr<ISoftbusListener> &SoftbusAdapter::GetSoftbusListenerByName(int32_t sessionId) 26866f3657fSopenharmony_ci{ 26966f3657fSopenharmony_ci DHLOGD("%{public}s: GetSoftbusListenerByName, sessionId:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 27066f3657fSopenharmony_ci std::string strListenerKey = ""; 27166f3657fSopenharmony_ci { 27266f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(idMapMutex_); 27366f3657fSopenharmony_ci for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end(); it++) { 27466f3657fSopenharmony_ci if (it->first == sessionId) { 27566f3657fSopenharmony_ci strListenerKey = it->second; 27666f3657fSopenharmony_ci break; 27766f3657fSopenharmony_ci } 27866f3657fSopenharmony_ci } 27966f3657fSopenharmony_ci } 28066f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 28166f3657fSopenharmony_ci if (mapListeners_.find(strListenerKey) == mapListeners_.end()) { 28266f3657fSopenharmony_ci DHLOGE("%{public}s: Find listener failed.", DSCREEN_LOG_TAG); 28366f3657fSopenharmony_ci return nullListener_; 28466f3657fSopenharmony_ci } 28566f3657fSopenharmony_ci return mapListeners_[strListenerKey]; 28666f3657fSopenharmony_ci} 28766f3657fSopenharmony_ci 28866f3657fSopenharmony_cistd::shared_ptr<ISoftbusListener> &SoftbusAdapter::GetSoftbusListenerById(int32_t sessionId) 28966f3657fSopenharmony_ci{ 29066f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 29166f3657fSopenharmony_ci if (mapSessListeners_.find(sessionId) == mapSessListeners_.end()) { 29266f3657fSopenharmony_ci DHLOGE("%{public}s: Find listener failed.", DSCREEN_LOG_TAG); 29366f3657fSopenharmony_ci return nullListener_; 29466f3657fSopenharmony_ci } 29566f3657fSopenharmony_ci 29666f3657fSopenharmony_ci return mapSessListeners_[sessionId]; 29766f3657fSopenharmony_ci} 29866f3657fSopenharmony_ci 29966f3657fSopenharmony_ciint32_t SoftbusAdapter::OnSoftbusSessionOpened(int32_t sessionId, PeerSocketInfo info) 30066f3657fSopenharmony_ci{ 30166f3657fSopenharmony_ci DHLOGI("%{public}s: OnSoftbusSessionOpened session:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 30266f3657fSopenharmony_ci { 30366f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(serverIdMapMutex_); 30466f3657fSopenharmony_ci for (auto it = serverIdMap_.begin(); it != serverIdMap_.end(); it++) { 30566f3657fSopenharmony_ci if (info.networkId == nullptr) { 30666f3657fSopenharmony_ci break; 30766f3657fSopenharmony_ci } 30866f3657fSopenharmony_ci std::string peerDevId(info.networkId); 30966f3657fSopenharmony_ci if ((it->second).find(peerDevId) != std::string::npos) { 31066f3657fSopenharmony_ci std::lock_guard<std::mutex> sessionLock(idMapMutex_); 31166f3657fSopenharmony_ci devId2SessIdMap_.insert(std::make_pair(sessionId, it->second)); 31266f3657fSopenharmony_ci break; 31366f3657fSopenharmony_ci } 31466f3657fSopenharmony_ci } 31566f3657fSopenharmony_ci } 31666f3657fSopenharmony_ci 31766f3657fSopenharmony_ci std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId); 31866f3657fSopenharmony_ci if (listener == nullptr) { 31966f3657fSopenharmony_ci DHLOGE("Get softbus listener failed."); 32066f3657fSopenharmony_ci return ERR_DH_SCREEN_TRANS_ERROR; 32166f3657fSopenharmony_ci } 32266f3657fSopenharmony_ci listener->OnSessionOpened(sessionId, info); 32366f3657fSopenharmony_ci 32466f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 32566f3657fSopenharmony_ci mapSessListeners_.insert(std::make_pair(sessionId, listener)); 32666f3657fSopenharmony_ci 32766f3657fSopenharmony_ci return DH_SUCCESS; 32866f3657fSopenharmony_ci} 32966f3657fSopenharmony_ci 33066f3657fSopenharmony_civoid SoftbusAdapter::OnSoftbusSessionClosed(int32_t sessionId, ShutdownReason reason) 33166f3657fSopenharmony_ci{ 33266f3657fSopenharmony_ci DHLOGI("%{public}s: OnSessionClosed sessionId:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 33366f3657fSopenharmony_ci std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerById(sessionId); 33466f3657fSopenharmony_ci if (listener == nullptr) { 33566f3657fSopenharmony_ci DHLOGE("Get softbus listener failed."); 33666f3657fSopenharmony_ci return; 33766f3657fSopenharmony_ci } 33866f3657fSopenharmony_ci listener->OnSessionClosed(sessionId, reason); 33966f3657fSopenharmony_ci { 34066f3657fSopenharmony_ci std::lock_guard<std::mutex> lock(idMapMutex_); 34166f3657fSopenharmony_ci devId2SessIdMap_.erase(sessionId); 34266f3657fSopenharmony_ci } 34366f3657fSopenharmony_ci std::lock_guard<std::mutex> lisLock(listenerMtx_); 34466f3657fSopenharmony_ci mapSessListeners_.erase(sessionId); 34566f3657fSopenharmony_ci} 34666f3657fSopenharmony_ci 34766f3657fSopenharmony_civoid SoftbusAdapter::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) 34866f3657fSopenharmony_ci{ 34966f3657fSopenharmony_ci DHLOGD("%{public}s: OnBytesReceived, sessionId:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 35066f3657fSopenharmony_ci if (data == nullptr) { 35166f3657fSopenharmony_ci DHLOGE("BytesData is null."); 35266f3657fSopenharmony_ci return; 35366f3657fSopenharmony_ci } 35466f3657fSopenharmony_ci if (dataLen == 0 || dataLen > DSCREEN_MAX_RECV_DATA_LEN) { 35566f3657fSopenharmony_ci DHLOGE("BytesData length is too large, dataLen:%{public}" PRIu32, dataLen); 35666f3657fSopenharmony_ci return; 35766f3657fSopenharmony_ci } 35866f3657fSopenharmony_ci 35966f3657fSopenharmony_ci std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId); 36066f3657fSopenharmony_ci if (listener == nullptr) { 36166f3657fSopenharmony_ci DHLOGE("Get softbus listener failed."); 36266f3657fSopenharmony_ci return; 36366f3657fSopenharmony_ci } 36466f3657fSopenharmony_ci listener->OnBytesReceived(sessionId, data, dataLen); 36566f3657fSopenharmony_ci} 36666f3657fSopenharmony_ci 36766f3657fSopenharmony_civoid SoftbusAdapter::OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, 36866f3657fSopenharmony_ci const StreamFrameInfo *frameInfo) 36966f3657fSopenharmony_ci{ 37066f3657fSopenharmony_ci DHLOGD("%{public}s OnStreamReceived, sessionId:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 37166f3657fSopenharmony_ci if (data == nullptr) { 37266f3657fSopenharmony_ci DHLOGE("StreamData is null."); 37366f3657fSopenharmony_ci return; 37466f3657fSopenharmony_ci } 37566f3657fSopenharmony_ci if (data->bufLen <= 0 || data->bufLen > static_cast<int32_t>(DSCREEN_MAX_RECV_DATA_LEN)) { 37666f3657fSopenharmony_ci DHLOGE("StreamData length is too large, dataLen:%{public}" PRId32, data->bufLen); 37766f3657fSopenharmony_ci return; 37866f3657fSopenharmony_ci } 37966f3657fSopenharmony_ci 38066f3657fSopenharmony_ci std::shared_ptr<ISoftbusListener> &listener = GetSoftbusListenerByName(sessionId); 38166f3657fSopenharmony_ci if (listener == nullptr) { 38266f3657fSopenharmony_ci DHLOGE("Get softbus listener failed."); 38366f3657fSopenharmony_ci return; 38466f3657fSopenharmony_ci } 38566f3657fSopenharmony_ci listener->OnStreamReceived(sessionId, data, ext, frameInfo); 38666f3657fSopenharmony_ci} 38766f3657fSopenharmony_ci 38866f3657fSopenharmony_civoid SoftbusAdapter::OnMessageReceived(int sessionId, const void *data, unsigned int dataLen) const 38966f3657fSopenharmony_ci{ 39066f3657fSopenharmony_ci (void)data; 39166f3657fSopenharmony_ci (void)dataLen; 39266f3657fSopenharmony_ci DHLOGD("%{public}s OnMessageReceived, sessionId:%{public}" PRId32, DSCREEN_LOG_TAG, sessionId); 39366f3657fSopenharmony_ci} 39466f3657fSopenharmony_ci} // namespace DistributedHardware 39566f3657fSopenharmony_ci} // namespace OHOS