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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_gatt_service"
17 #endif
18
19 #include "bluetooth_gatt_service.h"
20
21 #include <functional>
22 #include "bluetooth_gatt_characteristic.h"
23 #include "type_traits"
24 #include "uuid.h"
25 #include "vector"
26
27 namespace OHOS {
28 namespace Bluetooth {
AddCharacteristic(const GattCharacteristic & characteristic)29 void GattService::AddCharacteristic(const GattCharacteristic &characteristic)
30 {
31 characteristics_.insert(characteristics_.end(), characteristic)->service_ = this;
32 }
33
GattService(const UUID & uuid,const GattServiceType type)34 GattService::GattService(const UUID &uuid, const GattServiceType type)
35 : handle_(0), endHandle_(0), serviceType_(type), includeServices_(), characteristics_(), uuid_(uuid)
36 {}
37
GattService(const UUID & uuid,uint16_t handle,uint16_t endHandle,const GattServiceType type)38 GattService::GattService(const UUID &uuid, uint16_t handle, uint16_t endHandle, const GattServiceType type)
39 : handle_(handle), endHandle_(endHandle), serviceType_(type), includeServices_(), characteristics_(), uuid_(uuid)
40 {}
41
GattService(const GattService & src)42 GattService::GattService(const GattService &src)
43 : handle_(src.handle_),
44 endHandle_(src.endHandle_),
45 serviceType_(src.serviceType_),
46 includeServices_(),
47 characteristics_(),
48 uuid_(src.uuid_)
49 {
50 includeServices_ = src.includeServices_;
51 for (auto &characteristic : src.characteristics_) {
52 AddCharacteristic(characteristic);
53 }
54 }
55
GattService(GattService && src)56 GattService::GattService(GattService &&src)
57 : handle_(src.handle_),
58 endHandle_(src.endHandle_),
59 serviceType_(src.serviceType_),
60 includeServices_(),
61 characteristics_(),
62 uuid_(src.uuid_)
63 {
64 includeServices_ = std::move(src.includeServices_);
65 for (auto &characteristic : src.characteristics_) {
66 characteristics_.insert(characteristics_.end(), std::move(characteristic))->service_ = this;
67 }
68 }
69
AddService(GattService & service)70 void GattService::AddService(GattService &service)
71 {
72 includeServices_.push_back(std::ref(service));
73 }
74
GetCharacteristic(const UUID & uuid)75 GattCharacteristic *GattService::GetCharacteristic(const UUID &uuid)
76 {
77 for (auto &characteristic : characteristics_) {
78 if (characteristic.GetUuid().Equals(uuid)) {
79 return &characteristic;
80 }
81 }
82 return nullptr;
83 }
84
GetCharacteristic(const uint16_t handle)85 GattCharacteristic *GattService::GetCharacteristic(const uint16_t handle)
86 {
87 for (auto &characteristic : characteristics_) {
88 if (characteristic.GetHandle() == handle) {
89 return &characteristic;
90 }
91 }
92 return nullptr;
93 }
94
GetCharacteristics()95 std::vector<GattCharacteristic> &GattService::GetCharacteristics()
96 {
97 return characteristics_;
98 }
99
GetIncludedServices()100 const std::vector<std::reference_wrapper<GattService>> &GattService::GetIncludedServices()
101 {
102 return includeServices_;
103 }
104
GetHandle() const105 uint16_t GattService::GetHandle() const
106 {
107 return handle_;
108 }
109
IsPrimary() const110 bool GattService::IsPrimary() const
111 {
112 return (serviceType_ == GattServiceType::PRIMARY);
113 }
114
GetUuid() const115 const UUID &GattService::GetUuid() const
116 {
117 return uuid_;
118 }
119 } // namespace Bluetooth
120 } // namespace OHOS
121