1 /*
2 * Copyright (c) 2024 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
16 #include "get_device_info_plugin.h"
17
18 #ifdef TELEPHONY_CORE_EDM_ENABLE
19 #include "core_service_client.h"
20 #endif
21 #include "cjson_check.h"
22 #include "edm_data_ability_utils.h"
23 #include "edm_ipc_interface_code.h"
24 #include "edm_utils.h"
25 #include "external_manager_factory.h"
26 #include "parameter.h"
27 #include "iplugin_manager.h"
28 #include "string_serializer.h"
29
30 namespace OHOS {
31 namespace EDM {
32 const std::string KEY_DEVICE_NAME = "settings.general.display_device_name";
33
34 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(GetDeviceInfoPlugin::GetPlugin());
35
InitPlugin(std::shared_ptr<IPluginTemplate<GetDeviceInfoPlugin,std::string>> ptr)36 void GetDeviceInfoPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<GetDeviceInfoPlugin, std::string>> ptr)
37 {
38 EDMLOGI("GetDeviceInfoPlugin InitPlugin...");
39 ptr->InitAttribute(EdmInterfaceCode::GET_DEVICE_INFO, PolicyName::POLICY_GET_DEVICE_INFO,
40 EdmPermission::PERMISSION_ENTERPRISE_GET_DEVICE_INFO, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
41 ptr->SetSerializer(StringSerializer::GetInstance());
42 }
43
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)44 ErrCode GetDeviceInfoPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
45 int32_t userId)
46 {
47 std::string label = data.ReadString();
48 EDMLOGI("GetDeviceInfoPlugin OnGetPolicy GetDeviceInfo %{public}s", label.c_str());
49 if (label == EdmConstants::DeviceInfo::DEVICE_NAME) {
50 return GetDeviceName(reply);
51 }
52 if (label == EdmConstants::DeviceInfo::DEVICE_SERIAL) {
53 return GetDeviceSerial(reply);
54 }
55 #ifdef TELEPHONY_CORE_EDM_ENABLE
56 if (label == EdmConstants::DeviceInfo::SIM_INFO) {
57 return GetSimInfo(reply);
58 }
59 #endif
60 reply.WriteInt32(EdmReturnErrCode::INTERFACE_UNSUPPORTED);
61 if (GetPlugin()->GetExtensionPlugin() != nullptr) {
62 reply.WriteString(label);
63 }
64 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
65 }
66
GetExternalManagerFactory()67 std::shared_ptr<IExternalManagerFactory> GetDeviceInfoPlugin::GetExternalManagerFactory()
68 {
69 if (externalManagerFactory_ == nullptr) {
70 externalManagerFactory_ = std::make_shared<ExternalManagerFactory>();
71 }
72 return externalManagerFactory_;
73 }
74
GetDeviceName(MessageParcel & reply)75 ErrCode GetDeviceInfoPlugin::GetDeviceName(MessageParcel &reply)
76 {
77 std::string name;
78 std::vector<int32_t> ids;
79 std::string userId;
80 ErrCode code = GetExternalManagerFactory()->CreateOsAccountManager()->QueryActiveOsAccountIds(ids);
81 if (SUCCEEDED(code) && !ids.empty()) {
82 userId = std::to_string(ids.at(0));
83 } else {
84 EDMLOGE("GetDeviceInfoPlugin::get current account id failed : %{public}d.", code);
85 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
86 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
87 }
88 std::string settingsDataUri = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" +
89 userId + "?Proxy=true";
90 code = EdmDataAbilityUtils::GetStringFromSettingsDataShare(settingsDataUri, KEY_DEVICE_NAME, name);
91 if (FAILED(code)) {
92 EDMLOGE("GetDeviceInfoPlugin::get device name from database failed : %{public}d.", code);
93 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
94 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
95 }
96 if (name.empty()) {
97 const char *marketName = GetMarketName();
98 if (marketName == nullptr) {
99 EDMLOGE("GetDeviceInfoPlugin GetDeviceName Failed. GetMarketName is nullptr.");
100 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
101 }
102 name = marketName;
103 }
104 reply.WriteInt32(ERR_OK);
105 reply.WriteString(name);
106 return ERR_OK;
107 }
108
GetDeviceSerial(MessageParcel & reply)109 ErrCode GetDeviceInfoPlugin::GetDeviceSerial(MessageParcel &reply)
110 {
111 const char* serialPtr = GetSerial();
112 if (serialPtr == nullptr) {
113 EDMLOGE("GetDeviceInfoPlugin GetDeviceSerial Failed. GetSerial is nullptr.");
114 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
115 }
116 std::string serial = serialPtr;
117 reply.WriteInt32(ERR_OK);
118 reply.WriteString(serial);
119 return ERR_OK;
120 }
121
122 #ifdef TELEPHONY_CORE_EDM_ENABLE
GetSimInfo(MessageParcel & reply)123 ErrCode GetDeviceInfoPlugin::GetSimInfo(MessageParcel &reply)
124 {
125 cJSON *json = cJSON_CreateArray();
126 if (json == nullptr) {
127 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
128 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
129 }
130 if (!GetSimInfoBySlotId(EdmConstants::DeviceInfo::SIM_SLOT_ID_0, json) ||
131 !GetSimInfoBySlotId(EdmConstants::DeviceInfo::SIM_SLOT_ID_1, json)) {
132 cJSON_Delete(json);
133 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
134 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
135 }
136 char *jsonStr = cJSON_PrintUnformatted(json);
137 if (jsonStr == nullptr) {
138 cJSON_Delete(json);
139 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
140 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
141 }
142 std::string info = std::string(jsonStr);
143 cJSON_Delete(json);
144 cJSON_free(jsonStr);
145 reply.WriteInt32(ERR_OK);
146 reply.WriteString(info);
147 return ERR_OK;
148 }
149
GetSimInfoBySlotId(int32_t slotId,cJSON * simJson)150 bool GetDeviceInfoPlugin::GetSimInfoBySlotId(int32_t slotId, cJSON *simJson)
151 {
152 cJSON *slotJson = nullptr;
153 CJSON_CREATE_OBJECT_AND_CHECK(slotJson, false);
154 cJSON_AddNumberToObject(slotJson, EdmConstants::DeviceInfo::SIM_SLOT_ID, slotId);
155 std::u16string meid;
156 auto &telephonyService = Telephony::CoreServiceClient::GetInstance();
157 int32_t meidRet = telephonyService.GetMeid(slotId, meid);
158 if (FAILED(meidRet)) {
159 EDMLOGD("GetDeviceInfoPlugin::get sim meid failed: %{public}d.", meidRet);
160 }
161 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_MEID, EdmUtils::Utf16ToUtf8(meid).c_str());
162
163 std::u16string imsi;
164 int32_t imsiRet = telephonyService.GetIMSI(slotId, imsi);
165 if (FAILED(imsiRet)) {
166 EDMLOGD("GetDeviceInfoPlugin::get sim imsi failed: %{public}d.", imsiRet);
167 }
168 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_IMSI, EdmUtils::Utf16ToUtf8(imsi).c_str());
169
170 std::u16string iccId;
171 int32_t iccIdRet = telephonyService.GetSimIccId(slotId, iccId);
172 if (FAILED(iccIdRet)) {
173 EDMLOGD("GetDeviceInfoPlugin::get sim iccid failed: %{public}d.", iccIdRet);
174 }
175 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_ICCID, EdmUtils::Utf16ToUtf8(iccId).c_str());
176
177 std::u16string imei;
178 int32_t imeiRet = telephonyService.GetImei(slotId, imei);
179 if (FAILED(imeiRet)) {
180 EDMLOGD("GetDeviceInfoPlugin::get sim imei failed: %{public}d.", imeiRet);
181 }
182 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_IMEI, EdmUtils::Utf16ToUtf8(imei).c_str());
183 if (!cJSON_AddItemToArray(simJson, slotJson)) {
184 cJSON_Delete(slotJson);
185 return false;
186 }
187 return true;
188 }
189 #endif
190 } // namespace EDM
191 } // namespace OHOS
192