未验证 提交 b676d4a2 编写于 作者: O openharmony_ci 提交者: Gitee

!818 支持deviceinfo 动态启动

Merge pull request !818 from Mupceet/deviceinfo
......@@ -40,6 +40,7 @@ ohos_shared_library("deviceinfoservice") {
"//base/startup/init_lite/interfaces/hals",
]
defines = [
"INIT_AGENT",
"_GNU_SOURCE",
"USE_MBEDTLS",
]
......
......@@ -16,6 +16,7 @@
#include "beget_ext.h"
#include "device_info_proxy.h"
#include "device_info_load.h"
#include "idevice_info.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
......@@ -33,59 +34,66 @@ DeviceInfoKits &DeviceInfoKits::GetInstance()
return DelayedRefSingleton<DeviceInfoKits>::GetInstance();
}
void DeviceInfoKits::ResetService(const wptr<IRemoteObject> &remote)
void DeviceInfoKits::LoadDeviceInfoSa()
{
std::lock_guard<std::mutex> lock(lock_);
if (deviceInfoService_ != nullptr) {
sptr<IRemoteObject> object = deviceInfoService_->AsObject();
if ((object != nullptr) && (remote == object)) {
object->RemoveDeathRecipient(deathRecipient_);
deviceInfoService_ = nullptr;
}
DINFO_LOGV("deviceInfoService_ is %d", deviceInfoService_ == nullptr);
auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
DINFO_CHECK(sam != nullptr, return, "GetSystemAbilityManager return null");
sptr<DeviceInfoLoad> deviceInfoLoad = new (std::nothrow) DeviceInfoLoad();
DINFO_CHECK(deviceInfoLoad != nullptr, return, "new deviceInfoLoad fail.");
int32_t ret = sam->LoadSystemAbility(SYSPARAM_DEVICE_SERVICE_ID, deviceInfoLoad);
DINFO_CHECK(ret == ERR_OK, return, "LoadSystemAbility deviceinfo sa failed");
std::unique_lock<std::mutex> lock(lock_);
// wait_for release lock and block until time out(60s) or match the condition with notice
auto waitStatus = deviceInfoLoadCon_.wait_for(lock, std::chrono::milliseconds(DEVICEINFO_LOAD_SA_TIMEOUT_MS),
[this]() { return deviceInfoService_ != nullptr; });
if (!waitStatus) {
// time out or loadcallback fail
DINFO_LOGE("tokensync load sa timeout");
return;
}
}
sptr<IDeviceInfo> DeviceInfoKits::GetService()
{
std::lock_guard<std::mutex> lock(lock_);
if (deviceInfoService_ != nullptr) {
return deviceInfoService_;
}
LoadDeviceInfoSa();
return deviceInfoService_;
}
sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
DINFO_CHECK(samgr != nullptr, return nullptr, "Get samgr failed");
sptr<IRemoteObject> object = samgr->GetSystemAbility(SYSPARAM_DEVICE_SERVICE_ID);
DINFO_CHECK(object != nullptr, return nullptr, "Get device service object from samgr failed");
if (deathRecipient_ == nullptr) {
deathRecipient_ = new DeathRecipient();
}
void DeviceInfoKits::FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject)
{
DINFO_LOGI("get deviceinfo sa success.");
deviceInfoService_ = iface_cast<IDeviceInfo>(remoteObject);
if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
DINFO_LOGE("Failed to add death recipient");
}
deviceInfoService_ = iface_cast<IDeviceInfo>(object);
if (deviceInfoService_ == nullptr) {
DINFO_LOGE("device service iface_cast failed");
}
return deviceInfoService_;
// get lock which wait_for release and send a notice so that wait_for can out of block
std::unique_lock<std::mutex> lock(lock_);
deviceInfoLoadCon_.notify_one();
}
void DeviceInfoKits::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
void DeviceInfoKits::FinishStartSAFailed()
{
DelayedRefSingleton<DeviceInfoKits>::GetInstance().ResetService(remote);
DINFO_LOGI("get deviceinfo sa failed.");
// get lock which wait_for release and send a notice
std::unique_lock<std::mutex> lock(lock_);
deviceInfoLoadCon_.notify_one();
}
int32_t DeviceInfoKits::GetUdid(std::string& result)
{
auto deviceService = GetService();
DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get watcher manager");
DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
return deviceService->GetUdid(result);
}
int32_t DeviceInfoKits::GetSerialID(std::string& result)
{
auto deviceService = GetService();
DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get watcher manager");
DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
return deviceService->GetSerialID(result);
}
} // namespace device_info
......
......@@ -29,26 +29,16 @@ public:
static DeviceInfoKits &GetInstance();
int32_t GetUdid(std::string& result);
int32_t GetSerialID(std::string& result);
void ResetService(const wptr<IRemoteObject> &remote);
// For death event procession
class DeathRecipient final : public IRemoteObject::DeathRecipient {
public:
DeathRecipient() = default;
~DeathRecipient() final = default;
DISALLOW_COPY_AND_MOVE(DeathRecipient);
void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
};
sptr<IRemoteObject::DeathRecipient> GetDeathRecipient()
{
return deathRecipient_;
}
void FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject);
void FinishStartSAFailed();
private:
static const int DEVICEINFO_LOAD_SA_TIMEOUT_MS = 60000;
void LoadDeviceInfoSa();
sptr<IDeviceInfo> GetService();
std::mutex lock_;
std::condition_variable deviceInfoLoadCon_;
sptr<IDeviceInfo> deviceInfoService_ {};
sptr<IRemoteObject::DeathRecipient> deathRecipient_ {};
};
} // namespace device_info
} // namespace OHOS
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "device_info_load.h"
#include "device_info_kits.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace device_info {
DeviceInfoLoad::DeviceInfoLoad()
{
}
void DeviceInfoLoad::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr<IRemoteObject>& remoteObject)
{
DINFO_CHECK(systemAbilityId == SYSPARAM_DEVICE_SERVICE_ID, return,
"start aystemabilityId is not deviceinfo! %d", systemAbilityId);
DINFO_CHECK(remoteObject != nullptr, return, "remoteObject is null.");
DINFO_LOGI("OnLoadSystemAbilitySuccess start systemAbilityId: %d success!", systemAbilityId);
DeviceInfoKits::GetInstance().FinishStartSASuccess(remoteObject);
}
void DeviceInfoLoad::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
DINFO_CHECK(systemAbilityId == SYSPARAM_DEVICE_SERVICE_ID, return,
"start aystemabilityId is not deviceinfo! %d", systemAbilityId);
DINFO_LOGI("OnLoadSystemAbilityFail systemAbilityId: %d failed.", systemAbilityId);
DeviceInfoKits::GetInstance().FinishStartSAFailed();
}
} // namespace device_info
} // namespace OHOS
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DEVICE_INFO_LOAD_H
#define DEVICE_INFO_LOAD_H
#include "system_ability_load_callback_stub.h"
#include "idevice_info.h"
namespace OHOS {
namespace device_info {
class DeviceInfoLoad : public SystemAbilityLoadCallbackStub {
public:
explicit DeviceInfoLoad();
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
void OnLoadSystemAbilityFail(int32_t systemAbilityId);
private:
};
} // namespace device_info
} // namespace OHOS
#endif // DEVICE_INFO_LOAD_H
......@@ -15,6 +15,7 @@
"jobs" : {
"on-start" : "services:deviceinfoservice"
},
"ondemand" : true,
"secon" : "u:r:deviceinfoservice:s0"
}
]
......
......@@ -231,6 +231,7 @@ if (defined(ohos_lite)) {
if (enable_ohos_startup_init_feature_deviceinfo) {
sources += [
"//base/startup/init_lite/device_info/device_info_kits.cpp",
"//base/startup/init_lite/device_info/device_info_load.cpp",
"//base/startup/init_lite/device_info/device_info_proxy.cpp",
]
defines += [ "PARAM_FEATURE_DEVICEINFO" ]
......
......@@ -189,7 +189,8 @@ if (defined(ohos_lite)) {
deps = [
"//base/startup/init_lite/services/log:agent_log",
"//base/startup/init_lite/services/loopevent:loopevent",
"//base/startup/init_lite/services/param:parameter",
"//base/startup/init_lite/services/param/base:parameterbase",
"//base/startup/init_lite/services/param/linux:param_client",
"//base/startup/init_lite/services/utils:libinit_utils",
"//third_party/bounds_checking_function:libsec_static",
]
......
......@@ -265,6 +265,7 @@ ohos_unittest("init_unittest") {
if (enable_ohos_startup_init_feature_deviceinfo) {
sources += [
"//base/startup/init_lite/device_info/device_info_kits.cpp",
"//base/startup/init_lite/device_info/device_info_load.cpp",
"//base/startup/init_lite/device_info/device_info_proxy.cpp",
]
defines += [ "PARAM_FEATURE_DEVICEINFO" ]
......
......@@ -67,11 +67,6 @@ HWTEST_F(DeviceInfoUnittest, GetDevUdidTest, TestSize.Level1)
ASSERT_NE(nullptr, samgr);
sptr<IRemoteObject> object = samgr->GetSystemAbility(SYSPARAM_DEVICE_SERVICE_ID);
ASSERT_NE(nullptr, samgr);
OHOS::device_info::DeviceInfoKits &instance = OHOS::device_info::DeviceInfoKits::GetInstance();
auto deathRecipientPtr = instance.GetDeathRecipient();
if (deathRecipientPtr) {
deathRecipientPtr->OnRemoteDied(object);
}
}
HWTEST_F(DeviceInfoUnittest, StubTest, TestSize.Level1)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册