From 6187c0d120cc119cafa2d1a53b7186ac978efc12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 8 May 2024 16:57:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=8F=91=E9=80=81=E4=BA=8B=E4=BB=B6=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20Signed-off-by:=20=E9=82=B9=E5=8F=8B=E6=9D=BE=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bundle.json | 1 + .../callback/include/base_callback_utils.h | 19 ++- .../callback/src/base_callback_utils.cpp | 68 ++++++--- .../src/base_callback_utils_empty.cpp | 37 +++-- services/engine/engine_sa.gni | 3 + services/engine/include/hap_connection.h | 61 ++++++++ services/engine/include/i_notify_connection.h | 29 ++++ services/engine/include/ouc_hap_connection.h | 55 +++++++ services/engine/include/update_notify.h | 8 +- services/engine/include/update_service.h | 1 + .../engine/include/update_service_cache.h | 3 +- services/engine/src/hap_connection.cpp | 127 ++++++++++++++++ services/engine/src/ouc_hap_connection.cpp | 137 ++++++++++++++++++ services/engine/src/update_notify.cpp | 45 +++++- services/engine/src/update_service.cpp | 3 +- services/engine/src/update_service_cache.cpp | 6 +- .../include/firmware_callback_utils.h | 8 +- .../callback/src/firmware_callback_utils.cpp | 10 +- .../src/firmware_check_data_processor.cpp | 2 + .../upgrade/flow/src/firmware_manager.cpp | 2 + 20 files changed, 575 insertions(+), 50 deletions(-) create mode 100644 services/engine/include/hap_connection.h create mode 100644 services/engine/include/i_notify_connection.h create mode 100644 services/engine/include/ouc_hap_connection.h create mode 100644 services/engine/src/hap_connection.cpp create mode 100644 services/engine/src/ouc_hap_connection.cpp diff --git a/bundle.json b/bundle.json index 9df0c393..c82f8ef8 100644 --- a/bundle.json +++ b/bundle.json @@ -32,6 +32,7 @@ "common_event_service", "curl", "eventhandler", + "ffrt", "hilog", "ipc", "safwk", diff --git a/services/core/ability/callback/include/base_callback_utils.h b/services/core/ability/callback/include/base_callback_utils.h index 9486be05..8a9352a1 100644 --- a/services/core/ability/callback/include/base_callback_utils.h +++ b/services/core/ability/callback/include/base_callback_utils.h @@ -22,8 +22,10 @@ #include "error_message.h" #include "event_id.h" #include "event_info.h" +#include "firmware_task.h" #include "iupdate_callback.h" #include "progress.h" +#include "subscribe_info.h" #include "upgrade_status.h" #include "upgrade_info.h" #include "version_component.h" @@ -32,17 +34,20 @@ namespace OHOS { namespace UpdateEngine { class BaseCallbackUtils { public: - void NotifyEvent(const std::string &versionDigestInfo, EventId eventId, UpgradeStatus status, - const ErrorMessage &errorMessage = {}, const std::vector &versionComponents = {}); + bool NotifyEvent(const std::string &versionDigestInfo, EventId eventId, UpgradeStatus status, + const ErrorMessage &errorMessage = {}, const std::vector &versionComponents = {}, + bool isRecompile = false); + bool NotifyEvent(const FirmwareTask &task, EventId eventId, const ErrorMessage &errorMessage = {}, + const std::vector &versionComponents = {}); void ProgressCallback(const std::string &versionDigestInfo, const Progress &progress); - + bool IsOUCDisplayed(); protected: - virtual BusinessSubType GetBusinessSubType() = 0; + virtual SubscribeInfo GetSubscribeInfo() = 0; private: - void NotifyToHap(EventInfo &info); - void CallbackToHap(EventInfo &eventInfo); - sptr GetUpgradeCallback(const UpgradeInfo &upgradeInfo); + bool NotifyToHap(EventInfo &eventInfo); + bool ConnectToHap(EventInfo &eventInfo); + bool CallbackToHap(EventInfo &eventInfo); }; } // namespace UpdateEngine } // namespace OHOS diff --git a/services/core/ability/callback/src/base_callback_utils.cpp b/services/core/ability/callback/src/base_callback_utils.cpp index 5741a3b2..184a1f44 100644 --- a/services/core/ability/callback/src/base_callback_utils.cpp +++ b/services/core/ability/callback/src/base_callback_utils.cpp @@ -15,7 +15,9 @@ #include "base_callback_utils.h" +#include "dupdate_errno.h" #include "iupdate_callback.h" +#include "ouc_hap_connection.h" #include "subscribe_info.h" #include "update_log.h" #include "update_notify.h" @@ -25,16 +27,33 @@ namespace OHOS { namespace UpdateEngine { -void BaseCallbackUtils::NotifyEvent(const std::string &versionDigestInfo, EventId eventId, UpgradeStatus status, - const ErrorMessage &errorMessage, const std::vector &versionComponents) +constexpr int32_t APP_NOT_RECOMPILE = 0; +constexpr int32_t APP_RECOMPILE = 1; + +bool BaseCallbackUtils::NotifyEvent(const std::string &versionDigestInfo, EventId eventId, UpgradeStatus status, + const ErrorMessage &errorMessage, const std::vector &versionComponents, bool isRecompile) { TaskBody taskBody; taskBody.status = status; + taskBody.subStatus = isRecompile ? APP_RECOMPILE : APP_NOT_RECOMPILE; taskBody.versionDigestInfo.versionDigest = versionDigestInfo; taskBody.errorMessages.push_back(errorMessage); taskBody.versionComponents = versionComponents; - EventInfo info(eventId, taskBody); - NotifyToHap(info); + EventInfo eventInfo(eventId, taskBody); + return CallbackToHap(eventInfo); +} + +bool BaseCallbackUtils::NotifyEvent(const FirmwareTask &task, EventId eventId, const ErrorMessage &errorMessage, + const std::vector &versionComponents) +{ + TaskBody taskBody; + taskBody.status = task.status; + taskBody.progress = task.progress; + taskBody.versionDigestInfo.versionDigest = task.taskId; + taskBody.errorMessages.push_back(errorMessage); + taskBody.versionComponents = versionComponents; + EventInfo eventInfo(eventId, taskBody); + return CallbackToHap(eventInfo); } void BaseCallbackUtils::ProgressCallback(const std::string &versionDigestInfo, const Progress &progress) @@ -71,36 +90,43 @@ void BaseCallbackUtils::ProgressCallback(const std::string &versionDigestInfo, c CallbackToHap(eventInfo); } -void BaseCallbackUtils::CallbackToHap(EventInfo &eventInfo) +bool BaseCallbackUtils::ConnectToHap(EventInfo &eventInfo) +{ + ENGINE_LOGI("ConnectToHap upgradeCallback eventInfoStr %{public}s", eventInfo.ToJson().c_str()); + SubscribeInfo subscribeInfo = GetSubscribeInfo(); + return OucHapConnection::GetInstance()->SendMessage(eventInfo, subscribeInfo) == OHOS_SUCCESS; +} + +bool BaseCallbackUtils::CallbackToHap(EventInfo &eventInfo) { - UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(GetBusinessSubType()); - auto upgradeCallback = GetUpgradeCallback(upgradeInfo); + UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(GetSubscribeInfo()); + auto upgradeCallback = UpdateServiceUtil::GetUpgradeCallback(upgradeInfo); if (upgradeCallback != nullptr) { ENGINE_LOGD("CallbackToHap upgradeCallback eventInfoStr %{public}s", eventInfo.ToJson().c_str()); upgradeCallback->OnEvent(eventInfo); + return true; } else { - NotifyToHap(eventInfo); + return ConnectToHap(eventInfo); } } -sptr BaseCallbackUtils::GetUpgradeCallback(const UpgradeInfo &upgradeInfo) +bool BaseCallbackUtils::NotifyToHap(EventInfo &eventInfo) { - sptr service = UpdateService::GetInstance(); - if (service == nullptr) { - ENGINE_LOGI("GetUpgradeCallback no instance"); - return nullptr; + std::string eventInfoStr = eventInfo.ToJson(); + ENGINE_LOGI("Notify eventInfoStr %{public}s", eventInfoStr.c_str()); + if (eventInfoStr.empty()) { + ENGINE_LOGE("Notify eventInfoStr nukll"); + return false; } - return service->GetUpgradeCallback(upgradeInfo); + SubscribeInfo subscribeInfo = GetSubscribeInfo(); + return OHOS::UpdateEngine::UpdateNotify::NotifyToAppService(eventInfoStr, subscribeInfo.ToJson()); } -void BaseCallbackUtils::NotifyToHap(EventInfo &info) +bool BaseCallbackUtils::IsOUCDisplayed() { - std::string eventInfoStr = info.ToJson(); - ENGINE_LOGI("Notify eventInfoStr %{public}s", eventInfoStr.c_str()); - if (!eventInfoStr.empty()) { - SubscribeInfo subscribeInfo{GetBusinessSubType()}; - OHOS::UpdateEngine::UpdateNotify::NotifyToAppService(eventInfoStr, subscribeInfo.ToJson()); - } + UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(GetSubscribeInfo()); + auto upgradeCallback = UpdateServiceUtil::GetUpgradeCallback(upgradeInfo); + return upgradeCallback != nullptr; } } // namespace UpdateEngine } // namespace OHOS diff --git a/services/core/ability/callback/src/base_callback_utils_empty.cpp b/services/core/ability/callback/src/base_callback_utils_empty.cpp index 00ca9b65..eea6930b 100644 --- a/services/core/ability/callback/src/base_callback_utils_empty.cpp +++ b/services/core/ability/callback/src/base_callback_utils_empty.cpp @@ -21,30 +21,47 @@ namespace OHOS { namespace UpdateEngine { -void BaseCallbackUtils::NotifyEvent(const std::string &versionDigestInfo, EventId eventId, UpgradeStatus status, - const ErrorMessage &errorMessage, const std::vector &versionComponents) +bool BaseCallbackUtils::NotifyEvent(const std::string &versionDigestInfo, EventId eventId, UpgradeStatus status, + const ErrorMessage &errorMessage, const std::vector &versionComponents, bool isRecompile) { - ENGINE_LOGI("BaseCallbackUtils NotifyEvent"); + ENGINE_LOGI("NotifyEvent"); + return true; +} + +bool BaseCallbackUtils::NotifyEvent(const FirmwareTask &task, EventId eventId, const ErrorMessage &errorMessage, + const std::vector &versionComponents) +{ + ENGINE_LOGI("NotifyEvent"); + return true; } void BaseCallbackUtils::ProgressCallback(const std::string &versionDigestInfo, const Progress &progress) { - ENGINE_LOGI("BaseCallbackUtils ProgressCallback"); + ENGINE_LOGI("ProgressCallback"); +} + +bool BaseCallbackUtils::CallbackToHap(EventInfo &eventInfo) +{ + ENGINE_LOGI("CallbackToHap"); + return true; } -void BaseCallbackUtils::CallbackToHap(EventInfo &eventInfo) +bool BaseCallbackUtils::NotifyToHap(EventInfo &info) { - ENGINE_LOGI("BaseCallbackUtils CallbackToHap"); + ENGINE_LOGI("NotifyToHap"); + return true; } -sptr BaseCallbackUtils::GetUpgradeCallback(const UpgradeInfo &upgradeInfo) +bool BaseCallbackUtils::ConnectToHap(EventInfo &eventInfo) { - return nullptr; + ENGINE_LOGI("ConnectToHap"); + return true; } -void BaseCallbackUtils::NotifyToHap(EventInfo &info) +bool BaseCallbackUtils::IsOUCDisplayed() { - ENGINE_LOGI("BaseCallbackUtils NotifyToHap"); + ENGINE_LOGI("IsOUCDisplayed"); + return true; } } // namespace UpdateEngine } // namespace OHOS diff --git a/services/engine/engine_sa.gni b/services/engine/engine_sa.gni index 63facb72..01ddfd7a 100644 --- a/services/engine/engine_sa.gni +++ b/services/engine/engine_sa.gni @@ -53,6 +53,8 @@ if (ability_ability_base_enable || ability_ability_runtime_enable) { sa_sources += [ "$updateengine_root_path/services/core/ability/alarm/src/alarm_timer_utils.cpp", "$updateengine_root_path/services/core/ability/callback/src/base_callback_utils.cpp", + "$updateengine_root_path/services/engine/src/hap_connection.cpp", + "$updateengine_root_path/services/engine/src/ouc_hap_connection.cpp", "$updateengine_root_path/services/engine/src/update_notify.cpp", ] } else { @@ -128,6 +130,7 @@ sa_external_deps = [ "cJSON:cjson", "c_utils:utils", # refbase "curl:curl_shared", + "ffrt:libffrt", "hilog:libhilog", "hisysevent:libhisysevent", "init:libbegetutil", diff --git a/services/engine/include/hap_connection.h b/services/engine/include/hap_connection.h new file mode 100644 index 00000000..945be550 --- /dev/null +++ b/services/engine/include/hap_connection.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2024 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 HAP_CONNECTION_H +#define HAP_CONNECTION_H + +#include +#include +#include + +#include +#include "ability_manager_interface.h" +#include "ability_manager_client.h" + +#include "i_notify_connection.h" +#include "subscribe_info.h" + +namespace OHOS::UpdateEngine { +class HapConnection : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(HapConnection); + + HapConnection(); + ~HapConnection(); + + static sptr GetInstance(); + + sptr GetHapRemoteObject(const std::string &bundleName); + bool ConnectAbility(const std::string &bundleName, const std::string &serviceName, int32_t timeout); + [[maybe_unused]] bool CancelConnect(const std::string &bundleName); + + void HandleAbilityConnect(const std::string &bundleName, const sptr &remoteObject, + int32_t resultCode); + void HandleAbilityDisconnect(const std::string &bundleName, int32_t resultCode); + +private: + static std::mutex instanceLock_; + static sptr instance_; + + std::mutex remoteObjMapLock_; + std::unordered_map> remoteObjMap_; + + std::mutex connectMutex_; + std::condition_variable conditionVal_; + + std::unordered_map> connectMap_; +}; +} // namespace OHOS::UpdateEngine +#endif // HAP_CONNECTION_H diff --git a/services/engine/include/i_notify_connection.h b/services/engine/include/i_notify_connection.h new file mode 100644 index 00000000..c8e31552 --- /dev/null +++ b/services/engine/include/i_notify_connection.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 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 I_NOTIFY_CONNECTION_H + #define I_NOTIFY_CONNECTION_H + +#include + +#include "iremote_broker.h" + +namespace OHOS::UpdateEngine { +class INotifyConnection : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS::Updater.INotifyConnection"); +}; +} // namespace OHOS::UpdateEngine +#endif // I_NOTIFY_CONNECTION_H diff --git a/services/engine/include/ouc_hap_connection.h b/services/engine/include/ouc_hap_connection.h new file mode 100644 index 00000000..dcf851e3 --- /dev/null +++ b/services/engine/include/ouc_hap_connection.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024 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 OUC_HAP_CONNECTION_H +#define OUC_HAP_CONNECTION_H + +#include +#include + +#include "ffrt.h" +#include "singleton.h" + +#include "event_info.h" +#include "i_notify_connection.h" +#include "subscribe_info.h" + +namespace OHOS::UpdateEngine { +class OucHapConnection : public DelayedSingleton { + DECLARE_DELAYED_SINGLETON(OucHapConnection); + +public: + int32_t SendMessage(EventInfo &eventInfo, SubscribeInfo &subscribeInfo); + +private: + bool ConnectOuc(); + bool HandleMessage(const std::string &message); + void SetDelayedTask(int64_t time); + void CancelConnect(); + +private: + enum class OucCode { + UNKNOWN = 0, + OUC = 5 + }; + +private: + int64_t lastSendTime_ = 0; + std::mutex connectStatusLock_; + bool connectStatus_ = false; + ffrt::queue connectQueue_ { "ouc_queue" }; +}; +} // namespace OHOS::UpdateEngine +#endif // OUC_HAP_CONNECTION_H diff --git a/services/engine/include/update_notify.h b/services/engine/include/update_notify.h index a89c70b3..6f546026 100644 --- a/services/engine/include/update_notify.h +++ b/services/engine/include/update_notify.h @@ -21,6 +21,7 @@ #include "ability_connect_callback_interface.h" #include "ability_manager_interface.h" #include "ability_manager_client.h" +#include "hap_connection.h" #include "if_system_ability_manager.h" #include "system_ability_definition.h" #include "update_no_constructor.h" @@ -44,12 +45,15 @@ private: class NotifyConnection : public AAFwk::AbilityConnectionStub { public: - explicit NotifyConnection() = default; - ~NotifyConnection() = default; + explicit NotifyConnection(const sptr &instance); + ~NotifyConnection(); void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, int32_t resultCode) override; void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; + +private: + sptr instance_ = nullptr; }; } // namespace UpdateEngine } // namespace OHOS diff --git a/services/engine/include/update_service.h b/services/engine/include/update_service.h index 0090f95b..8fff3c78 100644 --- a/services/engine/include/update_service.h +++ b/services/engine/include/update_service.h @@ -21,6 +21,7 @@ #include "if_system_ability_manager.h" #include "ipc_skeleton.h" +#include "subscribe_info.h" #include "iremote_stub.h" #include "system_ability.h" diff --git a/services/engine/include/update_service_cache.h b/services/engine/include/update_service_cache.h index a42a986e..bf3d61cb 100644 --- a/services/engine/include/update_service_cache.h +++ b/services/engine/include/update_service_cache.h @@ -19,6 +19,7 @@ #include #include "business_sub_type.h" +#include "subscribe_info.h" #include "upgrade_info.h" #include "upgrade_interval.h" @@ -26,7 +27,7 @@ namespace OHOS { namespace UpdateEngine { class UpdateServiceCache { public: - static UpgradeInfo GetUpgradeInfo(BusinessSubType businessSubType); + static UpgradeInfo GetUpgradeInfo(const SubscribeInfo &subscribeInfo); static void SetUpgradeInfo(const UpgradeInfo &upgradeInfo); static uint64_t GetCheckInterval(BusinessSubType businessSubType); static void SetCheckInterval(BusinessSubType businessSubType, uint64_t interval); diff --git a/services/engine/src/hap_connection.cpp b/services/engine/src/hap_connection.cpp new file mode 100644 index 00000000..05660e1a --- /dev/null +++ b/services/engine/src/hap_connection.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2024 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 "hap_connection.h" + +#include + +#include "want.h" +#include "update_define.h" +#include "update_log.h" +#include "update_notify.h" + +namespace OHOS::UpdateEngine { +std::mutex HapConnection::instanceLock_; +sptr HapConnection::instance_ = nullptr; + +HapConnection::HapConnection() +{ + ENGINE_LOGD("HapConnection"); +} + +HapConnection::~HapConnection() +{ + ENGINE_LOGD("~HapConnection"); +} + +sptr HapConnection::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard autoLock(instanceLock_); + if (instance_ == nullptr) { + instance_ = new HapConnection(); + } + } + return instance_; +} + +sptr HapConnection::GetHapRemoteObject(const std::string &bundleName) +{ + auto remoteObj = remoteObjMap_.find(bundleName); + if (remoteObj == remoteObjMap_.end()) { + ENGINE_LOGE("SendMessage, remoteObj is nullptr"); + return nullptr; + } + return remoteObj->second; +} + +[[maybe_unused]] bool HapConnection::CancelConnect(const std::string &bundleName) +{ + auto connect = connectMap_.find(bundleName); + if (connect == connectMap_.end() || connect->second == nullptr) { + ENGINE_LOGE("connect is nullptr"); + return false; + } + + auto result = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(connect->second); + if (result != OHOS::ERR_OK) { + ENGINE_LOGE("DisconnectAbility false"); + return false; + } + return true; +} + +bool HapConnection::ConnectAbility(const std::string &bundleName, const std::string &serviceName, int32_t timeout) +{ + std::lock_guard autoLock(remoteObjMapLock_); + if (remoteObjMap_[bundleName] != nullptr) { + return true; + } + + AAFwk::Want want; + want.SetElementName(bundleName, serviceName); + want.SetParam("Timeout", timeout); + auto connect = sptr::MakeSptr(instance_); + auto ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, connect, -1); + + std::unique_lock uniqueLock(connectMutex_); + constexpr int32_t connectTimeoutTime = 5; + conditionVal_.wait_for(uniqueLock, std::chrono::seconds(connectTimeoutTime)); + + if (ret != OHOS::ERR_OK || remoteObjMap_[bundleName] == nullptr) { + ENGINE_LOGE("HapConnection failed, ret = %{public}d", ret); + return false; + } + + connectMap_.emplace(bundleName, connect); + ENGINE_LOGE("HapConnection success, ret = %{public}d", ret); + return true; +} + +void HapConnection::HandleAbilityConnect(const std::string &bundleName, const sptr &remoteObject, + int32_t resultCode) +{ + if (remoteObject != nullptr) { + remoteObjMap_[bundleName] = remoteObject; + } + conditionVal_.notify_one(); +} + +void HapConnection::HandleAbilityDisconnect(const std::string &bundleName, int32_t resultCode) +{ + std::lock_guard autoLock(remoteObjMapLock_); + auto remoteObj = remoteObjMap_.find(bundleName); + if (remoteObj != remoteObjMap_.end()) { + remoteObj->second = nullptr; + remoteObjMap_.erase(remoteObj); + } + + auto connect = connectMap_.find(bundleName); + if (connect != connectMap_.end()) { + connect->second = nullptr; + connectMap_.erase(connect); + } +} +} // namespace OHOS::UpdateEngine \ No newline at end of file diff --git a/services/engine/src/ouc_hap_connection.cpp b/services/engine/src/ouc_hap_connection.cpp new file mode 100644 index 00000000..64d018eb --- /dev/null +++ b/services/engine/src/ouc_hap_connection.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 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 "ouc_hap_connection.h" + +#include +#include +#include + +#include "want.h" + +#include "alarm_timer_utils.h" +#include "dupdate_errno.h" +#include "hap_connection.h" +#include "update_define.h" +#include "update_log.h" + +namespace OHOS::UpdateEngine { +namespace { +constexpr int64_t TIMEOUT = 12; // 超时时间为12秒 +} + +OucHapConnection::OucHapConnection() +{ + ENGINE_LOGD("OucHapConnection"); + lastSendTime_ = static_cast(AlarmTimerUtils::GetSystemBootTime()); +}; + +OucHapConnection::~OucHapConnection() +{ + ENGINE_LOGD("~OucHapConnection"); +} + +bool OucHapConnection::ConnectOuc() +{ + constexpr int32_t oucTimeout = 15; //ouc超时时间 + return HapConnection::GetInstance()->ConnectAbility(OUC_PACKAGE_NAME, OUC_SERVICE_EXT_ABILITY_NAME, oucTimeout); +} + +bool OucHapConnection::HandleMessage(const std::string &message) +{ + if (!ConnectOuc()) { + ENGINE_LOGE("HandleMessage, can not connect to ouc"); + return false; + } + + { + std::lock_guard autoLock(connectStatusLock_); + if (!connectStatus_) { + SetDelayedTask(TIMEOUT); + connectStatus_ = true; + } + } + + MessageParcel data; + if (!data.WriteString16(Str8ToStr16(message))) { + ENGINE_LOGE("HandleMessage, write subscribeInfo failed"); + return false; + } + + auto remoteObject = HapConnection::GetInstance()->GetHapRemoteObject(OUC_PACKAGE_NAME); + if (remoteObject == nullptr) { + ENGINE_LOGE("remoteObject is nullptr"); + return false; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + auto result = remoteObject->SendRequest(CAST_INT(OucCode::OUC), data, reply, option); + if (result != 0) { + ENGINE_LOGE("HandleMessage SendRequest, error result %{public}d", result); + HapConnection::GetInstance()->CancelConnect(OUC_PACKAGE_NAME); + return false; + } + ENGINE_LOGD("HandleMessage SendRequest success"); + return true; +} + +int32_t OucHapConnection::SendMessage(EventInfo &eventInfo, SubscribeInfo &subscribeInfo) +{ + std::string message = JsonBuilder().Append("{").Append("EventInfo", eventInfo.GetJsonBuilder()) + .Append("SubScribeInfo", subscribeInfo.GetJsonBuilder()).Append("}").ToJson(); + lastSendTime_ = static_cast(AlarmTimerUtils::GetSystemBootTime()); + constexpr int64_t sleepDuration = 1000; // 单位:毫秒 + constexpr int32_t retryTime = 3; // 重试次数 + int32_t count = 0; + do { + if (HandleMessage(message)) { + ENGINE_LOGD("SendMessage request success"); + return OHOS_SUCCESS; + } + count++; + if (count >= retryTime) { + ENGINE_LOGE("SendMessage request failed"); + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(sleepDuration)); + } while (count <= retryTime); + return OHOS_FAILURE; +} + +void OucHapConnection::SetDelayedTask(int64_t time) +{ + ENGINE_LOGI("SetDelayedTask %{public}" PRId64 "", time); + std::chrono::seconds ms(time); + std::chrono::microseconds us = std::chrono::duration_cast(ms); + connectQueue_.submit_h([this] { CancelConnect(); }, ffrt::task_attr().delay(us.count())); +} + +void OucHapConnection::CancelConnect() +{ + ENGINE_LOGI("CancelConnect"); + int64_t currentTime = static_cast(AlarmTimerUtils::GetSystemBootTime()); + int64_t time = currentTime - lastSendTime_; + time = time / 1000; // 转换成秒 + + int64_t nextTaskTime = TIMEOUT - time; + if (nextTaskTime <= 0) { + connectStatus_ = false; + HapConnection::GetInstance()->CancelConnect(OUC_PACKAGE_NAME); + return; + } + SetDelayedTask(nextTaskTime); +} +} // namespace OHOS::UpdateEngine diff --git a/services/engine/src/update_notify.cpp b/services/engine/src/update_notify.cpp index 01000ac1..0c805d7a 100644 --- a/services/engine/src/update_notify.cpp +++ b/services/engine/src/update_notify.cpp @@ -15,6 +15,8 @@ #include "update_notify.h" +#include + #include "iservice_registry.h" #include "subscribe_info.h" @@ -22,6 +24,12 @@ namespace OHOS { namespace UpdateEngine { +namespace { + const std::unordered_map hapNameMap = { + {"/com.ohos.updateapp//ServiceExtAbility", "com.ohos.updateapp"} + }; +} + ErrCode UpdateNotify::StartAbility(const AAFwk::Want &want) { ErrCode result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want); @@ -74,15 +82,48 @@ bool UpdateNotify::NotifyToAppService(const std::string &eventInfo, const std::s return StartAbility(want) == OHOS::ERR_OK; } +NotifyConnection::NotifyConnection(const sptr &instance) +{ + ENGINE_LOGD("NotifyConnection constructor"); + instance_ = instance; +} + +NotifyConnection::~NotifyConnection() +{ + ENGINE_LOGD("NotifyConnection destructor"); +} + void NotifyConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, int32_t resultCode) { - ENGINE_LOGI("OnAbilityConnectDone successfully. result %{public}d", resultCode); + ENGINE_LOGI("OnAbilityConnectDone uri: %{public}s", element.GetURI().c_str()); + auto bandleName = hapNameMap.find(element.GetURI()); + if (bandleName == hapNameMap.end()) { + ENGINE_LOGE("OnAbilityConnectDone has no hap"); + return; + } + + if (instance_ == nullptr) { + return; + } + + instance_->HandleAbilityConnect(bandleName->second, remoteObject, resultCode); } void NotifyConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) { - ENGINE_LOGI("OnAbilityDisconnectDone successfully. result %{public}d", resultCode); + ENGINE_LOGI("OnAbilityConnectDone uri: %{public}s", element.GetURI().c_str()); + auto bandleName = hapNameMap.find(element.GetURI()); + if (bandleName == hapNameMap.end()) { + ENGINE_LOGE("OnAbilityDisconnectDone has no hap"); + return; + } + + if (instance_ == nullptr) { + return; + } + + instance_->HandleAbilityDisconnect(bandleName->second, resultCode); } } // namespace UpdateEngine } // namespace OHOS \ No newline at end of file diff --git a/services/engine/src/update_service.cpp b/services/engine/src/update_service.cpp index 5071ee0f..ba4a5c92 100644 --- a/services/engine/src/update_service.cpp +++ b/services/engine/src/update_service.cpp @@ -411,7 +411,8 @@ int UpdateService::Dump(int fd, const std::vector &args) } if (args.size() == 0) { - UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(BusinessSubType::FIRMWARE); + SubscribeInfo subscribeInfo(BusinessSubType::FIRMWARE); + UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(subscribeInfo); BuildUpgradeInfoDump(fd, upgradeInfo); BuildTaskInfoDump(fd); DumpUpgradeCallback(fd); diff --git a/services/engine/src/update_service_cache.cpp b/services/engine/src/update_service_cache.cpp index 081ef7bf..81b1d7d5 100644 --- a/services/engine/src/update_service_cache.cpp +++ b/services/engine/src/update_service_cache.cpp @@ -48,14 +48,14 @@ bool UpdateServiceCache::IsParamType(BusinessSubType businessSubType) return businessSubType == BusinessSubType::PARAM; } -UpgradeInfo UpdateServiceCache::GetUpgradeInfo(BusinessSubType businessSubType) +UpgradeInfo UpdateServiceCache::GetUpgradeInfo(const SubscribeInfo &subscribeInfo) { - if (!IsTypelegal(businessSubType)) { + if (!IsTypelegal(subscribeInfo.businessType.subType)) { UpgradeInfo upgradeInfo; return upgradeInfo; } - return IsParamType(businessSubType) ? paramUpgradeInfo_ : upgradeInfo_; + return IsParamType(subscribeInfo.businessType.subType) ? paramUpgradeInfo_ : upgradeInfo_; } void UpdateServiceCache::SetUpgradeInfo(const UpgradeInfo &upgradeInfo) diff --git a/services/firmware/callback/include/firmware_callback_utils.h b/services/firmware/callback/include/firmware_callback_utils.h index 27ce0859..f80ac2a8 100644 --- a/services/firmware/callback/include/firmware_callback_utils.h +++ b/services/firmware/callback/include/firmware_callback_utils.h @@ -25,8 +25,14 @@ namespace UpdateEngine { class FirmwareCallbackUtils final : public BaseCallbackUtils, public DelayedSingleton { DECLARE_DELAYED_SINGLETON(FirmwareCallbackUtils); +public: + void SetBusinessSubType(BusinessSubType subType); + protected: - BusinessSubType GetBusinessSubType() final; + SubscribeInfo GetSubscribeInfo() final; + +private: + BusinessSubType subType_ = BusinessSubType::FIRMWARE; }; } // namespace UpdateEngine } // namespace OHOS diff --git a/services/firmware/callback/src/firmware_callback_utils.cpp b/services/firmware/callback/src/firmware_callback_utils.cpp index 9fa41276..33e487f6 100644 --- a/services/firmware/callback/src/firmware_callback_utils.cpp +++ b/services/firmware/callback/src/firmware_callback_utils.cpp @@ -21,9 +21,15 @@ FirmwareCallbackUtils::FirmwareCallbackUtils() {} FirmwareCallbackUtils::~FirmwareCallbackUtils() {} -BusinessSubType FirmwareCallbackUtils::GetBusinessSubType() +void FirmwareCallbackUtils::SetBusinessSubType(BusinessSubType subType) { - return BusinessSubType::FIRMWARE; + subType_ = subType; +} + +SubscribeInfo FirmwareCallbackUtils::GetSubscribeInfo() +{ + SubscribeInfo subscribeInfo(subType_); + return subscribeInfo; } } // namespace UpdateEngine } // namespace OHOS \ No newline at end of file diff --git a/services/firmware/upgrade/data_processor/src/firmware_check_data_processor.cpp b/services/firmware/upgrade/data_processor/src/firmware_check_data_processor.cpp index 5e4c2d6c..5d9f265e 100644 --- a/services/firmware/upgrade/data_processor/src/firmware_check_data_processor.cpp +++ b/services/firmware/upgrade/data_processor/src/firmware_check_data_processor.cpp @@ -212,6 +212,8 @@ void FirmwareCheckDataProcessor::HandleNoNewVersion() task.taskId, EventId::EVENT_TASK_CANCEL, UpgradeStatus::INIT); } FirmwareUpdateHelper::ClearFirmwareInfo(); + // 没有新版本,默认设置为:FIRMWARE + FirmwareCallbackUtils::GetInstance()->SetBusinessSubType(BusinessSubType::FIRMWARE); } CombinationType FirmwareCheckDataProcessor::GetCombinationType() diff --git a/services/firmware/upgrade/flow/src/firmware_manager.cpp b/services/firmware/upgrade/flow/src/firmware_manager.cpp index cb520ad6..9fc3d232 100644 --- a/services/firmware/upgrade/flow/src/firmware_manager.cpp +++ b/services/firmware/upgrade/flow/src/firmware_manager.cpp @@ -92,6 +92,8 @@ void FirmwareManager::DelayInit(StartupReason startupReason) eventType = CommonEventType::BOOT_COMPLETE; } + // 设置FirmwareCallbackUtils的subType + FirmwareCallbackUtils::GetInstance()->SetBusinessSubType(BusinessSubType::FIRMWARE); // 以下两种情况会向OUC发送初始启动消息: // 1. DUE启动原因为StartupReason::PROCESS_ENV_RESET,DUE缓存数据清空 // 2. DUE首次启动,还未向OUC发送过初始启动消息 -- Gitee From a90a411fc0760fc8f8ba5caf99e83a2039570069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 8 May 2024 09:48:41 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- services/core/ability/callback/src/base_callback_utils.cpp | 2 +- services/engine/include/i_notify_connection.h | 4 ++-- services/engine/src/ouc_hap_connection.cpp | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/services/core/ability/callback/src/base_callback_utils.cpp b/services/core/ability/callback/src/base_callback_utils.cpp index 184a1f44..e4aba858 100644 --- a/services/core/ability/callback/src/base_callback_utils.cpp +++ b/services/core/ability/callback/src/base_callback_utils.cpp @@ -44,7 +44,7 @@ bool BaseCallbackUtils::NotifyEvent(const std::string &versionDigestInfo, EventI } bool BaseCallbackUtils::NotifyEvent(const FirmwareTask &task, EventId eventId, const ErrorMessage &errorMessage, - const std::vector &versionComponents) + const std::vector &versionComponents) { TaskBody taskBody; taskBody.status = task.status; diff --git a/services/engine/include/i_notify_connection.h b/services/engine/include/i_notify_connection.h index c8e31552..77f3ce16 100644 --- a/services/engine/include/i_notify_connection.h +++ b/services/engine/include/i_notify_connection.h @@ -13,8 +13,8 @@ * limitations under the License. */ - #ifndef I_NOTIFY_CONNECTION_H - #define I_NOTIFY_CONNECTION_H +#ifndef I_NOTIFY_CONNECTION_H +#define I_NOTIFY_CONNECTION_H #include diff --git a/services/engine/src/ouc_hap_connection.cpp b/services/engine/src/ouc_hap_connection.cpp index 64d018eb..4e8242ac 100644 --- a/services/engine/src/ouc_hap_connection.cpp +++ b/services/engine/src/ouc_hap_connection.cpp @@ -122,9 +122,10 @@ void OucHapConnection::SetDelayedTask(int64_t time) void OucHapConnection::CancelConnect() { ENGINE_LOGI("CancelConnect"); + constexpr int64_t timeDuration = 1000; int64_t currentTime = static_cast(AlarmTimerUtils::GetSystemBootTime()); int64_t time = currentTime - lastSendTime_; - time = time / 1000; // 转换成秒 + time = time / timeDuration; // 转换成秒 int64_t nextTaskTime = TIMEOUT - time; if (nextTaskTime <= 0) { -- Gitee From 1dee7cb67e3024a175489f01087bfa3e449d23b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Thu, 9 May 2024 06:26:26 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- services/engine/include/i_notify_connection.h | 2 +- services/engine/src/ouc_hap_connection.cpp | 2 +- services/engine/src/update_notify.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/engine/include/i_notify_connection.h b/services/engine/include/i_notify_connection.h index 77f3ce16..c0b500c0 100644 --- a/services/engine/include/i_notify_connection.h +++ b/services/engine/include/i_notify_connection.h @@ -23,7 +23,7 @@ namespace OHOS::UpdateEngine { class INotifyConnection : public OHOS::IRemoteBroker { public: - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS::Updater.INotifyConnection"); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Updater.INotifyConnection"); }; } // namespace OHOS::UpdateEngine #endif // I_NOTIFY_CONNECTION_H diff --git a/services/engine/src/ouc_hap_connection.cpp b/services/engine/src/ouc_hap_connection.cpp index 4e8242ac..3a3b15bb 100644 --- a/services/engine/src/ouc_hap_connection.cpp +++ b/services/engine/src/ouc_hap_connection.cpp @@ -91,7 +91,7 @@ bool OucHapConnection::HandleMessage(const std::string &message) int32_t OucHapConnection::SendMessage(EventInfo &eventInfo, SubscribeInfo &subscribeInfo) { std::string message = JsonBuilder().Append("{").Append("EventInfo", eventInfo.GetJsonBuilder()) - .Append("SubScribeInfo", subscribeInfo.GetJsonBuilder()).Append("}").ToJson(); + .Append("SubscribeInfo", subscribeInfo.GetJsonBuilder()).Append("}").ToJson(); lastSendTime_ = static_cast(AlarmTimerUtils::GetSystemBootTime()); constexpr int64_t sleepDuration = 1000; // 单位:毫秒 constexpr int32_t retryTime = 3; // 重试次数 diff --git a/services/engine/src/update_notify.cpp b/services/engine/src/update_notify.cpp index 0c805d7a..110b1d80 100644 --- a/services/engine/src/update_notify.cpp +++ b/services/engine/src/update_notify.cpp @@ -112,7 +112,7 @@ void NotifyConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &eleme void NotifyConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) { - ENGINE_LOGI("OnAbilityConnectDone uri: %{public}s", element.GetURI().c_str()); + ENGINE_LOGI("OnAbilityDisConnectDone uri: %{public}s", element.GetURI().c_str()); auto bandleName = hapNameMap.find(element.GetURI()); if (bandleName == hapNameMap.end()) { ENGINE_LOGE("OnAbilityDisconnectDone has no hap"); -- Gitee