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 "system_ability_operator.h"
17 
18 #include <iservice_registry.h>
19 
20 #include "errors.h"
21 #include "network_type.h"
22 #include "system_ability_definition.h"
23 
24 #include "update_log.h"
25 
26 namespace OHOS {
27 namespace UpdateEngine {
UpdateStartupPolicy(const std::vector<ScheduleTask> & scheduleTasks)28 bool SystemAbilityOperator::UpdateStartupPolicy(const std::vector<ScheduleTask> &scheduleTasks)
29 {
30     ENGINE_LOGI("UpdateStartupPolicy");
31     constexpr uint64_t validMinInterval = 30;
32     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
33     for (const auto &task : scheduleTasks) {
34          // 启停policy中增加TIMED事件
35         if (task.minDelayTime >= validMinInterval) {
36             abilityOnDemandEvents.emplace_back(CreateTimedEvent(task.minDelayTime));
37             ENGINE_LOGI("UpdateStartupPolicy update timed event, loop interval %{public}s",
38                 std::to_string(task.minDelayTime).c_str());
39         } else {
40             ENGINE_LOGE("UpdateStartupPolicy failure, invalid loop interval %{public}s",
41                 std::to_string(task.minDelayTime).c_str());
42             return false;
43         }
44     }
45     return UpdateStartupOnDemandPolicy(abilityOnDemandEvents);
46 }
47 
CreateTimedEvent(const uint64_t nextStartDuration)48 SystemAbilityOnDemandEvent SystemAbilityOperator::CreateTimedEvent(const uint64_t nextStartDuration)
49 {
50     const std::string timedEventName = "loopevent"; // TIMED 事件名称,不可更改
51 
52     SystemAbilityOnDemandEvent timedEvent;
53     timedEvent.eventId = OnDemandEventId::TIMED_EVENT;
54     timedEvent.name = timedEventName;
55     timedEvent.value = std::to_string(nextStartDuration);
56     return timedEvent;
57 }
58 
UpdateStartupOnDemandPolicy(const std::vector<SystemAbilityOnDemandEvent> & events)59 bool SystemAbilityOperator::UpdateStartupOnDemandPolicy(const std::vector<SystemAbilityOnDemandEvent> &events)
60 {
61     ENGINE_LOGI("UpdateStartupOnDemandPolicy");
62     auto samgr = GetSystemAbilityManager();
63     if (samgr == nullptr) {
64         ENGINE_LOGE("UpdateStartupOnDemandPolicy failure, caused by samgr object null");
65         return false;
66     }
67 
68     auto ret = samgr->UpdateOnDemandPolicy(UPDATE_DISTRIBUTED_SERVICE_ID, OnDemandPolicyType::START_POLICY, events);
69     if (ret != ERR_OK) {
70         ENGINE_LOGE("UpdateStartupOnDemandPolicy failure, caused by samgr interface call fail, error code %{public}d",
71             ret);
72         return false;
73     }
74 
75     ENGINE_LOGD("UpdateStartupOnDemandPolicy success");
76     return true;
77 }
78 
UnloadSystemAbility()79 bool SystemAbilityOperator::UnloadSystemAbility()
80 {
81     ENGINE_LOGI("UnloadSystemAbility");
82     auto samgr = GetSystemAbilityManager();
83     if (samgr == nullptr) {
84         ENGINE_LOGE("UnloadSystemAbility failure, caused by samgr object null!");
85         return false;
86     }
87 
88     int32_t ret = samgr->UnloadSystemAbility(UPDATE_DISTRIBUTED_SERVICE_ID);
89     if (ret != ERR_OK) {
90         ENGINE_LOGE("UnloadSystemAbility failure, caused by samgr interface call fail, error code %{public}d", ret);
91         return false;
92     }
93 
94     ENGINE_LOGD("UnloadSystemAbility success");
95     return true;
96 }
97 
GetStartupOnDemandPolicy()98 std::vector<SystemAbilityOnDemandEvent> SystemAbilityOperator::GetStartupOnDemandPolicy()
99 {
100     ENGINE_LOGI("GetStartupOnDemandPolicy");
101     auto samgr = GetSystemAbilityManager();
102     if (samgr == nullptr) {
103         ENGINE_LOGE("GetStarrtupOnDemandPolicy failure, caused by samgr object null");
104         return {};
105     }
106 
107     std::vector<SystemAbilityOnDemandEvent> events;
108     int32_t ret = samgr->GetOnDemandPolicy(UPDATE_DISTRIBUTED_SERVICE_ID, OnDemandPolicyType::START_POLICY, events);
109     if (ret != ERR_OK) {
110         ENGINE_LOGE("GetStartupOnDemandPolicy failure, caused by samgr interface call fail, error code %{public}d",
111             ret);
112         return {};
113     }
114 
115     ENGINE_LOGD("GetStartupOnDemandPolicy success");
116     return events;
117 }
118 
GetSystemAbilityManager() const119 inline sptr<ISystemAbilityManager> SystemAbilityOperator::GetSystemAbilityManager() const
120 {
121     return SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
122 }
123 } // namespace UpdateEngine
124 } // namespace OHOS
125