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 41 namespace OHOS { 42 namespace Bluetooth { 43 struct GattManager::impl { 44 public: implOHOS::Bluetooth::GattManager::impl45 impl() 46 { 47 if (!clientProxy_) { 48 HILOGE("GattManager proxy_ failed"); 49 return; 50 } 51 }; ~implOHOS::Bluetooth::GattManager::impl52 ~impl() {}; 53 bool InitGattManagerProxy(void); 54 55 sptr<BluetoothGattClientProxy> clientProxy_; 56 }; 57 GattManager()58GattManager::GattManager() : pimpl(new GattManager::impl()) 59 {} 60 ~GattManager()61GattManager::~GattManager() 62 {} 63 InitGattManagerProxy(void)64bool GattManager::impl::InitGattManagerProxy(void) 65 { 66 if (clientProxy_) { 67 return true; 68 } 69 clientProxy_ = GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT); 70 if (!clientProxy_) { 71 HILOGE("get gatt manager proxy failed"); 72 return false; 73 } 74 return true; 75 } 76 GetDevicesByStates(const std::array<int,GATT_CONNECTION_STATE_NUM> & states)77std::vector<BluetoothRemoteDevice> GattManager::GetDevicesByStates( 78 const std::array<int, GATT_CONNECTION_STATE_NUM> &states) 79 { 80 std::vector<BluetoothRemoteDevice> result; 81 std::set<int> stateSet; 82 if (!IS_BLE_ENABLED()) { 83 HILOGE("bluetooth is off."); 84 return result; 85 } 86 87 if (pimpl == nullptr || !pimpl->InitGattManagerProxy()) { 88 HILOGE("pimpl or gattManager proxy is nullptr"); 89 return result; 90 } 91 92 for (auto &state : states) { 93 stateSet.emplace(state); 94 } 95 if (pimpl->clientProxy_ != nullptr) { 96 std::vector<BluetoothGattDevice> devices; 97 pimpl->clientProxy_->GetAllDevice(devices); 98 for (auto &item : devices) { 99 if (stateSet.find(item.connectState_) != stateSet.end()) { 100 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(), 101 item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR))); 102 } 103 } 104 } 105 return result; 106 } 107 GetConnectedDevices()108std::vector<BluetoothRemoteDevice> GattManager::GetConnectedDevices() 109 { 110 std::vector<BluetoothRemoteDevice> result; 111 if (!IS_BLE_ENABLED()) { 112 HILOGE("bluetooth is off."); 113 return result; 114 } 115 116 if (pimpl == nullptr || !pimpl->InitGattManagerProxy()) { 117 HILOGE("pimpl or gattManager proxy is nullptr"); 118 return result; 119 } 120 121 if (pimpl->clientProxy_ != nullptr) { 122 std::vector<BluetoothGattDevice> device; 123 pimpl->clientProxy_->GetAllDevice(device); 124 for (auto &item : device) { 125 if (item.connectState_ == static_cast<int>(BTConnectState::CONNECTED)) { 126 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(), 127 item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR))); 128 } 129 } 130 } 131 return result; 132 } 133 } // namespace Bluetooth 134 } // namespace OHOS 135