• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "dscreen_util.h"
17 
18 #include <algorithm>
19 #include <cstddef>
20 #include <iomanip>
21 #include <random>
22 #include <sstream>
23 #include <sys/time.h>
24 
25 #include "softbus_bus_center.h"
26 
27 #include "dscreen_constants.h"
28 #include "dscreen_errcode.h"
29 #include "dscreen_log.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 constexpr int32_t WORD_WIDTH_8 = 8;
34 constexpr int32_t WORD_WIDTH_4 = 4;
35 
GetLocalDeviceNetworkId(std::string & networkId)36 int32_t GetLocalDeviceNetworkId(std::string &networkId)
37 {
38     NodeBasicInfo basicInfo = { { 0 } };
39     int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo);
40     if (ret != DH_SUCCESS) {
41         DHLOGE("GetLocalDeviceNetworkId failed ret: %d", ret);
42         return ret;
43     }
44 
45     networkId = std::string(basicInfo.networkId);
46     return DH_SUCCESS;
47 }
48 
GetRandomID()49 std::string GetRandomID()
50 {
51     static std::random_device rd;
52     static std::uniform_int_distribution<uint64_t> dist(0ULL, 0xFFFFFFFFFFFFFFFFULL);
53     uint64_t ab = dist(rd);
54     uint64_t cd = dist(rd);
55     uint32_t a, b, c, d;
56     std::stringstream ss;
57     ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
58     cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
59     a = (ab >> 32U);
60     b = (ab & 0xFFFFFFFFU);
61     c = (cd >> 32U);
62     d = (cd & 0xFFFFFFFFU);
63     ss << std::hex << std::nouppercase << std::setfill('0');
64     ss << std::setw(WORD_WIDTH_8) << (a);
65     ss << std::setw(WORD_WIDTH_4) << (b >> 16U);
66     ss << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU);
67     ss << std::setw(WORD_WIDTH_4) << (c >> 16U);
68     ss << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU);
69     ss << std::setw(WORD_WIDTH_8) << d;
70 
71     return ss.str();
72 }
73 
GetAnonyString(const std::string & value)74 std::string GetAnonyString(const std::string &value)
75 {
76     constexpr size_t INT32_SHORT_ID_LENGTH = 20;
77     constexpr size_t INT32_MIN_ID_LENGTH = 3;
78     std::string res;
79     std::string tmpStr("******");
80     size_t strLen = value.length();
81     if (strLen < INT32_MIN_ID_LENGTH) {
82         return tmpStr;
83     }
84 
85     if (strLen <= INT32_SHORT_ID_LENGTH) {
86         res += value[0];
87         res += tmpStr;
88         res += value[strLen - 1];
89     } else {
90         constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
91         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
92         res += tmpStr;
93         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
94     }
95 
96     return res;
97 }
98 
GetInterruptString(const std::string & value)99 std::string GetInterruptString(const std::string &value)
100 {
101     constexpr size_t INT32_MIN_ID_LENGTH = 3;
102     constexpr size_t STRING_HALF_LENGTH = 2;
103     std::string res;
104     size_t strlen = value.length();
105     if (strlen <= INT32_MIN_ID_LENGTH) {
106         res = value;
107     } else  {
108         res = value.substr(0, strlen / STRING_HALF_LENGTH);
109     }
110 
111     return res;
112 }
113 } // namespace DistributedHardware
114 } // namespace OHOS