diff --git a/SystemServices/SystemServices.cpp b/SystemServices/SystemServices.cpp index 523dbedfc8..5500f2d0a7 100644 --- a/SystemServices/SystemServices.cpp +++ b/SystemServices/SystemServices.cpp @@ -1392,13 +1392,16 @@ namespace WPEFramework { JsonObject& response) { bool changeMode = true; + bool isTimerContext = false; + getBoolParameter("timercontext", isTimerContext); JsonObject param; std::string oldMode = m_currentMode; bool result = true; - if(m_remainingDuration>0) + if((!isTimerContext) && isTimerActive()) { populateResponseWithError(SysSrv_ModeChangeInProgress, response); - LOGERR("Mode change is already in progress.current mode is %s and it will be in progress for next %d seconds. Please try again later.\n",m_currentMode.c_str(),m_remainingDuration); + int actualDurationLeft = m_remainingDuration ? m_remainingDuration : 1; + LOGERR("Mode change is already in progress.current mode is %s and it will be in progress for next %d seconds. Please try again later.\n",m_currentMode.c_str(),actualDurationLeft); returnResponse(false); } if (parameters.HasLabel("modeInfo")) { @@ -1406,7 +1409,7 @@ namespace WPEFramework { if (param.HasLabel("duration") && param.HasLabel("mode")) { int duration = param["duration"].Number(); std::string newMode = param["mode"].String(); - if(duration>86400) + if(duration>86400) { LOGWARN("Duration is more than 24 hours. Setting duration to 24 hours,which is maximum allowed duration to set\n"); duration = 86400; @@ -1414,7 +1417,6 @@ namespace WPEFramework { LOGWARN("request to switch to mode '%s' from mode '%s' \ with duration %d\n", newMode.c_str(), oldMode.c_str(), duration); - if (MODE_NORMAL != newMode && MODE_WAREHOUSE != newMode && MODE_EAS != newMode) { LOGERR("value of new mode is incorrect, therefore \ @@ -1431,16 +1433,13 @@ namespace WPEFramework { m_currentMode = MODE_NORMAL; stopModeTimer(); } - if (changeMode) { IARM_Bus_CommonAPI_SysModeChange_Param_t modeParam; stringToIarmMode(oldMode, modeParam.oldMode); stringToIarmMode(m_currentMode, modeParam.newMode); - if (IARM_RESULT_SUCCESS == IARM_Bus_Call(IARM_BUS_DAEMON_NAME, "DaemonSysModeChange", &modeParam, sizeof(modeParam))) { LOGWARN("switched to mode '%s'\n", m_currentMode.c_str()); - if (MODE_NORMAL != m_currentMode && duration < 0) { LOGWARN("duration is negative, therefore \ mode timer stopped and Receiver will keep \ @@ -1462,6 +1461,7 @@ namespace WPEFramework { command = "rm -f "; } command += WAREHOUSE_MODE_FILE; + /* TODO: replace with system alternate. */ int sysStat = system(command.c_str()); LOGINFO("system returned %d\n", sysStat); @@ -1491,15 +1491,22 @@ namespace WPEFramework { m_temp_settings.setValue("mode_duration", m_remainingDuration); } - void SystemServices::stopModeTimer() + void SystemServices::stopModeTimer(bool isDetachRequired) { m_remainingDuration = 0; m_operatingModeTimer.stop(); - + if(isDetachRequired) + { + m_operatingModeTimer.detach(); + } //set values in temp file so they can be restored in receiver restarts / crashes // TODO: query & confirm time duration range. m_temp_settings.setValue("mode_duration", m_remainingDuration); } + bool SystemServices::isTimerActive() + { + return m_operatingModeTimer.isActive(); + } /** * @brief This function is used to update duration. @@ -1508,12 +1515,13 @@ namespace WPEFramework { { if (m_remainingDuration > 0) { m_remainingDuration--; + m_temp_settings.setValue("mode_duration", m_remainingDuration); } else { - m_operatingModeTimer.stop(); - m_operatingModeTimer.detach(); + stopModeTimer(true); JsonObject parameters, param, response; param["mode"] = "NORMAL"; param["duration"] = 0; + param["timercontext"] = true; parameters["modeInfo"] = param; if (_instance) { _instance->setMode(parameters,response); @@ -1521,9 +1529,6 @@ namespace WPEFramework { LOGERR("_instance is NULL.\n"); } } - - //set values in temp file so they can be restored in receiver restarts / crashes - m_temp_settings.setValue("mode_duration", m_remainingDuration); } /*** diff --git a/SystemServices/SystemServices.h b/SystemServices/SystemServices.h index 9bf1c6319b..e2092b8378 100644 --- a/SystemServices/SystemServices.h +++ b/SystemServices/SystemServices.h @@ -145,7 +145,8 @@ namespace WPEFramework { std::string m_strRegion; static void startModeTimer(int duration); - static void stopModeTimer(); + static void stopModeTimer(bool isDetachRequired = false); + static bool isTimerActive(); static void updateDuration(); std::string m_strStandardTerritoryList; #ifdef ENABLE_DEVICE_MANUFACTURER_INFO diff --git a/SystemServices/cTimer.cpp b/SystemServices/cTimer.cpp index 7dc450722f..be12c9943e 100644 --- a/SystemServices/cTimer.cpp +++ b/SystemServices/cTimer.cpp @@ -26,6 +26,7 @@ cTimer::cTimer() { clear = false; + active = false; interval = 0; } @@ -42,15 +43,17 @@ cTimer::~cTimer() Running this timer function as thread function */ void cTimer::timerFunction() { + this->active = true; while (true) { - if (this->clear) { + if (this->clear) { + this->active = false; return; - } + } std::this_thread::sleep_for(std::chrono::milliseconds(interval)); - if (this->clear) { + if (this->clear) { + this->active = false; return; - } - + } this->callBack_function(); } } @@ -74,19 +77,24 @@ bool cTimer::start() */ void cTimer::stop() { - this->clear = true; + this->clear = true; +} + +bool cTimer::isActive() +{ + return this->active; } void cTimer::detach() { - timerThread.detach(); + timerThread.detach(); } void cTimer::join() { - if (timerThread.joinable()) { - timerThread.join(); - } + if (timerThread.joinable()) { + timerThread.join(); + } } /*** @@ -100,4 +108,3 @@ void cTimer::setInterval(void (*function)(), int val) this->callBack_function = function; this->interval = val; } - diff --git a/SystemServices/cTimer.h b/SystemServices/cTimer.h index 2a7c2a1cec..b77c8a509f 100644 --- a/SystemServices/cTimer.h +++ b/SystemServices/cTimer.h @@ -25,6 +25,7 @@ using namespace std; class cTimer{ private: bool clear; + bool active; int interval; void (*callBack_function)() = NULL; std::thread timerThread; @@ -57,6 +58,7 @@ class cTimer{ void stop(); void detach(); void join(); + bool isActive(); /*** * @brief : Set interval in which the given function should be invoked.