1 /* 2 * Copyright (C) 2022 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 #ifndef FLASHD_COMMANDER_H 17 #define FLASHD_COMMANDER_H 18 19 #include "flashd/flashd.h" 20 #include "partition.h" 21 22 namespace Flashd { 23 enum class UpdaterState { 24 DOING, 25 SUCCESS, 26 FAIL 27 }; 28 29 using callbackFun = std::function<void(CmdType type, UpdaterState state, const std::string &msg)>; 30 31 class Commander { 32 public: Commander(callbackFun callback)33 explicit Commander(callbackFun callback) : callback_(callback) {} 34 virtual ~Commander() = default; 35 virtual void DoCommand([[maybe_unused]] const std::string &cmmParam, [[maybe_unused]] size_t fileSize) = 0; 36 virtual void DoCommand(const uint8_t *payload, int payloadSize) = 0; 37 virtual void PostCommand() = 0; 38 39 protected: 40 void NotifySuccess(CmdType type, const std::string &msg = "") const; 41 void NotifyFail(CmdType type, const std::string &msg = "") const; GetCmdName()42 std::string_view GetCmdName() const 43 { 44 return cmdName_; 45 } SetCmdName(const std::string & cmdName)46 void SetCmdName(const std::string &cmdName) 47 { 48 cmdName_ = cmdName; 49 } 50 51 void UpdateProgress(CmdType type) const; 52 53 bool IsCallbackVaild() const; 54 55 size_t fileSize_ = 0; 56 57 size_t currentSize_ = 0; 58 59 int64_t startTime_ = 0; 60 61 std::string cmdName_ = ""; 62 63 private: 64 callbackFun callback_ = nullptr; 65 }; 66 } // namespace Flashd 67 #endif