1 /*
2 * Copyright (C) 2021-2022 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
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_gatt_server_server.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_utils_server.h"
23 #include "gatt_data.h"
24 #include "hisysevent.h"
25 #include "i_bluetooth_gatt_server.h"
26 #include "interface_adapter_manager.h"
27 #include "interface_profile_gatt_server.h"
28 #include "interface_profile_manager.h"
29 #include "ipc_skeleton.h"
30 #include "permission_utils.h"
31
32 namespace OHOS {
33 namespace Bluetooth {
34 using namespace OHOS::bluetooth;
35 struct BluetoothGattServerServer::impl {
36 class GattServerCallbackImpl;
37 class SystemStateObserver;
38
39 bluetooth::IProfileGattServer *serverService_;
40 std::unique_ptr<SystemStateObserver> systemStateObserver_;
41 std::list<std::shared_ptr<GattServerCallbackImpl>> callbacks_;
42 std::mutex registerMutex_;
43
44 impl();
45 ~impl();
46
GetServicePtrOHOS::Bluetooth::BluetoothGattServerServer::impl47 bluetooth::IProfileGattServer *GetServicePtr()
48 {
49 if (bluetooth::IProfileManager::GetInstance() == nullptr) {
50 return nullptr;
51 }
52 return static_cast<bluetooth::IProfileGattServer *>(
53 bluetooth::IProfileManager::GetInstance()->GetProfileService(bluetooth::PROFILE_NAME_GATT_SERVER));
54 }
55 };
56 class BluetoothGattServerServer::impl::SystemStateObserver : public bluetooth::ISystemStateObserver {
57 public:
SystemStateObserver(BluetoothGattServerServer::impl * impl)58 SystemStateObserver(BluetoothGattServerServer::impl *impl) : impl_(impl){};
59 ~SystemStateObserver() override = default;
60
OnSystemStateChange(const bluetooth::BTSystemState state)61 void OnSystemStateChange(const bluetooth::BTSystemState state) override
62 {
63 std::lock_guard<std::mutex> lck(impl_->registerMutex_);
64 switch (state) {
65 case bluetooth::BTSystemState::ON:
66 impl_->serverService_ = impl_->GetServicePtr();
67 break;
68 case bluetooth::BTSystemState::OFF:
69 impl_->serverService_ = nullptr;
70 break;
71 default:
72 break;
73 }
74 }
75
76 private:
77 BluetoothGattServerServer::impl *impl_;
78 };
79
80 class BluetoothGattServerServer::impl::GattServerCallbackImpl : public bluetooth::IGattServerCallback {
81 public:
OnCharacteristicReadRequest(const bluetooth::GattDevice & device,const bluetooth::Characteristic & characteristic)82 void OnCharacteristicReadRequest(
83 const bluetooth::GattDevice &device, const bluetooth::Characteristic &characteristic) override
84 {
85 HILOGI("addr: %{public}s", GET_ENCRYPT_GATT_ADDR(device));
86 if (PermissionUtils::VerifyUseBluetoothPermission(tokenId_) == PERMISSION_DENIED) {
87 HILOGE("check permission failed, tokenId_: %{public}u", tokenId_);
88 return;
89 }
90 callback_->OnCharacteristicReadRequest(
91 (BluetoothGattDevice)device, (BluetoothGattCharacteristic)characteristic);
92 }
OnCharacteristicReadByUuidRequest(const bluetooth::GattDevice & device,const bluetooth::Characteristic & characteristic)93 void OnCharacteristicReadByUuidRequest(
94 const bluetooth::GattDevice &device, const bluetooth::Characteristic &characteristic) override
95 {
96 HILOGI("addr: %{public}s", GET_ENCRYPT_GATT_ADDR(device));
97 }
OnCharacteristicWriteRequest(const bluetooth::GattDevice & device,const bluetooth::Characteristic & characteristic,bool needRespones)98 void OnCharacteristicWriteRequest(const bluetooth::GattDevice &device,
99 const bluetooth::Characteristic &characteristic, bool needRespones) override
100 {
101 HILOGI("addr: %{public}s, needRespones: %{public}d", GET_ENCRYPT_GATT_ADDR(device), needRespones);
102 if (PermissionUtils::VerifyUseBluetoothPermission(tokenId_) == PERMISSION_DENIED) {
103 HILOGE("check permission failed, tokenId_: %{public}u", tokenId_);
104 return;
105 }
106 callback_->OnCharacteristicWriteRequest(
107 (BluetoothGattDevice)device, (BluetoothGattCharacteristic)characteristic, needRespones);
108 }
OnDescriptorReadRequest(const bluetooth::GattDevice & device,const bluetooth::Descriptor & descriptor)109 void OnDescriptorReadRequest(const bluetooth::GattDevice &device, const bluetooth::Descriptor &descriptor) override
110 {
111 HILOGI("addr: %{public}s", GET_ENCRYPT_GATT_ADDR(device));
112 if (PermissionUtils::VerifyUseBluetoothPermission(tokenId_) == PERMISSION_DENIED) {
113 HILOGE("check permission failed, tokenId_: %{public}u", tokenId_);
114 return;
115 }
116 callback_->OnDescriptorReadRequest((BluetoothGattDevice)device, (BluetoothGattDescriptor)descriptor);
117 }
OnDescriptorWriteRequest(const bluetooth::GattDevice & device,const bluetooth::Descriptor & descriptor)118 void OnDescriptorWriteRequest(const bluetooth::GattDevice &device, const bluetooth::Descriptor &descriptor) override
119 {
120 HILOGI("addr: %{public}s", GET_ENCRYPT_GATT_ADDR(device));
121 if (PermissionUtils::VerifyUseBluetoothPermission(tokenId_) == PERMISSION_DENIED) {
122 HILOGE("check permission failed, tokenId_: %{public}u", tokenId_);
123 return;
124 }
125 callback_->OnDescriptorWriteRequest((BluetoothGattDevice)device, (BluetoothGattDescriptor)descriptor);
126 }
OnNotifyConfirm(const bluetooth::GattDevice & device,const bluetooth::Characteristic & characteristic,int result)127 void OnNotifyConfirm(
128 const bluetooth::GattDevice &device, const bluetooth::Characteristic &characteristic, int result) override
129 {
130 HILOGI("addr: %{public}s, result: %{public}d", GET_ENCRYPT_GATT_ADDR(device), result);
131 callback_->OnNotifyConfirm((BluetoothGattDevice)device, (BluetoothGattCharacteristic)characteristic, result);
132 }
OnConnectionStateChanged(const bluetooth::GattDevice & device,int ret,int state)133 void OnConnectionStateChanged(const bluetooth::GattDevice &device, int ret, int state) override
134 {
135 HILOGI("addr: %{public}s, ret: %{public}d, state: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret, state);
136 if (PermissionUtils::VerifyUseBluetoothPermission(tokenId_) == PERMISSION_DENIED) {
137 HILOGE("check permission failed, tokenId_: %{public}u", tokenId_);
138 return;
139 }
140 int32_t pid = IPCSkeleton::GetCallingPid();
141 int32_t uid = IPCSkeleton::GetCallingUid();
142 if (state == static_cast<int>(BTConnectState::CONNECTED) ||
143 state == static_cast<int>(BTConnectState::DISCONNECTED)) {
144 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "GATT_SERVER_CONN_STATE",
145 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "PID", pid, "UID", uid, "STATE", state);
146 }
147 if (callback_ == nullptr) {
148 HILOGE("callback is nullptr.");
149 return;
150 }
151 callback_->OnConnectionStateChanged((BluetoothGattDevice)device, ret, state);
152 }
OnMtuChanged(const bluetooth::GattDevice & device,int mtu)153 void OnMtuChanged(const bluetooth::GattDevice &device, int mtu) override
154 {
155 HILOGI("addr: %{public}s, mtu: %{public}d", GET_ENCRYPT_GATT_ADDR(device), mtu);
156 callback_->OnMtuChanged((BluetoothGattDevice)device, mtu);
157 }
OnAddService(int ret,const bluetooth::Service & services)158 void OnAddService(int ret, const bluetooth::Service &services) override
159 {
160 HILOGI("ret: %{public}d", ret);
161 callback_->OnAddService(ret, (BluetoothGattService)services);
162 }
OnServiceChanged(const bluetooth::Service & services)163 void OnServiceChanged(const bluetooth::Service &services) override
164 {
165 HILOGI("enter");
166 }
OnConnectionParameterChanged(const bluetooth::GattDevice & device,int interval,int latency,int timeout,int status)167 void OnConnectionParameterChanged(
168 const bluetooth::GattDevice &device, int interval, int latency, int timeout, int status) override
169 {
170 HILOGI("addr: %{public}s, interval: %{public}d, latency: %{public}d, timeout: %{public}d, status: %{public}d",
171 GET_ENCRYPT_GATT_ADDR(device), interval, latency, timeout, status);
172 callback_->OnConnectionParameterChanged((BluetoothGattDevice)device, interval, latency, timeout, status);
173 }
174
GetCallback()175 sptr<IBluetoothGattServerCallback> GetCallback()
176 {
177 return callback_;
178 }
179
SetAppId(int32_t appId)180 void SetAppId(int32_t appId)
181 {
182 HILOGI("SetAppId = %{public}d", appId);
183 appId_ = appId;
184 }
185
GetAppId()186 int32_t GetAppId()
187 {
188 return appId_;
189 }
190
191 GattServerCallbackImpl(const sptr<IBluetoothGattServerCallback> &callback, BluetoothGattServerServer &owner);
~GattServerCallbackImpl()192 ~GattServerCallbackImpl()
193 {
194 if (!callback_->AsObject()->RemoveDeathRecipient(deathRecipient_)) {
195 HILOGE("Failed to unlink death recipient to callback");
196 }
197 callback_ = nullptr;
198 deathRecipient_ = nullptr;
199 };
200
201 private:
202 class GattServerCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
203 public:
204 GattServerCallbackDeathRecipient(
205 const sptr<IBluetoothGattServerCallback> &callback, BluetoothGattServerServer &owner);
206
GetCallback() const207 sptr<IBluetoothGattServerCallback> GetCallback() const
208 {
209 return callback_;
210 };
211
212 void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
213
214 private:
215 sptr<IBluetoothGattServerCallback> callback_;
216 BluetoothGattServerServer &owner_;
217 };
218
219 sptr<IBluetoothGattServerCallback> callback_;
220 sptr<GattServerCallbackDeathRecipient> deathRecipient_;
221 uint32_t tokenId_;
222 int32_t appId_;
223 };
224
GattServerCallbackImpl(const sptr<IBluetoothGattServerCallback> & callback,BluetoothGattServerServer & owner)225 BluetoothGattServerServer::impl::GattServerCallbackImpl::GattServerCallbackImpl(
226 const sptr<IBluetoothGattServerCallback> &callback, BluetoothGattServerServer &owner)
227 : callback_(callback), deathRecipient_(new GattServerCallbackDeathRecipient(callback, owner))
228 {
229 if (callback_ != nullptr &&
230 callback_->AsObject() != nullptr &&
231 !callback_->AsObject()->AddDeathRecipient(deathRecipient_)) {
232 HILOGE("Failed to link death recipient to callback");
233 }
234 tokenId_ = IPCSkeleton::GetCallingTokenID();
235 appId_ = -1;
236 }
237
238 BluetoothGattServerServer::impl::GattServerCallbackImpl::GattServerCallbackDeathRecipient::
GattServerCallbackDeathRecipient(const sptr<IBluetoothGattServerCallback> & callback,BluetoothGattServerServer & owner)239 GattServerCallbackDeathRecipient(
240 const sptr<IBluetoothGattServerCallback> &callback, BluetoothGattServerServer &owner)
241 : callback_(callback), owner_(owner)
242 {}
243
OnRemoteDied(const wptr<IRemoteObject> & remote)244 void BluetoothGattServerServer::impl::GattServerCallbackImpl::GattServerCallbackDeathRecipient::OnRemoteDied(
245 const wptr<IRemoteObject> &remote)
246 {
247 HILOGI("GattServerCallbackDeathRecipient OnRemoteDied start, list size = %{public}lu",
248 (unsigned long)owner_.pimpl->callbacks_.size());
249 std::lock_guard<std::mutex> lck(owner_.pimpl->registerMutex_);
250 for (auto it = owner_.pimpl->callbacks_.begin(); it != owner_.pimpl->callbacks_.end(); ++it) {
251 if ((*it) != nullptr && (*it)->GetCallback() != nullptr && (*it)->GetCallback()->AsObject() == remote) {
252 int appId = (*it)->GetAppId();
253 HILOGI("callback is erased from callbacks, appId: %{public}d", appId);
254 sptr<GattServerCallbackDeathRecipient> dr = (*it)->deathRecipient_;
255 if (!dr->GetCallback()->AsObject()->RemoveDeathRecipient(dr)) {
256 HILOGE("Failed to unlink death recipient from callback");
257 }
258 if (owner_.pimpl->serverService_ != nullptr) {
259 int ret = owner_.pimpl->serverService_->DeregisterApplication(appId);
260 HILOGI("DeregisterApplication result:%{public}d, appId:%{public}d", ret, appId);
261 }
262 owner_.pimpl->callbacks_.erase(it);
263 return;
264 }
265 }
266 }
impl()267 BluetoothGattServerServer::impl::impl() : serverService_(nullptr), systemStateObserver_(new SystemStateObserver(this))
268 {
269 bluetooth::IAdapterManager::GetInstance()->RegisterSystemStateObserver(*systemStateObserver_);
270 }
271
~impl()272 BluetoothGattServerServer::impl::~impl()
273 {
274 bluetooth::IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*systemStateObserver_);
275 }
276
AddService(int32_t appId,BluetoothGattService * services)277 int BluetoothGattServerServer::AddService(int32_t appId, BluetoothGattService *services)
278 {
279 HILOGI("enter, appId: %{public}d", appId);
280 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
281 HILOGE("check permission failed");
282 return BT_ERR_PERMISSION_FAILED;
283 }
284 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
285 if (!pimpl->serverService_) {
286 HILOGE("serverService_ is null");
287 return BT_ERR_INTERNAL_ERROR;
288 }
289 bluetooth::Service svc = (bluetooth::Service)*services;
290
291 int ret = pimpl->serverService_->AddService(appId, svc);
292 return (ret == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
293 }
294
ClearServices(int appId)295 void BluetoothGattServerServer::ClearServices(int appId)
296 {
297 HILOGI("enter, appId: %{public}d", appId);
298 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
299 if (!pimpl->serverService_) {
300 HILOGE("serverService_ is null");
301 return;
302 }
303 pimpl->serverService_->ClearServices(appId);
304 }
305
CancelConnection(const BluetoothGattDevice & device)306 void BluetoothGattServerServer::CancelConnection(const BluetoothGattDevice &device)
307 {
308 HILOGI("addr: %{public}s", GET_ENCRYPT_GATT_ADDR(device));
309 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
310 if (!pimpl->serverService_) {
311 HILOGE("serverService_ is null");
312 return;
313 }
314 pimpl->serverService_->CancelConnection((bluetooth::GattDevice)device);
315 }
316
NotifyClient(const BluetoothGattDevice & device,BluetoothGattCharacteristic * characteristic,bool needConfirm)317 int BluetoothGattServerServer::NotifyClient(
318 const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, bool needConfirm)
319 {
320 HILOGI("addr: %{public}s, needConfirm: %{public}d", GET_ENCRYPT_GATT_ADDR(device), needConfirm);
321 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
322 HILOGE("check permission failed");
323 return BT_ERR_PERMISSION_FAILED;
324 }
325 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
326 if (!pimpl->serverService_) {
327 HILOGE("serverService_ is null");
328 return BT_ERR_INTERNAL_ERROR;
329 }
330 bluetooth::Characteristic character(characteristic->handle_);
331 character.length_ = characteristic->length_;
332 character.value_ = std::move(characteristic->value_);
333 characteristic->length_ = 0;
334
335 int ret = pimpl->serverService_->NotifyClient((bluetooth::GattDevice)device, character, needConfirm);
336 return (ret == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
337 }
338
RemoveService(int32_t appId,const BluetoothGattService & services)339 int BluetoothGattServerServer::RemoveService(int32_t appId, const BluetoothGattService &services)
340 {
341 HILOGI("appId: %{public}d", appId);
342 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
343 HILOGE("check permission failed");
344 return BT_ERR_PERMISSION_FAILED;
345 }
346 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
347 if (!pimpl->serverService_) {
348 HILOGE("serverService_ is null");
349 return BT_ERR_INTERNAL_ERROR;
350 }
351
352 int ret = pimpl->serverService_->RemoveService(appId, (bluetooth::Service)services);
353 return (ret == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
354 }
355
RespondCharacteristicRead(const BluetoothGattDevice & device,BluetoothGattCharacteristic * characteristic,int32_t ret)356 int BluetoothGattServerServer::RespondCharacteristicRead(
357 const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, int32_t ret)
358 {
359 HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
360 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
361 HILOGE("check permission failed");
362 return BT_ERR_PERMISSION_FAILED;
363 }
364 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
365 if (!pimpl->serverService_) {
366 HILOGE("serverService_ is null");
367 return BT_ERR_INTERNAL_ERROR;
368 }
369 bluetooth::Characteristic character(characteristic->handle_);
370 character.length_ = characteristic->length_;
371 character.value_ = std::move(characteristic->value_);
372 characteristic->length_ = 0;
373
374 int result = pimpl->serverService_->RespondCharacteristicRead((bluetooth::GattDevice)device, character, ret);
375 return (result == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
376 }
377
RespondCharacteristicWrite(const BluetoothGattDevice & device,const BluetoothGattCharacteristic & characteristic,int32_t ret)378 int BluetoothGattServerServer::RespondCharacteristicWrite(
379 const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, int32_t ret)
380 {
381 HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
382 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
383 HILOGE("check permission failed");
384 return BT_ERR_PERMISSION_FAILED;
385 }
386 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
387 if (!pimpl->serverService_) {
388 HILOGE("serverService_ is null");
389 return BT_ERR_INTERNAL_ERROR;
390 }
391
392 int result = pimpl->serverService_->RespondCharacteristicWrite(
393 (bluetooth::GattDevice)device, (bluetooth::Characteristic)characteristic, ret);
394 return (result == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
395 }
396
RespondDescriptorRead(const BluetoothGattDevice & device,BluetoothGattDescriptor * descriptor,int32_t ret)397 int BluetoothGattServerServer::RespondDescriptorRead(
398 const BluetoothGattDevice &device, BluetoothGattDescriptor *descriptor, int32_t ret)
399 {
400 HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
401 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
402 HILOGE("check permission failed");
403 return BT_ERR_PERMISSION_FAILED;
404 }
405 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
406 if (!pimpl->serverService_) {
407 HILOGE("serverService_ is null");
408 return BT_ERR_INTERNAL_ERROR;
409 }
410 bluetooth::Descriptor desc(descriptor->handle_);
411 desc.length_ = descriptor->length_;
412 desc.value_ = std::move(descriptor->value_);
413 descriptor->length_ = 0;
414
415 int result = pimpl->serverService_->RespondDescriptorRead((bluetooth::GattDevice)device, desc, ret);
416 return (result == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
417 }
418
RespondDescriptorWrite(const BluetoothGattDevice & device,const BluetoothGattDescriptor & descriptor,int32_t ret)419 int BluetoothGattServerServer::RespondDescriptorWrite(
420 const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor, int32_t ret)
421 {
422 HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
423 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
424 HILOGE("check permission failed");
425 return BT_ERR_PERMISSION_FAILED;
426 }
427 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
428 if (!pimpl->serverService_) {
429 HILOGE("serverService_ is null");
430 return BT_ERR_INTERNAL_ERROR;
431 }
432
433 int result = pimpl->serverService_->RespondDescriptorWrite(
434 (bluetooth::GattDevice)device, (bluetooth::Descriptor)descriptor, ret);
435 return (result == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
436 }
437
RegisterApplication(const sptr<IBluetoothGattServerCallback> & callback)438 int BluetoothGattServerServer::RegisterApplication(const sptr<IBluetoothGattServerCallback> &callback)
439 {
440 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
441 pimpl->serverService_ = pimpl->GetServicePtr();
442 if (!pimpl->serverService_) {
443 HILOGE("serverService_ is null");
444 return bluetooth::GattStatus::REQUEST_NOT_SUPPORT;
445 }
446
447 auto it = pimpl->callbacks_.emplace(
448 pimpl->callbacks_.begin(), std::make_shared<impl::GattServerCallbackImpl>(callback, *this));
449
450 int ret = pimpl->serverService_->RegisterApplication(*it);
451 if (ret >= 0) {
452 HILOGI("appId, %{public}d", ret);
453 (*it)->SetAppId(ret);
454 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "GATT_APP_REGISTER",
455 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "register",
456 "SIDE", "server", "ADDRESS", "empty", "PID", OHOS::IPCSkeleton::GetCallingPid(),
457 "UID", OHOS::IPCSkeleton::GetCallingUid(), "APPID", ret);
458 } else {
459 HILOGE("RegisterApplication failed, ret: %{public}d", ret);
460 pimpl->callbacks_.erase(it);
461 }
462 return ret;
463 }
464
DeregisterApplication(int32_t appId)465 int BluetoothGattServerServer::DeregisterApplication(int32_t appId)
466 {
467 HILOGI("appId: %{public}d", appId);
468 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
469 HILOGE("check permission failed");
470 return BT_ERR_PERMISSION_FAILED;
471 }
472 std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
473 if (!pimpl->serverService_) {
474 HILOGE("serverService_ is null");
475 return BT_ERR_INTERNAL_ERROR;
476 }
477 int ret = pimpl->serverService_->DeregisterApplication(appId);
478 HILOGI("list size: %{public}lu", (unsigned long)pimpl->callbacks_.size());
479 for (auto it = pimpl->callbacks_.begin(); it != pimpl->callbacks_.end(); ++it) {
480 if ((*it) != nullptr && (*it)->GetAppId() == appId) {
481 HILOGI("erase appId: %{public}d", appId);
482 pimpl->callbacks_.erase(it);
483 break;
484 }
485 }
486 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "GATT_APP_REGISTER",
487 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "deregister",
488 "SIDE", "server", "ADDRESS", "empty", "PID", OHOS::IPCSkeleton::GetCallingPid(),
489 "UID", OHOS::IPCSkeleton::GetCallingUid(), "APPID", appId);
490 return (ret == GattStatus::GATT_SUCCESS ? BT_SUCCESS : BT_ERR_INTERNAL_ERROR);
491 }
BluetoothGattServerServer()492 BluetoothGattServerServer::BluetoothGattServerServer() : pimpl(new impl())
493 {
494 HILOGI("Bluetooth Gatt Server Server Created!");
495 }
496
~BluetoothGattServerServer()497 BluetoothGattServerServer::~BluetoothGattServerServer()
498 {}
499 } // namespace Bluetooth
500 } // namespace OHOS