From 777bc24525cbbeee694f8314f7dad932f9bcc5f5 Mon Sep 17 00:00:00 2001 From: LongestDistance Date: Tue, 12 Nov 2024 17:33:41 +0800 Subject: [PATCH 1/4] =?UTF-8?q?TicketNo:#IB3SV8=20Description:=E6=96=B0?= =?UTF-8?q?=E9=9C=80=E6=B1=82=EF=BC=9A=E6=96=B0=E9=9C=80=E6=B1=82=EF=BC=9A?= =?UTF-8?q?=E5=AE=8C=E5=96=84service=E7=9A=84=E5=8A=9F=E8=83=BD=20Signed-o?= =?UTF-8?q?ff-by:=20LongestDistance=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/cast_session_manager_service.h | 2 + service/src/cast_session_manager_service.cpp | 42 +++ .../include/cast_device_data_manager.h | 5 +- .../include/connection_manager.h | 7 +- .../include/connection_manager_listener.h | 1 + .../include/discovery_manager.h | 27 +- .../src/cast_device_data_manager.cpp | 59 ++++ .../device_manager/src/connection_manager.cpp | 35 ++- .../device_manager/src/discovery_manager.cpp | 251 +++++++++++++++--- service/src/session/src/utils/include/utils.h | 3 +- service/src/session/src/utils/src/utils.cpp | 4 +- 11 files changed, 382 insertions(+), 54 deletions(-) diff --git a/service/include/cast_session_manager_service.h b/service/include/cast_session_manager_service.h index 394a4db..c38e6a2 100644 --- a/service/include/cast_session_manager_service.h +++ b/service/include/cast_session_manager_service.h @@ -65,6 +65,7 @@ public: void ReportDeviceFound(const std::vector> &deviceList); void ReportSessionCreate(const sptr &castSession); void ReportDeviceOffline(const std::string &deviceId); + bool LoadSinkSA(const std::string &networkId); sptr GetCastSessionInner(std::string sessionId); class CastServiceLoadCallback : public SystemAbilityLoadCallbackStub { @@ -119,6 +120,7 @@ public: void GrabDevice(int32_t sessionId) override; int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override; int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override; + bool LoadSinkSA(const std::string &networkId) override; private: wptr service_; diff --git a/service/src/cast_session_manager_service.cpp b/service/src/cast_session_manager_service.cpp index 80ccb1f..e55f183 100644 --- a/service/src/cast_session_manager_service.cpp +++ b/service/src/cast_session_manager_service.cpp @@ -361,6 +361,16 @@ int32_t ConnectionManagerListener::SetSessionProtocolType(int sessionId, Protoco return CAST_ENGINE_SUCCESS; } +bool ConnectionManagerListener::LoadSinkSA(const std::string &networkId) +{ + auto service = service_.promote(); + if (!service) { + CLOGE("service is null"); + return false; + } + return service->LoadSinkSA(networkId); +} + int32_t CastSessionManagerService::RegisterListener(sptr listener) { CLOGI("RegisterListener in"); @@ -758,6 +768,38 @@ void CastSessionManagerService::ReportDeviceOffline(const std::string &deviceId) } } +bool CastSessionManagerService::LoadSinkSA(const std::string &networkId) +{ + CLOGI("LoadSinkSA in"); + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + CLOGE("Failed to get SA manager"); + return false; + } + sptr loadCallback = new (std::nothrow) CastServiceLoadCallback(); + if (loadCallback == nullptr) { + CLOGE("Failed to new object"); + return false; + } + + constexpr int32_t sleepTimeMs = 30; + constexpr int32_t retryTimeMax = 150; // The service startup timeout interval is 4s. + int32_t result = samgr->LoadSystemAbility(CAST_ENGINE_SA_ID, networkId, loadCallback); + for (int32_t retryTime = 1; (result != ERR_OK) && (retryTime < retryTimeMax); ++retryTime) { + std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs)); + result = samgr->LoadSystemAbility(CAST_ENGINE_SA_ID, networkId, loadCallback); + CLOGD("Attemp to reLoad SA for the %d time.", retryTime); + } + + if (result != ERR_OK) { + CLOGE("systemAbilityId: %d load failed, result code: %d", CAST_ENGINE_SA_ID, result); + return false; + } else { + CLOGI("Load sink SA success!"); + return true; + } +} + bool CastSessionManagerService::CheckAndWaitDevieManagerServiceInit() { auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); diff --git a/service/src/device_manager/include/cast_device_data_manager.h b/service/src/device_manager/include/cast_device_data_manager.h index d024727..a14601b 100644 --- a/service/src/device_manager/include/cast_device_data_manager.h +++ b/service/src/device_manager/include/cast_device_data_manager.h @@ -84,9 +84,12 @@ public: bool IsDeviceUsed(const std::string &deviceId); int GetSessionIdByDeviceId(const std::string &deviceId); + int GetCastSessionIdByDeviceId(const std::string &deviceId); bool UpdateDeviceByDeviceId(const std::string &deviceId); - bool IsDoubleFrameDevice(const std::string &deviceId); std::optional GetDeviceNameByDeviceId(const std::string &deviceId); + bool RemoveDeviceInfo(std::string deviceId, bool isWifi); + bool SetDeviceNotFresh(const std::string &deviceId); + bool IsDoubleFrameDevice(const std::string &deviceId); private: struct DeviceInfoCollection { diff --git a/service/src/device_manager/include/connection_manager.h b/service/src/device_manager/include/connection_manager.h index 7b18ad5..087d346 100644 --- a/service/src/device_manager/include/connection_manager.h +++ b/service/src/device_manager/include/connection_manager.h @@ -86,12 +86,13 @@ public: void UpdateGrabState(bool changeState, int32_t sessionId); int GetProtocolType() const; - void SetProtocolType(int protocols); + void SetProtocolType(ProtocolType protocol); std::unique_ptr GetLocalDeviceInfo(); void NotifySessionIsReady(int transportId); void NotifyDeviceIsOffline(const std::string &deviceId); bool NotifyConnectStage(const CastInnerRemoteDevice &device, int result, int32_t reasonCode = REASON_DEFAULT); + bool NotifyListenerToLoadSinkSA(const std::string &networkId); void AddSessionListener(int castSessionId, std::shared_ptr listener); void RemoveSessionListener(int castSessionId); @@ -101,11 +102,13 @@ public: void SetRTSPPort(int port); void SendConsultInfo(const std::string &deviceId, int port); + std::string GetConnectingDeviceId(); bool IsSingle(const CastInnerRemoteDevice &device); bool IsHuaweiDevice(const CastInnerRemoteDevice &device); bool IsThirdDevice(const CastInnerRemoteDevice &device); std::map isBindTargetMap_; + std::string connectingDeviceId_{}; private: bool BindTarget(const CastInnerRemoteDevice &dev); @@ -135,7 +138,7 @@ private: void ResetListener(); std::mutex mutex_; - int protocolType_ = 0; + ProtocolType protocolType_ = ProtocolType::CAST_PLUS_MIRROR; std::shared_ptr listener_; std::map> sessionListeners_; std::map transIdToCastSessionIdMap_; diff --git a/service/src/device_manager/include/connection_manager_listener.h b/service/src/device_manager/include/connection_manager_listener.h index 7aca80f..6efadac 100644 --- a/service/src/device_manager/include/connection_manager_listener.h +++ b/service/src/device_manager/include/connection_manager_listener.h @@ -37,6 +37,7 @@ public: virtual void GrabDevice(int32_t sessionId) = 0; virtual int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) = 0; virtual int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) = 0; + virtual bool LoadSinkSA(const std::string &networkId) = 0; }; class IConnectManagerSessionListener { diff --git a/service/src/device_manager/include/discovery_manager.h b/service/src/device_manager/include/discovery_manager.h index 5aaebdc..49c5847 100644 --- a/service/src/device_manager/include/discovery_manager.h +++ b/service/src/device_manager/include/discovery_manager.h @@ -29,6 +29,9 @@ #include "cast_service_common.h" #include "device_manager.h" #include "event_handler.h" +#include "json.hpp" + +using nlohmann::json; namespace OHOS { namespace CastEngine { @@ -99,34 +102,48 @@ public: bool StopAdvertise(); int GetProtocolType() const; - void SetProtocolType(int protocols); void OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInfo &dmDeviceInfo); void NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevice); void NotifyDeviceIsOnline(const DmDeviceInfo &dmDeviceInfo); + std::unordered_map> reportTypeMap_; + private: void StartDmDiscovery(); void StopDmDiscovery(); + void GetAndReportTrustedDevices(); + void ParseDeviceInfo(const DmDeviceInfo &dmDevice, CastInnerRemoteDevice &castDevice); + void ParseCustomData(const json &jsonObj, CastInnerRemoteDevice &castDevice); + void ParseCapability(const std::string customData, CastInnerRemoteDevice &castDevice); void SetListener(std::shared_ptr listener); + std::shared_ptr GetListener(); bool HasListener(); void ResetListener(); + void RemoveSameDeviceLocked(const CastInnerRemoteDevice &newDevice); + CastInnerRemoteDevice CreateRemoteDevice(const DmDeviceInfo &dmDeviceInfo); - void UpdateDeviceState(); + void UpdateDeviceStateLocked(); + void SetDeviceNotFresh(); + void RecordDeviceFoundType(const DmDeviceInfo dmDeviceInfo); + + std::string Mask(const std::string &str); + bool IsDrmMatch(const CastInnerRemoteDevice &newDevice); + bool IsNeedNotify(const CastInnerRemoteDevice &newDevice); std::mutex mutex_; - int32_t uid_{ 0 }; int protocolType_ = 0; + std::vector drmSchemes_; std::shared_ptr listener_; std::shared_ptr eventRunner_; std::shared_ptr eventHandler_; - std::unordered_map remoteDeviceMap; - int32_t scanCount; + std::unordered_map remoteDeviceMap_; + int32_t scanCount_; }; } // namespace CastEngineService } // namespace CastEngine diff --git a/service/src/device_manager/src/cast_device_data_manager.cpp b/service/src/device_manager/src/cast_device_data_manager.cpp index bff9358..283cebf 100644 --- a/service/src/device_manager/src/cast_device_data_manager.cpp +++ b/service/src/device_manager/src/cast_device_data_manager.cpp @@ -410,6 +410,23 @@ int CastDeviceDataManager::GetSessionIdByDeviceId(const std::string &deviceId) return INVALID_ID; } +int CastDeviceDataManager::GetCastSessionIdByDeviceId(const std::string &deviceId) +{ + if (deviceId.empty()) { + CLOGE("Empty device id!"); + return INVALID_ID; + } + + std::lock_guard lock(mutex_); + for (auto it = devices_.begin(); it != devices_.end(); it++) { + if (it->device.deviceId == deviceId) { + return it->device.localCastSessionId; + } + } + + return INVALID_ID; +} + bool CastDeviceDataManager::UpdateDeviceByDeviceId(const std::string &deviceId) { CLOGI("UpdateDeviceByDeviceId in %{public}s", Utils::Mask(deviceId).c_str()); @@ -469,6 +486,48 @@ bool CastDeviceDataManager::IsDoubleFrameDevice(const std::string &deviceId) return false; } +bool CastDeviceDataManager::RemoveDeviceInfo(std::string deviceId, bool isWifi) +{ + CLOGI("RemoveDeviceInfo in %{public}s", Utils::Mask(deviceId).c_str()); + std::lock_guard lock(mutex_); + auto it = GetDeviceLocked(deviceId); + if (it == devices_.end()) { + CLOGE("No device found"); + return false; + } + if (isWifi) { + it->wifiDeviceInfo = {}; + it->device.wifiIp = ""; + it->device.wifiPort = 0; + it->device.isWifiFresh = false; + uint32_t coap = static_cast(NotifyMediumType::COAP); + it->device.mediumTypes = (it->device.mediumTypes | coap) ^ coap; + } else { + it->bleDeviceInfo = {}; + it->device.bleMac = ""; + it->device.isBleFresh = false; + uint32_t ble = static_cast(NotifyMediumType::BLE); + it->device.mediumTypes = (it->device.mediumTypes | ble) ^ ble; + } + return true; +} + +bool CastDeviceDataManager::SetDeviceNotFresh(const std::string &deviceId) +{ + CLOGI("in %{public}s", Utils::Mask(deviceId).c_str()); + std::lock_guard lock(mutex_); + auto it = GetDeviceLocked(deviceId); + if (it == devices_.end()) { + CLOGE("No device found %s", deviceId.c_str()); + return false; + } + + it->device.isWifiFresh = false; + it->device.isBleFresh = false; + it->device.mediumTypes = 0; + return true; +} + } // namespace CastEngineService } // namespace CastEngine } // namespace OHOS diff --git a/service/src/device_manager/src/connection_manager.cpp b/service/src/device_manager/src/connection_manager.cpp index 2906a31..acb404d 100644 --- a/service/src/device_manager/src/connection_manager.cpp +++ b/service/src/device_manager/src/connection_manager.cpp @@ -249,16 +249,6 @@ DmDeviceInfo ConnectionManager::GetDmDeviceInfo(const std::string &deviceId) return {}; } -void ConnectionManager::SetProtocolType(int protocols) -{ - protocolType_ = protocols; -} - -int ConnectionManager::GetProtocolType() const -{ - return protocolType_; -} - bool ConnectionManager::ConnectDevice(const CastInnerRemoteDevice &dev, const ProtocolType &protocolType) { DeviceDiscoveryWriteWrap(__func__, GetAnonymousDeviceID(dev.deviceId)); @@ -864,6 +854,16 @@ void ConnectionManager::UpdateGrabState(bool changeState, int32_t sessionId) grabState_ = DeviceGrabState::NO_GRAB; } +int ConnectionManager::GetProtocolType() const +{ + return static_cast(protocolType_); +} + +void ConnectionManager::SetProtocolType(ProtocolType protocol) +{ + protocolType_ = protocol; +} + void ConnectionManager::AddSessionListener(int castSessionId, std::shared_ptr listener) { std::lock_guard lock(mutex_); @@ -966,6 +966,21 @@ bool ConnectionManager::NotifyConnectStage(const CastInnerRemoteDevice &device, return true; } +bool ConnectionManager::NotifyListenerToLoadSinkSA(const std::string& networkId) +{ + auto listener = GetListener(); + if (!listener) { + return false; + } + return listener->LoadSinkSA(networkId); +} + +std::string ConnectionManager::GetConnectingDeviceId() +{ + std::lock_guard lock(mutex_); + return connectingDeviceId_; +} + void CastBindTargetCallback::OnBindResult(const PeerTargetId &targetId, int32_t result, int32_t status, std::string content) { diff --git a/service/src/device_manager/src/discovery_manager.cpp b/service/src/device_manager/src/discovery_manager.cpp index 835c742..a8665b7 100644 --- a/service/src/device_manager/src/discovery_manager.cpp +++ b/service/src/device_manager/src/discovery_manager.cpp @@ -49,6 +49,22 @@ constexpr int TIMEOUT_COUNT = 40; constexpr int EVENT_START_DISCOVERY = 1; constexpr int EVENT_CONTINUE_DISCOVERY = 2; const std::string DISCOVERY_TRUST_VALUE = R"({"filters": [{"type": "isTrusted", "value": 2}]})"; +const std::string SINGLE_CUST_DATA = R"({"castPlus":"C020"})"; +constexpr int CAST_DATA_LENGTH = 4; + +constexpr int INT_THREE = 3; +constexpr int INT_FOUR = 4; +constexpr int INT_SEVEN = 7; + +// Bit bit of byte 0 +constexpr uint32_t BASE = 1; +constexpr uint32_t HUAWEI_IDENTIFICATION = BASE << 7; +constexpr uint32_t MIRROR_CAPABILITY = BASE << 3; + +// Bit bit of byte 1 +constexpr uint32_t DEVICE_COORDINATION_CAPABILITY = BASE << 7; +constexpr uint32_t DRM_CAPABILITY = BASE << 6; +constexpr uint32_t CAST_STREAM_CAPABILITY = BASE << 5; DeviceType ConvertDeviceType(uint16_t deviceTypeId) { @@ -146,25 +162,17 @@ void DiscoveryManager::GetAndReportTrustedDevices() } } -void DiscoveryManager::UpdateDeviceState() -{ - auto it = std::find_if(remoteDeviceMap.begin(), remoteDeviceMap.end(), [&](const auto& pair) { - return scanCount - pair.second >=1 ; - }); - if (it != remoteDeviceMap.end()) { - CastInnerRemoteDevice remoteDevice = it->first; - CLOGE("StartDmDiscovery offline: %{public}s", remoteDevice.deviceName.c_str()); - ConnectionManager::GetInstance().NotifyDeviceIsOffline(remoteDevice.deviceId); - CastDeviceDataManager::GetInstance().RemoveDevice(remoteDevice.deviceId); - remoteDeviceMap.erase(it); - } -} + void DiscoveryManager::StartDmDiscovery() { CLOGI("StartDmDiscovery in"); - UpdateDeviceState(); - scanCount++; + { + std::lock_guard lock(mutex_); + UpdateDeviceStateLocked(); + reportTypeMap_.clear(); + } + scanCount_++; std::map discoverParam{ { PARAM_KEY_META_TYPE, std::to_string(5) } }; std::map filterOptions{ @@ -209,11 +217,6 @@ bool DiscoveryManager::StopAdvertise() return true; } -void DiscoveryManager::SetProtocolType(int protocols) -{ - protocolType_ = protocols; -} - int DiscoveryManager::GetProtocolType() const { return protocolType_; @@ -233,8 +236,8 @@ void DiscoveryManager::StartDiscovery() {"PEER_UDID", ""}}); CLOGI("StartDiscovery in"); - scanCount = 0; - remoteDeviceMap.clear(); + scanCount_ = 0; + remoteDeviceMap_.clear(); std::lock_guard lock(mutex_); uid_ = IPCSkeleton::GetCallingUid(); eventHandler_->SendEvent(EVENT_START_DISCOVERY); @@ -244,8 +247,13 @@ void DiscoveryManager::StartDiscovery() void DiscoveryManager::StopDiscovery() { CLOGI("StopDiscovery in"); - StopDmDiscovery(); + SetDeviceNotFresh(); eventHandler_->RemoveAllEvents(); + if (eventRunner_ != nullptr) { + eventRunner_->Stop(); + } + + StopDmDiscovery(); } void DiscoveryManager::SetListener(std::shared_ptr listener) @@ -254,6 +262,12 @@ void DiscoveryManager::SetListener(std::shared_ptr li listener_ = listener; } +std::shared_ptr DiscoveryManager::GetListener() +{ + std::lock_guard lock(mutex_); + return listener_; +} + bool DiscoveryManager::HasListener() { std::lock_guard lock(mutex_); @@ -267,11 +281,11 @@ void DiscoveryManager::ResetListener() void DiscoveryManager::RemoveSameDeviceLocked(const CastInnerRemoteDevice &newDevice) { - auto it = std::find_if(remoteDeviceMap.begin(), remoteDeviceMap.end(), [&](const auto& pair) { + auto it = std::find_if(remoteDeviceMap_.begin(), remoteDeviceMap_.end(), [&](const auto& pair) { return pair.first.deviceId == newDevice.deviceId; }); - if (it != remoteDeviceMap.end()) { - remoteDeviceMap.erase(it); + if (it != remoteDeviceMap_.end()) { + remoteDeviceMap_.erase(it); } } @@ -301,11 +315,18 @@ void DiscoveryManager::OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInf CastInnerRemoteDevice newDevice = CreateRemoteDevice(dmDeviceInfo); // If the map does not exist, the notification is sent. - auto it = remoteDeviceMap.find(newDevice); - if (it == remoteDeviceMap.end()) { - // If the deviceId are the same, delete the old device. - RemoveSameDeviceLocked(newDevice); + bool isDeviceExist = true; + { + std::lock_guard lock(mutex_); + auto it = remoteDeviceMap_.find(newDevice); + if (it == remoteDeviceMap_.end()) { + // If the deviceId are the same, delete the old device. + RemoveSameDeviceLocked(newDevice); + isDeviceExist = false; + } + } + if (!isDeviceExist) { // Set subDeviceType based on isTrusted. std::string networkId = dmDeviceInfo.networkId; bool isTrusted = ConnectionManager::GetInstance().IsDeviceTrusted(dmDeviceInfo.deviceId, networkId); @@ -317,7 +338,9 @@ void DiscoveryManager::OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInf NotifyDeviceIsFound(newDevice); } } - remoteDeviceMap[newDevice] = scanCount; + + remoteDeviceMap_[newDevice] = scanCount_; + RecordDeviceFoundType(dmDeviceInfo); } void DiscoveryManager::NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevice) @@ -332,9 +355,21 @@ void DiscoveryManager::NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevic devices.push_back(newDevice); std::lock_guard lock(mutex_); - listener_->OnDeviceFound(devices); + auto listener = GetListener(); + if (listener == nullptr) { + CLOGE("listener is null"); + return; + } + + if (IsDrmMatch(newDevice)) { + listener->OnDeviceFound(devices); + } + if (uid_ == AV_SESSION_UID) { - listener_->OnDeviceFound(device2In1); + if (IsDrmMatch(newDevice)) { + listener->OnDeviceFound(device2In1); + } + return; } } @@ -380,6 +415,156 @@ void DiscoveryManager::ParseDeviceInfo(const DmDeviceInfo &dmDevice, CastInnerRe } } +void DiscoveryManager::ParseCustomData(const json &jsonObj, CastInnerRemoteDevice &castDevice) +{ + if (jsonObj.contains(PARAM_KEY_CUSTOM_DATA) && jsonObj[PARAM_KEY_CUSTOM_DATA].is_string()) { + std::string customData = jsonObj[PARAM_KEY_CUSTOM_DATA]; + json softbusCustData = json::parse(customData, nullptr, false); + if (!softbusCustData.is_discarded() && softbusCustData.contains("castPlus") + && softbusCustData["castPlus"].is_string()) { + std::string castData = softbusCustData["castPlus"]; + castDevice.customData = castData; + ParseCapability(castDevice.customData, castDevice); + } + if (!softbusCustData.is_discarded() && softbusCustData.contains("castId") + && softbusCustData["castId"].is_string()) { + castDevice.udid = softbusCustData["castId"]; + } + } +} + +void DiscoveryManager::ParseCapability(const std::string castData, CastInnerRemoteDevice &newDevice) +{ + auto firstByte = static_cast(Utils::StringToInt(castData.substr(0, 2), 16)); + auto secondByte = static_cast(Utils::StringToInt(castData.substr(2), 16)); + if ((firstByte & MIRROR_CAPABILITY) != 0) { + newDevice.capabilityInfo |= static_cast(ProtocolType::CAST_PLUS_MIRROR); + } + + std::string coordCapa; + if ((firstByte & HUAWEI_IDENTIFICATION) != 0) { + if ((secondByte & CAST_STREAM_CAPABILITY) != 0) { + newDevice.capabilityInfo |= static_cast(ProtocolType::CAST_PLUS_STREAM); + } + + if ((secondByte & DRM_CAPABILITY) != 0) { + newDevice.drmCapabilities.push_back(UUID_CHINADRM); + } + + if ((secondByte & DEVICE_COORDINATION_CAPABILITY) != 0) { + coordCapa = " coordinationCapa = 1"; + } + } + if ((newDevice.capabilityInfo & static_cast(ProtocolType::CAST_PLUS_STREAM)) != 0) { + if (newDevice.capability == CapabilityType::DLNA) { + newDevice.capability = CapabilityType::CAST_AND_DLNA; + } else if (newDevice.capability != CapabilityType::CAST_AND_DLNA) { + newDevice.capability = CapabilityType::CAST_PLUS; + } + } + CLOGI("device %{public}s castData:%{public}s authVersion:%{public}s capabilityInfo:%{public}u drmCapa:%{public}d" + "%{public}s capability:0x%{public}x", + Mask(newDevice.deviceName).c_str(), castData.c_str(), newDevice.authVersion.c_str(), newDevice.capabilityInfo, + (newDevice.drmCapabilities.size() > 0), coordCapa.c_str(), newDevice.capability); +} + +void DiscoveryManager::UpdateDeviceStateLocked() +{ + for (auto it = remoteDeviceMap_.begin(); it != remoteDeviceMap_.end();) { + if (scanCount_ == it->second) { + std::string deviceId = it->first.deviceId; + if (!reportTypeMap_[deviceId].first) { + CastDeviceDataManager::GetInstance().RemoveDeviceInfo(deviceId, true); + } + if (!reportTypeMap_[deviceId].second) { + CastDeviceDataManager::GetInstance().RemoveDeviceInfo(deviceId, false); + } + } + if (scanCount_ - it->second > 1 && + it->first.deviceId != ConnectionManager::GetInstance().GetConnectingDeviceId()) { + CastInnerRemoteDevice remoteDevice = it->first; + CLOGE("StartDmDiscovery offline: %{public}s", Mask(remoteDevice.deviceName).c_str()); + ConnectionManager::GetInstance().NotifyDeviceIsOffline(remoteDevice.deviceId); + CastDeviceDataManager::GetInstance().RemoveDevice(remoteDevice.deviceId); + it = remoteDeviceMap_.erase(it); + } else { + it++; + } + } +} +void DiscoveryManager::SetDeviceNotFresh() +{ + CLOGI("in"); + std::lock_guard lock(mutex_); + for (auto it = remoteDeviceMap_.begin(); it != remoteDeviceMap_.end();) { + std::string deviceId = it->first.deviceId; + CastDeviceDataManager::GetInstance().SetDeviceNotFresh(deviceId); + it++; + } + CLOGI("out"); +} + +void DiscoveryManager::RecordDeviceFoundType(const DmDeviceInfo dmDevice) +{ + CLOGI("scanCount_ is %{public}d", scanCount_); + std::string deviceId = dmDevice.deviceId; + if (reportTypeMap_.find(deviceId) == reportTypeMap_.end()) { + reportTypeMap_.insert({ deviceId, {false, false} }); + } + json jsonObj = json::parse(dmDevice.extraData, nullptr, false); + if (!jsonObj.is_discarded()) { + if (jsonObj.contains(PARAM_KEY_WIFI_IP) && jsonObj[PARAM_KEY_WIFI_IP].is_string()) { + reportTypeMap_[deviceId].first = true; + } + if (jsonObj.contains(PARAM_KEY_BLE_MAC) && jsonObj[PARAM_KEY_BLE_MAC].is_string()) { + reportTypeMap_[deviceId].second = true; + } + } +} + +std::string DiscoveryManager::Mask(const std::string &str) +{ + if (str.empty() || str.length() <= INT_THREE) { + return str; + } else if (str.length() < INT_SEVEN) { + return str.substr(0, INT_THREE) + "***" + str.substr(str.length() - 1); + } else { + return str.substr(0, INT_FOUR) + "***" + str.substr(str.length() - INT_THREE); + } +} + +bool DiscoveryManager::IsDrmMatch(const CastInnerRemoteDevice &newDevice) +{ + if (drmSchemes_.empty()) { + return true; + } + for (auto iter = drmSchemes_.begin(); iter != drmSchemes_.end(); ++iter) { + if (find(newDevice.drmCapabilities.begin(), newDevice.drmCapabilities.end(), *iter) != + newDevice.drmCapabilities.end()) { + return true; + } + } + return false; +} + +bool DiscoveryManager::IsNeedNotify(const CastInnerRemoteDevice &newDevice) +{ + bool isNotifyDevice = system::GetBoolParameter(NOTIFY_DEVICE_FOUND, false); + if (isNotifyDevice) { + CLOGD("prop is true, notify %{public}s ", Mask(newDevice.deviceName).c_str()); + return true; + } + if (newDevice.deviceType == DeviceType::DEVICE_HW_TV) { + if (newDevice.customData.empty() || newDevice.customData.length() < CAST_DATA_LENGTH) { + return false; + } + } else { + CLOGI("%{public}s is not huawei device, notify device", Mask(newDevice.deviceName).c_str()); + return true; + } + return true; +} + void CastDiscoveryCallback::OnDiscoverySuccess(uint16_t subscribeId) { CLOGI("OnDiscoverySuccess, subscribe id:%{public}u", subscribeId); diff --git a/service/src/session/src/utils/include/utils.h b/service/src/session/src/utils/include/utils.h index b98dd40..974c047 100644 --- a/service/src/session/src/utils/include/utils.h +++ b/service/src/session/src/utils/include/utils.h @@ -62,7 +62,7 @@ public: static bool StartWith(const std::string &mainStr, const std::string &subStr); static int IntToByteArray(int num, int length, uint8_t *output); static uint32_t ByteArrayToInt(const uint8_t *input, unsigned int length); - static int32_t StringToInt(const std::string &str); + static int32_t StringToInt(const std::string &str, int base = DECIMALISM); static std::string GetWifiIp(); static bool IsArrayAllZero(const uint8_t *input, int length); static std::string Mask(const std::string &str); @@ -83,6 +83,7 @@ private: constexpr static int MASK_PRINT_POSTFIX_LEN = 2; }; +inline const std::string UUID_CHINADRM = "3d5e6d35-9b9a-41e8-b843-dd3c6e72c42c"; inline constexpr char SANDBOX_PATH[] = "/data/service/el1/public/cast_engine_service"; inline constexpr char PARAM_MEDIA_DUMP[] = "debug.cast.media.dump"; inline constexpr char PARAM_VIDEO_CACHE[] = "debug.cast.video.cache"; diff --git a/service/src/session/src/utils/src/utils.cpp b/service/src/session/src/utils/src/utils.cpp index f434d67..0602aad 100644 --- a/service/src/session/src/utils/src/utils.cpp +++ b/service/src/session/src/utils/src/utils.cpp @@ -110,13 +110,13 @@ uint32_t Utils::ByteArrayToInt(const uint8_t *input, unsigned int length) return output; } -int32_t Utils::StringToInt(const std::string &str) +int32_t Utils::StringToInt(const std::string &str, int base) { if (str.size() == 0) { return 0; } char *nextPtr = nullptr; - long result = strtol(str.c_str(), &nextPtr, DECIMALISM); + long result = strtol(str.c_str(), &nextPtr, base); if (errno == ERANGE || *nextPtr != '\0') { return 0; } From 3add297d6a5e3e5271d05024cc7cf890fcb9095e Mon Sep 17 00:00:00 2001 From: LongestDistance Date: Tue, 12 Nov 2024 09:50:32 +0000 Subject: [PATCH 2/4] =?UTF-8?q?update=20service/src/device=5Fmanager/src/d?= =?UTF-8?q?iscovery=5Fmanager.cpp.=20=E5=A4=9A=E4=BD=99=E7=A9=BA=E8=A1=8C?= =?UTF-8?q?=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LongestDistance --- service/src/device_manager/src/discovery_manager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/service/src/device_manager/src/discovery_manager.cpp b/service/src/device_manager/src/discovery_manager.cpp index a8665b7..1321ce9 100644 --- a/service/src/device_manager/src/discovery_manager.cpp +++ b/service/src/device_manager/src/discovery_manager.cpp @@ -162,8 +162,6 @@ void DiscoveryManager::GetAndReportTrustedDevices() } } - - void DiscoveryManager::StartDmDiscovery() { CLOGI("StartDmDiscovery in"); @@ -492,6 +490,7 @@ void DiscoveryManager::UpdateDeviceStateLocked() } } } + void DiscoveryManager::SetDeviceNotFresh() { CLOGI("in"); From a07ba54aa4289cf1b913843e2a6a73ab9163de94 Mon Sep 17 00:00:00 2001 From: LongestDistance Date: Wed, 13 Nov 2024 02:54:09 +0000 Subject: [PATCH 3/4] =?UTF-8?q?update=20service/src/device=5Fmanager/src/d?= =?UTF-8?q?iscovery=5Fmanager.cpp.=20=E5=8E=BB=E6=8E=89HMOS=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LongestDistance --- .../device_manager/src/discovery_manager.cpp | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/service/src/device_manager/src/discovery_manager.cpp b/service/src/device_manager/src/discovery_manager.cpp index 1321ce9..26f2cd3 100644 --- a/service/src/device_manager/src/discovery_manager.cpp +++ b/service/src/device_manager/src/discovery_manager.cpp @@ -58,14 +58,8 @@ constexpr int INT_SEVEN = 7; // Bit bit of byte 0 constexpr uint32_t BASE = 1; -constexpr uint32_t HUAWEI_IDENTIFICATION = BASE << 7; constexpr uint32_t MIRROR_CAPABILITY = BASE << 3; -// Bit bit of byte 1 -constexpr uint32_t DEVICE_COORDINATION_CAPABILITY = BASE << 7; -constexpr uint32_t DRM_CAPABILITY = BASE << 6; -constexpr uint32_t CAST_STREAM_CAPABILITY = BASE << 5; - DeviceType ConvertDeviceType(uint16_t deviceTypeId) { switch (deviceTypeId) { @@ -439,20 +433,6 @@ void DiscoveryManager::ParseCapability(const std::string castData, CastInnerRemo newDevice.capabilityInfo |= static_cast(ProtocolType::CAST_PLUS_MIRROR); } - std::string coordCapa; - if ((firstByte & HUAWEI_IDENTIFICATION) != 0) { - if ((secondByte & CAST_STREAM_CAPABILITY) != 0) { - newDevice.capabilityInfo |= static_cast(ProtocolType::CAST_PLUS_STREAM); - } - - if ((secondByte & DRM_CAPABILITY) != 0) { - newDevice.drmCapabilities.push_back(UUID_CHINADRM); - } - - if ((secondByte & DEVICE_COORDINATION_CAPABILITY) != 0) { - coordCapa = " coordinationCapa = 1"; - } - } if ((newDevice.capabilityInfo & static_cast(ProtocolType::CAST_PLUS_STREAM)) != 0) { if (newDevice.capability == CapabilityType::DLNA) { newDevice.capability = CapabilityType::CAST_AND_DLNA; @@ -460,10 +440,11 @@ void DiscoveryManager::ParseCapability(const std::string castData, CastInnerRemo newDevice.capability = CapabilityType::CAST_PLUS; } } + CLOGI("device %{public}s castData:%{public}s authVersion:%{public}s capabilityInfo:%{public}u drmCapa:%{public}d" - "%{public}s capability:0x%{public}x", + "capability:0x%{public}x", Mask(newDevice.deviceName).c_str(), castData.c_str(), newDevice.authVersion.c_str(), newDevice.capabilityInfo, - (newDevice.drmCapabilities.size() > 0), coordCapa.c_str(), newDevice.capability); + (newDevice.drmCapabilities.size() > 0), newDevice.capability); } void DiscoveryManager::UpdateDeviceStateLocked() From 9ce4b7211a4824474e43b5c801bc4197ef2bc91f Mon Sep 17 00:00:00 2001 From: LongestDistance Date: Wed, 13 Nov 2024 03:45:58 +0000 Subject: [PATCH 4/4] =?UTF-8?q?update=20service/src/device=5Fmanager/src/d?= =?UTF-8?q?iscovery=5Fmanager.cpp.=20=E5=88=A0=E9=99=A4=E6=9C=AA=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LongestDistance --- service/src/device_manager/src/discovery_manager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/service/src/device_manager/src/discovery_manager.cpp b/service/src/device_manager/src/discovery_manager.cpp index 26f2cd3..90bab3c 100644 --- a/service/src/device_manager/src/discovery_manager.cpp +++ b/service/src/device_manager/src/discovery_manager.cpp @@ -428,7 +428,6 @@ void DiscoveryManager::ParseCustomData(const json &jsonObj, CastInnerRemoteDevic void DiscoveryManager::ParseCapability(const std::string castData, CastInnerRemoteDevice &newDevice) { auto firstByte = static_cast(Utils::StringToInt(castData.substr(0, 2), 16)); - auto secondByte = static_cast(Utils::StringToInt(castData.substr(2), 16)); if ((firstByte & MIRROR_CAPABILITY) != 0) { newDevice.capabilityInfo |= static_cast(ProtocolType::CAST_PLUS_MIRROR); }