1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2023 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#include "updater/hwfault_retry.h"
17fb299fa2Sopenharmony_ci#include <unistd.h>
18fb299fa2Sopenharmony_ci#include "init_reboot.h"
19fb299fa2Sopenharmony_ci#include "log/log.h"
20fb299fa2Sopenharmony_ci#include "misc_info/misc_info.h"
21fb299fa2Sopenharmony_ci#include "updater/updater.h"
22fb299fa2Sopenharmony_ci#include "updater/updater_const.h"
23fb299fa2Sopenharmony_ci#include "utils.h"
24fb299fa2Sopenharmony_ci#include "securec.h"
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cinamespace Updater {
27fb299fa2Sopenharmony_ciHwFaultRetry &HwFaultRetry::GetInstance()
28fb299fa2Sopenharmony_ci{
29fb299fa2Sopenharmony_ci    static HwFaultRetry instance;
30fb299fa2Sopenharmony_ci    return instance;
31fb299fa2Sopenharmony_ci}
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_ciHwFaultRetry::HwFaultRetry()
34fb299fa2Sopenharmony_ci{
35fb299fa2Sopenharmony_ci    RetryFunc rebootFunc = [this]() {
36fb299fa2Sopenharmony_ci                                return this->RebootRetry();
37fb299fa2Sopenharmony_ci                            };
38fb299fa2Sopenharmony_ci    RegisterFunc(VERIFY_FAILED_REBOOT, rebootFunc);
39fb299fa2Sopenharmony_ci    RegisterFunc(IO_FAILED_REBOOT, rebootFunc);
40fb299fa2Sopenharmony_ci}
41fb299fa2Sopenharmony_ci
42fb299fa2Sopenharmony_civoid HwFaultRetry::RegisterFunc(const std::string &faultInfo, RetryFunc func)
43fb299fa2Sopenharmony_ci{
44fb299fa2Sopenharmony_ci    if (!retryMap_.emplace(faultInfo, func).second) {
45fb299fa2Sopenharmony_ci        LOG(ERROR) << "emplace: " << faultInfo.c_str() << " fail";
46fb299fa2Sopenharmony_ci    }
47fb299fa2Sopenharmony_ci}
48fb299fa2Sopenharmony_ci
49fb299fa2Sopenharmony_civoid HwFaultRetry::DoRetryAction()
50fb299fa2Sopenharmony_ci{
51fb299fa2Sopenharmony_ci    auto it = retryMap_.find(faultInfo_);
52fb299fa2Sopenharmony_ci    if (it == retryMap_.end() || it->second == nullptr) {
53fb299fa2Sopenharmony_ci        LOG(ERROR) << "GetRepair func for: " << faultInfo_.c_str() << " fail";
54fb299fa2Sopenharmony_ci        return;
55fb299fa2Sopenharmony_ci    }
56fb299fa2Sopenharmony_ci    return (it->second)();
57fb299fa2Sopenharmony_ci}
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_civoid HwFaultRetry::SetFaultInfo(const std::string &faultInfo)
60fb299fa2Sopenharmony_ci{
61fb299fa2Sopenharmony_ci    faultInfo_ = faultInfo;
62fb299fa2Sopenharmony_ci}
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_civoid HwFaultRetry::SetRetryCount(const uint32_t count)
65fb299fa2Sopenharmony_ci{
66fb299fa2Sopenharmony_ci    retryCount_ = count;
67fb299fa2Sopenharmony_ci}
68fb299fa2Sopenharmony_ci
69fb299fa2Sopenharmony_civoid HwFaultRetry::RebootRetry()
70fb299fa2Sopenharmony_ci{
71fb299fa2Sopenharmony_ci    if (retryCount_ >= MAX_RETRY_COUNT) {
72fb299fa2Sopenharmony_ci        LOG(INFO) << "retry more than 3 times, no need retry";
73fb299fa2Sopenharmony_ci        return;
74fb299fa2Sopenharmony_ci    }
75fb299fa2Sopenharmony_ci
76fb299fa2Sopenharmony_ci    Utils::AddUpdateInfoToMisc("retry_count", retryCount_ + 1);
77fb299fa2Sopenharmony_ci    Utils::SetFaultInfoToMisc(faultInfo_);
78fb299fa2Sopenharmony_ci
79fb299fa2Sopenharmony_ci    PostUpdater(false);
80fb299fa2Sopenharmony_ci    sync();
81fb299fa2Sopenharmony_ci#ifndef UPDATER_UT
82fb299fa2Sopenharmony_ci    DoReboot("updater:Updater fault retry");
83fb299fa2Sopenharmony_ci    while (true) {
84fb299fa2Sopenharmony_ci        pause();
85fb299fa2Sopenharmony_ci    }
86fb299fa2Sopenharmony_ci#endif
87fb299fa2Sopenharmony_ci}
88fb299fa2Sopenharmony_ci} // Updater
89