188edb362Sopenharmony_ci/* 288edb362Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 388edb362Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 488edb362Sopenharmony_ci * you may not use this file except in compliance with the License. 588edb362Sopenharmony_ci * You may obtain a copy of the License at 688edb362Sopenharmony_ci * 788edb362Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 888edb362Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 988edb362Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1088edb362Sopenharmony_ci * See the License for the specific lan 1188edb362Sopenharmony_ci * guage governing permissions and 1288edb362Sopenharmony_ci * limitations under the License. 1388edb362Sopenharmony_ci */ 1488edb362Sopenharmony_ci 1588edb362Sopenharmony_ci#include <cstring> 1688edb362Sopenharmony_ci 1788edb362Sopenharmony_ci#include "isys_installer_callback_func.h" 1888edb362Sopenharmony_ci#include "isys_installer_callback.h" 1988edb362Sopenharmony_ci#include "module_update_kits.h" 2088edb362Sopenharmony_ci#include "module_error_code.h" 2188edb362Sopenharmony_ci 2288edb362Sopenharmony_ciusing namespace OHOS; 2388edb362Sopenharmony_ciusing namespace OHOS::SysInstaller; 2488edb362Sopenharmony_ci 2588edb362Sopenharmony_cistatic const int32_t MIN_PARAM_NUM = 2; 2688edb362Sopenharmony_cistatic const int32_t MAX_PARAM_NUM = 3; 2788edb362Sopenharmony_ci 2888edb362Sopenharmony_cistatic const std::string HELP_MSG = 2988edb362Sopenharmony_ci "usage: module_update_tool\n" 3088edb362Sopenharmony_ci "example: ./module_update_tool install /data/tmp/xxx.hmp \n" 3188edb362Sopenharmony_ci "command list:\n" 3288edb362Sopenharmony_ci " install : upgrade some SA via hmp package\n" 3388edb362Sopenharmony_ci " uninstall : degrade some SA via hmp name\n" 3488edb362Sopenharmony_ci " update : upgrade SA via hmp package\n" 3588edb362Sopenharmony_ci " get_hmp_version : get hmp package version\n" 3688edb362Sopenharmony_ci " get_result : get hmp upgrade result\n" 3788edb362Sopenharmony_ci " show hmpname : show upgrade sa info, if hmp name is null, show all\n"; 3888edb362Sopenharmony_ci 3988edb362Sopenharmony_cistatic const std::string INSTALL_PARAM = "install"; 4088edb362Sopenharmony_cistatic const std::string UNINSTALL_PARAM = "uninstall"; 4188edb362Sopenharmony_cistatic const std::string SHOW_INFO = "show"; 4288edb362Sopenharmony_cistatic const std::string UPDATE_PARAM = "update"; 4388edb362Sopenharmony_cistatic const std::string GET_HMP_VERSION = "get_hmp_version"; 4488edb362Sopenharmony_cistatic const std::string GET_RESULT = "get_result"; 4588edb362Sopenharmony_cistatic const int32_t RET_FAILED = -1; 4688edb362Sopenharmony_ci 4788edb362Sopenharmony_cistatic bool CheckParam(int argc) 4888edb362Sopenharmony_ci{ 4988edb362Sopenharmony_ci if (argc < MIN_PARAM_NUM || argc > MAX_PARAM_NUM) { 5088edb362Sopenharmony_ci printf("Invalid module update command\n"); 5188edb362Sopenharmony_ci printf("%s", HELP_MSG.c_str()); 5288edb362Sopenharmony_ci return false; 5388edb362Sopenharmony_ci } 5488edb362Sopenharmony_ci return true; 5588edb362Sopenharmony_ci} 5688edb362Sopenharmony_ci 5788edb362Sopenharmony_cistatic std::string GetFailReasonByErrCode(int32_t err) 5888edb362Sopenharmony_ci{ 5988edb362Sopenharmony_ci switch (err) { 6088edb362Sopenharmony_ci case 0: 6188edb362Sopenharmony_ci return "success"; 6288edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_SERVICE_NOT_FOUND: 6388edb362Sopenharmony_ci return "ERR_SERVICE_NOT_FOUND"; 6488edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_INVALID_PATH: 6588edb362Sopenharmony_ci return "ERR_INVALID_PATH"; 6688edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_LOWER_VERSION: 6788edb362Sopenharmony_ci return "ERR_LOWER_VERSION"; 6888edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_VERIFY_SIGN_FAIL: 6988edb362Sopenharmony_ci return "ERR_VERIFY_SIGN_FAIL"; 7088edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_INSTALL_FAIL: 7188edb362Sopenharmony_ci return "ERR_INSTALL_FAIL"; 7288edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_UNINSTALL_FAIL: 7388edb362Sopenharmony_ci return "ERR_UNINSTALL_FAIL"; 7488edb362Sopenharmony_ci case OHOS::SysInstaller::ERR_REPORT_STATUS_FAIL: 7588edb362Sopenharmony_ci return "ERR_REPORT_STATUS_FAIL"; 7688edb362Sopenharmony_ci default: 7788edb362Sopenharmony_ci return "Unknown Error"; 7888edb362Sopenharmony_ci } 7988edb362Sopenharmony_ci} 8088edb362Sopenharmony_ci 8188edb362Sopenharmony_cistatic void PrintErrMsg(const std::string &errMsg) 8288edb362Sopenharmony_ci{ 8388edb362Sopenharmony_ci printf("%s\n", errMsg.c_str()); 8488edb362Sopenharmony_ci} 8588edb362Sopenharmony_ci 8688edb362Sopenharmony_cistatic void PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> &modulePackageInfos) 8788edb362Sopenharmony_ci{ 8888edb362Sopenharmony_ci std::list<OHOS::SysInstaller::ModulePackageInfo>::iterator it; 8988edb362Sopenharmony_ci printf("Got %zu upgraded modules info\n", modulePackageInfos.size()); 9088edb362Sopenharmony_ci for (it = modulePackageInfos.begin(); it != modulePackageInfos.end(); it++) { 9188edb362Sopenharmony_ci OHOS::SysInstaller::ModulePackageInfo moduleInfo = *it; 9288edb362Sopenharmony_ci printf("%s\n", moduleInfo.hmpName.c_str()); 9388edb362Sopenharmony_ci std::list<OHOS::SysInstaller::SaInfo>::iterator saIt; 9488edb362Sopenharmony_ci for (saIt = moduleInfo.saInfoList.begin(); saIt != moduleInfo.saInfoList.end(); saIt++) { 9588edb362Sopenharmony_ci std::string verStr = (*saIt).version; 9688edb362Sopenharmony_ci printf(" {saName:%s saId:%d version:%s}\n", (*saIt).saName.c_str(), (*saIt).saId, verStr.c_str()); 9788edb362Sopenharmony_ci } 9888edb362Sopenharmony_ci printf(" \n"); 9988edb362Sopenharmony_ci } 10088edb362Sopenharmony_ci} 10188edb362Sopenharmony_ci 10288edb362Sopenharmony_ciclass ProcessCallback : public ISysInstallerCallbackFunc { 10388edb362Sopenharmony_cipublic: 10488edb362Sopenharmony_ci ProcessCallback() = default; 10588edb362Sopenharmony_ci ~ProcessCallback() = default; 10688edb362Sopenharmony_ci void OnUpgradeProgress(UpdateStatus updateStatus, int percent, const std::string &resultMsg) override 10788edb362Sopenharmony_ci { 10888edb362Sopenharmony_ci printf("ProgressCallback progress %d percent %d msg %s\n", updateStatus, percent, resultMsg.c_str()); 10988edb362Sopenharmony_ci } 11088edb362Sopenharmony_ci}; 11188edb362Sopenharmony_ci 11288edb362Sopenharmony_cistatic int UpdateModulePackage(const std::string &path) 11388edb362Sopenharmony_ci{ 11488edb362Sopenharmony_ci printf("try to update an upgrade package\n"); 11588edb362Sopenharmony_ci sptr<ISysInstallerCallbackFunc> callback = new ProcessCallback; 11688edb362Sopenharmony_ci if (callback == nullptr) { 11788edb362Sopenharmony_ci printf("callback new failed\n"); 11888edb362Sopenharmony_ci return -1; 11988edb362Sopenharmony_ci } 12088edb362Sopenharmony_ci 12188edb362Sopenharmony_ci int ret = ModuleUpdateKits::GetInstance().StartUpdateHmpPackage(path, callback); 12288edb362Sopenharmony_ci PrintErrMsg(GetFailReasonByErrCode(ret)); 12388edb362Sopenharmony_ci return ret; 12488edb362Sopenharmony_ci} 12588edb362Sopenharmony_ci 12688edb362Sopenharmony_cistatic int GetHmpVersion() 12788edb362Sopenharmony_ci{ 12888edb362Sopenharmony_ci printf("try to get hmp version\n"); 12988edb362Sopenharmony_ci 13088edb362Sopenharmony_ci std::vector<HmpVersionInfo> versioInfo = ModuleUpdateKits::GetInstance().GetHmpVersionInfo(); 13188edb362Sopenharmony_ci for (auto &info : versioInfo) { 13288edb362Sopenharmony_ci printf("name:%s laneCode:%s compatibleVersion:%s version:%s\n", 13388edb362Sopenharmony_ci info.name.c_str(), info.laneCode.c_str(), info.compatibleVersion.c_str(), info.version.c_str()); 13488edb362Sopenharmony_ci } 13588edb362Sopenharmony_ci return 0; 13688edb362Sopenharmony_ci} 13788edb362Sopenharmony_ci 13888edb362Sopenharmony_cistatic int GetResult() 13988edb362Sopenharmony_ci{ 14088edb362Sopenharmony_ci printf("try to get hmp result\n"); 14188edb362Sopenharmony_ci 14288edb362Sopenharmony_ci std::vector<HmpUpdateInfo> updateInfo = ModuleUpdateKits::GetInstance().GetHmpUpdateResult(); 14388edb362Sopenharmony_ci for (auto &info : updateInfo) { 14488edb362Sopenharmony_ci printf("path:%s result:%d resultMsg:%s\n", info.path.c_str(), info.result, info.resultMsg.c_str()); 14588edb362Sopenharmony_ci } 14688edb362Sopenharmony_ci return 0; 14788edb362Sopenharmony_ci} 14888edb362Sopenharmony_ci 14988edb362Sopenharmony_cistatic int ShowInfo(const std::string &hmpStr) 15088edb362Sopenharmony_ci{ 15188edb362Sopenharmony_ci printf("try to show module update info\n"); 15288edb362Sopenharmony_ci std::list<OHOS::SysInstaller::ModulePackageInfo> modulePackageInfos; 15388edb362Sopenharmony_ci int ret = OHOS::SysInstaller::ModuleUpdateKits::GetInstance().GetModulePackageInfo(hmpStr, modulePackageInfos); 15488edb362Sopenharmony_ci PrintUpgradeInfo(modulePackageInfos); 15588edb362Sopenharmony_ci PrintErrMsg(GetFailReasonByErrCode(ret)); 15688edb362Sopenharmony_ci return ret; 15788edb362Sopenharmony_ci} 15888edb362Sopenharmony_ci 15988edb362Sopenharmony_ciint main(int argc, char **argv) 16088edb362Sopenharmony_ci{ 16188edb362Sopenharmony_ci if (!CheckParam(argc)) { 16288edb362Sopenharmony_ci return RET_FAILED; 16388edb362Sopenharmony_ci } 16488edb362Sopenharmony_ci 16588edb362Sopenharmony_ci int ret = 0; 16688edb362Sopenharmony_ci OHOS::SysInstaller::ModuleUpdateKits& moduleUpdateKits = OHOS::SysInstaller::ModuleUpdateKits::GetInstance(); 16788edb362Sopenharmony_ci ret = moduleUpdateKits.InitModuleUpdate(); 16888edb362Sopenharmony_ci if (ret != 0) { 16988edb362Sopenharmony_ci PrintErrMsg(GetFailReasonByErrCode(ret)); 17088edb362Sopenharmony_ci return ret; 17188edb362Sopenharmony_ci } 17288edb362Sopenharmony_ci 17388edb362Sopenharmony_ci if (INSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) { 17488edb362Sopenharmony_ci printf("try to update a module\n"); 17588edb362Sopenharmony_ci ret = moduleUpdateKits.InstallModulePackage(argv[MIN_PARAM_NUM]); 17688edb362Sopenharmony_ci PrintErrMsg(GetFailReasonByErrCode(ret)); 17788edb362Sopenharmony_ci return ret; 17888edb362Sopenharmony_ci } 17988edb362Sopenharmony_ci if (UNINSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) { 18088edb362Sopenharmony_ci printf("try to uninstall an upgrade package\n"); 18188edb362Sopenharmony_ci ret = moduleUpdateKits.UninstallModulePackage(argv[MIN_PARAM_NUM]); 18288edb362Sopenharmony_ci PrintErrMsg(GetFailReasonByErrCode(ret)); 18388edb362Sopenharmony_ci return ret; 18488edb362Sopenharmony_ci } 18588edb362Sopenharmony_ci if (UPDATE_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) { 18688edb362Sopenharmony_ci return UpdateModulePackage(argv[MIN_PARAM_NUM]); 18788edb362Sopenharmony_ci } 18888edb362Sopenharmony_ci if (GET_HMP_VERSION.compare(argv[1]) == 0) { 18988edb362Sopenharmony_ci return GetHmpVersion(); 19088edb362Sopenharmony_ci } 19188edb362Sopenharmony_ci if (GET_RESULT.compare(argv[1]) == 0) { 19288edb362Sopenharmony_ci return GetResult(); 19388edb362Sopenharmony_ci } 19488edb362Sopenharmony_ci if (SHOW_INFO.compare(argv[1]) == 0) { 19588edb362Sopenharmony_ci return ShowInfo((argc != MIN_PARAM_NUM) ? argv[MIN_PARAM_NUM] : ""); 19688edb362Sopenharmony_ci } 19788edb362Sopenharmony_ci 19888edb362Sopenharmony_ci printf("invalid command. \n"); 19988edb362Sopenharmony_ci printf("%s", HELP_MSG.c_str()); 20088edb362Sopenharmony_ci return ret; 20188edb362Sopenharmony_ci}