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
16 #include "disallowed_bluetooth_protocols_plugin.h"
17
18 #include "array_int_serializer.h"
19 #include "bluetooth_config_utils.h"
20 #include "bt_protocol_utils.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "edm_constants.h"
24 #include "edm_errors.h"
25 #include "edm_ipc_interface_code.h"
26 #include "edm_os_account_manager_impl.h"
27 #include "func_code_utils.h"
28 #include "parameters.h"
29 #include "iplugin_manager.h"
30
31 namespace OHOS {
32 namespace EDM {
33 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedBluetoothProtocolsPlugin::GetPlugin());
34
InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedBluetoothProtocolsPlugin,std::vector<int32_t>>> ptr)35 void DisallowedBluetoothProtocolsPlugin::InitPlugin(
36 std::shared_ptr<IPluginTemplate<DisallowedBluetoothProtocolsPlugin, std::vector<int32_t>>> ptr)
37 {
38 EDMLOGI("DisallowedBluetoothProtocolsPlugin InitPlugin...");
39 ptr->InitAttribute(EdmInterfaceCode::DISALLOWED_BLUETOOTH_PROTOCOLS,
40 PolicyName::POLICY_DISALLOWED_BLUETOOTH_PROTOCOLS, EdmPermission::PERMISSION_ENTERPRISE_MANAGE_BLUETOOTH,
41 IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
42 ptr->SetSerializer(ArrayIntSerializer::GetInstance());
43 ptr->SetOnHandlePolicyListener(&DisallowedBluetoothProtocolsPlugin::OnSetPolicy, FuncOperateType::SET);
44 ptr->SetOnHandlePolicyDoneListener(&DisallowedBluetoothProtocolsPlugin::OnChangedPolicyDone, FuncOperateType::SET);
45 ptr->SetOnHandlePolicyListener(&DisallowedBluetoothProtocolsPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
46 ptr->SetOnHandlePolicyDoneListener(&DisallowedBluetoothProtocolsPlugin::OnChangedPolicyDone,
47 FuncOperateType::REMOVE);
48 ptr->SetOnAdminRemoveListener(&DisallowedBluetoothProtocolsPlugin::OnAdminRemove);
49 }
50
OnSetPolicy(std::vector<int32_t> & data,std::vector<int32_t> & currentData,std::vector<int32_t> & mergeData,int32_t userId)51 ErrCode DisallowedBluetoothProtocolsPlugin::OnSetPolicy(std::vector<int32_t> &data, std::vector<int32_t> ¤tData,
52 std::vector<int32_t> &mergeData, int32_t userId)
53 {
54 EDMLOGI("DisallowedBluetoothProtocolsPlugin OnSetPolicy");
55 if (data.empty()) {
56 EDMLOGE("DisallowedBluetoothProtocolsPlugin OnSetPolicy data is empty");
57 return EdmReturnErrCode::PARAM_ERROR;
58 }
59 if (data.size() > EdmConstants::DEFAULT_LOOP_MAX_SIZE) {
60 EDMLOGE("DisallowedBluetoothProtocolsPlugin OnSetPolicy size is over limit");
61 return EdmReturnErrCode::PARAM_ERROR;
62 }
63 bool isExist = false;
64 ErrCode ret = std::make_shared<EdmOsAccountManagerImpl>()->IsOsAccountExists(userId, isExist);
65 if (FAILED(ret) || !isExist) {
66 EDMLOGE("DisallowedBluetoothProtocolsPlugin OnSetPolicy userId is not exist");
67 return EdmReturnErrCode::PARAM_ERROR;
68 }
69 std::vector<int32_t> afterHandle = ArrayIntSerializer::GetInstance()->SetUnionPolicyData(data, currentData);
70 std::vector<int32_t> afterMerge = ArrayIntSerializer::GetInstance()->SetUnionPolicyData(mergeData, afterHandle);
71 BluetoothConfigUtils bluetoothConfigUtils;
72 for (size_t i = 0; i < afterMerge.size(); ++i) {
73 std::string protocol;
74 if (!BtProtocolUtils::IntToProtocolStr(afterMerge[i], protocol)) {
75 return EdmReturnErrCode::PARAM_ERROR;
76 }
77 if (!bluetoothConfigUtils.UpdateProtocol(std::to_string(userId), protocol, true)) {
78 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
79 }
80 }
81 currentData = afterHandle;
82 mergeData = afterMerge;
83 return ERR_OK;
84 }
85
OnRemovePolicy(std::vector<int32_t> & data,std::vector<int32_t> & currentData,std::vector<int32_t> & mergeData,int32_t userId)86 ErrCode DisallowedBluetoothProtocolsPlugin::OnRemovePolicy(std::vector<int32_t> &data,
87 std::vector<int32_t> ¤tData, std::vector<int32_t> &mergeData, int32_t userId)
88 {
89 EDMLOGI("DisallowedBluetoothProtocolsPlugin OnRemovePolicy");
90 if (data.empty()) {
91 EDMLOGE("DisallowedBluetoothProtocolsPlugin OnRemovePolicy data is empty");
92 return EdmReturnErrCode::PARAM_ERROR;
93 }
94 if (data.size() > EdmConstants::DEFAULT_LOOP_MAX_SIZE) {
95 EDMLOGE("DisallowedBluetoothProtocolsPlugin OnRemovePolicy size is over limit");
96 return EdmReturnErrCode::PARAM_ERROR;
97 }
98 std::vector<int32_t> needRemovePolicy =
99 ArrayIntSerializer::GetInstance()->SetIntersectionPolicyData(data, currentData);
100 std::vector<int32_t> afterHandle =
101 ArrayIntSerializer::GetInstance()->SetDifferencePolicyData(needRemovePolicy, currentData);
102 std::vector<int32_t> afterMerge = ArrayIntSerializer::GetInstance()->SetUnionPolicyData(mergeData, afterHandle);
103 BluetoothConfigUtils bluetoothConfigUtils;
104 if (!bluetoothConfigUtils.RemoveUserIdItem(std::to_string(userId))) {
105 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
106 }
107 for (size_t i = 0; i < afterMerge.size(); ++i) {
108 std::string protocol;
109 if (!BtProtocolUtils::IntToProtocolStr(afterMerge[i], protocol)) {
110 return EdmReturnErrCode::PARAM_ERROR;
111 }
112 if (!bluetoothConfigUtils.UpdateProtocol(std::to_string(userId), protocol, true)) {
113 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
114 }
115 }
116 currentData = afterHandle;
117 mergeData = afterMerge;
118 return ERR_OK;
119 }
120
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)121 ErrCode DisallowedBluetoothProtocolsPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data,
122 MessageParcel &reply, int32_t userId)
123 {
124 EDMLOGI("DisallowedBluetoothProtocolsPlugin OnGetPolicy");
125 std::vector<int32_t> protocols;
126 BluetoothConfigUtils bluetoothConfigUtils;
127 if (!bluetoothConfigUtils.QueryProtocols(std::to_string(userId), protocols)) {
128 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
129 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
130 }
131 reply.WriteInt32(ERR_OK);
132 reply.WriteInt32Vector(protocols);
133 return ERR_OK;
134 }
135
OnAdminRemove(const std::string & adminName,std::vector<int32_t> & policyData,std::vector<int32_t> & mergeData,int32_t userId)136 ErrCode DisallowedBluetoothProtocolsPlugin::OnAdminRemove(const std::string &adminName,
137 std::vector<int32_t> &policyData, std::vector<int32_t> &mergeData, int32_t userId)
138 {
139 EDMLOGI("DisallowedBluetoothProtocolsPlugin OnAdminRemove");
140 BluetoothConfigUtils bluetoothConfigUtils;
141 if (!bluetoothConfigUtils.RemoveUserIdItem(std::to_string(userId))) {
142 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
143 }
144 for (size_t i = 0; i < mergeData.size(); ++i) {
145 std::string protocol;
146 if (!BtProtocolUtils::IntToProtocolStr(mergeData[i], protocol)) {
147 return EdmReturnErrCode::PARAM_ERROR;
148 }
149 if (!bluetoothConfigUtils.UpdateProtocol(std::to_string(userId), protocol, true)) {
150 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
151 }
152 }
153 NotifyBluetoothProtocolsChanged();
154 return ERR_OK;
155 }
156
OnChangedPolicyDone(bool isGlobalChanged)157 void DisallowedBluetoothProtocolsPlugin::OnChangedPolicyDone(bool isGlobalChanged)
158 {
159 if (!isGlobalChanged) {
160 return;
161 }
162 NotifyBluetoothProtocolsChanged();
163 }
164
NotifyBluetoothProtocolsChanged()165 void DisallowedBluetoothProtocolsPlugin::NotifyBluetoothProtocolsChanged()
166 {
167 EDMLOGI("DisallowedBluetoothProtocolsPlugin NotifyBluetoothProtocolsChanged.");
168 AAFwk::Want want;
169 want.SetAction(EdmConstants::EDM_CONFIG_CHANGED_EVENT);
170 EventFwk::CommonEventData eventData;
171 eventData.SetWant(want);
172 int32_t bluetoothUid = 1002;
173 EventFwk::CommonEventPublishInfo eventInfo;
174 std::vector<int32_t> subscriberUids;
175 subscriberUids.push_back(bluetoothUid);
176 eventInfo.SetSubscriberUid(subscriberUids);
177 if (!EventFwk::CommonEventManager::PublishCommonEvent(eventData, eventInfo)) {
178 EDMLOGE("NotifyBluetoothProtocolsChanged failed.");
179 }
180 }
181 } // namespace EDM
182 } // namespace OHOS