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