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