• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CancelConnection(const BluetoothGattDevice & device)336 void BluetoothGattServerServer::CancelConnection(const BluetoothGattDevice &device)
337 {
338     HILOGE("Unsupport GattServer CancelConnection");
339 }
340 
NotifyClient(const BluetoothGattDevice & device,BluetoothGattCharacteristic * characteristic,bool needConfirm)341 int BluetoothGattServerServer::NotifyClient(
342     const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, bool needConfirm)
343 {
344     HILOGI("addr: %{public}s, needConfirm: %{public}d", GET_ENCRYPT_GATT_ADDR(device), needConfirm);
345     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
346         HILOGE("check permission failed");
347         return BT_ERR_PERMISSION_FAILED;
348     }
349     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
350     if (!pimpl->serverService_) {
351         HILOGE("serverService_ is null");
352         return BT_ERR_INTERNAL_ERROR;
353     }
354     bluetooth::Characteristic character(characteristic->handle_);
355     character.length_ = characteristic->length_;
356     character.value_ = std::move(characteristic->value_);
357     characteristic->length_ = 0;
358 
359     int ret = pimpl->serverService_->NotifyClient((bluetooth::GattDevice)device, character, needConfirm);
360     return (ret == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
361 }
362 
RemoveService(int32_t appId,const BluetoothGattService & services)363 int BluetoothGattServerServer::RemoveService(int32_t appId, const BluetoothGattService &services)
364 {
365     HILOGI("appId: %{public}d", appId);
366     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
367         HILOGE("check permission failed");
368         return BT_ERR_PERMISSION_FAILED;
369     }
370     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
371     if (!pimpl->serverService_) {
372         HILOGE("serverService_ is null");
373         return BT_ERR_INTERNAL_ERROR;
374     }
375 
376     int ret = pimpl->serverService_->RemoveService(appId, (bluetooth::Service)services);
377     return (ret == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
378 }
379 
RespondCharacteristicRead(const BluetoothGattDevice & device,BluetoothGattCharacteristic * characteristic,int32_t ret)380 int BluetoothGattServerServer::RespondCharacteristicRead(
381     const BluetoothGattDevice &device, BluetoothGattCharacteristic *characteristic, int32_t ret)
382 {
383     HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
384     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
385         HILOGE("check permission failed");
386         return BT_ERR_PERMISSION_FAILED;
387     }
388     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
389     if (!pimpl->serverService_) {
390         HILOGE("serverService_ is null");
391         return BT_ERR_INTERNAL_ERROR;
392     }
393     bluetooth::Characteristic character(characteristic->handle_);
394     character.length_ = characteristic->length_;
395     character.value_ = std::move(characteristic->value_);
396     characteristic->length_ = 0;
397 
398     int result = pimpl->serverService_->RespondCharacteristicRead((bluetooth::GattDevice)device, character, ret);
399     return (result == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
400 }
401 
RespondCharacteristicWrite(const BluetoothGattDevice & device,const BluetoothGattCharacteristic & characteristic,int32_t ret)402 int BluetoothGattServerServer::RespondCharacteristicWrite(
403     const BluetoothGattDevice &device, const BluetoothGattCharacteristic &characteristic, int32_t ret)
404 {
405     HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
406     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
407         HILOGE("check permission failed");
408         return BT_ERR_PERMISSION_FAILED;
409     }
410     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
411     if (!pimpl->serverService_) {
412         HILOGE("serverService_ is null");
413         return BT_ERR_INTERNAL_ERROR;
414     }
415 
416     int result = pimpl->serverService_->RespondCharacteristicWrite(
417         (bluetooth::GattDevice)device, (bluetooth::Characteristic)characteristic, ret);
418     return (result == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
419 }
420 
RespondDescriptorRead(const BluetoothGattDevice & device,BluetoothGattDescriptor * descriptor,int32_t ret)421 int BluetoothGattServerServer::RespondDescriptorRead(
422     const BluetoothGattDevice &device, BluetoothGattDescriptor *descriptor, int32_t ret)
423 {
424     HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
425     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
426         HILOGE("check permission failed");
427         return BT_ERR_PERMISSION_FAILED;
428     }
429     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
430     if (!pimpl->serverService_) {
431         HILOGE("serverService_ is null");
432         return BT_ERR_INTERNAL_ERROR;
433     }
434     bluetooth::Descriptor desc(descriptor->handle_);
435     desc.length_ = descriptor->length_;
436     desc.value_ = std::move(descriptor->value_);
437     descriptor->length_ = 0;
438 
439     int result = pimpl->serverService_->RespondDescriptorRead((bluetooth::GattDevice)device, desc, ret);
440     return (result == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
441 }
442 
RespondDescriptorWrite(const BluetoothGattDevice & device,const BluetoothGattDescriptor & descriptor,int32_t ret)443 int BluetoothGattServerServer::RespondDescriptorWrite(
444     const BluetoothGattDevice &device, const BluetoothGattDescriptor &descriptor, int32_t ret)
445 {
446     HILOGI("addr: %{public}s, ret: %{public}d", GET_ENCRYPT_GATT_ADDR(device), ret);
447     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
448         HILOGE("check permission failed");
449         return BT_ERR_PERMISSION_FAILED;
450     }
451     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
452     if (!pimpl->serverService_) {
453         HILOGE("serverService_ is null");
454         return BT_ERR_INTERNAL_ERROR;
455     }
456 
457     int result = pimpl->serverService_->RespondDescriptorWrite(
458         (bluetooth::GattDevice)device, (bluetooth::Descriptor)descriptor, ret);
459     return (result == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
460 }
461 
RegisterApplication(const sptr<IBluetoothGattServerCallback> & callback)462 int BluetoothGattServerServer::RegisterApplication(const sptr<IBluetoothGattServerCallback> &callback)
463 {
464     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
465     pimpl->serverService_ = pimpl->GetServicePtr();
466     if (!pimpl->serverService_) {
467         HILOGE("serverService_ is null");
468         return bluetooth::GattStatus::REQUEST_NOT_SUPPORT;
469     }
470 
471     auto it = pimpl->callbacks_.emplace(
472         pimpl->callbacks_.begin(), std::make_shared<impl::GattServerCallbackImpl>(callback, *this));
473 
474     int ret = pimpl->serverService_->RegisterApplication(*it);
475     if (ret >= 0) {
476         HILOGI("appId, %{public}d", ret);
477         (*it)->SetAppId(ret);
478         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BT_SERVICE, "GATT_APP_REGISTER",
479             OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,  "ACTION", "register",
480             "SIDE", "server", "ADDRESS", "empty", "PID", OHOS::IPCSkeleton::GetCallingPid(),
481             "UID", OHOS::IPCSkeleton::GetCallingUid(), "APPID", ret);
482     } else {
483         HILOGE("RegisterApplication failed, ret: %{public}d", ret);
484         pimpl->callbacks_.erase(it);
485     }
486     return ret;
487 }
488 
DeregisterApplication(int32_t appId)489 int BluetoothGattServerServer::DeregisterApplication(int32_t appId)
490 {
491     HILOGI("appId: %{public}d", appId);
492     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
493         HILOGE("check permission failed");
494         return BT_ERR_PERMISSION_FAILED;
495     }
496     std::lock_guard<std::mutex> lck(pimpl->registerMutex_);
497     if (!pimpl->serverService_) {
498         HILOGE("serverService_ is null");
499         return BT_ERR_INTERNAL_ERROR;
500     }
501     int ret = pimpl->serverService_->DeregisterApplication(appId);
502     HILOGI("list size: %{public}lu", (unsigned long)pimpl->callbacks_.size());
503     for (auto it = pimpl->callbacks_.begin(); it != pimpl->callbacks_.end(); ++it) {
504         if ((*it) != nullptr && (*it)->GetAppId() == appId) {
505             HILOGI("erase appId: %{public}d", appId);
506             pimpl->callbacks_.erase(it);
507             break;
508         }
509     }
510     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BT_SERVICE, "GATT_APP_REGISTER",
511         OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,  "ACTION", "deregister",
512         "SIDE", "server", "ADDRESS", "empty", "PID", OHOS::IPCSkeleton::GetCallingPid(),
513         "UID", OHOS::IPCSkeleton::GetCallingUid(), "APPID", appId);
514     return (ret == GattStatus::GATT_SUCCESS ? NO_ERROR : BT_ERR_INTERNAL_ERROR);
515 }
BluetoothGattServerServer()516 BluetoothGattServerServer::BluetoothGattServerServer() : pimpl(new impl())
517 {
518     HILOGI("Bluetooth Gatt Server Server Created!");
519 }
520 
~BluetoothGattServerServer()521 BluetoothGattServerServer::~BluetoothGattServerServer()
522 {}
523 }  // namespace Bluetooth
524 }  // namespace OHOS