1 /* 2 * Copyright (c) 2021 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_MAIN_H 16 #define UPDATER_MAIN_H 17 18 #include <iostream> 19 #include <string> 20 #include <vector> 21 #include "macros.h" 22 #include "updater/updater.h" 23 24 namespace Updater { 25 enum UpdaterInitEvent { 26 // updater 27 UPDATER_PRE_INIT_EVENT = 0, 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 UPDATER_INIT_EVENT_BUTT 39 }; 40 41 using InitHandler = void (*)(void); 42 43 class UpdaterInit { 44 DISALLOW_COPY_MOVE(UpdaterInit); 45 public: GetInstance()46 static UpdaterInit &GetInstance() 47 { 48 static UpdaterInit instance; 49 return instance; 50 } InvokeEvent(enum UpdaterInitEvent eventId)51 void InvokeEvent(enum UpdaterInitEvent eventId) const 52 { 53 if (eventId >= UPDATER_INIT_EVENT_BUTT) { 54 return; 55 } 56 for (const auto &handler : initEvent_[eventId]) { 57 if (handler != nullptr) { 58 handler(); 59 } 60 } 61 } SubscribeEvent(enum UpdaterInitEvent eventId,InitHandler handler)62 void SubscribeEvent(enum UpdaterInitEvent eventId, InitHandler handler) 63 { 64 if (eventId < UPDATER_INIT_EVENT_BUTT) { 65 initEvent_[eventId].push_back(handler); 66 } 67 } 68 private: 69 UpdaterInit() = default; 70 ~UpdaterInit() = default; 71 std::vector<InitHandler> initEvent_[UPDATER_INIT_EVENT_BUTT]; 72 }; 73 74 enum FactoryResetMode { 75 USER_WIPE_DATA = 0, 76 FACTORY_WIPE_DATA, 77 }; 78 79 int UpdaterMain(int argc, char **argv); 80 81 int FactoryReset(FactoryResetMode mode, const std::string &path); 82 83 UpdaterStatus UpdaterFromSdcard(); 84 85 bool IsBatteryCapacitySufficient(); 86 } // namespace Updater 87 #endif // UPDATER_MAIN_H 88