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 "plugin_manager.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = PluginManager::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         "ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
38     ptr->SetSerializer(ArrayStringSerializer::GetInstance());
39     ptr->SetOnHandlePolicyListener(&AllowedBluetoothDevicesPlugin::OnSetPolicy, FuncOperateType::SET);
40     ptr->SetOnHandlePolicyDoneListener(&AllowedBluetoothDevicesPlugin::OnChangedPolicyDone, FuncOperateType::SET);
41     ptr->SetOnHandlePolicyListener(&AllowedBluetoothDevicesPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
42     ptr->SetOnHandlePolicyDoneListener(&AllowedBluetoothDevicesPlugin::OnChangedPolicyDone, FuncOperateType::REMOVE);
43 }
44 
OnSetPolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,int32_t userId)45 ErrCode AllowedBluetoothDevicesPlugin::OnSetPolicy(std::vector<std::string> &data,
46     std::vector<std::string> &currentData, int32_t userId)
47 {
48     EDMLOGI("AllowedBluetoothDevicesPlugin OnSetPolicy userId = %{public}d", userId);
49     bool isDisabled = system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false);
50     if (isDisabled) {
51         EDMLOGE("AllowedBluetoothDevicesPlugin OnSetPolicy failed, because bluetooth disabled.");
52         return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
53     }
54     if (data.empty()) {
55         EDMLOGW("AllowedBluetoothDevicesPlugin OnSetPolicy data is empty.");
56         return ERR_OK;
57     }
58     if (data.size() > EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE) {
59         EDMLOGE("AllowedBluetoothDevicesPlugin OnSetPolicy input data is too large.");
60         return EdmReturnErrCode::PARAM_ERROR;
61     }
62 
63     std::vector<std::string> mergeData = ArrayStringSerializer::GetInstance()->SetUnionPolicyData(data, currentData);
64     if (mergeData.size() > EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE) {
65         EDMLOGE("AllowedBluetoothDevicesPlugin OnSetPolicy merge data is too large.");
66         return EdmReturnErrCode::PARAM_ERROR;
67     }
68     currentData = mergeData;
69     return ERR_OK;
70 }
71 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)72 ErrCode AllowedBluetoothDevicesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
73     int32_t userId)
74 {
75     EDMLOGI("AllowedBluetoothDevicesPlugin OnGetPolicy policyData : %{public}s, userId : %{public}d",
76         policyData.c_str(), userId);
77     std::vector<std::string> deviceIds;
78     pluginInstance_->serializer_->Deserialize(policyData, deviceIds);
79     reply.WriteInt32(ERR_OK);
80     reply.WriteInt32(deviceIds.size());
81     reply.WriteStringVector(deviceIds);
82     return ERR_OK;
83 }
84 
OnRemovePolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,int32_t userId)85 ErrCode AllowedBluetoothDevicesPlugin::OnRemovePolicy(std::vector<std::string> &data,
86     std::vector<std::string> &currentData, int32_t userId)
87 {
88     EDMLOGD("AllowedBluetoothDevicesPlugin OnRemovePolicy userId : %{public}d:", userId);
89     if (data.empty()) {
90         EDMLOGW("AllowedBluetoothDevicesPlugin OnRemovePolicy data is empty.");
91         return ERR_OK;
92     }
93     if (data.size() > EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE) {
94         EDMLOGE("AllowedBluetoothDevicesPlugin OnRemovePolicy input data is too large.");
95         return EdmReturnErrCode::PARAM_ERROR;
96     }
97     std::vector<std::string> mergeData =
98         ArrayStringSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
99     currentData = mergeData;
100     return ERR_OK;
101 }
102 
OnChangedPolicyDone(bool isGlobalChanged)103 void AllowedBluetoothDevicesPlugin::OnChangedPolicyDone(bool isGlobalChanged)
104 {
105     if (!isGlobalChanged) {
106         return;
107     }
108     NotifyBluetoothDevicesChanged();
109 }
110 
NotifyBluetoothDevicesChanged()111 void AllowedBluetoothDevicesPlugin::NotifyBluetoothDevicesChanged()
112 {
113     EDMLOGD("AllowedBluetoothDevicesPlugin NotifyBluetoothDevicesChanged.");
114     AAFwk::Want want;
115     want.SetAction(BLUETOOTH_WHITELIST_CHANGED_EVENT);
116     EventFwk::CommonEventData eventData;
117     eventData.SetWant(want);
118     if (!EventFwk::CommonEventManager::PublishCommonEvent(eventData)) {
119         EDMLOGE("NotifyBluetoothDevicesChanged failed.");
120     }
121 }
122 
123 } // namespace EDM
124 } // namespace OHOS