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