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