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 "dh_utils_tool.h"
17
18 #include <iomanip>
19 #include <random>
20 #include <sstream>
21 #include <sys/time.h>
22
23 #include "openssl/sha.h"
24 #include "softbus_bus_center.h"
25
26 #include "constants.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_log.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 constexpr int32_t MS_ONE_SECOND = 1000;
33 constexpr int32_t WORD_WIDTH_8 = 8;
34 constexpr int32_t WORD_WIDTH_4 = 4;
35
GetCurrentTime()36 int64_t GetCurrentTime()
37 {
38 struct timeval tv {
39 0
40 };
41 gettimeofday(&tv, nullptr);
42 return tv.tv_sec * MS_ONE_SECOND + tv.tv_usec / MS_ONE_SECOND;
43 }
44
GetRandomID()45 std::string GetRandomID()
46 {
47 static std::random_device rd;
48 static std::uniform_int_distribution<uint64_t> dist(0ULL, 0xFFFFFFFFFFFFFFFFULL);
49 uint64_t ab = dist(rd);
50 uint64_t cd = dist(rd);
51 uint32_t a, b, c, d;
52 std::stringstream ss;
53 ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
54 cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
55 a = (ab >> 32U);
56 b = (ab & 0xFFFFFFFFU);
57 c = (cd >> 32U);
58 d = (cd & 0xFFFFFFFFU);
59 ss << std::hex << std::nouppercase << std::setfill('0');
60 ss << std::setw(WORD_WIDTH_8) << (a);
61 ss << std::setw(WORD_WIDTH_4) << (b >> 16U);
62 ss << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU);
63 ss << std::setw(WORD_WIDTH_4) << (c >> 16U);
64 ss << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU);
65 ss << std::setw(WORD_WIDTH_8) << d;
66
67 return ss.str();
68 }
69
GetUUIDBySoftBus(const std::string & networkId)70 std::string GetUUIDBySoftBus(const std::string &networkId)
71 {
72 if (networkId.empty()) {
73 return "";
74 }
75 char uuid[UUID_BUF_LEN] = {0};
76 auto ret = GetNodeKeyInfo(DH_FWK_PKG_NAME.c_str(), networkId.c_str(), NodeDeviceInfoKey::NODE_KEY_UUID,
77 reinterpret_cast<uint8_t *>(uuid), UUID_BUF_LEN);
78 return (ret == DH_FWK_SUCCESS) ? std::string(uuid) : "";
79 }
80
GetDeviceIdByUUID(const std::string & uuid)81 std::string GetDeviceIdByUUID(const std::string &uuid)
82 {
83 unsigned char hash[SHA256_DIGEST_LENGTH * 2 + 1] = {0};
84 SHA256_CTX ctx;
85 SHA256_Init(&ctx);
86 SHA256_Update(&ctx, uuid.data(), uuid.size());
87 SHA256_Final(&hash[SHA256_DIGEST_LENGTH], &ctx);
88 // here we translate sha256 hash to hexadecimal. each 8-bit char will be presented by two characters([0-9a-f])
89 constexpr int32_t WIDTH = 4;
90 constexpr unsigned char MASK = 0x0F;
91 const char* hexCode = "0123456789abcdef";
92 constexpr int32_t DOUBLE_TIMES = 2;
93 for (int32_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
94 unsigned char value = hash[SHA256_DIGEST_LENGTH + i];
95 // uint8_t is 2 digits in hexadecimal.
96 hash[i * DOUBLE_TIMES] = hexCode[(value >> WIDTH) & MASK];
97 hash[i * DOUBLE_TIMES + 1] = hexCode[value & MASK];
98 }
99 hash[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0;
100 return reinterpret_cast<char*>(hash);
101 }
102
GetLocalDeviceInfo()103 DeviceInfo GetLocalDeviceInfo()
104 {
105 DeviceInfo devInfo { "", "", "", 0 };
106 auto info = std::make_unique<NodeBasicInfo>();
107 auto ret = GetLocalNodeDeviceInfo(DH_FWK_PKG_NAME.c_str(), info.get());
108 if (ret != DH_FWK_SUCCESS) {
109 DHLOGE("GetLocalNodeDeviceInfo failed, errCode = %d", ret);
110 return devInfo;
111 }
112 devInfo.uuid = GetUUIDBySoftBus(info->networkId);
113 devInfo.deviceId = GetDeviceIdByUUID(devInfo.uuid);
114 devInfo.deviceName = info->deviceName;
115 devInfo.deviceType = info->deviceTypeId;
116 return devInfo;
117 }
118 }
119 }
120