• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "update_engine_utils.h"
16 
17 #include <fstream>
18 #include "securec.h"
19 #include "intell_voice_log.h"
20 #include "string_util.h"
21 #include "history_info_mgr.h"
22 #include "intell_voice_definitions.h"
23 
24 #define LOG_TAG "UpdateEngineUtils"
25 
26 using namespace OHOS::IntellVoiceUtils;
27 
28 namespace OHOS {
29 namespace IntellVoiceEngine {
30 const std::string INTELL_VOICE_DEFAULT_RES_PATH = "/sys_prod/variant/region_comm/china/etc/intellvoice/wakeup/";
31 const std::string INTELL_VOICE_CONDICT_PATH_DSP = "/dsp/condict/";
32 static constexpr uint32_t WAKEUP_VERSION_SPLIT_NUM = 2;
33 static constexpr uint32_t WAKEUP_VERSION_NUMBERS = 3;
34 
UpdateEngineUtils()35 UpdateEngineUtils::UpdateEngineUtils()
36 {
37 }
38 
~UpdateEngineUtils()39 UpdateEngineUtils::~UpdateEngineUtils()
40 {
41 }
42 
GetWakeupVesion(std::string & versionNumber)43 void UpdateEngineUtils::GetWakeupVesion(std::string &versionNumber)
44 {
45     std::string filePath = INTELL_VOICE_DEFAULT_RES_PATH + INTELL_VOICE_CONDICT_PATH_DSP + "version.txt";
46     std::string wakeupVersion;
47     std::ifstream file(filePath);
48 
49     INTELL_VOICE_LOG_INFO("enter");
50     if (!file.good()) {
51         INTELL_VOICE_LOG_ERROR("Open file(%{public}s) failed", filePath.c_str());
52         return;
53     }
54 
55     getline(file, wakeupVersion);
56 
57     if (wakeupVersion.empty()) {
58         INTELL_VOICE_LOG_ERROR("wakeupVersion empty");
59         return;
60     }
61 
62     INTELL_VOICE_LOG_INFO("wakeupVersion: (%{public}s)", wakeupVersion.c_str());
63 
64     std::vector<std::string> tokens;
65     StringUtil::Split(wakeupVersion, "v.", tokens);
66     if (tokens.size() != WAKEUP_VERSION_SPLIT_NUM || tokens[1].empty()) {
67         INTELL_VOICE_LOG_INFO("wakeupVersion split empty (%{public}zu)", tokens.size());
68         return;
69     }
70     /* 5.2.1 -> 050201 */
71     std::string version = tokens[1];
72     tokens.resize(0);
73     StringUtil::Split(version, ".", tokens);
74     if (tokens.size() != WAKEUP_VERSION_NUMBERS) {
75         INTELL_VOICE_LOG_INFO("vesion split empty");
76         return;
77     }
78 
79     for (size_t index = 0; index < tokens.size(); index++) {
80         if (tokens[index].length() == 1) {
81             tokens[index] = "0" + tokens[index];
82         }
83         versionNumber += tokens[index];
84     }
85     INTELL_VOICE_LOG_INFO("exit (%{public}s)", versionNumber.c_str());
86 }
87 
SaveWakeupVesion()88 void UpdateEngineUtils::SaveWakeupVesion()
89 {
90     std::string versionNumber;
91     HistoryInfoMgr &historyInfoMgr = HistoryInfoMgr::GetInstance();
92 
93     GetWakeupVesion(versionNumber);
94     if (versionNumber.empty()) {
95         return;
96     }
97 
98     historyInfoMgr.SetStringKVPair(KEY_WAKEUP_VESRION, versionNumber);
99 }
100 
IsVersionUpdate()101 bool UpdateEngineUtils::IsVersionUpdate()
102 {
103     std::string versionNumberSave;
104     std::string versionNumberCur;
105     HistoryInfoMgr &historyInfoMgr = HistoryInfoMgr::GetInstance();
106 
107     versionNumberSave = historyInfoMgr.GetStringKVPair(KEY_WAKEUP_VESRION);
108     if (versionNumberSave.empty()) {
109         INTELL_VOICE_LOG_ERROR("versionNumberSave is null");
110         return false;
111     }
112 
113     GetWakeupVesion(versionNumberCur);
114     if (versionNumberCur.empty()) {
115         INTELL_VOICE_LOG_ERROR("versionNumberCur is null");
116         return false;
117     }
118 
119     int32_t historyVersion = 0;
120     int32_t curVersion = 0;
121     if ((!StringUtil::StringToInt(versionNumberSave, historyVersion)) ||
122         (!StringUtil::StringToInt(versionNumberCur, curVersion))) {
123         INTELL_VOICE_LOG_ERROR("failed to get version");
124         return false;
125     }
126 
127     if (curVersion > historyVersion) {
128         INTELL_VOICE_LOG_INFO("version new %{public}d, cur %{public}d", curVersion, historyVersion);
129         return true;
130     }
131     return false;
132 }
133 }
134 }