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 "startup_schedule.h"
17
18#include "service_control.h"
19
20#include "alarm_timer_utils.h"
21#include "constant.h"
22#include "firmware_preferences_utils.h"
23#include "startup_constant.h"
24#include "system_ability_operator.h"
25#include "time_utils.h"
26#include "update_log.h"
27
28namespace OHOS {
29namespace UpdateEngine {
30constexpr uint64_t STARTUP_LOOPER_INTERVAL = 180; // 动态启停定时器轮询周期
31StartupSchedule::StartupSchedule()
32{
33    ENGINE_LOGD("StartupSchedule constructor");
34}
35
36StartupSchedule::~StartupSchedule()
37{
38    ENGINE_LOGD("StartupSchedule deConstructor");
39}
40
41void StartupSchedule::RegisterLooper(const ScheduleLooper &looper)
42{
43    UnregisterLooper();
44    ENGINE_LOGI("RegisterLooper");
45    int64_t startTime = static_cast<int64_t>(AlarmTimerUtils::GetSystemBootTime()) +
46     static_cast<int64_t>(STARTUP_LOOPER_INTERVAL) * Constant::MILLESECONDS;
47    looperTimerId_ = AlarmTimerUtils::RegisterRepeatAlarm(startTime, STARTUP_LOOPER_INTERVAL, [=]() { looper(); });
48}
49
50void StartupSchedule::UnregisterLooper()
51{
52    ENGINE_LOGI("UnregisterLooper");
53    if (looperTimerId_ > 0) {
54        AlarmTimerUtils::UnregisterAlarm(looperTimerId_);
55    }
56    looperTimerId_ = 0;
57}
58
59bool StartupSchedule::Schedule(const ScheduleTask &task)
60{
61    ENGINE_LOGI("Schedule next SA start time is %{public}s",
62        TimeUtils::GetPrintTimeStr(TimeUtils::GetTimestamp() + task.minDelayTime).c_str());
63    uint64_t scheduleTime = task.minDelayTime * Startup::ONE_SECOND_MILLISECONDS;
64
65    // 由于SaMgr暂不支持记录启动原因,因此临时将启动原因写入SP文件中
66    DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->SaveInt(Constant::PROCESS_RESTART_REASON,
67        CAST_INT(task.startupReason));
68
69    int32_t ret = StartServiceByTimer(Startup::UPDATER_SA_NAME.c_str(), scheduleTime);
70    ENGINE_LOGI("StartServiceByTimer finish, ret is %{public}d", ret);
71    return ret == 0;
72}
73
74bool StartupSchedule::OnDemandSchedule(const std::vector<ScheduleTask> &tasks)
75{
76    if (tasks.empty()) {
77        ENGINE_LOGE("scheduleTasks is null");
78        return false;
79    }
80    for (const auto &task : tasks) {
81        ENGINE_LOGI("OnDemandSchedule task %{public}s", task.ToString().c_str());
82    }
83    auto isSuccess = SystemAbilityOperator().UpdateStartupPolicy(tasks);
84    ENGINE_LOGI("OnDemandSchedule %{public}s", isSuccess ? "success" : "failure");
85    return isSuccess;
86}
87} // namespace UpdateEngine
88} // namespace OHOS