1/*
2 * Copyright (c) 2022-2023 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#ifndef UTIL_H
17#define UTIL_H
18
19#include <limits>
20#include <string>
21#include <vector>
22
23#include <sys/types.h>
24
25namespace OHOS {
26namespace Msdp {
27namespace DeviceStatus {
28enum class BizState {
29    STATE_IDLE = 0,
30    STATE_BEGIN = 1,
31    STATE_END = 2
32};
33
34enum class BizStage {
35    STAGE_START_DRAG = 1,
36    STAGE_STOP_DRAG,
37    STAGE_MOTION_DRAGGING,
38	STAGE_DRAGGING
39};
40
41enum class StageRes {
42    RES_IDLE = 0,
43	RES_SUCCESS,
44	RES_FAIL,
45    RES_CANCEL
46};
47
48enum class DragRadarErrCode {
49    DRAG_SUCCESS = 0,
50    FAILED_INIT_DRAWING = 61210623,
51	FAILED_ADD_INPUT_MONITOR,
52	INVALID_DRAG_DATA,
53    REPEATE_START_DRAG_EXCEPTION,
54    FAILED_SET_DRAG_VISIBLE,
55    FAILED_REMOVE_INPUT_MONITOR,
56    FAILED_NOTIFY_DRAG_RESULT,
57    DRAG_STOP_EXCEPTION,
58    REPEATE_STOP_DRAG_EXCEPTION,
59    FAILED_SYNC_DATA_FROM_UDMF
60};
61
62struct DragRadarInfo {
63    std::string funcName;
64    int32_t bizState { -1 };
65    int32_t bizStage { -1 };
66    int32_t stageRes { -1 };
67    int32_t errCode { -1 };
68    std::string hostName;
69    std::string localNetId;
70    std::string peerNetId;
71    std::string dragSumary;
72    std::string callingPid;
73};
74
75int32_t GetPid();
76const char* GetProgramName();
77int64_t GetMillisTime();
78
79uint64_t GetThisThreadId();
80
81void SetThreadName(const std::string &name);
82void GetTimeStamp(std::string &startTime);
83
84template<typename T>
85bool AddInt(T op1, T op2, T minValue, T maxValue, T &res)
86{
87    if (op1 >= 0) {
88        if (op2 > maxValue - op1) {
89            return false;
90        }
91    } else {
92        if (op2 < minValue - op1) {
93            return false;
94        }
95    }
96    res = op1 + op2;
97    return true;
98}
99
100inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
101{
102    return AddInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
103}
104
105inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
106{
107    return AddInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
108}
109
110template<typename T>
111bool MultiplyInt(T op1, T op2, T minVal, T maxVal, T &res)
112{
113    if (op1 > 0) {
114        if (op2 > 0) {
115            if (op1 > maxVal / op2) {
116                return false;
117            }
118        } else {
119            if (op2 < minVal / op1) {
120                return false;
121            }
122        }
123    } else {
124        if (op2 > 0) {
125            if (op1 < minVal / op2) {
126                return false;
127            }
128        } else {
129            if (op1 != 0 && op2 < maxVal / op1) {
130                return false;
131            }
132        }
133    }
134    res = op1 * op2;
135    return true;
136}
137
138inline bool MultiplyInt32(int32_t op1, int32_t op2, int32_t& res)
139{
140    return MultiplyInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
141}
142
143inline bool MultiplyInt64(int64_t op1, int64_t op2, int64_t& res)
144{
145    return MultiplyInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
146}
147
148size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
149std::string GetAnonyString(const std::string &value);
150std::string StringPrintf(const char *format, ...);
151bool CheckFileExtendName(const std::string &filePath, const std::string &checkExtension);
152bool IsValidPath(const std::string &rootDir, const std::string &filePath);
153bool IsValidSvgPath(const std::string &filePath);
154bool IsValidSvgFile(const std::string &filePath);
155bool IsNum(const std::string &str);
156void GetRotatePolicy(bool &isScreenRotation, std::vector<std::string> &foldRotatePolicys);
157} // namespace DeviceStatus
158} // namespace Msdp
159} // namespace OHOS
160#endif // UTIL_H
161