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