• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
16 #include <list>
17 #include <mutex>
18 #include <string>
19 #include "bluetooth_map_mse_observer_stub.h"
20 #include "bluetooth_map_mse_proxy.h"
21 #include "bluetooth_map_mse.h"
22 #include "bluetooth_remote_device.h"
23 #include "bluetooth_host.h"
24 #include "bluetooth_profile_manager.h"
25 #include "bluetooth_utils.h"
26 #include "bluetooth_observer_list.h"
27 #include "iservice_registry.h"
28 #include "raw_address.h"
29 #include "system_ability_definition.h"
30 #include "bluetooth_host_proxy.h"
31 #include "bluetooth_log.h"
32 
33 namespace OHOS {
34 namespace Bluetooth {
35 class BluetoothMapMseObserverImp : public BluetoothMapMseObserverStub {
36 public:
BluetoothMapMseObserverImp(BluetoothObserverList<MapMseObserver> & observers)37     explicit BluetoothMapMseObserverImp(BluetoothObserverList<MapMseObserver> &observers)
38         : observers_(observers)
39     {}
~BluetoothMapMseObserverImp()40     ~BluetoothMapMseObserverImp() override
41     {}
42 
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)43     void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
44     {
45         HILOGI("enter, device: %{public}s, state: %{public}s",
46             GetEncryptAddr((device).GetAddress()).c_str(), GetProfileConnStateName(state).c_str());
47         observers_.ForEach([device, state](std::shared_ptr<MapMseObserver> observer) {
48             BluetoothRemoteDevice dev(device.GetAddress(), 0);
49             observer->OnConnectionStateChanged(dev, state);
50         });
51     }
52 
53 private:
54     BluetoothObserverList<MapMseObserver> &observers_;
55     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothMapMseObserverImp);
56 };
57 
58 struct MapMse::impl {
59     impl();
60     ~impl();
61     void RegisterObserver(std::shared_ptr<MapMseObserver> &observer);
62     void DeregisterObserver(std::shared_ptr<MapMseObserver> &observer);
63     int32_t profileRegisterId = 0;
64 private:
65     BluetoothObserverList<MapMseObserver> observers_;
66     sptr<BluetoothMapMseObserverImp> serviceObserverImp_ = nullptr;
67 };
68 
impl()69 MapMse::impl::impl()
70 {
71     serviceObserverImp_ = new (std::nothrow) BluetoothMapMseObserverImp(observers_);
72     CHECK_AND_RETURN_LOG(serviceObserverImp_ != nullptr, "serviceObserverImp_ is nullptr");
73     profileRegisterId = DelayedSingleton<BluetoothProfileManager>::GetInstance()->RegisterFunc(PROFILE_MAP_MSE,
74         [this](sptr<IRemoteObject> remote) {
75         sptr<IBluetoothMapMse> proxy = iface_cast<IBluetoothMapMse>(remote);
76         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
77         proxy->RegisterObserver(serviceObserverImp_);
78     });
79 };
80 
~impl()81 MapMse::impl::~impl()
82 {
83     HILOGD("enter");
84     DelayedSingleton<BluetoothProfileManager>::GetInstance()->DeregisterFunc(profileRegisterId);
85     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
86     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
87     proxy->DeregisterObserver(serviceObserverImp_);
88 }
89 
RegisterObserver(std::shared_ptr<MapMseObserver> & observer)90 void MapMse::impl::RegisterObserver(std::shared_ptr<MapMseObserver> &observer)
91 {
92     HILOGI("enter");
93     if (observer) {
94         observers_.Register(observer);
95     }
96 }
97 
DeregisterObserver(std::shared_ptr<MapMseObserver> & observer)98 void MapMse::impl::DeregisterObserver(std::shared_ptr<MapMseObserver> &observer)
99 {
100     HILOGI("enter");
101     if (observer) {
102         observers_.Deregister(observer);
103     }
104 }
105 
GetProfile()106 MapMse *MapMse::GetProfile()
107 {
108     static MapMse instance;
109     return &instance;
110 }
111 
MapMse()112 MapMse::MapMse()
113 {
114     pimpl = std::make_unique<impl>();
115 }
116 
~MapMse()117 MapMse::~MapMse()
118 {
119     HILOGI("enter");
120 }
121 
RegisterObserver(std::shared_ptr<MapMseObserver> observer)122 void MapMse::RegisterObserver(std::shared_ptr<MapMseObserver> observer)
123 {
124     HILOGI("enter");
125     pimpl->RegisterObserver(observer);
126 }
127 
DeregisterObserver(std::shared_ptr<MapMseObserver> observer)128 void MapMse::DeregisterObserver(std::shared_ptr<MapMseObserver> observer)
129 {
130     HILOGI("enter");
131     pimpl->DeregisterObserver(observer);
132 }
133 
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state) const134 int32_t MapMse::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const
135 {
136     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
137     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
138     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
139     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
140     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
141 
142     return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
143 }
144 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRemoteDevice> & result) const145 int32_t MapMse::GetDevicesByStates(const std::vector<int32_t> &states, std::vector<BluetoothRemoteDevice> &result) const
146 {
147     HILOGI("enter");
148     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
149     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
150     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
151 
152     std::vector<BluetoothRawAddress> rawAddress {};
153     int32_t ret = proxy->GetDevicesByStates(states, rawAddress);
154     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "inner error");
155 
156     for (BluetoothRawAddress rawAddr : rawAddress) {
157         BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
158         result.push_back(device);
159     }
160     return BT_NO_ERROR;
161 }
162 
Disconnect(const BluetoothRemoteDevice & device)163 int32_t MapMse::Disconnect(const BluetoothRemoteDevice &device)
164 {
165     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
166     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
167     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
168     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
169     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
170 
171     return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
172 }
173 
SetConnectionStrategy(const BluetoothRemoteDevice & device,int32_t strategy)174 int32_t MapMse::SetConnectionStrategy(const BluetoothRemoteDevice &device, int32_t strategy)
175 {
176     HILOGI("device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
177     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
178     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
179     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
180     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
181     CHECK_AND_RETURN_LOG_RET(CheckConnectionStrategyInvalid(strategy), BT_ERR_INVALID_PARAM, "strategy param error");
182 
183     return proxy->SetConnectionStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
184 }
185 
GetConnectionStrategy(const BluetoothRemoteDevice & device,int32_t & strategy) const186 int32_t MapMse::GetConnectionStrategy(const BluetoothRemoteDevice &device, int32_t &strategy) const
187 {
188     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
189     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
190     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
191     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
192     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
193 
194     return proxy->GetConnectionStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
195 }
196 
SetMessageAccessAuthorization(const BluetoothRemoteDevice & device,int32_t accessAuthorization)197 int32_t MapMse::SetMessageAccessAuthorization(const BluetoothRemoteDevice &device, int32_t accessAuthorization)
198 {
199     HILOGI("device: %{public}s, accessAuthorization: %{public}d", GET_ENCRYPT_ADDR(device), accessAuthorization);
200     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
201     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
202     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
203     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
204     CHECK_AND_RETURN_LOG_RET(CheckAccessAuthorizationInvalid(accessAuthorization),
205         BT_ERR_INVALID_PARAM, "metaData param error");
206 
207     return proxy->SetMessageAccessAuthorization(
208         BluetoothRawAddress(device.GetDeviceAddr()), accessAuthorization);
209 }
210 
GetMessageAccessAuthorization(const BluetoothRemoteDevice & device,int32_t & accessAuthorization) const211 int32_t MapMse::GetMessageAccessAuthorization(const BluetoothRemoteDevice &device, int32_t &accessAuthorization) const
212 {
213     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
214     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
215     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
216     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
217     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
218 
219     return proxy->GetMessageAccessAuthorization(
220         BluetoothRawAddress(device.GetDeviceAddr()), accessAuthorization);
221 }
222 
223 } // namespace Bluetooth
224 } // namespace OHOS