|
|
@@ -1,51 +1,116 @@
|
|
|
#include <stdlib.h>
|
|
|
+#include <stdio.h>
|
|
|
#include <sys/types.h>
|
|
|
#include <sys/stat.h>
|
|
|
#include <fcntl.h>
|
|
|
#include <unistd.h>
|
|
|
+#include <signal.h>
|
|
|
#include <string.h>
|
|
|
-#include <pthread.h>
|
|
|
+#include <stdarg.h>
|
|
|
#include "upgrade.h"
|
|
|
-#include "sys.h"
|
|
|
#include "md5.h"
|
|
|
|
|
|
|
|
|
-#define LOG log_print
|
|
|
-
|
|
|
-
|
|
|
+typedef struct {
|
|
|
+ char *pbuf;
|
|
|
+ int dlen;
|
|
|
+ int blen;
|
|
|
+}node_t;
|
|
|
typedef struct {
|
|
|
uint32_t src; //文件下载完毕后,需要调用upgrade_set_src()设置该src,标记数据来源
|
|
|
uint8_t obj; //升级对象,解压后扫描文件,赋值到该变量
|
|
|
uint8_t stat; //当前升级所处状态
|
|
|
uint8_t step; //当前升级所处阶段
|
|
|
- sem_t sem; //信号量,没有文件下载完成是,升级线程处于挂起状态
|
|
|
|
|
|
- pthread_t thdId;
|
|
|
+ pthread_t tid;
|
|
|
FILE* log;
|
|
|
+ int fifo;
|
|
|
+ char buf[500];
|
|
|
}upg_handle_t;
|
|
|
-typedef struct {
|
|
|
- char *pbuf;
|
|
|
- int dlen;
|
|
|
- int blen;
|
|
|
-}node_t;
|
|
|
static upg_handle_t upgHandle;
|
|
|
+static int log_print(const char *fmt, ...);
|
|
|
+
|
|
|
+#define LOG log_print
|
|
|
+
|
|
|
+
|
|
|
+static int node_free(node_t *nd)
|
|
|
+{
|
|
|
+ if(!nd) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ free(nd->pbuf);
|
|
|
+ nd->pbuf = NULL;
|
|
|
+ nd->dlen = 0;
|
|
|
+ nd->blen = 0;
|
|
|
+}
|
|
|
+static int exe_cmd(char *cmd, node_t *nd)
|
|
|
+{
|
|
|
+ int r=0;
|
|
|
+ FILE *fp=NULL;
|
|
|
+ char *pbuf=NULL;
|
|
|
+ int onelen=1000;
|
|
|
+ int dlen=0,blen=onelen;
|
|
|
+
|
|
|
+ pbuf = malloc(onelen);
|
|
|
+ if(!pbuf) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ fp = popen(cmd, "r");
|
|
|
+ if (fp == NULL) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while(1) {
|
|
|
+ r = fread(pbuf+dlen, sizeof(char), onelen, fp);
|
|
|
+ if(r<=0) {
|
|
|
+ LOG("upgrade relloac %d failed\n", dlen+onelen);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ dlen += r;
|
|
|
+ if(dlen+onelen>blen) {
|
|
|
+ char* p = realloc(pbuf, dlen+onelen);
|
|
|
+ if(!p) {
|
|
|
+ LOG("upgrade relloac %d failed\n", dlen+onelen);
|
|
|
+ free(pbuf); pclose(fp);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ pbuf = p;
|
|
|
+ blen = dlen+onelen;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pclose(fp);
|
|
|
+
|
|
|
+ nd->pbuf = pbuf;
|
|
|
+ nd->dlen = dlen;
|
|
|
+ nd->blen = blen;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static int exe_cmd2(char *cmd)
|
|
|
+{
|
|
|
+ return system(cmd);
|
|
|
+}
|
|
|
|
|
|
//https://blog.csdn.net/qq_38650447/article/details/136284071
|
|
|
static int log_init(void)
|
|
|
{
|
|
|
+ char *path=UPG_LOG_PATH;
|
|
|
+
|
|
|
if (upgHandle.log) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- upgHandle.log = fopen(UPG_LOG_FILE, "wt");
|
|
|
+ upgHandle.log = fopen(path, "wt");
|
|
|
if (!upgHandle.log) {
|
|
|
- printf("%s open failed\n", UPG_LOG_FILE);
|
|
|
+ printf("%s open failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
-static int log_print(const *fmt, ...)
|
|
|
+static int log_print(const char *fmt, ...)
|
|
|
{
|
|
|
int r;
|
|
|
char buf[1000];
|
|
|
@@ -65,10 +130,10 @@ static int log_print(const *fmt, ...)
|
|
|
}
|
|
|
static int log_flush(void)
|
|
|
{
|
|
|
- if (logFp) {
|
|
|
+ if (upgHandle.log) {
|
|
|
return -1;
|
|
|
}
|
|
|
- fflush(logFp);
|
|
|
+ fflush(upgHandle.log);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -95,11 +160,11 @@ static int file_length(char* path)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- return st.st_size);
|
|
|
+ return st.st_size;
|
|
|
}
|
|
|
static int file_touch(char *path, int id)
|
|
|
{
|
|
|
- FILE *fp
|
|
|
+ FILE *fp;
|
|
|
char tmp[100];
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s%d", path, id);
|
|
|
@@ -107,10 +172,17 @@ static int file_touch(char *path, int id)
|
|
|
if (fp == NULL) {
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
fclose(fp); // 关闭文件
|
|
|
return 0;
|
|
|
}
|
|
|
+static int file_touch_rm(char *path, int id)
|
|
|
+{
|
|
|
+ char tmp[100];
|
|
|
+
|
|
|
+ snprintf(tmp, sizeof(tmp), "%s%d", path, id);
|
|
|
+ return remove(tmp);
|
|
|
+
|
|
|
+}
|
|
|
static int file_save(char *path, char *buf, int len)
|
|
|
{
|
|
|
int r;
|
|
|
@@ -124,6 +196,10 @@ static int file_save(char *path, char *buf, int len)
|
|
|
r = fwrite(buf, 1, len, fp);
|
|
|
if(r!=len) {
|
|
|
LOG("file_save %s failed, %d, %d\n", path, r, len);
|
|
|
+ r = -1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ r = 0;
|
|
|
}
|
|
|
fclose(fp);
|
|
|
|
|
|
@@ -136,155 +212,270 @@ static int file_load(char* path, char* buf, int len)
|
|
|
|
|
|
fp = fopen(path, "rb");
|
|
|
if (!fp) {
|
|
|
- log_e("file_load, fopen %s failed, %d, %d\n", path);
|
|
|
+ LOG("file_load, fopen %s failed, %d, %d\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
r = fread(buf, 1, len, fp);
|
|
|
if (r != len) {
|
|
|
- log_e("file_load %s failed, %d, %d\n", path, r, len);
|
|
|
+ LOG("file_load %s failed, %d, %d\n", path, r, len);
|
|
|
+ r = -1;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ r = 0;
|
|
|
}
|
|
|
fclose(fp);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
-static int file_remove(char *path, int id)
|
|
|
+static int file_remove(char *path)
|
|
|
{
|
|
|
- char tmp[100];
|
|
|
-
|
|
|
- snprintf(tmp, sizeof(tmp), "%s%d", path, id);
|
|
|
- return remove(tmp);
|
|
|
+ return remove(path);
|
|
|
}
|
|
|
+
|
|
|
+/*
|
|
|
+ tar pdutar to run/
|
|
|
+*/
|
|
|
static int file_cover(void)
|
|
|
{
|
|
|
int r;
|
|
|
- node_t nd;
|
|
|
- char tmp[200];
|
|
|
+ char cmd[100];
|
|
|
|
|
|
- snprintf(tmp, "tar -zxvfp %s %s", TMP_FOLDER, REL_FOLDER);
|
|
|
- r = sys_do(tmp, &nd);
|
|
|
- if(r==0) {
|
|
|
-
|
|
|
- sys_done(&nd);
|
|
|
+ snprintf(cmd, sizeof(cmd), "tar -xjfp %s %s", UPG_TAR_PATH, RUN_FOLDER);
|
|
|
+ r = exe_cmd2(cmd);
|
|
|
+ if(r) {
|
|
|
+ LOG("% failed, %d\n", cmd, r);
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
- return r;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+/*
|
|
|
+ when app run success, copy run/* to bak/
|
|
|
+*/
|
|
|
+static int file_bakup(void)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ char cmd[100];
|
|
|
+
|
|
|
+ snprintf(cmd, sizeof(cmd), "cp -rfp %s %s", RUN_FOLDER, BAK_FOLDER);
|
|
|
+ r = exe_cmd2(cmd);
|
|
|
+ r = exe_cmd2(cmd);
|
|
|
+ if(r) {
|
|
|
+ LOG("% failed, %d\n", cmd, r);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ when app run failed, copy to bak/ run/
|
|
|
+*/
|
|
|
+static int file_restore(void)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ char cmd[100];
|
|
|
+
|
|
|
+ snprintf(cmd, sizeof(cmd), "cp -rfp %s %s", BAK_FOLDER, RUN_FOLDER);
|
|
|
+ r = exe_cmd2(cmd);
|
|
|
+ if(r) {
|
|
|
+ LOG("% failed, %d\n", cmd, r);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
+/*
|
|
|
+ when app upgrade success, clear tmp files
|
|
|
+*/
|
|
|
static int file_clear(void)
|
|
|
{
|
|
|
- file_remove();
|
|
|
+ int r;
|
|
|
+ //file_remove();
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
-static int file_scan(char *path, char *buf, int len)
|
|
|
+
|
|
|
+
|
|
|
+static int kill_app(void)
|
|
|
{
|
|
|
int r;
|
|
|
- FILE *fp;
|
|
|
-
|
|
|
- fp = fopen(path, "wb");
|
|
|
- if(!fp) {
|
|
|
- log_e("file_save, fopen %s failed, %d, %d\n", path);
|
|
|
+ char cmd[100];
|
|
|
+
|
|
|
+ snprintf(cmd, sizeof(cmd), "killall -e main");
|
|
|
+ r = exe_cmd2(cmd);
|
|
|
+ if(r) {
|
|
|
+ LOG("% failed, %d\n", cmd, r);
|
|
|
return -1;
|
|
|
}
|
|
|
- r = fwrite(buf, 1, len, fp);
|
|
|
- if(r!=len) {
|
|
|
- log_e("file_save %s failed, %d, %d\n", path, r, len);
|
|
|
- }
|
|
|
- fclose(fp);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
- return r;
|
|
|
+#define CHAR(b) (((b)<=9)?((b)+0x30):((b)-10+0x61));
|
|
|
+static void byte2char(char *chr, uint8_t b)
|
|
|
+{
|
|
|
+ chr[0] = CHAR(b>>4);
|
|
|
+ chr[1] = CHAR(b&0x0f);
|
|
|
}
|
|
|
-static int kill_me(void)
|
|
|
+static void print_md5(char *s, md5_t *m)
|
|
|
{
|
|
|
- pid_t pid = getpid(); // 获取当前进程的PID
|
|
|
- int ret = kill(pid, SIGKILL); // 发送SIGKILL信号杀死自身
|
|
|
-
|
|
|
- if (ret == -1) {
|
|
|
- log_e("kill_me failed\n"); // 如果kill调用失败,打印错误信息
|
|
|
- return -1;
|
|
|
+ int i;
|
|
|
+
|
|
|
+ LOG("__%s", s);
|
|
|
+ for(i=0; i<32; i++) {
|
|
|
+ LOG("%c", m->data[i]);
|
|
|
}
|
|
|
-
|
|
|
- return 0;
|
|
|
+ LOG("\n");
|
|
|
}
|
|
|
-static int upg_step_check(upg_handle_t *h)
|
|
|
-{
|
|
|
- int id,r=0;
|
|
|
- char tmp[100];
|
|
|
|
|
|
- for(id=1; id<UPGRADE_STEP_MAX; id++) {
|
|
|
- snprintf(tmp, sizeof(tmp), "cp -rf %s", UPG_FOLDER, id);
|
|
|
- r = file_exist(tmp, id);
|
|
|
- if(r) {
|
|
|
- break;
|
|
|
- }
|
|
|
+//md5相同返回0,否则返回1
|
|
|
+static int md5_check(char *buf, int len)
|
|
|
+{
|
|
|
+ md5_ctx_t ctx;
|
|
|
+ uint8_t i,tmp[16];
|
|
|
+ md5_t m1,m2;
|
|
|
+
|
|
|
+ md5_init(&ctx);
|
|
|
+ md5_update(&ctx, buf, len);
|
|
|
+ md5_final(&ctx, tmp);
|
|
|
+
|
|
|
+ for (i=0; i<16; i++) {
|
|
|
+ byte2char(&m2.data[i*2], tmp[i]);
|
|
|
}
|
|
|
+ memcpy(&m1, buf+len-sizeof(m1), sizeof(m1));
|
|
|
|
|
|
- if(r) {
|
|
|
- h->step = id;
|
|
|
+ print_md5("ori md5: ", &m1);
|
|
|
+ print_md5("cal md5: ", &m2);
|
|
|
+ if(memcmp(&m1, &m2, sizeof(md5_t))) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
- //重启后如果是处于覆盖进度,则将进度置为SUCCESS
|
|
|
- if(h->step==UPGRADE_STEP_COVER) {
|
|
|
- h->step = UPGRADE_STEP_SUCCESS;
|
|
|
- }
|
|
|
- sem_post(&upgHandle.sem);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static int file_split(char *buf, int len)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+
|
|
|
+ r = file_save(UPG_HDR_PATH, buf, sizeof(upgrade_file_t));
|
|
|
+ if(r) {
|
|
|
+ LOG("file_save %s failed\n", UPG_HDR_PATH);
|
|
|
+ return r;
|
|
|
}
|
|
|
-
|
|
|
- return r;
|
|
|
+
|
|
|
+ r = file_save(UPG_TAR_PATH, buf+sizeof(upgrade_file_t), len-sizeof(upgrade_file_t)-sizeof(md5_t));
|
|
|
+ if(r) {
|
|
|
+ LOG("file_save %s failed\n", UPG_TAR_PATH);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-static int upg_file_parse(upg_handle_t *h)
|
|
|
+static int file_parse(upg_handle_t *h)
|
|
|
{
|
|
|
int r=0,flen;
|
|
|
- char tmp[100];
|
|
|
char *fbuf=NULL;
|
|
|
+ char *path=UPG_PKG_PATH;
|
|
|
upgrade_file_t *hfile=NULL;
|
|
|
|
|
|
- snprintf(tmp, sizeof(tmp), "%s/%s", UPG_FOLDER, UPG_FILE);
|
|
|
- flen = file_length(tmp);
|
|
|
+ flen = file_length(path);
|
|
|
if(flen<=0) {
|
|
|
- log_e("file_length %s failed\n", tmp);
|
|
|
+ LOG("file_length %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
fbuf = (char*)malloc(flen);
|
|
|
if(!fbuf) {
|
|
|
- log_e("malloc %d failed\n", flen);
|
|
|
+ LOG("malloc %d failed\n", flen);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ r = file_load(path, fbuf, flen);
|
|
|
+ if(r) {
|
|
|
+ LOG("%s load failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
- file_load(tmp, fbuf, flen);
|
|
|
-
|
|
|
- //hfile = (upgrade_file_t*)fbuf;
|
|
|
- //
|
|
|
|
|
|
+ r = md5_check(fbuf, flen);
|
|
|
+ if(r) {
|
|
|
+ LOG("md5 check failed\n");
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
|
|
|
+ hfile = (upgrade_file_t*)fbuf;
|
|
|
+ if(hfile->magic!=UPG_FILE_MAGIC ||
|
|
|
+ hfile->obj.num>=UPGRADE_NUM_MAX ||
|
|
|
+ hfile->obj.stra>=UPGRADE_STRA_MAX) {
|
|
|
+ r = -1;
|
|
|
+ LOG("magic: 0x%08x, should be: 0x%08x\n", hfile->magic, UPG_FILE_MAGIC);
|
|
|
+ LOG("num: %d, should < %d\n", hfile->obj.num, UPGRADE_NUM_MAX);
|
|
|
+ LOG("stra: %d, should < %d\n", hfile->obj.stra, UPGRADE_STRA_MAX);
|
|
|
+
|
|
|
+ LOG("file hdr check failed\n");
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
|
|
|
+ r = file_split(fbuf, flen);
|
|
|
+ if(r) {
|
|
|
+ LOG("file_split failed\n");
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
+
|
|
|
+ r = file_remove(path);
|
|
|
+ if(r) {
|
|
|
+ LOG("file_remove %s failed\n", path);
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
|
|
|
+fail:
|
|
|
+ free(fbuf);
|
|
|
+ return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
-static int upg_file_proc(upg_handle_t *h)
|
|
|
+
|
|
|
+//////////////////////////////////////////////////
|
|
|
+static int set_src(uint8_t src)
|
|
|
{
|
|
|
- int r;
|
|
|
+ upgHandle.src = src;
|
|
|
+ upgHandle.step = UPGRADE_STEP_DOWN;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static int upg_init(upg_handle_t *h)
|
|
|
+{
|
|
|
+ int id,r;
|
|
|
+ char tmp[100];
|
|
|
|
|
|
- threads_stop();
|
|
|
+ mkfifo(UPG_FIFO_PATH, 0666);
|
|
|
|
|
|
- while(h->step!=UPGRADE_STEP_NONE) {
|
|
|
-
|
|
|
- switch(h->step) {
|
|
|
- case UPGRADE_STEP_DOWN:
|
|
|
- {
|
|
|
- file_save(UPG_FOLDER, UPGRADE_STEP_DOWN);
|
|
|
- r = file_release(void);
|
|
|
- if(r==0) {
|
|
|
- h->step = UPGRADE_STEP_RELEASE;
|
|
|
- }
|
|
|
- }
|
|
|
+ memset(h, 0, sizeof(upgHandle));
|
|
|
+ h->src = UPGRADE_SRC_NONE;
|
|
|
+ h->stat = UPGRADE_STAT_NONE;
|
|
|
+ h->fifo = open(UPG_FIFO_PATH, O_RDWR);
|
|
|
+
|
|
|
+ for(id=1; id<UPGRADE_STEP_MAX; id++) {
|
|
|
+ snprintf(tmp, sizeof(tmp), "cp -rf %s", UPG_FOLDER, id);
|
|
|
+ r = file_exist(tmp, id);
|
|
|
+ if(r) {
|
|
|
+ //重启后如果是处于覆盖进度,则将进度置为SUCCESS
|
|
|
+ h->step = (id==UPGRADE_STEP_COVER)?UPGRADE_STEP_SUCCESS:id;
|
|
|
break;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- case UPGRADE_STEP_RELEASE:
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int upg_proc(upg_handle_t *h)
|
|
|
+{
|
|
|
+ int r=0;
|
|
|
+
|
|
|
+ while(1) {
|
|
|
+ switch(h->step) {
|
|
|
+ case UPGRADE_STEP_DOWN:
|
|
|
{
|
|
|
- file_save(UPG_FOLDER, UPGRADE_STEP_RELEASE);
|
|
|
- file_remove(UPG_FOLDER, UPGRADE_STEP_DOWN);
|
|
|
- r = file_cover(void);
|
|
|
+ file_touch(UPG_FOLDER, UPGRADE_STEP_DOWN);
|
|
|
+ r = file_cover();
|
|
|
if(r==0) {
|
|
|
h->step = UPGRADE_STEP_COVER;
|
|
|
}
|
|
|
@@ -293,89 +484,53 @@ static int upg_file_proc(upg_handle_t *h)
|
|
|
|
|
|
case UPGRADE_STEP_COVER:
|
|
|
{
|
|
|
- file_remove(UPG_FOLDER, UPGRADE_STEP_RELEASE);
|
|
|
- file_save(UPG_FOLDER, UPGRADE_STEP_COVER);
|
|
|
- kill_me(); //重启应用
|
|
|
+ file_touch_rm(UPG_FOLDER, UPGRADE_STEP_DOWN);
|
|
|
+ file_touch(UPG_FOLDER, UPGRADE_STEP_COVER);
|
|
|
+ kill_app(); //重启应用
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case UPGRADE_STEP_SUCCESS:
|
|
|
{
|
|
|
- file_remove(UPG_FOLDER, UPGRADE_STEP_COVER);
|
|
|
+ file_touch_rm(UPG_FOLDER, UPGRADE_STEP_COVER);
|
|
|
h->step = UPGRADE_STEP_NONE;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
+ default:
|
|
|
+ r = 1;
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
- threads_start();
|
|
|
-
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-//////////////////////////////////////////////////
|
|
|
+ if(r) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-static void* upgrade_thread(void *arg)
|
|
|
-{
|
|
|
- upg_handle_t *h=(upg_handle_t*)arg;
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
- while(1) {
|
|
|
- sem_wait(&upgHandle.sem);
|
|
|
-
|
|
|
- upg_file_proc();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- pthread_exit(NULL);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-/* @brief 升级初始化
|
|
|
- 该函数将启动升级线程
|
|
|
-*/
|
|
|
-int upgrade_init(void)
|
|
|
+int main(void)
|
|
|
{
|
|
|
- pthread_attr_t attr;
|
|
|
- struct sched_param param;
|
|
|
- //_globalDeviceManager
|
|
|
-
|
|
|
- memset(&upgHandle, 0, sizeof(upgHandle));
|
|
|
-
|
|
|
- upgHandle.src = UPGRADE_SRC_NONE;
|
|
|
- upgHandle.stat = UPGRADE_STAT_NONE;
|
|
|
- upgHandle.thdId = 0;
|
|
|
- upgHandle.sem = ;
|
|
|
- sem_init(&upgHandle.sem, 0, 0);
|
|
|
-
|
|
|
- pthread_create(&upgHandle.thdId, NULL,upgrade_thread, &upgHandle);
|
|
|
+ int id,r=0;
|
|
|
+ upgrade_msg_t msg;
|
|
|
+ upg_handle_t *h=&upgHandle;
|
|
|
+
|
|
|
+ upg_init(h);
|
|
|
+ while(1) {
|
|
|
+ r = read(h->fifo, &msg, sizeof(msg));
|
|
|
+ if(r>0) {
|
|
|
+ if(msg.evt==UPGRADE_EVT_APP_RUN) {
|
|
|
+ h->step = UPGRADE_STEP_SUCCESS;
|
|
|
+ }
|
|
|
+ else if(msg.evt==UPGRADE_EVT_DL_FINISH) {
|
|
|
+ h->step = UPGRADE_STEP_DOWN;
|
|
|
+ }
|
|
|
+ upg_proc(h);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
-
|
|
|
-int upgrade_stat(uint8_t *st)
|
|
|
-{
|
|
|
- if(!st) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- *st = upgHandle.stat;
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-int upgrade_set_src(uint8_t src)
|
|
|
-{
|
|
|
- upgHandle.src = src;
|
|
|
- upgHandle.step = UPGRADE_STEP_DOWN;
|
|
|
- sem_post(&upgHandle.sem);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|