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 
16 #include "ble_properties.h"
17 
18 #include "ble_defs.h"
19 
20 #include "bt_uuid.h"
21 #include "btstack.h"
22 #include "gap_if.h"
23 #include "log.h"
24 #include "raw_address.h"
25 #include "securec.h"
26 
27 namespace OHOS {
28 namespace bluetooth {
29 struct BleProperties::impl {
30     std::string deviceName_{BLE_DEFAULT_DEVICE_NAME};
31     BaseObserverList<IAdapterBleObserver> *observer_ = nullptr;
32     int ioCapability_ = BLE_DEFAULT_IO;
33     std::string passkey_{BLE_DEFAULT_LOCAL_PASSKEY};
34     int bondableMode_ = BLE_BONDABLE_MODE_NONE;
35     std::string macAddr_{BLE_INVALID_MAC_ADDRESS};
36 };
37 
GetInstance()38 BleProperties &BleProperties::GetInstance()
39 {
40     static BleProperties instance;
41     return instance;
42 }
43 
BleProperties()44 BleProperties::BleProperties() : pimpl(std::make_unique<BleProperties::impl>())
45 {}
46 
~BleProperties()47 BleProperties::~BleProperties()
48 {}
49 
GetLocalName() const50 std::string BleProperties::GetLocalName() const
51 {
52     LOG_DEBUG("[BleProperties] %{public}s", __func__);
53 
54     return pimpl->deviceName_;
55 }
56 
SetLocalName(const std::string &name) const57 bool BleProperties::SetLocalName(const std::string &name) const
58 {
59     LOG_DEBUG("[BleProperties] %{public}s", __func__);
60 
61     int length = name.length();
62     std::string newName = name;
63 
64     if (name.empty()) {
65         return false;
66     }
67 
68     if (length >= DEVICE_NAME_MAX_LEN) {
69         length = DEVICE_NAME_MAX_LEN;
70         newName = name.substr(0, length);
71     }
72 
73     pimpl->deviceName_ = BleConfig::GetInstance().GetLocalName();
74     if (newName != pimpl->deviceName_) {
75         pimpl->deviceName_ = newName;
76         int type = BLE_CONFIG_LOCAL_NAME;
77         UpdateConfig(type);
78     }
79     return true;
80 }
81 
GetLocalAddress() const82 std::string BleProperties::GetLocalAddress() const
83 {
84     LOG_DEBUG("[BleProperties] %{public}s", __func__);
85     return pimpl->macAddr_;
86 }
87 
GetBondableMode() const88 int BleProperties::GetBondableMode() const
89 {
90     LOG_DEBUG("[BleProperties] %{public}s", __func__);
91 
92     return pimpl->bondableMode_;
93 }
94 
SetBondableMode(int mode) const95 int BleProperties::SetBondableMode(int mode) const
96 {
97     LOG_DEBUG("[BleProperties] %{public}s", __func__);
98 
99     if (pimpl->bondableMode_ == mode) {
100         return BT_SUCCESS;
101     }
102     switch (mode) {
103         case BLE_BONDABLE_MODE_NONE:
104             pimpl->bondableMode_ = GAP_BONDABLE_MODE_NON;
105             break;
106         case BLE_BONDABLE_MODE_ON:
107             pimpl->bondableMode_ = GAP_BONDABLE_MODE;
108             break;
109         default:
110             pimpl->bondableMode_ = GAP_BONDABLE_MODE_NON;
111             break;
112     }
113     return GAPIF_LeSetBondMode(pimpl->bondableMode_);
114 }
115 
GetPasskey() const116 std::string BleProperties::GetPasskey() const
117 {
118     LOG_DEBUG("[BleProperties] %{public}s", __func__);
119 
120     return pimpl->passkey_;
121 }
122 
GetIoCapability() const123 int BleProperties::GetIoCapability() const
124 {
125     LOG_DEBUG("[BleProperties] %{public}s", __func__);
126 
127     return pimpl->ioCapability_;
128 }
129 
SetIoCapability(int ioCapability) const130 bool BleProperties::SetIoCapability(int ioCapability) const
131 {
132     LOG_DEBUG("[BleProperties] %{public}s", __func__);
133 
134     pimpl->ioCapability_ = ioCapability;
135     return true;
136 }
137 
GetAddrFromController() const138 bool BleProperties::GetAddrFromController() const
139 {
140     LOG_DEBUG("[BleProperties] %{public}s", __func__);
141 
142     BtAddr btAddr;
143     (void)memset_s(&btAddr, sizeof(btAddr), 0x00, sizeof(btAddr));
144     int ret = GAPIF_GetLocalAddr(&btAddr);
145     if (ret != BT_SUCCESS) {
146         LOG_ERROR("BleProperties::GAP_GetLocalAddr Failed");
147         return false;
148     }
149     RawAddress addr = RawAddress::ConvertToString(btAddr.addr);
150     pimpl->macAddr_ = addr.GetAddress();
151     return UpdateConfig(BLE_CONFIG_LOCAL_ADDRESS);
152 }
153 
UpdateConfig(int type) const154 bool BleProperties::UpdateConfig(int type) const
155 {
156     LOG_DEBUG("[BleProperties] %{public}s:Type = %{public}d", __func__, type);
157 
158     bool ret = BT_OPERATION_FAILED;
159     switch (type) {
160         case BLE_CONFIG_LOCAL_NAME:
161             ret = BleConfig::GetInstance().SetLocalName(pimpl->deviceName_);
162             if (pimpl->observer_ != nullptr) {
163                 std::string deviceName = pimpl->deviceName_;
164                 pimpl->observer_->ForEach(
165                     [deviceName](IAdapterBleObserver &observer) { observer.OnDeviceNameChanged(deviceName); });
166             }
167             break;
168         case BLE_CONFIG_LOCAL_ADDRESS:
169             BleConfig::GetInstance().SetLocalAddress(pimpl->macAddr_);
170             ret = BleConfig::GetInstance().SetBleLocalAddrType(BLE_ADDR_TYPE::BLE_ADDR_TYPE_PUBLIC);
171             if (pimpl->observer_ != nullptr) {
172                 std::string macAddr = pimpl->macAddr_;
173                 pimpl->observer_->ForEach(
174                     [macAddr](IAdapterBleObserver &observer) { observer.OnDeviceAddrChanged(macAddr); });
175             }
176             break;
177         default:
178             break;
179     }
180 
181     ret &= BleConfig::GetInstance().Save();
182     return ret;
183 }
184 
LoadBleConfigInfo() const185 bool BleProperties::LoadBleConfigInfo() const
186 {
187     LOG_DEBUG("[BleProperties] %{public}s", __func__);
188 
189     bool ret = BleConfig::GetInstance().LoadConfigInfo();
190     if (!ret) {
191         LOG_ERROR("[BleProperties] %{public}s:%{public}s", __func__, "Load device config file failed!");
192     }
193     ReadBleHostInfo();
194     return ret;
195 }
196 
ConfigBleProperties() const197 bool BleProperties::ConfigBleProperties() const
198 {
199     LOG_DEBUG("[BleProperties] %{public}s", __func__);
200 
201     return SetLocalName(pimpl->deviceName_);
202 }
203 
GetBleRoles()204 int BleProperties::GetBleRoles()
205 {
206     LOG_DEBUG("[BleProperties] %{public}s", __func__);
207 
208     return BleConfig::GetInstance().GetBleRoles();
209 }
210 
SetBleRoles(uint8_t roles)211 bool BleProperties::SetBleRoles(uint8_t roles)
212 {
213     LOG_DEBUG("[BleProperties] %{public}s:%u", __func__, roles);
214     int ret = GAPIF_LeSetRole(roles);
215     if (ret != BT_SUCCESS) {
216         LOG_ERROR("[BleProperties] %{public}s:%{public}s", __func__, "Set ble roles failed!");
217     }
218 
219     return BleConfig::GetInstance().SetBleRoles(roles);
220 }
221 
ReadBleHostInfo() const222 void BleProperties::ReadBleHostInfo() const
223 {
224     LOG_DEBUG("[BleProperties] %{public}s", __func__);
225 
226     pimpl->deviceName_ = BleConfig::GetInstance().GetLocalName();
227     pimpl->ioCapability_ = BleConfig::GetInstance().GetIoCapability();
228     pimpl->passkey_ = BleConfig::GetInstance().GetLoaclPasskey();
229 }
230 
GetAppearance()231 int BleProperties::GetAppearance()
232 {
233     LOG_DEBUG("[BleProperties] %{public}s", __func__);
234 
235     return BleConfig::GetInstance().GetAppearance();
236 }
237 
SetPasskey(const std::string &passkey)238 bool BleProperties::SetPasskey(const std::string &passkey)
239 {
240     LOG_DEBUG("[BleProperties] %{public}s:%{public}s", __func__, passkey.c_str());
241 
242     return BleConfig::GetInstance().SetPasskey(passkey);
243 }
244 
SetBleModel1Level(int level)245 bool BleProperties::SetBleModel1Level(int level)
246 {
247     LOG_DEBUG("[BleProperties] %{public}s:%{public}d", __func__, level);
248 
249     return BleConfig::GetInstance().SetBleModel1Level(level);
250 }
251 
SetBleModel2Level(int level)252 bool BleProperties::SetBleModel2Level(int level)
253 {
254     LOG_DEBUG("[BleProperties] %{public}s:%{public}d", __func__, level);
255 
256     return BleConfig::GetInstance().SetBleModel2Level(level);
257 }
258 
SetBleSecurity(bool security)259 bool BleProperties::SetBleSecurity(bool security)
260 {
261     LOG_DEBUG("[BleProperties] %{public}s:%{public}d", __func__, security);
262 
263     return BleConfig::GetInstance().SetBleSecurity(security);
264 }
265 
SetBleScanMode(int scanmode)266 bool BleProperties::SetBleScanMode(int scanmode)
267 {
268     LOG_DEBUG("[BleProperties] %{public}s:%{public}d", __func__, scanmode);
269 
270     return BleConfig::GetInstance().SetBleScanMode(scanmode);
271 }
272 
SaveDefaultValues() const273 bool BleProperties::SaveDefaultValues() const
274 {
275     LOG_DEBUG("[BleProperties] %{public}s", __func__);
276 
277     bool ret = SetLocalName(BLE_DEFAULT_DEVICE_NAME);
278     ret &= SetIoCapability(BLE_DEFAULT_IO);
279     ret &= SetPasskey(BLE_DEFAULT_LOCAL_PASSKEY);
280     ret &= SetBleRoles(BLE_DEFAULT_ROLES);
281     ret &= SetBleModel1Level(BLE_DEFAULT_MODEL1_LEVEL);
282     ret &= SetBleModel2Level(BLE_DEFAULT_MODEL2_LEVEL);
283     ret &= SetBleSecurity(BLE_DEFAULT_SECURITY);
284     ret &= SetBleScanMode(BLE_DEFAULT_SCAN_MODE);
285     ret &= BleConfig::GetInstance().Save();
286     return ret;
287 }
288 
RegisterBleAdapterObserver(BaseObserverList<IAdapterBleObserver> &observer) const289 void BleProperties::RegisterBleAdapterObserver(BaseObserverList<IAdapterBleObserver> &observer) const
290 {
291     LOG_DEBUG("[BleProperties] %{public}s", __func__);
292 
293     pimpl->observer_ = &observer;
294 }
295 
DeregisterBleAdapterObserver(IAdapterBleObserver &observer) const296 void BleProperties::DeregisterBleAdapterObserver(IAdapterBleObserver &observer) const
297 {
298     LOG_DEBUG("[BleProperties] %{public}s", __func__);
299 
300     pimpl->observer_->Deregister(observer);
301 }
302 }  // namespace bluetooth
303 }  // namespace OHOS
304