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 #include "parameter.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 constexpr int32_t WORD_WIDTH_8 = 8;
35 constexpr int32_t WORD_WIDTH_4 = 4;
36
GetLocalDeviceNetworkId(std::string & networkId)37 int32_t GetLocalDeviceNetworkId(std::string &networkId)
38 {
39 NodeBasicInfo basicInfo = { { 0 } };
40 int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo);
41 if (ret != DH_SUCCESS) {
42 DHLOGE("GetLocalDeviceNetworkId failed ret: %" PRId32, ret);
43 return ret;
44 }
45
46 networkId = std::string(basicInfo.networkId);
47 return DH_SUCCESS;
48 }
49
GetRandomID()50 std::string GetRandomID()
51 {
52 static std::random_device rd;
53 static std::uniform_int_distribution<uint64_t> dist(0ULL, 0xFFFFFFFFFFFFFFFFULL);
54 uint64_t ab = dist(rd);
55 uint64_t cd = dist(rd);
56 uint32_t a, b, c, d;
57 std::stringstream ss;
58 ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
59 cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
60 a = (ab >> 32U);
61 b = (ab & 0xFFFFFFFFU);
62 c = (cd >> 32U);
63 d = (cd & 0xFFFFFFFFU);
64 ss << std::hex << std::nouppercase << std::setfill('0');
65 ss << std::setw(WORD_WIDTH_8) << (a);
66 ss << std::setw(WORD_WIDTH_4) << (b >> 16U);
67 ss << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU);
68 ss << std::setw(WORD_WIDTH_4) << (c >> 16U);
69 ss << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU);
70 ss << std::setw(WORD_WIDTH_8) << d;
71
72 return ss.str();
73 }
74
GetAnonyString(const std::string & value)75 std::string GetAnonyString(const std::string &value)
76 {
77 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
78 constexpr size_t INT32_MIN_ID_LENGTH = 3;
79 std::string res;
80 std::string tmpStr("******");
81 size_t strLen = value.length();
82 if (strLen < INT32_MIN_ID_LENGTH) {
83 return tmpStr;
84 }
85
86 if (strLen <= INT32_SHORT_ID_LENGTH) {
87 res += value[0];
88 res += tmpStr;
89 res += value[strLen - 1];
90 } else {
91 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
92 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
93 res += tmpStr;
94 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
95 }
96
97 return res;
98 }
99
GetInterruptString(const std::string & value)100 std::string GetInterruptString(const std::string &value)
101 {
102 constexpr size_t INT32_MIN_ID_LENGTH = 3;
103 constexpr size_t STRING_HALF_LENGTH = 2;
104 std::string res;
105 size_t strlen = value.length();
106 if (strlen <= INT32_MIN_ID_LENGTH) {
107 res = value;
108 } else {
109 res = value.substr(0, strlen / STRING_HALF_LENGTH);
110 }
111
112 return res;
113 }
114
IsPartialRefreshEnabled()115 bool IsPartialRefreshEnabled()
116 {
117 char tempValue[SYSTEM_PARAM_VALUE_SIZE] = {0};
118 auto ret = GetParameter(PARTIAL_REFRESH_PARAM, "-1", tempValue, sizeof(tempValue));
119 if (ret <= 0) {
120 DHLOGE("get system parameter (dscreen.partial.refresh.enable) failed, ret=%" PRId32, ret);
121 return false;
122 }
123 DHLOGI("get system parameter (dscreen.partial.refresh.enable) success, param value = %s", tempValue);
124 return (std::atoi(tempValue) == PARTIAL_REFRESH_ENABLED_VALUE);
125 }
126
IsSupportAVTransEngine(const std::string & version)127 bool IsSupportAVTransEngine(const std::string &version)
128 {
129 return (std::atoi(version.c_str()) >= AV_TRANS_SUPPORTED_VERSION) && !IsPartialRefreshEnabled();
130 }
131 } // namespace DistributedHardware
132 } // namespace OHOS