Monitor PM state to be reactive to the device wakeups 14/140014/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 21 Jul 2017 10:25:34 +0000 (19:25 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 21 Jul 2017 10:40:49 +0000 (19:40 +0900)
Change-Id: I71a9fcc8a9faaa0649b3c2197f413917a91cae0d
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
packaging/context-job-scheduler.spec
src/server/CMakeLists.txt
src/server/JobRunner.cpp
src/server/JobRunner.h
src/server/JobState.cpp
src/server/SchedTimer.cpp
src/server/SchedTimer.h

index d90d492b63901dc97325968dadacba79b8b261d2..4a5350fa57741fcecfedc186b2faec564b80102a 100644 (file)
@@ -13,6 +13,7 @@ BuildRequires: pkgconfig(jsoncpp)
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(bundle)
+BuildRequires: pkgconfig(vconf)
 BuildRequires: pkgconfig(capi-base-common)
 
 BuildRequires: pkgconfig(context-common-server)
index 9326172d41740784bc31d29baab44d4d059ae2dc..21573b2b23bcb07746d83fd89e148a7f4d5a2bd7 100644 (file)
@@ -1,6 +1,6 @@
 SET(target "${PROJECT_NAME}-server-genuine")
 
-SET(DEPS "${DEPS} jsoncpp sqlite3 context-common-server")
+SET(DEPS "${DEPS} jsoncpp sqlite3 vconf context-common-server")
 
 FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
 MESSAGE("Sources: ${SRCS}")
index 7671fc70645e19262eef3a71990fb02faddb08aa..4feafa94d1ab2bf74405187c92907b9600873638 100644 (file)
@@ -22,7 +22,7 @@
 
 using namespace ctx;
 
-unsigned int PeriodicJobRunner::__periodicJobRunnerCnt = 0;
+unsigned int PeriodicJobRunner::__activePeriodicJobCnt = 0;
 
 JobRunner::JobRunner(JobManager* mgr, const std::string& owner, JobInfo* info) :
        __jobManager(mgr),
@@ -107,12 +107,10 @@ PeriodicJobRunner::PeriodicJobRunner(JobManager* mgr, const std::string& owner,
        JobRunner(mgr, owner, info),
        __lastScheduledTime(0)
 {
-       ++__periodicJobRunnerCnt;
 }
 
 PeriodicJobRunner::~PeriodicJobRunner()
 {
-       --__periodicJobRunnerCnt;
 }
 
 void PeriodicJobRunner::start()
@@ -120,7 +118,9 @@ void PeriodicJobRunner::start()
        __jobInfo->setStarted(true);
        setState(new TimerStandbyState(this));
 
-       if (__periodicJobRunnerCnt == TimerStandbyState::getCount())
+       __activePeriodicJobCnt++;
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
                SchedTimer::reset();
 }
 
@@ -129,7 +129,9 @@ void PeriodicJobRunner::stop()
        __jobInfo->setStarted(false);
        setState(NULL);
 
-       if (__periodicJobRunnerCnt == TimerStandbyState::getCount())
+       __activePeriodicJobCnt--;
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
                SchedTimer::reset();
 }
 
@@ -137,7 +139,7 @@ void PeriodicJobRunner::restart()
 {
        setState(new TimerStandbyState(this));
 
-       if (__periodicJobRunnerCnt == TimerStandbyState::getCount())
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
                SchedTimer::reset();
 }
 
@@ -146,7 +148,9 @@ void PeriodicJobRunner::terminate()
        setState(NULL);
        __jobManager->removeRunner(this);
 
-       if (__periodicJobRunnerCnt == TimerStandbyState::getCount())
+       __activePeriodicJobCnt--;
+
+       if (__activePeriodicJobCnt == TimerStandbyState::getCount())
                SchedTimer::reset();
 }
 
index cd7d79d4bc950f6ef758b9cd712e2749070267eb..0a3c6fc014d83753e6ea4834dac3a84af8f0b5e5 100644 (file)
@@ -74,7 +74,7 @@ namespace ctx {
 
        private:
                time_t __lastScheduledTime;
-               static unsigned int __periodicJobRunnerCnt;
+               static unsigned int __activePeriodicJobCnt;
        };
 
 
index ed78e56f3e25f3e47cfe49643e452a268cf23ffd..c3b199a39c4050ac15b658b00f2dd7d2ec508ea5 100644 (file)
@@ -91,6 +91,7 @@ TimerStandbyState::~TimerStandbyState()
 {
        EXIT;
        --__timerStandbyStateCnt;
+       SchedTimer::remove(this);
 }
 
 bool TimerStandbyState::execute()
index 96a11077f7cfb9557105e8235e22c850e6dd0325..f273087fc1b3e16db5f99e428d13e077554776aa 100644 (file)
@@ -28,12 +28,14 @@ SchedTimer* SchedTimer::__instance = NULL;
 
 SchedTimer::SchedTimer() :
        __scheduledTime(0),
-       __paused(false)
+       __paused(false),
+       __listeningPmState(false)
 {
 }
 
 SchedTimer::~SchedTimer()
 {
+       __unlistenPmState();
 }
 
 bool SchedTimer::init()
@@ -85,6 +87,7 @@ bool SchedTimer::onTimerExpired(unsigned int timerId, unsigned int intervalMs, v
        time_t limit = time(NULL);
        ISchedTimerListener* listener = NULL;
 
+       _D("Checking the scheduled jobs");
        while ((listener = __timeLine.pop(limit))) {
                listener->onSchedTimerExpired();
        }
@@ -113,7 +116,10 @@ void SchedTimer::__reset()
        __scheduledTime = next;
        __timer.cancelAll();
 
-       IF_FAIL_VOID(next != 0);
+       if (next == 0) {
+               __unlistenPmState();
+               return;
+       }
 
        unsigned int intervalMin = (next - time(NULL) + 59) / 60;
        if (intervalMin < 1) {
@@ -122,8 +128,7 @@ void SchedTimer::__reset()
        }
 
        __timer.addAlarm(intervalMin, this, NULL);
-
-       //TODO: g_timeout to catch the device wakeups caused by other reasons.
+       __listenPmState();
 }
 
 void SchedTimer::__pause()
@@ -139,6 +144,45 @@ void SchedTimer::__resume()
        __reset();
 }
 
+void SchedTimer::__listenPmState()
+{
+       IF_FAIL_VOID(!__listeningPmState);
+
+       _D("Subscribing the power state");
+       vconf_notify_key_changed(VCONFKEY_PM_STATE, __pmStateChanged, this);
+
+       __listeningPmState = true;
+}
+
+void SchedTimer::__unlistenPmState()
+{
+       IF_FAIL_VOID(__listeningPmState);
+
+       _D("Unsubscribing the power state");
+       vconf_ignore_key_changed(VCONFKEY_PM_STATE, __pmStateChanged);
+
+       __listeningPmState = false;
+}
+
+void SchedTimer::__pmStateChanged(keynode_t* key, void* data)
+{
+       static time_t expiredTime = 0;
+       static int pmState = VCONFKEY_PM_STATE_NORMAL;
+
+       int prevState = pmState;
+       int err = vconf_get_int(VCONFKEY_PM_STATE, &pmState);
+
+       IF_FAIL_VOID_TAG(err == E_NONE, _E, "Cannot get %s", VCONFKEY_PM_STATE);
+       IF_FAIL_VOID(prevState >= VCONFKEY_PM_STATE_LCDOFF);
+
+       time_t now = time(NULL);
+       IF_FAIL_VOID(now - expiredTime > DIFF_THRESHOLD);
+
+       static_cast<SchedTimer*>(data)->onTimerExpired(0, 0, NULL);
+       expiredTime = now;
+}
+
+
 SchedTimer::TimeLineEntry::TimeLineEntry(ISchedTimerListener* l, time_t t, unsigned int m) :
        listener(l),
        scheduledTime(t),
index 6f2b82e61d52dac2e87e7240a4630d243241d8dd..851c0a093ff33aeab44f2190b24807497e834009 100644 (file)
@@ -18,6 +18,7 @@
 #define __CONTEXT_JOB_SCHEDULER_SCHED_TIMER_H__
 
 #include <list>
+#include <vconf.h>
 #include <Timer.h>
 #include <ITimerListener.h>
 
@@ -56,6 +57,11 @@ namespace ctx {
                void __pause();
                void __resume();
 
+               void __listenPmState();
+               void __unlistenPmState();
+
+               static void __pmStateChanged(keynode_t* key, void* data);
+
                struct TimeLineEntry {
                        ISchedTimerListener* listener;
                        time_t scheduledTime;
@@ -88,6 +94,7 @@ namespace ctx {
                Timer __timer;
                time_t __scheduledTime;
                bool __paused;
+               bool __listeningPmState;
 
                static SchedTimer* __instance;
        };