• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_(nullptr),
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     service_ = src.service_;
119     src.service_ = nullptr;
120 }
121 
AddDescriptor(const GattDescriptor & descriptor)122 void GattCharacteristic::AddDescriptor(const GattDescriptor &descriptor)
123 {
124     descriptors_.insert(descriptors_.end(), descriptor)->characteristic_ = this;
125 }
126 
GetDescriptor(const UUID & uuid)127 GattDescriptor *GattCharacteristic::GetDescriptor(const UUID &uuid)
128 {
129     for (auto &desc : descriptors_) {
130         if (desc.GetUuid().Equals(uuid)) {
131             return &desc;
132         }
133     }
134     return nullptr;
135 }
136 
GetDescriptors()137 std::vector<GattDescriptor> &GattCharacteristic::GetDescriptors()
138 {
139     return descriptors_;
140 }
141 
GetHandle() const142 uint16_t GattCharacteristic::GetHandle() const
143 {
144     return handle_;
145 }
146 
GetPermissions() const147 int GattCharacteristic::GetPermissions() const
148 {
149     return permissions_;
150 }
151 
GetProperties() const152 int GattCharacteristic::GetProperties() const
153 {
154     return properties_;
155 }
156 
GetService() const157 GattService *GattCharacteristic::GetService() const
158 {
159     return service_;
160 }
161 
GetUuid() const162 const UUID &GattCharacteristic::GetUuid() const
163 {
164     return uuid_;
165 }
166 
GetValue(size_t * size) const167 const std::unique_ptr<uint8_t[]> &GattCharacteristic::GetValue(size_t *size) const
168 {
169     *size = length_;
170     return value_;
171 }
172 
GetWriteType() const173 int GattCharacteristic::GetWriteType() const
174 {
175     return writeType_;
176 }
177 
SetWriteType(int type)178 int GattCharacteristic::SetWriteType(int type)
179 {
180     if (type != WriteType::DEFAULT && type != WriteType::NO_RESPONSE && type != WriteType::SIGNED) {
181         HILOGE("Invalid parameter");
182         return GattStatus::INVALID_PARAMETER;
183     }
184 
185     writeType_ = type;
186     return GattStatus::GATT_SUCCESS;
187 }
188 
SetValue(const uint8_t * values,const size_t length)189 void GattCharacteristic::SetValue(const uint8_t *values, const size_t length)
190 {
191     if (values == nullptr || length == 0) {
192         HILOGD("values is nullptr, or length is 0");
193         return;
194     }
195     value_ = std::make_unique<uint8_t[]>(length);
196     length_ = length;
197     (void)memcpy_s(value_.get(), length, values, length);
198 }
199 
200 }  // namespace Bluetooth
201 }  // namespace OHOS
202