@@ -415,6 +415,7 @@ namespace WPEFramework {
415
415
if (!m_abort_flag)
416
416
{
417
417
LOGINFO (" Starting Script (SM) : %s \n " , cmd.c_str ());
418
+ currentScript = cmd;
418
419
if (retry_count == TASK_RETRY_COUNT){
419
420
LOGINFO (" Starting Timer for %s \n " , cmd.c_str ());
420
421
startTimer ();
@@ -559,8 +560,83 @@ namespace WPEFramework {
559
560
return thunder_client;
560
561
}
561
562
562
- // TBD: Check Implementation & add proper handling
563
+ bool MaintenanceManager::checkTimerExists () {
564
+ struct itimerspec current;
565
+ int result = timer_gettime (timerid, ¤t);
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, ¤t) == 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
+
563
626
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
+
564
640
struct itimerspec its;
565
641
566
642
its.it_value .tv_sec = TASK_TIMEOUT;
@@ -578,9 +654,20 @@ namespace WPEFramework {
578
654
}
579
655
}
580
656
581
- // TBD: Check Implementation & add proper handling
582
657
void MaintenanceManager::stopTimer ()
583
658
{
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
+
584
671
struct itimerspec its = {};
585
672
its.it_value .tv_sec = 0 ;
586
673
its.it_value .tv_nsec = 0 ;
@@ -595,6 +682,49 @@ namespace WPEFramework {
595
682
}
596
683
}
597
684
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
+
598
728
// TBD: Check Implementation & add proper handling
599
729
void MaintenanceManager::timer_handler (int signo)
600
730
{
@@ -1056,32 +1186,19 @@ namespace WPEFramework {
1056
1186
return string (" [ERROR] Failed to register signal handler" );
1057
1187
}
1058
1188
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 ())
1066
1190
{
1067
- LOGERR (" [Error] Failed to create timer" );
1068
1191
return string (" [Error] Failed to create timer" );
1069
1192
}
1193
+
1070
1194
/* On Success; return empty to indicate no error text. */
1071
1195
return (string ());
1072
1196
}
1073
1197
1074
1198
// TBD: Check Implementation & add proper handling
1075
1199
void MaintenanceManager::Deinitialize (PluginHost::IShell* service)
1076
1200
{
1077
- if (timer_delete (timerid) == -1 )
1078
- {
1079
- LOGERR (" Failed to delete timer" );
1080
- }
1081
- else
1082
- {
1083
- LOGINFO (" Timer deleted." );
1084
- }
1201
+ deleteTimer ();
1085
1202
#if defined(USE_IARMBUS) || defined(USE_IARM_BUS)
1086
1203
stopMaintenanceTasks ();
1087
1204
DeinitializeIARM ();
@@ -1788,11 +1905,13 @@ namespace WPEFramework {
1788
1905
}
1789
1906
}
1790
1907
// TBD: Check if timer exist & running -> if so stop and delete the timer
1908
+ deleteTimer ();
1791
1909
result=true ;
1792
1910
}
1793
1911
else {
1794
1912
LOGERR (" Failed to stopMaintenance without starting maintenance" );
1795
1913
// TBD: check if timer exist -> if so delete timer
1914
+ deleteTimer ();
1796
1915
}
1797
1916
task_thread.notify_one ();
1798
1917
0 commit comments