• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // wait_for release lock and block until time out(60s) or match the condition with notice
54     auto waitStatus = deviceInfoLoadCon_.wait_for(lock, std::chrono::milliseconds(DEVICEINFO_LOAD_SA_TIMEOUT_MS),
55         [this]() { return deviceInfoService_ != nullptr; });
56     if (!waitStatus) {
57         // time out or loadcallback fail
58         DINFO_LOGE("tokensync load sa timeout");
59         return;
60     }
61 }
62 
GetService()63 sptr<IDeviceInfo> DeviceInfoKits::GetService()
64 {
65     LoadDeviceInfoSa();
66     return deviceInfoService_;
67 }
68 
FinishStartSASuccess(const sptr<IRemoteObject> & remoteObject)69 void DeviceInfoKits::FinishStartSASuccess(const sptr<IRemoteObject> &remoteObject)
70 {
71     DINFO_LOGI("get deviceinfo sa success.");
72     // get lock which wait_for release and send a notice so that wait_for can out of block
73     std::unique_lock<std::mutex> lock(lock_);
74     deviceInfoService_ = iface_cast<IDeviceInfo>(remoteObject);
75     deviceInfoLoadCon_.notify_one();
76 }
77 
FinishStartSAFailed()78 void DeviceInfoKits::FinishStartSAFailed()
79 {
80     DINFO_LOGI("get deviceinfo sa failed.");
81     // get lock which wait_for release and send a notice
82     std::unique_lock<std::mutex> lock(lock_);
83     deviceInfoService_ = nullptr;
84     deviceInfoLoadCon_.notify_one();
85 }
86 
GetUdid(std::string & result)87 int32_t DeviceInfoKits::GetUdid(std::string& result)
88 {
89     auto deviceService = GetService();
90     DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
91     return deviceService->GetUdid(result);
92 }
93 
GetSerialID(std::string & result)94 int32_t DeviceInfoKits::GetSerialID(std::string& result)
95 {
96     auto deviceService = GetService();
97     DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager");
98     return deviceService->GetSerialID(result);
99 }
100 } // namespace device_info
101 } // namespace OHOS
102