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 "module_update_proxy.h"
17
18 #include "log/log.h"
19 #include "module_ipc_helper.h"
20 #include "string_ex.h"
21 #include "sys_installer_sa_ipc_interface_code.h"
22
23 namespace OHOS {
24 namespace SysInstaller {
25 using namespace Updater;
26
InstallModulePackage(const std::string & pkgPath)27 int32_t ModuleUpdateProxy::InstallModulePackage(const std::string &pkgPath)
28 {
29 LOG(INFO) << "InstallModulePackage " << pkgPath;
30 auto remote = Remote();
31 if (remote == nullptr) {
32 LOG(ERROR) << "Can not get remote";
33 return ERR_FLATTEN_OBJECT;
34 }
35
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(GetDescriptor())) {
38 LOG(ERROR) << "WriteInterfaceToken error";
39 return ERR_FLATTEN_OBJECT;
40 }
41 data.WriteString16(Str8ToStr16(pkgPath));
42
43 MessageParcel reply;
44 MessageOption option;
45 int32_t ret = remote->SendRequest(
46 static_cast<uint32_t>(ModuleUpdateInterfaceCode::INSTALL_MODULE_PACKAGE), data, reply, option);
47 if (ret != ERR_OK) {
48 LOG(ERROR) << "SendRequest error";
49 return ERR_FLATTEN_OBJECT;
50 }
51
52 return reply.ReadInt32();
53 }
54
UninstallModulePackage(const std::string & hmpName)55 int32_t ModuleUpdateProxy::UninstallModulePackage(const std::string &hmpName)
56 {
57 LOG(INFO) << "UninstallModulePackage " << hmpName;
58 auto remote = Remote();
59 if (remote == nullptr) {
60 LOG(ERROR) << "Can not get remote";
61 return ERR_FLATTEN_OBJECT;
62 }
63
64 MessageParcel data;
65 if (!data.WriteInterfaceToken(GetDescriptor())) {
66 LOG(ERROR) << "WriteInterfaceToken error";
67 return ERR_FLATTEN_OBJECT;
68 }
69 data.WriteString16(Str8ToStr16(hmpName));
70
71 MessageParcel reply;
72 MessageOption option;
73 int32_t ret = remote->SendRequest(
74 static_cast<uint32_t>(ModuleUpdateInterfaceCode::UNINSTALL_MODULE_PACKAGE), data, reply, option);
75 if (ret != ERR_OK) {
76 LOG(ERROR) << "SendRequest error";
77 return ERR_FLATTEN_OBJECT;
78 }
79
80 return reply.ReadInt32();
81 }
82
GetModulePackageInfo(const std::string & hmpName,std::list<ModulePackageInfo> & modulePackageInfos)83 int32_t ModuleUpdateProxy::GetModulePackageInfo(const std::string &hmpName,
84 std::list<ModulePackageInfo> &modulePackageInfos)
85 {
86 LOG(INFO) << "GetModulePackageInfo " << hmpName;
87 auto remote = Remote();
88 if (remote == nullptr) {
89 LOG(ERROR) << "Can not get remote";
90 return ERR_FLATTEN_OBJECT;
91 }
92
93 MessageParcel data;
94 if (!data.WriteInterfaceToken(GetDescriptor())) {
95 LOG(ERROR) << "WriteInterfaceToken error";
96 return ERR_FLATTEN_OBJECT;
97 }
98 data.WriteString16(Str8ToStr16(hmpName));
99
100 MessageParcel reply;
101 MessageOption option;
102 int32_t ret = remote->SendRequest(
103 static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_MODULE_PACKAGE_INFO), data, reply, option);
104 if (ret != ERR_OK) {
105 LOG(ERROR) << "SendRequest error";
106 return ERR_FLATTEN_OBJECT;
107 }
108
109 ModuleIpcHelper::ReadModulePackageInfos(reply, modulePackageInfos);
110 return reply.ReadInt32();
111 }
112
ReportModuleUpdateStatus(const ModuleUpdateStatus & status)113 int32_t ModuleUpdateProxy::ReportModuleUpdateStatus(const ModuleUpdateStatus &status)
114 {
115 LOG(INFO) << "ReportModuleUpdateStatus process=" << status.process;
116 auto remote = Remote();
117 if (remote == nullptr) {
118 LOG(ERROR) << "Can not get remote";
119 return ERR_FLATTEN_OBJECT;
120 }
121
122 MessageParcel data;
123 if (!data.WriteInterfaceToken(GetDescriptor())) {
124 LOG(ERROR) << "WriteInterfaceToken error";
125 return ERR_FLATTEN_OBJECT;
126 }
127 ModuleIpcHelper::WriteModuleUpdateStatus(data, status);
128
129 MessageParcel reply;
130 MessageOption option;
131 int32_t ret = remote->SendRequest(
132 static_cast<uint32_t>(ModuleUpdateInterfaceCode::REPORT_MODULE_UPDATE_STATUS), data, reply, option);
133 if (ret != ERR_OK) {
134 LOG(ERROR) << "SendRequest error";
135 return ERR_FLATTEN_OBJECT;
136 }
137
138 return reply.ReadInt32();
139 }
140
ExitModuleUpdate()141 int32_t ModuleUpdateProxy::ExitModuleUpdate()
142 {
143 LOG(INFO) << "ExitModuleUpdate";
144 auto remote = Remote();
145 if (remote == nullptr) {
146 LOG(ERROR) << "Can not get remote";
147 return ERR_FLATTEN_OBJECT;
148 }
149
150 MessageParcel data;
151 if (!data.WriteInterfaceToken(GetDescriptor())) {
152 LOG(ERROR) << "WriteInterfaceToken error";
153 return ERR_FLATTEN_OBJECT;
154 }
155
156 MessageParcel reply;
157 MessageOption option;
158 int32_t ret = remote->SendRequest(
159 static_cast<uint32_t>(ModuleUpdateInterfaceCode::EXIT_MODULE_UPDATE), data, reply, option);
160 if (ret != ERR_OK) {
161 LOG(ERROR) << "SendRequest error";
162 return ERR_FLATTEN_OBJECT;
163 }
164
165 return reply.ReadInt32();
166 }
167
GetHmpVersionInfo()168 std::vector<HmpVersionInfo> ModuleUpdateProxy::GetHmpVersionInfo()
169 {
170 LOG(INFO) << "GetHmpVersionInfo";
171 std::vector<HmpVersionInfo> versionInfo {};
172 auto remote = Remote();
173 if (remote == nullptr) {
174 LOG(ERROR) << "Can not get remote";
175 return versionInfo;
176 }
177
178 MessageParcel data;
179 if (!data.WriteInterfaceToken(GetDescriptor())) {
180 LOG(ERROR) << "WriteInterfaceToken error";
181 return versionInfo;
182 }
183
184 MessageParcel reply;
185 MessageOption option;
186 int32_t ret = remote->SendRequest(
187 static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_HMP_VERSION_INFO), data, reply, option);
188 if (ret != ERR_OK) {
189 LOG(ERROR) << "SendRequest error";
190 return versionInfo;
191 }
192
193 int32_t count = reply.ReadInt32();
194 for (int32_t i = 0; i < count; i++) {
195 std::unique_ptr<HmpVersionInfo> infoPtr(reply.ReadParcelable<HmpVersionInfo>());
196 if (infoPtr != nullptr) {
197 versionInfo.push_back(*infoPtr);
198 }
199 }
200 return versionInfo;
201 }
202
StartUpdateHmpPackage(const std::string & path,const sptr<ISysInstallerCallback> & updateCallback)203 int32_t ModuleUpdateProxy::StartUpdateHmpPackage(const std::string &path,
204 const sptr<ISysInstallerCallback> &updateCallback)
205 {
206 LOG(INFO) << "StartUpdateHmpPackage";
207 if (updateCallback == nullptr) {
208 LOG(ERROR) << "Invalid param";
209 return ERR_INVALID_VALUE;
210 }
211 auto remote = Remote();
212 if (remote == nullptr) {
213 LOG(ERROR) << "Can not get remote";
214 return ERR_FLATTEN_OBJECT;
215 }
216
217 MessageParcel data;
218 if (!data.WriteInterfaceToken(GetDescriptor())) {
219 LOG(ERROR) << "WriteInterfaceToken error";
220 return ERR_FLATTEN_OBJECT;
221 }
222
223 if (!data.WriteRemoteObject(updateCallback->AsObject())) {
224 LOG(ERROR) << "WriteRemoteObject fail";
225 return ERR_FLATTEN_OBJECT;
226 }
227 data.WriteString16(Str8ToStr16(path));
228
229 MessageParcel reply;
230 MessageOption option;
231 int32_t ret = remote->SendRequest(
232 static_cast<uint32_t>(ModuleUpdateInterfaceCode::START_UPDATE_HMP_PACKAGE), data, reply, option);
233 if (ret != ERR_OK) {
234 LOG(ERROR) << "SendRequest error";
235 return ERR_FLATTEN_OBJECT;
236 }
237
238 return reply.ReadInt32();
239 }
240
GetHmpUpdateResult()241 std::vector<HmpUpdateInfo> ModuleUpdateProxy::GetHmpUpdateResult()
242 {
243 LOG(INFO) << "GetHmpUpdateResult";
244 std::vector<HmpUpdateInfo> updateInfo {};
245 auto remote = Remote();
246 if (remote == nullptr) {
247 LOG(ERROR) << "Can not get remote";
248 return updateInfo;
249 }
250
251 MessageParcel data;
252 if (!data.WriteInterfaceToken(GetDescriptor())) {
253 LOG(ERROR) << "WriteInterfaceToken error";
254 return updateInfo;
255 }
256
257 MessageParcel reply;
258 MessageOption option;
259 int32_t ret = remote->SendRequest(
260 static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_HMP_UPDATE_RESULT), data, reply, option);
261 if (ret != ERR_OK) {
262 LOG(ERROR) << "SendRequest error";
263 return updateInfo;
264 }
265
266 int32_t count = reply.ReadInt32();
267 for (int32_t i = 0; i < count; i++) {
268 std::unique_ptr<HmpUpdateInfo> infoPtr(reply.ReadParcelable<HmpUpdateInfo>());
269 if (infoPtr != nullptr) {
270 updateInfo.push_back(*infoPtr);
271 }
272 }
273 return updateInfo;
274 }
275 } // namespace SysInstaller
276 } // namespace OHOS
277