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 #include "commander.h"
17
18 #include "flashd_define.h"
19
20 namespace Flashd {
NotifySuccess(CmdType type,const std::string & msg) const21 void Commander::NotifySuccess(CmdType type, const std::string &msg) const
22 {
23 if (!IsCallbackVaild()) {
24 return;
25 }
26 callback_(type, UpdaterState::SUCCESS, "[Success] " + msg);
27 }
28
NotifyFail(CmdType type,const std::string & msg) const29 void Commander::NotifyFail(CmdType type, const std::string &msg) const
30 {
31 if (!IsCallbackVaild()) {
32 return;
33 }
34 callback_(type, UpdaterState::FAIL, " " + msg);
35 }
36
37
UpdateProgress(CmdType type) const38 void Commander::UpdateProgress(CmdType type) const
39 {
40 if (!IsCallbackVaild()) {
41 return;
42 }
43 if (fileSize_ == 0) {
44 FLASHD_LOGE("file size is invaild");
45 return;
46 }
47 uint8_t currentProcess = static_cast<uint8_t>(static_cast<int64_t>(currentSize_) * PERCENT_FINISH / fileSize_);
48 if (currentProcess > PERCENT_FINISH) {
49 FLASHD_LOGW("currSize_ = %u, fileSize_ = %u, currentProces = %u", currentSize_, fileSize_, currentProcess);
50 return;
51 }
52
53 static uint8_t preProcess = 0;
54 if (currentProcess == preProcess) {
55 return;
56 }
57 preProcess = currentProcess;
58 callback_(type, UpdaterState::DOING, std::to_string(currentProcess));
59 }
60
IsCallbackVaild() const61 bool Commander::IsCallbackVaild() const
62 {
63 if (callback_ == nullptr) {
64 FLASHD_LOGE("callback_ is null");
65 return false;
66 }
67 return true;
68 }
69 } // namespace Flashd