!102 完善service的功能

Merge pull request !102 from LongestDistance/master
This commit is contained in:
openharmony_ci 2024-11-13 04:58:19 +00:00 committed by Gitee
commit 052fc51fd1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 362 additions and 55 deletions

View File

@ -65,6 +65,7 @@ public:
void ReportDeviceFound(const std::vector<std::pair<CastRemoteDevice, bool>> &deviceList); void ReportDeviceFound(const std::vector<std::pair<CastRemoteDevice, bool>> &deviceList);
void ReportSessionCreate(const sptr<ICastSessionImpl> &castSession); void ReportSessionCreate(const sptr<ICastSessionImpl> &castSession);
void ReportDeviceOffline(const std::string &deviceId); void ReportDeviceOffline(const std::string &deviceId);
bool LoadSinkSA(const std::string &networkId);
sptr<ICastSessionImpl> GetCastSessionInner(std::string sessionId); sptr<ICastSessionImpl> GetCastSessionInner(std::string sessionId);
class CastServiceLoadCallback : public SystemAbilityLoadCallbackStub { class CastServiceLoadCallback : public SystemAbilityLoadCallbackStub {
@ -119,6 +120,7 @@ public:
void GrabDevice(int32_t sessionId) override; void GrabDevice(int32_t sessionId) override;
int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override; int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override;
int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override; int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override;
bool LoadSinkSA(const std::string &networkId) override;
private: private:
wptr<CastSessionManagerService> service_; wptr<CastSessionManagerService> service_;

View File

@ -361,6 +361,16 @@ int32_t ConnectionManagerListener::SetSessionProtocolType(int sessionId, Protoco
return CAST_ENGINE_SUCCESS; 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<ICastServiceListenerImpl> listener) int32_t CastSessionManagerService::RegisterListener(sptr<ICastServiceListenerImpl> listener)
{ {
CLOGI("RegisterListener in"); 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<CastServiceLoadCallback> 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() bool CastSessionManagerService::CheckAndWaitDevieManagerServiceInit()
{ {
auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();

View File

@ -84,9 +84,12 @@ public:
bool IsDeviceUsed(const std::string &deviceId); bool IsDeviceUsed(const std::string &deviceId);
int GetSessionIdByDeviceId(const std::string &deviceId); int GetSessionIdByDeviceId(const std::string &deviceId);
int GetCastSessionIdByDeviceId(const std::string &deviceId);
bool UpdateDeviceByDeviceId(const std::string &deviceId); bool UpdateDeviceByDeviceId(const std::string &deviceId);
bool IsDoubleFrameDevice(const std::string &deviceId);
std::optional<std::string> GetDeviceNameByDeviceId(const std::string &deviceId); std::optional<std::string> 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: private:
struct DeviceInfoCollection { struct DeviceInfoCollection {

View File

@ -86,12 +86,13 @@ public:
void UpdateGrabState(bool changeState, int32_t sessionId); void UpdateGrabState(bool changeState, int32_t sessionId);
int GetProtocolType() const; int GetProtocolType() const;
void SetProtocolType(int protocols); void SetProtocolType(ProtocolType protocol);
std::unique_ptr<CastLocalDevice> GetLocalDeviceInfo(); std::unique_ptr<CastLocalDevice> GetLocalDeviceInfo();
void NotifySessionIsReady(int transportId); void NotifySessionIsReady(int transportId);
void NotifyDeviceIsOffline(const std::string &deviceId); void NotifyDeviceIsOffline(const std::string &deviceId);
bool NotifyConnectStage(const CastInnerRemoteDevice &device, int result, int32_t reasonCode = REASON_DEFAULT); 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<IConnectManagerSessionListener> listener); void AddSessionListener(int castSessionId, std::shared_ptr<IConnectManagerSessionListener> listener);
void RemoveSessionListener(int castSessionId); void RemoveSessionListener(int castSessionId);
@ -101,11 +102,13 @@ public:
void SetRTSPPort(int port); void SetRTSPPort(int port);
void SendConsultInfo(const std::string &deviceId, int port); void SendConsultInfo(const std::string &deviceId, int port);
std::string GetConnectingDeviceId();
bool IsSingle(const CastInnerRemoteDevice &device); bool IsSingle(const CastInnerRemoteDevice &device);
bool IsHuaweiDevice(const CastInnerRemoteDevice &device); bool IsHuaweiDevice(const CastInnerRemoteDevice &device);
bool IsThirdDevice(const CastInnerRemoteDevice &device); bool IsThirdDevice(const CastInnerRemoteDevice &device);
std::map<std::string, bool> isBindTargetMap_; std::map<std::string, bool> isBindTargetMap_;
std::string connectingDeviceId_{};
private: private:
bool BindTarget(const CastInnerRemoteDevice &dev); bool BindTarget(const CastInnerRemoteDevice &dev);
@ -135,7 +138,7 @@ private:
void ResetListener(); void ResetListener();
std::mutex mutex_; std::mutex mutex_;
int protocolType_ = 0; ProtocolType protocolType_ = ProtocolType::CAST_PLUS_MIRROR;
std::shared_ptr<IConnectionManagerListener> listener_; std::shared_ptr<IConnectionManagerListener> listener_;
std::map<int, std::shared_ptr<IConnectManagerSessionListener>> sessionListeners_; std::map<int, std::shared_ptr<IConnectManagerSessionListener>> sessionListeners_;
std::map<int, int> transIdToCastSessionIdMap_; std::map<int, int> transIdToCastSessionIdMap_;

View File

@ -37,6 +37,7 @@ public:
virtual void GrabDevice(int32_t sessionId) = 0; virtual void GrabDevice(int32_t sessionId) = 0;
virtual int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) = 0; virtual int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) = 0;
virtual int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) = 0; virtual int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) = 0;
virtual bool LoadSinkSA(const std::string &networkId) = 0;
}; };
class IConnectManagerSessionListener { class IConnectManagerSessionListener {

View File

@ -29,6 +29,9 @@
#include "cast_service_common.h" #include "cast_service_common.h"
#include "device_manager.h" #include "device_manager.h"
#include "event_handler.h" #include "event_handler.h"
#include "json.hpp"
using nlohmann::json;
namespace OHOS { namespace OHOS {
namespace CastEngine { namespace CastEngine {
@ -99,34 +102,48 @@ public:
bool StopAdvertise(); bool StopAdvertise();
int GetProtocolType() const; int GetProtocolType() const;
void SetProtocolType(int protocols);
void OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInfo &dmDeviceInfo); void OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInfo &dmDeviceInfo);
void NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevice); void NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevice);
void NotifyDeviceIsOnline(const DmDeviceInfo &dmDeviceInfo); void NotifyDeviceIsOnline(const DmDeviceInfo &dmDeviceInfo);
std::unordered_map<std::string, std::pair<bool, bool>> reportTypeMap_;
private: private:
void StartDmDiscovery(); void StartDmDiscovery();
void StopDmDiscovery(); void StopDmDiscovery();
void GetAndReportTrustedDevices(); void GetAndReportTrustedDevices();
void ParseDeviceInfo(const DmDeviceInfo &dmDevice, CastInnerRemoteDevice &castDevice); 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<IDiscoveryManagerListener> listener); void SetListener(std::shared_ptr<IDiscoveryManagerListener> listener);
std::shared_ptr<IDiscoveryManagerListener> GetListener();
bool HasListener(); bool HasListener();
void ResetListener(); void ResetListener();
void RemoveSameDeviceLocked(const CastInnerRemoteDevice &newDevice); void RemoveSameDeviceLocked(const CastInnerRemoteDevice &newDevice);
CastInnerRemoteDevice CreateRemoteDevice(const DmDeviceInfo &dmDeviceInfo); 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_; std::mutex mutex_;
int32_t uid_{ 0 }; int32_t uid_{ 0 };
int protocolType_ = 0; int protocolType_ = 0;
std::vector<std::string> drmSchemes_;
std::shared_ptr<IDiscoveryManagerListener> listener_; std::shared_ptr<IDiscoveryManagerListener> listener_;
std::shared_ptr<EventRunner> eventRunner_; std::shared_ptr<EventRunner> eventRunner_;
std::shared_ptr<DiscoveryEventHandler> eventHandler_; std::shared_ptr<DiscoveryEventHandler> eventHandler_;
std::unordered_map<CastInnerRemoteDevice, int> remoteDeviceMap; std::unordered_map<CastInnerRemoteDevice, int> remoteDeviceMap_;
int32_t scanCount; int32_t scanCount_;
}; };
} // namespace CastEngineService } // namespace CastEngineService
} // namespace CastEngine } // namespace CastEngine

View File

@ -410,6 +410,23 @@ int CastDeviceDataManager::GetSessionIdByDeviceId(const std::string &deviceId)
return INVALID_ID; return INVALID_ID;
} }
int CastDeviceDataManager::GetCastSessionIdByDeviceId(const std::string &deviceId)
{
if (deviceId.empty()) {
CLOGE("Empty device id!");
return INVALID_ID;
}
std::lock_guard<std::mutex> 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) bool CastDeviceDataManager::UpdateDeviceByDeviceId(const std::string &deviceId)
{ {
CLOGI("UpdateDeviceByDeviceId in %{public}s", Utils::Mask(deviceId).c_str()); CLOGI("UpdateDeviceByDeviceId in %{public}s", Utils::Mask(deviceId).c_str());
@ -469,6 +486,48 @@ bool CastDeviceDataManager::IsDoubleFrameDevice(const std::string &deviceId)
return false; return false;
} }
bool CastDeviceDataManager::RemoveDeviceInfo(std::string deviceId, bool isWifi)
{
CLOGI("RemoveDeviceInfo in %{public}s", Utils::Mask(deviceId).c_str());
std::lock_guard<std::mutex> 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<uint32_t>(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<uint32_t>(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<std::mutex> 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 CastEngineService
} // namespace CastEngine } // namespace CastEngine
} // namespace OHOS } // namespace OHOS

View File

@ -249,16 +249,6 @@ DmDeviceInfo ConnectionManager::GetDmDeviceInfo(const std::string &deviceId)
return {}; return {};
} }
void ConnectionManager::SetProtocolType(int protocols)
{
protocolType_ = protocols;
}
int ConnectionManager::GetProtocolType() const
{
return protocolType_;
}
bool ConnectionManager::ConnectDevice(const CastInnerRemoteDevice &dev, const ProtocolType &protocolType) bool ConnectionManager::ConnectDevice(const CastInnerRemoteDevice &dev, const ProtocolType &protocolType)
{ {
DeviceDiscoveryWriteWrap(__func__, GetAnonymousDeviceID(dev.deviceId)); DeviceDiscoveryWriteWrap(__func__, GetAnonymousDeviceID(dev.deviceId));
@ -864,6 +854,16 @@ void ConnectionManager::UpdateGrabState(bool changeState, int32_t sessionId)
grabState_ = DeviceGrabState::NO_GRAB; grabState_ = DeviceGrabState::NO_GRAB;
} }
int ConnectionManager::GetProtocolType() const
{
return static_cast<int>(protocolType_);
}
void ConnectionManager::SetProtocolType(ProtocolType protocol)
{
protocolType_ = protocol;
}
void ConnectionManager::AddSessionListener(int castSessionId, std::shared_ptr<IConnectManagerSessionListener> listener) void ConnectionManager::AddSessionListener(int castSessionId, std::shared_ptr<IConnectManagerSessionListener> listener)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@ -966,6 +966,21 @@ bool ConnectionManager::NotifyConnectStage(const CastInnerRemoteDevice &device,
return true; 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<std::mutex> lock(mutex_);
return connectingDeviceId_;
}
void CastBindTargetCallback::OnBindResult(const PeerTargetId &targetId, int32_t result, int32_t status, void CastBindTargetCallback::OnBindResult(const PeerTargetId &targetId, int32_t result, int32_t status,
std::string content) std::string content)
{ {

View File

@ -49,6 +49,16 @@ constexpr int TIMEOUT_COUNT = 40;
constexpr int EVENT_START_DISCOVERY = 1; constexpr int EVENT_START_DISCOVERY = 1;
constexpr int EVENT_CONTINUE_DISCOVERY = 2; constexpr int EVENT_CONTINUE_DISCOVERY = 2;
const std::string DISCOVERY_TRUST_VALUE = R"({"filters": [{"type": "isTrusted", "value": 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 MIRROR_CAPABILITY = BASE << 3;
DeviceType ConvertDeviceType(uint16_t deviceTypeId) DeviceType ConvertDeviceType(uint16_t deviceTypeId)
{ {
@ -146,25 +156,15 @@ 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() void DiscoveryManager::StartDmDiscovery()
{ {
CLOGI("StartDmDiscovery in"); CLOGI("StartDmDiscovery in");
UpdateDeviceState(); {
scanCount++; std::lock_guard<std::mutex> lock(mutex_);
UpdateDeviceStateLocked();
reportTypeMap_.clear();
}
scanCount_++;
std::map<std::string, std::string> discoverParam{ std::map<std::string, std::string> discoverParam{
{ PARAM_KEY_META_TYPE, std::to_string(5) } }; { PARAM_KEY_META_TYPE, std::to_string(5) } };
std::map<std::string, std::string> filterOptions{ std::map<std::string, std::string> filterOptions{
@ -209,11 +209,6 @@ bool DiscoveryManager::StopAdvertise()
return true; return true;
} }
void DiscoveryManager::SetProtocolType(int protocols)
{
protocolType_ = protocols;
}
int DiscoveryManager::GetProtocolType() const int DiscoveryManager::GetProtocolType() const
{ {
return protocolType_; return protocolType_;
@ -233,8 +228,8 @@ void DiscoveryManager::StartDiscovery()
{"PEER_UDID", ""}}); {"PEER_UDID", ""}});
CLOGI("StartDiscovery in"); CLOGI("StartDiscovery in");
scanCount = 0; scanCount_ = 0;
remoteDeviceMap.clear(); remoteDeviceMap_.clear();
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
uid_ = IPCSkeleton::GetCallingUid(); uid_ = IPCSkeleton::GetCallingUid();
eventHandler_->SendEvent(EVENT_START_DISCOVERY); eventHandler_->SendEvent(EVENT_START_DISCOVERY);
@ -244,8 +239,13 @@ void DiscoveryManager::StartDiscovery()
void DiscoveryManager::StopDiscovery() void DiscoveryManager::StopDiscovery()
{ {
CLOGI("StopDiscovery in"); CLOGI("StopDiscovery in");
StopDmDiscovery(); SetDeviceNotFresh();
eventHandler_->RemoveAllEvents(); eventHandler_->RemoveAllEvents();
if (eventRunner_ != nullptr) {
eventRunner_->Stop();
}
StopDmDiscovery();
} }
void DiscoveryManager::SetListener(std::shared_ptr<IDiscoveryManagerListener> listener) void DiscoveryManager::SetListener(std::shared_ptr<IDiscoveryManagerListener> listener)
@ -254,6 +254,12 @@ void DiscoveryManager::SetListener(std::shared_ptr<IDiscoveryManagerListener> li
listener_ = listener; listener_ = listener;
} }
std::shared_ptr<IDiscoveryManagerListener> DiscoveryManager::GetListener()
{
std::lock_guard<std::mutex> lock(mutex_);
return listener_;
}
bool DiscoveryManager::HasListener() bool DiscoveryManager::HasListener()
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@ -267,11 +273,11 @@ void DiscoveryManager::ResetListener()
void DiscoveryManager::RemoveSameDeviceLocked(const CastInnerRemoteDevice &newDevice) 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; return pair.first.deviceId == newDevice.deviceId;
}); });
if (it != remoteDeviceMap.end()) { if (it != remoteDeviceMap_.end()) {
remoteDeviceMap.erase(it); remoteDeviceMap_.erase(it);
} }
} }
@ -301,11 +307,18 @@ void DiscoveryManager::OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInf
CastInnerRemoteDevice newDevice = CreateRemoteDevice(dmDeviceInfo); CastInnerRemoteDevice newDevice = CreateRemoteDevice(dmDeviceInfo);
// If the map does not exist, the notification is sent. // If the map does not exist, the notification is sent.
auto it = remoteDeviceMap.find(newDevice); bool isDeviceExist = true;
if (it == remoteDeviceMap.end()) { {
// If the deviceId are the same, delete the old device. std::lock_guard<std::mutex> lock(mutex_);
RemoveSameDeviceLocked(newDevice); 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. // Set subDeviceType based on isTrusted.
std::string networkId = dmDeviceInfo.networkId; std::string networkId = dmDeviceInfo.networkId;
bool isTrusted = ConnectionManager::GetInstance().IsDeviceTrusted(dmDeviceInfo.deviceId, networkId); bool isTrusted = ConnectionManager::GetInstance().IsDeviceTrusted(dmDeviceInfo.deviceId, networkId);
@ -317,7 +330,9 @@ void DiscoveryManager::OnDeviceInfoFound(uint16_t subscribeId, const DmDeviceInf
NotifyDeviceIsFound(newDevice); NotifyDeviceIsFound(newDevice);
} }
} }
remoteDeviceMap[newDevice] = scanCount;
remoteDeviceMap_[newDevice] = scanCount_;
RecordDeviceFoundType(dmDeviceInfo);
} }
void DiscoveryManager::NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevice) void DiscoveryManager::NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevice)
@ -332,9 +347,21 @@ void DiscoveryManager::NotifyDeviceIsFound(const CastInnerRemoteDevice &newDevic
devices.push_back(newDevice); devices.push_back(newDevice);
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> 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) { if (uid_ == AV_SESSION_UID) {
listener_->OnDeviceFound(device2In1); if (IsDrmMatch(newDevice)) {
listener->OnDeviceFound(device2In1);
}
return; return;
} }
} }
@ -380,6 +407,143 @@ 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<uint32_t>(Utils::StringToInt(castData.substr(0, 2), 16));
if ((firstByte & MIRROR_CAPABILITY) != 0) {
newDevice.capabilityInfo |= static_cast<uint32_t>(ProtocolType::CAST_PLUS_MIRROR);
}
if ((newDevice.capabilityInfo & static_cast<uint32_t>(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"
"capability:0x%{public}x",
Mask(newDevice.deviceName).c_str(), castData.c_str(), newDevice.authVersion.c_str(), newDevice.capabilityInfo,
(newDevice.drmCapabilities.size() > 0), 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<std::mutex> 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) void CastDiscoveryCallback::OnDiscoverySuccess(uint16_t subscribeId)
{ {
CLOGI("OnDiscoverySuccess, subscribe id:%{public}u", subscribeId); CLOGI("OnDiscoverySuccess, subscribe id:%{public}u", subscribeId);

View File

@ -62,7 +62,7 @@ public:
static bool StartWith(const std::string &mainStr, const std::string &subStr); static bool StartWith(const std::string &mainStr, const std::string &subStr);
static int IntToByteArray(int num, int length, uint8_t *output); static int IntToByteArray(int num, int length, uint8_t *output);
static uint32_t ByteArrayToInt(const uint8_t *input, unsigned int length); 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 std::string GetWifiIp();
static bool IsArrayAllZero(const uint8_t *input, int length); static bool IsArrayAllZero(const uint8_t *input, int length);
static std::string Mask(const std::string &str); static std::string Mask(const std::string &str);
@ -83,6 +83,7 @@ private:
constexpr static int MASK_PRINT_POSTFIX_LEN = 2; 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 SANDBOX_PATH[] = "/data/service/el1/public/cast_engine_service";
inline constexpr char PARAM_MEDIA_DUMP[] = "debug.cast.media.dump"; inline constexpr char PARAM_MEDIA_DUMP[] = "debug.cast.media.dump";
inline constexpr char PARAM_VIDEO_CACHE[] = "debug.cast.video.cache"; inline constexpr char PARAM_VIDEO_CACHE[] = "debug.cast.video.cache";

View File

@ -110,13 +110,13 @@ uint32_t Utils::ByteArrayToInt(const uint8_t *input, unsigned int length)
return output; return output;
} }
int32_t Utils::StringToInt(const std::string &str) int32_t Utils::StringToInt(const std::string &str, int base)
{ {
if (str.size() == 0) { if (str.size() == 0) {
return 0; return 0;
} }
char *nextPtr = nullptr; char *nextPtr = nullptr;
long result = strtol(str.c_str(), &nextPtr, DECIMALISM); long result = strtol(str.c_str(), &nextPtr, base);
if (errno == ERANGE || *nextPtr != '\0') { if (errno == ERANGE || *nextPtr != '\0') {
return 0; return 0;
} }