1 /*
2 * Copyright (c) 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 #include "firmware_update_adapter.h"
17
18 #include <ctime>
19 #include <cstdlib>
20 #include <fstream>
21 #include <iomanip>
22 #include <ohos_types.h>
23 #include <securec.h>
24 #include <sstream>
25
26 #include "securec.h"
27
28 #include "constant.h"
29 #include "config_parse.h"
30 #include "device_adapter.h"
31 #include "firmware_constant.h"
32 #include "firmware_file_utils.h"
33 #include "firmware_log.h"
34 #include "firmware_preferences_utils.h"
35 #include "firmware_task.h"
36 #include "time_utils.h"
37 #include "update_helper.h"
38
39 namespace OHOS {
40 namespace UpdateEngine {
41 constexpr int32_t REQUEST_ID_LEN = 21;
42 constexpr int32_t TIME_STAMP_LEN = 15;
43 constexpr int32_t TIME_START_YEAR = 1900;
44 constexpr int32_t RANDOM_BOUND_SIX = 1000000;
45
GetRequestId()46 std::string FirmwareUpdateAdapter::GetRequestId()
47 {
48 char requestId[REQUEST_ID_LEN] = {0};
49 char timeStamp[TIME_STAMP_LEN] = {0};
50 struct tm timeInfo;
51 time_t now;
52 time(&now);
53 localtime_r(&now, &timeInfo);
54 int res = sprintf_s(timeStamp, sizeof(timeStamp), "%d%02d%02d%02d%02d%02d", timeInfo.tm_year + TIME_START_YEAR,
55 timeInfo.tm_mon + 1, timeInfo.tm_mday, timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
56 if (res <= 0) {
57 FIRMWARE_LOGE("CreateTimeStamp error: %{public}d", res);
58 return "";
59 }
60
61 srand((unsigned int)now);
62 int32_t randomInt = rand() % RANDOM_BOUND_SIX; /* random len is 6 */
63 res = sprintf_s(requestId, REQUEST_ID_LEN, "%s%d", timeStamp, randomInt);
64 if (res <= 0) {
65 FIRMWARE_LOGE("CreateRequestId error: %{public}d", res);
66 return "";
67 }
68
69 return std::string(requestId);
70 }
71
GetBusinessDomain()72 std::string FirmwareUpdateAdapter::GetBusinessDomain()
73 {
74 return DelayedSingleton<ConfigParse>::GetInstance()->GetBusinessDomain();
75 }
76
GetUpdateAction()77 std::string FirmwareUpdateAdapter::GetUpdateAction()
78 {
79 std::string action =
80 DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->ObtainString(Firmware::UPDATE_ACTION, "");
81 if (action.compare("recovery") == 0) {
82 return "recovery";
83 }
84 return "upgrade";
85 }
86 } // namespace UpdateEngine
87 } // namespace OHOS
88