diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..e69de29
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..f433b1a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,177 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
diff --git a/README.en.md b/README.en.md
deleted file mode 100644
index 185a744..0000000
--- a/README.en.md
+++ /dev/null
@@ -1,36 +0,0 @@
-# update_updateservice
-
-#### Description
-Update service module | 升级服务层组件
-
-#### Software Architecture
-Software architecture description
-
-#### Installation
-
-1. xxxx
-2. xxxx
-3. xxxx
-
-#### Instructions
-
-1. xxxx
-2. xxxx
-3. xxxx
-
-#### Contribution
-
-1. Fork the repository
-2. Create Feat_xxx branch
-3. Commit your code
-4. Create Pull Request
-
-
-#### Gitee Feature
-
-1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
-2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
-3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
-4. The most valuable open source project [GVP](https://gitee.com/gvp)
-5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
-6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
diff --git a/README.md b/README.md
old mode 100644
new mode 100755
index 32242f1..07b23bd
--- a/README.md
+++ b/README.md
@@ -1,37 +1,159 @@
-# update_updateservice
+# Update Service
-#### 介绍
-Update service module | 升级服务层组件
+- [Introduction](#section184mcpsimp)
+- [Directory Structure](#section193mcpsimp)
+- [Description](#section208mcpsimp)
+ - [JS APIs](#section210mcpsimp)
+ - [Usage](#section253mcpsimp)
-#### 软件架构
-软件架构说明
+- [Repositories Involved](#section366mcpsimp)
+## Introduction
-#### 安装教程
+The update service is a system ability \(SA\) started by the init process of OHOS to implement an update.
-1. xxxx
-2. xxxx
-3. xxxx
+The update service provides the following functions:
-#### 使用说明
+1. Searching for available update packages
-1. xxxx
-2. xxxx
-3. xxxx
+2. Downloading update packages
-#### 参与贡献
+3. Setting and obtaining the update policy
-1. Fork 本仓库
-2. 新建 Feat_xxx 分支
-3. 提交代码
-4. 新建 Pull Request
+4. Triggering an update
+## Directory Structure
-#### 特技
+```
+base/update/updateservice # Update service code
+├── client # NAPI-based update client
+├── engine # Update client engine
+│ ├── etc # rc configuration files for the update client engine
+│ ├── include # Header files for the update client engine
+│ ├── sa_profile # SA profiles
+│ └── src # Source code of the update client engine
+├── interfaces # Update client APIs
+│ └── innerkits # SA APIs
+├── kits # External APIs
+│ └── js # JS APIs for the update app
+└── tests # Test code
+ └── unittest # Unit test code for the update client
+```
+
+## Description
+
+### JS APIs
+
+
+
API
+ |
+Description
+ |
+
+checkNewVersion
+ |
+Checks whether a new update package is available.
+ |
+
+download()
+ |
+Downloads the update package.
+ |
+
+upgrade()
+ |
+Writes the update command to the misc partition and runs the reboot command to access the updater.
+ |
+
+getNewVersionInfo()
+ |
+Obtains the version information after a version update.
+ |
+
+setUpdatePolicy
+ |
+Sets the update policy.
+ |
+
+getUpdatePolicy
+ |
+Obtains the update policy.
+ |
+
+
+
+
+### Usage
+
+1. Import **libupdateclient**.
+
+```
+import client from 'libupdateclient.z.so'
+```
+
+2. Obtain the **Updater** object.
+
+```
+let updater = client.getUpdater('OTA');
+```
+
+3. Obtain the new version information.
+
+```
+updater.getNewVersionInfo(info => {
+ info "New version information"
+});
+```
+
+4. Checks for a new version.
+
+```
+updater.checkNewVersion(info => {
+ info "New version information"
+});
+```
+
+5. Download the new version and monitor the download process.
+
+```
+updater.download();
+updater.on("downloadProgress", progress => {
+ progress "Download progress information"
+});
+```
+
+6. Start the update.
+
+```
+updater.upgrade();
+updater.on("upgradeProgress", progress => {
+ progress "Update progress information"
+});
+```
+
+7. Set the update policy.
+
+```
+updater.setUpdatePolicy(result => {
+ result "Update policy setting result"
+});
+```
+
+8. Check the update policy.
+
+```
+updater.getUpdatePolicy(policy => {
+ policy "Update policy"
+});
+```
+
+## Repositories Involved
+
+Update subsystem
+
+update\_app
+
+**update\_updateservice**
+
+update\_updater
-1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
-2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
-3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
-4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
-5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
-6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
diff --git a/README_zh.md b/README_zh.md
new file mode 100755
index 0000000..06374d0
--- /dev/null
+++ b/README_zh.md
@@ -0,0 +1,159 @@
+# 升级服务组件
+
+- [简介](#section184mcpsimp)
+- [目录](#section193mcpsimp)
+- [说明](#section208mcpsimp)
+ - [JS接口说明](#section210mcpsimp)
+ - [使用说明](#section253mcpsimp)
+
+- [相关仓](#section366mcpsimp)
+
+## 简介
+
+升级服务组件是一个SA\(System Ability\), 由OHOS 的init 进程负责启动。
+
+升级服务器引擎主要功能包括:
+
+1、查找可用的升级包
+
+2、下载升级包
+
+3、设置/获取升级策略
+
+4、触发升级
+
+## 目录
+
+```
+base/update/updateservice # 升级服务代码仓目录
+├── client # 升级客户端napi 接口目录
+├── engine # 升级客户端引擎服务目录
+│ ├── etc # 升级客户端引擎rc配置文件目录
+│ ├── include # 升级客户端引擎头文件目录
+│ ├── sa_profile # SA 配置文件目录
+│ └── src # 升级客户端引擎源码目录
+├── interfaces # 升级客户端接口目录
+│ └── innerkits # SA 接口定义和封装目录
+├── kits # 对外接口封装目录
+│ └── js # 提供给升级客户端应用的JS 接口目录
+└── tests # 测试代码目录
+ └── unittest # 升级客户端UT代码目录
+```
+
+## 说明
+
+### JS接口说明
+
+
+接口
+ |
+说明
+ |
+
+checkNewVersion
+ |
+检查是否有可用的升级包版本
+ |
+
+download()
+ |
+下载升级包
+ |
+
+upgrade()
+ |
+将升级命令写入到misc分区,最终调用reboot命令,进入到updater 子系统中。
+ |
+
+getNewVersionInfo()
+ |
+升级完成后,获取升级后的版本信息
+ |
+
+setUpdatePolicy
+ |
+设置升级策略
+ |
+
+getUpdatePolicy
+ |
+获取升级策略
+ |
+
+
+
+
+### 使用说明
+
+1,导入updateclient lib
+
+```
+import client from 'libupdateclient.z.so'
+```
+
+2,获取update对象
+
+```
+let updater = client.getUpdater('OTA');
+```
+
+3,获取新版本信息
+
+```
+updater.getNewVersionInfo(info => {
+ info "新版本信息"
+});
+```
+
+4,检查新版本
+
+```
+updater.checkNewVersion(info => {
+ info "新版本信息"
+});
+```
+
+5,下载新版本,并监听下载进程
+
+```
+updater.download();
+updater.on("downloadProgress", progress => {
+ progress "下载进度信息"
+});
+```
+
+6,启动升级
+
+```
+updater.upgrade();
+updater.on("upgradeProgress", progress => {
+ progress "升级进度信息"
+});
+```
+
+7,设置升级策略
+
+```
+updater.setUpdatePolicy(result => {
+ result "设置升级策略结果"
+});
+```
+
+8,查看升级策略
+
+```
+updater.getUpdatePolicy(policy => {
+ policy "升级策略"
+});
+```
+
+## 相关仓
+
+升级子系统
+
+update\_app
+
+**update\_updateservice**
+
+update\_updater
+
diff --git a/callback/include/update_callback.h b/callback/include/update_callback.h
new file mode 100755
index 0000000..64e16ec
--- /dev/null
+++ b/callback/include/update_callback.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UPDATER_CALLBACK_H
+#define UPDATER_CALLBACK_H
+
+#include "if_system_ability_manager.h"
+#include "ipc_skeleton.h"
+#include "iremote_stub.h"
+#include "iupdate_callback.h"
+#include "update_callback_stub.h"
+#include "update_helper.h"
+#include "system_ability.h"
+
+namespace OHOS {
+namespace update_engine {
+class UpdateCallback : public UpdateCallbackStub {
+public:
+ explicit UpdateCallback() = default;
+
+ ~UpdateCallback() = default;
+
+ void OnCheckVersionDone(const VersionInfo &info) override;
+
+ void OnDownloadProgress(const Progress &progress) override;
+
+ void OnUpgradeProgress(const Progress &progress) override;
+};
+}
+} // namespace OHOS
+#endif // UPDATER_CALLBACK_H
\ No newline at end of file
diff --git a/callback/include/update_callback_proxy.h b/callback/include/update_callback_proxy.h
new file mode 100755
index 0000000..2dda345
--- /dev/null
+++ b/callback/include/update_callback_proxy.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UPDATER_CALLBACK_PROXY_H
+#define UPDATER_CALLBACK_PROXY_H
+
+#include "iremote_proxy.h"
+#include "iupdate_service.h"
+
+namespace OHOS {
+namespace update_engine {
+class UpdateCallbackProxy : public IRemoteProxy {
+public:
+ explicit UpdateCallbackProxy(const sptr& impl) : IRemoteProxy(impl) {}
+
+ virtual ~UpdateCallbackProxy() = default;
+
+ void OnCheckVersionDone(const VersionInfo &info) override;
+
+ void OnDownloadProgress(const Progress &progress) override;
+
+ void OnUpgradeProgress(const Progress &progress) override;
+
+private:
+ static inline BrokerDelegator delegator_;
+};
+}
+} // namespace OHOS
+#endif // UPDATER_CALLBACK_PROXY_H
\ No newline at end of file
diff --git a/callback/include/update_callback_stub.h b/callback/include/update_callback_stub.h
new file mode 100755
index 0000000..33d4ece
--- /dev/null
+++ b/callback/include/update_callback_stub.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UPDATE_CALLBACK_STUB_H_
+#define UPDATE_CALLBACK_STUB_H_
+
+#include "iremote_stub.h"
+#include "iupdate_callback.h"
+#include "message_parcel.h"
+#include "parcel.h"
+
+namespace OHOS {
+namespace update_engine {
+class UpdateCallbackStub : public IRemoteStub {
+public:
+ int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
+ MessageOption &option) override;
+};
+}
+}
+#endif // !defined(UPDATE_CALLBACK_STUB_H_)
diff --git a/callback/src/update_callback.cpp b/callback/src/update_callback.cpp
new file mode 100755
index 0000000..478b44b
--- /dev/null
+++ b/callback/src/update_callback.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "update_callback.h"
+#include
+#include
+#include
+#include
+#include
+#include
+#include "iservice_registry.h"
+#include "parameters.h"
+#include "securec.h"
+#include "system_ability_definition.h"
+#include "update_helper.h"
+
+namespace OHOS {
+namespace update_engine {
+void UpdateCallback::OnCheckVersionDone(const VersionInfo &info)
+{
+ ENGINE_LOGI("OnCheckVersionDone VersionInfo status %d", info.status);
+ ENGINE_LOGI("OnCheckVersionDone VersionInfo errMsg %s", info.errMsg.c_str());
+ ENGINE_LOGI("OnCheckVersionDone VersionInfo versionName : %s", info.result[0].versionName.c_str());
+ ENGINE_LOGI("OnCheckVersionDone VersionInfo versionCode : %s", info.result[0].versionCode.c_str());
+ ENGINE_LOGI("OnCheckVersionDone VersionInfo verifyInfo : %s", info.result[0].verifyInfo.c_str());
+ ENGINE_LOGI("OnCheckVersionDone VersionInfo size : %zu", info.result[0].size);
+}
+
+void UpdateCallback::OnDownloadProgress(const Progress &progress)
+{
+ ENGINE_LOGI("OnDownloadProgress progress %u %d", progress.percent, progress.status);
+}
+
+void UpdateCallback::OnUpgradeProgress(const Progress &progress)
+{
+ ENGINE_LOGI("OnUpgradeProgress progress %u %d", progress.percent, progress.status);
+}
+}
+} // namespace OHOS
diff --git a/callback/src/update_callback_proxy.cpp b/callback/src/update_callback_proxy.cpp
new file mode 100755
index 0000000..9fd6f01
--- /dev/null
+++ b/callback/src/update_callback_proxy.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "update_callback_proxy.h"
+#include "securec.h"
+#include "update_helper.h"
+
+namespace OHOS {
+namespace update_engine {
+void UpdateCallbackProxy::OnCheckVersionDone(const VersionInfo &info)
+{
+ ENGINE_LOGI("UpdateCallbackProxy::OnCheckVersionDone");
+ MessageParcel data;
+ MessageParcel reply;
+ MessageOption option { MessageOption::TF_SYNC };
+
+ auto remote = Remote();
+ ENGINE_CHECK(remote != nullptr, return, "Can not get remote");
+
+ int32_t result = UpdateHelper::WriteVersionInfo(data, info);
+ ENGINE_CHECK(result == 0, return, "Can not WriteVersionInfo");
+
+ result = remote->SendRequest(CHECK_VERSION, data, reply, option);
+ ENGINE_CHECK(result == ERR_OK, return, "Can not SendRequest");
+ return;
+}
+
+void UpdateCallbackProxy::OnDownloadProgress(const Progress &progress)
+{
+ ENGINE_LOGI("UpdateCallbackProxy::OnDownloadProgress");
+ MessageParcel data;
+ MessageParcel reply;
+ MessageOption option { MessageOption::TF_SYNC };
+
+ auto remote = Remote();
+ ENGINE_CHECK(remote != nullptr, return, "Can not get remote");
+
+ int32_t result = UpdateHelper::WriteUpdateProgress(data, progress);
+ ENGINE_CHECK(result == 0, return, "Can not WriteUpdateProgress");
+
+ result = remote->SendRequest(DOWNLOAD, data, reply, option);
+ ENGINE_CHECK(result == ERR_OK, return, "Can not SendRequest %d", result);
+ return;
+}
+
+void UpdateCallbackProxy::OnUpgradeProgress(const Progress &progress)
+{
+ ENGINE_LOGI("UpdateCallbackProxy::OnUpgradeProgress");
+ MessageParcel data;
+ MessageParcel reply;
+ MessageOption option { MessageOption::TF_SYNC };
+
+ auto remote = Remote();
+ ENGINE_CHECK(remote != nullptr, return, "Can not get remote");
+
+ int32_t result = UpdateHelper::WriteUpdateProgress(data, progress);
+ ENGINE_CHECK(result == 0, return, "Can not WriteUpdateProgress");
+
+ result = remote->SendRequest(UPGRADE, data, reply, option);
+ ENGINE_CHECK(result == ERR_OK, return, "Can not SendRequest");
+ return;
+}
+}
+} // namespace OHOS
diff --git a/callback/src/update_callback_stub.cpp b/callback/src/update_callback_stub.cpp
new file mode 100644
index 0000000..844e7df
--- /dev/null
+++ b/callback/src/update_callback_stub.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "update_callback_stub.h"
+#include "securec.h"
+#include "update_helper.h"
+
+using namespace std;
+
+namespace OHOS {
+namespace update_engine {
+int32_t UpdateCallbackStub::OnRemoteRequest(uint32_t code,
+ MessageParcel &data, MessageParcel &reply, MessageOption &option)
+{
+ switch (code) {
+ case CHECK_VERSION: {
+ VersionInfo info;
+ UpdateHelper::ReadVersionInfo(data, info);
+ OnCheckVersionDone(info);
+ break;
+ }
+ case DOWNLOAD: {
+ Progress info;
+ UpdateHelper::ReadUpdateProgress(data, info);
+ OnDownloadProgress(info);
+ break;
+ }
+ case UPGRADE: {
+ Progress info;
+ UpdateHelper::ReadUpdateProgress(data, info);
+ OnUpgradeProgress(info);
+ break;
+ }
+ default: {
+ return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
+ }
+ }
+ return 0;
+}
+}
+}
diff --git a/client/BUILD.gn b/client/BUILD.gn
new file mode 100755
index 0000000..5c44c0a
--- /dev/null
+++ b/client/BUILD.gn
@@ -0,0 +1,65 @@
+# Copyright (c) 2021 Huawei Device Co., Ltd.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("//build/ohos.gni")
+
+ohos_shared_library("update") {
+ sources = [
+ "//base/update/updateservice/client/update_client.cpp",
+ "//base/update/updateservice/client/update_module.cpp",
+ "//base/update/updateservice/client/update_session.cpp",
+ ]
+
+ include_dirs = [
+ "//base/update/updater/interfaces/kits/include",
+ "//base/update/updater/services/include",
+ "//base/update/updater/utils/include",
+ "//third_party/node/src",
+ "//foundation/ace/napi/interfaces/kits",
+ "//utils/native/base/include",
+ "//utils/system/safwk/native/include",
+ "//base/update/updateservice/interfaces/innerkits/include",
+ "//base/update/updateservice/client",
+ "//base/startup/syspara_lite/adapter/native/syspara/include",
+ "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include",
+ "//foundation/distributedschedule/safwk/services/safwk/include",
+ "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk",
+ "//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include",
+ "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include",
+ ]
+
+ deps = [
+ "//base/update/updater/interfaces/kits/misc_info:libmiscinfo",
+ "//base/update/updater/interfaces/kits/packages:libpackageExt",
+ "//base/update/updater/services/log:libupdaterlog",
+ "//base/update/updater/utils:libutils",
+ "//base/update/updateservice/interfaces/innerkits/engine:updateservicekits",
+ "//foundation/ace/napi:ace_napi",
+ "//third_party/bounds_checking_function:libsec_static",
+ "//third_party/openssl:crypto_source",
+ "//third_party/openssl:ssl_source",
+ "//utils/native/base:utils",
+ ]
+
+ external_deps = [
+ "hiviewdfx_hilog_native:libhilog",
+ "ipc:ipc_core",
+ "safwk:system_ability_fwk",
+ "samgr_L2:samgr_proxy",
+ "startup_l2:syspara",
+ ]
+
+ install_enable = true
+ relative_install_dir = "module"
+ part_name = "updater"
+}
diff --git a/client/update_client.cpp b/client/update_client.cpp
new file mode 100644
index 0000000..a8390c8
--- /dev/null
+++ b/client/update_client.cpp
@@ -0,0 +1,910 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "update_client.h"
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "iupdate_service.h"
+#include "misc_info/misc_info.h"
+#include "node_api.h"
+#include "node_api_types.h"
+#include "package/package.h"
+#include "securec.h"
+#include "update_helper.h"
+#include "update_service_kits.h"
+#include "update_session.h"
+
+using namespace std;
+using namespace OHOS::update_engine;
+
+namespace updateClient {
+const int32_t MAX_ARGC = 3;
+const int32_t DEVICE_ID_INDEX = 1;
+const int32_t MID_ARGC = 2;
+const int32_t CLIENT_STRING_MAX_LENGTH = 200;
+constexpr int PROGRESS_DOWNLOAD_FINISH = 100;
+const std::string MISC_FILE = "/dev/block/platform/soc/10100000.himci.eMMC/by-name/misc";
+const std::string UPDATER_PKG_NAME = "/data/updater/updater.zip";
+
+UpdateClient::UpdateClient(napi_env env, napi_value thisVar)
+{
+ thisReference_ = nullptr;
+ env_ = env;
+ napi_create_reference(env, thisVar, 1, &thisReference_);
+ context_.type = "OTA";
+ context_.upgradeDevId = "local";
+ context_.controlDevId = "local";
+ context_.upgradeApp = "updateclient";
+ context_.upgradeFile = UPDATER_PKG_NAME;
+}
+
+UpdateClient::~UpdateClient()
+{
+ sessions_.clear();
+ if (thisReference_ != nullptr) {
+ napi_delete_reference(env_, thisReference_);
+ }
+}
+
+UpdateSession *UpdateClient::RemoveSession(uint32_t sessionId)
+{
+ CLIENT_LOGI("RemoveSession sess");
+ std::lock_guard guard(sessionMutex_);
+ UpdateSession *sess = nullptr;
+ auto iter = sessions_.find(sessionId);
+ if (iter != sessions_.end()) {
+ sess = iter->second.get();
+ sessions_.erase(iter);
+ }
+ return sess;
+}
+
+void UpdateClient::AddSession(std::shared_ptr session)
+{
+ CLIENT_CHECK(session != nullptr, return, "Invalid param");
+ std::lock_guard guard(sessionMutex_);
+ sessions_.insert(make_pair(session->GetSessionId(), session));
+}
+
+napi_value UpdateClient::GetUpdaterForOther(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ CLIENT_CHECK_NAPI_CALL(env, argc >= MID_ARGC, return nullptr, "Invalid param");
+
+ // Get the devid.
+ int ret = GetStringValue(env, args[1], context_.upgradeDevId);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Error get type");
+ return GetUpdater(env, info, DEVICE_ID_INDEX + 1);
+}
+
+napi_value UpdateClient::GetUpdaterFromOther(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ CLIENT_CHECK_NAPI_CALL(env, argc >= MID_ARGC, return nullptr, "Invalid param");
+
+ // Get the devid.
+ int ret = GetStringValue(env, args[1], context_.controlDevId);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Error get type");
+ return GetUpdater(env, info, DEVICE_ID_INDEX + 1);
+}
+
+bool UpdateClient::CheckUpgradeType(const std::string &type)
+{
+ std::vector upgradeTypes = {
+ "ota", "patch"
+ };
+ std::string upgradeType = type;
+ upgradeType.erase(0, upgradeType.find_first_not_of(" "));
+ upgradeType.erase(upgradeType.find_last_not_of(" ") + 1);
+ std::transform(upgradeType.begin(), upgradeType.end(), upgradeType.begin(), ::tolower);
+ for (auto inter = upgradeTypes.cbegin(); inter != upgradeTypes.cend(); inter++) {
+ if ((*inter).compare(upgradeType) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool UpdateClient::CheckUpgradeFile(const std::string &upgradeFile)
+{
+ if (upgradeFile.empty()) {
+ return false;
+ }
+ std::string file = upgradeFile;
+ file.erase(0, file.find_first_not_of(" "));
+ file.erase(file.find_last_not_of(" ") + 1);
+ int32_t pos = file.find_first_of('/');
+ if (pos != 0) {
+ return false;
+ }
+ pos = file.find_last_of('.');
+ if (pos < 0) {
+ return false;
+ }
+ std::string postfix = file.substr(pos + 1, -1);
+ std::transform(postfix.begin(), postfix.end(), postfix.begin(), ::tolower);
+ if (postfix.compare("bin") == 0) {
+ return true;
+ } else if (postfix.compare("zip") == 0) {
+ return true;
+ } else if (postfix.compare("lz4") == 0) {
+ return true;
+ } else if (postfix.compare("gz") == 0) {
+ return true;
+ }
+ return false;
+}
+
+napi_value UpdateClient::GetUpdater(napi_env env, napi_callback_info info, int32_t typeIndex)
+{
+ napi_value result;
+ napi_create_int32(env, 0, &result);
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ CLIENT_CHECK_NAPI_CALL(env, argc >= 1, return nullptr, "Invalid param");
+ CLIENT_CHECK_NAPI_CALL(env, !isInit, return nullptr, "Has beed init");
+
+ int ret = GetStringValue(env, args[0], context_.upgradeFile);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok && CheckUpgradeFile(context_.upgradeFile),
+ return nullptr, "Invalid upgradeFile");
+ ret = GetStringValue(env, args[typeIndex], context_.type);
+ if (ret == napi_ok) {
+ CLIENT_CHECK_NAPI_CALL(env, CheckUpgradeType(context_.type), return nullptr, "Error get upgradeType");
+ } else {
+ context_.type = "OTA";
+ }
+ CLIENT_LOGE("GetUpdater argc %s", context_.type.c_str());
+ UpdateCallbackInfo callback {
+ [&](const VersionInfo &info) {
+ this->NotifyCheckVersionDone(info);
+ },
+ [&](const Progress &info) {
+ this->NotifyDownloadProgress(info);
+ },
+ [&](const Progress &info) {
+ this->NotifyUpgradeProgresss(info);
+ },
+ };
+ UpdateServiceKits::GetInstance().RegisterUpdateCallback(context_, callback);
+ isInit = true;
+ return nullptr;
+}
+
+napi_value UpdateClient::StartSession(napi_env env,
+ napi_callback_info info, int32_t type, size_t callbackStartIndex, DoWorkFunction function)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+
+ CLIENT_LOGE("StartSession type %d argc %zu callbackStartIndex %d", type, argc, callbackStartIndex);
+ std::shared_ptr sess = nullptr;
+ if (argc > callbackStartIndex) {
+ sess = std::make_shared(this, type, argc, 1);
+ } else {
+ sess = std::make_shared(this, type, argc, 0);
+ }
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
+ AddSession(sess);
+ napi_value retValue = sess->StartWork(env, callbackStartIndex, args, function, nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId()); return nullptr, "Failed to start worker.");
+
+ return retValue;
+}
+
+napi_value UpdateClient::CheckNewVersion(napi_env env, napi_callback_info info)
+{
+ versionInfo_.status = SYSTEM_ERROR;
+ napi_value ret = StartSession(env, info, SESSION_CHECK_VERSION, 0,
+ [&](int32_t type, void *context) -> int {
+ return UpdateServiceKits::GetInstance().CheckNewVersion();
+ });
+ CLIENT_CHECK(ret != nullptr, return nullptr, "Failed to start worker.");
+ return ret;
+}
+
+napi_value UpdateClient::CancelUpgrade(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ CLIENT_LOGE("CancelUpgrade");
+ std::shared_ptr sess = nullptr;
+ sess = std::make_shared(this, SESSION_CANCEL_UPGRADE, argc, 0);
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
+ AddSession(sess);
+ napi_value retValue = sess->StartWork(env, 0, args,
+ [&](int32_t type, void *context) -> int {
+ return UpdateServiceKits::GetInstance().Cancel(IUpdateService::DOWNLOAD);
+ }, nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId()); return nullptr, "Failed to start worker.");
+ return retValue;
+}
+
+napi_value UpdateClient::DownloadVersion(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ CLIENT_LOGE("DownloadVersion");
+ std::shared_ptr sess = nullptr;
+ sess = std::make_shared(this, SESSION_DOWNLOAD, argc, 0);
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
+ AddSession(sess);
+ napi_value retValue = sess->StartWork(env, 0, args,
+ [&](int32_t type, void *context) -> int {
+ return UpdateServiceKits::GetInstance().DownloadVersion();
+ }, nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId()); return nullptr, "Failed to start worker.");
+ return retValue;
+}
+
+napi_value UpdateClient::UpgradeVersion(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ std::shared_ptr sess = nullptr;
+ sess = std::make_shared(this, SESSION_UPGRADE, argc, 0);
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
+ AddSession(sess);
+ napi_value retValue = sess->StartWork(env, 0, args,
+ [&](int32_t type, void *context) -> int {
+#ifndef UPDATER_API_TEST
+ return UpdateServiceKits::GetInstance().DoUpdate();
+#else
+ return 0;
+#endif
+ }, nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId()); return nullptr, "Failed to start worker.");
+ return retValue;
+}
+
+napi_value UpdateClient::SetUpdatePolicy(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+
+ int ret = GetUpdatePolicyFromArg(env, args[0], updatePolicy_);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Failed to get policy para");
+
+ std::shared_ptr sess = nullptr;
+ if (argc >= MID_ARGC) {
+ sess = std::make_shared(this, SESSION_SET_POLICY, argc, 1);
+ } else {
+ sess = std::make_shared(this, SESSION_SET_POLICY, argc, 0);
+ }
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
+ AddSession(sess);
+ napi_value retValue = sess->StartWork(env, 1, args,
+ [&](int32_t type, void *context) -> int {
+ result_ = UpdateServiceKits::GetInstance().SetUpdatePolicy(updatePolicy_);
+ return result_;
+ }, nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId());
+ return nullptr, "Failed to SetUpdatePolicy.");
+ return retValue;
+}
+
+napi_value UpdateClient::GetUpdatePolicy(napi_env env, napi_callback_info info)
+{
+ napi_value retValue = StartSession(env, info, SESSION_GET_POLICY, 0,
+ [&](int32_t type, void *context) -> int {
+ return UpdateServiceKits::GetInstance().GetUpdatePolicy(updatePolicy_);
+ });
+ CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to UpgradeVersion.");
+ return retValue;
+}
+
+napi_value UpdateClient::GetNewVersionInfo(napi_env env, napi_callback_info info)
+{
+ napi_value retValue = StartSession(env, info, SESSION_GET_NEW_VERSION, 0,
+ [&](int32_t type, void *context) -> int {
+ return UpdateServiceKits::GetInstance().GetNewVersion(versionInfo_);
+ });
+ CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo.");
+ return retValue;
+}
+
+napi_value UpdateClient::GetUpgradeStatus(napi_env env, napi_callback_info info)
+{
+ napi_value retValue = StartSession(env, info, SESSION_GET_STATUS, 0,
+ [&](int32_t type, void *context) -> int {
+ return UpdateServiceKits::GetInstance().GetUpgradeStatus(upgradeInfo_);
+ });
+ CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetUpgradeStatus.");
+ return retValue;
+}
+
+napi_value UpdateClient::ApplyNewVersion(napi_env env, napi_callback_info info)
+{
+ napi_value retValue = StartSession(env, info, SESSION_APPLY_NEW_VERSION, 0,
+ [&](int32_t type, void *context) -> int {
+ result_ = UpdateServiceKits::GetInstance().RebootAndInstall(MISC_FILE, UPDATER_PKG_NAME);
+ return result_;
+ });
+ CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo.");
+ return retValue;
+}
+
+napi_value UpdateClient::RebootAndClean(napi_env env, napi_callback_info info)
+{
+ napi_value retValue = StartSession(env, info, SESSION_REBOOT_AND_CLEAN, 0,
+ [&](int32_t type, void *context) -> int {
+ result_ = UpdateServiceKits::GetInstance().RebootAndClean(MISC_FILE, UPDATER_PKG_NAME);
+ return result_;
+ });
+ CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo.");
+ return retValue;
+}
+
+napi_value UpdateClient::VerifyUpdatePackage(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ CLIENT_CHECK_NAPI_CALL(env, argc >= MID_ARGC, return nullptr, "Error get cb info");
+
+ int ret = GetStringValue(env, args[0], upgradeFile_);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Error get upgradeType");
+ ret = GetStringValue(env, args[1], certsFile_);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Error get certsFile");
+
+ CLIENT_LOGE("VerifyUpdatePackage");
+ std::shared_ptr sess = nullptr;
+ sess = std::make_shared(this, SESSION_VERIFY_PACKAGE, argc, 0);
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Fail to create update session");
+ AddSession(sess);
+ size_t startIndex = 2;
+ napi_value retValue = sess->StartWork(env, startIndex, args,
+ [&](int32_t type, void *context) -> int {
+ CLIENT_LOGE("StartWork VerifyUpdatePackage");
+ result_ = VerifyPackageWithCallback(upgradeFile_, certsFile_,
+ [&](int32_t result, uint32_t percent) { NotifyVerifyProgresss(result, percent); });
+ return result_;
+ },
+ nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId()); return nullptr, "Failed to start worker.");
+ return retValue;
+}
+
+napi_value UpdateClient::SubscribeEvent(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+ // Two arguments are required: type + callback.
+ CLIENT_CHECK_NAPI_CALL(env, argc >= MID_ARGC, return nullptr, "Invalid param");
+
+ std::string eventType;
+ int ret = UpdateClient::GetStringValue(env, args[0], eventType);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Failed to get event type");
+ CLIENT_CHECK(FindSessionByHandle(env, eventType, args[1]) == nullptr, return nullptr, "Handle has been sub");
+
+ std::shared_ptr sess = std::make_shared(this, SESSION_SUBSCRIBE, argc, 1, false);
+ CLIENT_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create listener");
+ AddSession(sess);
+ napi_value retValue = sess->StartWork(env, 1, args,
+ [&](int32_t type, void *context) -> int {
+ return 0;
+ }, nullptr);
+ CLIENT_CHECK(retValue != nullptr, RemoveSession(sess->GetSessionId()); return nullptr, "Failed to SubscribeEvent.");
+ return retValue;
+}
+
+napi_value UpdateClient::UnsubscribeEvent(napi_env env, napi_callback_info info)
+{
+ size_t argc = MAX_ARGC;
+ napi_value args[MAX_ARGC] = {0};
+ napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
+
+ std::string eventType;
+ int ret = UpdateClient::GetStringValue(env, args[0], eventType);
+ CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Failed to get event type");
+
+ CLIENT_LOGI("UnsubscribeEvent %s argc %d", eventType.c_str(), argc);
+ if (argc >= MID_ARGC) {
+ napi_valuetype valuetype;
+ napi_status status = napi_typeof(env, args[1], &valuetype);
+ CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to napi_typeof");
+ CLIENT_CHECK_NAPI_CALL(env, valuetype == napi_function, return nullptr, "Invalid callback type");
+ }
+ ret = ProcessUnsubscribe(eventType, argc, args[1]);
+ napi_value result;
+ napi_create_int32(env, ret, &result);
+ return result;
+}
+
+int32_t UpdateClient::ProcessUnsubscribe(const std::string &eventType, size_t argc, napi_value arg)
+{
+ napi_handle_scope scope;
+ napi_status status = napi_open_handle_scope(env_, &scope);
+ CLIENT_CHECK(status == napi_ok, return -1, "Error open handle");
+
+ uint32_t nextSessId = 0;
+ bool hasNext = GetFirstSessionId(nextSessId);
+ while (hasNext) {
+ uint32_t currSessId = nextSessId;
+ auto iter = sessions_.find(currSessId);
+ if (iter == sessions_.end()) {
+ break;
+ }
+ hasNext = GetNextSessionId(nextSessId);
+
+ UpdateListener *listener = static_cast(iter->second.get());
+ if (listener->GetType() != SESSION_SUBSCRIBE
+ || eventType.compare(listener->GetEventType()) != 0) {
+ continue;
+ }
+ CLIENT_LOGI("ProcessUnsubscribe remove session");
+ if (argc == 1) {
+ listener->RemoveHandlerRef(env_);
+ RemoveSession(currSessId);
+ } else if (listener->CheckEqual(env_, arg, eventType)) {
+ listener->RemoveHandlerRef(env_);
+ RemoveSession(currSessId);
+ break;
+ }
+ }
+ napi_close_handle_scope(env_, scope);
+ return 0;
+}
+
+UpdateSession *UpdateClient::FindSessionByHandle(napi_env env, const std::string &eventType, napi_value arg)
+{
+ uint32_t nextSessId = 0;
+ bool hasNext = GetFirstSessionId(nextSessId);
+ while (hasNext) {
+ uint32_t currSessId = nextSessId;
+ auto iter = sessions_.find(currSessId);
+ if (iter == sessions_.end()) {
+ break;
+ }
+ hasNext = GetNextSessionId(nextSessId);
+
+ UpdateListener *listener = static_cast(iter->second.get());
+ if (listener->GetType() != SESSION_SUBSCRIBE) {
+ continue;
+ }
+ if ((eventType.compare(listener->GetEventType()) == 0) && listener->CheckEqual(env_, arg, eventType)) {
+ return listener;
+ }
+ }
+ return nullptr;
+}
+
+bool UpdateClient::GetNextSessionId(uint32_t &sessionId)
+{
+ std::lock_guard guard(sessionMutex_);
+ {
+ auto iter = sessions_.find(sessionId);
+ if (iter == sessions_.end()) {
+ return false;
+ }
+ iter++;
+ if (iter == sessions_.end()) {
+ return false;
+ }
+ sessionId = iter->second->GetSessionId();
+ }
+ return true;
+}
+
+bool UpdateClient::GetFirstSessionId(uint32_t &sessionId)
+{
+ std::lock_guard guard(sessionMutex_);
+ {
+ if (sessions_.empty()) {
+ return false;
+ }
+ sessionId = sessions_.begin()->second->GetSessionId();
+ return true;
+ }
+}
+
+void UpdateClient::Emit(const std::string &type, int32_t retcode, const UpdateResult &result)
+{
+ napi_handle_scope scope;
+ napi_status status = napi_open_handle_scope(env_, &scope);
+ CLIENT_CHECK_NAPI_CALL(env_, status == napi_ok, return, "Error open_handle_scope");
+ napi_value thisVar = nullptr;
+ status = napi_get_reference_value(env_, thisReference_, &thisVar);
+ CLIENT_CHECK_NAPI_CALL(env_, status == napi_ok, return, "Error get_reference");
+
+ uint32_t nextSessId = 0;
+ bool hasNext = GetFirstSessionId(nextSessId);
+ while (hasNext) {
+ uint32_t currSessId = nextSessId;
+ auto iter = sessions_.find(currSessId);
+ if (iter == sessions_.end()) {
+ break;
+ }
+ hasNext = GetNextSessionId(nextSessId);
+ UpdateListener *listener = static_cast((iter->second).get());
+ if ((listener->GetType() != SESSION_SUBSCRIBE) || (type.compare(listener->GetEventType()) != 0)) {
+ continue;
+ }
+ listener->NotifyJS(env_, thisVar, retcode, result);
+ iter = sessions_.find(currSessId);
+ if (iter == sessions_.end()) {
+ continue;
+ }
+ listener = static_cast((iter->second).get());
+ if (listener->IsOnce()) {
+ listener->RemoveHandlerRef(env_);
+ RemoveSession(currSessId);
+ }
+ }
+ napi_close_handle_scope(env_, scope);
+}
+
+void UpdateClient::NotifyDownloadProgress(const Progress &progress)
+{
+ CLIENT_LOGI("NotifyDownloadProgress status %d %d", progress.status, progress.percent);
+ if (progress.percent == PROGRESS_DOWNLOAD_FINISH && progress.status == UPDATE_STATE_DOWNLOAD_ON) {
+ return;
+ }
+ progress_.percent = progress.percent;
+ progress_.status = progress.status;
+ progress_.endReason = progress.endReason;
+ UpdateResult result;
+ result.type = SESSION_DOWNLOAD;
+ result.result.progress = &progress_;
+ result.buildJSObject = BuildProgress;
+ int32_t fail = (progress_.status == UPDATE_STATE_DOWNLOAD_FAIL || progress_.status == UPDATE_STATE_VERIFY_FAIL) ?
+ progress_.status : 0;
+ Emit("downloadProgress", fail, result);
+}
+
+void UpdateClient::NotifyUpgradeProgresss(const Progress &progress)
+{
+ CLIENT_LOGI("NotifyUpgradeProgresss status %d %d", progress.status, progress.percent);
+ progress_.percent = progress.percent;
+ progress_.status = progress.status;
+
+ UpdateResult result;
+ result.type = SESSION_UPGRADE;
+ result.result.progress = &progress_;
+ result.buildJSObject = BuildProgress;
+ int32_t fail = (progress_.status == UPDATE_STATE_DOWNLOAD_FAIL) ? progress_.status : 0;
+ Emit("upgradeProgress", fail, result);
+}
+
+void UpdateClient::NotifyVerifyProgresss(int32_t retCode, uint32_t percent)
+{
+ verifyProgress_.status = (retCode == 0) ? UPDATE_STATE_VERIFY_SUCCESS : UPDATE_STATE_VERIFY_FAIL;
+ verifyProgress_.percent = percent;
+
+ UpdateResult result;
+ result.type = SESSION_VERIFY_PACKAGE;
+ result.result.progress = &verifyProgress_;
+ result.buildJSObject = BuildProgress;
+ Emit("verifyProgress", retCode, result);
+}
+
+void UpdateClient::NotifyCheckVersionDone(const VersionInfo &info)
+{
+ CLIENT_LOGE("NotifyCheckVersionDone status %d", info.status);
+ CLIENT_LOGE("NotifyCheckVersionDone errMsg %s", info.errMsg.c_str());
+ CLIENT_LOGE("NotifyCheckVersionDone versionName : %s", info.result[0].versionName.c_str());
+ CLIENT_LOGE("NotifyCheckVersionDone versionCode : %s", info.result[0].versionCode.c_str());
+ CLIENT_LOGE("NotifyCheckVersionDone verifyInfo : %s", info.result[0].verifyInfo.c_str());
+ CLIENT_LOGE("NotifyCheckVersionDone size : %zu", info.result[0].size);
+ CLIENT_LOGE("NotifyCheckVersionDone content : %s", info.descriptInfo[0].content.c_str());
+
+ UpdateHelper::CopyVersionInfo(info, versionInfo_);
+}
+
+int32_t UpdateClient::GetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t &intValue)
+{
+ bool result = false;
+ napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result);
+ if (result && (status == napi_ok)) {
+ napi_value value;
+ napi_get_named_property(env, arg, attrName.c_str(), &value);
+ napi_get_value_int32(env, value, &intValue);
+ }
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::GetBool(napi_env env, napi_value arg, const std::string &attrName, bool &value)
+{
+ bool result = false;
+ napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result);
+ if (result && (status == napi_ok)) {
+ napi_value obj;
+ napi_get_named_property(env, arg, attrName.c_str(), &obj);
+ napi_get_value_bool(env, obj, &value);
+ }
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::GetStringValue(napi_env env, napi_value arg, std::string &strValue)
+{
+ napi_valuetype valuetype;
+ napi_status status = napi_typeof(env, arg, &valuetype);
+ CLIENT_CHECK(status == napi_ok, return status, "Failed to napi_typeof");
+ CLIENT_CHECK(valuetype == napi_string, return CLIENT_INVALID_TYPE, "Invalid type");
+ std::vector buff(CLIENT_STRING_MAX_LENGTH);
+ size_t copied;
+ status = napi_get_value_string_utf8(env, arg, (char*)buff.data(), CLIENT_STRING_MAX_LENGTH, &copied);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_INVALID_TYPE, "Error get string");
+ strValue.assign(buff.data(), copied);
+ return napi_ok;
+}
+
+int32_t UpdateClient::SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string)
+{
+ napi_value value;
+ napi_create_string_utf8(env, string.c_str(), string.length(), &value);
+ napi_set_named_property(env, arg, attrName.c_str(), value);
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue)
+{
+ napi_value infoStatus;
+ napi_create_int32(env, intValue, &infoStatus);
+ napi_set_named_property(env, arg, attrName.c_str(), infoStatus);
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value)
+{
+ napi_value infoStatus;
+ napi_create_int32(env, value, &infoStatus);
+ napi_set_named_property(env, arg, attrName.c_str(), infoStatus);
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue)
+{
+ napi_value infoStatus;
+ napi_create_int64(env, intValue, &infoStatus);
+ napi_set_named_property(env, arg, attrName.c_str(), infoStatus);
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::GetUpdatePolicyFromArg(napi_env env,
+ const napi_value arg, UpdatePolicy &updatePolicy) const
+{
+ napi_valuetype type = napi_undefined;
+ napi_status status = napi_typeof(env, arg, &type);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_INVALID_TYPE, "Invlid argc %d", static_cast(status));
+ CLIENT_CHECK(type == napi_object, return CLIENT_INVALID_TYPE, "Invlid argc %d", static_cast(type));
+
+ // updatePolicy
+ int32_t tmpValue = 0;
+ int32_t ret = GetBool(env, arg, "autoDownload", updatePolicy.autoDownload);
+ ret |= GetBool(env, arg, "autoDownloadNet", updatePolicy.autoDownloadNet);
+ ret |= GetInt32(env, arg, "mode", tmpValue);
+ updatePolicy.mode = static_cast(tmpValue);
+ CLIENT_CHECK(ret == 0, return CLIENT_INVALID_TYPE, "Failed to get attr ");
+
+ // Get the array.
+ bool result = false;
+ status = napi_has_named_property(env, arg, "autoUpgradeInterval", &result);
+ if (result && (status == napi_ok)) {
+ napi_value value;
+ status = napi_get_named_property(env, arg, "autoUpgradeInterval", &value);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_FAIL, "Failed to get attr autoUpgradeInterval");
+ status = napi_is_array(env, value, &result);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_FAIL, "napi_is_array failed");
+ uint32_t count = 0;
+ status = napi_get_array_length(env, value, &count);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_FAIL, "napi_get_array_length failed");
+ uint32_t i = 0;
+ do {
+ napi_value element;
+ ret = napi_get_element(env, value, i, &element);
+ ret = napi_get_value_uint32(env, element, &updatePolicy.autoUpgradeInterval[i]);
+ CLIENT_LOGI("updatePolicy autoUpgradeInterval:%u ", updatePolicy.autoUpgradeInterval[i]);
+ if (i >= sizeof(updatePolicy.autoUpgradeInterval) / sizeof(updatePolicy.autoUpgradeInterval[0])) {
+ break;
+ }
+ i++;
+ } while (i < count);
+ }
+ ret |= GetInt32(env, arg, "autoUpgradeCondition", tmpValue);
+ CLIENT_CHECK(ret == 0, return CLIENT_INVALID_TYPE, "Failed to get attr autoUpgradeCondition");
+ updatePolicy.autoUpgradeCondition = static_cast(tmpValue);
+ CLIENT_LOGI("updatePolicy autoDownload:%d autoDownloadNet:%d mode:%d autoUpgradeCondition:%d",
+ static_cast(updatePolicy.autoDownload),
+ static_cast(updatePolicy.autoDownloadNet),
+ static_cast(updatePolicy.mode),
+ static_cast(updatePolicy.autoUpgradeCondition));
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::BuildCheckVersionResult(napi_env env, napi_value &obj, const UpdateResult &result)
+{
+ CLIENT_CHECK(result.type == SESSION_CHECK_VERSION || result.type == SESSION_GET_NEW_VERSION,
+ return CLIENT_INVALID_TYPE, "invalid type %d", result.type);
+ napi_status status = napi_create_object(env, &obj);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_INVALID_TYPE,
+ "Failed to create napi_create_object %d", static_cast(status));
+ VersionInfo *info = result.result.versionInfo;
+
+ // Add the result.
+ int32_t ret = SetInt32(env, obj, "status", info->status);
+ if (info->status == SERVER_BUSY || info->status == SYSTEM_ERROR) {
+ ret = SetString(env, obj, "errMsg", info->errMsg);
+ return ret;
+ }
+ napi_value checkResults;
+ napi_create_array_with_length(env, sizeof(info->result) / sizeof(info->result[0]), &checkResults);
+ for (size_t i = 0; i < sizeof(info->result) / sizeof(info->result[0]); i++) {
+ napi_value result;
+ status = napi_create_object(env, &result);
+
+ ret |= SetString(env, result, "versionName", info->result[i].versionName);
+ ret |= SetString(env, result, "versionCode", info->result[i].versionCode);
+ ret |= SetString(env, result, "verifyInfo", info->result[i].verifyInfo);
+ ret |= SetString(env, result, "descriptionId", info->result[i].descriptPackageId);
+ ret |= SetInt64(env, result, "size", info->result[i].size);
+ ret |= SetInt32(env, result, "packageType", info->result[i].packageType);
+ napi_set_element(env, checkResults, i, result);
+ }
+ napi_set_named_property(env, obj, "checkResults", checkResults);
+
+ napi_value descriptInfos;
+ napi_create_array_with_length(env, sizeof(info->descriptInfo) / sizeof(info->descriptInfo[0]), &descriptInfos);
+ for (size_t i = 0; i < sizeof(info->descriptInfo) / sizeof(info->descriptInfo[0]); i++) {
+ napi_value descriptInfo;
+ status = napi_create_object(env, &descriptInfo);
+ ret |= SetString(env, descriptInfo, "descriptionId", info->descriptInfo[i].descriptPackageId);
+ ret |= SetString(env, descriptInfo, "content", info->descriptInfo[i].content);
+ napi_set_element(env, descriptInfos, i, descriptInfo);
+ }
+ napi_set_named_property(env, obj, "descriptionInfo", descriptInfos);
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::BuildProgress(napi_env env, napi_value &obj, const UpdateResult &result)
+{
+ napi_status status = napi_create_object(env, &obj);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_INVALID_TYPE,
+ "Failed to create napi_create_object %d", static_cast(status));
+ int32_t ret = SetInt32(env, obj, "status", result.result.progress->status);
+ ret |= SetInt32(env, obj, "percent", result.result.progress->percent);
+ ret |= SetString(env, obj, "endReason", result.result.progress->endReason);
+ return CLIENT_SUCCESS;
+}
+
+int32_t UpdateClient::BuildErrorResult(napi_env env, napi_value &obj, int32_t result)
+{
+ napi_status status = napi_create_object(env, &obj);
+ CLIENT_CHECK(status == napi_ok, return CLIENT_INVALID_TYPE,
+ "Failed to create napi_create_object %d", static_cast(status));
+ return SetInt32(env, obj, "code", result);
+}
+
+int32_t UpdateClient::BuildInt32Status(napi_env env, napi_value &obj, const UpdateResult &result)
+{
+ return napi_create_int32(env, result.result.status, &obj);
+}
+
+int32_t UpdateClient::BuildUpdatePolicy(napi_env env, napi_value &obj, const UpdateResult &result)
+{
+ CLIENT_CHECK(result.type == SESSION_GET_POLICY || result.type == SESSION_SET_POLICY,
+ return CLIENT_INVALID_TYPE, "invalid type %d", result.type);
+ napi_status status = napi_create_object(env, &obj);
+ CLIENT_CHECK(status == napi_ok, return status, "Failed to create napi_create_object %d", status);
+ UpdatePolicy &updatePolicy = *result.result.updatePolicy;
+
+ // Add the result.
+ int32_t ret = SetBool(env, obj, "autoDownload", updatePolicy.autoDownload);
+ ret |= SetBool(env, obj, "autoDownloadNet", updatePolicy.autoDownloadNet);
+ ret |= SetInt32(env, obj, "mode", static_cast(updatePolicy.mode));
+ CLIENT_CHECK(ret == napi_ok, return ret, "Failed to add value %d", ret);
+
+ napi_value autoUpgradeInterval;
+ size_t count = sizeof(updatePolicy.autoUpgradeInterval) / sizeof(updatePolicy.autoUpgradeInterval[0]);
+ status = napi_create_array_with_length(env, count, &autoUpgradeInterval);
+ CLIENT_CHECK(status == napi_ok, return status, "Failed to create array for interval %d", status);
+ for (size_t i = 0; i < count; i++) {
+ napi_value interval;
+ status = napi_create_uint32(env, updatePolicy.autoUpgradeInterval[i], &interval);
+ status = napi_set_element(env, autoUpgradeInterval, i, interval);
+ CLIENT_CHECK(status == napi_ok, return status, "Failed to add interval to array %d", status);
+ }
+ status = napi_set_named_property(env, obj, "autoUpgradeInterval", autoUpgradeInterval);
+ CLIENT_CHECK(status == napi_ok, return status, "Failed to add autoUpgradeInterval %d", status);
+ ret |= SetInt32(env, obj, "autoUpgradeCondition", static_cast(updatePolicy.autoUpgradeCondition));
+ CLIENT_CHECK(ret == napi_ok, return ret, "Failed to add autoUpgradeCondition %d", ret);
+ return napi_ok;
+}
+
+int32_t UpdateClient::GetUpdateResult(int type, UpdateResult &result, int32_t &fail)
+{
+ fail = 0;
+ result.type = type;
+ switch (type) {
+ case SESSION_CHECK_VERSION:
+ case SESSION_GET_NEW_VERSION: {
+ fail = (versionInfo_.status == SYSTEM_ERROR) ? versionInfo_.status : 0;
+ result.result.versionInfo = &versionInfo_;
+ result.buildJSObject = BuildCheckVersionResult;
+ break;
+ }
+ case SESSION_DOWNLOAD: {
+ fail = (progress_.status == UPDATE_STATE_DOWNLOAD_FAIL || progress_.status == UPDATE_STATE_VERIFY_FAIL) ?
+ progress_.status : 0;
+ result.result.progress = &progress_;
+ result.buildJSObject = BuildProgress;
+ break;
+ }
+ case SESSION_UPGRADE: {
+ fail = (progress_.status == UPDATE_STATE_DOWNLOAD_FAIL) ? progress_.status : 0;
+ result.result.progress = &progress_;
+ result.buildJSObject = BuildProgress;
+ break;
+ }
+ case SESSION_VERIFY_PACKAGE: {
+ fail = (verifyProgress_.status == UPDATE_STATE_VERIFY_FAIL) ? verifyProgress_.status : 0;
+ result.result.progress = &verifyProgress_;
+ result.buildJSObject = BuildProgress;
+ break;
+ }
+ case SESSION_GET_POLICY: {
+ result.result.updatePolicy = &updatePolicy_;
+ result.buildJSObject = BuildUpdatePolicy;
+ break;
+ }
+ case SESSION_GET_STATUS: {
+ result.result.status = upgradeInfo_.status;
+ result.buildJSObject = BuildInt32Status;
+ break;
+ }
+ default:{
+ fail = result_;
+ result.result.status = result_;
+ result.buildJSObject = BuildInt32Status;
+ break;
+ }
+ }
+ return napi_ok;
+}
+} // namespace updateClient
diff --git a/client/update_client.h b/client/update_client.h
new file mode 100644
index 0000000..07abab0
--- /dev/null
+++ b/client/update_client.h
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UPDATE_CLIENT_H
+#define UPDATE_CLIENT_H
+
+#include
+#include
+#include