1 /*
2 * Copyright (c) 2023-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 OHOS_CAMERA_DPS_UTILS_H
17 #define OHOS_CAMERA_DPS_UTILS_H
18
19 #include <unistd.h>
20
21 #include "watch_dog.h"
22 #include "camera_hdi_const.h"
23
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
27 using SteadyTimePoint = std::chrono::steady_clock::time_point;
28 using Nano = std::chrono::nanoseconds;
29 using Micro = std::chrono::microseconds;
30 using Milli = std::chrono::milliseconds;
31 using Seconds = std::chrono::seconds;
32 constexpr int32_t DEC_RADIX = 10;
33
GetSteadyNow()34 inline SteadyTimePoint GetSteadyNow()
35 {
36 return SteadyTimePoint::clock::now();
37 }
38
39 template <typename Duration, typename TimePoint>
GetDiffTime(TimePoint begin,TimePoint end)40 inline auto GetDiffTime(TimePoint begin, TimePoint end)
41 {
42 if (begin > end) {
43 return static_cast<typename Duration::rep>(0);
44 }
45 return std::chrono::duration_cast<Duration>(end - begin).count();
46 }
47
48 template <typename Duration, typename TimePoint>
GetDiffTime(TimePoint begin)49 inline auto GetDiffTime(TimePoint begin)
50 {
51 return GetDiffTime<Duration>(begin, TimePoint::clock::now());
52 }
53
GetTimestampMilli()54 inline uint64_t GetTimestampMilli()
55 {
56 return std::chrono::duration_cast<Milli>(GetSteadyNow().time_since_epoch()).count();
57 }
58
GetTimestampMicro()59 inline uint64_t GetTimestampMicro()
60 {
61 return std::chrono::duration_cast<Micro>(GetSteadyNow().time_since_epoch()).count();
62 }
63
IsFileEmpty(int fd)64 inline bool IsFileEmpty(int fd)
65 {
66 off_t fileSize = lseek(fd, 0, SEEK_END);
67 if (fileSize == (off_t)-1) {
68 return false;
69 }
70 return fileSize == 0;
71 }
72
ClearFileContent(int fd)73 inline bool ClearFileContent(int fd)
74 {
75 if (ftruncate(fd, 0) != 0) {
76 return false;
77 }
78 if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
79 return false;
80 }
81 return true;
82 }
83
84 template <typename U>
AlignUp(U num,U alignment)85 constexpr U AlignUp(U num, U alignment)
86 {
87 return alignment ? ((num + alignment - 1) & (~(alignment - 1))) : num;
88 }
89
90 template <typename T, typename... Args>
91 struct MakeSharedHelper : public T {
MakeSharedHelperMakeSharedHelper92 explicit MakeSharedHelper(Args&&... args) : T(std::forward<Args>(args)...)
93 {
94 }
95 };
96
97 template <typename T, typename... Args>
CreateShared(Args &&...args)98 std::shared_ptr<T> CreateShared(Args&&... args)
99 {
100 return std::make_shared<MakeSharedHelper<T, Args &&...>>(std::forward<Args>(args)...);
101 }
102
103 template <typename T, typename... Args>
104 struct MakeUniqueHelper : public T {
MakeUniqueHelperMakeUniqueHelper105 explicit MakeUniqueHelper(Args&&... args) : T(std::forward<Args>(args)...)
106 {
107 }
108 };
109
110 template <typename T, typename... Args>
CreateUnique(Args &&...args)111 std::unique_ptr<T> CreateUnique(Args&&... args)
112 {
113 return std::make_unique<MakeUniqueHelper<T, Args &&...>>(std::forward<Args>(args)...);
114 }
115
GetVersionId(uint32_t major,uint32_t minor)116 inline int32_t GetVersionId(uint32_t major, uint32_t minor)
117 {
118 const uint32_t offset = 8;
119 return static_cast<int32_t>((major << offset) | minor);
120 }
121
StrToLL(const std::string & str,long long & value,int32_t base)122 inline bool StrToLL(const std::string &str, long long &value, int32_t base)
123 {
124 auto add = str.c_str();
125 char *end = nullptr;
126 errno = 0;
127 value = strtoll(add, &end, base);
128 DP_CHECK_ERROR_RETURN_RET_LOG(
129 (errno == ERANGE && (value == LLONG_MAX || value == LLONG_MIN)) || (errno != 0 && value == 0),
130 false, "strtoll converse error,str=%{public}s", str.c_str());
131 DP_CHECK_ERROR_RETURN_RET_LOG(end == add, false, "strtoll no digit find");
132 DP_CHECK_ERROR_RETURN_RET_LOG(end[0] != '\0', false, "strtoll no all find");
133 return true;
134 }
135
StrToULL(const std::string & str,unsigned long long & value)136 inline bool StrToULL(const std::string &str, unsigned long long &value)
137 {
138 auto add = str.c_str();
139 char *end = nullptr;
140 errno = 0;
141 value = strtoull(add, &end, DEC_RADIX);
142 DP_CHECK_ERROR_RETURN_RET_LOG(
143 (errno == ERANGE && value == ULLONG_MAX) || (errno != 0 && value == 0),
144 false, "strtoull converse error,str=%{public}s", str.c_str());
145 DP_CHECK_ERROR_RETURN_RET_LOG(end == add, false, "strtoull no digit find");
146 DP_CHECK_ERROR_RETURN_RET_LOG(end[0] != '\0', false, "strtoull no all find");
147 return true;
148 }
149
StrToI64(const std::string & str,int64_t & value)150 inline bool StrToI64(const std::string &str, int64_t &value)
151 {
152 long long tmp = 0;
153 bool isOK = StrToLL(str, tmp, DEC_RADIX);
154 value = tmp;
155 return isOK && (tmp >= INT64_MIN && tmp <= INT64_MAX);
156 }
157
158 Watchdog& GetGlobalWatchdog();
159 struct DpsCallerInfo {
160 int32_t pid;
161 int32_t uid;
162 uint32_t tokenID;
163 std::string bundleName;
164 std::string version;
165 };
166
167 DpsCallerInfo GetDpsCallerInfo();
168 std::unordered_map<std::string, std::string> ParseKeyValue(const std::string& input);
169 } // namespace DeferredProcessing
170 } // namespace CameraStandard
171 } // namespace OHOS
172 #endif // OHOS_CAMERA_DPS_UTILS_H