1 /*
2 * Copyright (c) 2023 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 "device_adapter.h"
17
18 #include <ctime>
19 #include <cstdlib>
20 #include <fstream>
21 #include <iomanip>
22 #include <ohos_types.h>
23 #include <securec.h>
24 #include <sstream>
25
26 #include "param/init_param.h"
27 #include "parameter.h"
28 #include "securec.h"
29
30 #include "constant.h"
31 #include "config_parse.h"
32 #include "encrypt_utils.h"
33 #include "update_log.h"
34 #include "update_helper.h"
35
36 namespace OHOS {
37 namespace UpdateEngine {
38 constexpr int32_t MAX_PARAM_VALUE_LEN = 128;
39
IsValidParamValue(const char * value,uint32_t len)40 bool DeviceAdapter::IsValidParamValue(const char *value, uint32_t len)
41 {
42 return (value != NULL) && (strlen(value) + 1 <= len);
43 }
44
GetParameter(const std::string & key,const std::string & def)45 std::string DeviceAdapter::GetParameter(const std::string &key, const std::string &def)
46 {
47 uint32_t size = 0;
48 int ret = SystemReadParam(key.c_str(), NULL, &size);
49 if (ret == 0) {
50 std::vector<char> value(size + 1);
51 ret = SystemReadParam(key.c_str(), value.data(), &size);
52 if (ret == 0) {
53 return std::string(value.data());
54 }
55 }
56 if (IsValidParamValue(def.c_str(), MAX_PARAM_VALUE_LEN)) {
57 return std::string(def);
58 }
59 return "";
60 }
61
GetDeviceName()62 std::string DeviceAdapter::GetDeviceName()
63 {
64 return GetParameter("const.product.model", "");
65 }
66
GetOsVersion()67 std::string DeviceAdapter::GetOsVersion()
68 {
69 return GetParameter("const.ohos.fullname", "");
70 }
71
GetDisplayVersion()72 std::string DeviceAdapter::GetDisplayVersion()
73 {
74 ENGINE_LOGI("GetDisplayVersion %{public}s", GetParameter("const.product.software.version", "").c_str());
75 return GetParameter("const.product.software.version", "");
76 }
77
GetRealVersion()78 std::string DeviceAdapter::GetRealVersion()
79 {
80 return GetParameter("const.build.ver.physical", "");
81 }
82
GetBootSlot()83 std::string DeviceAdapter::GetBootSlot()
84 {
85 return GetParameter("ohos.boot.bootslots", "0"); // 0:not ab,2:ab; default 0
86 }
87 } // namespace UpdateEngine
88 } // namespace OHOS
89