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