1 /*
2 * Copyright (c) 2025 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 "disallowed_sim_plugin.h"
16
17 #include "core_service_client.h"
18 #include "parameters.h"
19 #include "edm_ipc_interface_code.h"
20 #include "iplugin_manager.h"
21
22 namespace OHOS {
23 namespace EDM {
24
25 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedSimPlugin::GetPlugin());
26 const std::string PARAM_DISABLE_SLOT0 = "persist.edm.disable_slot_0";
27 const std::string PARAM_DISABLE_SLOT1 = "persist.edm.disable_slot_1";
28
29 constexpr uint32_t SOLT0_ID = 0;
30 constexpr uint32_t SOLT1_ID = 1;
31 #define SOLT0_BIT (1 << 0)
32 #define SOLT1_BIT (1 << 1)
33
34
InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedSimPlugin,uint32_t>> ptr)35 void DisallowedSimPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedSimPlugin, uint32_t>> ptr)
36 {
37 EDMLOGI("DisallowedSimPlugin InitPlugin...");
38 ptr->InitAttribute(EdmInterfaceCode::DISALLOWED_SIM, "disallowed_sim",
39 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_TELEPHONY, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
40 ptr->SetSerializer(BitSerializer::GetInstance());
41 ptr->SetOnHandlePolicyListener(&DisallowedSimPlugin::OnSetPolicy, FuncOperateType::SET);
42 ptr->SetOnHandlePolicyListener(&DisallowedSimPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
43 ptr->SetOnAdminRemoveListener(&DisallowedSimPlugin::OnAdminRemove);
44 }
45
OnSetPolicy(uint32_t & data,uint32_t & currentData,uint32_t & mergeData,int32_t userId)46 ErrCode DisallowedSimPlugin::OnSetPolicy(uint32_t &data, uint32_t ¤tData, uint32_t &mergeData, int32_t userId)
47 {
48 EDMLOGI("DisallowedSimPlugin OnSetPolicy slotId %{public}d", data);
49 bool ret = false;
50 if (data == SOLT0_ID) {
51 currentData |= SOLT0_BIT;
52 ret = system::SetParameter(PARAM_DISABLE_SLOT0, "true");
53 } else if (data == SOLT1_ID) {
54 currentData |= SOLT1_BIT;
55 ret = system::SetParameter(PARAM_DISABLE_SLOT1, "true");
56 } else {
57 EDMLOGE("DisallowedSimPlugin:OnSetPolicy Parameter err, slotId %{public}d", data);
58 return EdmReturnErrCode::PARAM_ERROR;
59 }
60 if (!ret) {
61 EDMLOGE("DisallowedSimPlugin:OnSetPolicy SetParameter fail, slotId %{public}d", data);
62 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
63 }
64 int32_t simId = Telephony::CoreServiceClient::GetInstance().GetSimId(data);
65 if (simId > 0) {
66 Telephony::CoreServiceClient::GetInstance().SetActiveSim(data, 0);
67 }
68 mergeData |= currentData;
69
70 return ERR_OK;
71 }
72
OnRemovePolicy(uint32_t & data,uint32_t & currentData,uint32_t & mergeData,int32_t userId)73 ErrCode DisallowedSimPlugin::OnRemovePolicy(uint32_t &data, uint32_t ¤tData, uint32_t &mergeData, int32_t userId)
74 {
75 EDMLOGI("DisallowedSimPlugin OnRemovePolicy slotId %{public}d", data);
76 bool ret = false;
77 if (data == SOLT0_ID) {
78 currentData = currentData & ~SOLT0_BIT;
79 if ((mergeData & SOLT0_BIT) == 0) {
80 ret = system::SetParameter(PARAM_DISABLE_SLOT0, "false");
81 }
82 } else if (data == SOLT1_ID) {
83 currentData = currentData & ~SOLT1_BIT;
84 if ((mergeData & SOLT1_BIT) == 0) {
85 ret = system::SetParameter(PARAM_DISABLE_SLOT1, "false");
86 }
87 } else {
88 EDMLOGE("DisallowedSimPlugin:OnRemovePolicy Parameter err, slotId %{public}d", data);
89 return EdmReturnErrCode::PARAM_ERROR;
90 }
91 if (!ret) {
92 EDMLOGE("DisallowedSimPlugin:OnRemovePolicy SetParameter fail, slotId %{public}d", data);
93 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
94 }
95 mergeData |= currentData;
96
97 return ERR_OK;
98 }
99
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)100 ErrCode DisallowedSimPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
101 int32_t userId)
102 {
103 EDMLOGD("DisallowedSimPlugin OnGetPolicy.");
104 int32_t slotId = data.ReadInt32();
105
106 bool isDisable = false;
107 if (slotId == SOLT0_ID) {
108 isDisable = system::GetBoolParameter(PARAM_DISABLE_SLOT0, false);
109 } else if (slotId == SOLT1_ID) {
110 isDisable = system::GetBoolParameter(PARAM_DISABLE_SLOT1, false);
111 } else {
112 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
113 return EdmReturnErrCode::PARAM_ERROR;
114 }
115 reply.WriteInt32(ERR_OK);
116 reply.WriteBool(isDisable);
117
118 return ERR_OK;
119 }
120
OnAdminRemove(const std::string & adminName,uint32_t & data,uint32_t & mergeData,int32_t userId)121 ErrCode DisallowedSimPlugin::OnAdminRemove(const std::string &adminName, uint32_t &data,
122 uint32_t &mergeData, int32_t userId)
123 {
124 EDMLOGI("DisallowedSimPlugin OnAdminRemove adminName : %{public}s, data : %{public}d",
125 adminName.c_str(), data);
126 if ((mergeData & SOLT0_BIT) == 0) {
127 if (!system::SetParameter(PARAM_DISABLE_SLOT0, "false")) {
128 EDMLOGE("DisallowedSimPlugin:OnAdminRemove slot0 SetParameter fail");
129 }
130 }
131 if ((mergeData & SOLT1_BIT) == 0) {
132 if (!system::SetParameter(PARAM_DISABLE_SLOT1, "false")) {
133 EDMLOGE("DisallowedSimPlugin:OnAdminRemove slot1 SetParameter fail");
134 }
135 }
136
137 return ERR_OK;
138 }
139
140 } // namespace EDM
141 } // namespace OHOS