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