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
16 #include "bluetooth_address.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <hdf_log.h>
22 #include <unistd.h>
23 #include "securec.h"
24
25 namespace OHOS {
26 namespace HDI {
27 namespace Bluetooth {
28 namespace Hci {
29 namespace {
30 constexpr int ADDRESS_STR_LEN = 17;
31 constexpr int ADDRESS_SIZE = 6;
32 } // namespace
33
BluetoothAddress()34 BluetoothAddress::BluetoothAddress()
35 {
36 address_.resize(ADDRESS_SIZE);
37 }
38
ParseAddressFromString(const std::string & string) const39 int BluetoothAddress::ParseAddressFromString(const std::string &string) const
40 {
41 size_t offset = 0;
42 int bytesIndex = 0;
43 int readCount = 0;
44 for (bytesIndex = 0; bytesIndex < ADDRESS_SIZE && offset < string.size(); bytesIndex++) {
45 readCount = 0;
46 if (sscanf_s(&string[offset], "%02hhx:%n", &address_[bytesIndex], &readCount) > 0) {
47 if (readCount == 0 && bytesIndex != ADDRESS_SIZE - 1) {
48 return bytesIndex;
49 }
50 offset += readCount;
51 } else {
52 break;
53 }
54 }
55
56 return bytesIndex;
57 }
58
GetDeviceAddress(const std::string & path)59 std::shared_ptr<BluetoothAddress> BluetoothAddress::GetDeviceAddress(const std::string &path)
60 {
61 int addrFd = open(path.c_str(), O_RDONLY);
62 if (addrFd < 0) {
63 HDF_LOGI("open %{public}s failed err:%{public}s.", path.c_str(), strerror(errno));
64 char addressStr[ADDRESS_STR_LEN + 1] = {"00:11:22:33:44:55"};
65 auto ptr = std::make_shared<BluetoothAddress>();
66 if (ptr->ParseAddressFromString(addressStr) != ADDRESS_SIZE) {
67 return nullptr;
68 }
69 return ptr;
70 }
71
72 char addressStr[ADDRESS_STR_LEN + 1] = {0};
73 if (read(addrFd, addressStr, ADDRESS_STR_LEN) != ADDRESS_STR_LEN) {
74 HDF_LOGE("read %s failed.", path.c_str());
75 close(addrFd);
76 return nullptr;
77 }
78 close(addrFd);
79
80 auto ptr = std::make_shared<BluetoothAddress>();
81 if (ptr->ParseAddressFromString(addressStr) != ADDRESS_SIZE) {
82 return nullptr;
83 }
84
85 return ptr;
86 }
87
GenerateDeviceAddress(const std::string & prefix)88 std::shared_ptr<BluetoothAddress> BluetoothAddress::GenerateDeviceAddress(const std::string &prefix)
89 {
90 auto ptr = std::make_shared<BluetoothAddress>();
91 int prefixCount = ptr->ParseAddressFromString(prefix);
92 if (prefixCount < ADDRESS_SIZE) {
93 int fd = open("/dev/urandom", O_RDONLY);
94 if (fd < 0) {
95 HDF_LOGE("open /dev/urandom failed err:%s.", strerror(errno));
96 return nullptr;
97 }
98
99 if (read(fd, &ptr->address_[prefixCount], ADDRESS_SIZE - prefixCount) != ADDRESS_SIZE - prefixCount) {
100 HDF_LOGE("read /dev/urandom failed.");
101 close(fd);
102 return nullptr;
103 }
104 close(fd);
105 }
106
107 return ptr;
108 }
109
ReadAddress(std::vector<uint8_t> & address) const110 void BluetoothAddress::ReadAddress(std::vector<uint8_t> &address) const
111 {
112 address = address_;
113 }
114
ReadAddress(std::string & address) const115 void BluetoothAddress::ReadAddress(std::string &address) const
116 {
117 address.resize(ADDRESS_STR_LEN + 1);
118
119 int offset = 0;
120 for (int ii = 0; ii < ADDRESS_SIZE; ii++) {
121 int ret = snprintf_s(
122 &address[offset], (ADDRESS_STR_LEN + 1) - offset, ADDRESS_STR_LEN - offset, "%02x:", address_[ii]);
123 if (ret < 0) {
124 break;
125 }
126 offset += ret;
127 }
128 }
129 } // namespace Hci
130 } // namespace Bluetooth
131 } // namespace HDI
132 } // namespace OHOS