• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "sys_installer_proxy.h"
17 
18 #include <string_ex.h>
19 
20 #include "log/log.h"
21 #include "securec.h"
22 #include "sys_installer_sa_ipc_interface_code.h"
23 
24 namespace OHOS {
25 namespace SysInstaller {
26 using namespace Updater;
27 
SysInstallerInit(bool bStreamUpgrade)28 int32_t SysInstallerProxy::SysInstallerInit(bool bStreamUpgrade)
29 {
30     LOG(INFO) << "SysInstallerInit";
31     auto remote = Remote();
32     if (remote == nullptr) {
33         LOG(ERROR) << "Can not get remote";
34         return ERR_FLATTEN_OBJECT;
35     }
36 
37     MessageParcel data;
38     if (!data.WriteInterfaceToken(GetDescriptor())) {
39         return ERR_FLATTEN_OBJECT;
40     }
41     data.WriteBool(bStreamUpgrade);
42     MessageParcel reply;
43     MessageOption option;
44     int32_t ret = remote->SendRequest(
45         static_cast<uint32_t>(SysInstallerInterfaceCode::SYS_INSTALLER_INIT), data, reply, option);
46     if (ret != ERR_OK) {
47         LOG(ERROR) << "SendRequest error";
48         return ERR_FLATTEN_OBJECT;
49     }
50 
51     return reply.ReadInt32();
52 }
53 
StartUpdatePackageZip(const std::string & pkgPath)54 int32_t SysInstallerProxy::StartUpdatePackageZip(const std::string &pkgPath)
55 {
56     LOG(INFO) << "StartUpdatePackageZip";
57     auto remote = Remote();
58     if (remote == nullptr) {
59         LOG(ERROR) << "Can not get remote";
60         return ERR_FLATTEN_OBJECT;
61     }
62 
63     MessageParcel data;
64     if (!data.WriteInterfaceToken(GetDescriptor())) {
65         LOG(ERROR) << "WriteInterfaceToken error";
66         return ERR_FLATTEN_OBJECT;
67     }
68     data.WriteString16(Str8ToStr16(pkgPath));
69 
70     MessageParcel reply;
71     MessageOption option { MessageOption::TF_ASYNC};
72     int32_t ret = remote->SendRequest(
73         static_cast<uint32_t>(SysInstallerInterfaceCode::UPDATE_PACKAGE), data, reply, option);
74     if (ret != ERR_OK) {
75         LOG(ERROR) << "SendRequest error";
76         return ERR_FLATTEN_OBJECT;
77     }
78 
79     return reply.ReadInt32();
80 }
81 
StartStreamUpdate()82 int32_t SysInstallerProxy::StartStreamUpdate()
83 {
84     LOG(INFO) << "StartStreamUpdate";
85     auto remote = Remote();
86     if (remote == nullptr) {
87         LOG(ERROR) << "Can not get remote";
88         return ERR_FLATTEN_OBJECT;
89     }
90 
91     MessageParcel data;
92     if (!data.WriteInterfaceToken(GetDescriptor())) {
93         LOG(ERROR) << "WriteInterfaceToken error";
94         return ERR_FLATTEN_OBJECT;
95     }
96 
97     MessageParcel reply;
98     MessageOption option { MessageOption::TF_SYNC};
99     int32_t ret = remote->SendRequest(
100         static_cast<uint32_t>(SysInstallerInterfaceCode::START_STREAM_UPDATE), data, reply, option);
101     if (ret != ERR_OK) {
102         LOG(ERROR) << "SendRequest error";
103         return ERR_FLATTEN_OBJECT;
104     }
105 
106     return reply.ReadInt32();
107 }
108 
StopStreamUpdate()109 int32_t SysInstallerProxy::StopStreamUpdate()
110 {
111     LOG(INFO) << "StopStreamUpdate";
112     auto remote = Remote();
113     if (remote == nullptr) {
114         LOG(ERROR) << "Can not get remote";
115         return ERR_FLATTEN_OBJECT;
116     }
117 
118     MessageParcel data;
119     if (!data.WriteInterfaceToken(GetDescriptor())) {
120         LOG(ERROR) << "WriteInterfaceToken error";
121         return ERR_FLATTEN_OBJECT;
122     }
123 
124     MessageParcel reply;
125     MessageOption option { MessageOption::TF_SYNC};
126     int32_t ret = remote->SendRequest(
127         static_cast<uint32_t>(SysInstallerInterfaceCode::STOP_STREAM_UPDATE), data, reply, option);
128     if (ret != ERR_OK) {
129         LOG(ERROR) << "SendRequest error";
130         return ERR_FLATTEN_OBJECT;
131     }
132 
133     return reply.ReadInt32();
134 }
135 
ProcessStreamData(const uint8_t * buffer,size_t size)136 int32_t SysInstallerProxy::ProcessStreamData(const uint8_t *buffer, size_t size)
137 {
138     LOG(INFO) << "ProcessStreamData";
139     auto remote = Remote();
140     if (remote == nullptr) {
141         LOG(ERROR) << "Can not get remote";
142         return ERR_FLATTEN_OBJECT;
143     }
144 
145     MessageParcel data;
146     if (!data.WriteInterfaceToken(GetDescriptor())) {
147         LOG(ERROR) << "WriteInterfaceToken error";
148         return ERR_FLATTEN_OBJECT;
149     }
150     data.WriteInt32(size);
151     data.WriteBuffer(buffer, size);
152 
153     MessageParcel reply;
154     MessageOption option { MessageOption::TF_SYNC};
155     int32_t ret = remote->SendRequest(
156         static_cast<uint32_t>(SysInstallerInterfaceCode::PROCESS_STREAM_DATA), data, reply, option);
157     if (ret != ERR_OK) {
158         LOG(ERROR) << "SendRequest error";
159         return ERR_FLATTEN_OBJECT;
160     }
161 
162     return reply.ReadInt32();
163 }
164 
SetUpdateCallback(const sptr<ISysInstallerCallback> & cb)165 int32_t SysInstallerProxy::SetUpdateCallback(const sptr<ISysInstallerCallback> &cb)
166 {
167     if (cb == nullptr) {
168         LOG(ERROR) <<  "Invalid param";
169         return ERR_INVALID_VALUE;
170     }
171     LOG(INFO) << "RegisterUpdateCallback";
172 
173     auto remote = Remote();
174     if (remote == nullptr) {
175         LOG(ERROR) << "Can not get remote";
176         return ERR_FLATTEN_OBJECT;
177     }
178 
179     MessageParcel data;
180     if (!data.WriteInterfaceToken(GetDescriptor())) {
181         LOG(ERROR) << "WriteInterfaceToken fail";
182         return -1;
183     }
184 
185     bool ret = data.WriteRemoteObject(cb->AsObject());
186     if (!ret) {
187         LOG(ERROR) << "WriteRemoteObject error";
188         return ERR_FLATTEN_OBJECT;
189     }
190     MessageParcel reply;
191     MessageOption option { MessageOption::TF_ASYNC};
192     int32_t res = remote->SendRequest(
193         static_cast<uint32_t>(SysInstallerInterfaceCode::SET_UPDATE_CALLBACK), data, reply, option);
194     if (res != ERR_OK) {
195         LOG(ERROR) << "SendRequest error";
196         return ERR_FLATTEN_OBJECT;
197     }
198     return reply.ReadInt32();
199 }
200 
GetUpdateStatus()201 int32_t SysInstallerProxy::GetUpdateStatus()
202 {
203     LOG(INFO) << "GetUpdateStatus";
204     auto remote = Remote();
205     if (remote == nullptr) {
206         LOG(ERROR) << "Can not get remote";
207         return ERR_FLATTEN_OBJECT;
208     }
209 
210     MessageParcel data;
211     if (!data.WriteInterfaceToken(GetDescriptor())) {
212         return -1;
213     }
214     MessageParcel reply;
215     MessageOption option { MessageOption::TF_ASYNC};
216     int32_t ret = remote->SendRequest(
217         static_cast<uint32_t>(SysInstallerInterfaceCode::GET_UPDATE_STATUS), data, reply, option);
218     if (ret != ERR_OK) {
219         LOG(ERROR) << "SendRequest error";
220         return ERR_FLATTEN_OBJECT;
221     }
222 
223     return reply.ReadInt32();
224 }
225 
StartUpdateParaZip(const std::string & pkgPath,const std::string & location,const std::string & cfgDir)226 int32_t SysInstallerProxy::StartUpdateParaZip(const std::string &pkgPath,
227     const std::string &location, const std::string &cfgDir)
228 {
229     LOG(INFO) << "StartUpdateParaZip";
230     auto remote = Remote();
231     if (remote == nullptr) {
232         LOG(ERROR) << "Can not get remote";
233         return ERR_FLATTEN_OBJECT;
234     }
235 
236     MessageParcel data;
237     if (!data.WriteInterfaceToken(GetDescriptor())) {
238         LOG(ERROR) << "WriteInterfaceToken error";
239         return ERR_FLATTEN_OBJECT;
240     }
241     data.WriteString16(Str8ToStr16(pkgPath));
242     data.WriteString16(Str8ToStr16(location));
243     data.WriteString16(Str8ToStr16(cfgDir));
244 
245     MessageParcel reply;
246     MessageOption option;
247     int32_t ret = remote->SendRequest(
248         static_cast<uint32_t>(SysInstallerInterfaceCode::UPDATE_PARA_PACKAGE), data, reply, option);
249     if (ret != ERR_OK) {
250         LOG(ERROR) << "SendRequest error";
251         return ERR_FLATTEN_OBJECT;
252     }
253 
254     return reply.ReadInt32();
255 }
256 
StartDeleteParaZip(const std::string & location,const std::string & cfgDir)257 int32_t SysInstallerProxy::StartDeleteParaZip(const std::string &location, const std::string &cfgDir)
258 {
259     LOG(INFO) << "StartDeleteParaZip";
260     auto remote = Remote();
261     if (remote == nullptr) {
262         LOG(ERROR) << "Can not get remote";
263         return ERR_FLATTEN_OBJECT;
264     }
265 
266     MessageParcel data;
267     if (!data.WriteInterfaceToken(GetDescriptor())) {
268         LOG(ERROR) << "WriteInterfaceToken error";
269         return ERR_FLATTEN_OBJECT;
270     }
271     data.WriteString16(Str8ToStr16(location));
272     data.WriteString16(Str8ToStr16(cfgDir));
273 
274     MessageParcel reply;
275     MessageOption option;
276     int32_t ret = remote->SendRequest(
277         static_cast<uint32_t>(SysInstallerInterfaceCode::DELETE_PARA_PACKAGE), data, reply, option);
278     if (ret != ERR_OK) {
279         LOG(ERROR) << "SendRequest error";
280         return ERR_FLATTEN_OBJECT;
281     }
282 
283     return reply.ReadInt32();
284 }
285 
AccDecompressAndVerifyPkg(const std::string & srcPath,const std::string & dstPath,const uint32_t type)286 int32_t SysInstallerProxy::AccDecompressAndVerifyPkg(const std::string &srcPath,
287     const std::string &dstPath, const uint32_t type)
288 {
289     LOG(INFO) << "AccDecompressAndVerifyPkg";
290     auto remote = Remote();
291     if (remote == nullptr) {
292         LOG(ERROR) << "Can not get remote";
293         return ERR_FLATTEN_OBJECT;
294     }
295 
296     MessageParcel data;
297     if (!data.WriteInterfaceToken(GetDescriptor())) {
298         LOG(ERROR) << "WriteInterfaceToken error";
299         return ERR_FLATTEN_OBJECT;
300     }
301     data.WriteString16(Str8ToStr16(srcPath));
302     data.WriteString16(Str8ToStr16(dstPath));
303     data.WriteInt32(static_cast<int32_t>(type));
304 
305     MessageParcel reply;
306     MessageOption option;
307     int32_t ret = remote->SendRequest(
308         static_cast<uint32_t>(SysInstallerInterfaceCode::DECOMPRESS_ACC_PACKAGE), data, reply, option);
309     if (ret != ERR_OK) {
310         LOG(ERROR) << "SendRequest error";
311         return ERR_FLATTEN_OBJECT;
312     }
313 
314     return reply.ReadInt32();
315 }
316 
AccDeleteDir(const std::string & dstPath)317 int32_t SysInstallerProxy::AccDeleteDir(const std::string &dstPath)
318 {
319     LOG(INFO) << "AccDeleteDir";
320     auto remote = Remote();
321     if (remote == nullptr) {
322         LOG(ERROR) << "Can not get remote";
323         return ERR_FLATTEN_OBJECT;
324     }
325 
326     MessageParcel data;
327     if (!data.WriteInterfaceToken(GetDescriptor())) {
328         LOG(ERROR) << "WriteInterfaceToken error";
329         return ERR_FLATTEN_OBJECT;
330     }
331     data.WriteString16(Str8ToStr16(dstPath));
332 
333     MessageParcel reply;
334     MessageOption option;
335     int32_t ret = remote->SendRequest(
336         static_cast<uint32_t>(SysInstallerInterfaceCode::DELETE_ACC_PACKAGE), data, reply, option);
337     if (ret != ERR_OK) {
338         LOG(ERROR) << "SendRequest error";
339         return ERR_FLATTEN_OBJECT;
340     }
341 
342     return reply.ReadInt32();
343 }
344 }
345 } // namespace OHOS
346