1/*
2 * Copyright (C) 2021 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############
17This file is used to support compatibility between platforms, differences between old and new projects and
18compilation platforms
19
20defined HARMONY_PROJECT
21With openharmony toolchains support. If not defined, it should be [device]buildroot or [PC]msys64(...)/ubuntu-apt(...)
22envirments
23############
24*/
25#include "system_depend.h"
26#include "base.h"
27#if defined(HARMONY_PROJECT)
28extern "C" {
29#include "init_reboot.h"
30#include "parameter.h"
31}
32#endif
33
34namespace Hdc {
35namespace SystemDepend {
36    bool SetDevItem(const char *key, const char *value)
37    {
38        bool ret = true;
39#ifdef HARMONY_PROJECT
40        ret = SetParameter(key, value) == 0;
41#else
42        char outBuf[256] = "";
43        string stringBuf = Base::StringFormat("param set %s %s", key, value);
44        Base::RunPipeComand(stringBuf.c_str(), outBuf, sizeof(outBuf), true);
45#endif  // HARMONY_PROJECT
46        return ret;
47    }
48
49    bool GetDevItem(const char *key, string &out, const char *preDefine)
50    {
51        bool ret = true;
52        char tmpStringBuf[BUF_SIZE_MEDIUM] = "";
53#ifdef HARMONY_PROJECT
54        auto res = GetParameter(key, preDefine, tmpStringBuf, BUF_SIZE_MEDIUM);
55        if (res <= 0) {
56            return false;
57        }
58#else
59        string sFailString = Base::StringFormat("Get parameter \"%s\" fail", key);
60        string stringBuf = "param get " + string(key);
61        Base::RunPipeComand(stringBuf.c_str(), tmpStringBuf, BUF_SIZE_MEDIUM - 1, true);
62        if (!strcmp(sFailString.c_str(), tmpStringBuf)) {
63            // failed
64            ret = false;
65            Base::ZeroBuf(tmpStringBuf, BUF_SIZE_MEDIUM);
66        }
67#endif
68        out = tmpStringBuf;
69        return ret;
70    }
71
72    uint32_t GetDevUint(const char *key, uint32_t defaultValue)
73    {
74        return GetUintParameter(key, defaultValue);
75    }
76
77    bool CallDoReboot(const char *reason)
78    {
79        string rebootCtrl = "ohos.startup.powerctrl";
80#ifdef HARMONY_PROJECT
81        return SetDevItem(rebootCtrl.c_str(), reason);
82#else
83        return false;
84#endif
85    }
86
87    bool RebootDevice(const string &cmd)
88    {
89        string reason = "reboot";
90        if (cmd != "") {
91            reason += ",";
92            reason += cmd;
93        }
94        WRITE_LOG(LOG_DEBUG, "DoReboot with args:[%s] for cmd:[%s]", reason.c_str(), cmd.c_str());
95        return CallDoReboot(reason.c_str());
96    }
97}
98}  // namespace Hdc
99