diff --git a/services/native/include/thermal_action/action/action_airplane.h b/services/native/include/thermal_action/action/action_airplane.h index 446895d49d7bff95d7f587ba955e625a78c87c11..91ec90594b46b682b28bd1c24eb96502050c8e3d 100644 --- a/services/native/include/thermal_action/action/action_airplane.h +++ b/services/native/include/thermal_action/action/action_airplane.h @@ -33,8 +33,8 @@ public: void SetEnableEvent(bool enable) override; void AddActionValue(std::string value) override; void Execute() override; - int32_t AirplaneRequest(const uint32_t value); - int32_t AirplaneExecution(const uint32_t value); + int32_t AirplaneRequest(const uint32_t& value); + int32_t AirplaneExecution(const uint32_t& value); private: uint32_t GetActionValue(); std::vector valueList_; diff --git a/services/native/include/thermal_service.h b/services/native/include/thermal_service.h index 4deba00591b9b2d3f5f37686923d9d816d933a07..dd7e75a2beea62cab9e3906dfa1d866031a1e19c 100644 --- a/services/native/include/thermal_service.h +++ b/services/native/include/thermal_service.h @@ -41,6 +41,12 @@ #include "thermal_srv_stub.h" #include "v1_1/ithermal_interface.h" #include "v1_1/thermal_types.h" +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART +#include "common_event_data.h" +#include "common_event_manager.h" +#include "common_event_subscribe_info.h" +#include "common_event_support.h" +#endif namespace OHOS { namespace PowerMgr { @@ -158,7 +164,10 @@ public: { return scene_; } - +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART + static bool userAirplaneState_; + static bool isThermalAirplane_; +#endif private: bool Init(); bool InitThermalDriver(); @@ -179,6 +188,9 @@ private: void RegisterFanHdiCallback(); void RegisterBootCompletedCallback(); void EnableMock(const std::string& actionName, void* mockObject); +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART + bool SubscribeCommonEvent(); +#endif bool ready_ {false}; static std::atomic_bool isBootCompleted_; bool isSimulation_ {false}; @@ -201,7 +213,19 @@ private: static std::string scene_; static sptr instance_; static std::mutex singletonMutex_; +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART + std::shared_ptr subscriberPtr_; +#endif +}; +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART +class AirplaneCommonEventSubscriber : public EventFwk::CommonEventSubscriber { +public: + explicit AirplaneCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo) + : EventFwk::CommonEventSubscriber(subscribeInfo) {} + virtual ~AirplaneCommonEventSubscriber() {} + void OnReceiveEvent(const EventFwk::CommonEventData &data) override; }; +#endif } // namespace PowerMgr } // namespace OHOS #endif // THERMAL_SERVICE_H diff --git a/services/native/src/thermal_action/action/action_airplane.cpp b/services/native/src/thermal_action/action/action_airplane.cpp index 4c20d67e19916aacf30c4b7fc284b28d917094d1..afc8eb5947720a5f3a31bcd85fce0bd54f806cb9 100644 --- a/services/native/src/thermal_action/action/action_airplane.cpp +++ b/services/native/src/thermal_action/action/action_airplane.cpp @@ -58,6 +58,7 @@ void ActionAirplane::AddActionValue(std::string value) char *endptr; long int result = strtol(value.c_str(), &endptr, STRTOL_FORMART_DEC); if (*endptr != '\0') { + THERMAL_HILOGE(COMP_SVC, "parse airplane value failed"); return; } valueList_.push_back(static_cast(result)); @@ -95,38 +96,48 @@ uint32_t ActionAirplane::GetActionValue() return value; } -int32_t ActionAirplane::AirplaneRequest(const uint32_t value) +int32_t ActionAirplane::AirplaneRequest(const uint32_t& value) { +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART switch (value) { case ActionAirplane::TempStatus::LOWER_TEMP: - THERMAL_HILOGI(COMP_SVC, "device exit Airplane mode"); -#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART - NetManagerStandard::NetConnClient::GetInstance().SetAirplaneMode(false); -#endif + if (!ThermalService::userAirplaneState_) { + THERMAL_HILOGD(COMP_SVC, "device exit Airplane mode"); + ThermalService::isThermalAirplane_ = true; + NetManagerStandard::NetConnClient::GetInstance().SetAirplaneMode(false); + } else { + THERMAL_HILOGD(COMP_SVC, "device keep Airplane mode"); + } break; case ActionAirplane::TempStatus::HIGHER_TEMP: - THERMAL_HILOGI(COMP_SVC, "device start Airplane mode"); -#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART - NetManagerStandard::NetConnClient::GetInstance().SetAirplaneMode(true); -#endif + if (!ThermalService::userAirplaneState_) { + THERMAL_HILOGD(COMP_SVC, "device start Airplane mode"); + ThermalService::isThermalAirplane_ = true; + NetManagerStandard::NetConnClient::GetInstance().SetAirplaneMode(true); + } else { + THERMAL_HILOGD(COMP_SVC, "device already in Airplane mode"); + } break; default: break; } +#endif return ERR_OK; } -int32_t ActionAirplane::AirplaneExecution(const uint32_t value) +int32_t ActionAirplane::AirplaneExecution(const uint32_t& value) { int32_t ret = -1; char buf[MAX_PATH] = {0}; ret = snprintf_s(buf, MAX_PATH, sizeof(buf) - 1, AIRPLANE_PATH); if (ret < ERR_OK) { + THERMAL_HILOGE(COMP_SVC, "snprintf_s airplane value failed"); return ret; } std::string valueString = std::to_string(value) + "\n"; ret = FileOperation::WriteFile(buf, valueString, valueString.length()); if (ret != ERR_OK) { + THERMAL_HILOGE(COMP_SVC, "write airplane value failed"); return ret; } return ERR_OK; diff --git a/services/native/src/thermal_service.cpp b/services/native/src/thermal_service.cpp index 65dc07eb4a9b2739019cea57f8ec0454d5baba3e..13a9611b086a32a3bea35f14e178d9174c89e55f 100644 --- a/services/native/src/thermal_service.cpp +++ b/services/native/src/thermal_service.cpp @@ -52,6 +52,10 @@ SysParam::BootCompletedCallback g_bootCompletedCallback; } // namespace std::atomic_bool ThermalService::isBootCompleted_ = false; std::string ThermalService::scene_; +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART +bool ThermalService::userAirplaneState_ = false; +bool ThermalService::isThermalAirplane_ = false; +#endif ThermalService::ThermalService() : SystemAbility(POWER_MANAGER_THERMAL_SERVICE_ID, true) {} ThermalService::~ThermalService() {} @@ -103,9 +107,47 @@ void ThermalService::OnAddSystemAbility(int32_t systemAbilityId, const std::stri THERMAL_HILOGI(COMP_SVC, "systemAbilityId=%{public}d, deviceId=%{private}s", systemAbilityId, deviceId.c_str()); if (systemAbilityId == COMMON_EVENT_SERVICE_ID) { InitStateMachine(); +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART + SubscribeCommonEvent(); +#endif + } +} + +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART +bool ThermalService::SubscribeCommonEvent() +{ + using namespace OHOS::EventFwk; + bool result = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::COMMON); + if (!subscriberPtr_) { + subscriberPtr_ = std::make_shared(subscribeInfo); } + result = CommonEventManager::SubscribeCommonEvent(subscriberPtr_); + if (!result) { + THERMAL_HILOGE(COMP_SVC, "Subscribe CommonEvent failed"); + } else { + THERMAL_HILOGD(COMP_SVC, "Subscribe CommonEvent success"); + } + return result; } +void AirplaneCommonEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) +{ + if (!ThermalService::isThermalAirplane_) { + int32_t code = data.GetCode(); + ThermalService::userAirplaneState_ = static_cast(code); + THERMAL_HILOGD(COMP_SVC, "user %{public}s Airplane mode", + ThermalService::userAirplaneState_ ? "open" : "close"); + } else { + ThermalService::isThermalAirplane_ = false; + THERMAL_HILOGD(COMP_SVC, "thermal change Airplane mode"); + } +} +#endif + bool ThermalService::Init() { THERMAL_HILOGD(COMP_SVC, "Enter"); @@ -336,6 +378,13 @@ void ThermalService::OnStop() hdiServiceMgr_->UnregisterServiceStatusListener(hdiServStatListener_); hdiServiceMgr_ = nullptr; } +#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART + if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr_)) { + THERMAL_HILOGE(COMP_SVC, "Thermal Onstop unregister to commonevent manager failed!"); + } else { + THERMAL_HILOGD(COMP_SVC, "Thermal Onstop unregister to commonevent manager success!"); + } +#endif } bool ThermalService::SubscribeThermalTempCallback(