1 /*
2 * Copyright (C) 2021 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 <set>
16 #include <vector>
17
18 #include <functional>
19 #include "__tree"
20 #include "array"
21
22 #include "bluetooth_def.h"
23 #include "bluetooth_gatt_client_proxy.h"
24 #include "bluetooth_gatt_device.h"
25 #include "bluetooth_gatt_manager.h"
26 #include "bluetooth_host.h"
27 #include "bluetooth_host_proxy.h"
28 #include "bluetooth_log.h"
29 #include "bluetooth_remote_device.h"
30 #include "bluetooth_utils.h"
31 #include "i_bluetooth_host.h"
32 #include "if_system_ability_manager.h"
33 #include "iremote_object.h"
34 #include "iservice_registry.h"
35 #include "memory"
36 #include "new"
37 #include "raw_address.h"
38 #include "refbase.h"
39 #include "system_ability_definition.h"
40 #include "bluetooth_profile_manager.h"
41
42 namespace OHOS {
43 namespace Bluetooth {
44 struct GattManager::impl {
45 public:
implOHOS::Bluetooth::GattManager::impl46 impl()
47 {
48 sptr<BluetoothGattClientProxy> proxy =
49 GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT);
50 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
51 };
~implOHOS::Bluetooth::GattManager::impl52 ~impl() {};
53 bool InitGattManagerProxy(void);
54
55 sptr<BluetoothGattClientProxy> clientProxy_;
56 };
57
GattManager()58 GattManager::GattManager() : pimpl(new GattManager::impl())
59 {
60 sptr<BluetoothGattClientProxy> proxy =
61 GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT);
62 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
63 }
64
~GattManager()65 GattManager::~GattManager()
66 {}
67
GetDevicesByStates( const std::array<int, GATT_CONNECTION_STATE_NUM> &states)68 std::vector<BluetoothRemoteDevice> GattManager::GetDevicesByStates(
69 const std::array<int, GATT_CONNECTION_STATE_NUM> &states)
70 {
71 std::vector<BluetoothRemoteDevice> result;
72 std::set<int> stateSet;
73 if (!IS_BLE_ENABLED()) {
74 HILOGE("bluetooth is off.");
75 return result;
76 }
77 sptr<BluetoothGattClientProxy> proxy =
78 GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT);
79 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, result, "failed: no proxy");
80
81 for (auto &state : states) {
82 stateSet.emplace(state);
83 }
84 if (pimpl->clientProxy_ != nullptr) {
85 std::vector<BluetoothGattDevice> devices;
86 pimpl->clientProxy_->GetAllDevice(devices);
87 for (auto &item : devices) {
88 if (stateSet.find(item.connectState_) != stateSet.end()) {
89 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(),
90 item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR)));
91 }
92 }
93 }
94 return result;
95 }
96
GetConnectedDevices()97 std::vector<BluetoothRemoteDevice> GattManager::GetConnectedDevices()
98 {
99 std::vector<BluetoothRemoteDevice> result;
100 if (!IS_BLE_ENABLED()) {
101 HILOGE("bluetooth is off.");
102 return result;
103 }
104
105 sptr<BluetoothGattClientProxy> proxy =
106 GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT);
107 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, result, "failed: no proxy");
108
109 if (pimpl->clientProxy_ != nullptr) {
110 std::vector<BluetoothGattDevice> device;
111 pimpl->clientProxy_->GetAllDevice(device);
112 for (auto &item : device) {
113 if (item.connectState_ == static_cast<int>(BTConnectState::CONNECTED)) {
114 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(),
115 item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR)));
116 }
117 }
118 }
119 return result;
120 }
121 } // namespace Bluetooth
122 } // namespace OHOS
123