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