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 "gatt_data.h"
17#include "bt_def.h"
18#include "memory"
19#include "securec.h"
20
21namespace OHOS {
22namespace bluetooth {
23GattDevice::GattDevice(const RawAddress& addr, uint8_t type, uint8_t transport)
24    : isEncryption_((transport == GATT_TRANSPORT_TYPE_CLASSIC) ? true : false),
25    transport_(transport), addressType_(type), connectState_(0), addr_(addr)
26{
27}
28
29GattDevice::GattDevice(const RawAddress& addr, uint8_t type, uint8_t transport, int state)
30    : isEncryption_((transport == GATT_TRANSPORT_TYPE_CLASSIC) ? true : false),
31    transport_(transport), addressType_(type), connectState_(state), addr_(addr)
32{
33}
34
35GattDevice::GattDevice(const RawAddress& addr, uint8_t transport)
36    : isEncryption_((transport == GATT_TRANSPORT_TYPE_CLASSIC) ? true : false),
37    transport_(transport), addressType_(0), connectState_(0), addr_(addr) {}
38
39Descriptor::Descriptor(const Uuid &uuid, uint16_t handle, int permissions, const uint8_t *value, size_t length)
40    : handle_(handle), permissions_(permissions), value_(nullptr), length_(length), uuid_(uuid)
41    {
42        if (value != nullptr && length != 0) {
43            value_ = std::make_unique<uint8_t[]>(length);
44            (void)memcpy_s(value_.get(), length_, value, length_);
45        } else {
46            value_ = nullptr;
47            length_ = 0;
48        }
49    }
50
51Descriptor::Descriptor(const Descriptor& src)
52    : handle_(src.handle_), permissions_(src.permissions_), value_(nullptr), length_(src.length_), uuid_(src.uuid_)
53{
54    if (src.value_ != nullptr && src.length_ != 0) {
55        value_ = std::make_unique<uint8_t[]>(length_);
56        (void)memcpy_s(value_.get(), length_, src.value_.get(), length_);
57    } else {
58        value_ = nullptr;
59        length_ = 0;
60    }
61}
62
63Descriptor::Descriptor(uint16_t handle, const uint8_t *value, size_t length)
64    : handle_(handle), permissions_(0), value_(nullptr), length_(length), uuid_()
65{
66    value_ = std::make_unique<uint8_t[]>(length_);
67    (void)memcpy_s(value_.get(), length_, value, length_);
68}
69
70Characteristic::Characteristic(
71    const Uuid& uuid, uint16_t handle, int properties, int permissions, const uint8_t *value, size_t length)
72    : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(properties), permissions_(permissions),
73    value_(nullptr), length_(length), uuid_(uuid), descriptors_()
74{
75    if (value != nullptr && length != 0) {
76        value_ = std::make_unique<uint8_t[]>(length_);
77        (void)memcpy_s(value_.get(), length_, value, length_);
78    } else {
79        value_ = nullptr;
80        length_ = 0;
81    }
82}
83
84Characteristic::Characteristic(uint16_t handle, const uint8_t *value, size_t length)
85    : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(0), permissions_(0),
86    value_(std::make_unique<uint8_t[]>(length)), length_(length), uuid_(), descriptors_()
87{
88    (void)memcpy_s(value_.get(), length_, value, length_);
89}
90
91Characteristic::Characteristic(const Characteristic& src)
92    : handle_(src.handle_), endHandle_(src.endHandle_), valueHandle_(src.handle_ + 1),
93    properties_(src.properties_), permissions_(src.permissions_), value_(nullptr), length_(src.length_),
94    uuid_(src.uuid_), descriptors_(src.descriptors_)
95{
96    if (src.value_ != nullptr && src.length_ != 0) {
97        value_ = std::make_unique<uint8_t[]>(length_);
98        (void)memcpy_s(value_.get(), length_, src.value_.get(), length_);
99    } else {
100        value_ = nullptr;
101        length_ = 0;
102    }
103}
104
105void Characteristic::SetValue(const uint8_t *value, size_t length)
106{
107    if (value_ != nullptr) {
108        value_.reset(nullptr);
109    }
110
111    length_ = length;
112    value_ = std::make_unique<uint8_t[]>(length_);
113    (void)memcpy_s(value_.get(), length_, value, length_);
114}
115}  // namespace bluetooth
116}  // namespace OHOS