• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "bluetooth_manager_proxy.h"
17 
18 #include "edm_log.h"
19 #include "enterprise_device_mgr_proxy.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 std::shared_ptr<BluetoothManagerProxy> BluetoothManagerProxy::instance_ = nullptr;
24 std::once_flag BluetoothManagerProxy::flag_;
25 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
26 
GetBluetoothManagerProxy()27 std::shared_ptr<BluetoothManagerProxy> BluetoothManagerProxy::GetBluetoothManagerProxy()
28 {
29     std::call_once(flag_, []() {
30         if (instance_ == nullptr) {
31             instance_ = std::make_shared<BluetoothManagerProxy>();
32         }
33     });
34     return instance_;
35 }
36 
GetBluetoothInfo(MessageParcel & data,BluetoothInfo & bluetoothInfo)37 int32_t BluetoothManagerProxy::GetBluetoothInfo(MessageParcel &data, BluetoothInfo &bluetoothInfo)
38 {
39     EDMLOGD("BluetoothManagerProxy::GetBluetoothInfo");
40     MessageParcel reply;
41     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::GET_BLUETOOTH_INFO, data, reply);
42     int32_t ret = ERR_INVALID_VALUE;
43     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
44     if (!blRes) {
45         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
46         return ret;
47     }
48     reply.ReadString(bluetoothInfo.name);
49     reply.ReadInt32(bluetoothInfo.state);
50     reply.ReadInt32(bluetoothInfo.connectionState);
51     return ERR_OK;
52 }
53 
SetBluetoothDisabled(MessageParcel & data)54 int32_t BluetoothManagerProxy::SetBluetoothDisabled(MessageParcel &data)
55 {
56     EDMLOGD("BluetoothManagerProxy::SetBluetoothDisabled");
57     return EnterpriseDeviceMgrProxy::GetInstance()->SetPolicyDisabled(data, EdmInterfaceCode::DISABLE_BLUETOOTH);
58 }
59 
IsBluetoothDisabled(MessageParcel & data,bool & result)60 int32_t BluetoothManagerProxy::IsBluetoothDisabled(MessageParcel &data, bool &result)
61 {
62     EDMLOGD("BluetoothManagerProxy::IsBluetoothDisabled");
63     return EnterpriseDeviceMgrProxy::GetInstance()->IsPolicyDisabled(data, EdmInterfaceCode::DISABLE_BLUETOOTH, result);
64 }
65 
GetAllowedBluetoothDevices(const AppExecFwk::ElementName * admin,std::vector<std::string> & deviceIds)66 int32_t BluetoothManagerProxy::GetAllowedBluetoothDevices(const AppExecFwk::ElementName *admin,
67     std::vector<std::string> &deviceIds)
68 {
69     EDMLOGD("BluetoothManagerProxy::GetAllowedBluetoothDevices");
70     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
71     MessageParcel data;
72     MessageParcel reply;
73     data.WriteInterfaceToken(DESCRIPTOR);
74     data.WriteInt32(WITHOUT_USERID);
75     data.WriteString(WITHOUT_PERMISSION_TAG);
76     if (admin != nullptr) {
77         data.WriteInt32(HAS_ADMIN);
78         data.WriteParcelable(admin);
79     } else {
80         if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
81             return ERR_OK;
82         }
83         data.WriteInt32(WITHOUT_ADMIN);
84     }
85     proxy->GetPolicy(EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES, data, reply);
86     int32_t ret = ERR_INVALID_VALUE;
87     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
88     if (!blRes) {
89         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
90         return ret;
91     }
92     reply.ReadStringVector(&deviceIds);
93     return ERR_OK;
94 }
95 
GetAllowedBluetoothDevices(std::vector<std::string> & deviceIds)96 int32_t BluetoothManagerProxy::GetAllowedBluetoothDevices(std::vector<std::string> &deviceIds)
97 {
98     EDMLOGD("BluetoothManagerProxy::GetAllowedBluetoothDevices");
99     return GetBluetoothDevices(deviceIds, EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES);
100 }
101 
GetDisallowedBluetoothDevices(std::vector<std::string> & deviceIds)102 int32_t BluetoothManagerProxy::GetDisallowedBluetoothDevices(std::vector<std::string> &deviceIds)
103 {
104     EDMLOGD("BluetoothManagerProxy::GetDisallowedBluetoothDevices");
105     return GetBluetoothDevices(deviceIds, EdmInterfaceCode::DISALLOWED_BLUETOOTH_DEVICES);
106 }
107 
GetBluetoothDevices(std::vector<std::string> & deviceIds,EdmInterfaceCode policyCode)108 int32_t BluetoothManagerProxy::GetBluetoothDevices(std::vector<std::string> &deviceIds, EdmInterfaceCode policyCode)
109 {
110     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
111     MessageParcel data;
112     MessageParcel reply;
113     data.WriteInterfaceToken(DESCRIPTOR);
114     data.WriteInt32(WITHOUT_USERID);
115     data.WriteString(WITHOUT_PERMISSION_TAG);
116     if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
117         return ERR_OK;
118     }
119     data.WriteInt32(WITHOUT_ADMIN);
120     proxy->GetPolicy(policyCode, data, reply);
121     int32_t ret = ERR_INVALID_VALUE;
122     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
123     if (!blRes) {
124         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
125         return ret;
126     }
127     reply.ReadStringVector(&deviceIds);
128     return ERR_OK;
129 }
130 
GetBluetoothDevices(MessageParcel & data,std::vector<std::string> & deviceIds,EdmInterfaceCode policyCode)131 int32_t BluetoothManagerProxy::GetBluetoothDevices(MessageParcel &data, std::vector<std::string> &deviceIds,
132     EdmInterfaceCode policyCode)
133 {
134     EDMLOGD("BluetoothManagerProxy::GetBluetoothDevices");
135     if (EnterpriseDeviceMgrProxy::GetInstance()->CheckDataInEdmDisabled(data)) {
136         return ERR_OK;
137     }
138     MessageParcel reply;
139     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(policyCode, data, reply);
140     int32_t ret = ERR_INVALID_VALUE;
141     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
142     if (!blRes) {
143         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
144         return ret;
145     }
146     reply.ReadStringVector(&deviceIds);
147     return ERR_OK;
148 }
149 
AddOrRemoveBluetoothDevices(MessageParcel & data,FuncOperateType operateType,EdmInterfaceCode policyCode)150 int32_t BluetoothManagerProxy::AddOrRemoveBluetoothDevices(MessageParcel &data, FuncOperateType operateType,
151     EdmInterfaceCode policyCode)
152 {
153     EDMLOGD("BluetoothManagerProxy::AddOrRemoveBluetoothDevices");
154     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)operateType, policyCode);
155     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
156 }
157 
TurnOnOrOffBluetooth(MessageParcel & data)158 int32_t BluetoothManagerProxy::TurnOnOrOffBluetooth(MessageParcel &data)
159 {
160     EDMLOGD("BluetoothManagerProxy::TurnOnOrOffBluetooth");
161     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SWITCH_BLUETOOTH);
162     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
163 }
164 
AddOrRemoveDisallowedBluetoothProtocols(MessageParcel & data,bool isAdd)165 int32_t BluetoothManagerProxy::AddOrRemoveDisallowedBluetoothProtocols(MessageParcel &data, bool isAdd)
166 {
167     EDMLOGD("BluetoothManagerProxy::AddOrRemoveDisallowedBluetoothProtocols");
168     FuncOperateType operateType = isAdd ? FuncOperateType::SET : FuncOperateType::REMOVE;
169     std::uint32_t funcCode =
170         POLICY_FUNC_CODE((std::uint32_t)operateType, EdmInterfaceCode::DISALLOWED_BLUETOOTH_PROTOCOLS);
171     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
172 }
GetDisallowedBluetoothProtocols(MessageParcel & data,std::vector<int32_t> & protocols)173 int32_t BluetoothManagerProxy::GetDisallowedBluetoothProtocols(MessageParcel &data, std::vector<int32_t> &protocols)
174 {
175     EDMLOGD("BluetoothManagerProxy::GetDisallowedBluetoothProtocols");
176     MessageParcel reply;
177     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::DISALLOWED_BLUETOOTH_PROTOCOLS, data, reply);
178     int32_t ret = ERR_INVALID_VALUE;
179     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
180     if (!blRes) {
181         EDMLOGE("BluetoothManagerProxy:GetDisallowedBluetoothProtocols fail. %{public}d", ret);
182         return ret;
183     }
184     if (!reply.ReadInt32Vector(&protocols)) {
185         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
186     }
187     return ERR_OK;
188 }
189 } // namespace EDM
190 } // namespace OHOS