mj.qu | f47abdc | 2025-01-14 17:23:20 -0800 | [diff] [blame^] | 1 | #include <stdio.h>
|
| 2 | #include <string.h>
|
| 3 | #include <stdlib.h>
|
| 4 | #include <time.h>
|
| 5 | #include <unistd.h>
|
| 6 | #include <fcntl.h>
|
| 7 | #include "wefota_device.h"
|
| 8 |
|
| 9 | static int g_first_check_tesk = 0;
|
| 10 |
|
| 11 | int get_iccid(char *iccid)
|
| 12 | {
|
| 13 | if (iccid == NULL) {
|
| 14 | return -1;
|
| 15 | }
|
| 16 | char buf[32] = {0} ;
|
| 17 | cfg_get_item("sim_iccid", buf, sizeof(buf));
|
| 18 | strcpy(iccid, buf);
|
| 19 | return 0;
|
| 20 | }
|
| 21 |
|
| 22 | int get_imei(char *imei)
|
| 23 | {
|
| 24 | if (imei == NULL) {
|
| 25 | return -1;
|
| 26 | }
|
| 27 | char buf[32] = {0};
|
| 28 | cfg_get_item("imei", buf, sizeof(buf));
|
| 29 | strcpy(imei, buf);
|
| 30 | return 0;
|
| 31 | }
|
| 32 |
|
| 33 | int get_version(char *version)
|
| 34 | {
|
| 35 | if (version == NULL) {
|
| 36 | return -1;
|
| 37 | }
|
| 38 | char buf[30] = {0};
|
| 39 | cfg_get_item("wa_version", buf, sizeof(buf));
|
| 40 | strcpy(version, buf);
|
| 41 | return 0;
|
| 42 | }
|
| 43 |
|
| 44 | int is_data_connected(void)
|
| 45 | {
|
| 46 | char buf[32] = {0};
|
| 47 | cfg_get_item("ppp_status", buf, sizeof(buf));
|
| 48 | if (strcmp(buf, "ppp_connected") == 0) {
|
| 49 | return 1;
|
| 50 | }
|
| 51 | return 0;
|
| 52 | }
|
| 53 |
|
| 54 | int get_wefota_server1_cfg(char *ip, int *port)
|
| 55 | {
|
| 56 | if (ip == NULL || port == NULL) {
|
| 57 | return -1;
|
| 58 | }
|
| 59 | char buf[32] = {0};
|
| 60 | cfg_get_item("wefota_server1_ip", buf, sizeof(buf));
|
| 61 | strcpy(ip, buf);
|
| 62 | cfg_get_item("wefota_server1_port", buf, sizeof(buf));
|
| 63 | *port = atoi(buf);
|
| 64 | return 0;
|
| 65 | }
|
| 66 |
|
| 67 | int set_wefota_server2_cfg(const char *ip, int port)
|
| 68 | {
|
| 69 | if (ip == NULL) {
|
| 70 | return -1;
|
| 71 | }
|
| 72 | cfg_set("wefota_server2_ip", ip);
|
| 73 | char buf[32] = {0};
|
| 74 | sprintf(buf, "%d", port);
|
| 75 | cfg_set("wefota_server2_port", buf);
|
| 76 | return 0;
|
| 77 | }
|
| 78 |
|
| 79 | int get_last_work_time(int *time, int *interval)
|
| 80 | {
|
| 81 | if (time == NULL || interval == NULL) {
|
| 82 | return -1;
|
| 83 | }
|
| 84 | char buf[32] = {0};
|
| 85 | cfg_get_item("wefota_last_work_time", buf, sizeof(buf));
|
| 86 | *time = atoi(buf);
|
| 87 | cfg_get_item("wefota_last_work_interval", buf, sizeof(buf));
|
| 88 | *interval = atoi(buf);
|
| 89 | return 0;
|
| 90 | }
|
| 91 |
|
| 92 | int set_last_work_time(int time, int interval)
|
| 93 | {
|
| 94 | char buf[32] = {0};
|
| 95 | sprintf(buf, "%d", time);
|
| 96 | cfg_set("wefota_last_work_time", buf);
|
| 97 | sprintf(buf, "%d", interval);
|
| 98 | cfg_set("wefota_last_work_interval", buf);
|
| 99 | return 0;
|
| 100 | }
|
| 101 |
|
| 102 | static int fota_is_file_exist(const char* path)
|
| 103 | {
|
| 104 | if ( (path == NULL) || (*path == '\0') )
|
| 105 | return 0;
|
| 106 | if (access(path, R_OK) != 0)
|
| 107 | return 0;
|
| 108 |
|
| 109 | return 1;
|
| 110 | }
|
| 111 |
|
| 112 | static int fota_read_file(const char*path, char*buf, size_t sz)
|
| 113 | {
|
| 114 | int fd = -1;
|
| 115 | size_t cnt;
|
| 116 |
|
| 117 | fd = open(path, O_RDONLY, 0);
|
| 118 | if(fd < 0)
|
| 119 | {
|
| 120 | printf("fota_read_file failed to open %s\n", path);
|
| 121 | cnt = -1;
|
| 122 | return cnt;
|
| 123 | }
|
| 124 | cnt = read(fd, buf, sz - 1);
|
| 125 | if(cnt <= 0)
|
| 126 | {
|
| 127 | printf("failed to read %s\n", path);
|
| 128 | close(fd);
|
| 129 | cnt = -1;
|
| 130 | return cnt;
|
| 131 | }
|
| 132 | buf[cnt] = '\0';
|
| 133 | if(buf[cnt - 1] == '\n')
|
| 134 | {
|
| 135 | cnt--;
|
| 136 | buf[cnt] = '\0';
|
| 137 | }
|
| 138 | close(fd);
|
| 139 |
|
| 140 | return cnt;
|
| 141 | }
|
| 142 |
|
| 143 | static int fota_read_file_int(const char* path, int *val)
|
| 144 | {
|
| 145 | char buf[32];
|
| 146 | char *end;
|
| 147 | int ret;
|
| 148 | int tmp;
|
| 149 |
|
| 150 | ret = fota_read_file(path, buf, sizeof(buf));
|
| 151 | if(ret < 0)
|
| 152 | return -1;
|
| 153 |
|
| 154 | tmp = strtol(buf, &end, 0);
|
| 155 | if ((end == buf) || ((end < buf + sizeof(buf)) && (*end != '\0')))
|
| 156 | {
|
| 157 | return -1;
|
| 158 | }
|
| 159 |
|
| 160 | *val = tmp;
|
| 161 |
|
| 162 | return 0;
|
| 163 | }
|
| 164 |
|
| 165 | static int fota_get_update_status(int *fota_status)
|
| 166 | {
|
| 167 |
|
| 168 | int status = 0;
|
| 169 | int ret = 0;
|
| 170 | if(!fota_is_file_exist(FOTA_UPDATE_STATUS_FILE))
|
| 171 | {
|
| 172 | *fota_status = -1;
|
| 173 | return -1;
|
| 174 | }
|
| 175 | ret = fota_read_file_int(FOTA_UPDATE_STATUS_FILE, &status);
|
| 176 | if(ret < 0) {
|
| 177 | *fota_status = -1;
|
| 178 | return -1;
|
| 179 | }
|
| 180 | *fota_status = status;
|
| 181 | return 0;
|
| 182 | }
|
| 183 |
|
| 184 | int start_wefota_install(void)
|
| 185 | {
|
| 186 | int upgradeStatus = 0;
|
| 187 | int result = 0;
|
| 188 |
|
| 189 | system("fota_upi -u verify");
|
| 190 | result = fota_get_update_status(&upgradeStatus);
|
| 191 | if(result < 0)
|
| 192 | {
|
| 193 | printf("failed to read the update_status file\n");
|
| 194 | return -1;
|
| 195 | }
|
| 196 | else if(upgradeStatus != 0)
|
| 197 | {
|
| 198 | printf("verify failed\n");
|
| 199 | return -1;
|
| 200 | }
|
| 201 | else
|
| 202 | {
|
| 203 | printf("verify success, start upgrade!\n");
|
| 204 | system("fota_upi -u recovery");
|
| 205 | }
|
| 206 |
|
| 207 | return 0;
|
| 208 | }
|
| 209 |
|
| 210 | int wait_fota_conditions(void)
|
| 211 | {
|
| 212 | // wait for data connected
|
| 213 | while (1) {
|
| 214 | if (is_data_connected()) {
|
| 215 | break;
|
| 216 | }
|
| 217 | sleep(10);
|
| 218 | }
|
| 219 |
|
| 220 | // wait for sntp time sync
|
| 221 | sleep(10);
|
| 222 | srand(time(NULL));
|
| 223 |
|
| 224 | // wait for fota time
|
| 225 | while (1) {
|
| 226 | int last_time = 0;
|
| 227 | int interval = 0;
|
| 228 | get_last_work_time(&last_time, &interval);
|
| 229 | int cur_time = time(NULL);
|
| 230 | if ((last_time + interval < cur_time) || g_first_check_tesk == 0) {
|
| 231 | break;
|
| 232 | }
|
| 233 | sleep(10);
|
| 234 | }
|
| 235 |
|
| 236 | g_first_check_tesk = 1;
|
| 237 | //set last work time
|
| 238 | int interval = FOTA_INTERVAL_MIN + rand() % FOTA_INTERVAL_RANDOM;
|
| 239 | set_last_work_time(time(NULL), interval);
|
| 240 |
|
| 241 | return 0;
|
| 242 | }
|