[Feature][T108][task-view-1791][patch from Question #137798] Delay AB system sync for 15 minutes before, exit AB system sync when FOTA upgrade is coming.

Only Configure: No
Affected branch: GSW_V1453
Affected module: ota
Self-test: yes
Doc Update: no

Change-Id: I0b60e877ab2767d63d3edc16da6830f1b81ca8de
diff --git a/marvell/services/ota/otad.c b/marvell/services/ota/otad.c
index a214c81..bb0e9f4 100755
--- a/marvell/services/ota/otad.c
+++ b/marvell/services/ota/otad.c
@@ -27,6 +27,13 @@
 #include "otad.h"

 

 #ifdef CONFIG_AB_SYSTEM

+#define AB_SYNC_DEFERED	900	/* defer ab sync xx seconds */

+#if AB_SYNC_DEFERED

+#define AB_STATE_SYNC		(1 << 0)

+#define AB_STATE_DOWNLOAD	(1 << 1)

+static volatile unsigned int g_ab_state = 0;

+#endif

+

 #define MEMLOCKPRIV    _IO('M', 25)

 #define MEMUNLOCKPRIV  _IO('M', 26)

 

@@ -775,11 +782,33 @@
 		memset(sbuf, 0, erasesize);

 		memset(dbuf, 0, erasesize);

 

+#if AB_SYNC_DEFERED

+		if (g_ab_state & AB_STATE_DOWNLOAD) {

+			ret = -2;

+			goto err;

+		}

+#endif

+

 		if (complete_read(fdSrc, sbuf, n) < 0)

 			goto err;

+

+#if AB_SYNC_DEFERED

+		if (g_ab_state & AB_STATE_DOWNLOAD) {

+			ret = -2;

+			goto err;

+		}

+#endif

+

 		if (complete_read(fdDst, dbuf, n) < 0)

 			goto err;

 

+#if AB_SYNC_DEFERED

+		if (g_ab_state & AB_STATE_DOWNLOAD) {

+			ret = -2;

+			goto err;

+		}

+#endif

+

 		if (memcmp(sbuf, dbuf, n) != 0) {

 			OTA_ERR("Src: %s, Dst: %s, are not same, NEED to sync\n", sdev, ddev);

 			goto err;

@@ -803,12 +832,18 @@
 

 static int __system_sync(char *src, char *dst, int size, int erasesize)

 {

-	if (__system_compare(src, dst, size, erasesize) == 0)

-		return 0;

-

 	int ret = -1;

 	char *buf = NULL;

 	char sdev[32] = {0}, ddev[32] = {0};

+

+	ret = __system_compare(src, dst, size, erasesize);

+	if (ret == 0) return ret;

+

+#if AB_SYNC_DEFERED

+	/* abort sync */

+	if (ret == -2) return ret;

+#endif

+

 	sprintf(sdev, "/dev/%s", src);

 	sprintf(ddev, "/dev/%s", dst);

 	int fdSrc = open(sdev, O_RDWR | O_SYNC);

@@ -842,8 +877,23 @@
 			n = size;

 		memset(buf, 0, erasesize);

 

+#if AB_SYNC_DEFERED

+		if (g_ab_state & AB_STATE_DOWNLOAD) {

+			ret = -2;

+			goto err;

+		}

+#endif

+

 		if (complete_read(fdSrc, buf, n) < 0)

 			goto err;

+

+#if AB_SYNC_DEFERED

+		if (g_ab_state & AB_STATE_DOWNLOAD) {

+			ret = -2;

+			goto err;

+		}

+#endif

+

 #ifndef CONFIG_PARTITION_EMMC

 		ioctl(fdDst, MEMUNLOCK, &mtdEraseInfo);

 		ioctl(fdDst, MEMERASE, &mtdEraseInfo);

@@ -880,7 +930,7 @@
 

 static int system_sync()

 {

-	int changed = 0;

+	int changed = 0, rc;

 

 	OTA_DEBUG("Start to sync system %c to %c...\n", gAsrFlag.temp_active_slot, gInActiveSystem);

 	for (int i = 0; i < server_cfg.mtd_cnt; i++) {

@@ -894,12 +944,19 @@
 						if (strncmp(pSrc->name, pDst->name, len - 3) == 0) {

 							/* remove the suffix "-a\n" or "-b\n", 3 bytes */

 							OTA_DEBUG("Start to sync %s to %s...\n", pSrc->name, pDst->name);

-							if (__system_sync(pSrc->dev, pDst->dev, pDst->size, pDst->erasesize) < 0) {

-								OTA_ERR("Fatal error: sync failed...\n");

-								mbtk_mtd = -1;

-								set_mtd_check_type(mbtk_mtd);

-								return -1;

-							}

+								rc = __system_sync(pSrc->dev, pDst->dev, pDst->size, pDst->erasesize);

+								if (rc < 0) {

+									#if AB_SYNC_DEFERED

+										if (rc == -2) {

+											OTA_ERR("%s: abort by download request.\n", __func__);

+										}

+										mbtk_mtd = -1;

+										set_mtd_check_type(mbtk_mtd);

+										else

+									#endif

+										OTA_ERR("Fatal error: sync failed...\n");

+									return rc;

+								}

 							OTA_DEBUG("Sync %s to %s OK...\n", pSrc->name, pDst->name);

 							break;

 						}

@@ -950,6 +1007,41 @@
 	return write_asr_flag(&gAsrFlag);

 }

 

+#if AB_SYNC_DEFERED

+static void system_sync_cb(union sigval sv)

+{

+	OTA_DEBUG("%s: enter.\n", __func__);

+	g_ab_state |= AB_STATE_SYNC;

+

+	if (g_ab_state & AB_STATE_DOWNLOAD) {

+		OTA_DEBUG("%s: ab download is in progress, quit sync\n", __func__);

+		g_ab_state &= ~AB_STATE_SYNC;

+		return;

+	}

+

+	system_sync();

+	g_ab_state &= ~AB_STATE_SYNC;

+

+	OTA_DEBUG("%s: exit.\n", __func__);

+}

+

+static void system_sync_defered(void)

+{

+	timer_t timerid;

+	struct sigevent sev = {

+        .sigev_notify = SIGEV_THREAD,

+        .sigev_notify_function = system_sync_cb

+    };

+

+	OTA_DEBUG("%s: sync after %ds.\n", __func__, AB_SYNC_DEFERED);

+

+    timer_create(CLOCK_MONOTONIC, &sev, &timerid);

+

+    struct itimerspec its = { .it_value = { .tv_sec = AB_SYNC_DEFERED } };

+    timer_settime(timerid, 0, &its, NULL);

+}

+#endif

+

 #endif

 

 

@@ -3085,8 +3177,26 @@
     int ret = -1;

     int sync = 0; /* default is no sync */

 	int type=-1;

-	blob_buf_init(&b, 0);

 	

+	blob_buf_init(&b, 0);

+#if AB_SYNC_DEFERED

+	int timeout = 30;

+	g_ab_state |= AB_STATE_DOWNLOAD;

+

+	while (g_ab_state & AB_STATE_SYNC) {

+		OTA_DEBUG("%s: ab sync is in progress, wait sync process abort. timeout: %d\n", __func__, timeout);

+		timeout--;

+		sleep(1);

+	}

+

+	if (timeout <= 0) {

+		OTA_ERR("%s: wait ab sync abort timeout! quit download.\n", __func__);

+		blobmsg_add_string(&b, "response", "download failed (wait ab sync abort timeout)");

+		ret = -1;

+		goto err;

+	}

+#endif

+

 	blobmsg_parse(download_policy, ARRAY_SIZE(download_policy), tb, blob_data(msg), blob_len(msg));

 	if (tb[DOWNLOAD_TYPE]) type = blobmsg_get_u32(tb[DOWNLOAD_TYPE]);

 

@@ -3139,6 +3249,9 @@
 		ret = -1;

 	}

 

+#if AB_SYNC_DEFERED

+err:

+#endif

     ubus_send_reply(ctx, req, b.head);

 	return ret;

 }

@@ -3787,8 +3900,13 @@
 		return -1;

 	}

 	OTA_DEBUG("Inactive System is %c...\n", gInActiveSystem);

+

+#if AB_SYNC_DEFERED

+	system_sync_defered();

+#else

 	system_sync();

 #endif

+#endif

 	return 0;

 }