• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     pimpl->clientProxy_ = proxy;
68 }
69 
~GattManager()70 GattManager::~GattManager()
71 {}
72 
GetDevicesByStates(const std::array<int,GATT_CONNECTION_STATE_NUM> & states)73 std::vector<BluetoothRemoteDevice> GattManager::GetDevicesByStates(
74     const std::array<int, GATT_CONNECTION_STATE_NUM> &states)
75 {
76     std::vector<BluetoothRemoteDevice> result;
77     std::set<int> stateSet;
78     if (!IS_BLE_ENABLED()) {
79         HILOGE("bluetooth is off.");
80         return result;
81     }
82     sptr<BluetoothGattClientProxy> proxy =
83         GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT);
84     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, result, "failed: no proxy");
85 
86     for (auto &state : states) {
87         stateSet.emplace(state);
88     }
89     if (pimpl->clientProxy_ != nullptr) {
90         std::vector<BluetoothGattDevice> devices;
91         pimpl->clientProxy_->GetAllDevice(devices);
92         for (auto &item : devices) {
93             if (stateSet.find(item.connectState_) != stateSet.end()) {
94                 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(),
95                     item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR)));
96             }
97         }
98     }
99     return result;
100 }
101 
GetConnectedDevices()102 std::vector<BluetoothRemoteDevice> GattManager::GetConnectedDevices()
103 {
104     std::vector<BluetoothRemoteDevice> result;
105     if (!IS_BLE_ENABLED()) {
106         HILOGE("bluetooth is off.");
107         return result;
108     }
109 
110     sptr<BluetoothGattClientProxy> proxy =
111         GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT);
112     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, result, "failed: no proxy");
113 
114     if (pimpl->clientProxy_ != nullptr) {
115         std::vector<BluetoothGattDevice> device;
116         pimpl->clientProxy_->GetAllDevice(device);
117         for (auto &item : device) {
118             if (item.connectState_ == static_cast<int>(BTConnectState::CONNECTED)) {
119                 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(),
120                     item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR)));
121             }
122         }
123     }
124     return result;
125 }
126 }  // namespace Bluetooth
127 }  // namespace OHOS
128