• 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 suport. If not defined, it should be [device]buildroot or [PC]msys64(...)/ubuntu-apt(...)
22 envirments
23 ############
24 */
25 #include "system_depend.h"
26 #include "../common/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,string preDefine)49     bool GetDevItem(const char *key, string &out, string preDefine)
50     {
51         bool ret = true;
52         char tmpStringBuf[BUF_SIZE_MEDIUM] = "";
53 #ifdef HARMONY_PROJECT
54         const string strKey(key);
55         if (GetParameter(key, preDefine.c_str(), tmpStringBuf, BUF_SIZE_MEDIUM) < 0) {
56             ret = false;
57             Base::ZeroStruct(tmpStringBuf);
58         }
59 #else
60         string sFailString = Base::StringFormat("Get parameter \"%s\" fail", key);
61         string stringBuf = "param get " + string(key);
62         Base::RunPipeComand(stringBuf.c_str(), tmpStringBuf, BUF_SIZE_MEDIUM - 1, true);
63         if (!strcmp(sFailString.c_str(), tmpStringBuf)) {
64             // failed
65             ret = false;
66             Base::ZeroStruct(tmpStringBuf);
67         }
68 #endif
69         out = tmpStringBuf;
70         return ret;
71     }
72 
CallDoReboot(const char * reason)73     bool CallDoReboot(const char *reason)
74     {
75 #ifdef HARMONY_PROJECT
76         return DoReboot(reason);
77 #else
78         return false;
79 #endif
80     }
81 
RebootDevice(const string & cmd)82     bool RebootDevice(const string &cmd)
83     {
84         string reason;
85         if (cmd == "recovery") {
86             reason = "updater";
87         } else if (cmd == "bootloader") {
88             reason = "updater";
89         }
90         WRITE_LOG(LOG_DEBUG, "DoReboot with args:[%s] for cmd:[%s]", reason.c_str(), cmd.c_str());
91         return CallDoReboot(reason.c_str());
92     }
93 }
94 }  // namespace Hdc
95