• 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 
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 
39 constexpr int START_POS = 6;
40 constexpr int END_POS = 13;
41 constexpr int ADDR_BYTE = 18;
GetEncryptAddr(std::string addr)42 std::string GetEncryptAddr(std::string addr)
43 {
44     if (addr.empty() || addr.length() != ADDRESS_STR_LEN) {
45         HDF_LOGE("addr is invalid.");
46         return std::string("");
47     }
48     std::string tmp = "**:**:**:**:**:**";
49     std::string out = addr;
50     for (int i = START_POS; i <= END_POS; i++) {
51         out[i] = tmp[i];
52     }
53     return out;
54 }
55 
ParseAddressToString(std::vector<uint8_t> & address,std::string & outString)56 void BluetoothAddress::ParseAddressToString(std::vector<uint8_t> &address, std::string &outString)
57 {
58     char temp[ADDR_BYTE] = {0};
59     int ret = sprintf_s(temp, sizeof(temp), "%02X:%02X:%02X:%02X:%02X:%02X",
60         address[0], address[1], address[2], address[3], address[4], address[5]);
61     if (ret == -1) {
62         HDF_LOGE("ConvertAddr sprintf_s return error, ret -1");
63     }
64     outString = temp;
65 }
66 
ParseAddressFromString(const std::string & string) const67 int BluetoothAddress::ParseAddressFromString(const std::string &string) const
68 {
69     size_t offset = 0;
70     int bytesIndex = 0;
71     int readCount = 0;
72     for (bytesIndex = 0; bytesIndex < ADDRESS_SIZE && offset < string.size(); bytesIndex++) {
73         readCount = 0;
74         if (sscanf_s(&string[offset], "%02hhx:%n", &address_[bytesIndex], &readCount) > 0) {
75             if (readCount == 0 && bytesIndex != ADDRESS_SIZE - 1) {
76                 return bytesIndex;
77             }
78             offset += readCount;
79         } else {
80             break;
81         }
82     }
83 
84     return bytesIndex;
85 }
86 
GetDeviceAddress(const std::string & path)87 std::shared_ptr<BluetoothAddress> BluetoothAddress::GetDeviceAddress(const std::string &path)
88 {
89     const int bufsize = 256;
90     char buf[bufsize] = {0};
91     int addrFd = open(path.c_str(), O_RDONLY);
92     if (addrFd < 0) {
93         HDF_LOGI("GetDeviceAddress open %{public}s.", path.c_str());
94         int newFd = open(path.c_str(), O_RDWR | O_CREAT, 00644);
95         HDF_LOGI("GetDeviceAddress open newFd %{public}d.", newFd);
96         char addressStr[ADDRESS_STR_LEN + 1] = {"00:11:22:33:44:55"};
97         auto tmpPtr = GenerateDeviceAddress();
98         std::string strAddress;
99         ParseAddressToString(tmpPtr->address_, strAddress);
100         HDF_LOGI("device mac addr: %{public}s", GetEncryptAddr(strAddress).c_str());
101         int ret = strcpy_s(addressStr, ADDRESS_STR_LEN + 1, strAddress.c_str());
102         if (ret != 0) {
103             HDF_LOGI("ParseAddressToString strcpy_s err!");
104         }
105         if (newFd >= 0) {
106             int fdRet = write(newFd, addressStr, ADDRESS_STR_LEN);
107             if (fdRet < 0) {
108                 strerror_r(errno, buf, sizeof(buf));
109                 HDF_LOGI("GetDeviceAddress addr write failed, err:%{public}s.", buf);
110             }
111             close(newFd);
112         }
113         auto ptr = std::make_shared<BluetoothAddress>();
114         if (ptr->ParseAddressFromString(addressStr) != ADDRESS_SIZE) {
115             return nullptr;
116         }
117         return ptr;
118     }
119 
120     char addressStr[ADDRESS_STR_LEN + 1] = {0};
121     if (read(addrFd, addressStr, ADDRESS_STR_LEN) != ADDRESS_STR_LEN) {
122         HDF_LOGE("read %{public}s failed.", path.c_str());
123         close(addrFd);
124         return nullptr;
125     }
126     close(addrFd);
127 
128     auto ptr = std::make_shared<BluetoothAddress>();
129     if (ptr->ParseAddressFromString(addressStr) != ADDRESS_SIZE) {
130         return nullptr;
131     }
132 
133     return ptr;
134 }
135 
GenerateDeviceAddress(const std::string & prefix)136 std::shared_ptr<BluetoothAddress> BluetoothAddress::GenerateDeviceAddress(const std::string &prefix)
137 {
138     const int bufsize = 256;
139     char buf[bufsize] = {0};
140     auto ptr = std::make_shared<BluetoothAddress>();
141     char addressStr[ADDRESS_STR_LEN + 1] = {"00:11:22:33:44:55"};
142     ptr->ParseAddressFromString(addressStr);
143     int prefixCount = ptr->ParseAddressFromString(prefix);
144     if (prefixCount < ADDRESS_SIZE) {
145         int fd = open("/dev/urandom", O_RDONLY);
146         if (fd < 0) {
147             strerror_r(errno, buf, sizeof(buf));
148             HDF_LOGE("open /dev/urandom failed err:%{public}s.", buf);
149             return ptr;
150         }
151         if (read(fd, &ptr->address_[prefixCount], ADDRESS_SIZE - prefixCount) != ADDRESS_SIZE - prefixCount) {
152             strerror_r(errno, buf, sizeof(buf));
153             HDF_LOGE("read /dev/urandom failed err:%{public}s.", buf);
154         }
155         close(fd);
156     }
157     return ptr;
158 }
159 
ReadAddress(std::vector<uint8_t> & address) const160 void BluetoothAddress::ReadAddress(std::vector<uint8_t> &address) const
161 {
162     address = address_;
163 }
164 
ReadAddress(std::string & address) const165 void BluetoothAddress::ReadAddress(std::string &address) const
166 {
167     address.resize(ADDRESS_STR_LEN + 1);
168 
169     int offset = 0;
170     for (int ii = 0; ii < ADDRESS_SIZE; ii++) {
171         int ret = snprintf_s(
172             &address[offset], (ADDRESS_STR_LEN + 1) - offset, ADDRESS_STR_LEN - offset, "%02x:", address_[ii]);
173         if (ret < 0) {
174             break;
175         }
176         offset += ret;
177     }
178 }
179 }  // namespace Hci
180 }  // namespace Bluetooth
181 }  // namespace HDI
182 }  // namespace OHOS