1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#ifndef UPDATER_INIT_H 16fb299fa2Sopenharmony_ci#define UPDATER_INIT_H 17fb299fa2Sopenharmony_ci 18fb299fa2Sopenharmony_ci#include <vector> 19fb299fa2Sopenharmony_ci#include "macros_updater.h" 20fb299fa2Sopenharmony_ci 21fb299fa2Sopenharmony_cinamespace Updater { 22fb299fa2Sopenharmony_cienum UpdaterInitEvent { 23fb299fa2Sopenharmony_ci // main 24fb299fa2Sopenharmony_ci UPDATER_MAIN_PRE_EVENT = 0, 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_ci // updater 27fb299fa2Sopenharmony_ci UPDATER_PRE_INIT_EVENT, 28fb299fa2Sopenharmony_ci UPDATER_INIT_EVENT, 29fb299fa2Sopenharmony_ci UPDATER_PRE_UPDATE_PACKAGE_EVENT, 30fb299fa2Sopenharmony_ci UPDATER_POST_UPDATE_PACKAGE_EVENT, 31fb299fa2Sopenharmony_ci UPDATER_POST_INIT_EVENT, 32fb299fa2Sopenharmony_ci 33fb299fa2Sopenharmony_ci // flashd 34fb299fa2Sopenharmony_ci FLAHSD_PRE_INIT_EVENT, 35fb299fa2Sopenharmony_ci 36fb299fa2Sopenharmony_ci // binary 37fb299fa2Sopenharmony_ci UPDATER_BINARY_INIT_EVENT, 38fb299fa2Sopenharmony_ci UPDATER_BINARY_INIT_DONE_EVENT, 39fb299fa2Sopenharmony_ci 40fb299fa2Sopenharmony_ci // factory reset 41fb299fa2Sopenharmony_ci FACTORY_RESET_INIT_EVENT, 42fb299fa2Sopenharmony_ci 43fb299fa2Sopenharmony_ci UPDATER_INIT_EVENT_BUTT 44fb299fa2Sopenharmony_ci}; 45fb299fa2Sopenharmony_ci 46fb299fa2Sopenharmony_ciusing InitHandler = void (*)(void); 47fb299fa2Sopenharmony_ci 48fb299fa2Sopenharmony_ciclass UpdaterInit { 49fb299fa2Sopenharmony_ci DISALLOW_COPY_MOVE(UpdaterInit); 50fb299fa2Sopenharmony_cipublic: 51fb299fa2Sopenharmony_ci static UpdaterInit &GetInstance() 52fb299fa2Sopenharmony_ci { 53fb299fa2Sopenharmony_ci static UpdaterInit instance; 54fb299fa2Sopenharmony_ci return instance; 55fb299fa2Sopenharmony_ci } 56fb299fa2Sopenharmony_ci void InvokeEvent(enum UpdaterInitEvent eventId) const 57fb299fa2Sopenharmony_ci { 58fb299fa2Sopenharmony_ci if (eventId >= UPDATER_INIT_EVENT_BUTT) { 59fb299fa2Sopenharmony_ci return; 60fb299fa2Sopenharmony_ci } 61fb299fa2Sopenharmony_ci for (const auto &handler : initEvent_[eventId]) { 62fb299fa2Sopenharmony_ci if (handler != nullptr) { 63fb299fa2Sopenharmony_ci handler(); 64fb299fa2Sopenharmony_ci } 65fb299fa2Sopenharmony_ci } 66fb299fa2Sopenharmony_ci } 67fb299fa2Sopenharmony_ci void SubscribeEvent(enum UpdaterInitEvent eventId, InitHandler handler) 68fb299fa2Sopenharmony_ci { 69fb299fa2Sopenharmony_ci if (eventId < UPDATER_INIT_EVENT_BUTT) { 70fb299fa2Sopenharmony_ci initEvent_[eventId].push_back(handler); 71fb299fa2Sopenharmony_ci } 72fb299fa2Sopenharmony_ci } 73fb299fa2Sopenharmony_ciprivate: 74fb299fa2Sopenharmony_ci UpdaterInit() = default; 75fb299fa2Sopenharmony_ci ~UpdaterInit() = default; 76fb299fa2Sopenharmony_ci std::vector<InitHandler> initEvent_[UPDATER_INIT_EVENT_BUTT]; 77fb299fa2Sopenharmony_ci}; 78fb299fa2Sopenharmony_ci 79fb299fa2Sopenharmony_ci#define DEFINE_INIT_EVENT(name, event, ...) \ 80fb299fa2Sopenharmony_ci static void name##_##event##_Init(void) \ 81fb299fa2Sopenharmony_ci { \ 82fb299fa2Sopenharmony_ci __VA_ARGS__; \ 83fb299fa2Sopenharmony_ci } \ 84fb299fa2Sopenharmony_ci __attribute((constructor)) static void Register_##name##_##event(void) \ 85fb299fa2Sopenharmony_ci { \ 86fb299fa2Sopenharmony_ci UpdaterInit::GetInstance().SubscribeEvent(event, name##_##event##_Init); \ 87fb299fa2Sopenharmony_ci } \ 88fb299fa2Sopenharmony_ci static_assert(true) 89fb299fa2Sopenharmony_ci 90fb299fa2Sopenharmony_ci// mode related macro 91fb299fa2Sopenharmony_ci#define MODE_ENTRY(name) name##Main 92fb299fa2Sopenharmony_ci 93fb299fa2Sopenharmony_ci#define MODE_CONDITION(name) Is##name 94fb299fa2Sopenharmony_ci 95fb299fa2Sopenharmony_ci#define BOOT_MODE(name, para) BootMode { MODE_CONDITION(name), STRINGFY(name), para, MODE_ENTRY(name) } 96fb299fa2Sopenharmony_ci 97fb299fa2Sopenharmony_ci#define REGISTER_MODE(name, para, ...) \ 98fb299fa2Sopenharmony_ci DEFINE_INIT_EVENT(name, UPDATER_MAIN_PRE_EVENT, RegisterMode(BOOT_MODE(name, para))) 99fb299fa2Sopenharmony_ci} 100fb299fa2Sopenharmony_ci 101fb299fa2Sopenharmony_ci#endif