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