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