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::string> perms;
36     perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11,
37         "ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH"));
38     perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
39         "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS"));
40     IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(perms,
41         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
42     ptr->InitAttribute(EdmInterfaceCode::DISABLE_BLUETOOTH, "disabled_bluetooth", config, false);
43     ptr->SetSerializer(BoolSerializer::GetInstance());
44     ptr->SetOnHandlePolicyListener(&DisableBluetoothPlugin::OnSetPolicy, FuncOperateType::SET);
45 }
46 
OnSetPolicy(bool & disable)47 ErrCode DisableBluetoothPlugin::OnSetPolicy(bool &disable)
48 {
49     std::string originalPara = system::GetParameter(PERSIST_BLUETOOTH_CONTROL, "false");
50     std::string newPara = disable ? "true" : "false";
51     bool setParaRet = system::SetParameter(PERSIST_BLUETOOTH_CONTROL, newPara);
52     if (!setParaRet) {
53         EDMLOGW("DisableBluetoothPlugin failed when set system para: %{public}d", disable);
54         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
55     }
56 
57     if (disable && Bluetooth::BluetoothHost::GetDefaultHost().IsBrEnabled()) {
58         int ret = Bluetooth::BluetoothHost::GetDefaultHost().DisableBt();
59         if (ret != Bluetooth::BT_NO_ERROR) {
60             setParaRet = system::SetParameter(PERSIST_BLUETOOTH_CONTROL, originalPara);
61             EDMLOGW("DisableBluetoothPlugin failed when disable bt: %{public}d, rollback: %{public}d", ret, setParaRet);
62             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
63         }
64     }
65 
66     EDMLOGI("DisableBluetoothPlugin set system para: %{public}d", disable);
67     return ERR_OK;
68 }
69 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)70 ErrCode DisableBluetoothPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
71     int32_t userId)
72 {
73     bool paraValue = system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false);
74     reply.WriteInt32(ERR_OK);
75     reply.WriteBool(paraValue);
76     return ERR_OK;
77 }
78 
79 } // namespace EDM
80 } // namespace OHOS
81