166f3657fSopenharmony_ci/*
266f3657fSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
366f3657fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
466f3657fSopenharmony_ci * you may not use this file except in compliance with the License.
566f3657fSopenharmony_ci * You may obtain a copy of the License at
666f3657fSopenharmony_ci *
766f3657fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
866f3657fSopenharmony_ci *
966f3657fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1066f3657fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1166f3657fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1266f3657fSopenharmony_ci * See the License for the specific language governing permissions and
1366f3657fSopenharmony_ci * limitations under the License.
1466f3657fSopenharmony_ci */
1566f3657fSopenharmony_ci
1666f3657fSopenharmony_ci#include "dscreen_util.h"
1766f3657fSopenharmony_ci
1866f3657fSopenharmony_ci#include <algorithm>
1966f3657fSopenharmony_ci#include <cstddef>
2066f3657fSopenharmony_ci#include <iomanip>
2166f3657fSopenharmony_ci#include <random>
2266f3657fSopenharmony_ci#include <sstream>
2366f3657fSopenharmony_ci#include <sys/time.h>
2466f3657fSopenharmony_ci
2566f3657fSopenharmony_ci#include "softbus_bus_center.h"
2666f3657fSopenharmony_ci
2766f3657fSopenharmony_ci#include "dscreen_constants.h"
2866f3657fSopenharmony_ci#include "dscreen_errcode.h"
2966f3657fSopenharmony_ci#include "dscreen_log.h"
3066f3657fSopenharmony_ci#include "parameter.h"
3166f3657fSopenharmony_ci
3266f3657fSopenharmony_cinamespace OHOS {
3366f3657fSopenharmony_cinamespace DistributedHardware {
3466f3657fSopenharmony_ciconstexpr int32_t WORD_WIDTH_8 = 8;
3566f3657fSopenharmony_ciconstexpr int32_t WORD_WIDTH_4 = 4;
3666f3657fSopenharmony_ci
3766f3657fSopenharmony_ciuint64_t GetCurrentTimeUs()
3866f3657fSopenharmony_ci{
3966f3657fSopenharmony_ci    constexpr int32_t usOneSecond = 1000 * 1000;
4066f3657fSopenharmony_ci    struct timeval tv;
4166f3657fSopenharmony_ci    gettimeofday(&tv, nullptr);
4266f3657fSopenharmony_ci    return tv.tv_sec * usOneSecond + tv.tv_usec;
4366f3657fSopenharmony_ci}
4466f3657fSopenharmony_ci
4566f3657fSopenharmony_ciint32_t GetLocalDeviceNetworkId(std::string &networkId)
4666f3657fSopenharmony_ci{
4766f3657fSopenharmony_ci    NodeBasicInfo basicInfo = { { 0 } };
4866f3657fSopenharmony_ci    int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo);
4966f3657fSopenharmony_ci    if (ret != DH_SUCCESS) {
5066f3657fSopenharmony_ci        DHLOGE("GetLocalDeviceNetworkId failed ret: %{public}" PRId32, ret);
5166f3657fSopenharmony_ci        return ret;
5266f3657fSopenharmony_ci    }
5366f3657fSopenharmony_ci
5466f3657fSopenharmony_ci    networkId = std::string(basicInfo.networkId);
5566f3657fSopenharmony_ci    return DH_SUCCESS;
5666f3657fSopenharmony_ci}
5766f3657fSopenharmony_ci
5866f3657fSopenharmony_cistd::string GetRandomID()
5966f3657fSopenharmony_ci{
6066f3657fSopenharmony_ci    static std::random_device randomDevice;
6166f3657fSopenharmony_ci    static std::uniform_int_distribution<uint64_t> dist(0ULL, 0xFFFFFFFFFFFFFFFFULL);
6266f3657fSopenharmony_ci    uint64_t ab = dist(randomDevice);
6366f3657fSopenharmony_ci    uint64_t cd = dist(randomDevice);
6466f3657fSopenharmony_ci    uint32_t a;
6566f3657fSopenharmony_ci    uint32_t b;
6666f3657fSopenharmony_ci    uint32_t c;
6766f3657fSopenharmony_ci    uint32_t d;
6866f3657fSopenharmony_ci    std::stringstream stringStream;
6966f3657fSopenharmony_ci    ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
7066f3657fSopenharmony_ci    cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
7166f3657fSopenharmony_ci    a = (ab >> 32U);
7266f3657fSopenharmony_ci    b = (ab & 0xFFFFFFFFU);
7366f3657fSopenharmony_ci    c = (cd >> 32U);
7466f3657fSopenharmony_ci    d = (cd & 0xFFFFFFFFU);
7566f3657fSopenharmony_ci    stringStream << std::hex << std::nouppercase << std::setfill('0');
7666f3657fSopenharmony_ci    stringStream << std::setw(WORD_WIDTH_8) << (a);
7766f3657fSopenharmony_ci    stringStream << std::setw(WORD_WIDTH_4) << (b >> 16U);
7866f3657fSopenharmony_ci    stringStream << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU);
7966f3657fSopenharmony_ci    stringStream << std::setw(WORD_WIDTH_4) << (c >> 16U);
8066f3657fSopenharmony_ci    stringStream << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU);
8166f3657fSopenharmony_ci    stringStream << std::setw(WORD_WIDTH_8) << d;
8266f3657fSopenharmony_ci
8366f3657fSopenharmony_ci    return stringStream.str();
8466f3657fSopenharmony_ci}
8566f3657fSopenharmony_ci
8666f3657fSopenharmony_cistd::string GetAnonyString(const std::string &value)
8766f3657fSopenharmony_ci{
8866f3657fSopenharmony_ci    constexpr size_t int32ShortIdLength = 20;
8966f3657fSopenharmony_ci    constexpr size_t int32MinIdLength = 3;
9066f3657fSopenharmony_ci    std::string result;
9166f3657fSopenharmony_ci    std::string tmpStr("******");
9266f3657fSopenharmony_ci    size_t strLen = value.length();
9366f3657fSopenharmony_ci    if (strLen < int32MinIdLength) {
9466f3657fSopenharmony_ci        return tmpStr;
9566f3657fSopenharmony_ci    }
9666f3657fSopenharmony_ci
9766f3657fSopenharmony_ci    if (strLen <= int32ShortIdLength) {
9866f3657fSopenharmony_ci        result += value[0];
9966f3657fSopenharmony_ci        result += tmpStr;
10066f3657fSopenharmony_ci        result += value[strLen - 1];
10166f3657fSopenharmony_ci    } else {
10266f3657fSopenharmony_ci        constexpr size_t int32PlainTextLength = 4;
10366f3657fSopenharmony_ci        result.append(value, 0, int32PlainTextLength);
10466f3657fSopenharmony_ci        result += tmpStr;
10566f3657fSopenharmony_ci        result.append(value, strLen - int32PlainTextLength, int32PlainTextLength);
10666f3657fSopenharmony_ci    }
10766f3657fSopenharmony_ci
10866f3657fSopenharmony_ci    return result;
10966f3657fSopenharmony_ci}
11066f3657fSopenharmony_ci
11166f3657fSopenharmony_cistd::string GetInterruptString(const std::string &value)
11266f3657fSopenharmony_ci{
11366f3657fSopenharmony_ci    constexpr size_t int32MinIdLength = 3;
11466f3657fSopenharmony_ci    constexpr size_t stringHalfLength = 2;
11566f3657fSopenharmony_ci    std::string res;
11666f3657fSopenharmony_ci    size_t strlen = value.length();
11766f3657fSopenharmony_ci    if (strlen <= int32MinIdLength) {
11866f3657fSopenharmony_ci        res = value;
11966f3657fSopenharmony_ci    } else  {
12066f3657fSopenharmony_ci        res = value.substr(0, strlen / stringHalfLength);
12166f3657fSopenharmony_ci    }
12266f3657fSopenharmony_ci
12366f3657fSopenharmony_ci    return res;
12466f3657fSopenharmony_ci}
12566f3657fSopenharmony_ci
12666f3657fSopenharmony_cibool IsPartialRefreshEnabled()
12766f3657fSopenharmony_ci{
12866f3657fSopenharmony_ci    char tempValue[SYSTEM_PARAM_VALUE_SIZE] = {0};
12966f3657fSopenharmony_ci    auto ret = GetParameter(PARTIAL_REFRESH_PARAM, "-1", tempValue, sizeof(tempValue));
13066f3657fSopenharmony_ci    if (ret <= 0) {
13166f3657fSopenharmony_ci        DHLOGE("get system parameter (dscreen.partial.refresh.enable) failed, ret=%{public}" PRId32, ret);
13266f3657fSopenharmony_ci        return false;
13366f3657fSopenharmony_ci    }
13466f3657fSopenharmony_ci    DHLOGI("get system parameter (dscreen.partial.refresh.enable) success, param value = %{public}s", tempValue);
13566f3657fSopenharmony_ci    return (std::atoi(tempValue) == PARTIAL_REFRESH_ENABLED_VALUE);
13666f3657fSopenharmony_ci}
13766f3657fSopenharmony_ci
13866f3657fSopenharmony_cibool IsSupportAVTransEngine(const std::string &version)
13966f3657fSopenharmony_ci{
14066f3657fSopenharmony_ci    return (std::atoi(version.c_str()) >= AV_TRANS_SUPPORTED_VERSION) && !IsPartialRefreshEnabled();
14166f3657fSopenharmony_ci}
14266f3657fSopenharmony_ci} // namespace DistributedHardware
14366f3657fSopenharmony_ci} // namespace OHOS