• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "bluetooth_ble_advertiser_server.h"
17 
18 #include "bluetooth_ble_central_manager_server.h"
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_log.h"
21 #include "interface_adapter_ble.h"
22 #include "interface_adapter_manager.h"
23 #include "ipc_skeleton.h"
24 #include "remote_observer_list.h"
25 #include "permission_utils.h"
26 #include "safe_map.h"
27 
28 #define ADVERTISE_FAILED_TOO_MANY_ADVERTISERS 0x02
29 
30 namespace OHOS {
31 namespace Bluetooth {
32 using namespace OHOS::bluetooth;
33 class BleAdvertiserCallback : public IBleAdvertiserCallback {
34 public:
35     BleAdvertiserCallback() = default;
36     ~BleAdvertiserCallback() override = default;
37 
OnStartResultEvent(int result,uint8_t advHandle,int opcode)38     void OnStartResultEvent(int result, uint8_t advHandle, int opcode) override
39     {
40         HILOGI("result: %{public}d, advHandle: %{public}d, opcode: %{public}d", result, advHandle, opcode);
41 
42         observers_->ForEach([this, result, advHandle, opcode](IBluetoothBleAdvertiseCallback *observer) {
43             int32_t pid = observersPid_.ReadVal(observer->AsObject());
44             if (BluetoothBleCentralManagerServer::IsResourceScheduleControlApp(pid)) {
45                 HILOGD("pid:%{public}d is proxy pid, not callback.", pid);
46                 return;
47             }
48             observer->OnStartResultEvent(result, advHandle, opcode);
49         });
50     }
51 
OnEnableResultEvent(int result,uint8_t advHandle)52     void OnEnableResultEvent(int result, uint8_t advHandle) override
53     {
54         HILOGI("result: %{public}d, advHandle: %{public}d", result, advHandle);
55         observers_->ForEach([this, result, advHandle](IBluetoothBleAdvertiseCallback *observer) {
56             int32_t pid = observersPid_.ReadVal(observer->AsObject());
57             if (BluetoothBleCentralManagerServer::IsResourceScheduleControlApp(pid)) {
58                 HILOGD("pid:%{public}d is proxy pid, not callback.", pid);
59                 return;
60             }
61             observer->OnEnableResultEvent(result, advHandle);
62         });
63     }
64 
OnDisableResultEvent(int result,uint8_t advHandle)65     void OnDisableResultEvent(int result, uint8_t advHandle) override
66     {
67         HILOGI("result: %{public}d, advHandle: %{public}d", result, advHandle);
68         observers_->ForEach([this, result, advHandle](IBluetoothBleAdvertiseCallback *observer) {
69             int32_t pid = observersPid_.ReadVal(observer->AsObject());
70             if (BluetoothBleCentralManagerServer::IsResourceScheduleControlApp(pid)) {
71                 HILOGD("pid:%{public}d is proxy pid, not callback.", pid);
72                 return;
73             }
74             observer->OnDisableResultEvent(result, advHandle);
75         });
76     }
77 
OnStopResultEvent(int result,uint8_t advHandle)78     void OnStopResultEvent(int result, uint8_t advHandle) override
79     {
80         HILOGI("result: %{public}d, advHandle: %{public}d", result, advHandle);
81 
82         observers_->ForEach([this, result, advHandle](IBluetoothBleAdvertiseCallback *observer) {
83             int32_t pid = observersPid_.ReadVal(observer->AsObject());
84             if (BluetoothBleCentralManagerServer::IsResourceScheduleControlApp(pid)) {
85                 HILOGD("pid:%{public}d is proxy pid, not callback.", pid);
86                 return;
87             }
88             observer->OnStopResultEvent(result, advHandle);
89         });
90     }
91 
OnAutoStopAdvEvent(uint8_t advHandle)92     void OnAutoStopAdvEvent(uint8_t advHandle) override
93     {
94         HILOGI("advHandle: %{public}d", advHandle);
95 
96         observers_->ForEach(
97             [advHandle](IBluetoothBleAdvertiseCallback *observer) { observer->OnAutoStopAdvEvent(advHandle); });
98     }
99 
OnSetAdvDataEvent(int32_t result,int32_t advHandle)100     void OnSetAdvDataEvent(int32_t result, int32_t advHandle) override
101     {
102         return;
103     }
104 
SetObserver(RemoteObserverList<IBluetoothBleAdvertiseCallback> * observers)105     void SetObserver(RemoteObserverList<IBluetoothBleAdvertiseCallback> *observers)
106     {
107         observers_ = observers;
108     }
109 
110     SafeMap<sptr<IRemoteObject>, int32_t> observersPid_;
111 
112 private:
113     RemoteObserverList<IBluetoothBleAdvertiseCallback> *observers_;
114 };
115 
116 struct BluetoothBleAdvertiserServer::impl {
117     impl();
118     ~impl();
119 
120     BleAdvertiserDataImpl ConvertAdvertisingData(const BluetoothBleAdvertiserData &data) const;
121 
122     /// sys state observer
123     class SystemStateObserver;
124     std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
125 
126     RemoteObserverList<IBluetoothBleAdvertiseCallback> observers_;
127     std::unique_ptr<BleAdvertiserCallback> observerImp_ = std::make_unique<BleAdvertiserCallback>();
128     std::vector<sptr<IBluetoothBleAdvertiseCallback>> advCallBack_;
129     std::mutex advCallBackMutex;
130 };
131 
132 class BluetoothBleAdvertiserServer::impl::SystemStateObserver : public ISystemStateObserver {
133 public:
SystemStateObserver(BluetoothBleAdvertiserServer::impl * pimpl)134     explicit SystemStateObserver(BluetoothBleAdvertiserServer::impl *pimpl) : pimpl_(pimpl) {};
OnSystemStateChange(const BTSystemState state)135     void OnSystemStateChange(const BTSystemState state) override
136     {
137         auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
138         switch (state) {
139             case BTSystemState::ON:
140                 if (bleService != nullptr) {
141                     bleService->RegisterBleAdvertiserCallback(*pimpl_->observerImp_.get());
142                 }
143                 break;
144             default:
145                 break;
146         }
147     };
148 
149 private:
150     BluetoothBleAdvertiserServer::impl *pimpl_ = nullptr;
151 };
152 
impl()153 BluetoothBleAdvertiserServer::impl::impl()
154 {}
155 
~impl()156 BluetoothBleAdvertiserServer::impl::~impl()
157 {
158     auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
159     if (bleService != nullptr) {
160         bleService->DeregisterBleAdvertiserCallback();
161     }
162 }
163 
BluetoothBleAdvertiserServer()164 BluetoothBleAdvertiserServer::BluetoothBleAdvertiserServer()
165 {
166     pimpl = std::make_unique<impl>();
167     pimpl->observerImp_->SetObserver(&(pimpl->observers_));
168     pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
169     IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
170 
171     auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
172     if (bleService != nullptr) {
173         bleService->RegisterBleAdvertiserCallback(*pimpl->observerImp_.get());
174     }
175 }
176 
~BluetoothBleAdvertiserServer()177 BluetoothBleAdvertiserServer::~BluetoothBleAdvertiserServer()
178 {
179     IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
180 }
181 
ConvertAdvertisingData(const BluetoothBleAdvertiserData & data) const182 BleAdvertiserDataImpl BluetoothBleAdvertiserServer::impl::ConvertAdvertisingData(
183     const BluetoothBleAdvertiserData &data) const
184 {
185     BleAdvertiserDataImpl outData;
186 
187     std::map<uint16_t, std::string> manufacturerData = data.GetManufacturerData();
188     for (auto iter = manufacturerData.begin(); iter != manufacturerData.end(); iter++) {
189         outData.AddManufacturerData(iter->first, iter->second);
190     }
191     std::map<Uuid, std::string> serviceData = data.GetServiceData();
192     for (auto it = serviceData.begin(); it != serviceData.end(); it++) {
193         outData.AddServiceData(it->first, it->second);
194     }
195     std::vector<Uuid> serviceUuids = data.GetServiceUuids();
196     outData.AddServiceUuids(serviceUuids);
197     outData.AddData(data.GetPayload());
198 
199     return outData;
200 }
201 
StartAdvertising(const BluetoothBleAdvertiserSettings & settings,const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle,uint16_t duration,bool isRawData)202 int BluetoothBleAdvertiserServer::StartAdvertising(const BluetoothBleAdvertiserSettings &settings,
203     const BluetoothBleAdvertiserData &advData, const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle,
204     uint16_t duration, bool isRawData)
205 {
206     HILOGI("enter");
207     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
208         HILOGE("check permission failed");
209         return BT_ERR_PERMISSION_FAILED;
210     }
211 
212     auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
213     if (bleService != nullptr) {
214         BleAdvertiserSettingsImpl settingsImpl;
215         settingsImpl.SetConnectable(settings.IsConnectable());
216         settingsImpl.SetInterval(settings.GetInterval());
217         settingsImpl.SetLegacyMode(settings.IsLegacyMode());
218         settingsImpl.SetTxPower(settings.GetTxPower());
219 
220         BleAdvertiserDataImpl bleAdvertiserData = pimpl->ConvertAdvertisingData(advData);
221         if (!isRawData) {
222             bleAdvertiserData.SetFlags(advData.GetAdvFlag());
223         }
224         BleAdvertiserDataImpl bleScanResponse = pimpl->ConvertAdvertisingData(scanResponse);
225         HILOGI("NOT support duration now");
226         bleService->StartAdvertising(settingsImpl, bleAdvertiserData, bleScanResponse, advHandle);
227     }
228     return NO_ERROR;
229 }
230 
EnableAdvertising(uint8_t advHandle,uint16_t duration)231 int BluetoothBleAdvertiserServer::EnableAdvertising(uint8_t advHandle, uint16_t duration)
232 {
233     HILOGI("enter, advHandle: %{public}d", advHandle);
234     pimpl->observerImp_->OnEnableResultEvent(NO_ERROR, advHandle);
235     return NO_ERROR;
236 }
237 
DisableAdvertising(uint8_t advHandle)238 int BluetoothBleAdvertiserServer::DisableAdvertising(uint8_t advHandle)
239 {
240     HILOGI("enter, advHandle: %{public}d", advHandle);
241     pimpl->observerImp_->OnDisableResultEvent(NO_ERROR, advHandle);
242     return NO_ERROR;
243 }
244 
StopAdvertising(int32_t advHandle)245 int BluetoothBleAdvertiserServer::StopAdvertising(int32_t advHandle)
246 {
247     HILOGI("enter, advHandle: %{public}d", advHandle);
248     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
249         HILOGE("check permission failed");
250         return BT_ERR_PERMISSION_FAILED;
251     }
252 
253     auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
254     if (bleService != nullptr) {
255         bleService->StopAdvertising(advHandle);
256     }
257     pimpl->observerImp_->OnStopResultEvent(NO_ERROR, advHandle);
258     return NO_ERROR;
259 }
260 
Close(int32_t advHandle)261 void BluetoothBleAdvertiserServer::Close(int32_t advHandle)
262 {
263     HILOGI("enter, advHandle: %{public}d", advHandle);
264 
265     auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
266     if (bleService != nullptr) {
267         bleService->Close(advHandle);
268     }
269 }
270 
RegisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> & callback)271 void BluetoothBleAdvertiserServer::RegisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> &callback)
272 {
273     HILOGI("enter");
274 
275     if (callback == nullptr) {
276         HILOGE("callback is null");
277         return;
278     }
279     if (pimpl != nullptr) {
280         pimpl->observerImp_->observersPid_.EnsureInsert(callback->AsObject(), IPCSkeleton::GetCallingPid());
281         auto func = std::bind(&BluetoothBleAdvertiserServer::DeregisterBleAdvertiserCallback,
282             this, std::placeholders::_1);
283         pimpl->observers_.Register(callback, func);
284         std::lock_guard<std::mutex> lock(pimpl->advCallBackMutex);
285         pimpl->advCallBack_.push_back(callback);
286     }
287 }
288 
DeregisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> & callback)289 void BluetoothBleAdvertiserServer::DeregisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> &callback)
290 {
291     HILOGI("enter");
292 
293     if (callback == nullptr || pimpl == nullptr) {
294         HILOGE("callback is null, or pimpl is null");
295         return;
296     }
297     {
298         std::lock_guard<std::mutex> lock(pimpl->advCallBackMutex);
299         for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
300             if ((*iter)->AsObject() == callback->AsObject()) {
301                 HILOGI("Deregister observer");
302                 pimpl->observers_.Deregister(*iter);
303                 pimpl->advCallBack_.erase(iter);
304                 break;
305             }
306         }
307     }
308     pimpl->observerImp_->observersPid_.Erase(callback->AsObject());
309 }
310 
GetAdvertiserHandle(int32_t & advHandle,const sptr<IBluetoothBleAdvertiseCallback> & callback)311 int32_t BluetoothBleAdvertiserServer::GetAdvertiserHandle(int32_t &advHandle,
312     const sptr<IBluetoothBleAdvertiseCallback> &callback)
313 {
314     HILOGI("enter");
315     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
316         HILOGE("check permission failed");
317         return BT_ERR_PERMISSION_FAILED;
318     }
319     auto bleService = IAdapterManager::GetInstance()->GetBleAdapterInterface();
320     if (bleService == nullptr) {
321         return BT_ERR_INTERNAL_ERROR;
322     }
323     int32_t status = BT_NO_ERROR;
324     advHandle = bleService->GetAdvertiserHandle(status);
325     if (advHandle == BLE_INVALID_ADVERTISING_HANDLE) {
326         if (status == ADVERTISE_FAILED_TOO_MANY_ADVERTISERS) {
327             return BT_ERR_MAX_RESOURCES;
328         }
329         return BT_ERR_INTERNAL_ERROR;
330     }
331 
332     return NO_ERROR;
333 }
334 
SetAdvertisingData(const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle)335 void BluetoothBleAdvertiserServer::SetAdvertisingData(const BluetoothBleAdvertiserData &advData,
336     const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle)
337 {
338     HILOGI("NOT SUPPORT NOW");
339     return;
340 }
341 
ChangeAdvertisingParams(uint8_t advHandle,const BluetoothBleAdvertiserSettings & settings)342 int BluetoothBleAdvertiserServer::ChangeAdvertisingParams(uint8_t advHandle,
343     const BluetoothBleAdvertiserSettings &settings)
344 {
345     HILOGI("NOT SUPPORT NOW");
346     return BT_ERR_API_NOT_SUPPORT;
347 }
348 }  // namespace Bluetooth
349 }  // namespace OHOS
350