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_constants.h"
19 #include "edm_log.h"
20 #include "enterprise_device_mgr_proxy.h"
21 #include "func_code.h"
22 
23 namespace OHOS {
24 namespace EDM {
25 std::shared_ptr<BluetoothManagerProxy> BluetoothManagerProxy::instance_ = nullptr;
26 std::mutex BluetoothManagerProxy::mutexLock_;
27 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
28 
GetBluetoothManagerProxy()29 std::shared_ptr<BluetoothManagerProxy> BluetoothManagerProxy::GetBluetoothManagerProxy()
30 {
31     if (instance_ == nullptr) {
32         std::lock_guard<std::mutex> lock(mutexLock_);
33         if (instance_ == nullptr) {
34             std::shared_ptr<BluetoothManagerProxy> temp = std::make_shared<BluetoothManagerProxy>();
35             instance_ = temp;
36         }
37     }
38     return instance_;
39 }
40 
GetBluetoothInfo(const AppExecFwk::ElementName & admin,BluetoothInfo & bluetoothInfo)41 int32_t BluetoothManagerProxy::GetBluetoothInfo(const AppExecFwk::ElementName &admin, BluetoothInfo &bluetoothInfo)
42 {
43     EDMLOGD("BluetoothManagerProxy::GetBluetoothInfo");
44     MessageParcel data;
45     MessageParcel reply;
46     data.WriteInterfaceToken(DESCRIPTOR);
47     data.WriteInt32(WITHOUT_USERID);
48     data.WriteString(WITHOUT_PERMISSION_TAG);
49     data.WriteInt32(HAS_ADMIN);
50     data.WriteParcelable(&admin);
51     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::GET_BLUETOOTH_INFO, data, reply);
52     int32_t ret = ERR_INVALID_VALUE;
53     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
54     if (!blRes) {
55         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
56         return ret;
57     }
58     reply.ReadString(bluetoothInfo.name);
59     reply.ReadInt32(bluetoothInfo.state);
60     reply.ReadInt32(bluetoothInfo.connectionState);
61     return ERR_OK;
62 }
63 
SetBluetoothDisabled(const AppExecFwk::ElementName & admin,bool disabled)64 int32_t BluetoothManagerProxy::SetBluetoothDisabled(const AppExecFwk::ElementName &admin, bool disabled)
65 {
66     EDMLOGD("BluetoothManagerProxy::SetBluetoothDisabled");
67     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
68     return proxy->SetPolicyDisabled(admin, disabled, EdmInterfaceCode::DISABLE_BLUETOOTH,
69         EdmConstants::PERMISSION_TAG_VERSION_11);
70 }
71 
IsBluetoothDisabled(const AppExecFwk::ElementName * admin,bool & result)72 int32_t BluetoothManagerProxy::IsBluetoothDisabled(const AppExecFwk::ElementName *admin, bool &result)
73 {
74     EDMLOGD("BluetoothManagerProxy::IsBluetoothDisabled");
75     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
76     return proxy->IsPolicyDisabled(admin, EdmInterfaceCode::DISABLE_BLUETOOTH, result,
77         EdmConstants::PERMISSION_TAG_VERSION_11);
78 }
79 
AddAllowedBluetoothDevices(const AppExecFwk::ElementName & admin,const std::vector<std::string> & deviceIds)80 int32_t BluetoothManagerProxy::AddAllowedBluetoothDevices(const AppExecFwk::ElementName &admin,
81     const std::vector<std::string> &deviceIds)
82 {
83     return AddOrRemoveAllowedBluetoothDevices(admin, deviceIds, "AddAllowedBluetoothDevices");
84 }
85 
GetAllowedBluetoothDevices(const AppExecFwk::ElementName * admin,std::vector<std::string> & deviceIds)86 int32_t BluetoothManagerProxy::GetAllowedBluetoothDevices(const AppExecFwk::ElementName *admin,
87     std::vector<std::string> &deviceIds)
88 {
89     EDMLOGD("BluetoothManagerProxy::GetAllowedBluetoothDevices");
90     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
91     MessageParcel data;
92     MessageParcel reply;
93     data.WriteInterfaceToken(DESCRIPTOR);
94     data.WriteInt32(WITHOUT_USERID);
95     data.WriteString(WITHOUT_PERMISSION_TAG);
96     if (admin != nullptr) {
97         data.WriteInt32(HAS_ADMIN);
98         data.WriteParcelable(admin);
99     } else {
100         if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
101             return ERR_OK;
102         }
103         data.WriteInt32(WITHOUT_ADMIN);
104     }
105     proxy->GetPolicy(EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES, data, reply);
106     int32_t ret = ERR_INVALID_VALUE;
107     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
108     if (!blRes) {
109         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
110         return ret;
111     }
112     int32_t size = reply.ReadInt32();
113     if (size > EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE) {
114         EDMLOGE("BluetoothManagerProxy:GetAllowedBluetoothDevices size=[%{public}d] is too large.", size);
115         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
116     }
117     reply.ReadStringVector(&deviceIds);
118     return ERR_OK;
119 }
120 
RemoveAllowedBluetoothDevices(const AppExecFwk::ElementName & admin,const std::vector<std::string> & deviceIds)121 int32_t BluetoothManagerProxy::RemoveAllowedBluetoothDevices(const AppExecFwk::ElementName &admin,
122     const std::vector<std::string> &deviceIds)
123 {
124     return AddOrRemoveAllowedBluetoothDevices(admin, deviceIds, "RemoveAllowedBluetoothDevices");
125 }
126 
AddOrRemoveAllowedBluetoothDevices(const AppExecFwk::ElementName & admin,const std::vector<std::string> & deviceIds,std::string function)127 int32_t BluetoothManagerProxy::AddOrRemoveAllowedBluetoothDevices(const AppExecFwk::ElementName &admin,
128     const std::vector<std::string> &deviceIds, std::string function)
129 {
130     EDMLOGD("BluetoothManagerProxy::%{public}s", function.c_str());
131     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
132     MessageParcel data;
133     std::uint32_t funcCode = 0;
134     if (function == "AddAllowedBluetoothDevices") {
135         funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES);
136     } else {
137         funcCode =
138             POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES);
139     }
140     data.WriteInterfaceToken(DESCRIPTOR);
141     data.WriteInt32(WITHOUT_USERID);
142     data.WriteParcelable(&admin);
143     data.WriteString(WITHOUT_PERMISSION_TAG);
144     data.WriteStringVector(deviceIds);
145     return proxy->HandleDevicePolicy(funcCode, data);
146 }
147 
148 } // namespace EDM
149 } // namespace OHOS