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 "parameter.h"
26 #include "plugin_manager.h"
27 #include "string_serializer.h"
28
29 namespace OHOS {
30 namespace EDM {
31 const std::string KEY_DEVICE_NAME = "settings.general.device_name";
32
33 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(GetDeviceInfoPlugin::GetPlugin());
34
InitPlugin(std::shared_ptr<IPluginTemplate<GetDeviceInfoPlugin,std::string>> ptr)35 void GetDeviceInfoPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<GetDeviceInfoPlugin, std::string>> ptr)
36 {
37 EDMLOGI("GetDeviceInfoPlugin InitPlugin...");
38 ptr->InitAttribute(EdmInterfaceCode::GET_DEVICE_INFO, "get_device_info",
39 "ohos.permission.ENTERPRISE_GET_DEVICE_INFO", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
40 ptr->SetSerializer(StringSerializer::GetInstance());
41 }
42
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)43 ErrCode GetDeviceInfoPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
44 int32_t userId)
45 {
46 std::string label = data.ReadString();
47 EDMLOGI("GetDeviceInfoPlugin OnGetPolicy GetDeviceInfo %{public}s", label.c_str());
48 if (label == EdmConstants::DeviceInfo::DEVICE_NAME) {
49 return GetDeviceName(reply);
50 }
51 if (label == EdmConstants::DeviceInfo::DEVICE_SERIAL) {
52 return GetDeviceSerial(reply);
53 }
54 #ifdef TELEPHONY_CORE_EDM_ENABLE
55 if (label == EdmConstants::DeviceInfo::SIM_INFO) {
56 return GetSimInfo(reply);
57 }
58 #endif
59 reply.WriteInt32(EdmReturnErrCode::INTERFACE_UNSUPPORTED);
60 if (GetPlugin()->GetExtensionPlugin() != nullptr) {
61 reply.WriteString(label);
62 }
63 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
64 }
65
GetDeviceName(MessageParcel & reply)66 ErrCode GetDeviceInfoPlugin::GetDeviceName(MessageParcel &reply)
67 {
68 std::string name;
69 ErrCode code = EdmDataAbilityUtils::GetStringFromSettingsDataShare(KEY_DEVICE_NAME, name);
70 if (FAILED(code)) {
71 EDMLOGD("GetDeviceInfoPlugin::get device name from database failed : %{public}d.", code);
72 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
73 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
74 }
75 if (name.empty()) {
76 name = GetMarketName();
77 }
78 reply.WriteInt32(ERR_OK);
79 reply.WriteString(name);
80 return ERR_OK;
81 }
82
GetDeviceSerial(MessageParcel & reply)83 ErrCode GetDeviceInfoPlugin::GetDeviceSerial(MessageParcel &reply)
84 {
85 std::string serial = GetSerial();
86 reply.WriteInt32(ERR_OK);
87 reply.WriteString(serial);
88 return ERR_OK;
89 }
90
91 #ifdef TELEPHONY_CORE_EDM_ENABLE
GetSimInfo(MessageParcel & reply)92 ErrCode GetDeviceInfoPlugin::GetSimInfo(MessageParcel &reply)
93 {
94 cJSON *json = cJSON_CreateArray();
95 if (json == nullptr) {
96 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
97 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
98 }
99 if (!GetSimInfoBySlotId(EdmConstants::DeviceInfo::SIM_SLOT_ID_0, json) ||
100 !GetSimInfoBySlotId(EdmConstants::DeviceInfo::SIM_SLOT_ID_1, json)) {
101 cJSON_Delete(json);
102 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
103 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
104 }
105 char *jsonStr = cJSON_PrintUnformatted(json);
106 if (jsonStr == nullptr) {
107 cJSON_Delete(json);
108 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
109 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
110 }
111 std::string info = std::string(jsonStr);
112 cJSON_Delete(json);
113 cJSON_free(jsonStr);
114 reply.WriteInt32(ERR_OK);
115 reply.WriteString(info);
116 return ERR_OK;
117 }
118
GetSimInfoBySlotId(int32_t slotId,cJSON * simJson)119 bool GetDeviceInfoPlugin::GetSimInfoBySlotId(int32_t slotId, cJSON *simJson)
120 {
121 cJSON *slotJson = nullptr;
122 CJSON_CREATE_OBJECT_AND_CHECK(slotJson, false);
123 cJSON_AddNumberToObject(slotJson, EdmConstants::DeviceInfo::SIM_SLOT_ID, slotId);
124 std::u16string meid;
125 auto &telephonyService = Telephony::CoreServiceClient::GetInstance();
126 int32_t meidRet = telephonyService.GetMeid(slotId, meid);
127 if (FAILED(meidRet)) {
128 EDMLOGD("GetDeviceInfoPlugin::get sim meid failed: %{public}d.", meidRet);
129 }
130 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_MEID, EdmUtils::Utf16ToUtf8(meid).c_str());
131
132 std::u16string imsi;
133 int32_t imsiRet = telephonyService.GetIMSI(slotId, imsi);
134 if (FAILED(imsiRet)) {
135 EDMLOGD("GetDeviceInfoPlugin::get sim imsi failed: %{public}d.", imsiRet);
136 }
137 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_IMSI, EdmUtils::Utf16ToUtf8(imsi).c_str());
138
139 std::u16string iccId;
140 int32_t iccIdRet = telephonyService.GetSimIccId(slotId, iccId);
141 if (FAILED(iccIdRet)) {
142 EDMLOGD("GetDeviceInfoPlugin::get sim iccid failed: %{public}d.", iccIdRet);
143 }
144 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_ICCID, EdmUtils::Utf16ToUtf8(iccId).c_str());
145
146 std::u16string imei;
147 int32_t imeiRet = telephonyService.GetImei(slotId, imei);
148 if (FAILED(imeiRet)) {
149 EDMLOGD("GetDeviceInfoPlugin::get sim imei failed: %{public}d.", imeiRet);
150 }
151 cJSON_AddStringToObject(slotJson, EdmConstants::DeviceInfo::SIM_IMEI, EdmUtils::Utf16ToUtf8(imei).c_str());
152 if (!cJSON_AddItemToArray(simJson, slotJson)) {
153 cJSON_Delete(slotJson);
154 return false;
155 }
156 return true;
157 }
158 #endif
159 } // namespace EDM
160 } // namespace OHOS
161