195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
495489c19Sopenharmony_ci * you may not use this file except in compliance with the License.
595489c19Sopenharmony_ci * You may obtain a copy of the License at
695489c19Sopenharmony_ci *
795489c19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
895489c19Sopenharmony_ci *
995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and
1395489c19Sopenharmony_ci * limitations under the License.
1495489c19Sopenharmony_ci */
1595489c19Sopenharmony_ci
1695489c19Sopenharmony_ci#include "bluetooth_gatt_service.h"
1795489c19Sopenharmony_ci
1895489c19Sopenharmony_ci#include <functional>
1995489c19Sopenharmony_ci#include "bluetooth_gatt_characteristic.h"
2095489c19Sopenharmony_ci#include "type_traits"
2195489c19Sopenharmony_ci#include "uuid.h"
2295489c19Sopenharmony_ci#include "vector"
2395489c19Sopenharmony_ci
2495489c19Sopenharmony_cinamespace OHOS {
2595489c19Sopenharmony_cinamespace Bluetooth {
2695489c19Sopenharmony_civoid GattService::AddCharacteristic(const GattCharacteristic &characteristic)
2795489c19Sopenharmony_ci{
2895489c19Sopenharmony_ci    characteristics_.insert(characteristics_.end(), characteristic)->service_ = this;
2995489c19Sopenharmony_ci}
3095489c19Sopenharmony_ci
3195489c19Sopenharmony_ciGattService::GattService(const UUID &uuid, const GattServiceType type)
3295489c19Sopenharmony_ci    : handle_(0), endHandle_(0), serviceType_(type), includeServices_(), characteristics_(), uuid_(uuid)
3395489c19Sopenharmony_ci{}
3495489c19Sopenharmony_ci
3595489c19Sopenharmony_ciGattService::GattService(const UUID &uuid, uint16_t handle, uint16_t endHandle, const GattServiceType type)
3695489c19Sopenharmony_ci    : handle_(handle), endHandle_(endHandle), serviceType_(type), includeServices_(), characteristics_(), uuid_(uuid)
3795489c19Sopenharmony_ci{}
3895489c19Sopenharmony_ci
3995489c19Sopenharmony_ciGattService::GattService(const GattService &src)
4095489c19Sopenharmony_ci    : handle_(src.handle_),
4195489c19Sopenharmony_ci      endHandle_(src.endHandle_),
4295489c19Sopenharmony_ci      serviceType_(src.serviceType_),
4395489c19Sopenharmony_ci      includeServices_(),
4495489c19Sopenharmony_ci      characteristics_(),
4595489c19Sopenharmony_ci      uuid_(src.uuid_)
4695489c19Sopenharmony_ci{
4795489c19Sopenharmony_ci    includeServices_ = src.includeServices_;
4895489c19Sopenharmony_ci    for (auto &characteristic : src.characteristics_) {
4995489c19Sopenharmony_ci        AddCharacteristic(characteristic);
5095489c19Sopenharmony_ci    }
5195489c19Sopenharmony_ci}
5295489c19Sopenharmony_ci
5395489c19Sopenharmony_ciGattService::GattService(GattService &&src)
5495489c19Sopenharmony_ci    : handle_(src.handle_),
5595489c19Sopenharmony_ci      endHandle_(src.endHandle_),
5695489c19Sopenharmony_ci      serviceType_(src.serviceType_),
5795489c19Sopenharmony_ci      includeServices_(),
5895489c19Sopenharmony_ci      characteristics_(),
5995489c19Sopenharmony_ci      uuid_(src.uuid_)
6095489c19Sopenharmony_ci{
6195489c19Sopenharmony_ci    includeServices_ = std::move(src.includeServices_);
6295489c19Sopenharmony_ci    for (auto &characteristic : src.characteristics_) {
6395489c19Sopenharmony_ci        characteristics_.insert(characteristics_.end(), std::move(characteristic))->service_ = this;
6495489c19Sopenharmony_ci    }
6595489c19Sopenharmony_ci}
6695489c19Sopenharmony_ci
6795489c19Sopenharmony_civoid GattService::AddService(GattService &service)
6895489c19Sopenharmony_ci{
6995489c19Sopenharmony_ci    includeServices_.push_back(std::ref(service));
7095489c19Sopenharmony_ci}
7195489c19Sopenharmony_ci
7295489c19Sopenharmony_ciGattCharacteristic *GattService::GetCharacteristic(const UUID &uuid)
7395489c19Sopenharmony_ci{
7495489c19Sopenharmony_ci    for (auto &characteristic : characteristics_) {
7595489c19Sopenharmony_ci        if (characteristic.GetUuid().Equals(uuid)) {
7695489c19Sopenharmony_ci            return &characteristic;
7795489c19Sopenharmony_ci        }
7895489c19Sopenharmony_ci    }
7995489c19Sopenharmony_ci    return nullptr;
8095489c19Sopenharmony_ci}
8195489c19Sopenharmony_ci
8295489c19Sopenharmony_cistd::vector<GattCharacteristic> &GattService::GetCharacteristics()
8395489c19Sopenharmony_ci{
8495489c19Sopenharmony_ci    return characteristics_;
8595489c19Sopenharmony_ci}
8695489c19Sopenharmony_ci
8795489c19Sopenharmony_ciconst std::vector<std::reference_wrapper<GattService>> &GattService::GetIncludedServices()
8895489c19Sopenharmony_ci{
8995489c19Sopenharmony_ci    return includeServices_;
9095489c19Sopenharmony_ci}
9195489c19Sopenharmony_ci
9295489c19Sopenharmony_ciuint16_t GattService::GetHandle() const
9395489c19Sopenharmony_ci{
9495489c19Sopenharmony_ci    return handle_;
9595489c19Sopenharmony_ci}
9695489c19Sopenharmony_ci
9795489c19Sopenharmony_cibool GattService::IsPrimary() const
9895489c19Sopenharmony_ci{
9995489c19Sopenharmony_ci    return (serviceType_ == GattServiceType::PRIMARY);
10095489c19Sopenharmony_ci}
10195489c19Sopenharmony_ci
10295489c19Sopenharmony_ciconst UUID &GattService::GetUuid() const
10395489c19Sopenharmony_ci{
10495489c19Sopenharmony_ci    return uuid_;
10595489c19Sopenharmony_ci}
10695489c19Sopenharmony_ci}  // namespace Bluetooth
10795489c19Sopenharmony_ci}  // namespace OHOS
108