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