Merge "[Feature][ZXW-204][ZXW-205] add query fota upgrade process api and backup Status"
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-fota-backup/files/lynq-fota-backup.cpp b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-fota-backup/files/lynq-fota-backup.cpp
index 84a727b..d91390c 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-fota-backup/files/lynq-fota-backup.cpp
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-fota-backup/files/lynq-fota-backup.cpp
@@ -27,6 +27,7 @@
 extern "C" {

 #endif

 

+#define FOTA_REBOOT_FLAG "/mnt/userdata/.fota_reboot_flag"

 #define FOTA_FLAG_FILE "/mnt/userdata/.back_up_flag"

 #define FOTA_SYNC_FLAG    1

 

@@ -34,6 +35,24 @@
 extern int lynq_fota_get_addr_value(char *tmp_value);

 extern int lynq_fota_set_addr_value(char        *value,int size);

 extern int lynq_fota_nrestart(void);

+#define REBOOT_DONE  1

+

+void set_upgrade_reboot_flag(void)

+{

+    FILE *fp = NULL;

+    int reboot_flag = REBOOT_DONE;

+    fp = fopen(FOTA_REBOOT_FLAG,"w+");

+    if(fp == NULL)

+    {

+        printf("Open reboot flag file failed\n");

+        return;

+    }

+    

+    fwrite(&reboot_flag,sizeof(int),1,fp);

+    fclose(fp);

+    system("sync");

+    return ;

+}

 int main()

 {

     int ret = 0 ;

@@ -48,8 +67,11 @@
         return -1;

 

     }

-    

+   

     fread(&sync_flag,sizeof(int),1,fp);

+    fclose(fp);

+    

+    set_upgrade_reboot_flag();

     

     if(sync_flag == FOTA_SYNC_FLAG)

     {

@@ -57,7 +79,6 @@
         if(ret != 0)

         {

             printf("sync faild\n");

-            fclose(fp);

         }

        system("rm -rf /mnt/userdata/.back_up_flag");

         

@@ -76,7 +97,6 @@
         {

             printf("Set addr failed\n");

             return -1;

-

         }

         ret = lynq_fota_nrestart();

         if(ret != 0)

diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/files/lynq-qser-fota-demo.cpp b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/files/lynq-qser-fota-demo.cpp
index 63c9504..de637ec 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/files/lynq-qser-fota-demo.cpp
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/files/lynq-qser-fota-demo.cpp
@@ -5,14 +5,30 @@
 #include <unistd.h>

 #include <dlfcn.h>

 #include <stdint.h>

+#include <pthread.h>

 

 int (*lynq_get_upgrade_status)(void);

 int (*lynq_fota_set_addr_value)(char *value,int size);

 int (*lynq_fota_nrestart)(void);

 int (*lynq_rock_main)(int first_run);

-

+int (*lynq_read_process)(void);

 void *dlHandle_fota = NULL;

 

+void *thread_function_noreboot(void *arg) 

+{

+

+        lynq_fota_nrestart();

+        return NULL;

+}

+

+void *thread_function_reboot(void *arg) 

+{

+

+        lynq_rock_main(1);

+        return NULL;

+}

+

+

 int main(int argc,char *argv[])

 {

     int ret = 0;

@@ -53,6 +69,13 @@
         printf("lynq_rock_main is null\n");

         return -1;

     }

+

+    lynq_read_process = (int (*)(void))dlsym(dlHandle_fota,"lynq_read_process");

+    if(lynq_read_process == NULL)

+    {

+        printf("lynq_read_process is null\n");

+        return -1;

+    }

     

     ret = lynq_fota_set_addr_value(value,(int )strlen(value));

     if(ret != 0)

@@ -60,34 +83,53 @@
         printf("set upgrade package addr failed\n");

         return -1;

     }

-

-    printf("Please chose  reboot or not when upgrade done  1: reboot 0: not reboot\n");

-    scanf("%d",&reboot_flag);

-

-    switch(reboot_flag)

+    while(1)

     {

-        case 0:

-            printf("Not reboot when upgrade done!!!\n ");

-            ret = lynq_fota_nrestart();

-            printf("upgrade result is %d\n",ret);

-            ret = lynq_get_upgrade_status();

-            printf("get upgrade status result is %d\n",ret);

-            break;

+        printf("Please chose action  0:  upgrade done ,not reboot 1: upgrade done ,reboot 2:get upgrade status 3:read fota process \n");

+        scanf("%d",&reboot_flag);

 

-        case 1:

-            printf("Will reboot when upgrade done!!!\n ");

-            ret = lynq_rock_main(1);

-            printf("upgrade result is %d\n",ret);

-            ret = lynq_get_upgrade_status();

-            printf("get upgrade status result is %d\n",ret);

-            break;

+        switch(reboot_flag)

+        {

+            case 0:

+                {

+                    pthread_t thread_id_noreboot;

+                    int result = pthread_create(&thread_id_noreboot, NULL, thread_function_noreboot, NULL);

+                    if (result != 0) 

+                    {

+                        printf("pthread_create failed \n");

+                        return -1;;

+                    }

+                }

+                break;

 

-        default:

-            printf("please input right flag 1 or 0\n");

-            break;

+            case 1:

+                {

+                    pthread_t thread_id_reboot;

+                    int result = pthread_create(&thread_id_reboot, NULL, thread_function_reboot, NULL);

+                    if (result != 0) 

+                    {

+                        printf("pthread_create failed \n");

+                        return -1;;

+                    }

+                }

+                break;

+            case 2:

+                printf("Get fota upgrade status \n");

+                ret = lynq_get_upgrade_status();

+                printf("lynq_get_upgrade_status ret is %d\n",ret);

+                break;

+            case 3:

+                printf("get fota upgrade process\n");

+                ret = lynq_read_process();

+                printf("Now upgrade process is %d\n",ret);

+                break;

+            default:

+                printf("please input right flag 0 or 1 or 2 or 3\n");

+                break;

 

+        }

+         

     }

-    

     return 0;

 

 }

diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/lynq-qser-fota-demo.bb b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/lynq-qser-fota-demo.bb
index d24b8bb..1ce3f8c 100644
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/lynq-qser-fota-demo.bb
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-qser-fota-demo/lynq-qser-fota-demo.bb
@@ -16,7 +16,7 @@
 
 LOCAL_C_INCLUDES = "-I."
 
-LOCAL_LIBS = "-L. -ldl -lstdc++"
+LOCAL_LIBS = "-L. -ldl -lstdc++ -lpthread"
 
 #INHIBIT_PACKAGE_STRIP = "1"
 S = "${WORKDIR}"
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/include/lynq-qser-fota.h b/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/include/lynq-qser-fota.h
index 34be9aa..2a8cfd6 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/include/lynq-qser-fota.h
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/include/lynq-qser-fota.h
@@ -18,6 +18,7 @@
 int lynq_fota_set_addr_value(char        *value,int size);

 int lynq_fota_nrestart(void);

 int lynq_rock_main(int first_run);

+int lynq_read_process(void);

 

 #ifdef __cplusplus

 }

diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/lynq-qser-fota.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/lynq-qser-fota.cpp
index 53ee3c8..5dab391 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/lynq-qser-fota.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-fota/lynq-qser-fota.cpp
@@ -28,12 +28,13 @@
 #include "include/lynq-qser-fota.h"

 #include "liblog/lynq_deflog.h"

 

-#define FOTA_FLAG_FILE "/mnt/userdata/.back_up_flag"

 

+#define FOTA_REBOOT_FLAG "/mnt/userdata/.fota_reboot_flag"

+#define FOTA_FLAG_FILE "/mnt/userdata/.back_up_flag"

 #define USER_LOG_TAG    "LYNQ_FOTA"

 #define FOTA_ADDR_FILE  "/mnt/userdata/.addr_value"

 #define FOTA_FILE_NAME  "upgrade.package"

-

+#define FOTA_UPGRADE_PROCESS  "/mnt/userdata/.fota_upgrade_process"

 #define  SYSTEM_A  "34650"

 #define  SYSTEM_B  "39019"

 

@@ -49,6 +50,18 @@
 #define    LYNQ_UPGRADE_STATUS_UPDATE_SUCCESS      (4)

 #define    LYNQ_UPGRADE_STATUS_UPDATE_FAIL         (5)

 

+#define    LYNQ_SYNNCHRONIZING  8

+#define    LYNQ_SYNC_SUCCESS    9

+#define    LYNQ_SYNC_FAILED    -1

+

+#define    ZXIC_SYNCHRONIZING    1

+#define    ZXIC_SYNC_SUCCESS      0

+

+#define    NO_REBOOT    0

+

+

+int total_size = 0;

+int upgrade_size = 0;

 

 typedef struct

 {

@@ -122,6 +135,7 @@
     int ret = 0;

     int current_slot = 0;

     int fota_sync_flag = 0;

+    int reboot_flg = NO_REBOOT;

     FILE *fp = NULL;

     fp = fopen(FOTA_FLAG_FILE,"w+");

     if(fp == NULL)

@@ -134,6 +148,17 @@
 

     /* T106BUG-189 fix */

     system("sync");

+

+    fp =fopen(FOTA_REBOOT_FLAG,"w+");

+    if(fp == NULL)

+    {

+        LYERRLOG("Creaf get upgrade status flag failed");

+        return -1;

+    }

+

+    fwrite(&reboot_flg,sizeof(int),1,fp);

+    fclose(fp);

+    system("sync");

     

     ret = lynq_fota_verify();

     if(ret != 0)

@@ -233,7 +258,20 @@
     LYINFLOG("Total size:%d ", p_status->total_size);

 

     LYINFLOG("Updated size:%d ", p_status->upgraded_size);

-    return ;

+

+    total_size = p_status->total_size;

+    upgrade_size = p_status->upgraded_size;

+

+    FILE *fp = NULL;

+    fp = fopen(FOTA_UPGRADE_PROCESS,"w");

+    if (fp == NULL) 

+    {

+        LYERRLOG("Error opening file");

+        return;

+    }

+    

+    fprintf(fp, "%d,%d\n", total_size, upgrade_size);

+    fclose(fp);

 }

 

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

@@ -528,21 +566,59 @@
 {

     z_upgrade_status_info_t status;

     int ret = 0;

-

-    

-    ret = zxic_dual_get_upgrade_status(&status);

-    if(ret  < 0)

+    int reboot_flag = -1;

+    FILE *fp = NULL;

+    fp = fopen(FOTA_REBOOT_FLAG,"r");

+    if(fp == NULL)

     {

-        LYINFLOG("Get upgrade status fail! ");

+        LYERRLOG("Open reboot flag file failed");

         return -1;

     }

+    

+    fread(&reboot_flag,sizeof(int),1,fp);

+    fclose(fp);

+    

+    //get upgrade status before no reboot

+    if(reboot_flag == NO_REBOOT)

+    {

+    

+        ret = zxic_dual_get_upgrade_status(&status);

+        if(ret  < 0)

+        {

+            LYERRLOG("Get upgrade status fail! ");

+            return -1;

+        }

      

-    LYINFLOG("Current upgrade info: ");

-    LYINFLOG("Current upgrade status:%d ", status.upgrade_status);

-    LYINFLOG("Current upgrade total size:%d ", status.total_size);

-    LYINFLOG("Current upgrade updated size:%d ", status.upgraded_size);

-

-    return status.upgrade_status;

+        LYINFLOG("Current upgrade info: ");

+        LYINFLOG("Current upgrade status:%d ", status.upgrade_status);

+        LYINFLOG("Current upgrade total size:%d ", status.total_size);

+        LYINFLOG("Current upgrade updated size:%d ", status.upgraded_size);

+                           

+        return status.upgrade_status;

+    }

+    else

+    {

+        //get sync status 

+        int fota_sync_tatus = 0;

+        fota_sync_tatus = lynq_get_sync_status();

+        if(fota_sync_tatus == ZXIC_SYNCHRONIZING)

+        {

+            LYINFLOG("Now fota upgrade sync status is synchronizing");

+            return LYNQ_SYNNCHRONIZING;

+        }

+        else if(fota_sync_tatus == ZXIC_SYNC_SUCCESS)

+        {

+            LYINFLOG("Now fota upgrade sync status sync success ");

+            return LYNQ_SYNC_SUCCESS;

+        }        

+        else if(fota_sync_tatus == LYNQ_SYNC_FAILED)

+        {

+            LYERRLOG("Now fota upgrade sync status sync failed ");

+            return LYNQ_SYNC_FAILED;

+        }

+        

+        

+    }

 

 }

 

@@ -753,7 +829,7 @@
     int sync_status = -2;

     zxic_dual_get_sync_status(&sync_status);

     LYINFLOG("Current sync status is %d", sync_status);

-    return 0;

+    return sync_status;

 }

 

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

@@ -785,6 +861,67 @@
     return 0;

 }

 

+/*****************************************

+* @brief:lynq_read_process

+* @param count [IN]:NS

+* @param sum [OUT]:NA

+* @return :fota upgrade process 0-10

+* @todo:NA

+* @see:NA

+* @warning:NA

+*****************************************/

+int lynq_read_process(void)

+{

+    LYINFLOG("Enter lynq_read_process");

+    

+    float fota_process = 0;

+    int ration = 0;

+    int read_count = 0;

+    FILE *fp = NULL;

+    

+    while(1)

+    {

+        

+        fp = fopen(FOTA_UPGRADE_PROCESS, "r");

+        if(fp == NULL)

+        {

+            LYERRLOG("lynq_read_process open file failed");

+            usleep(10000);

+            read_count++;

+            if(read_count > 5)

+            {

+                break;

+            }

+        }

+        else

+        {

+            break;

+        }

+    }

+    

+    if(fp != NULL) 

+    {

+        

+        char line[256] = {0};

+        if (fgets(line, sizeof(line), fp) != NULL) 

+        {

+            sscanf(line, "%d,%d", &total_size, &upgrade_size);

+        }

+        fclose(fp);

+    }

+    

+    

+    if(total_size != 0 && upgrade_size <= total_size)

+    {

+        LYINFLOG("Caculate fota process ration ");

+        fota_process =  (float) upgrade_size / total_size;

+     }

+     

+     ration = (int)(fota_process * 10);

+     LYINFLOG("Fota process ration is %d",ration);

+     return ration;

+}

+

 DEFINE_LYNQ_LIB_LOG(LYNQ_FOTA)

 

 #ifdef __cplusplus