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 "idevice_info.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "securec.h"
24
25 namespace {
26 static constexpr int UDID_LEN = 65;
27 static constexpr int MAX_SERIAL_LEN = 65;
28 } // namespace name
29
30 namespace OHOS {
31 namespace device_info {
DeviceInfoKits()32 DeviceInfoKits::DeviceInfoKits() {}
33
~DeviceInfoKits()34 DeviceInfoKits::~DeviceInfoKits() {}
35
GetInstance()36 DeviceInfoKits &DeviceInfoKits::GetInstance()
37 {
38 return DelayedRefSingleton<DeviceInfoKits>::GetInstance();
39 }
40
ResetService(const wptr<IRemoteObject> & remote)41 void DeviceInfoKits::ResetService(const wptr<IRemoteObject> &remote)
42 {
43 std::lock_guard<std::mutex> lock(lock_);
44 if (deviceInfoService_ != nullptr) {
45 sptr<IRemoteObject> object = deviceInfoService_->AsObject();
46 if ((object != nullptr) && (remote == object)) {
47 object->RemoveDeathRecipient(deathRecipient_);
48 deviceInfoService_ = nullptr;
49 }
50 }
51 }
52
GetService()53 sptr<IDeviceInfo> DeviceInfoKits::GetService()
54 {
55 std::lock_guard<std::mutex> lock(lock_);
56 if (deviceInfoService_ != nullptr) {
57 return deviceInfoService_;
58 }
59
60 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61 DINFO_CHECK(samgr != nullptr, return nullptr, "Get samgr failed");
62 sptr<IRemoteObject> object = samgr->GetSystemAbility(SYSPARAM_DEVICE_SERVICE_ID);
63 DINFO_CHECK(object != nullptr, return nullptr, "Get device service object from samgr failed");
64 if (deathRecipient_ == nullptr) {
65 deathRecipient_ = new DeathRecipient();
66 }
67
68 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
69 DINFO_LOGE("Failed to add death recipient");
70 }
71 deviceInfoService_ = iface_cast<IDeviceInfo>(object);
72 if (deviceInfoService_ == nullptr) {
73 DINFO_LOGE("device service iface_cast failed");
74 }
75 return deviceInfoService_;
76 }
77
OnRemoteDied(const wptr<IRemoteObject> & remote)78 void DeviceInfoKits::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
79 {
80 DelayedRefSingleton<DeviceInfoKits>::GetInstance().ResetService(remote);
81 }
82
GetUdid(std::string & result)83 int32_t DeviceInfoKits::GetUdid(std::string& result)
84 {
85 printf("DeviceInfoKits::GetUdid \n");
86 auto deviceService = GetService();
87 DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get watcher manager");
88 return deviceService->GetUdid(result);
89 }
90
GetSerialID(std::string & result)91 int32_t DeviceInfoKits::GetSerialID(std::string& result)
92 {
93 auto deviceService = GetService();
94 DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get watcher manager");
95 return deviceService->GetSerialID(result);
96 }
97 } // namespace device_info
98 } // namespace OHOS
99
100 #ifdef __cplusplus
101 #if __cplusplus
102 extern "C" {
103 #endif
104 #endif /* __cplusplus */
105
AclGetDevUdid(char * udid,int size)106 int AclGetDevUdid(char *udid, int size)
107 {
108 if (udid == nullptr || size < UDID_LEN) {
109 return -1;
110 }
111 printf("AclGetDevUdid \n");
112 DINFO_LOGI("AclGetDevUdid");
113 std::string result = {};
114 OHOS::device_info::DeviceInfoKits &instance = OHOS::device_info::DeviceInfoKits::GetInstance();
115 int ret = instance.GetUdid(result);
116 if (ret == 0) {
117 ret = strcpy_s(udid, size, result.c_str());
118 }
119 printf("GetDevUdid %s \n", udid);
120 DINFO_LOGI("GetDevUdid %s", udid);
121 return ret;
122 }
123
AclGetSerial(void)124 const char *AclGetSerial(void)
125 {
126 DINFO_LOGI("AclGetSerial");
127 static char serialNumber[MAX_SERIAL_LEN] = {"1234567890"};
128 std::string result = {};
129 OHOS::device_info::DeviceInfoKits &instance = OHOS::device_info::DeviceInfoKits::GetInstance();
130 int ret = instance.GetSerialID(result);
131 if (ret == 0) {
132 (void)strcpy_s(serialNumber, sizeof(serialNumber), result.c_str());
133 }
134 DINFO_LOGI("GetSerial %s", serialNumber);
135 return serialNumber;
136 }
137
138 #ifdef __cplusplus
139 #if __cplusplus
140 }
141 #endif
142 #endif /* __cplusplus */