1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#ifndef UTIL_H
17f857971dSopenharmony_ci#define UTIL_H
18f857971dSopenharmony_ci
19f857971dSopenharmony_ci#include <limits>
20f857971dSopenharmony_ci#include <string>
21f857971dSopenharmony_ci#include <vector>
22f857971dSopenharmony_ci
23f857971dSopenharmony_ci#include <sys/types.h>
24f857971dSopenharmony_ci
25f857971dSopenharmony_cinamespace OHOS {
26f857971dSopenharmony_cinamespace Msdp {
27f857971dSopenharmony_cinamespace DeviceStatus {
28f857971dSopenharmony_cienum class BizState {
29f857971dSopenharmony_ci    STATE_IDLE = 0,
30f857971dSopenharmony_ci    STATE_BEGIN = 1,
31f857971dSopenharmony_ci    STATE_END = 2
32f857971dSopenharmony_ci};
33f857971dSopenharmony_ci
34f857971dSopenharmony_cienum class BizStage {
35f857971dSopenharmony_ci    STAGE_START_DRAG = 1,
36f857971dSopenharmony_ci    STAGE_STOP_DRAG,
37f857971dSopenharmony_ci    STAGE_MOTION_DRAGGING,
38f857971dSopenharmony_ci	STAGE_DRAGGING
39f857971dSopenharmony_ci};
40f857971dSopenharmony_ci
41f857971dSopenharmony_cienum class StageRes {
42f857971dSopenharmony_ci    RES_IDLE = 0,
43f857971dSopenharmony_ci	RES_SUCCESS,
44f857971dSopenharmony_ci	RES_FAIL,
45f857971dSopenharmony_ci    RES_CANCEL
46f857971dSopenharmony_ci};
47f857971dSopenharmony_ci
48f857971dSopenharmony_cienum class DragRadarErrCode {
49f857971dSopenharmony_ci    DRAG_SUCCESS = 0,
50f857971dSopenharmony_ci    FAILED_INIT_DRAWING = 61210623,
51f857971dSopenharmony_ci	FAILED_ADD_INPUT_MONITOR,
52f857971dSopenharmony_ci	INVALID_DRAG_DATA,
53f857971dSopenharmony_ci    REPEATE_START_DRAG_EXCEPTION,
54f857971dSopenharmony_ci    FAILED_SET_DRAG_VISIBLE,
55f857971dSopenharmony_ci    FAILED_REMOVE_INPUT_MONITOR,
56f857971dSopenharmony_ci    FAILED_NOTIFY_DRAG_RESULT,
57f857971dSopenharmony_ci    DRAG_STOP_EXCEPTION,
58f857971dSopenharmony_ci    REPEATE_STOP_DRAG_EXCEPTION,
59f857971dSopenharmony_ci    FAILED_SYNC_DATA_FROM_UDMF
60f857971dSopenharmony_ci};
61f857971dSopenharmony_ci
62f857971dSopenharmony_cistruct DragRadarInfo {
63f857971dSopenharmony_ci    std::string funcName;
64f857971dSopenharmony_ci    int32_t bizState { -1 };
65f857971dSopenharmony_ci    int32_t bizStage { -1 };
66f857971dSopenharmony_ci    int32_t stageRes { -1 };
67f857971dSopenharmony_ci    int32_t errCode { -1 };
68f857971dSopenharmony_ci    std::string hostName;
69f857971dSopenharmony_ci    std::string localNetId;
70f857971dSopenharmony_ci    std::string peerNetId;
71f857971dSopenharmony_ci    std::string dragSumary;
72f857971dSopenharmony_ci    std::string callingPid;
73f857971dSopenharmony_ci};
74f857971dSopenharmony_ci
75f857971dSopenharmony_ciint32_t GetPid();
76f857971dSopenharmony_ciconst char* GetProgramName();
77f857971dSopenharmony_ciint64_t GetMillisTime();
78f857971dSopenharmony_ci
79f857971dSopenharmony_ciuint64_t GetThisThreadId();
80f857971dSopenharmony_ci
81f857971dSopenharmony_civoid SetThreadName(const std::string &name);
82f857971dSopenharmony_civoid GetTimeStamp(std::string &startTime);
83f857971dSopenharmony_ci
84f857971dSopenharmony_citemplate<typename T>
85f857971dSopenharmony_cibool AddInt(T op1, T op2, T minValue, T maxValue, T &res)
86f857971dSopenharmony_ci{
87f857971dSopenharmony_ci    if (op1 >= 0) {
88f857971dSopenharmony_ci        if (op2 > maxValue - op1) {
89f857971dSopenharmony_ci            return false;
90f857971dSopenharmony_ci        }
91f857971dSopenharmony_ci    } else {
92f857971dSopenharmony_ci        if (op2 < minValue - op1) {
93f857971dSopenharmony_ci            return false;
94f857971dSopenharmony_ci        }
95f857971dSopenharmony_ci    }
96f857971dSopenharmony_ci    res = op1 + op2;
97f857971dSopenharmony_ci    return true;
98f857971dSopenharmony_ci}
99f857971dSopenharmony_ci
100f857971dSopenharmony_ciinline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
101f857971dSopenharmony_ci{
102f857971dSopenharmony_ci    return AddInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
103f857971dSopenharmony_ci}
104f857971dSopenharmony_ci
105f857971dSopenharmony_ciinline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
106f857971dSopenharmony_ci{
107f857971dSopenharmony_ci    return AddInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
108f857971dSopenharmony_ci}
109f857971dSopenharmony_ci
110f857971dSopenharmony_citemplate<typename T>
111f857971dSopenharmony_cibool MultiplyInt(T op1, T op2, T minVal, T maxVal, T &res)
112f857971dSopenharmony_ci{
113f857971dSopenharmony_ci    if (op1 > 0) {
114f857971dSopenharmony_ci        if (op2 > 0) {
115f857971dSopenharmony_ci            if (op1 > maxVal / op2) {
116f857971dSopenharmony_ci                return false;
117f857971dSopenharmony_ci            }
118f857971dSopenharmony_ci        } else {
119f857971dSopenharmony_ci            if (op2 < minVal / op1) {
120f857971dSopenharmony_ci                return false;
121f857971dSopenharmony_ci            }
122f857971dSopenharmony_ci        }
123f857971dSopenharmony_ci    } else {
124f857971dSopenharmony_ci        if (op2 > 0) {
125f857971dSopenharmony_ci            if (op1 < minVal / op2) {
126f857971dSopenharmony_ci                return false;
127f857971dSopenharmony_ci            }
128f857971dSopenharmony_ci        } else {
129f857971dSopenharmony_ci            if (op1 != 0 && op2 < maxVal / op1) {
130f857971dSopenharmony_ci                return false;
131f857971dSopenharmony_ci            }
132f857971dSopenharmony_ci        }
133f857971dSopenharmony_ci    }
134f857971dSopenharmony_ci    res = op1 * op2;
135f857971dSopenharmony_ci    return true;
136f857971dSopenharmony_ci}
137f857971dSopenharmony_ci
138f857971dSopenharmony_ciinline bool MultiplyInt32(int32_t op1, int32_t op2, int32_t& res)
139f857971dSopenharmony_ci{
140f857971dSopenharmony_ci    return MultiplyInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
141f857971dSopenharmony_ci}
142f857971dSopenharmony_ci
143f857971dSopenharmony_ciinline bool MultiplyInt64(int64_t op1, int64_t op2, int64_t& res)
144f857971dSopenharmony_ci{
145f857971dSopenharmony_ci    return MultiplyInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
146f857971dSopenharmony_ci}
147f857971dSopenharmony_ci
148f857971dSopenharmony_cisize_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
149f857971dSopenharmony_cistd::string GetAnonyString(const std::string &value);
150f857971dSopenharmony_cistd::string StringPrintf(const char *format, ...);
151f857971dSopenharmony_cibool CheckFileExtendName(const std::string &filePath, const std::string &checkExtension);
152f857971dSopenharmony_cibool IsValidPath(const std::string &rootDir, const std::string &filePath);
153f857971dSopenharmony_cibool IsValidSvgPath(const std::string &filePath);
154f857971dSopenharmony_cibool IsValidSvgFile(const std::string &filePath);
155f857971dSopenharmony_cibool IsNum(const std::string &str);
156f857971dSopenharmony_civoid GetRotatePolicy(bool &isScreenRotation, std::vector<std::string> &foldRotatePolicys);
157f857971dSopenharmony_ci} // namespace DeviceStatus
158f857971dSopenharmony_ci} // namespace Msdp
159f857971dSopenharmony_ci} // namespace OHOS
160f857971dSopenharmony_ci#endif // UTIL_H
161