#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include "wefota_device.h" | |
static int g_first_check_tesk = 0; | |
int get_iccid(char *iccid) | |
{ | |
if (iccid == NULL) { | |
return -1; | |
} | |
char buf[32] = {0} ; | |
cfg_get_item("sim_iccid", buf, sizeof(buf)); | |
strcpy(iccid, buf); | |
return 0; | |
} | |
int get_imei(char *imei) | |
{ | |
if (imei == NULL) { | |
return -1; | |
} | |
char buf[32] = {0}; | |
cfg_get_item("imei", buf, sizeof(buf)); | |
strcpy(imei, buf); | |
return 0; | |
} | |
int get_version(char *version) | |
{ | |
if (version == NULL) { | |
return -1; | |
} | |
char buf[30] = {0}; | |
cfg_get_item("wa_version", buf, sizeof(buf)); | |
strcpy(version, buf); | |
return 0; | |
} | |
int is_data_connected(void) | |
{ | |
char buf[32] = {0}; | |
cfg_get_item("ppp_status", buf, sizeof(buf)); | |
if (strcmp(buf, "ppp_connected") == 0) { | |
return 1; | |
} | |
return 0; | |
} | |
int get_wefota_server1_cfg(char *ip, int *port) | |
{ | |
if (ip == NULL || port == NULL) { | |
return -1; | |
} | |
char buf[32] = {0}; | |
cfg_get_item("wefota_server1_ip", buf, sizeof(buf)); | |
strcpy(ip, buf); | |
cfg_get_item("wefota_server1_port", buf, sizeof(buf)); | |
*port = atoi(buf); | |
return 0; | |
} | |
int set_wefota_server2_cfg(const char *ip, int port) | |
{ | |
if (ip == NULL) { | |
return -1; | |
} | |
cfg_set("wefota_server2_ip", ip); | |
char buf[32] = {0}; | |
sprintf(buf, "%d", port); | |
cfg_set("wefota_server2_port", buf); | |
return 0; | |
} | |
int get_last_work_time(int *time, int *interval) | |
{ | |
if (time == NULL || interval == NULL) { | |
return -1; | |
} | |
char buf[32] = {0}; | |
cfg_get_item("wefota_last_work_time", buf, sizeof(buf)); | |
*time = atoi(buf); | |
cfg_get_item("wefota_last_work_interval", buf, sizeof(buf)); | |
*interval = atoi(buf); | |
return 0; | |
} | |
int set_last_work_time(int time, int interval) | |
{ | |
char buf[32] = {0}; | |
sprintf(buf, "%d", time); | |
cfg_set("wefota_last_work_time", buf); | |
sprintf(buf, "%d", interval); | |
cfg_set("wefota_last_work_interval", buf); | |
return 0; | |
} | |
static int fota_is_file_exist(const char* path) | |
{ | |
if ( (path == NULL) || (*path == '\0') ) | |
return 0; | |
if (access(path, R_OK) != 0) | |
return 0; | |
return 1; | |
} | |
static int fota_read_file(const char*path, char*buf, size_t sz) | |
{ | |
int fd = -1; | |
size_t cnt; | |
fd = open(path, O_RDONLY, 0); | |
if(fd < 0) | |
{ | |
printf("fota_read_file failed to open %s\n", path); | |
cnt = -1; | |
return cnt; | |
} | |
cnt = read(fd, buf, sz - 1); | |
if(cnt <= 0) | |
{ | |
printf("failed to read %s\n", path); | |
close(fd); | |
cnt = -1; | |
return cnt; | |
} | |
buf[cnt] = '\0'; | |
if(buf[cnt - 1] == '\n') | |
{ | |
cnt--; | |
buf[cnt] = '\0'; | |
} | |
close(fd); | |
return cnt; | |
} | |
static int fota_read_file_int(const char* path, int *val) | |
{ | |
char buf[32]; | |
char *end; | |
int ret; | |
int tmp; | |
ret = fota_read_file(path, buf, sizeof(buf)); | |
if(ret < 0) | |
return -1; | |
tmp = strtol(buf, &end, 0); | |
if ((end == buf) || ((end < buf + sizeof(buf)) && (*end != '\0'))) | |
{ | |
return -1; | |
} | |
*val = tmp; | |
return 0; | |
} | |
static int fota_get_update_status(int *fota_status) | |
{ | |
int status = 0; | |
int ret = 0; | |
if(!fota_is_file_exist(FOTA_UPDATE_STATUS_FILE)) | |
{ | |
*fota_status = -1; | |
return -1; | |
} | |
ret = fota_read_file_int(FOTA_UPDATE_STATUS_FILE, &status); | |
if(ret < 0) { | |
*fota_status = -1; | |
return -1; | |
} | |
*fota_status = status; | |
return 0; | |
} | |
int start_wefota_install(void) | |
{ | |
int upgradeStatus = 0; | |
int result = 0; | |
system("fota_upi -u verify"); | |
result = fota_get_update_status(&upgradeStatus); | |
if(result < 0) | |
{ | |
printf("failed to read the update_status file\n"); | |
return -1; | |
} | |
else if(upgradeStatus != 0) | |
{ | |
printf("verify failed\n"); | |
return -1; | |
} | |
else | |
{ | |
printf("verify success, start upgrade!\n"); | |
system("fota_upi -u recovery"); | |
} | |
return 0; | |
} | |
int wait_fota_conditions(void) | |
{ | |
// wait for data connected | |
while (1) { | |
if (is_data_connected()) { | |
break; | |
} | |
sleep(10); | |
} | |
// wait for sntp time sync | |
sleep(10); | |
srand(time(NULL)); | |
// wait for fota time | |
while (1) { | |
int last_time = 0; | |
int interval = 0; | |
get_last_work_time(&last_time, &interval); | |
int cur_time = time(NULL); | |
if ((last_time + interval < cur_time) || g_first_check_tesk == 0) { | |
break; | |
} | |
sleep(10); | |
} | |
g_first_check_tesk = 1; | |
//set last work time | |
int interval = FOTA_INTERVAL_MIN + rand() % FOTA_INTERVAL_RANDOM; | |
set_last_work_time(time(NULL), interval); | |
return 0; | |
} |