• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /// This macro can define a rust SA and give callback function on_start on_stop
17 #[macro_export]
18 macro_rules! define_system_ability {
19     {
20         sa: $system_ability:ident($on_start:path, $on_stop:path),
21     } => {
22         /// $system_ability is passed in by the user
23         pub struct $system_ability {
24             r_system_ability: RSystemAbility<$system_ability>,
25         }
26 
27         impl $system_ability {
28             /// Create a $system_ability object
29             #[allow(dead_code)]
30             pub fn new_system_ability(said: i32, run_on_create: bool) -> Option<Self> {
31                 let r_system_ability = RSystemAbility::new(said, run_on_create);
32                 match r_system_ability {
33                     Some(r_system_ability) => {
34                         Some($system_ability { r_system_ability })
35                     },
36                     None => {
37                         error!(LOG_LABEL,"RSystemAbility::new failed");
38                         None
39                     }
40                 }
41             }
42         }
43 
44         impl $crate::ISystemAbility for $system_ability {
45             /// Callback to deal safwk onstart for this system_ability
46             fn on_start(&self) {
47                 $on_start(self)
48             }
49 
50             /// Callback to deal safwk onstop for this system_ability
51             fn on_stop(&self) {
52                 $on_stop(self)
53             }
54         }
55 
56         impl $crate::IMethod for $system_ability {
57             /// Call RSystemAbility<$system_ability> register_ability
58             fn register(&self) {
59                 self.r_system_ability.register_ability(self);
60             }
61 
62             /// Call RSystemAbility<$system_ability> publish
63             fn publish(&self, service: &RemoteObj, said : i32) {
64                 self.r_system_ability.publish(service, said);
65             }
66         }
67     };
68 }