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