1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <aos/vfs.h>
8 #include "ota_log.h"
9 #include "ota_import.h"
10 #include "ota_hal_os.h"
11 #include "ota_hal.h"
12 #include "upack_data_file.h"
13 
ota_fopen(const char * filename,int mode)14 OTA_WEAK int ota_fopen(const char *filename, int mode)
15 {
16     int fd = -1;
17     if (filename != NULL) {
18         fd = aos_open(filename, mode);
19     }
20     return fd;
21 }
22 
ota_fwrite(int fd,const void * buf,unsigned int size)23 OTA_WEAK int ota_fwrite(int fd, const void *buf, unsigned int size)
24 {
25     int ret = -1;
26     if ((fd >= 0) && (buf != NULL)) {
27         ret = aos_write(fd, buf, size);
28     }
29     return ret;
30 }
31 
ota_fread(int fd,void * buf,unsigned int size)32 OTA_WEAK int ota_fread(int fd, void *buf, unsigned int size)
33 {
34     int ret = -1;
35     if ((fd >= 0) && (buf != NULL)) {
36         ret = aos_read(fd, buf, size);
37     }
38     return ret;
39 }
40 
ota_fclose(int fd)41 OTA_WEAK int ota_fclose(int fd)
42 {
43     int ret = -1;
44     if (fd >= 0) {
45         ret = aos_close(fd);
46     }
47     return ret;
48 }
49 
ota_jsapp_version_get(char * version,char * file_path)50 OTA_WEAK int ota_jsapp_version_get(char *version, char *file_path)
51 {
52     int ret = -1;
53     int i = 0;
54     int fd = -1;
55     char read_buf[65];
56     int read_len = 0;
57     char *pos = 0;
58     if ((version != NULL) && (file_path != NULL)) {
59         ret = 0;
60         fd = ota_fopen(file_path, O_RDONLY);
61         if (fd < 0) {
62             OTA_LOG_I("can't find app.json file\r\n");
63             strcpy(version, "0.0.0");
64         } else {
65             memset(read_buf, 0x00, sizeof(read_buf));
66             read_len = ota_fread(fd, read_buf, sizeof(read_buf) - 1);
67             if (read_len > 13) {
68                 pos = strstr(read_buf, "\"version\":");
69                 if (pos != NULL) {
70                     pos += 10;
71                     while (pos[0] != '"') {
72                         pos++;
73                     }
74                     pos++;
75                     while (pos[0] != '"') {
76                         version[i++] = pos[0];
77                         pos++;
78                     }
79                     version[i] = 0;
80                 }
81             }
82         }
83         if (fd >= 0) {
84             ota_fclose(fd);
85         }
86     }
87     return ret;
88 }
89 
ota_install_jsapp(void * ota_ctx,char * store_file,int store_file_len,char * install_path)90 int ota_install_jsapp(void *ota_ctx, char *store_file, int store_file_len, char *install_path)
91 {
92     int ret = -1;
93     ota_service_t *ctx = ota_ctx;
94     OTA_LOG_I("upgrade sub module\n");
95     if ((store_file != NULL) && (install_path != NULL)) {
96         OTA_LOG_I("store_file:%s, install_file:%s\n", store_file, install_path);
97         ret = data_file_unpack(store_file, store_file_len, install_path);
98     }
99     if (ret < 0) {
100         OTA_LOG_E("js app install failed\n");
101         if ((ctx != NULL) && (ctx->report_func.report_status_cb !=  NULL)) {
102             ctx->report_func.report_status_cb(ctx->report_func.param, ret);
103         }
104     }
105     if ((ctx != NULL) && (ctx->feedback_func.on_user_event_cb != NULL)) {
106         ctx->feedback_func.on_user_event_cb(OTA_EVENT_INSTALL, ret, ctx->feedback_func.param);
107     }
108     return ret;
109 }