Skip to content

Commit cb7c57c

Browse files
RDK-55619: Add Timer Mechanism in MaintenanceManager
Signed-off-by: Gomathi Shankar <[email protected]>
1 parent f4b03e6 commit cb7c57c

File tree

2 files changed

+142
-18
lines changed

2 files changed

+142
-18
lines changed

MaintenanceManager/MaintenanceManager.cpp

+137-18
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ namespace WPEFramework {
415415
if (!m_abort_flag)
416416
{
417417
LOGINFO("Starting Script (SM) : %s \n", cmd.c_str());
418+
currentScript = cmd;
418419
if (retry_count == TASK_RETRY_COUNT){
419420
LOGINFO("Starting Timer for %s \n", cmd.c_str());
420421
startTimer();
@@ -559,8 +560,83 @@ namespace WPEFramework {
559560
return thunder_client;
560561
}
561562

562-
// TBD: Check Implementation & add proper handling
563+
bool MaintenanceManager::checkTimerExists() {
564+
struct itimerspec current;
565+
int result = timer_gettime(timerid, &current);
566+
if (result == 0)
567+
{
568+
return true; /* Timer Exist */
569+
}
570+
else if (errno == EINVAL)
571+
{
572+
LOGERR("[ERROR] Timer does not exist (EINVAL)");
573+
}
574+
else
575+
{
576+
perror("[ERROR] timer_gettime");
577+
}
578+
return false;
579+
}
580+
581+
bool MaintenanceManager::isTimerRunning()
582+
{
583+
struct itimerspec current;
584+
if (timer_gettime(timerid, &current) == 0)
585+
{
586+
return (current.it_value.tv_sec != 0 || current.it_value.tv_nsec != 0); // Timer is running if non-zero time
587+
}
588+
else
589+
{
590+
perror("[ERROR] timer_gettime");
591+
return false; // Timer_gettime failed
592+
}
593+
}
594+
595+
bool MaintenanceManager::createTimer()
596+
{
597+
if (checkTimerExists())
598+
{
599+
LOGINFO("Timer already exists.");
600+
if (isTimerRunning())
601+
{
602+
LOGINFO("Timer is already running. No need to create a new timer.");
603+
return false; // Timer exists and is running
604+
}
605+
else
606+
{
607+
LOGINFO("Timer exists but is not running. Proceeding to create timer.");
608+
}
609+
}
610+
611+
struct sigevent sev = {};
612+
sev.sigev_notify = SIGEV_SIGNAL;
613+
sev.sigev_signo = SIGALRM;
614+
sev.sigev_value.sival_ptr = &timerid;
615+
616+
if (timer_create(BASE_CLOCK, &sev, &timerid) == -1)
617+
{
618+
LOGERR("[Error] Failed to create timer");
619+
return false; // Timer creation failed
620+
}
621+
622+
LOGINFO("Timer created successfully.");
623+
return true; // Timer created successfully
624+
}
625+
563626
void MaintenanceManager::startTimer(){
627+
628+
if (!checkTimerExists())
629+
{
630+
LOGERR("[ERROR] Timer does not exist. Unable to start timer.");
631+
return;
632+
}
633+
634+
if (isTimerRunning())
635+
{
636+
LOGINFO("Timer is already running. Not starting a new timer.");
637+
return;
638+
}
639+
564640
struct itimerspec its;
565641

566642
its.it_value.tv_sec = TASK_TIMEOUT;
@@ -578,9 +654,20 @@ namespace WPEFramework {
578654
}
579655
}
580656

581-
// TBD: Check Implementation & add proper handling
582657
void MaintenanceManager::stopTimer()
583658
{
659+
if (!checkTimerExists())
660+
{
661+
LOGERR("[ERROR] Timer does not exist. Unable to stop timer.");
662+
return;
663+
}
664+
665+
if (!isTimerRunning())
666+
{
667+
LOGINFO("Timer is not running. No need to stop.");
668+
return;
669+
}
670+
584671
struct itimerspec its = {};
585672
its.it_value.tv_sec = 0;
586673
its.it_value.tv_nsec = 0;
@@ -595,6 +682,49 @@ namespace WPEFramework {
595682
}
596683
}
597684

685+
void MaintenanceManager::deleteTimer()
686+
{
687+
if (!checkTimerExists())
688+
{
689+
LOGERR("[ERROR] Timer does not exist. Unable to delete timer.");
690+
return;
691+
}
692+
693+
if (isTimerRunning())
694+
{
695+
LOGINFO("Timer is currently running. Attempting to stop it before deletion.");
696+
stopTimer();
697+
if (!isTimerRunning())
698+
{
699+
// Timer was successfully stopped
700+
if (timer_delete(timerid) == -1)
701+
{
702+
LOGERR("[ERROR] Failed to delete timer after stopping it.");
703+
}
704+
else
705+
{
706+
LOGINFO("Timer successfully deleted after stopping.");
707+
}
708+
}
709+
else
710+
{
711+
LOGERR("[ERROR] Failed to stop the timer, hence could not delete it.");
712+
}
713+
}
714+
else
715+
{
716+
// Timer is not running, delete directly
717+
if (timer_delete(timerid) == -1)
718+
{
719+
LOGERR("[ERROR] Failed to delete timer.");
720+
}
721+
else
722+
{
723+
LOGINFO("Timer successfully deleted.");
724+
}
725+
}
726+
}
727+
598728
// TBD: Check Implementation & add proper handling
599729
void MaintenanceManager::timer_handler(int signo)
600730
{
@@ -1056,32 +1186,19 @@ namespace WPEFramework {
10561186
return string("[ERROR] Failed to register signal handler");
10571187
}
10581188

1059-
// Create Timer
1060-
struct sigevent sev = {};
1061-
sev.sigev_notify = SIGEV_SIGNAL;
1062-
sev.sigev_signo = SIGALRM;
1063-
sev.sigev_value.sival_ptr = &timerid;
1064-
1065-
if (timer_create(BASE_CLOCK, &sev, &timerid) == -1)
1189+
if (!createTimer())
10661190
{
1067-
LOGERR("[Error] Failed to create timer");
10681191
return string("[Error] Failed to create timer");
10691192
}
1193+
10701194
/* On Success; return empty to indicate no error text. */
10711195
return (string());
10721196
}
10731197

10741198
// TBD: Check Implementation & add proper handling
10751199
void MaintenanceManager::Deinitialize(PluginHost::IShell* service)
10761200
{
1077-
if (timer_delete(timerid) == -1)
1078-
{
1079-
LOGERR("Failed to delete timer");
1080-
}
1081-
else
1082-
{
1083-
LOGINFO("Timer deleted.");
1084-
}
1201+
deleteTimer();
10851202
#if defined(USE_IARMBUS) || defined(USE_IARM_BUS)
10861203
stopMaintenanceTasks();
10871204
DeinitializeIARM();
@@ -1788,11 +1905,13 @@ namespace WPEFramework {
17881905
}
17891906
}
17901907
// TBD: Check if timer exist & running -> if so stop and delete the timer
1908+
deleteTimer();
17911909
result=true;
17921910
}
17931911
else {
17941912
LOGERR("Failed to stopMaintenance without starting maintenance");
17951913
// TBD: check if timer exist -> if so delete timer
1914+
deleteTimer();
17961915
}
17971916
task_thread.notify_one();
17981917

MaintenanceManager/MaintenanceManager.h

+5
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,13 @@ namespace WPEFramework {
215215
static timer_t timerid;
216216
static string currentScript;
217217
static bool scriptCompleted;
218+
219+
bool checkTimerExists();
220+
bool isTimerRunning();
221+
bool createTimer();
218222
void startTimer();
219223
void stopTimer();
224+
void deleteTimer();
220225

221226
BEGIN_INTERFACE_MAP(MaintenanceManager)
222227
INTERFACE_ENTRY(PluginHost::IPlugin)

0 commit comments

Comments
 (0)