[Bugfix][MD310/R307/R306][bug-view-1170][fota] Pressing the reset button during FOTA upgrade leads to upgrade failure, and disabling the reset button during wefota and web upgrade

Change-Id: I1bf6ffc635c8e70af2c6de4caaa1bb28be12f57d
diff --git a/lynq/R307L/ap/app/cgi/cgi.c b/lynq/R307L/ap/app/cgi/cgi.c
index 5f56ca5..a7c86f3 100755
--- a/lynq/R307L/ap/app/cgi/cgi.c
+++ b/lynq/R307L/ap/app/cgi/cgi.c
@@ -4,6 +4,7 @@
 #include <unistd.h>

 #include <fcntl.h>

 #include <errno.h>

+#include "cfg_api.h"

 

 #define BUFFER_SIZE (1024 * 20) // Define the buffer size for each read

 #define FIND_STR_LEN 128 // Define the length of the string to find

@@ -291,10 +292,12 @@
     result = fota_get_update_status(&upgradeStatus);

     if(result < 0)

     {

+        cfg_set("fota_update_flag", "0");

         print_json_response(0, "Fail to read update file");

     }

     else if(upgradeStatus != 0)

     {

+        cfg_set("fota_update_flag", "0");

         print_json_response(0, "Verify update file failed");

     }

     else

@@ -331,12 +334,13 @@
         print_json_response(0, "Invalid CONTENT_LENGTH");

         return 1;

     }

-

+    cfg_set("fota_update_flag", "1");

     // Call the file upload handling function

     if (handle_file_upload(source_filename, target_filename, content_length) != 0)

     {

         system("rm -rf /tmp/firmware_tmp_file");

         print_json_response(0, "File upload failed");

+        cfg_set("fota_update_flag", "0");

         return 1;

     }

 

diff --git a/lynq/R307L/ap/app/wefota/wefota_device.c b/lynq/R307L/ap/app/wefota/wefota_device.c
index 93caef3..fe6063c 100755
--- a/lynq/R307L/ap/app/wefota/wefota_device.c
+++ b/lynq/R307L/ap/app/wefota/wefota_device.c
@@ -90,6 +90,13 @@
     return 0;

 }

 

+int set_wefota_upgrade_flag_cfg(const char *state)

+{

+    cfg_set("fota_update_flag", state);

+

+    return 0;

+}

+

 int get_last_work_time(int *time, int *interval)

 {

     if (time == NULL || interval == NULL) {

@@ -113,7 +120,7 @@
     return 0;

 }

 

-static int fota_is_file_exist(const char* path)

+int fota_is_file_exist(const char* path)

 {

 	if ( (path == NULL) || (*path == '\0') )

 		return 0;

@@ -123,7 +130,7 @@
 	return 1;

 }

 

-static int fota_read_file(const char*path, char*buf, size_t sz)

+int fota_read_file(const char*path, char*buf, size_t sz)

 {

 	int fd = -1;

 	size_t cnt;

@@ -154,7 +161,7 @@
 	return cnt;

 }

 

-static int fota_read_file_int(const char* path, int *val)

+int fota_read_file_int(const char* path, int *val)

 {

 	char buf[32];

 	char *end;

@@ -176,21 +183,24 @@
 	return 0;

 }

 

-static int fota_get_update_status(int *fota_status)

+int fota_get_update_status(int *fota_status)

 {

 

 	int status = 0;

 	int ret = 0;

 	if(!fota_is_file_exist(FOTA_UPDATE_STATUS_FILE))

 	{

+		printf("fota_get_update_status file not exist\n");

 		*fota_status = -1;

 		return -1;

 	}

 	ret = fota_read_file_int(FOTA_UPDATE_STATUS_FILE, &status);

 	if(ret < 0) {

+		printf("fota_get_update_status read update_status error\n");

 		*fota_status = -1;

 		return -1;

 	}

+	printf("fota_get_update_status read status:%d\n", status);

 	*fota_status = status;

 	return 0;

 }

@@ -224,6 +234,13 @@
     return 0;

 }

 

+void wefota_upgrade_without_verify(void)

+{

+    system("fota_upi -u recovery");

+

+    return;

+}

+

 int wait_fota_conditions(void)

 {

     // wait for data connected

diff --git a/lynq/R307L/ap/app/wefota/wefota_device.h b/lynq/R307L/ap/app/wefota/wefota_device.h
index fa67f81..cec628e 100755
--- a/lynq/R307L/ap/app/wefota/wefota_device.h
+++ b/lynq/R307L/ap/app/wefota/wefota_device.h
@@ -7,6 +7,7 @@
 #define FOTA_INTERVAL_RANDOM (3600 * 2)

 #define FOTA_DOWNLOAD_FILEPATH "/cache/zte_fota/delta.package"

 #define FOTA_UPDATE_STATUS_FILE "/cache/zte_fota/update_status"

+#define FOTA_NEED_CONTINUE_PREVIOUS_UPGRADE 2

 

 int get_iccid(char *iccid);

 int get_imei(char *imei);

@@ -20,5 +21,11 @@
 int wait_fota_conditions(void);

 int set_wefota_current_upgrade_state_cfg(const char *state);

 int set_wefota_new_version_state_cfg(const char *state);

+int set_wefota_upgrade_flag_cfg(const char *state);

+int fota_is_file_exist(const char* path);

+int fota_read_file(const char*path, char*buf, size_t sz);

+int fota_read_file_int(const char* path, int *val);

+int fota_get_update_status(int *fota_status);

+void wefota_upgrade_without_verify(void);

 

 #endif
\ No newline at end of file
diff --git a/lynq/R307L/ap/app/wefota/wefota_main.c b/lynq/R307L/ap/app/wefota/wefota_main.c
index c030192..587a0c4 100755
--- a/lynq/R307L/ap/app/wefota/wefota_main.c
+++ b/lynq/R307L/ap/app/wefota/wefota_main.c
@@ -514,11 +514,24 @@
     int ret = -1;

 	int i = 0;

 	int recv_packet_retry = 0;

+    int upgradeStatus = -1;

     WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;

     int count = wefota_header_init(hdr);

     printf("proc_get_diff_pack_data send code=%d, count=%d)\n", hdr->code, count);

-	system("rm -rf /cache/zte_fota");

-	system("mkdir /cache/zte_fota");

+

+    fota_get_update_status(&upgradeStatus);

+    printf("proc_get_diff_pack_data fota_get_update_status upgradeStatus=%d\n", upgradeStatus);

+    if (upgradeStatus == 0)

+    {

+        if(fota_is_file_exist(FOTA_DOWNLOAD_FILEPATH))

+        {

+            printf("proc_get_diff_pack_data upgradeStatus=0, Continue upgrading!!\n");

+            return FOTA_NEED_CONTINUE_PREVIOUS_UPGRADE;

+        }

+    }

+	

+    system("rm -rf /cache/zte_fota");

+    system("mkdir /cache/zte_fota");

 

     int filefd = open(FOTA_DOWNLOAD_FILEPATH, O_WRONLY | O_CREAT | O_TRUNC, 0644);

     if (filefd < 0)

@@ -716,6 +729,7 @@
     int sock = -1;

 	FILE *fd_voltage = NULL;

 	char buf_volt[8] = {0};

+	set_wefota_upgrade_flag_cfg("1");

     do {

 		fd_voltage = fopen(CHARGE_VOLTAGE_PATH, "r");

 		if (fd_voltage != NULL)

@@ -786,6 +800,14 @@
 			printf("**********proc_get_diff_pack_data error \n");

             break;

         }

+        else if (ret == FOTA_NEED_CONTINUE_PREVIOUS_UPGRADE)

+        {

+            printf("**********wefota_proc,The last upgrade was not completed, continue upgrading \n");

+            close_udp_socket(sock);

+            sock = -1;

+            wefota_upgrade_without_verify();

+            break;

+        }

 

         ret = proc_get_diff_pack_end(sock);

 

@@ -794,7 +816,8 @@
 

         ret = proc_install_diff_pack();

     }while(0);

-    

+

+    set_wefota_upgrade_flag_cfg("0");

     if (sock >= 0) {

         close_udp_socket(sock);

     }

diff --git a/lynq/R307L/ap/app/zte_comm/at_ctl/src/atconfig/ps_normal.c b/lynq/R307L/ap/app/zte_comm/at_ctl/src/atconfig/ps_normal.c
index 57944da..bc1a809 100755
--- a/lynq/R307L/ap/app/zte_comm/at_ctl/src/atconfig/ps_normal.c
+++ b/lynq/R307L/ap/app/zte_comm/at_ctl/src/atconfig/ps_normal.c
@@ -1664,6 +1664,7 @@
 

     cfg_set(LOCK_CELL_STATE_FLAG, "0");

     cfg_set("ussd_cancel_flag","no");//ussd

+    cfg_set("fota_update_flag", "");

 	

 }