1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3190978c3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4190978c3Sopenharmony_ci * you may not use this file except in compliance with the License.
5190978c3Sopenharmony_ci * You may obtain a copy of the License at
6190978c3Sopenharmony_ci *
7190978c3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8190978c3Sopenharmony_ci *
9190978c3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10190978c3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11190978c3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12190978c3Sopenharmony_ci * See the License for the specific language governing permissions and
13190978c3Sopenharmony_ci * limitations under the License.
14190978c3Sopenharmony_ci */
15190978c3Sopenharmony_ci
16190978c3Sopenharmony_ci#include "startup_schedule.h"
17190978c3Sopenharmony_ci
18190978c3Sopenharmony_ci#include "service_control.h"
19190978c3Sopenharmony_ci
20190978c3Sopenharmony_ci#include "alarm_timer_utils.h"
21190978c3Sopenharmony_ci#include "constant.h"
22190978c3Sopenharmony_ci#include "firmware_preferences_utils.h"
23190978c3Sopenharmony_ci#include "startup_constant.h"
24190978c3Sopenharmony_ci#include "system_ability_operator.h"
25190978c3Sopenharmony_ci#include "time_utils.h"
26190978c3Sopenharmony_ci#include "update_log.h"
27190978c3Sopenharmony_ci
28190978c3Sopenharmony_cinamespace OHOS {
29190978c3Sopenharmony_cinamespace UpdateEngine {
30190978c3Sopenharmony_ciconstexpr uint64_t STARTUP_LOOPER_INTERVAL = 180; // 动态启停定时器轮询周期
31190978c3Sopenharmony_ciStartupSchedule::StartupSchedule()
32190978c3Sopenharmony_ci{
33190978c3Sopenharmony_ci    ENGINE_LOGD("StartupSchedule constructor");
34190978c3Sopenharmony_ci}
35190978c3Sopenharmony_ci
36190978c3Sopenharmony_ciStartupSchedule::~StartupSchedule()
37190978c3Sopenharmony_ci{
38190978c3Sopenharmony_ci    ENGINE_LOGD("StartupSchedule deConstructor");
39190978c3Sopenharmony_ci}
40190978c3Sopenharmony_ci
41190978c3Sopenharmony_civoid StartupSchedule::RegisterLooper(const ScheduleLooper &looper)
42190978c3Sopenharmony_ci{
43190978c3Sopenharmony_ci    UnregisterLooper();
44190978c3Sopenharmony_ci    ENGINE_LOGI("RegisterLooper");
45190978c3Sopenharmony_ci    int64_t startTime = static_cast<int64_t>(AlarmTimerUtils::GetSystemBootTime()) +
46190978c3Sopenharmony_ci     static_cast<int64_t>(STARTUP_LOOPER_INTERVAL) * Constant::MILLESECONDS;
47190978c3Sopenharmony_ci    looperTimerId_ = AlarmTimerUtils::RegisterRepeatAlarm(startTime, STARTUP_LOOPER_INTERVAL, [=]() { looper(); });
48190978c3Sopenharmony_ci}
49190978c3Sopenharmony_ci
50190978c3Sopenharmony_civoid StartupSchedule::UnregisterLooper()
51190978c3Sopenharmony_ci{
52190978c3Sopenharmony_ci    ENGINE_LOGI("UnregisterLooper");
53190978c3Sopenharmony_ci    if (looperTimerId_ > 0) {
54190978c3Sopenharmony_ci        AlarmTimerUtils::UnregisterAlarm(looperTimerId_);
55190978c3Sopenharmony_ci    }
56190978c3Sopenharmony_ci    looperTimerId_ = 0;
57190978c3Sopenharmony_ci}
58190978c3Sopenharmony_ci
59190978c3Sopenharmony_cibool StartupSchedule::Schedule(const ScheduleTask &task)
60190978c3Sopenharmony_ci{
61190978c3Sopenharmony_ci    ENGINE_LOGI("Schedule next SA start time is %{public}s",
62190978c3Sopenharmony_ci        TimeUtils::GetPrintTimeStr(TimeUtils::GetTimestamp() + task.minDelayTime).c_str());
63190978c3Sopenharmony_ci    uint64_t scheduleTime = task.minDelayTime * Startup::ONE_SECOND_MILLISECONDS;
64190978c3Sopenharmony_ci
65190978c3Sopenharmony_ci    // 由于SaMgr暂不支持记录启动原因,因此临时将启动原因写入SP文件中
66190978c3Sopenharmony_ci    DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->SaveInt(Constant::PROCESS_RESTART_REASON,
67190978c3Sopenharmony_ci        CAST_INT(task.startupReason));
68190978c3Sopenharmony_ci
69190978c3Sopenharmony_ci    int32_t ret = StartServiceByTimer(Startup::UPDATER_SA_NAME.c_str(), scheduleTime);
70190978c3Sopenharmony_ci    ENGINE_LOGI("StartServiceByTimer finish, ret is %{public}d", ret);
71190978c3Sopenharmony_ci    return ret == 0;
72190978c3Sopenharmony_ci}
73190978c3Sopenharmony_ci
74190978c3Sopenharmony_cibool StartupSchedule::OnDemandSchedule(const std::vector<ScheduleTask> &tasks)
75190978c3Sopenharmony_ci{
76190978c3Sopenharmony_ci    if (tasks.empty()) {
77190978c3Sopenharmony_ci        ENGINE_LOGE("scheduleTasks is null");
78190978c3Sopenharmony_ci        return false;
79190978c3Sopenharmony_ci    }
80190978c3Sopenharmony_ci    for (const auto &task : tasks) {
81190978c3Sopenharmony_ci        ENGINE_LOGI("OnDemandSchedule task %{public}s", task.ToString().c_str());
82190978c3Sopenharmony_ci    }
83190978c3Sopenharmony_ci    auto isSuccess = SystemAbilityOperator().UpdateStartupPolicy(tasks);
84190978c3Sopenharmony_ci    ENGINE_LOGI("OnDemandSchedule %{public}s", isSuccess ? "success" : "failure");
85190978c3Sopenharmony_ci    return isSuccess;
86190978c3Sopenharmony_ci}
87190978c3Sopenharmony_ci} // namespace UpdateEngine
88190978c3Sopenharmony_ci} // namespace OHOS