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