• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ############
17 This file is used to support compatibility between platforms, differences between old and new projects and
18 compilation platforms
19 
20 defined HARMONY_PROJECT
21 With openharmony toolchains support. If not defined, it should be [device]buildroot or [PC]msys64(...)/ubuntu-apt(...)
22 envirments
23 ############
24 */
25 #include "system_depend.h"
26 #include "base.h"
27 #if defined(HARMONY_PROJECT)
28 extern "C" {
29 #include "init_reboot.h"
30 #include "parameter.h"
31 }
32 #endif
33 
34 namespace Hdc {
35 namespace SystemDepend {
SetDevItem(const char * key,const char * value)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 
GetDevItem(const char * key,string & out,const char * preDefine)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::ZeroStruct(tmpStringBuf);
66         }
67 #endif
68         out = tmpStringBuf;
69         return ret;
70     }
71 
CallDoReboot(const char * reason)72     bool CallDoReboot(const char *reason)
73     {
74         string rebootCtrl = "ohos.startup.powerctrl";
75 #ifdef HARMONY_PROJECT
76         return SetDevItem(rebootCtrl.c_str(), reason);
77 #else
78         return false;
79 #endif
80     }
81 
RebootDevice(const string & cmd)82     bool RebootDevice(const string &cmd)
83     {
84         string reason = "reboot";
85         if (cmd != "") {
86             reason += ",";
87             reason += cmd;
88         }
89         WRITE_LOG(LOG_DEBUG, "DoReboot with args:[%s] for cmd:[%s]", reason.c_str(), cmd.c_str());
90         return CallDoReboot(reason.c_str());
91     }
92 }
93 }  // namespace Hdc
94