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