[bugfix][T106][bug-view-2360] Solve the problem that sync operations may affect upgrades

Only Configure:No
Affected branch:master
Affected module:fota
Self-test:Yes

Change-Id: Ia513c5f32c8659c044fd4174b1eb604d93013d02
diff --git a/cap/zx297520v3/src/lynq/framework/lynq-fota-backup/lynq-fota-backup.cpp b/cap/zx297520v3/src/lynq/framework/lynq-fota-backup/lynq-fota-backup.cpp
index c2e7d1d..887d036 100755
--- a/cap/zx297520v3/src/lynq/framework/lynq-fota-backup/lynq-fota-backup.cpp
+++ b/cap/zx297520v3/src/lynq/framework/lynq-fota-backup/lynq-fota-backup.cpp
@@ -34,6 +34,8 @@
 

 #define USER_LOG_TAG "LYNQ_FOTA_BACKUP"

 

+#define SYNC_RUNNING_FLAG "/tmp/sync_running.flag"

+#define UPDATE_ACTIVE_FLAG "/tmp/update_active.flag"

 extern int lynq_sync_system();

 extern int lynq_fota_get_addr_value(char *tmp_value);

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

@@ -192,6 +194,34 @@
 

 }

 

+static int set_sync_status(int active)

+{

+    if (active) 

+    {

+        FILE *fp = fopen(SYNC_RUNNING_FLAG, "wx");

+        if (!fp) 

+	{

+            LYERRLOG("Open sync_running_flag file failed.\n");

+            return -1;

+        }

+        fprintf(fp, "Sync started\n");

+        fclose(fp);

+        LYERRLOG("Sync: Status flag created: %s\n", SYNC_RUNNING_FLAG);

+    } 

+    else 

+    {

+        if (unlink(SYNC_RUNNING_FLAG) == -1) 

+	{

+                LYERRLOG("Sync: Failed to remove running flag");

+                return -1;

+        }

+        LYERRLOG("Sync: Status flag removed: %s\n", SYNC_RUNNING_FLAG);

+    }

+    system("sync");

+    return 0;

+}

+

+

 int main()

 {

     int ret = 0 ;

@@ -199,12 +229,25 @@
     char tmp_addr[128] = {0};

     FILE *fp = NULL;

 

+    if (access(UPDATE_ACTIVE_FLAG, F_OK) == 0) 

+    {

+        LYERRLOG("No need fota sync: Upgrade is active.\n");

+        return -1;

+    }

+

+    ret = set_sync_status(1);

+    if(ret != 0)

+    {

+	LYERRLOG("set sync status:1 error\n");

+        return -1;

+    }

     check_need_sync();

 

     fp = fopen(FOTA_FLAG_FILE,"r");

     if(fp == NULL)

     {

         LYERRLOG("No need fota sync\n");

+    	set_sync_status(0);

         return -1;

 

     }

@@ -230,6 +273,7 @@
         if(ret != 0)

         {

             LYERRLOG("Get addr failed\n");

+    	    set_sync_status(0);

             return -1;

         }

         LYINFLOG("tmp_addr is %s\n",tmp_addr);

@@ -237,18 +281,21 @@
         if(ret != 0)

         {

             LYERRLOG("Set addr failed\n");

+    	    set_sync_status(0);

             return -1;

         }

         ret = lynq_fota_nrestart();

         if(ret != 0)

         {

             LYERRLOG("Upgrade failed\n");

+    	    set_sync_status(0);

             return -1;

         }

         

     }

-   

-    return 0;

+

+    set_sync_status(0);

+    return ret;

 

 }

 DEFINE_LYNQ_LIB_LOG(LYNQ_FOTA_BACKUP)

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 811e7e7..20816fb 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
@@ -39,6 +39,8 @@
 #define  SYSTEM_A  "34650"

 #define  SYSTEM_B  "39019"

 

+#define SYNC_RUNNING_FLAG "/tmp/sync_running.flag"

+#define UPDATE_ACTIVE_FLAG "/tmp/update_active.flag"

 

 #define BOOTABLE   "45227"

 #define UNBOOTABLE "47806"

@@ -125,11 +127,36 @@
 int lynq_set_sync_status(char *option_para);

 int  calculate_md5_sum( char *md5_sum,int md5_size);

 

+

+static int can_run_update() 

+{

+    if (access(SYNC_RUNNING_FLAG, F_OK) == 0) 

+    {

+        LYINFLOG("Update: Status=-2 (Sync is running, please try later)\n");

+        return -2;

+    }

+

+    return 0;

+}

+

+static int set_update_flag()

+{

+    FILE *fp = fopen(UPDATE_ACTIVE_FLAG, "w");

+    if (!fp) 

+    {

+        LYINFLOG("open update_active_flag file failed \n");

+        return -1;

+    }

+    fprintf(fp, "OTA update active");

+    fclose(fp);

+    system("sync");

+    return 0;

+}

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

 * @brief:rock_update_main

 * @param count [IN]:NA

 * @param sum [OUT]:NA

-* @return :success 0, failed -1

+* @return :success 0, failed -1, busy -2

 * @todo:NA

 * @see:NA

 * @warning:NA

@@ -146,8 +173,16 @@
     struct tm *localTime;

     double elapsed = 0;

     char md5_sum[48] = {0};

-

-    FILE *fp = NULL;

+    FILE *fp = NULL; 

+    

+    if(set_update_flag() != 0)

+    {

+    	return -1;

+    }

+    if(can_run_update() != 0)

+    {

+	return -2;

+    }

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

     if(fp == NULL)

     {

@@ -474,7 +509,7 @@
 * @brief:lynq_fota_nrestart no reboot

 * @param count [IN]:NA

 * @param sum [OUT]:NA

-* @return :success 0, failed -1

+* @return :success 0, failed -1, busy -2

 * @todo:NA

 * @see:NA

 * @warning:NA

@@ -515,7 +550,7 @@
 * @brief:lynq_rock_main,upgrade done reboot

 * @param count [IN]:int first_run

 * @param sum [OUT]:NA

-* @return :success 0, failed -1

+* @return :success 0, failed -1, busy -2

 * @todo:NA

 * @see:NA

 * @warning:NA