1 /*
2 * Copyright (c) 2024 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 "allowed_bluetooth_devices_plugin.h"
16
17 #include "array_string_serializer.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "edm_errors.h"
21 #include "edm_ipc_interface_code.h"
22 #include "parameters.h"
23 #include "iplugin_manager.h"
24
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(AllowedBluetoothDevicesPlugin::GetPlugin());
28 const char *const PERSIST_BLUETOOTH_CONTROL = "persist.edm.prohibit_bluetooth";
29 const char *const BLUETOOTH_WHITELIST_CHANGED_EVENT = "com.ohos.edm.bluetoothdeviceschanged";
30
InitPlugin(std::shared_ptr<IPluginTemplate<AllowedBluetoothDevicesPlugin,std::vector<std::string>>> ptr)31 void AllowedBluetoothDevicesPlugin::InitPlugin(
32 std::shared_ptr<IPluginTemplate<AllowedBluetoothDevicesPlugin, std::vector<std::string>>> ptr)
33
34 {
35 EDMLOGI("AllowedBluetoothDevicesPlugin InitPlugin...");
36 ptr->InitAttribute(EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES, "allowed_bluetooth_devices",
37 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_BLUETOOTH, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
38 ptr->SetSerializer(ArrayStringSerializer::GetInstance());
39 ptr->SetOnHandlePolicyListener(&AllowedBluetoothDevicesPlugin::OnBasicSetPolicy, FuncOperateType::SET);
40 ptr->SetOnHandlePolicyDoneListener(&AllowedBluetoothDevicesPlugin::OnChangedPolicyDone, FuncOperateType::SET);
41 ptr->SetOnHandlePolicyListener(&AllowedBluetoothDevicesPlugin::OnBasicRemovePolicy, FuncOperateType::REMOVE);
42 ptr->SetOnHandlePolicyDoneListener(&AllowedBluetoothDevicesPlugin::OnChangedPolicyDone, FuncOperateType::REMOVE);
43 maxListSize_ = EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE;
44 }
45
SetOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)46 ErrCode AllowedBluetoothDevicesPlugin::SetOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
47 std::vector<std::string> &failedData)
48 {
49 EDMLOGI("AllowedBluetoothDevicesPlugin OnSetPolicy userId = %{public}d", userId);
50 bool isDisabled = system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false);
51 if (isDisabled) {
52 EDMLOGE("AllowedBluetoothDevicesPlugin OnSetPolicy failed, because bluetooth disabled.");
53 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
54 }
55 return ERR_OK;
56 }
57
OnChangedPolicyDone(bool isGlobalChanged)58 void AllowedBluetoothDevicesPlugin::OnChangedPolicyDone(bool isGlobalChanged)
59 {
60 if (!isGlobalChanged) {
61 return;
62 }
63 NotifyBluetoothDevicesChanged();
64 }
65
NotifyBluetoothDevicesChanged()66 void AllowedBluetoothDevicesPlugin::NotifyBluetoothDevicesChanged()
67 {
68 EDMLOGD("AllowedBluetoothDevicesPlugin NotifyBluetoothDevicesChanged.");
69 AAFwk::Want want;
70 want.SetAction(BLUETOOTH_WHITELIST_CHANGED_EVENT);
71 EventFwk::CommonEventData eventData;
72 eventData.SetWant(want);
73 if (!EventFwk::CommonEventManager::PublishCommonEvent(eventData)) {
74 EDMLOGE("NotifyBluetoothDevicesChanged failed.");
75 }
76 }
77
78 } // namespace EDM
79 } // namespace OHOS