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