1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "device_info_kits.h"
16
17 #include "beget_ext.h"
18 #include "device_info_proxy.h"
19 #include "device_info_load.h"
20 #include "idevice_info.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "securec.h"
25
26 namespace OHOS {
27 namespace device_info {
DeviceInfoKits()28 DeviceInfoKits::DeviceInfoKits() {}
29
~DeviceInfoKits()30 DeviceInfoKits::~DeviceInfoKits() {}
31
GetInstance()32 DeviceInfoKits &DeviceInfoKits::GetInstance()
33 {
34 return DelayedRefSingleton<DeviceInfoKits>::GetInstance();
35 }
36
LoadDeviceInfoSa()37 void DeviceInfoKits::LoadDeviceInfoSa()
38 {
39 DINFO_LOGV("deviceInfoService_ is %d", deviceInfoService_ == nullptr);
40 std::unique_lock<std::mutex> lock(lock_);
41 if (deviceInfoService_ != nullptr) {
42 return;
43 }
44 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 DINFO_CHECK(sam != nullptr, return, "GetSystemAbilityManager return null");
46
47 sptr<DeviceInfoLoad> deviceInfoLoad = new (std::nothrow) DeviceInfoLoad();
48 DINFO_CHECK(deviceInfoLoad != nullptr, return, "new deviceInfoLoad fail.");
49
50 int32_t ret = sam->LoadSystemAbility(SYSPARAM_DEVICE_SERVICE_ID, deviceInfoLoad);
51 DINFO_CHECK(ret == ERR_OK, return, "LoadSystemAbility deviceinfo sa failed");
52
53 if (deathRecipient_ == nullptr) {
54 deathRecipient_ = new DeathRecipient();
55 }
56
57 // wait_for release lock and block until time out(60s) or match the condition with notice
58 auto waitStatus = deviceInfoLoadCon_.wait_for(lock, std::chrono::milliseconds(DEVICEINFO_LOAD_SA_TIMEOUT_MS),
59 [this]() { return deviceInfoService_ != nullptr; });
60 if (!waitStatus || deviceInfoService_ == nullptr) {
61 // time out or loadcallback fail
62 DINFO_LOGE("tokensync load sa timeout");
63 return;
64 }
65
66 // for dead
67 auto object = deviceInfoService_->AsObject();
68 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
69 DINFO_LOGE("Failed to add death recipient");
70 }
71 }
72
GetService()73 sptr<IDeviceInfo> DeviceInfoKits::GetService()
74 {
75 LoadDeviceInfoSa();
76 return deviceInfoService_;
77 }
78
FinishStartSASuccess(const sptr<IRemoteObject> & remoteObject)79 void DeviceInfoKits::FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject)
80 {
81 DINFO_LOGI("get deviceinfo sa success.");
82 // get lock which wait_for release and send a notice so that wait_for can out of block
83 std::unique_lock<std::mutex> lock(lock_);
84 deviceInfoService_ = iface_cast<IDeviceInfo>(remoteObject);
85 deviceInfoLoadCon_.notify_one();
86 }
87
FinishStartSAFailed()88 void DeviceInfoKits::FinishStartSAFailed()
89 {
90 DINFO_LOGI("Get deviceinfo sa failed.");
91 // get lock which wait_for release and send a notice
92 std::unique_lock<std::mutex> lock(lock_);
93 deviceInfoService_ = nullptr;
94 deviceInfoLoadCon_.notify_one();
95 }
96
GetUdid(std::string & result)97 int32_t DeviceInfoKits::GetUdid(std::string& result)
98 {
99 auto deviceService = GetService();
100 DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
101 return deviceService->GetUdid(result);
102 }
103
GetSerialID(std::string & result)104 int32_t DeviceInfoKits::GetSerialID(std::string& result)
105 {
106 auto deviceService = GetService();
107 DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
108 return deviceService->GetSerialID(result);
109 }
110
OnRemoteDied(const wptr<IRemoteObject> & remote)111 void DeviceInfoKits::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
112 {
113 DelayedRefSingleton<DeviceInfoKits>::GetInstance().ResetService(remote);
114 }
115
ResetService(const wptr<IRemoteObject> & remote)116 void DeviceInfoKits::ResetService(const wptr<IRemoteObject> &remote)
117 {
118 DINFO_LOGI("Remote is dead, reset service instance");
119 std::lock_guard<std::mutex> lock(lock_);
120 if (deviceInfoService_ != nullptr) {
121 sptr<IRemoteObject> object = deviceInfoService_->AsObject();
122 if ((object != nullptr) && (remote == object)) {
123 object->RemoveDeathRecipient(deathRecipient_);
124 deviceInfoService_ = nullptr;
125 }
126 }
127 }
128 } // namespace device_info
129 } // namespace OHOS
130