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 #ifndef UPDATER_UT
41 if (!IsCallbackVaild()) {
42 return;
43 }
44 if (fileSize_ == 0) {
45 FLASHD_LOGE("file size is invaild");
46 return;
47 }
48 uint8_t currentProcess = static_cast<uint8_t>(static_cast<int64_t>(currentSize_) * PERCENT_FINISH / fileSize_);
49 if (currentProcess > PERCENT_FINISH) {
50 FLASHD_LOGW("currSize_ = %u, fileSize_ = %u, currentProces = %u", currentSize_, fileSize_, currentProcess);
51 return;
52 }
53
54 static uint8_t preProcess = 0;
55 if (currentProcess == preProcess) {
56 return;
57 }
58 preProcess = currentProcess;
59 callback_(type, UpdaterState::DOING, std::to_string(currentProcess));
60 #endif
61 }
62
IsCallbackVaild() const63 bool Commander::IsCallbackVaild() const
64 {
65 if (callback_ == nullptr) {
66 FLASHD_LOGE("callback_ is null");
67 return false;
68 }
69 return true;
70 }
71 } // namespace Flashd