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 #include "securec.h"
16 #include "bluetooth_gatt_descriptor.h"
17
18 namespace OHOS {
19 namespace Bluetooth {
GattDescriptor(const UUID uuid,const int permissions)20 GattDescriptor::GattDescriptor(const UUID uuid, const int permissions)
21 : handle_(0), permissions_(permissions), characteristic_(nullptr), value_(nullptr), length_(0), uuid_(uuid)
22 {}
23
GattDescriptor(const UUID uuid,uint16_t handle,const int permissions)24 GattDescriptor::GattDescriptor(const UUID uuid, uint16_t handle, const int permissions)
25 : handle_(handle), permissions_(permissions), characteristic_(nullptr), value_(nullptr), length_(0), uuid_(uuid)
26 {}
27
GattDescriptor(const GattDescriptor & src)28 GattDescriptor::GattDescriptor(const GattDescriptor &src)
29 : handle_(src.handle_),
30 permissions_(src.permissions_),
31 characteristic_(src.characteristic_),
32 value_(nullptr),
33 length_(src.length_),
34 uuid_(src.uuid_)
35 {
36 if (length_ != 0 && src.value_ != nullptr) {
37 value_ = std::make_unique<uint8_t[]>(length_);
38 memcpy_s(value_.get(), length_, src.value_.get(), length_);
39 } else {
40 value_.reset(nullptr);
41 length_ = 0;
42 }
43 }
44
operator =(const GattDescriptor & src)45 GattDescriptor &GattDescriptor::operator=(const GattDescriptor &src)
46 {
47 if (this != &src) {
48 uuid_ = src.uuid_;
49 permissions_ = src.permissions_;
50 handle_ = src.handle_;
51 characteristic_ = src.characteristic_;
52 length_ = src.length_;
53
54 if (length_ != 0 && src.value_ != nullptr) {
55 value_ = std::make_unique<uint8_t[]>(length_);
56 memcpy_s(value_.get(), length_, src.value_.get(), length_);
57 } else {
58 value_.reset(nullptr);
59 length_ = 0;
60 }
61 }
62 return *this;
63 }
64
GetCharacteristic() const65 GattCharacteristic *GattDescriptor::GetCharacteristic() const
66 {
67 return characteristic_;
68 }
69
GetPermissions() const70 int GattDescriptor::GetPermissions() const
71 {
72 return permissions_;
73 }
74
GetUuid() const75 const UUID &GattDescriptor::GetUuid() const
76 {
77 return uuid_;
78 }
79
GetValue(size_t * size) const80 const std::unique_ptr<uint8_t[]> &GattDescriptor::GetValue(size_t *size) const
81 {
82 *size = length_;
83 return value_;
84 }
85
SetValue(const uint8_t * values,const size_t length)86 void GattDescriptor::SetValue(const uint8_t *values, const size_t length)
87 {
88 if (length == 0 || values == nullptr) {
89 return;
90 }
91 value_ = std::make_unique<uint8_t[]>(length);
92 length_ = length;
93 memcpy_s(value_.get(), length, values, length);
94 }
95
GetHandle() const96 uint16_t GattDescriptor::GetHandle() const
97 {
98 return handle_;
99 }
100 } // namespace Bluetooth
101 } // namespace OHOS
102