1 /*
2 * Copyright (c) 2021-2022 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 #include "form_util.h"
17
18 #include <chrono>
19 #include <cinttypes>
20 #include <regex>
21
22 #include "bundle_constants.h"
23 #include "form_constants.h"
24 #include "hilog_wrapper.h"
25 #include "os_account_manager_wrapper.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 constexpr int64_t SEC_TO_NANOSEC = 1000000000;
31 constexpr int64_t SEC_TO_MILLISEC = 1000;
32 constexpr int64_t MILLISEC_TO_NANOSEC = 1000000;
33 constexpr int64_t INVALID_UDID_HASH = 0;
34 } // namespace
35
36 using namespace std;
37 using namespace std::chrono;
38
39 /**
40 * @brief create want for form.
41 * @param formName The name of the form.
42 * @param specificationId specification id.
43 * @param isTemporaryForm temporary form or not.
44 * @param want The want of the form.
45 */
CreateFormWant(const std::string & formName,const int32_t specificationId,const bool isTemporaryForm,Want & want)46 void FormUtil::CreateFormWant(const std::string &formName,
47 const int32_t specificationId, const bool isTemporaryForm, Want &want)
48 {
49 want.SetParam(Constants::PARAM_FORM_NAME_KEY, formName);
50 want.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, specificationId);
51 want.SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, isTemporaryForm);
52 }
53
54 /**
55 * @brief create default want for form.
56 * @param want The want of the form..
57 * @param uri The uri.
58 * @param connectId connect id.
59 */
CreateDefaultFormWant(Want & want,const std::string & uri,const int32_t connectId)60 void FormUtil::CreateDefaultFormWant(Want &want, const std::string &uri, const int32_t connectId)
61 {
62 want.SetParam(Constants::FORM_CONNECT_ID, connectId);
63 want.SetParam(Constants::FORM_SUPPLY_INFO, uri);
64 }
65
66 /**
67 * @brief create udid for form.
68 * @return udid.
69 */
GenerateUdid()70 std::string FormUtil::GenerateUdid()
71 {
72 char buf[256] = {0};
73 return buf;
74 }
75
76 /**
77 * @brief create form id for form.
78 * @param udidHash udid hash
79 * @return new form id.
80 */
GenerateFormId(int64_t udidHash)81 int64_t FormUtil::GenerateFormId(int64_t udidHash)
82 {
83 struct timespec t;
84 t.tv_sec = 0;
85 t.tv_nsec = 0;
86 clock_gettime(CLOCK_REALTIME, &t);
87
88 int64_t elapsedTime { ((t.tv_sec) * SEC_TO_NANOSEC + t.tv_nsec) };
89 size_t elapsedHash = std::hash<std::string>()(std::to_string(elapsedTime));
90 HILOG_INFO("%{public}s, GenerateFormId generate elapsed hash %{public}zu", __func__, elapsedHash);
91 uint64_t unsignedUdidHash = static_cast<uint64_t>(udidHash);
92 uint64_t formId = unsignedUdidHash | (uint32_t)(elapsedHash & 0x000000007fffffffL);
93 int64_t ret = static_cast<int64_t>(formId);
94 HILOG_INFO("%{public}s, GenerateFormId generate formId %{public}" PRId64 "", __func__, ret);
95 return ret;
96 }
97
98 /**
99 * @brief padding form id.
100 * @param formId The id of the form.
101 * @param udidHash udid hash
102 * @return new form id.
103 */
PaddingUdidHash(uint64_t formId,uint64_t udidHash)104 int64_t FormUtil::PaddingUdidHash(uint64_t formId, uint64_t udidHash)
105 {
106 // Compatible with int form id.
107 if ((formId & 0xffffffff00000000L) == 0) {
108 return udidHash | formId;
109 }
110
111 return formId;
112 }
113 /**
114 * @brief create udid hash.
115 * @param udidHash udid hash.
116 * @return Returns true on success, false on failure.
117 */
GenerateUdidHash(int64_t & udidHash)118 bool FormUtil::GenerateUdidHash(int64_t &udidHash)
119 {
120 HILOG_INFO("%{public}s start, udidHash:%{private}s", __func__, std::to_string(udidHash).c_str());
121 if (udidHash != INVALID_UDID_HASH) {
122 return true;
123 }
124
125 u_int64_t hashId = 0L;
126 const int32_t thirtyTwo = 32;
127 udidHash = (hashId & 0x0000000000ffffffL) << thirtyTwo;
128 if (udidHash < 0) {
129 udidHash = 0L;
130 }
131 HILOG_INFO("%{public}s, generate hash %{private}s", __func__, std::to_string(udidHash).c_str());
132 return true;
133 }
134 /**
135 * @brief Get current system nanosecond.
136 * @return Current system nanosecond.
137 */
GetCurrentNanosecond()138 int64_t FormUtil::GetCurrentNanosecond()
139 {
140 struct timespec ts;
141 ts.tv_sec = 0;
142 ts.tv_nsec = 0;
143 clock_gettime(CLOCK_MONOTONIC, &ts);
144 return (ts.tv_sec * SEC_TO_NANOSEC + ts.tv_nsec);
145 }
146 /**
147 * @brief Get current system millisecond.
148 * @return Current system millisecond.
149 */
GetCurrentMillisecond()150 int64_t FormUtil::GetCurrentMillisecond()
151 {
152 struct timespec ts;
153 clock_gettime(CLOCK_REALTIME, &ts);
154 return (ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC);
155 }
156 /**
157 * @brief Get millisecond from tm.
158 * @param tmAtTime tm time.
159 * @return Millisecond.
160 */
GetMillisecondFromTm(struct tm & tmAtTime)161 int64_t FormUtil::GetMillisecondFromTm(struct tm &tmAtTime)
162 {
163 time_t inputTime = mktime(&tmAtTime);
164 if (inputTime == -1) {
165 HILOG_ERROR("%{public}s fail, mktime failed.", __func__);
166 return -1;
167 }
168 system_clock::time_point pointTime = system_clock::from_time_t(inputTime);
169 auto timeMilliseconds = chrono::duration_cast<chrono::milliseconds>(pointTime.time_since_epoch());
170 return timeMilliseconds.count();
171 }
172
173 /**
174 * @brief split string.
175 * @param in string.
176 * @param delim delimiter.
177 * @return string list.
178 */
StringSplit(const std::string & in,const std::string & delim)179 std::vector<std::string> FormUtil::StringSplit(const std::string &in, const std::string &delim)
180 {
181 std::regex reg { delim };
182 return std::vector<std::string> {
183 std::sregex_token_iterator(in.begin(), in.end(), reg, -1),
184 std::sregex_token_iterator()
185 };
186 }
187
188 /**
189 * @brief get current active account id.
190 * @return int current active account id.
191 */
GetCurrentAccountId()192 int FormUtil::GetCurrentAccountId()
193 {
194 std::vector<int32_t> activeList;
195 ErrCode errCode = DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
196 if (errCode != ERR_OK) {
197 HILOG_ERROR("QueryActiveOsAccountIds failed.");
198 return Constants::ANY_USERID;
199 }
200 if (activeList.empty()) {
201 HILOG_ERROR("QueryActiveOsAccountIds is empty, no accounts.");
202 return Constants::ANY_USERID;
203 }
204
205 return activeList.front();
206 }
207 } // namespace AppExecFwk
208 } // namespace OHOS