1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 //! This create implement the request server register and publish
14
15 use std::mem::MaybeUninit;
16 use std::sync::atomic::{AtomicBool, Ordering};
17 use std::sync::{Arc, Mutex};
18
19 use hisysevent::{build_number_param, write, EventType};
20 use samgr::definition::APP_MGR_SERVICE_ID;
21 use samgr::manage::SystemAbilityManager;
22 use system_ability_fwk::ability::{Ability, Handler};
23
24 use crate::database::clear_database;
25 use crate::manage::app_state::AppStateListener;
26 use crate::manage::{account, SystemConfigManager, TaskManager};
27 use crate::service::client::ClientManager;
28 use crate::service::run_count::RunCountManager;
29 pub(crate) static mut PANIC_INFO: Option<String> = None;
30 use crate::manage::events::TaskManagerEvent;
31 use crate::manage::task_manager::TaskManagerTx;
32 use crate::service::RequestServiceStub;
33
34 /// TEST_SERVICE_ID SAID
35 pub(crate) static mut SYSTEM_CONFIG_MANAGER: MaybeUninit<SystemConfigManager> =
36 MaybeUninit::uninit();
37
service_start_fault()38 fn service_start_fault() {
39 const DOMAIN: &str = "REQUEST";
40 const SERVICE_START_FAULT: &str = "SERVICE_START_FAULT";
41 const ERROR_INFO: &str = "ERROR_INFO";
42 const DOWNLOAD_PUBLISH_FAIL: i32 = -1;
43
44 write(
45 DOMAIN,
46 SERVICE_START_FAULT,
47 EventType::Fault,
48 &[build_number_param!(ERROR_INFO, DOWNLOAD_PUBLISH_FAIL)],
49 );
50 }
51
52 /// request ability
53 pub struct RequestAbility {
54 task_manager: Mutex<Option<TaskManagerTx>>,
55 remote_busy: Arc<AtomicBool>,
56 }
57
58 impl RequestAbility {
59 /// new request ability
new() -> Self60 pub fn new() -> Self {
61 Self {
62 remote_busy: Arc::new(AtomicBool::new(false)),
63 task_manager: Mutex::new(None),
64 }
65 }
66
init(&self, handler: Handler)67 fn init(&self, handler: Handler) {
68 info!("ability init");
69
70 std::panic::set_hook(Box::new(|info| unsafe {
71 let info = info.to_string();
72 error!("{}", info);
73 PANIC_INFO = Some(info);
74 }));
75
76 ylong_runtime::builder::RuntimeBuilder::new_multi_thread()
77 .worker_num(4)
78 .build_global()
79 .unwrap();
80 info!("ylong_runtime init ok");
81
82 let runcount_manager = RunCountManager::init();
83 info!("runcount_manager init ok");
84
85 let client_manger = ClientManager::init();
86 info!("client_manger init ok");
87
88 unsafe { SYSTEM_CONFIG_MANAGER.write(SystemConfigManager::init()) };
89 info!("system_config_manager init ok");
90
91 let task_manager = TaskManager::init(runcount_manager.clone(), client_manger.clone());
92 *self.task_manager.lock().unwrap() = Some(task_manager.clone());
93
94 info!("task_manager init ok");
95
96 AppStateListener::init(client_manger.clone(), task_manager.clone());
97
98 SystemAbilityManager::subscribe_system_ability(
99 APP_MGR_SERVICE_ID,
100 |_, _| {
101 info!("app manager service init");
102 AppStateListener::register();
103 },
104 |_, _| {
105 error!("app manager service died");
106 },
107 );
108
109 let stub = RequestServiceStub::new(
110 handler.clone(),
111 task_manager,
112 client_manger,
113 runcount_manager,
114 self.remote_busy.clone(),
115 );
116
117 info!("ability init succeed");
118 if !handler.publish(stub) {
119 service_start_fault();
120 }
121 }
122 }
123
124 impl Ability for RequestAbility {
on_start_with_reason( &self, reason: system_ability_fwk::cxx_share::SystemAbilityOnDemandReason, handler: Handler, )125 fn on_start_with_reason(
126 &self,
127 reason: system_ability_fwk::cxx_share::SystemAbilityOnDemandReason,
128 handler: Handler,
129 ) {
130 info!("on_start_with_reason: {:?}", reason);
131 if reason.name == "usual.event.USER_REMOVED" {
132 let user_id = reason.value.parse::<i32>().unwrap();
133 account::remove_account_tasks(user_id);
134 self.init(handler);
135 } else {
136 self.init(handler);
137 }
138 }
139
on_active(&self, reason: system_ability_fwk::cxx_share::SystemAbilityOnDemandReason)140 fn on_active(&self, reason: system_ability_fwk::cxx_share::SystemAbilityOnDemandReason) {
141 info!("on_active: {:?}", reason);
142 }
143
on_idle(&self, _reason: system_ability_fwk::cxx_share::SystemAbilityOnDemandReason) -> i32144 fn on_idle(&self, _reason: system_ability_fwk::cxx_share::SystemAbilityOnDemandReason) -> i32 {
145 if self.remote_busy.load(Ordering::Acquire) {
146 info!("remote is busy reject idle");
147 -1
148 } else {
149 info!("remote not busy accept idle");
150 clear_database();
151 0
152 }
153 }
154
on_device_level_changed(&self, change_type: i32, level: i32, action: String)155 fn on_device_level_changed(&self, change_type: i32, level: i32, action: String) {
156 info!(
157 "on_device_level_changed type {} level {} action {}",
158 change_type, level, action
159 );
160 if let Some(task_manager) = self.task_manager.lock().unwrap().as_ref() {
161 task_manager.send_event(TaskManagerEvent::Device(level));
162 }
163 }
164 }
165
166 #[cfg(not(test))]
167 #[used]
168 #[link_section = ".init_array"]
169 static A: extern "C" fn() = {
170 #[link_section = "..text.startup"]
init()171 extern "C" fn init() {
172 info!("begin request service init");
173 let system_ability = RequestAbility::new()
174 .build_system_ability(samgr::definition::DOWNLOAD_SERVICE_ID, false)
175 .unwrap();
176 system_ability.register();
177 info!("request service inited");
178 }
179 init
180 };
181