1 /*
2 * Copyright (C) 2021-2022 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_characteristic.h"
17 #include "bluetooth_gatt_descriptor.h"
18 #include "bluetooth_log.h"
19 #include "type_traits"
20
21 namespace OHOS {
22 namespace Bluetooth {
23
GattCharacteristic(const UUID uuid,int permissions,int properties)24 GattCharacteristic::GattCharacteristic(const UUID uuid, int permissions, int properties)
25 : writeType_(properties & Propertie::AUTHENTICATED_SIGNED_WRITES ? WriteType::SIGNED
26 : properties & Propertie::WRITE_WITHOUT_RESPONSE ? WriteType::NO_RESPONSE
27 : WriteType::DEFAULT),
28 handle_(0),
29 permissions_(permissions),
30 properties_(properties),
31 service_(nullptr),
32 value_(nullptr),
33 length_(0),
34 descriptors_(),
35 uuid_(uuid)
36 {}
37
GattCharacteristic(const UUID uuid,uint16_t handle,const int permissions,const int properties)38 GattCharacteristic::GattCharacteristic(const UUID uuid, uint16_t handle, const int permissions, const int properties)
39 : writeType_(properties & Propertie::AUTHENTICATED_SIGNED_WRITES ? WriteType::SIGNED
40 : properties & Propertie::WRITE_WITHOUT_RESPONSE ? WriteType::NO_RESPONSE
41 : WriteType::DEFAULT),
42 handle_(handle),
43 permissions_(permissions),
44 properties_(properties),
45 service_(nullptr),
46 value_(nullptr),
47 length_(0),
48 descriptors_(),
49 uuid_(uuid)
50 {}
51
GattCharacteristic(const GattCharacteristic & src)52 GattCharacteristic::GattCharacteristic(const GattCharacteristic &src)
53 : writeType_(src.writeType_),
54 handle_(src.handle_),
55 permissions_(src.permissions_),
56 properties_(src.properties_),
57 service_(src.service_),
58 value_(nullptr),
59 length_(src.length_),
60 descriptors_(),
61 uuid_(src.uuid_)
62 {
63 if (nullptr != src.value_ && 0 != length_) {
64 value_ = std::make_unique<uint8_t[]>(length_);
65 (void)memcpy_s(value_.get(), length_, src.value_.get(), length_);
66 } else {
67 value_.reset(nullptr);
68 length_ = 0;
69 }
70
71 for (auto &desc : src.descriptors_) {
72 AddDescriptor(desc);
73 }
74 }
75
operator =(const GattCharacteristic & src)76 GattCharacteristic &GattCharacteristic::operator=(const GattCharacteristic &src)
77 {
78 if (this != &src) {
79 uuid_ = src.uuid_;
80 permissions_ = src.permissions_;
81 properties_ = src.properties_;
82 service_ = src.service_;
83 handle_ = src.handle_;
84 length_ = src.length_;
85 writeType_ = src.writeType_;
86
87 if (nullptr != src.value_ && 0 != length_) {
88 value_ = std::make_unique<uint8_t[]>(length_);
89 (void)memcpy_s(value_.get(), length_, src.value_.get(), length_);
90 } else {
91 value_.reset(nullptr);
92 length_ = 0;
93 }
94
95 descriptors_ = src.descriptors_;
96 }
97 return *this;
98 }
99
GattCharacteristic(GattCharacteristic && src)100 GattCharacteristic::GattCharacteristic(GattCharacteristic &&src)
101 : writeType_(src.writeType_),
102 handle_(src.handle_),
103 permissions_(src.permissions_),
104 properties_(src.properties_),
105 service_(src.service_),
106 value_(std::move(src.value_)),
107 length_(src.length_),
108 descriptors_(),
109 uuid_(src.uuid_)
110 {
111 for (auto &desc : src.descriptors_) {
112 descriptors_.insert(descriptors_.end(), std::move(desc))->characteristic_ = this;
113 }
114 }
115
AddDescriptor(const GattDescriptor & descriptor)116 void GattCharacteristic::AddDescriptor(const GattDescriptor &descriptor)
117 {
118 descriptors_.insert(descriptors_.end(), descriptor)->characteristic_ = this;
119 }
120
GetDescriptor(const UUID & uuid)121 GattDescriptor *GattCharacteristic::GetDescriptor(const UUID &uuid)
122 {
123 for (auto &desc : descriptors_) {
124 if (desc.GetUuid().Equals(uuid)) {
125 return &desc;
126 }
127 }
128 return nullptr;
129 }
130
GetDescriptors()131 std::vector<GattDescriptor> &GattCharacteristic::GetDescriptors()
132 {
133 return descriptors_;
134 }
135
GetHandle() const136 uint16_t GattCharacteristic::GetHandle() const
137 {
138 return handle_;
139 }
140
GetPermissions() const141 int GattCharacteristic::GetPermissions() const
142 {
143 return permissions_;
144 }
145
GetProperties() const146 int GattCharacteristic::GetProperties() const
147 {
148 return properties_;
149 }
150
GetService() const151 GattService *GattCharacteristic::GetService() const
152 {
153 return service_;
154 }
155
GetUuid() const156 const UUID &GattCharacteristic::GetUuid() const
157 {
158 return uuid_;
159 }
160
GetValue(size_t * size) const161 const std::unique_ptr<uint8_t[]> &GattCharacteristic::GetValue(size_t *size) const
162 {
163 *size = length_;
164 return value_;
165 }
166
GetWriteType() const167 int GattCharacteristic::GetWriteType() const
168 {
169 return writeType_;
170 }
171
SetWriteType(int type)172 int GattCharacteristic::SetWriteType(int type)
173 {
174 if (type != WriteType::DEFAULT && type != WriteType::NO_RESPONSE && type != WriteType::SIGNED) {
175 HILOGE("Invalid parameter");
176 return GattStatus::INVALID_PARAMETER;
177 }
178
179 writeType_ = type;
180 return GattStatus::GATT_SUCCESS;
181 }
182
SetValue(const uint8_t * values,const size_t length)183 void GattCharacteristic::SetValue(const uint8_t *values, const size_t length)
184 {
185 if (values == nullptr || length == 0) {
186 HILOGE("values is nullptr, or length is 0");
187 return;
188 }
189 value_ = std::make_unique<uint8_t[]>(length);
190 length_ = length;
191 (void)memcpy_s(value_.get(), length, values, length);
192 }
193
194 } // namespace Bluetooth
195 } // namespace OHOS
196