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