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 "switch_bluetooth_plugin.h"
16
17 #include "bluetooth_def.h"
18 #include "bluetooth_errorcode.h"
19 #include "bluetooth_host.h"
20
21 #include "bool_serializer.h"
22 #include "edm_constants.h"
23 #include "edm_ipc_interface_code.h"
24 #include "iplugin_manager.h"
25 #include "parameters.h"
26
27 namespace OHOS {
28 namespace EDM {
29
30 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(SwitchBluetoothPlugin::GetPlugin());
31 const std::string MDM_BLUETOOTH_PROP = "persist.edm.prohibit_bluetooth";
32
InitPlugin(std::shared_ptr<IPluginTemplate<SwitchBluetoothPlugin,bool>> ptr)33 void SwitchBluetoothPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<SwitchBluetoothPlugin, bool>> ptr)
34 {
35 EDMLOGI("SwitchBluetoothPlugin InitPlugin...");
36 ptr->InitAttribute(EdmInterfaceCode::SWITCH_BLUETOOTH, "switch_bluetooth",
37 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_BLUETOOTH, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
38 ptr->SetSerializer(BoolSerializer::GetInstance());
39 ptr->SetOnHandlePolicyListener(&SwitchBluetoothPlugin::OnSetPolicy, FuncOperateType::SET);
40 }
41
OnSetPolicy(bool & isOpen)42 ErrCode SwitchBluetoothPlugin::OnSetPolicy(bool &isOpen)
43 {
44 if (system::GetBoolParameter(MDM_BLUETOOTH_PROP, false)) {
45 EDMLOGE("SwitchBluetoothPlugin OnSetPolicy failed, because bluetooth disabled.");
46 return EdmReturnErrCode::ENTERPRISE_POLICES_DENIED;
47 }
48 int32_t ret = Bluetooth::BT_NO_ERROR;
49 if (isOpen) {
50 ret = Bluetooth::BluetoothHost::GetDefaultHost().EnableBle();
51 } else {
52 ret = Bluetooth::BluetoothHost::GetDefaultHost().DisableBt();
53 }
54 if (ret != Bluetooth::BT_NO_ERROR) {
55 EDMLOGE("SwitchBluetoothPlugin:OnSetPolicy send request fail. %{public}d", ret);
56 }
57 return ERR_OK;
58 }
59
60 } // namespace EDM
61 } // namespace OHOS