/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include "bluetooth_map_mse_observer_stub.h" #include "bluetooth_map_mse_proxy.h" #include "bluetooth_raw_address.h" #include "bluetooth_map_mse.h" #include "bt_def.h" #include "bluetooth_host.h" #include "bluetooth_load_system_ability.h" #include "bluetooth_utils.h" #include "bluetooth_remote_device.h" #include "bluetooth_observer_list.h" #include "iservice_registry.h" #include "system_ability_definition.h" #include "i_bluetooth_host.h" namespace OHOS { namespace Bluetooth { class MseServiceObserver : public BluetoothMapMseObserverStub { public: MseServiceObserver(BluetoothObserverList *observers) : observers_(observers) { HILOGI("enter"); } ~MseServiceObserver() override = default; void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override { BluetoothRemoteDevice remoteDevice(device.GetAddress(), bluetooth::BTTransport::ADAPTER_BREDR); observers_->ForEach([remoteDevice, state](std::shared_ptr observer) { observer->OnConnectionStateChanged(remoteDevice, state); }); } void OnPermission(const BluetoothRawAddress &device) override { BluetoothRemoteDevice remoteDevice(device.GetAddress(), bluetooth::BTTransport::ADAPTER_BREDR); observers_->ForEach([remoteDevice](std::shared_ptr observer) { observer->OnPermission(remoteDevice); }); } private: BluetoothObserverList *observers_; }; struct MapServer::impl { impl(); ~impl() { if (proxy_ != nullptr) { proxy_->DeregisterObserver(serviceObserver_); proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_); } } bool InitMapServerProxy(void); std::mutex mutex_; sptr proxy_; class BluetoothMapMseDeathRecipient; sptr deathRecipient_ = nullptr; BluetoothObserverList observers_; sptr serviceObserver_ = new MseServiceObserver(&observers_); }; class MapServer::impl::BluetoothMapMseDeathRecipient final : public IRemoteObject::DeathRecipient { public: explicit BluetoothMapMseDeathRecipient(MapServer::impl &MapMse) : MapMse_(MapMse) {}; ~BluetoothMapMseDeathRecipient() final = default; BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothMapMseDeathRecipient); void OnRemoteDied(const wptr &remote) final { HILOGI("starts"); if (!MapMse_.proxy_) { return; } MapMse_.proxy_->DeregisterObserver(MapMse_.serviceObserver_); MapMse_.proxy_->AsObject()->RemoveDeathRecipient(MapMse_.deathRecipient_); MapMse_.proxy_ = nullptr; } private: MapServer::impl &MapMse_; }; MapServer::impl::impl() { if (proxy_) { return; } BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_MAP_MSE); if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) { BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility(); return; } InitMapServerProxy(); } MapServer::MapServer() : pimpl(nullptr) { HILOGI("excute"); pimpl = std::make_unique(); } bool MapServer::impl::InitMapServerProxy(void) { if (proxy_) { return true; } proxy_ = GetRemoteProxy(PROFILE_MAP_MSE); if (!proxy_) { HILOGE("get MapServer proxy_ failed"); return false; } if (serviceObserver_ != nullptr) { proxy_->RegisterObserver(serviceObserver_); } deathRecipient_ = new BluetoothMapMseDeathRecipient(*this); if (deathRecipient_ != nullptr) { proxy_->AsObject()->AddDeathRecipient(deathRecipient_); } return true; } MapServer::~MapServer() {} MapServer *MapServer::GetProfile() { static MapServer instance; return &instance; } void MapServer::Init() { if (!pimpl) { HILOGE("fails: no pimpl"); return; } if (!pimpl->InitMapServerProxy()) { HILOGE("MapServer proxy_ is nullptr"); return; } } void MapServer::RegisterObserver(MapServerObserver &observer) { HILOGI("enter"); std::shared_ptr pointer(&observer, [](MapServerObserver *) {}); pimpl->observers_.Register(pointer); } void MapServer::DeregisterObserver(MapServerObserver &observer) { HILOGI("enter"); std::shared_ptr pointer(&observer, [](MapServerObserver *) {}); pimpl->observers_.Deregister(pointer); } int MapServer::GetState() const { HILOGI("enter"); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return RET_BAD_STATUS; } int32_t ret = RET_NO_ERROR; pimpl->proxy_->GetState(ret); return ret; } bool MapServer::Disconnect(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return false; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return false; } if (!device.IsValidBluetoothRemoteDevice()) { HILOGE("BluetoothRemoteDevice error"); return false; } int32_t ret = RET_NO_ERROR; BluetoothRawAddress rawAddress(device.GetDeviceAddr()); pimpl->proxy_->Disconnect(rawAddress, ret); return ret == RET_NO_ERROR; } bool MapServer::IsConnected(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return false; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return false; } if (!device.IsValidBluetoothRemoteDevice()) { HILOGE("BluetoothRemoteDevice error"); return false; } bool ret = false; BluetoothRawAddress rawAddress(device.GetDeviceAddr()); pimpl->proxy_->IsConnected(rawAddress, ret); return ret; } std::vector MapServer::GetConnectedDevices() const { HILOGI("enter"); std::vector btDeviceList; if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return btDeviceList; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return btDeviceList; } std::vector btDevice; pimpl->proxy_->GetConnectedDevices(btDevice); for (auto it = btDevice.begin(); it != btDevice.end(); it++) { btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0)); } return btDeviceList; } std::vector MapServer::GetDevicesByStates(std::vector states) const { HILOGI("enter"); std::vector btDeviceList; if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return btDeviceList; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return btDeviceList; } std::vector btDevice; pimpl->proxy_->GetDevicesByStates(states, btDevice); for (auto it = btDevice.begin(); it != btDevice.end(); it++) { btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0)); } return btDeviceList; } int MapServer::GetConnectionState(const BluetoothRemoteDevice &device) const { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return static_cast(BTConnectState::DISCONNECTED); } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return static_cast(BTConnectState::DISCONNECTED); } if (!device.IsValidBluetoothRemoteDevice()) { HILOGE("BluetoothRemoteDevice error"); return static_cast(BTConnectState::DISCONNECTED); } int32_t ret = RET_NO_ERROR; BluetoothRawAddress rawAddress(device.GetDeviceAddr()); pimpl->proxy_->GetConnectionState(rawAddress, ret); return ret; } bool MapServer::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy) { HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return false; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return false; } if (!device.IsValidBluetoothRemoteDevice()) { HILOGE("BluetoothRemoteDevice error"); return false; } bool ret = false; BluetoothRawAddress rawAddress(device.GetDeviceAddr()); pimpl->proxy_->SetConnectionStrategy(rawAddress, strategy, ret); return ret; } int MapServer::GetConnectionStrategy(const BluetoothRemoteDevice &device) const { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return static_cast(BTStrategyType::CONNECTION_FORBIDDEN); } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return static_cast(BTStrategyType::CONNECTION_FORBIDDEN); } if (!device.IsValidBluetoothRemoteDevice()) { HILOGE("BluetoothRemoteDevice error"); return static_cast(BTStrategyType::CONNECTION_FORBIDDEN); } int32_t ret = static_cast(BTStrategyType::CONNECTION_FORBIDDEN); BluetoothRawAddress rawAddress(device.GetDeviceAddr()); pimpl->proxy_->GetConnectionStrategy(rawAddress, ret); return ret; } void MapServer::GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save) { HILOGI("enter, device: %{public}s, allow: %{public}d, save: %{public}d", GET_ENCRYPT_ADDR(device), allow, save); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return; } if (pimpl == nullptr || !pimpl->InitMapServerProxy()) { HILOGE("pimpl or mapServer proxy_ is nullptr"); return; } if (!device.IsValidBluetoothRemoteDevice()) { HILOGE("BluetoothRemoteDevice error"); return; } BluetoothRawAddress rawAddress(device.GetDeviceAddr()); pimpl->proxy_->GrantPermission(rawAddress, allow, save); } } // namespace Bluetooth } // namespace OHOS