195489c19Sopenharmony_ci/* 295489c19Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 495489c19Sopenharmony_ci * you may not use this file except in compliance with the License. 595489c19Sopenharmony_ci * You may obtain a copy of the License at 695489c19Sopenharmony_ci * 795489c19Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 895489c19Sopenharmony_ci * 995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and 1395489c19Sopenharmony_ci * limitations under the License. 1495489c19Sopenharmony_ci */ 1595489c19Sopenharmony_ci#include <set> 1695489c19Sopenharmony_ci#include <vector> 1795489c19Sopenharmony_ci 1895489c19Sopenharmony_ci#include <functional> 1995489c19Sopenharmony_ci#include "__tree" 2095489c19Sopenharmony_ci#include "array" 2195489c19Sopenharmony_ci 2295489c19Sopenharmony_ci#include "bluetooth_def.h" 2395489c19Sopenharmony_ci#include "bluetooth_gatt_client_proxy.h" 2495489c19Sopenharmony_ci#include "bluetooth_gatt_device.h" 2595489c19Sopenharmony_ci#include "bluetooth_gatt_manager.h" 2695489c19Sopenharmony_ci#include "bluetooth_host.h" 2795489c19Sopenharmony_ci#include "bluetooth_host_proxy.h" 2895489c19Sopenharmony_ci#include "bluetooth_log.h" 2995489c19Sopenharmony_ci#include "bluetooth_remote_device.h" 3095489c19Sopenharmony_ci#include "bluetooth_utils.h" 3195489c19Sopenharmony_ci#include "i_bluetooth_host.h" 3295489c19Sopenharmony_ci#include "if_system_ability_manager.h" 3395489c19Sopenharmony_ci#include "iremote_object.h" 3495489c19Sopenharmony_ci#include "iservice_registry.h" 3595489c19Sopenharmony_ci#include "memory" 3695489c19Sopenharmony_ci#include "new" 3795489c19Sopenharmony_ci#include "raw_address.h" 3895489c19Sopenharmony_ci#include "refbase.h" 3995489c19Sopenharmony_ci#include "system_ability_definition.h" 4095489c19Sopenharmony_ci#include "bluetooth_profile_manager.h" 4195489c19Sopenharmony_ci 4295489c19Sopenharmony_cinamespace OHOS { 4395489c19Sopenharmony_cinamespace Bluetooth { 4495489c19Sopenharmony_cistruct GattManager::impl { 4595489c19Sopenharmony_cipublic: 4695489c19Sopenharmony_ci impl() 4795489c19Sopenharmony_ci { 4895489c19Sopenharmony_ci sptr<BluetoothGattClientProxy> proxy = 4995489c19Sopenharmony_ci GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT); 5095489c19Sopenharmony_ci CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy"); 5195489c19Sopenharmony_ci }; 5295489c19Sopenharmony_ci ~impl() {}; 5395489c19Sopenharmony_ci bool InitGattManagerProxy(void); 5495489c19Sopenharmony_ci 5595489c19Sopenharmony_ci sptr<BluetoothGattClientProxy> clientProxy_; 5695489c19Sopenharmony_ci}; 5795489c19Sopenharmony_ci 5895489c19Sopenharmony_ciGattManager::GattManager() : pimpl(new GattManager::impl()) 5995489c19Sopenharmony_ci{ 6095489c19Sopenharmony_ci sptr<BluetoothGattClientProxy> proxy = 6195489c19Sopenharmony_ci GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT); 6295489c19Sopenharmony_ci CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy"); 6395489c19Sopenharmony_ci} 6495489c19Sopenharmony_ci 6595489c19Sopenharmony_ciGattManager::~GattManager() 6695489c19Sopenharmony_ci{} 6795489c19Sopenharmony_ci 6895489c19Sopenharmony_cistd::vector<BluetoothRemoteDevice> GattManager::GetDevicesByStates( 6995489c19Sopenharmony_ci const std::array<int, GATT_CONNECTION_STATE_NUM> &states) 7095489c19Sopenharmony_ci{ 7195489c19Sopenharmony_ci std::vector<BluetoothRemoteDevice> result; 7295489c19Sopenharmony_ci std::set<int> stateSet; 7395489c19Sopenharmony_ci if (!IS_BLE_ENABLED()) { 7495489c19Sopenharmony_ci HILOGE("bluetooth is off."); 7595489c19Sopenharmony_ci return result; 7695489c19Sopenharmony_ci } 7795489c19Sopenharmony_ci sptr<BluetoothGattClientProxy> proxy = 7895489c19Sopenharmony_ci GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT); 7995489c19Sopenharmony_ci CHECK_AND_RETURN_LOG_RET(proxy != nullptr, result, "failed: no proxy"); 8095489c19Sopenharmony_ci 8195489c19Sopenharmony_ci for (auto &state : states) { 8295489c19Sopenharmony_ci stateSet.emplace(state); 8395489c19Sopenharmony_ci } 8495489c19Sopenharmony_ci if (pimpl->clientProxy_ != nullptr) { 8595489c19Sopenharmony_ci std::vector<BluetoothGattDevice> devices; 8695489c19Sopenharmony_ci pimpl->clientProxy_->GetAllDevice(devices); 8795489c19Sopenharmony_ci for (auto &item : devices) { 8895489c19Sopenharmony_ci if (stateSet.find(item.connectState_) != stateSet.end()) { 8995489c19Sopenharmony_ci result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(), 9095489c19Sopenharmony_ci item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR))); 9195489c19Sopenharmony_ci } 9295489c19Sopenharmony_ci } 9395489c19Sopenharmony_ci } 9495489c19Sopenharmony_ci return result; 9595489c19Sopenharmony_ci} 9695489c19Sopenharmony_ci 9795489c19Sopenharmony_cistd::vector<BluetoothRemoteDevice> GattManager::GetConnectedDevices() 9895489c19Sopenharmony_ci{ 9995489c19Sopenharmony_ci std::vector<BluetoothRemoteDevice> result; 10095489c19Sopenharmony_ci if (!IS_BLE_ENABLED()) { 10195489c19Sopenharmony_ci HILOGE("bluetooth is off."); 10295489c19Sopenharmony_ci return result; 10395489c19Sopenharmony_ci } 10495489c19Sopenharmony_ci 10595489c19Sopenharmony_ci sptr<BluetoothGattClientProxy> proxy = 10695489c19Sopenharmony_ci GetRemoteProxy<BluetoothGattClientProxy>(PROFILE_GATT_CLIENT); 10795489c19Sopenharmony_ci CHECK_AND_RETURN_LOG_RET(proxy != nullptr, result, "failed: no proxy"); 10895489c19Sopenharmony_ci 10995489c19Sopenharmony_ci if (pimpl->clientProxy_ != nullptr) { 11095489c19Sopenharmony_ci std::vector<BluetoothGattDevice> device; 11195489c19Sopenharmony_ci pimpl->clientProxy_->GetAllDevice(device); 11295489c19Sopenharmony_ci for (auto &item : device) { 11395489c19Sopenharmony_ci if (item.connectState_ == static_cast<int>(BTConnectState::CONNECTED)) { 11495489c19Sopenharmony_ci result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(), 11595489c19Sopenharmony_ci item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR))); 11695489c19Sopenharmony_ci } 11795489c19Sopenharmony_ci } 11895489c19Sopenharmony_ci } 11995489c19Sopenharmony_ci return result; 12095489c19Sopenharmony_ci} 12195489c19Sopenharmony_ci} // namespace Bluetooth 12295489c19Sopenharmony_ci} // namespace OHOS 123