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