• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "disable_bluetooth_plugin.h"
17 
18 #include "bluetooth_def.h"
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_host.h"
21 #include "bool_serializer.h"
22 #include "edm_constants.h"
23 #include "edm_ipc_interface_code.h"
24 #include "parameters.h"
25 #include "plugin_manager.h"
26 
27 namespace OHOS {
28 namespace EDM {
29 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(DisableBluetoothPlugin::GetPlugin());
30 const std::string DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL = "persist.edm.prohibit_bluetooth";
31 
InitPlugin(std::shared_ptr<IPluginTemplate<DisableBluetoothPlugin,bool>> ptr)32 void DisableBluetoothPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<DisableBluetoothPlugin, bool>> ptr)
33 {
34     EDMLOGI("DisableBluetoothPlugin InitPlugin...");
35     std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissions;
36     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11;
37     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12;
38     typePermissionsForTag11.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
39         "ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH");
40     typePermissionsForTag12.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
41         "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS");
42     typePermissionsForTag12.emplace(IPlugin::PermissionType::BYOD_DEVICE_ADMIN,
43         "ohos.permission.PERSONAL_MANAGE_RESTRICTIONS");
44     tagPermissions.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11);
45     tagPermissions.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12);
46     IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(tagPermissions, IPlugin::ApiType::PUBLIC);
47     ptr->InitAttribute(EdmInterfaceCode::DISABLE_BLUETOOTH, "disabled_bluetooth", config, true);
48     ptr->SetSerializer(BoolSerializer::GetInstance());
49     ptr->SetOnHandlePolicyListener(&DisableBluetoothPlugin::OnSetPolicy, FuncOperateType::SET);
50     ptr->SetOnAdminRemoveListener(&DisableBluetoothPlugin::OnAdminRemove);
51 }
52 
OnSetPolicy(bool & disable)53 ErrCode DisableBluetoothPlugin::OnSetPolicy(bool &disable)
54 {
55     std::string originalPara = system::GetParameter(PERSIST_BLUETOOTH_CONTROL, "false");
56     std::string newPara = disable ? "true" : "false";
57     bool setParaRet = system::SetParameter(PERSIST_BLUETOOTH_CONTROL, newPara);
58     if (!setParaRet) {
59         EDMLOGW("DisableBluetoothPlugin failed when set system para: %{public}d", disable);
60         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
61     }
62 
63     if (disable && Bluetooth::BluetoothHost::GetDefaultHost().IsBrEnabled()) {
64         int ret = Bluetooth::BluetoothHost::GetDefaultHost().DisableBt();
65         if (ret != Bluetooth::BT_NO_ERROR) {
66             setParaRet = system::SetParameter(PERSIST_BLUETOOTH_CONTROL, originalPara);
67             EDMLOGW("DisableBluetoothPlugin failed when disable bt: %{public}d, rollback: %{public}d", ret, setParaRet);
68             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
69         }
70     }
71 
72     EDMLOGI("DisableBluetoothPlugin set system para: %{public}d", disable);
73     return ERR_OK;
74 }
75 
OnAdminRemove(const std::string & adminName,bool & data,int32_t userId)76 ErrCode DisableBluetoothPlugin::OnAdminRemove(const std::string &adminName, bool &data, int32_t userId)
77 {
78     EDMLOGI("DisableBluetoothPlugin OnAdminRemove %{public}d...", data);
79     if (!data) {
80         return ERR_OK;
81     }
82     bool reset = false;
83     return OnSetPolicy(reset);
84 }
85 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)86 ErrCode DisableBluetoothPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
87     int32_t userId)
88 {
89     bool paraValue = system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false);
90     reply.WriteInt32(ERR_OK);
91     reply.WriteBool(paraValue);
92     return ERR_OK;
93 }
94 
95 } // namespace EDM
96 } // namespace OHOS
97