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
23 namespace OHOS {
24 namespace SysInstaller {
25 using namespace Updater;
26
SysInstallerInit()27 int32_t SysInstallerProxy::SysInstallerInit()
28 {
29 LOG(INFO) << "SysInstallerInit";
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 return ERR_FLATTEN_OBJECT;
39 }
40 MessageParcel reply;
41 MessageOption option;
42 int32_t ret = remote->SendRequest(SYS_INSTALLER_INIT, data, reply, option);
43 if (ret != ERR_OK) {
44 LOG(ERROR) << "SendRequest error";
45 return ERR_FLATTEN_OBJECT;
46 }
47
48 return reply.ReadInt32();
49 }
50
StartUpdatePackageZip(const std::string & pkgPath)51 int32_t SysInstallerProxy::StartUpdatePackageZip(const std::string &pkgPath)
52 {
53 LOG(INFO) << "StartUpdatePackageZip";
54 auto remote = Remote();
55 if (remote == nullptr) {
56 LOG(ERROR) << "Can not get remote";
57 return ERR_FLATTEN_OBJECT;
58 }
59
60 MessageParcel data;
61 if (!data.WriteInterfaceToken(GetDescriptor())) {
62 LOG(ERROR) << "WriteInterfaceToken error";
63 return ERR_FLATTEN_OBJECT;
64 }
65 data.WriteString16(Str8ToStr16(pkgPath));
66
67 MessageParcel reply;
68 MessageOption option;
69 int32_t ret = remote->SendRequest(UPDATE_PACKAGE, data, reply, option);
70 if (ret != ERR_OK) {
71 LOG(ERROR) << "SendRequest error";
72 return ERR_FLATTEN_OBJECT;
73 }
74
75 return reply.ReadInt32();
76 }
77
SetUpdateCallback(const sptr<ISysInstallerCallback> & cb)78 int32_t SysInstallerProxy::SetUpdateCallback(const sptr<ISysInstallerCallback> &cb)
79 {
80 if (cb == nullptr) {
81 LOG(ERROR) << "Invalid param";
82 return ERR_INVALID_VALUE;
83 }
84 LOG(INFO) << "RegisterUpdateCallback";
85
86 auto remote = Remote();
87 if (remote == nullptr) {
88 LOG(ERROR) << "Can not get remote";
89 return ERR_FLATTEN_OBJECT;
90 }
91
92 MessageParcel data;
93 if (!data.WriteInterfaceToken(GetDescriptor())) {
94 LOG(ERROR) << "WriteInterfaceToken fail";
95 return -1;
96 }
97
98 bool ret = data.WriteRemoteObject(cb->AsObject());
99 if (!ret) {
100 LOG(ERROR) << "WriteRemoteObject error";
101 return ERR_FLATTEN_OBJECT;
102 }
103 MessageParcel reply;
104 MessageOption option { MessageOption::TF_SYNC };
105 int32_t res = remote->SendRequest(SET_UPDATE_CALLBACK, data, reply, option);
106 if (res != ERR_OK) {
107 LOG(ERROR) << "SendRequest error";
108 return ERR_FLATTEN_OBJECT;
109 }
110 return reply.ReadInt32();
111 }
112
GetUpdateStatus()113 int32_t SysInstallerProxy::GetUpdateStatus()
114 {
115 LOG(INFO) << "GetUpdateStatus";
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 return -1;
125 }
126 MessageParcel reply;
127 MessageOption option;
128 int32_t ret = remote->SendRequest(GET_UPDATE_STATUS, data, reply, option);
129 if (ret != ERR_OK) {
130 LOG(ERROR) << "SendRequest error";
131 return ERR_FLATTEN_OBJECT;
132 }
133
134 return reply.ReadInt32();
135 }
136
StartUpdateParaZip(const std::string & pkgPath,const std::string & location,const std::string & cfgDir)137 int32_t SysInstallerProxy::StartUpdateParaZip(const std::string &pkgPath,
138 const std::string &location, const std::string &cfgDir)
139 {
140 LOG(INFO) << "StartUpdateParaZip";
141 auto remote = Remote();
142 if (remote == nullptr) {
143 LOG(ERROR) << "Can not get remote";
144 return ERR_FLATTEN_OBJECT;
145 }
146
147 MessageParcel data;
148 if (!data.WriteInterfaceToken(GetDescriptor())) {
149 LOG(ERROR) << "WriteInterfaceToken error";
150 return ERR_FLATTEN_OBJECT;
151 }
152 data.WriteString16(Str8ToStr16(pkgPath));
153 data.WriteString16(Str8ToStr16(location));
154 data.WriteString16(Str8ToStr16(cfgDir));
155
156 MessageParcel reply;
157 MessageOption option;
158 int32_t ret = remote->SendRequest(UPDATE_PARA_PACKAGE, data, reply, option);
159 if (ret != ERR_OK) {
160 LOG(ERROR) << "SendRequest error";
161 return ERR_FLATTEN_OBJECT;
162 }
163
164 return reply.ReadInt32();
165 }
166 }
167 } // namespace OHOS
168