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