1 /*
2 * Copyright (c) 2023-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
16 #include "bluetooth_manager_proxy.h"
17
18 #include "edm_log.h"
19 #include "enterprise_device_mgr_proxy.h"
20 #include "func_code.h"
21
22 namespace OHOS {
23 namespace EDM {
24 std::shared_ptr<BluetoothManagerProxy> BluetoothManagerProxy::instance_ = nullptr;
25 std::once_flag BluetoothManagerProxy::flag_;
26 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
27
GetBluetoothManagerProxy()28 std::shared_ptr<BluetoothManagerProxy> BluetoothManagerProxy::GetBluetoothManagerProxy()
29 {
30 std::call_once(flag_, []() {
31 if (instance_ == nullptr) {
32 instance_ = std::make_shared<BluetoothManagerProxy>();
33 }
34 });
35 return instance_;
36 }
37
GetBluetoothInfo(MessageParcel & data,BluetoothInfo & bluetoothInfo)38 int32_t BluetoothManagerProxy::GetBluetoothInfo(MessageParcel &data, BluetoothInfo &bluetoothInfo)
39 {
40 EDMLOGD("BluetoothManagerProxy::GetBluetoothInfo");
41 MessageParcel reply;
42 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::GET_BLUETOOTH_INFO, data, reply);
43 int32_t ret = ERR_INVALID_VALUE;
44 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
45 if (!blRes) {
46 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
47 return ret;
48 }
49 reply.ReadString(bluetoothInfo.name);
50 reply.ReadInt32(bluetoothInfo.state);
51 reply.ReadInt32(bluetoothInfo.connectionState);
52 return ERR_OK;
53 }
54
SetBluetoothDisabled(MessageParcel & data)55 int32_t BluetoothManagerProxy::SetBluetoothDisabled(MessageParcel &data)
56 {
57 EDMLOGD("BluetoothManagerProxy::SetBluetoothDisabled");
58 return EnterpriseDeviceMgrProxy::GetInstance()->SetPolicyDisabled(data, EdmInterfaceCode::DISABLE_BLUETOOTH);
59 }
60
IsBluetoothDisabled(MessageParcel & data,bool & result)61 int32_t BluetoothManagerProxy::IsBluetoothDisabled(MessageParcel &data, bool &result)
62 {
63 EDMLOGD("BluetoothManagerProxy::IsBluetoothDisabled");
64 return EnterpriseDeviceMgrProxy::GetInstance()->IsPolicyDisabled(data, EdmInterfaceCode::DISABLE_BLUETOOTH, result);
65 }
66
GetAllowedBluetoothDevices(const AppExecFwk::ElementName * admin,std::vector<std::string> & deviceIds)67 int32_t BluetoothManagerProxy::GetAllowedBluetoothDevices(const AppExecFwk::ElementName *admin,
68 std::vector<std::string> &deviceIds)
69 {
70 EDMLOGD("BluetoothManagerProxy::GetAllowedBluetoothDevices");
71 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
72 MessageParcel data;
73 MessageParcel reply;
74 data.WriteInterfaceToken(DESCRIPTOR);
75 data.WriteInt32(WITHOUT_USERID);
76 data.WriteString(WITHOUT_PERMISSION_TAG);
77 if (admin != nullptr) {
78 data.WriteInt32(HAS_ADMIN);
79 data.WriteParcelable(admin);
80 } else {
81 if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
82 return ERR_OK;
83 }
84 data.WriteInt32(WITHOUT_ADMIN);
85 }
86 proxy->GetPolicy(EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES, data, reply);
87 int32_t ret = ERR_INVALID_VALUE;
88 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
89 if (!blRes) {
90 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
91 return ret;
92 }
93 reply.ReadStringVector(&deviceIds);
94 return ERR_OK;
95 }
96
GetAllowedBluetoothDevices(MessageParcel & data,std::vector<std::string> & deviceIds)97 int32_t BluetoothManagerProxy::GetAllowedBluetoothDevices(MessageParcel &data, std::vector<std::string> &deviceIds)
98 {
99 EDMLOGD("BluetoothManagerProxy::GetAllowedBluetoothDevices");
100 if (EnterpriseDeviceMgrProxy::GetInstance()->CheckDataInEdmDisabled(data)) {
101 return ERR_OK;
102 }
103 MessageParcel reply;
104 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES, data, reply);
105 int32_t ret = ERR_INVALID_VALUE;
106 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
107 if (!blRes) {
108 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
109 return ret;
110 }
111 reply.ReadStringVector(&deviceIds);
112 return ERR_OK;
113 }
114
AddOrRemoveAllowedBluetoothDevices(MessageParcel & data,bool isAdd)115 int32_t BluetoothManagerProxy::AddOrRemoveAllowedBluetoothDevices(MessageParcel &data, bool isAdd)
116 {
117 EDMLOGD("BluetoothManagerProxy::AddOrRemoveAllowedBluetoothDevices");
118 FuncOperateType operateType = isAdd ? FuncOperateType::SET : FuncOperateType::REMOVE;
119 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)operateType, EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES);
120 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
121 }
122 } // namespace EDM
123 } // namespace OHOS