• 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_pbap_pse_observer_stub.h"
20 #include "bluetooth_pbap_pse_proxy.h"
21 #include "bluetooth_pbap_pse.h"
22 #include "bluetooth_remote_device.h"
23 #include "bluetooth_host.h"
24 #include "bluetooth_utils.h"
25 #include "bluetooth_observer_list.h"
26 #include "iservice_registry.h"
27 #include "raw_address.h"
28 #include "system_ability_definition.h"
29 #include "bluetooth_host_proxy.h"
30 #include "bluetooth_log.h"
31 #include "bluetooth_profile_manager.h"
32 
33 namespace OHOS {
34 namespace Bluetooth {
35 enum class BTShareType : int32_t {
36     SHARE_NAME_AND_PHONE_NUMBER = 0,
37     SHARE_ALL = 1,
38     SHARE_NOTHING = 2,
39 };
40 
41 class BluetoothPbapPseObserverImp : public BluetoothPbapPseObserverStub {
42 public:
BluetoothPbapPseObserverImp(BluetoothObserverList<PbapPseObserver> & observers)43     explicit BluetoothPbapPseObserverImp(BluetoothObserverList<PbapPseObserver> &observers)
44         : observers_(observers)
45     {}
~BluetoothPbapPseObserverImp()46     ~BluetoothPbapPseObserverImp() override
47     {}
48 
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)49     void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
50     {
51         observers_.ForEach([device, state](std::shared_ptr<PbapPseObserver> observer) {
52             BluetoothRemoteDevice dev(device.GetAddress(), 0);
53             observer->OnConnectionStateChanged(dev, state);
54         });
55     }
56 
57 private:
58     BluetoothObserverList<PbapPseObserver> &observers_;
59     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothPbapPseObserverImp);
60 };
61 
62 struct PbapPse::impl {
63     impl();
64     ~impl();
65     void RegisterObserver(std::shared_ptr<PbapPseObserver> &observer);
66     void DeregisterObserver(std::shared_ptr<PbapPseObserver> &observer);
67     std::mutex pbapPseProxyMutex_;
68     int32_t profileRegisterId = 0;
69 private:
70     BluetoothObserverList<PbapPseObserver> observers_;
71     sptr<BluetoothPbapPseObserverImp> serviceObserverImp_ = nullptr;
72 };
73 
impl()74 PbapPse::impl::impl()
75 {
76     serviceObserverImp_ = new BluetoothPbapPseObserverImp(observers_);
77     profileRegisterId = DelayedSingleton<BluetoothProfileManager>::GetInstance()->RegisterFunc(PROFILE_PBAP_PSE,
78         [this](sptr<IRemoteObject> remote) {
79         sptr<IBluetoothPbapPse> proxy = iface_cast<IBluetoothPbapPse>(remote);
80         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
81         proxy->RegisterObserver(serviceObserverImp_);
82     });
83 }
84 
~impl()85 PbapPse::impl::~impl()
86 {
87     HILOGI("enter");
88     DelayedSingleton<BluetoothProfileManager>::GetInstance()->DeregisterFunc(profileRegisterId);
89     sptr<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
90     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
91     proxy->DeregisterObserver(serviceObserverImp_);
92 }
93 
RegisterObserver(std::shared_ptr<PbapPseObserver> & observer)94 void PbapPse::impl::RegisterObserver(std::shared_ptr<PbapPseObserver> &observer)
95 {
96     HILOGI("enter");
97     if (observer) {
98         observers_.Register(observer);
99     }
100 }
101 
DeregisterObserver(std::shared_ptr<PbapPseObserver> & observer)102 void PbapPse::impl::DeregisterObserver(std::shared_ptr<PbapPseObserver> &observer)
103 {
104     HILOGI("enter");
105     if (observer) {
106         observers_.Deregister(observer);
107     }
108 }
109 
GetProfile()110 PbapPse *PbapPse::GetProfile()
111 {
112     static PbapPse instance;
113     return &instance;
114 }
115 
PbapPse()116 PbapPse::PbapPse()
117 {
118     pimpl = std::make_unique<impl>();
119 }
120 
~PbapPse()121 PbapPse::~PbapPse()
122 {
123     HILOGI("enter");
124 }
125 
RegisterObserver(std::shared_ptr<PbapPseObserver> observer)126 void PbapPse::RegisterObserver(std::shared_ptr<PbapPseObserver> observer)
127 {
128     HILOGI("enter");
129     pimpl->RegisterObserver(observer);
130 }
131 
DeregisterObserver(std::shared_ptr<PbapPseObserver> observer)132 void PbapPse::DeregisterObserver(std::shared_ptr<PbapPseObserver> observer)
133 {
134     HILOGI("enter");
135     pimpl->DeregisterObserver(observer);
136 }
137 
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state) const138 int32_t PbapPse::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const
139 {
140     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
141     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
142     sptr<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
143     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
144     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
145 
146     return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
147 }
148 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRemoteDevice> & result) const149 int32_t PbapPse::GetDevicesByStates(const std::vector<int32_t> &states,
150     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<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
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 PbapPse::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<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
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 PbapPse::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<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
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 PbapPse::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<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
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 
CheckShareTypeInvalid(int32_t shareType)202 bool CheckShareTypeInvalid(int32_t shareType)
203 {
204     if (shareType == static_cast<int32_t>(BTShareType::SHARE_NAME_AND_PHONE_NUMBER) ||
205         shareType == static_cast<int32_t>(BTShareType::SHARE_ALL) ||
206         shareType == static_cast<int32_t>(BTShareType::SHARE_NOTHING)) {
207         return true;
208     }
209     return false;
210 }
211 
SetShareType(const BluetoothRemoteDevice & device,int32_t shareType)212 int32_t PbapPse::SetShareType(const BluetoothRemoteDevice &device, int32_t shareType)
213 {
214     HILOGI("device: %{public}s, shareType: %{public}d", GET_ENCRYPT_ADDR(device), shareType);
215     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
216     sptr<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
217     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
218     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
219     CHECK_AND_RETURN_LOG_RET(CheckShareTypeInvalid(shareType), BT_ERR_INVALID_PARAM, "shareType param error");
220 
221     return proxy->SetShareType(BluetoothRawAddress(device.GetDeviceAddr()), shareType);
222 }
223 
GetShareType(const BluetoothRemoteDevice & device,int32_t & shareType) const224 int32_t PbapPse::GetShareType(const BluetoothRemoteDevice &device, int32_t &shareType) const
225 {
226     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
227     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
228     sptr<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
229     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
230     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
231 
232     return proxy->GetShareType(BluetoothRawAddress(device.GetDeviceAddr()), shareType);
233 }
234 
SetPhoneBookAccessAuthorization(const BluetoothRemoteDevice & device,int32_t accessAuthorization)235 int32_t PbapPse::SetPhoneBookAccessAuthorization(const BluetoothRemoteDevice &device, int32_t accessAuthorization)
236 {
237     HILOGI("device: %{public}s, accessAuthorization: %{public}d",
238         GET_ENCRYPT_ADDR(device), accessAuthorization);
239     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
240     sptr<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
241     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
242     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
243     CHECK_AND_RETURN_LOG_RET(CheckAccessAuthorizationInvalid(accessAuthorization),
244         BT_ERR_INVALID_PARAM, "accessAuthorization param error");
245 
246     return proxy->SetPhoneBookAccessAuthorization(BluetoothRawAddress(device.GetDeviceAddr()),
247         accessAuthorization);
248 }
249 
GetPhoneBookAccessAuthorization(const BluetoothRemoteDevice & device,int32_t & accessAuthorization) const250 int32_t PbapPse::GetPhoneBookAccessAuthorization(const BluetoothRemoteDevice &device,
251     int32_t &accessAuthorization) const
252 {
253     HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
254     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
255     sptr<IBluetoothPbapPse> proxy = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
256     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
257     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
258 
259     return proxy->GetPhoneBookAccessAuthorization(BluetoothRawAddress(device.GetDeviceAddr()),
260         accessAuthorization);
261 }
262 
263 } // namespace Bluetooth
264 } // namespace OHOS