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