!2158 注册上下线监听给业务报一次上线

Merge pull request !2158 from liuzhongming/master
This commit is contained in:
openharmony_ci 2024-11-22 08:29:22 +00:00 committed by Gitee
commit d5076546e7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 140 additions and 9 deletions

View File

@ -420,6 +420,7 @@ private:
int32_t CheckApiPermission(int32_t permissionLevel);
void ConvertDeviceInfoToDeviceBasicInfo(const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo);
uint16_t GetSubscribeIdFromMap(const std::string &pkgName);
void RegDevStateCallbackToService(const std::string &pkgName);
private:
#if !defined(__LITEOS_M__)

View File

@ -392,6 +392,7 @@ int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName,
return ret;
}
#endif
RegDevStateCallbackToService(pkgName);
DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback);
DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback", DM_OK);
LOGI("Completed");
@ -408,6 +409,7 @@ int32_t DeviceManagerImpl::RegisterDevStatusCallback(const std::string &pkgName,
return ERR_DM_INPUT_PARA_INVALID;
}
LOGI("Start, pkgName: %{public}s", pkgName.c_str());
RegDevStateCallbackToService(pkgName);
DeviceManagerNotify::GetInstance().RegisterDeviceStatusCallback(pkgName, callback);
DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStatusCallback", DM_OK);
LOGI("Completed");
@ -1867,6 +1869,7 @@ int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName,
LOGE("DeviceManagerImpl::RegisterDeviceStateCallback failed: input pkgName or callback is empty.");
return ERR_DM_INPUT_PARA_INVALID;
}
RegDevStateCallbackToService(pkgName);
DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback);
DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback", DM_OK);
LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
@ -2412,5 +2415,26 @@ uint16_t DeviceManagerImpl::GetSubscribeIdFromMap(const std::string &pkgName)
}
return DM_INVALID_FLAG_ID;
}
void DeviceManagerImpl::RegDevStateCallbackToService(const std::string &pkgName)
{
if (pkgName.empty()) {
LOGE("Invalid parameter, pkgName is empty.");
return;
}
std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
req->SetPkgName(pkgName);
int32_t ret = ipcClientProxy_->SendRequest(REGISTER_DEV_STATE_CALLBACK, req, rsp);
if (ret != DM_OK) {
LOGI("Send Request failed ret: %{public}d", ret);
return;
}
ret = rsp->GetErrCode();
if (ret != DM_OK) {
LOGE("Failed with ret %{public}d", ret);
return;
}
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -1675,5 +1675,21 @@ ON_IPC_CMD(SINK_BIND_TARGET_RESULT, MessageParcel &data, MessageParcel &reply)
reply.WriteInt32(DM_OK);
return DM_OK;
}
ON_IPC_SET_REQUEST(REGISTER_DEV_STATE_CALLBACK, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
{
std::shared_ptr<IpcReq> pReq = std::static_pointer_cast<IpcReq>(pBaseReq);
std::string pkgName = pReq->GetPkgName();
if (!data.WriteString(pkgName)) {
return ERR_DM_IPC_WRITE_FAILED;
}
return DM_OK;
}
ON_IPC_READ_RESPONSE(REGISTER_DEV_STATE_CALLBACK, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
{
pBaseRsp->SetErrCode(reply.ReadInt32());
return DM_OK;
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -201,6 +201,8 @@ public:
const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid);
int32_t SetLocalDeviceName(const std::string &localDeviceName, const std::string &localDisplayName);
void RemoveNotifyRecord(const ProcessInfo &processInfo);
int32_t RegDevStateCallbackToService(const std::string &pkgName);
int32_t GetTrustedDeviceList(const std::string &pkgName, std::vector<DmDeviceInfo> &deviceList);
private:
bool IsDMServiceImplReady();
bool IsDMServiceAdapterLoad();

View File

@ -78,6 +78,7 @@ public:
void OnSinkBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId, int32_t result,
int32_t status, std::string content) override;
void OnProcessRemove(const ProcessInfo &processInfo) override;
void OnDevStateCallbackAdd(const ProcessInfo &processInfo, const std::vector<DmDeviceInfo> &deviceList) override;
private:
void ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName,

View File

@ -163,6 +163,8 @@ public:
virtual void OnSinkBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId, int32_t result,
int32_t status, std::string content) = 0;
virtual void OnProcessRemove(const ProcessInfo &processInfo) = 0;
virtual void OnDevStateCallbackAdd(const ProcessInfo &processInfo,
const std::vector<DmDeviceInfo> &deviceList) = 0;
};
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -2231,5 +2231,58 @@ void DeviceManagerService::RemoveNotifyRecord(const ProcessInfo &processInfo)
listener_->OnProcessRemove(processInfo);
}
int32_t DeviceManagerService::RegDevStateCallbackToService(const std::string &pkgName)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
CHECK_NULL_RETURN(listener_, ERR_DM_POINT_NULL);
std::vector<DmDeviceInfo> deviceList;
GetTrustedDeviceList(pkgName, deviceList);
if (deviceList.size() == 0) {
return DM_OK;
}
int32_t userId = -1;
MultipleUserConnector::GetCallerUserId(userId);
ProcessInfo processInfo;
processInfo.pkgName = pkgName;
processInfo.userId = userId;
listener_->OnDevStateCallbackAdd(processInfo, deviceList);
#else
(void)pkgName;
#endif
return DM_OK;
}
int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, std::vector<DmDeviceInfo> &deviceList)
{
LOGI("Begin for pkgName = %{public}s.", pkgName.c_str());
if (pkgName.empty()) {
LOGE("Invalid parameter, pkgName is empty.");
return ERR_DM_INPUT_PARA_INVALID;
}
std::vector<DmDeviceInfo> onlineDeviceList;
CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
int32_t ret = softbusListener_->GetTrustedDeviceList(onlineDeviceList);
if (ret != DM_OK) {
LOGE("failed");
return ret;
}
if (!onlineDeviceList.empty() && IsDMServiceImplReady()) {
std::unordered_map<std::string, DmAuthForm> udidMap;
if (PermissionManager::GetInstance().CheckWhiteListSystemSA(pkgName)) {
udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(std::string(ALL_PKGNAME));
} else {
udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(pkgName);
}
for (auto item : onlineDeviceList) {
std::string udid = "";
SoftbusListener::GetUdidByNetworkId(item.networkId, udid);
if (udidMap.find(udid) != udidMap.end()) {
item.authForm = udidMap[udid];
deviceList.push_back(item);
}
}
}
return DM_OK;
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -776,5 +776,27 @@ void DeviceManagerServiceListener::OnProcessRemove(const ProcessInfo &processInf
}
}
}
void DeviceManagerServiceListener::OnDevStateCallbackAdd(const ProcessInfo &processInfo,
const std::vector<DmDeviceInfo> &deviceList)
{
for (auto item : deviceList) {
std::string notifyPkgName = processInfo.pkgName + "#" + std::to_string(processInfo.userId) + "#" +
std::string(item.deviceId);
{
std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
continue;
}
alreadyOnlinePkgName_[notifyPkgName] = item;
}
DmDeviceBasicInfo deviceBasicInfo;
std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
ConvertDeviceInfoToDeviceBasicInfo(processInfo.pkgName, item, deviceBasicInfo);
SetDeviceInfo(pReq, processInfo, DmDeviceState::DEVICE_STATE_ONLINE, item, deviceBasicInfo);
ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
}
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -1625,5 +1625,16 @@ ON_IPC_READ_RESPONSE(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, MessageParcel &reply
pBaseRsp->SetErrCode(reply.ReadInt32());
return DM_OK;
}
ON_IPC_CMD(REGISTER_DEV_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)
{
std::string pkgName = data.ReadString();
int32_t result = DeviceManagerService::GetInstance().RegDevStateCallbackToService(pkgName);
if (!reply.WriteInt32(result)) {
LOGE("write result failed");
return ERR_DM_IPC_WRITE_FAILED;
}
return DM_OK;
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -20,6 +20,7 @@ namespace DistributedHardware {
void DMCommToolTest::SetUp()
{
dmCommTool = DMCommTool::GetInstance();
dmCommTool->Init();
}
void DMCommToolTest::TearDown()
{
@ -31,15 +32,6 @@ void DMCommToolTest::TearDownTestCase()
{
}
/**
* @tc.name: Init_Success
* @tc.type: FUNC
*/
HWTEST_F(DMCommToolTest, Init_Success, testing::ext::TestSize.Level0)
{
EXPECT_NO_THROW(dmCommTool->Init());
}
/**
* @tc.name: GetEventHandler_NotNull
* @tc.type: FUNC

View File

@ -215,6 +215,13 @@ public:
{
(void)processInfo;
}
virtual void OnDevStateCallbackAdd(const ProcessInfo &processInfo,
const std::vector<DmDeviceInfo> &deviceList) override
{
(void)processInfo;
(void)deviceList;
}
};
} // namespace DistributedHardware
} // namespace OHOS