• 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 
27 namespace OHOS {
28 namespace Bluetooth {
29 using namespace OHOS::bluetooth;
30 class BleAdvertiserCallback : public IBleAdvertiserCallback {
31 public:
32     BleAdvertiserCallback() = default;
33     ~BleAdvertiserCallback() override = default;
34 
OnStartResultEvent(int result,uint8_t advHandle,int opcode)35     void OnStartResultEvent(int result, uint8_t advHandle, int opcode) override
36     {
37         HILOGI("result: %{public}d, advHandle: %{public}d, opcode: %{public}d", result, advHandle, opcode);
38 
39         observers_->ForEach([this, result, advHandle, opcode](IBluetoothBleAdvertiseCallback *observer) {
40             int32_t uid = observersUid_[observer->AsObject()];
41             if (BluetoothBleCentralManagerServer::IsProxyUid(uid)) {
42                 HILOGD("uid:%{public}d is proxy uid, not callback.", uid);
43                 return;
44             }
45             observer->OnStartResultEvent(result, advHandle, opcode);
46         });
47     }
48 
OnAutoStopAdvEvent(uint8_t advHandle)49     void OnAutoStopAdvEvent(uint8_t advHandle) override
50     {
51         HILOGI("advHandle: %{public}d", advHandle);
52 
53         observers_->ForEach(
54             [advHandle](IBluetoothBleAdvertiseCallback *observer) { observer->OnAutoStopAdvEvent(advHandle); });
55     }
56 
SetObserver(RemoteObserverList<IBluetoothBleAdvertiseCallback> * observers)57     void SetObserver(RemoteObserverList<IBluetoothBleAdvertiseCallback> *observers)
58     {
59         observers_ = observers;
60     }
61 
62     std::map<sptr<IRemoteObject>, int32_t> observersUid_;
63 
64 private:
65     RemoteObserverList<IBluetoothBleAdvertiseCallback> *observers_;
66 };
67 
68 struct BluetoothBleAdvertiserServer::impl {
69     impl();
70     ~impl();
71 
72     BleAdvertiserDataImpl ConvertAdvertisingData(const BluetoothBleAdvertiserData &data) const;
73 
74     /// sys state observer
75     class SystemStateObserver;
76     std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
77 
78     RemoteObserverList<IBluetoothBleAdvertiseCallback> observers_;
79     std::unique_ptr<BleAdvertiserCallback> observerImp_ = std::make_unique<BleAdvertiserCallback>();
80     IAdapterBle *bleService_ = nullptr;
81     std::vector<sptr<IBluetoothBleAdvertiseCallback>> advCallBack_;
82 };
83 
84 class BluetoothBleAdvertiserServer::impl::SystemStateObserver : public ISystemStateObserver {
85 public:
SystemStateObserver(BluetoothBleAdvertiserServer::impl * pimpl)86     explicit SystemStateObserver(BluetoothBleAdvertiserServer::impl *pimpl) : pimpl_(pimpl) {};
OnSystemStateChange(const BTSystemState state)87     void OnSystemStateChange(const BTSystemState state) override
88     {
89         pimpl_->bleService_ =
90             static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
91         switch (state) {
92             case BTSystemState::ON:
93                 if (pimpl_->bleService_ != nullptr) {
94                     pimpl_->bleService_->RegisterBleAdvertiserCallback(*pimpl_->observerImp_.get());
95                 }
96                 break;
97             case BTSystemState::OFF:
98                 pimpl_->bleService_ = nullptr;
99                 break;
100             default:
101                 break;
102         }
103     };
104 
105 private:
106     BluetoothBleAdvertiserServer::impl *pimpl_ = nullptr;
107 };
108 
impl()109 BluetoothBleAdvertiserServer::impl::impl()
110 {}
111 
~impl()112 BluetoothBleAdvertiserServer::impl::~impl()
113 {
114     bleService_ = static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
115     if (bleService_ != nullptr) {
116         bleService_->DeregisterBleAdvertiserCallback();
117     }
118 }
119 
BluetoothBleAdvertiserServer()120 BluetoothBleAdvertiserServer::BluetoothBleAdvertiserServer()
121 {
122     pimpl = std::make_unique<impl>();
123     pimpl->observerImp_->SetObserver(&(pimpl->observers_));
124     pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
125     IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
126 
127     pimpl->bleService_ =
128         static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
129     if (pimpl->bleService_ != nullptr) {
130         pimpl->bleService_->RegisterBleAdvertiserCallback(*pimpl->observerImp_.get());
131     }
132 }
133 
~BluetoothBleAdvertiserServer()134 BluetoothBleAdvertiserServer::~BluetoothBleAdvertiserServer()
135 {
136     IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
137 }
138 
ConvertAdvertisingData(const BluetoothBleAdvertiserData & data) const139 BleAdvertiserDataImpl BluetoothBleAdvertiserServer::impl::ConvertAdvertisingData(
140     const BluetoothBleAdvertiserData &data) const
141 {
142     BleAdvertiserDataImpl outData;
143 
144     std::map<uint16_t, std::string> manufacturerData = data.GetManufacturerData();
145     for (auto iter = manufacturerData.begin(); iter != manufacturerData.end(); iter++) {
146         outData.AddManufacturerData(iter->first, iter->second);
147     }
148     std::map<Uuid, std::string> serviceData = data.GetServiceData();
149     for (auto it = serviceData.begin(); it != serviceData.end(); it++) {
150         outData.AddServiceData(it->first, it->second);
151     }
152     std::vector<Uuid> serviceUuids = data.GetServiceUuids();
153     for (auto it = serviceUuids.begin(); it != serviceUuids.end(); it++) {
154         outData.AddServiceUuid(*it);
155     }
156     outData.AddData(data.GetPayload());
157 
158     return outData;
159 }
160 
StartAdvertising(const BluetoothBleAdvertiserSettings & settings,const BluetoothBleAdvertiserData & advData,const BluetoothBleAdvertiserData & scanResponse,int32_t advHandle,bool isRawData)161 int BluetoothBleAdvertiserServer::StartAdvertising(const BluetoothBleAdvertiserSettings &settings,
162     const BluetoothBleAdvertiserData &advData, const BluetoothBleAdvertiserData &scanResponse, int32_t advHandle,
163     bool isRawData)
164 {
165     HILOGI("enter");
166     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
167         HILOGE("check permission failed");
168         return BT_ERR_PERMISSION_FAILED;
169     }
170 
171     pimpl->bleService_ =
172         static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
173 
174     if (pimpl->bleService_ != nullptr) {
175         BleAdvertiserSettingsImpl settingsImpl;
176         settingsImpl.SetConnectable(settings.IsConnectable());
177         settingsImpl.SetInterval(settings.GetInterval());
178         settingsImpl.SetLegacyMode(settings.IsLegacyMode());
179         settingsImpl.SetTxPower(settings.GetTxPower());
180 
181         BleAdvertiserDataImpl bleAdvertiserData = pimpl->ConvertAdvertisingData(advData);
182         if (!isRawData) {
183             bleAdvertiserData.SetFlags(advData.GetAdvFlag());
184         }
185         BleAdvertiserDataImpl bleScanResponse = pimpl->ConvertAdvertisingData(scanResponse);
186 
187         pimpl->bleService_->StartAdvertising(settingsImpl, bleAdvertiserData, bleScanResponse, advHandle);
188     }
189     return BT_SUCCESS;
190 }
191 
StopAdvertising(int32_t advHandle)192 int BluetoothBleAdvertiserServer::StopAdvertising(int32_t advHandle)
193 {
194     HILOGI("enter, advHandle: %{public}d", advHandle);
195     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
196         HILOGE("check permission failed");
197         return BT_ERR_PERMISSION_FAILED;
198     }
199 
200     pimpl->bleService_ =
201         static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
202 
203     if (pimpl->bleService_ != nullptr) {
204         pimpl->bleService_->StopAdvertising(advHandle);
205     }
206     return BT_SUCCESS;
207 }
208 
Close(int32_t advHandle)209 void BluetoothBleAdvertiserServer::Close(int32_t advHandle)
210 {
211     HILOGI("enter, advHandle: %{public}d", advHandle);
212 
213     pimpl->bleService_ =
214         static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
215 
216     if (pimpl->bleService_ != nullptr) {
217         pimpl->bleService_->Close(advHandle);
218     }
219 }
220 
RegisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> & callback)221 void BluetoothBleAdvertiserServer::RegisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> &callback)
222 {
223     HILOGI("enter");
224 
225     if (callback == nullptr) {
226         HILOGE("callback is null");
227         return;
228     }
229     if (pimpl != nullptr) {
230         pimpl->observerImp_->observersUid_[callback->AsObject()] = IPCSkeleton::GetCallingUid();
231         pimpl->observers_.Register(callback);
232         pimpl->advCallBack_.push_back(callback);
233     }
234 }
235 
DeregisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> & callback)236 void BluetoothBleAdvertiserServer::DeregisterBleAdvertiserCallback(const sptr<IBluetoothBleAdvertiseCallback> &callback)
237 {
238     HILOGI("enter");
239 
240     if (callback == nullptr || pimpl == nullptr) {
241         HILOGE("callback is null, or pimpl is null");
242         return;
243     }
244     for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
245         if ((*iter)->AsObject() == callback->AsObject()) {
246             HILOGI("Deregister observer");
247             pimpl->observers_.Deregister(*iter);
248             pimpl->advCallBack_.erase(iter);
249             break;
250         }
251     }
252     for (auto iter = pimpl->observerImp_->observersUid_.begin(); iter != pimpl->observerImp_->observersUid_.end();
253         ++iter) {
254         if (iter->first == callback->AsObject()) {
255             pimpl->observerImp_->observersUid_.erase(iter);
256             break;
257         }
258     }
259 }
260 
GetAdvertiserHandle()261 int32_t BluetoothBleAdvertiserServer::GetAdvertiserHandle()
262 {
263     HILOGI("enter");
264 
265     int32_t advHandle = BLE_INVALID_ADVERTISING_HANDLE;
266 
267     pimpl->bleService_ =
268         static_cast<IAdapterBle *>(IAdapterManager::GetInstance()->GetAdapter(BTTransport::ADAPTER_BLE));
269 
270     if (pimpl->bleService_ != nullptr) {
271         advHandle = pimpl->bleService_->GetAdvertiserHandle();
272     }
273 
274     return advHandle;
275 }
276 }  // namespace Bluetooth
277 }  // namespace OHOS