1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gatt_based_services_manager.h"
17 #include <condition_variable>
18 #include <mutex>
19 #include "bt_def.h"
20 #include "log.h"
21
22 namespace OHOS {
23 namespace bluetooth {
GattBasedServicesManager(GattServerService & server,utility::Dispatcher & dispatcher)24 GattBasedServicesManager::GattBasedServicesManager(GattServerService &server, utility::Dispatcher &dispatcher)
25 : gas_(std::make_unique<GenericAccessService>(server, dispatcher)),
26 dis_(std::make_unique<DeviceInformationService>(server)),
27 gatts_(std::make_unique<GenericAttributeService>(server, dispatcher))
28 {}
29
~GattBasedServicesManager()30 GattBasedServicesManager::~GattBasedServicesManager()
31 {}
32
Enable() const33 int GattBasedServicesManager::Enable() const
34 {
35 int result = gas_->RegisterService();
36 if (result != GattStatus::GATT_SUCCESS) {
37 LOG_WARN("%{public}s:%{public}s:%{public}d Register GAS failed!", __FILE__, __FUNCTION__, __LINE__);
38 }
39
40 result = dis_->RegisterService();
41 if (result != GattStatus::GATT_SUCCESS) {
42 LOG_WARN("%{public}s:%{public}s:%{public}d Register DIS failed!", __FILE__, __FUNCTION__, __LINE__);
43 }
44
45 if (dis_->RegisterSDP() != GattStatus::GATT_SUCCESS) {
46 LOG_WARN("%{public}s:%{public}s:%{public}d Register DIS to SDP failed!", __FILE__, __FUNCTION__, __LINE__);
47 }
48
49 result = gatts_->RegisterService();
50 if (result != GattStatus::GATT_SUCCESS) {
51 LOG_WARN("%{public}s:%{public}s:%{public}d Register GATTS failed!", __FILE__, __FUNCTION__, __LINE__);
52 }
53
54 if (gatts_->RegisterSDP() != GattStatus::GATT_SUCCESS) {
55 LOG_WARN("%{public}s:%{public}s:%{public}d Register GATTS to SDP failed!", __FILE__, __FUNCTION__, __LINE__);
56 }
57
58 return result;
59 }
60
Disable() const61 void GattBasedServicesManager::Disable() const
62 {
63 gatts_->DeregisterSDP();
64 gatts_->DeregisterService();
65 dis_->DeregisterSDP();
66 dis_->DeregisterService();
67 gas_->DeregisterService();
68 }
69 } // namespace bluetooth
70 } // namespace OHOS