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
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 enum class BizState {
29 STATE_IDLE = 0,
30 STATE_BEGIN = 1,
31 STATE_END = 2
32 };
33
34 enum class BizStage {
35 STAGE_START_DRAG = 1,
36 STAGE_STOP_DRAG,
37 STAGE_MOTION_DRAGGING,
38 STAGE_DRAGGING
39 };
40
41 enum class StageRes {
42 RES_IDLE = 0,
43 RES_SUCCESS,
44 RES_FAIL,
45 RES_CANCEL
46 };
47
48 enum 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
62 struct 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 std::string packageName;
74 std::string appVersionId;
75 };
76
77 int32_t GetPid();
78 const char* GetProgramName();
79 int64_t GetMillisTime();
80
81 uint64_t GetThisThreadId();
82
83 void SetThreadName(const std::string &name);
84 void GetTimeStamp(std::string &startTime);
85
86 template<typename T>
AddInt(T op1,T op2,T minValue,T maxValue,T & res)87 bool AddInt(T op1, T op2, T minValue, T maxValue, T &res)
88 {
89 if (op1 >= 0) {
90 if (op2 > maxValue - op1) {
91 return false;
92 }
93 } else {
94 if (op2 < minValue - op1) {
95 return false;
96 }
97 }
98 res = op1 + op2;
99 return true;
100 }
101
AddInt32(int32_t op1,int32_t op2,int32_t & res)102 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
103 {
104 return AddInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
105 }
106
AddInt64(int64_t op1,int64_t op2,int64_t & res)107 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
108 {
109 return AddInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
110 }
111
112 template<typename T>
MultiplyInt(T op1,T op2,T minVal,T maxVal,T & res)113 bool MultiplyInt(T op1, T op2, T minVal, T maxVal, T &res)
114 {
115 if (op1 > 0) {
116 if (op2 > 0) {
117 if (op1 > maxVal / op2) {
118 return false;
119 }
120 } else {
121 if (op2 < minVal / op1) {
122 return false;
123 }
124 }
125 } else {
126 if (op2 > 0) {
127 if (op1 < minVal / op2) {
128 return false;
129 }
130 } else {
131 if (op1 != 0 && op2 < maxVal / op1) {
132 return false;
133 }
134 }
135 }
136 res = op1 * op2;
137 return true;
138 }
139
MultiplyInt32(int32_t op1,int32_t op2,int32_t & res)140 inline bool MultiplyInt32(int32_t op1, int32_t op2, int32_t& res)
141 {
142 return MultiplyInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
143 }
144
MultiplyInt64(int64_t op1,int64_t op2,int64_t & res)145 inline bool MultiplyInt64(int64_t op1, int64_t op2, int64_t& res)
146 {
147 return MultiplyInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
148 }
149
150 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
151 std::string GetAnonyString(const std::string &value);
152 std::string StringPrintf(const char *format, ...);
153 bool CheckFileExtendName(const std::string &filePath, const std::string &checkExtension);
154 bool IsValidPath(const std::string &rootDir, const std::string &filePath);
155 bool IsValidSvgPath(const std::string &filePath);
156 bool IsValidSvgFile(const std::string &filePath);
157 bool IsNum(const std::string &str);
158 void GetRotatePolicy(bool &isScreenRotation, std::vector<std::string> &foldRotatePolicys);
159 } // namespace DeviceStatus
160 } // namespace Msdp
161 } // namespace OHOS
162 #endif // UTIL_H
163