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 "gatt_data.h"
1795489c19Sopenharmony_ci#include "bt_def.h"
1895489c19Sopenharmony_ci#include "memory"
1995489c19Sopenharmony_ci#include "securec.h"
2095489c19Sopenharmony_ci
2195489c19Sopenharmony_cinamespace OHOS {
2295489c19Sopenharmony_cinamespace bluetooth {
2395489c19Sopenharmony_ciGattDevice::GattDevice(const RawAddress& addr, uint8_t type, uint8_t transport)
2495489c19Sopenharmony_ci    : isEncryption_((transport == GATT_TRANSPORT_TYPE_CLASSIC) ? true : false),
2595489c19Sopenharmony_ci    transport_(transport), addressType_(type), connectState_(0), addr_(addr)
2695489c19Sopenharmony_ci{
2795489c19Sopenharmony_ci}
2895489c19Sopenharmony_ci
2995489c19Sopenharmony_ciGattDevice::GattDevice(const RawAddress& addr, uint8_t type, uint8_t transport, int state)
3095489c19Sopenharmony_ci    : isEncryption_((transport == GATT_TRANSPORT_TYPE_CLASSIC) ? true : false),
3195489c19Sopenharmony_ci    transport_(transport), addressType_(type), connectState_(state), addr_(addr)
3295489c19Sopenharmony_ci{
3395489c19Sopenharmony_ci}
3495489c19Sopenharmony_ci
3595489c19Sopenharmony_ciGattDevice::GattDevice(const RawAddress& addr, uint8_t transport)
3695489c19Sopenharmony_ci    : isEncryption_((transport == GATT_TRANSPORT_TYPE_CLASSIC) ? true : false),
3795489c19Sopenharmony_ci    transport_(transport), addressType_(0), connectState_(0), addr_(addr) {}
3895489c19Sopenharmony_ci
3995489c19Sopenharmony_ciDescriptor::Descriptor(const Uuid &uuid, uint16_t handle, int permissions, const uint8_t *value, size_t length)
4095489c19Sopenharmony_ci    : handle_(handle), permissions_(permissions), value_(nullptr), length_(length), uuid_(uuid)
4195489c19Sopenharmony_ci    {
4295489c19Sopenharmony_ci        if (value != nullptr && length != 0) {
4395489c19Sopenharmony_ci            value_ = std::make_unique<uint8_t[]>(length);
4495489c19Sopenharmony_ci            (void)memcpy_s(value_.get(), length_, value, length_);
4595489c19Sopenharmony_ci        } else {
4695489c19Sopenharmony_ci            value_ = nullptr;
4795489c19Sopenharmony_ci            length_ = 0;
4895489c19Sopenharmony_ci        }
4995489c19Sopenharmony_ci    }
5095489c19Sopenharmony_ci
5195489c19Sopenharmony_ciDescriptor::Descriptor(const Descriptor& src)
5295489c19Sopenharmony_ci    : handle_(src.handle_), permissions_(src.permissions_), value_(nullptr), length_(src.length_), uuid_(src.uuid_)
5395489c19Sopenharmony_ci{
5495489c19Sopenharmony_ci    if (src.value_ != nullptr && src.length_ != 0) {
5595489c19Sopenharmony_ci        value_ = std::make_unique<uint8_t[]>(length_);
5695489c19Sopenharmony_ci        (void)memcpy_s(value_.get(), length_, src.value_.get(), length_);
5795489c19Sopenharmony_ci    } else {
5895489c19Sopenharmony_ci        value_ = nullptr;
5995489c19Sopenharmony_ci        length_ = 0;
6095489c19Sopenharmony_ci    }
6195489c19Sopenharmony_ci}
6295489c19Sopenharmony_ci
6395489c19Sopenharmony_ciDescriptor::Descriptor(uint16_t handle, const uint8_t *value, size_t length)
6495489c19Sopenharmony_ci    : handle_(handle), permissions_(0), value_(nullptr), length_(length), uuid_()
6595489c19Sopenharmony_ci{
6695489c19Sopenharmony_ci    value_ = std::make_unique<uint8_t[]>(length_);
6795489c19Sopenharmony_ci    (void)memcpy_s(value_.get(), length_, value, length_);
6895489c19Sopenharmony_ci}
6995489c19Sopenharmony_ci
7095489c19Sopenharmony_ciCharacteristic::Characteristic(
7195489c19Sopenharmony_ci    const Uuid& uuid, uint16_t handle, int properties, int permissions, const uint8_t *value, size_t length)
7295489c19Sopenharmony_ci    : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(properties), permissions_(permissions),
7395489c19Sopenharmony_ci    value_(nullptr), length_(length), uuid_(uuid), descriptors_()
7495489c19Sopenharmony_ci{
7595489c19Sopenharmony_ci    if (value != nullptr && length != 0) {
7695489c19Sopenharmony_ci        value_ = std::make_unique<uint8_t[]>(length_);
7795489c19Sopenharmony_ci        (void)memcpy_s(value_.get(), length_, value, length_);
7895489c19Sopenharmony_ci    } else {
7995489c19Sopenharmony_ci        value_ = nullptr;
8095489c19Sopenharmony_ci        length_ = 0;
8195489c19Sopenharmony_ci    }
8295489c19Sopenharmony_ci}
8395489c19Sopenharmony_ci
8495489c19Sopenharmony_ciCharacteristic::Characteristic(uint16_t handle, const uint8_t *value, size_t length)
8595489c19Sopenharmony_ci    : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(0), permissions_(0),
8695489c19Sopenharmony_ci    value_(std::make_unique<uint8_t[]>(length)), length_(length), uuid_(), descriptors_()
8795489c19Sopenharmony_ci{
8895489c19Sopenharmony_ci    (void)memcpy_s(value_.get(), length_, value, length_);
8995489c19Sopenharmony_ci}
9095489c19Sopenharmony_ci
9195489c19Sopenharmony_ciCharacteristic::Characteristic(const Characteristic& src)
9295489c19Sopenharmony_ci    : handle_(src.handle_), endHandle_(src.endHandle_), valueHandle_(src.handle_ + 1),
9395489c19Sopenharmony_ci    properties_(src.properties_), permissions_(src.permissions_), value_(nullptr), length_(src.length_),
9495489c19Sopenharmony_ci    uuid_(src.uuid_), descriptors_(src.descriptors_)
9595489c19Sopenharmony_ci{
9695489c19Sopenharmony_ci    if (src.value_ != nullptr && src.length_ != 0) {
9795489c19Sopenharmony_ci        value_ = std::make_unique<uint8_t[]>(length_);
9895489c19Sopenharmony_ci        (void)memcpy_s(value_.get(), length_, src.value_.get(), length_);
9995489c19Sopenharmony_ci    } else {
10095489c19Sopenharmony_ci        value_ = nullptr;
10195489c19Sopenharmony_ci        length_ = 0;
10295489c19Sopenharmony_ci    }
10395489c19Sopenharmony_ci}
10495489c19Sopenharmony_ci
10595489c19Sopenharmony_civoid Characteristic::SetValue(const uint8_t *value, size_t length)
10695489c19Sopenharmony_ci{
10795489c19Sopenharmony_ci    if (value_ != nullptr) {
10895489c19Sopenharmony_ci        value_.reset(nullptr);
10995489c19Sopenharmony_ci    }
11095489c19Sopenharmony_ci
11195489c19Sopenharmony_ci    length_ = length;
11295489c19Sopenharmony_ci    value_ = std::make_unique<uint8_t[]>(length_);
11395489c19Sopenharmony_ci    (void)memcpy_s(value_.get(), length_, value, length_);
11495489c19Sopenharmony_ci}
11595489c19Sopenharmony_ci}  // namespace bluetooth
11695489c19Sopenharmony_ci}  // namespace OHOS