[Bugfix][T106BUG-181] Resolve the issue of wakealaem waking up early

Only Configure:No
Affected branch:master
Affected module:Rtc
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update:Yes

Change-Id: Ic562d9a073be1c3670d61fef6677556eab1d4d25
diff --git a/cap/zx297520v3/src/lynq/lib/libpoweralarm/libpoweralarm.cpp b/cap/zx297520v3/src/lynq/lib/libpoweralarm/libpoweralarm.cpp
index 945e0a4..5aeb496 100755
--- a/cap/zx297520v3/src/lynq/lib/libpoweralarm/libpoweralarm.cpp
+++ b/cap/zx297520v3/src/lynq/lib/libpoweralarm/libpoweralarm.cpp
@@ -17,7 +17,8 @@
 #include "liblog/lynq_deflog.h"

 #include <include/libpoweralarm.h>

 #include <pthread.h>

-

+#include <string.h>

+#include <errno.h>

 

 #ifdef __cplusplus

 extern "C" {

@@ -25,6 +26,9 @@
 

 #define USER_LOG_TAG "LYNQ_POWERALARM"

 

+#define MIN_TIME    1

+#define MAX_TIME    268435456

+

 

 static int wk_rtc_id = 1;

 static int pw_rtc_id = 2;

@@ -36,7 +40,7 @@
 extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);

 extern int sc_rtc_timer_del(int srcid, int rtc_id);

 

-

+extern int sc_rtc_timer_add_utc(int srcid, int rtc_id, struct tm *utc_sec, int wakeup, sc_rtc_timer_exp_cb rtc_notify);

 

 /*****************************************

 * @brief:sc_rtc_timer_add_cb

@@ -66,6 +70,12 @@
 {

     unsigned long  time_sec = 0;

     int ret = 0;

+    char *buf_bak = buffer;

+    time_t tmp_time = 0;

+    struct tm *ptime = NULL;

+    struct tm *wake_arlarm = NULL;

+    tmp_time = time(NULL);

+    ptime = localtime(&tmp_time);

     

     LYLOGSET(LOG_INFO);

     LYLOGEINIT(USER_LOG_TAG);

@@ -76,16 +86,41 @@
         return -1;

     }

 

-    time_sec = atoll(buffer);

+    while(*buffer != '\0' )

+    {

+        if((*buffer < '0') || (*buffer > '9'))

+        {

+            LYERRLOG("Illeagel input buffer , there is invalid character!!!!");

+            return -1;

+        }

+        else

+        {

+            buffer++;

+        }

+    }

+   

+

+    buffer = buf_bak;

+    time_sec = strtoul(buffer,NULL,10);

+    if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)

+    {

+        LYERRLOG("Illeagle input: too large or too small !!!");

+        return -1;

+    }

+

+    tmp_time += time_sec;

+    wake_arlarm = localtime(&tmp_time);

     

     LYINFLOG("Set  wakealarm   %lu seconds ",time_sec);

+    

     wk_srcid = sc_rtc_timer_init();

     if (wk_srcid <= 0)

     {

         LYINFLOG("rtc_timer_init fail!");

         return -1;

     }

-    ret = sc_rtc_timer_add(wk_srcid, wk_rtc_id, time_sec, sc_rtc_timer_add_cb);

+    

+    ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);

     if(ret < 0)

     {

         LYINFLOG("Add  rtc timer failed!!!!");

@@ -119,29 +154,50 @@
     int srcid = 0;

     unsigned long  time_sec = 0;

     int ret = 0;

+    char *buf_bak = buffer;

     

     LYLOGSET(LOG_INFO);

     LYLOGEINIT(USER_LOG_TAG);

-    

+   

     if(buffer == NULL)

     {

-        LYINFLOG("Bad input parameter,exit!!!!");

+        LYERRLOG("Bad input parameter,exit!!!!");

         return -1;

     }

+    

+    while(*buffer != '\0' )

+    {

+        if(( *buffer < '0' )|| ( *buffer > '9'))

+        {

+            LYERRLOG("Illeagel input buffer , there is invalid character!!!!");

+            return -1;

+        }

+        else

+        {

+            buffer++;

+        }

+    }

+    

 

-    time_sec = atoll(buffer);

+    buffer = buf_bak;

+    time_sec = strtoul(buffer,NULL,10);

+    if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)

+    {

+        LYERRLOG("Illeagle input: too large or too small !!!");

+        return -1;

+    }

     

     LYINFLOG("Set poweralarm %lu seconds",time_sec);

     srcid = sc_rtc_timer_init();

     if (srcid <= 0)

     {

-        LYINFLOG("rtc_timer_init fail!");

+        LYERRLOG("rtc_timer_init fail!");

         return -1;

     }

     ret = sc_rtc_timer_add(srcid, pw_rtc_id, time_sec, sc_rtc_timer_add_cb);

     if(ret < 0)

     {

-        LYINFLOG("Add  rtc timer failed!!!!");

+        LYERRLOG("Add  rtc timer failed!!!!");

         return -1;

     }

     

@@ -174,7 +230,7 @@
     ret = sc_rtc_timer_del(wk_srcid, wk_rtc_id);

     if(ret < 0)

     {

-        LYINFLOG("Del wakealarm failed!!!");

+        LYERRLOG("Del wakealarm failed!!!");

         return -1;

     }

 

@@ -194,15 +250,24 @@
 ******************************************/

 int  lynq_set_poweralarm(unsigned long time_sec)

 {

-    LYLOGSET(LOG_INFO);

-    LYLOGEINIT(USER_LOG_TAG);

+

     int ret = 0;

     int srcid = 0;

+

+    LYLOGSET(LOG_INFO);

+    LYLOGEINIT(USER_LOG_TAG);

+    

+    if(time_sec < MIN_TIME || time_sec > MAX_TIME )

+    {

+        LYERRLOG("Illeagle input: too large or too small !!!");

+        return -1;

+    }

+    

     LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);

     srcid = sc_rtc_timer_init();

     if (srcid <= 0)

     {

-        LYINFLOG("rtc_timer_init fail!");

+        LYERRLOG("rtc_timer_init fail!");

         return -1;

     }

     ret = sc_rtc_timer_add(srcid, pw_rtc_id, time_sec, sc_rtc_timer_add_cb);

@@ -238,17 +303,35 @@
 {

     int ret = 0;

     

+    time_t tmp_time = 0;

+    struct tm *ptime = NULL;

+    struct tm *wake_arlarm = NULL;

+    tmp_time = time(NULL);

+    ptime = localtime(&tmp_time);

+    

     LYLOGSET(LOG_INFO);

     LYLOGEINIT(USER_LOG_TAG);

     

+    if(time_sec < MIN_TIME || time_sec > MAX_TIME)

+    {

+        LYERRLOG("Illeagle input: too large or too small !!!");

+        return -1;

+    }

+    

     LYINFLOG("lynq_set_wakealarm   %lu seconds ",time_sec);

+    

     wk_srcid = sc_rtc_timer_init();

     if (wk_srcid <= 0)

     {

         LYINFLOG("rtc_timer_init fail!");

         return -1;

     }

-    ret = sc_rtc_timer_add(wk_srcid, wk_rtc_id, time_sec, sc_rtc_timer_add_cb);

+    

+    tmp_time += time_sec;

+    wake_arlarm = localtime(&tmp_time);

+    LYINFLOG("Set  wakealarm   %lu seconds ",time_sec);

+    

+    ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);

     if(ret < 0)

     {

         LYINFLOG("Add  rtc timer failed!!!!");