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