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