|
|
@@ -16,10 +16,13 @@
|
|
|
#include "md5.h"
|
|
|
|
|
|
|
|
|
-//#define LOG log_print
|
|
|
-#define LOG printf
|
|
|
-
|
|
|
-
|
|
|
+#if 0
|
|
|
+#define LOGD log_print
|
|
|
+#define LOGE log_print
|
|
|
+#else
|
|
|
+#define LOGD printf
|
|
|
+#define LOGE printf
|
|
|
+#endif
|
|
|
|
|
|
typedef struct {
|
|
|
char *pbuf;
|
|
|
@@ -34,7 +37,6 @@ typedef struct {
|
|
|
|
|
|
pthread_t tid;
|
|
|
FILE* log;
|
|
|
- key_t key;
|
|
|
}upg_handle_t;
|
|
|
static upg_handle_t upgHandle;
|
|
|
static int log_print(const char *fmt, ...);
|
|
|
@@ -74,14 +76,14 @@ static int exe_cmd(char *cmd, node_t *nd)
|
|
|
while(1) {
|
|
|
r = fread(pbuf+dlen, sizeof(char), onelen, fp);
|
|
|
if(r<=0) {
|
|
|
- LOG("upgrade relloac %d failed\n", dlen+onelen);
|
|
|
+ LOGE("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);
|
|
|
+ LOGE("upgrade relloac %d failed\n", dlen+onelen);
|
|
|
free(pbuf); pclose(fp);
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -117,7 +119,7 @@ static int log_init(void)
|
|
|
|
|
|
upgHandle.log = fopen(path, "wt");
|
|
|
if (!upgHandle.log) {
|
|
|
- printf("%s open failed\n", path);
|
|
|
+ LOGE("%s open failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -219,12 +221,12 @@ static int file_save(char *path, void *buf, int len)
|
|
|
|
|
|
fp = fopen(path, "wb");
|
|
|
if(!fp) {
|
|
|
- LOG("file_save, fopen %s failed\n", path);
|
|
|
+ LOGE("file_save, fopen %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
r = fwrite(buf, 1, len, fp);
|
|
|
if(r!=len) {
|
|
|
- LOG("file_save %s failed\n", path);
|
|
|
+ LOGE("file_save %s failed\n", path);
|
|
|
r = -1;
|
|
|
}
|
|
|
else {
|
|
|
@@ -241,12 +243,12 @@ static int file_load(char* path, void* buf, int len)
|
|
|
|
|
|
fp = fopen(path, "rb");
|
|
|
if (!fp) {
|
|
|
- LOG("file_load, fopen %s failed\n", path);
|
|
|
+ LOGE("file_load, fopen %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
r = fread(buf, 1, len, fp);
|
|
|
if (r != len) {
|
|
|
- LOG("file_load %s failed\n", path);
|
|
|
+ LOGE("file_load %s failed\n", path);
|
|
|
r = -1;
|
|
|
}
|
|
|
else {
|
|
|
@@ -272,7 +274,7 @@ static int file_cover(char *path)
|
|
|
snprintf(cmd, sizeof(cmd), "tar -xpf %s -C / > /dev/null 2>&1", path);
|
|
|
r = system(cmd);
|
|
|
if(r) {
|
|
|
- LOG("%s failed\n", cmd);
|
|
|
+ LOGE("%s failed\n", cmd);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -313,34 +315,34 @@ static int file_backup(void)
|
|
|
//dl目录有tar压缩包,则拷贝到bak目录
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
if(file_exist(tmp)) {
|
|
|
- LOG("%s exist\n", tmp);
|
|
|
+ LOGD("%s exist\n", tmp);
|
|
|
snprintf(cmd, sizeof(cmd), "cp -f %s %s/", tmp, BAK_FOLDER);
|
|
|
r = system(cmd);
|
|
|
if(r==0) {
|
|
|
return 0;
|
|
|
}
|
|
|
- LOG("cp %s to %s failed\n", tmp, BAK_FOLDER);
|
|
|
+ LOGD("cp %s to %s failed\n", tmp, BAK_FOLDER);
|
|
|
}
|
|
|
else {
|
|
|
- LOG("%s not exist\n", tmp);
|
|
|
+ LOGD("%s not exist\n", tmp);
|
|
|
}
|
|
|
|
|
|
//bak目录有tar压缩包,则退出
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", BAK_FOLDER, UPG_TAR_NAME);
|
|
|
if(file_exist(tmp)) {
|
|
|
- LOG("%s exist\n", tmp);
|
|
|
+ LOGD("%s exist\n", tmp);
|
|
|
return 0;
|
|
|
}
|
|
|
else {
|
|
|
- LOG("%s not exist\n", tmp);
|
|
|
+ LOGD("%s not exist\n", tmp);
|
|
|
}
|
|
|
|
|
|
//bak目录没有tar压缩包,则将run目录打包存进bak目录
|
|
|
- LOG("tar %s to %s/%s\n", RUN_FOLDER, BAK_FOLDER, UPG_TAR_NAME);
|
|
|
+ LOGD("tar %s to %s/%s\n", RUN_FOLDER, BAK_FOLDER, UPG_TAR_NAME);
|
|
|
snprintf(cmd, sizeof(cmd), "tar -czpPf %s/%s %s > /dev/null 2>&1", BAK_FOLDER, UPG_TAR_NAME, RUN_FOLDER);
|
|
|
r = system(cmd);
|
|
|
if(r) {
|
|
|
- LOG("%s failed, %d\n", cmd, r);
|
|
|
+ LOGE("%s failed, %d\n", cmd, r);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -358,7 +360,7 @@ static int file_restore(void)
|
|
|
snprintf(cmd, sizeof(cmd), "tar -xpf %s/%s -C / > /dev/null 2>&1", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
r = system(cmd);
|
|
|
if(r) {
|
|
|
- LOG("%s failed, %d\n", cmd, r);
|
|
|
+ LOGE("%s failed, %d\n", cmd, r);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -376,11 +378,11 @@ static void print_md5(char *s, md5_t *m)
|
|
|
{
|
|
|
int i;
|
|
|
|
|
|
- LOG("__%s", s);
|
|
|
+ LOGD("__%s", s);
|
|
|
for(i=0; i<32; i++) {
|
|
|
- LOG("%c", m->data[i]);
|
|
|
+ LOGD("%c", m->data[i]);
|
|
|
}
|
|
|
- LOG("\n");
|
|
|
+ LOGD("\n");
|
|
|
}
|
|
|
|
|
|
//md5相同返回0,否则返回1
|
|
|
@@ -416,19 +418,19 @@ static int file_split(void)
|
|
|
snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
|
|
|
flen = file_length(path);
|
|
|
if(flen<=0) {
|
|
|
- LOG("file_length %s failed\n", path);
|
|
|
+ LOGE("file_length %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
fbuf = (char*)malloc(flen);
|
|
|
if(!fbuf) {
|
|
|
- LOG("malloc %d failed\n", flen);
|
|
|
+ LOGE("malloc %d failed\n", flen);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
r = file_load(path, fbuf, flen);
|
|
|
if(r) {
|
|
|
- LOG("%s load failed\n", path);
|
|
|
+ LOGE("%s load failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
upgrade_file_t *hfile = (upgrade_file_t*)fbuf;
|
|
|
@@ -436,21 +438,21 @@ static int file_split(void)
|
|
|
snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
|
|
|
r = file_save(path, hfile, sizeof(upgrade_file_t));
|
|
|
if(r) {
|
|
|
- LOG("file_save %s failed\n", path);
|
|
|
+ LOGE("file_save %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
r = file_save(path, hfile->data, hfile->dlen);
|
|
|
if(r) {
|
|
|
- LOG("file_save %s failed\n", path);
|
|
|
+ LOGE("file_save %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
|
|
|
r = file_remove(path);
|
|
|
if(r) {
|
|
|
- LOG("file_remove %s failed\n", path);
|
|
|
+ LOGE("file_remove %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -465,19 +467,19 @@ static int file_check(char *path)
|
|
|
|
|
|
flen = file_length(path);
|
|
|
if(flen<=0) {
|
|
|
- LOG("file_length %s failed\n", path);
|
|
|
+ LOGE("file_length %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
fbuf = (char*)malloc(flen);
|
|
|
if(!fbuf) {
|
|
|
- LOG("malloc %d failed\n", flen);
|
|
|
+ LOGE("malloc %d failed\n", flen);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
r = file_load(path, fbuf, flen);
|
|
|
if(r) {
|
|
|
- LOG("%s load failed\n", path);
|
|
|
+ LOGE("%s load failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -485,17 +487,17 @@ static int file_check(char *path)
|
|
|
if(hfile->magic!=UPG_FILE_MAGIC ||
|
|
|
hfile->dlen!=flen-sizeof(upgrade_file_t)-sizeof(md5_t)) {
|
|
|
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);
|
|
|
+ LOGE("magic: 0x%08x, should be: 0x%08x\n", hfile->magic, UPG_FILE_MAGIC);
|
|
|
+ LOGE("num: %d, should < %d\n", hfile->obj.num, UPGRADE_NUM_MAX);
|
|
|
+ LOGE("stra: %d, should < %d\n", hfile->obj.stra, UPGRADE_STRA_MAX);
|
|
|
|
|
|
- LOG("file hdr check failed\n");
|
|
|
+ LOGE("file hdr check failed\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
|
|
|
r = md5_check(hfile);
|
|
|
if(r) {
|
|
|
- LOG("md5 check failed\n");
|
|
|
+ LOGE("md5 check failed\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
|
|
|
@@ -526,15 +528,11 @@ static int file_clear(void)
|
|
|
//////////////////////////////////////////////////
|
|
|
static int app_start(void)
|
|
|
{
|
|
|
- return system("/etc/init.d/S90PDUApp start");
|
|
|
+ return 0;//system("smartMonitor.sh &");
|
|
|
}
|
|
|
static int app_stop(void)
|
|
|
{
|
|
|
- return system("/etc/init.d/S90PDUApp stop");
|
|
|
-}
|
|
|
-static int app_restart(void)
|
|
|
-{
|
|
|
- return system("/etc/init.d/S90PDUApp restart");
|
|
|
+ return 0;//system("killall smartMonitor.sh && /etc/init.d/S90PDUApp stop");
|
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
@@ -542,36 +540,45 @@ static void* upg_thread(void *arg)
|
|
|
{
|
|
|
int r=0;
|
|
|
int msgid;
|
|
|
- upgrade_msg_t msg;
|
|
|
+ key_t key;
|
|
|
+ msgbuf_t mb;
|
|
|
upg_handle_t *h=(upg_handle_t*)arg;
|
|
|
|
|
|
- LOG("upg_thread running\n");
|
|
|
- msgid = msgget(h->key, 0666| IPC_CREAT);
|
|
|
- LOG("___ key: %d, msgid: %d\n", h->key, msgid);
|
|
|
+ LOGD("upg_thread running\n");
|
|
|
+ key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
|
|
|
+ if(key<0) {
|
|
|
+ LOGE("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
|
|
|
+ pthread_exit(NULL);
|
|
|
+ }
|
|
|
+
|
|
|
+ msgid = msgget(key, 0666|IPC_CREAT);
|
|
|
+ LOGD("___ key: %d, msgid: %d\n", key, msgid);
|
|
|
+
|
|
|
+ //msgctl(msgid, IPC_RMID, NULL);
|
|
|
|
|
|
while(1) {
|
|
|
- r = msgrcv(msgid, &msg, sizeof(msg), 1, 0);
|
|
|
- if(r<=0) {
|
|
|
+ r = msgrcv(msgid, &mb, sizeof(mb.evt), 0, 0);
|
|
|
+ if(r<0) {
|
|
|
+ LOGE("___msgrcv failed, %d\n", errno);
|
|
|
continue;
|
|
|
}
|
|
|
- LOG("___ msg.evt: 0x%02x\n", msg.evt);
|
|
|
+ LOGD("___ msgrcv evt: 0x%02x\n", mb.evt);
|
|
|
|
|
|
- switch(msg.evt) {
|
|
|
+ switch(mb.evt) {
|
|
|
case UPGRADE_EVT_APP_RUNNING:
|
|
|
{
|
|
|
- LOG("___ smartPDU running!\n");
|
|
|
+ LOGD("___ smartPDU running!\n");
|
|
|
h->step = UPGRADE_STEP_SUCCESS;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case UPGRADE_EVT_UPLOAD_FINISH:
|
|
|
{
|
|
|
- LOG("___ upgrade package upload finished!\n");
|
|
|
+ LOGD("___ upgrade package upload finished!\n");
|
|
|
h->step = UPGRADE_STEP_DOWN;
|
|
|
file_touch(UPGRADE_STEP_DOWN);
|
|
|
}
|
|
|
break;
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -579,19 +586,12 @@ static void* upg_thread(void *arg)
|
|
|
}
|
|
|
static int upg_init(upg_handle_t *h)
|
|
|
{
|
|
|
- int id,r;
|
|
|
- char tmp[200];
|
|
|
- key_t key;
|
|
|
+ int r;
|
|
|
|
|
|
folder_init();
|
|
|
-
|
|
|
- key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
|
|
|
- if(key<0) {
|
|
|
- LOG("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
|
|
|
- }
|
|
|
+
|
|
|
h->src = UPGRADE_SRC_NONE;
|
|
|
h->stat = UPGRADE_STAT_NONE;
|
|
|
- h->key = key;
|
|
|
|
|
|
return pthread_create(&h->tid, NULL, upg_thread, h);
|
|
|
}
|
|
|
@@ -604,12 +604,12 @@ static int upg_check(upg_handle_t *h)
|
|
|
snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, i);
|
|
|
r = file_exist(tmp);
|
|
|
if(r) {
|
|
|
- LOG("%s exist\n", tmp);
|
|
|
+ LOGD("%s exist\n", tmp);
|
|
|
//重启后如果是覆盖完成,则将进度置为SUCCESS
|
|
|
h->step = i+1;
|
|
|
break;
|
|
|
}
|
|
|
- LOG("%s not exist\n", tmp);
|
|
|
+ LOGD("%s not exist\n", tmp);
|
|
|
}
|
|
|
|
|
|
return r;
|
|
|
@@ -619,12 +619,12 @@ static int upg_handle(upg_handle_t *h)
|
|
|
int r=-1;
|
|
|
char tmp[200];
|
|
|
|
|
|
- LOG("___h->step: %d\n", h->step);
|
|
|
+ LOGD("___h->step: %d\n", h->step);
|
|
|
while(h->step!=UPGRADE_STEP_NONE) {
|
|
|
switch(h->step) {
|
|
|
case UPGRADE_STEP_DOWN:
|
|
|
{
|
|
|
- LOG("___ UPGRADE_STEP_DOWN\n");
|
|
|
+ LOGD("___ UPGRADE_STEP_DOWN\n");
|
|
|
|
|
|
//先检查下载目录是否存在pdupkg文件
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
|
|
|
@@ -633,11 +633,11 @@ static int upg_handle(upg_handle_t *h)
|
|
|
h->step = UPGRADE_STEP_SPLIT;
|
|
|
}
|
|
|
else {
|
|
|
- LOG("___ %s not exist, ", tmp);
|
|
|
+ LOGD("___ %s not exist, ", tmp);
|
|
|
|
|
|
//下载目录不存在pdupkg文件,则继续检查是否存在pdutar文件
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
- LOG("read %s\n", tmp);
|
|
|
+ LOGD("read %s\n", tmp);
|
|
|
|
|
|
if(file_exist(tmp)) {
|
|
|
file_touch(UPGRADE_STEP_DOWN);
|
|
|
@@ -653,7 +653,7 @@ static int upg_handle(upg_handle_t *h)
|
|
|
|
|
|
case UPGRADE_STEP_SPLIT:
|
|
|
{
|
|
|
- LOG("___ UPGRADE_STEP_SPLIT\n");
|
|
|
+ LOGD("___ UPGRADE_STEP_SPLIT\n");
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
|
|
|
r = file_check(tmp);
|
|
|
@@ -678,7 +678,7 @@ static int upg_handle(upg_handle_t *h)
|
|
|
|
|
|
case UPGRADE_STEP_COVER:
|
|
|
{
|
|
|
- LOG("___ UPGRADE_STEP_COVER\n");
|
|
|
+ LOGD("___ UPGRADE_STEP_COVER\n");
|
|
|
app_stop();
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
@@ -689,7 +689,7 @@ static int upg_handle(upg_handle_t *h)
|
|
|
app_start();
|
|
|
}
|
|
|
else {
|
|
|
- LOG("file_cover, delete %s!\n", tmp);
|
|
|
+ LOGD("file_cover, delete %s!\n", tmp);
|
|
|
file_remove(tmp);
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
|
|
|
file_remove(tmp);
|
|
|
@@ -707,14 +707,14 @@ static int upg_handle(upg_handle_t *h)
|
|
|
|
|
|
case UPGRADE_STEP_FINISH:
|
|
|
{
|
|
|
- LOG("___ UPGRADE_STEP_FINISH\n");
|
|
|
+ LOGD("___ UPGRADE_STEP_FINISH\n");
|
|
|
h->step = UPGRADE_STEP_NONE;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case UPGRADE_STEP_SUCCESS:
|
|
|
{
|
|
|
- LOG("___ UPGRADE_STEP_SUCCESS\n");
|
|
|
+ LOGD("___ UPGRADE_STEP_SUCCESS\n");
|
|
|
|
|
|
h->step = UPGRADE_STEP_NONE;
|
|
|
|
|
|
@@ -727,7 +727,7 @@ static int upg_handle(upg_handle_t *h)
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
- LOG("___ wrong step: %d\n", h->step);
|
|
|
+ LOGD("___ wrong step: %d\n", h->step);
|
|
|
r = 1;
|
|
|
break;
|
|
|
}
|
|
|
@@ -741,7 +741,7 @@ int main(void)
|
|
|
int id,r=0;
|
|
|
upg_handle_t *h=&upgHandle;
|
|
|
|
|
|
- LOG("___ upgd running ...\n");
|
|
|
+ LOGD("___upgrade running ...\n");
|
|
|
|
|
|
upg_init(h);
|
|
|
upg_check(h);
|