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