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_ipc_interface_code.h"
23 #include "iplugin_manager.h"
24 #include "parameters.h"
25
26 namespace OHOS {
27 namespace EDM {
28 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisableBluetoothPlugin::GetPlugin());
29 const std::string DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL = "persist.edm.prohibit_bluetooth";
30
InitPlugin(std::shared_ptr<IPluginTemplate<DisableBluetoothPlugin,bool>> ptr)31 void DisableBluetoothPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<DisableBluetoothPlugin, bool>> ptr)
32 {
33 EDMLOGD("DisableBluetoothPlugin InitPlugin...");
34 ptr->InitAttribute(EdmInterfaceCode::DISABLE_BLUETOOTH, "disabled_bluetooth",
35 "ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
36 ptr->SetSerializer(BoolSerializer::GetInstance());
37 ptr->SetOnHandlePolicyListener(&DisableBluetoothPlugin::OnSetPolicy, FuncOperateType::SET);
38 }
39
OnSetPolicy(bool & disable)40 ErrCode DisableBluetoothPlugin::OnSetPolicy(bool &disable)
41 {
42 std::string originalPara = system::GetParameter(PERSIST_BLUETOOTH_CONTROL, "false");
43 std::string newPara = disable ? "true" : "false";
44 bool setParaRet = system::SetParameter(PERSIST_BLUETOOTH_CONTROL, newPara);
45 if (!setParaRet) {
46 EDMLOGW("DisableBluetoothPlugin failed when set system para: %{public}d", disable);
47 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
48 }
49
50 if (disable && Bluetooth::BluetoothHost::GetDefaultHost().IsBrEnabled()) {
51 int ret = Bluetooth::BluetoothHost::GetDefaultHost().DisableBt();
52 if (ret != Bluetooth::BT_NO_ERROR) {
53 setParaRet = system::SetParameter(PERSIST_BLUETOOTH_CONTROL, originalPara);
54 EDMLOGW("DisableBluetoothPlugin failed when disable bt: %{public}d, rollback: %{public}d", ret, setParaRet);
55 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
56 }
57 }
58
59 EDMLOGI("DisableBluetoothPlugin set system para: %{public}d", disable);
60 return ERR_OK;
61 }
62
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)63 ErrCode DisableBluetoothPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
64 int32_t userId)
65 {
66 bool paraValue = system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false);
67 reply.WriteInt32(ERR_OK);
68 reply.WriteBool(paraValue);
69 return ERR_OK;
70 }
71
72 } // namespace EDM
73 } // namespace OHOS
74