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