• 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 #ifndef UPDATER_INIT_H
16 #define UPDATER_INIT_H
17 
18 #include <vector>
19 #include "macros.h"
20 
21 namespace Updater {
22 enum UpdaterInitEvent {
23     // main
24     UPDATER_MAIN_PRE_EVENT = 0,
25 
26     // updater
27     UPDATER_PRE_INIT_EVENT,
28     UPDATER_INIT_EVENT,
29     UPDATER_PRE_VERIFY_EVENT,
30     UPDATER_POST_INIT_EVENT,
31 
32     // flashd
33     FLAHSD_PRE_INIT_EVENT,
34 
35     // binary
36     UPDATER_BINARY_INIT_EVENT,
37     UPDATER_BINARY_INIT_DONE_EVENT,
38 
39     // rpmmb
40     UPDATER_RPMB_DATA_CLEAR_EVENT,
41 
42     // factory reset
43     FACTORY_RESET_INIT_EVENT,
44 
45     UPDATER_INIT_EVENT_BUTT
46 };
47 
48 using InitHandler = void (*)(void);
49 
50 class UpdaterInit {
51     DISALLOW_COPY_MOVE(UpdaterInit);
52 public:
GetInstance()53     static UpdaterInit &GetInstance()
54     {
55         static UpdaterInit instance;
56         return instance;
57     }
InvokeEvent(enum UpdaterInitEvent eventId)58     void InvokeEvent(enum UpdaterInitEvent eventId) const
59     {
60         if (eventId >= UPDATER_INIT_EVENT_BUTT) {
61             return;
62         }
63         for (const auto &handler : initEvent_[eventId]) {
64             if (handler != nullptr) {
65                 handler();
66             }
67         }
68     }
SubscribeEvent(enum UpdaterInitEvent eventId,InitHandler handler)69     void SubscribeEvent(enum UpdaterInitEvent eventId, InitHandler handler)
70     {
71         if (eventId < UPDATER_INIT_EVENT_BUTT) {
72             initEvent_[eventId].push_back(handler);
73         }
74     }
75 private:
76     UpdaterInit() = default;
77     ~UpdaterInit() = default;
78     std::vector<InitHandler> initEvent_[UPDATER_INIT_EVENT_BUTT];
79 };
80 
81 #define DEFINE_INIT_EVENT(name, event, ...)                                      \
82     static void name##_##event##_Init(void)                                      \
83     {                                                                            \
84         __VA_ARGS__;                                                             \
85     }                                                                            \
86     __attribute((constructor)) static void Register_##name##_##event(void)       \
87     {                                                                            \
88         UpdaterInit::GetInstance().SubscribeEvent(event, name##_##event##_Init); \
89     }                                                                            \
90     static_assert(true)
91 
92 // mode related macro
93 #define MODE_ENTRY(name) name##Main
94 
95 #define MODE_CONDITION(name) Is##name
96 
97 #define BOOT_MODE(name, para) BootMode { MODE_CONDITION(name), STRINGFY(name), para, MODE_ENTRY(name) }
98 
99 #define REGISTER_MODE(name, para, ...) \
100     DEFINE_INIT_EVENT(name, UPDATER_MAIN_PRE_EVENT, RegisterMode(BOOT_MODE(name, para)))
101 }
102 
103 #endif