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     {}
40     ~BluetoothMapMseObserverImp() override
41     {}
42 
43     void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
44     {
45         HILOGI("enter, device: %{public}s, state: %{public}s, cause: %{public}d",
46             GET_ENCRYPT_RAW_ADDR(device), GetProfileConnStateName(state).c_str(), cause);
47         observers_.ForEach([device, state, cause](std::shared_ptr<MapMseObserver> observer) {
48             BluetoothRemoteDevice dev(device.GetAddress(), 0);
49             observer->OnConnectionStateChanged(dev, state, cause);
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 = 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     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 #ifdef DTFUZZ_TEST
109     static BluetoothNoDestructor<MapMse> instance;
110     return instance.get();
111 #else
112     static MapMse instance;
113     return &instance;
114 #endif
115 }
116 
MapMse()117 MapMse::MapMse()
118 {
119     pimpl = std::make_unique<impl>();
120 }
121 
~MapMse()122 MapMse::~MapMse()
123 {
124     HILOGI("enter");
125 }
126 
RegisterObserver(std::shared_ptr<MapMseObserver> observer)127 void MapMse::RegisterObserver(std::shared_ptr<MapMseObserver> observer)
128 {
129     HILOGI("enter");
130     pimpl->RegisterObserver(observer);
131 }
132 
DeregisterObserver(std::shared_ptr<MapMseObserver> observer)133 void MapMse::DeregisterObserver(std::shared_ptr<MapMseObserver> observer)
134 {
135     HILOGI("enter");
136     pimpl->DeregisterObserver(observer);
137 }
138 
GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const139 int32_t MapMse::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const
140 {
141     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
142     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
143     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
144     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
145     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
146 
147     return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
148 }
149 
GetDevicesByStates(const std::vector<int32_t> &states, std::vector<BluetoothRemoteDevice> &result) const150 int32_t MapMse::GetDevicesByStates(const std::vector<int32_t> &states, std::vector<BluetoothRemoteDevice> &result) const
151 {
152     HILOGI("enter");
153     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
154     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
155     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
156 
157     std::vector<BluetoothRawAddress> rawAddress {};
158     int32_t ret = proxy->GetDevicesByStates(states, rawAddress);
159     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "inner error");
160 
161     for (BluetoothRawAddress rawAddr : rawAddress) {
162         BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
163         result.push_back(device);
164     }
165     return BT_NO_ERROR;
166 }
167 
Disconnect(const BluetoothRemoteDevice &device)168 int32_t MapMse::Disconnect(const BluetoothRemoteDevice &device)
169 {
170     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
171     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
172     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
173     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
174     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
175 
176     return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
177 }
178 
SetConnectionStrategy(const BluetoothRemoteDevice &device, int32_t strategy)179 int32_t MapMse::SetConnectionStrategy(const BluetoothRemoteDevice &device, int32_t strategy)
180 {
181     HILOGI("device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
182     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
183     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
184     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
185     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
186     CHECK_AND_RETURN_LOG_RET(CheckConnectionStrategyInvalid(strategy), BT_ERR_INVALID_PARAM, "strategy param error");
187 
188     return proxy->SetConnectionStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
189 }
190 
GetConnectionStrategy(const BluetoothRemoteDevice &device, int32_t &strategy) const191 int32_t MapMse::GetConnectionStrategy(const BluetoothRemoteDevice &device, int32_t &strategy) const
192 {
193     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
194     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
195     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
196     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
197     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
198 
199     return proxy->GetConnectionStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
200 }
201 
SetMessageAccessAuthorization(const BluetoothRemoteDevice &device, int32_t accessAuthorization)202 int32_t MapMse::SetMessageAccessAuthorization(const BluetoothRemoteDevice &device, int32_t accessAuthorization)
203 {
204     HILOGI("device: %{public}s, accessAuthorization: %{public}d", GET_ENCRYPT_ADDR(device), accessAuthorization);
205     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
206     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
207     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
208     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
209     CHECK_AND_RETURN_LOG_RET(CheckAccessAuthorizationInvalid(accessAuthorization),
210         BT_ERR_INVALID_PARAM, "metaData param error");
211 
212     return proxy->SetMessageAccessAuthorization(
213         BluetoothRawAddress(device.GetDeviceAddr()), accessAuthorization);
214 }
215 
GetMessageAccessAuthorization(const BluetoothRemoteDevice &device, int32_t &accessAuthorization) const216 int32_t MapMse::GetMessageAccessAuthorization(const BluetoothRemoteDevice &device, int32_t &accessAuthorization) const
217 {
218     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
219     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
220     sptr<IBluetoothMapMse> proxy = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
221     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
222     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
223 
224     return proxy->GetMessageAccessAuthorization(
225         BluetoothRawAddress(device.GetDeviceAddr()), accessAuthorization);
226 }
227 
228 } // namespace Bluetooth
229 } // namespace OHOS