Browse Source

add:ota提出来

Dozingfiretruck 4 years ago
parent
commit
154ad80cb3
1 changed files with 70 additions and 43 deletions
  1. 70 43
      app/port/luat_fs_air101.c

+ 70 - 43
app/port/luat_fs_air101.c

@@ -23,6 +23,74 @@ uint32_t luadb_size_kb;
 uint32_t lfs_addr;
 uint32_t lfs_size_kb;
 
+#define UPDATE_TGZ_PATH "/update.tgz"
+#define UPDATE_BIN_PATH "/update.bin"
+
+extern int zlib_decompress(FILE *source, FILE *dest);
+extern int luat_luadb_checkfile(const char* path);
+
+static int luat_ota(void){
+    int ret  = 0;
+    //检测是否有压缩升级文件
+    if(luat_fs_fexist(UPDATE_TGZ_PATH)){
+        LLOGI("found update.tgz, decompress ...");
+        FILE *fd_in = luat_fs_fopen(UPDATE_TGZ_PATH, "r");
+        if (fd_in == NULL){
+            LLOGE("open the input file : %s error!", UPDATE_TGZ_PATH);
+            ret = -1;
+            goto _close_decompress;
+        }
+        luat_fs_remove(UPDATE_BIN_PATH);
+        FILE *fd_out = luat_fs_fopen(UPDATE_BIN_PATH, "w+");
+        if (fd_out == NULL){
+            LLOGE("open the output file : %s error!", UPDATE_BIN_PATH);
+            ret = -1;
+            goto _close_decompress;
+        }
+        ret = zlib_decompress(fd_in, fd_out);
+        if (ret != 0){
+            LLOGE("decompress file error!");
+        }
+_close_decompress:
+        if(fd_in != NULL){
+            luat_fs_fclose(fd_in);
+        }
+        if(fd_out != NULL){
+            luat_fs_fclose(fd_out);
+        }
+        //不论成功与否都删掉避免每次启动都执行一遍
+        luat_fs_remove(UPDATE_TGZ_PATH);
+    }
+
+    //检测是否有升级文件
+    if(luat_fs_fexist(UPDATE_BIN_PATH)){
+        LLOGI("found update.bin, checking");
+        if (luat_luadb_checkfile(UPDATE_BIN_PATH) == 0) {
+            LLOGI("update.bin ok, updating...");
+            #define UPDATE_BUFF_SIZE 4096
+            char* buff = luat_heap_malloc(UPDATE_BUFF_SIZE);
+            int len = 0;
+            int offset = 0;
+            if (buff != NULL) {
+                FILE* fd = luat_fs_fopen(UPDATE_BIN_PATH, "rb");
+                while (1) {
+                    len = luat_fs_fread(buff, UPDATE_BUFF_SIZE, 1, fd);
+                    if (len < 1)
+                        break;
+                    tls_fls_write(luadb_addr + offset, buff, len);
+                    offset += len;
+                }
+            }
+        }
+        else {
+            ret = -1;
+            LLOGW("update.bin NOT ok, skip");
+        }
+        luat_fs_remove(UPDATE_BIN_PATH);
+    }
+    return ret;
+}
+
 
 #ifndef FLASH_FS_REGION_SIZE
 #define FLASH_FS_REGION_SIZE 112
@@ -40,9 +108,6 @@ void luat_lv_fs_init(void);
 void lv_split_jpeg_init(void);
 #endif
 
-extern int zlib_decompress(FILE *source, FILE *dest);
-extern int luat_luadb_checkfile(const char* path);
-
 int luat_fs_init(void) {
     //luat_timer_mdelay(1000);
 #ifdef AIR103
@@ -93,46 +158,8 @@ int luat_fs_init(void) {
 		.mount_point = "/"
 	};
 	luat_fs_mount(&conf);
-
-    // 检查是否有压缩升级文件
-    #define UPDATE_BIN_PATH "/update.bin"
-    if(luat_fs_fexist("/update.tgz")){
-        LLOGI("found update.tgz, decompress ...");
-        FILE *fd_in = luat_fs_fopen("/update.tgz", "r");
-        luat_fs_remove(UPDATE_BIN_PATH);
-        FILE *fd_out = luat_fs_fopen(UPDATE_BIN_PATH, "w+");
-        zlib_decompress(fd_in, fd_out);
-        luat_fs_fclose(fd_in);
-        luat_fs_fclose(fd_out);
-        luat_fs_remove("/update.tgz");
-    }
-
-    // 检查是否有普通升级文件
-    if(luat_fs_fexist(UPDATE_BIN_PATH)){
-        LLOGI("found update.bin, checking");
-        if (luat_luadb_checkfile(UPDATE_BIN_PATH) == 0) {
-            LLOGI("update.bin ok, updating...");
-            #define UPDATE_BUFF_SIZE 4096
-            char* buff = luat_heap_malloc(UPDATE_BUFF_SIZE);
-            int len = 0;
-            int offset = 0;
-            if (buff != NULL) {
-                FILE* fd = luat_fs_fopen(UPDATE_BIN_PATH, "rb");
-                while (1) {
-                    len = luat_fs_fread(buff, UPDATE_BUFF_SIZE, 1, fd);
-                    if (len < 1)
-                        break;
-                    tls_fls_write(luadb_addr + offset, buff, len);
-                    offset += len;
-                }
-            }
-        }
-        else {
-            LLOGW("update.bin NOT ok, skip");
-        }
-        luat_fs_remove(UPDATE_BIN_PATH);
-    }
-
+    //OTA检测升级
+    luat_ota();
 	luat_vfs_reg(&vfs_fs_luadb);
 	luat_fs_conf_t conf2 = {
 		.busname = (char*)(luadb_addr == 0 ? luadb_inline_sys : ptr),