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 BizLatencyStage {
49 STAGE_LATENCY = 1,
50 };
51
52 enum class DragRadarErrCode {
53 DRAG_SUCCESS = 0,
54 FAILED_INIT_DRAWING = 61210623,
55 FAILED_ADD_INPUT_MONITOR,
56 INVALID_DRAG_DATA,
57 REPEATE_START_DRAG_EXCEPTION,
58 FAILED_SET_DRAG_VISIBLE,
59 FAILED_REMOVE_INPUT_MONITOR,
60 FAILED_NOTIFY_DRAG_RESULT,
61 DRAG_STOP_EXCEPTION,
62 REPEATE_STOP_DRAG_EXCEPTION,
63 FAILED_SYNC_DATA_FROM_UDMF,
64 DRAG_STOP_CANCEL,
65 FAILED_APPEND_EXTRA_DATA
66 };
67
68 struct DragRadarInfo {
69 std::string funcName;
70 int32_t bizState { -1 };
71 int32_t bizStage { -1 };
72 int32_t stageRes { -1 };
73 int32_t errCode { -1 };
74 std::string hostName;
75 std::string localNetId;
76 std::string peerNetId;
77 std::string dragSumary;
78 std::string callingPid;
79 std::string packageName;
80 std::string appVersionId;
81 std::string appCallee;
82 std::string appCaller;
83 int32_t dragNum { -1 };
84 };
85
86 int32_t GetPid();
87 const char* GetProgramName();
88 int64_t GetMillisTime();
89
90 uint64_t GetThisThreadId();
91
92 void SetThreadName(const std::string &name);
93 void GetTimeStamp(std::string &startTime);
94
95 template<typename T>
AddInt(T op1,T op2,T minValue,T maxValue,T & res)96 bool AddInt(T op1, T op2, T minValue, T maxValue, T &res)
97 {
98 if (op1 >= 0) {
99 if (op2 > maxValue - op1) {
100 return false;
101 }
102 } else {
103 if (op2 < minValue - op1) {
104 return false;
105 }
106 }
107 res = op1 + op2;
108 return true;
109 }
110
AddInt32(int32_t op1,int32_t op2,int32_t & res)111 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
112 {
113 return AddInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
114 }
115
AddInt64(int64_t op1,int64_t op2,int64_t & res)116 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
117 {
118 return AddInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
119 }
120
121 template<typename T>
MultiplyInt(T op1,T op2,T minVal,T maxVal,T & res)122 bool MultiplyInt(T op1, T op2, T minVal, T maxVal, T &res)
123 {
124 if (op1 > 0) {
125 if (op2 > 0) {
126 if (op1 > maxVal / op2) {
127 return false;
128 }
129 } else {
130 if (op2 < minVal / op1) {
131 return false;
132 }
133 }
134 } else {
135 if (op2 > 0) {
136 if (op1 < minVal / op2) {
137 return false;
138 }
139 } else {
140 if (op1 != 0 && op2 < maxVal / op1) {
141 return false;
142 }
143 }
144 }
145 res = op1 * op2;
146 return true;
147 }
148
MultiplyInt32(int32_t op1,int32_t op2,int32_t & res)149 inline bool MultiplyInt32(int32_t op1, int32_t op2, int32_t& res)
150 {
151 return MultiplyInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
152 }
153
MultiplyInt64(int64_t op1,int64_t op2,int64_t & res)154 inline bool MultiplyInt64(int64_t op1, int64_t op2, int64_t& res)
155 {
156 return MultiplyInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
157 }
158
159 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
160 std::string GetAnonyString(const std::string &value);
161 std::string StringPrintf(const char *format, ...);
162 bool CheckFileExtendName(const std::string &filePath, const std::string &checkExtension);
163 bool IsValidPath(const std::string &rootDir, const std::string &filePath);
164 bool IsValidSvgPath(const std::string &filePath);
165 bool IsValidSvgFile(const std::string &filePath);
166 bool IsNum(const std::string &str);
167 void GetRotatePolicy(bool &isScreenRotation, std::vector<std::string> &foldRotatePolicys);
168 bool IsSecondaryDevice();
169 bool IsValidJsonPath(const std::string &filePath);
170 bool IsFileExists(const std::string &fileName);
171 std::string ReadFile(const std::string &filePath);
172 std::string ReadJsonFile(const std::string &filePath);
173 } // namespace DeviceStatus
174 } // namespace Msdp
175 } // namespace OHOS
176 #endif // UTIL_H
177