2022-06-21 08:40:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 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_kits.h"
|
|
|
|
|
|
|
|
#include "beget_ext.h"
|
|
|
|
#include "device_info_proxy.h"
|
2022-06-22 01:09:13 +00:00
|
|
|
#include "device_info_load.h"
|
2022-06-21 08:40:48 +00:00
|
|
|
#include "idevice_info.h"
|
|
|
|
#include "if_system_ability_manager.h"
|
|
|
|
#include "iservice_registry.h"
|
|
|
|
#include "system_ability_definition.h"
|
|
|
|
#include "securec.h"
|
|
|
|
|
|
|
|
namespace OHOS {
|
|
|
|
namespace device_info {
|
|
|
|
DeviceInfoKits::DeviceInfoKits() {}
|
|
|
|
|
|
|
|
DeviceInfoKits::~DeviceInfoKits() {}
|
|
|
|
|
|
|
|
DeviceInfoKits &DeviceInfoKits::GetInstance()
|
|
|
|
{
|
|
|
|
return DelayedRefSingleton<DeviceInfoKits>::GetInstance();
|
|
|
|
}
|
|
|
|
|
2022-06-22 01:09:13 +00:00
|
|
|
void DeviceInfoKits::LoadDeviceInfoSa()
|
2022-06-21 08:40:48 +00:00
|
|
|
{
|
2022-06-22 01:09:13 +00:00
|
|
|
DINFO_LOGV("deviceInfoService_ is %d", deviceInfoService_ == nullptr);
|
2022-07-01 05:03:03 +00:00
|
|
|
std::unique_lock<std::mutex> lock(lock_);
|
|
|
|
if (deviceInfoService_ != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-22 01:09:13 +00:00
|
|
|
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");
|
|
|
|
|
2023-04-07 00:32:09 +00:00
|
|
|
if (deathRecipient_ == nullptr) {
|
|
|
|
deathRecipient_ = new DeathRecipient();
|
|
|
|
}
|
|
|
|
|
2022-06-22 01:09:13 +00:00
|
|
|
// 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; });
|
2023-04-07 00:32:09 +00:00
|
|
|
if (!waitStatus || deviceInfoService_ == nullptr) {
|
2022-06-22 01:09:13 +00:00
|
|
|
// time out or loadcallback fail
|
|
|
|
DINFO_LOGE("tokensync load sa timeout");
|
|
|
|
return;
|
2022-06-21 08:40:48 +00:00
|
|
|
}
|
2023-04-07 00:32:09 +00:00
|
|
|
|
|
|
|
// for dead
|
|
|
|
auto object = deviceInfoService_->AsObject();
|
|
|
|
if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
|
|
|
|
DINFO_LOGE("Failed to add death recipient");
|
|
|
|
}
|
2022-06-21 08:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr<IDeviceInfo> DeviceInfoKits::GetService()
|
|
|
|
{
|
2022-06-22 01:09:13 +00:00
|
|
|
LoadDeviceInfoSa();
|
|
|
|
return deviceInfoService_;
|
|
|
|
}
|
2022-06-21 08:40:48 +00:00
|
|
|
|
2022-06-22 01:09:13 +00:00
|
|
|
void DeviceInfoKits::FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject)
|
|
|
|
{
|
|
|
|
DINFO_LOGI("get deviceinfo sa success.");
|
|
|
|
// 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_);
|
2022-07-01 05:03:03 +00:00
|
|
|
deviceInfoService_ = iface_cast<IDeviceInfo>(remoteObject);
|
2022-06-22 01:09:13 +00:00
|
|
|
deviceInfoLoadCon_.notify_one();
|
2022-06-21 08:40:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 01:09:13 +00:00
|
|
|
void DeviceInfoKits::FinishStartSAFailed()
|
2022-06-21 08:40:48 +00:00
|
|
|
{
|
2022-10-31 06:28:17 +00:00
|
|
|
DINFO_LOGI("Get deviceinfo sa failed.");
|
2022-06-22 01:09:13 +00:00
|
|
|
// get lock which wait_for release and send a notice
|
|
|
|
std::unique_lock<std::mutex> lock(lock_);
|
2022-07-01 05:03:03 +00:00
|
|
|
deviceInfoService_ = nullptr;
|
2022-06-22 01:09:13 +00:00
|
|
|
deviceInfoLoadCon_.notify_one();
|
2022-06-21 08:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DeviceInfoKits::GetUdid(std::string& result)
|
|
|
|
{
|
|
|
|
auto deviceService = GetService();
|
2022-06-22 01:09:13 +00:00
|
|
|
DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
|
2022-06-21 08:40:48 +00:00
|
|
|
return deviceService->GetUdid(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DeviceInfoKits::GetSerialID(std::string& result)
|
|
|
|
{
|
|
|
|
auto deviceService = GetService();
|
2022-06-22 01:09:13 +00:00
|
|
|
DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
|
2022-06-21 08:40:48 +00:00
|
|
|
return deviceService->GetSerialID(result);
|
|
|
|
}
|
2023-04-07 00:32:09 +00:00
|
|
|
|
|
|
|
void DeviceInfoKits::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
|
|
|
|
{
|
|
|
|
DelayedRefSingleton<DeviceInfoKits>::GetInstance().ResetService(remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceInfoKits::ResetService(const wptr<IRemoteObject> &remote)
|
|
|
|
{
|
|
|
|
DINFO_LOGI("Remote is dead, reset service instance");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 08:40:48 +00:00
|
|
|
} // namespace device_info
|
|
|
|
} // namespace OHOS
|