zw.wang | 96c5d4e | 2025-07-01 11:35:13 +0800 | [diff] [blame^] | 1 | #include <stdio.h>
|
| 2 | #include <string.h>
|
| 3 | #include <stdlib.h>
|
| 4 | #include <unistd.h>
|
| 5 | #include <fcntl.h>
|
| 6 | #include "wefota_main.h"
|
| 7 | #include "wefota_socket.h"
|
| 8 | #include "wefota_device.h"
|
| 9 |
|
| 10 | #define RECV_WHOLE_PACK 1
|
| 11 |
|
| 12 | static FOTA_Status s_status = FOTA_STATUS_IDLE;
|
| 13 | static struct sockaddr_in s_server1_addr = {0};
|
| 14 | static struct sockaddr_in s_server2_addr = {0};
|
| 15 | static unsigned char s_req[WEFOTA_REQ_MAX_LEN] = {0};
|
| 16 | static unsigned char s_resp[WEFOTA_RESP_MAX_LEN] = {0};
|
| 17 | static char s_destVersion[32] = {0};
|
| 18 | static VerID s_verID = {0};
|
| 19 | static DiffPackInfo s_diffpack_info = {0};
|
| 20 | static unsigned int s_costTime = 0;
|
| 21 | static unsigned int s_pack_date_count = 0;
|
| 22 | static DiffPackDataReq s_diffpack_data_req = {0};
|
| 23 |
|
| 24 | const WeFOTA_MSG s_wefota_msg_table[] = {
|
| 25 | {FOTA_STATUS_IDLE, 0xFF, 0, 0x00, 0}, // no use
|
| 26 | {FOTA_STATUS_GET_IP_1, 0x10, 0, 0x11, 18},
|
| 27 | {FOTA_STATUS_CHECK_TASK, 0xBF, 32, 0xC0, 41},
|
| 28 | {FOTA_STATUS_GET_IP_2, 0x10, 0, 0x11, 18},
|
| 29 | {FOTA_STATUS_GET_VER_ID, 0x04, 68, 0x05, 12},
|
| 30 | {FOTA_STATUS_GET_DIFF_PACK_ID, 0x00, 12, 0x02, 35},
|
| 31 | {FOTA_STATUS_GET_DIFF_PACK_DATA, 0x01, 23, 0x03, WEFOTA_RESP_DIFF_PACK_DATA_MAX_LEN},
|
| 32 | {FOTA_STATUS_GET_DIFF_PACK_END, 0x30, 19, 0x33, 0},
|
| 33 | {FOTA_STATUS_INSTALL_DIFF_PACK, 0xFF, 0, 0x00, 0}, // no use
|
| 34 | {FOTA_STATUS_MAX, 0xFF, 0, 0x00, 0}, // no use
|
| 35 | };
|
| 36 |
|
| 37 | void set_fota_status(FOTA_Status status) {
|
| 38 | s_status = status;
|
| 39 | }
|
| 40 |
|
| 41 | FOTA_Status get_fota_status(void) {
|
| 42 | return s_status;
|
| 43 | }
|
| 44 |
|
| 45 | int wefota_header_init(WeFOTAHeader *hdr) {
|
| 46 | if(NULL == hdr) {
|
| 47 | return -1;
|
| 48 | }
|
| 49 |
|
| 50 | memset(hdr, 0, sizeof(WeFOTAHeader));
|
| 51 | hdr->magic = WEFOTA_HEADER_MAGIC; // maybe need htonl
|
| 52 | hdr->protocol = WEFOTA_HEADER_PROTOCOL;
|
| 53 | get_iccid(hdr->iccid);
|
| 54 | get_imei(hdr->devid + 16);
|
| 55 | hdr->tid = WEFOTA_HEADER_TID;
|
| 56 |
|
| 57 | hdr->code = s_wefota_msg_table[s_status].req_code;
|
| 58 | int count = s_wefota_msg_table[s_status].req_count;
|
| 59 | hdr->count = htons(count);
|
| 60 |
|
| 61 | printf("header:[imei:%s]\n", hdr->devid + 16);
|
| 62 | return count;
|
| 63 | }
|
| 64 |
|
| 65 | int is_valid_resp_hdr(const WeFOTAHeader *hdr) {
|
| 66 | if(NULL == hdr) {
|
| 67 | return 0;
|
| 68 | }
|
| 69 | if(hdr->magic != WEFOTA_HEADER_MAGIC && hdr->magic != htonl(WEFOTA_HEADER_MAGIC)) {
|
| 70 | return 0;
|
| 71 | }
|
| 72 | if(hdr->protocol != WEFOTA_HEADER_PROTOCOL) {
|
| 73 | return 0;
|
| 74 | }
|
| 75 |
|
| 76 | return 1;
|
| 77 | }
|
| 78 |
|
| 79 | int init_wefota_server1_addr(void) {
|
| 80 | char ip[16] = {0};
|
| 81 | int port = 0;
|
| 82 | get_wefota_server1_cfg(ip, &port);
|
| 83 | return init_server_addr(&s_server1_addr, ip, port);
|
| 84 | }
|
| 85 |
|
| 86 | int init_wefota_server2_addr(const char *ip, int port) {
|
| 87 | int ret = init_server_addr(&s_server2_addr, ip, port);
|
| 88 | if(0 == ret) {
|
| 89 | set_wefota_server2_cfg(ip, port);
|
| 90 | }
|
| 91 | return ret;
|
| 92 | }
|
| 93 |
|
| 94 | int recv_with_retry(int sock, struct sockaddr_in* server_addr,
|
| 95 | unsigned char* response, size_t response_size, int max_retries) {
|
| 96 | if (NULL == response || response_size < WEFOTA_HEADER_SIZE) {
|
| 97 | return -1;
|
| 98 | }
|
| 99 | int retry_count = 0;
|
| 100 | int recv_date_err = 0;
|
| 101 | while (retry_count < max_retries) {
|
| 102 | do {
|
| 103 | recv_date_err = 0;
|
| 104 | WeFOTAHeader *hdr = (WeFOTAHeader *)response;
|
| 105 | int received = receive_message(sock, hdr, response_size, server_addr);
|
| 106 | printf("received=%d\n", received);
|
| 107 | if (received < WEFOTA_HEADER_SIZE) {
|
| 108 | printf("recv header failed\n");
|
| 109 | break;
|
| 110 | }
|
| 111 | if (0 == is_valid_resp_hdr(hdr)) {
|
| 112 | printf("recv invalid header\n");
|
| 113 | break;
|
| 114 | }
|
| 115 | printf("recv code=%d, count=%d)\n", hdr->code, hdr->count);
|
| 116 | if (hdr->count + WEFOTA_HEADER_SIZE != received) {
|
| 117 | printf("recv count error\n");
|
| 118 | break;
|
| 119 | }
|
| 120 |
|
| 121 | if (s_status == FOTA_STATUS_GET_DIFF_PACK_DATA)
|
| 122 | {
|
| 123 | if (hdr->code == s_wefota_msg_table[s_status].resp_code && hdr->count == s_pack_date_count)
|
| 124 | {
|
| 125 | DiffPackDataResp * data = (DiffPackDataResp *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 126 | printf("proc_get_diff_pack_data recv [data->offset %u offset:%u][data->length %u length:%u]\n",
|
| 127 | data->offset, s_diffpack_data_req.offset, data->length, s_diffpack_data_req.length);
|
| 128 | if (data->offset == s_diffpack_data_req.offset || data->length == s_diffpack_data_req.length) {
|
| 129 | return received;
|
| 130 | }
|
| 131 | }
|
| 132 | printf("proc_get_diff_pack_data recv wrong data\n");
|
| 133 | recv_date_err = 1;
|
| 134 | break;
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | if (hdr->code == s_wefota_msg_table[s_status].resp_code
|
| 139 | && hdr->count == s_wefota_msg_table[s_status].resp_count) { // code and count right
|
| 140 | return received;
|
| 141 | } else { // code or count wrong
|
| 142 | break;
|
| 143 | }
|
| 144 | }
|
| 145 | }while(0);
|
| 146 |
|
| 147 | printf("retry (%d/%d)\n", retry_count + 1, max_retries);
|
| 148 | if (recv_date_err == 0)
|
| 149 | {
|
| 150 | retry_count++;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | return -1;
|
| 155 | }
|
| 156 |
|
| 157 | #ifdef RECV_WHOLE_PACK
|
| 158 | int send_with_retry(int sock, const unsigned char* msg, size_t len, struct sockaddr_in* server_addr,
|
| 159 | unsigned char* response, size_t response_size, int max_retries) {
|
| 160 | if (NULL == msg || NULL == response || 0 == len || response_size < WEFOTA_HEADER_SIZE) {
|
| 161 | return -1;
|
| 162 | }
|
| 163 | int retry_count = 0;
|
| 164 | while (retry_count < max_retries) {
|
| 165 | do {
|
| 166 | if (send_message(sock, msg, len, server_addr) < 0) {
|
| 167 | printf("send message failed\n");
|
| 168 | break;
|
| 169 | }
|
| 170 | #if 0
|
| 171 | WeFOTAHeader *hdr = (WeFOTAHeader *)response;;
|
| 172 | int received = receive_message(sock, hdr, response_size, server_addr);
|
| 173 | printf("received=%d\n", received);
|
| 174 | if (received < WEFOTA_HEADER_SIZE) {
|
| 175 | printf("recv header failed\n");
|
| 176 | break;
|
| 177 | }
|
| 178 | if (0 == is_valid_resp_hdr(hdr)) {
|
| 179 | printf("recv invalid header\n");
|
| 180 | break;
|
| 181 | }
|
| 182 | printf("recv code=%d, count=%d)\n", hdr->code, hdr->count);
|
| 183 | if (hdr->count + WEFOTA_HEADER_SIZE != received) {
|
| 184 | printf("recv count error\n");
|
| 185 | break;
|
| 186 | }
|
| 187 |
|
| 188 | if (s_status == FOTA_STATUS_GET_DIFF_PACK_DATA)
|
| 189 | {
|
| 190 | if (hdr->code == s_wefota_msg_table[s_status].resp_code && hdr->count == s_pack_date_count)
|
| 191 | {
|
| 192 | return received;
|
| 193 | }
|
| 194 | else
|
| 195 | {
|
| 196 | return -1;
|
| 197 | }
|
| 198 | }
|
| 199 | else
|
| 200 | {
|
| 201 | if (hdr->code == s_wefota_msg_table[s_status].resp_code
|
| 202 | && hdr->count == s_wefota_msg_table[s_status].resp_count) { // code and count right
|
| 203 | return received;
|
| 204 | } else { // code or count wrong
|
| 205 | return -1;
|
| 206 | }
|
| 207 | }
|
| 208 | #endif
|
| 209 | int received = recv_with_retry(sock, server_addr, response, response_size, max_retries);
|
| 210 | if (received > 0)
|
| 211 | {
|
| 212 | return received;
|
| 213 | }
|
| 214 | }while(0);
|
| 215 |
|
| 216 | printf("retry (%d/%d)\n", retry_count + 1, max_retries);
|
| 217 | retry_count++;
|
| 218 | sleep(WEFOTA_TIMEOUT_SEC);
|
| 219 | }
|
| 220 |
|
| 221 | return -1;
|
| 222 | }
|
| 223 | #else
|
| 224 | int send_with_retry(int sock, const unsigned char* msg, size_t len, struct sockaddr_in* server_addr,
|
| 225 | unsigned char* response, size_t response_size, int max_retries) {
|
| 226 | if (NULL == msg || NULL == response || 0 == len || response_size < WEFOTA_HEADER_SIZE) {
|
| 227 | return -1;
|
| 228 | }
|
| 229 | int retry_count = 0;
|
| 230 | while (retry_count < max_retries) {
|
| 231 | do {
|
| 232 | if (send_message(sock, msg, len, server_addr) < 0) {
|
| 233 | printf("send message failed\n");
|
| 234 | break;
|
| 235 | }
|
| 236 |
|
| 237 | WeFOTAHeader *hdr = (WeFOTAHeader *)response;;
|
| 238 | int received = receive_message(sock, hdr, WEFOTA_HEADER_SIZE, server_addr);
|
| 239 | printf("received=%d\n", received);
|
| 240 | if (received != WEFOTA_HEADER_SIZE) {
|
| 241 | printf("recv header failed\n");
|
| 242 | break;
|
| 243 | }
|
| 244 | if (0 == is_valid_resp_hdr(hdr)) {
|
| 245 | printf("recv invalid header\n");
|
| 246 | break;
|
| 247 | }
|
| 248 | printf("recv code=%d, count=%d)\n", hdr->code, hdr->count);
|
| 249 | if (hdr->count + WEFOTA_HEADER_SIZE > response_size) {
|
| 250 | printf("recv count > response_size\n");
|
| 251 | break;
|
| 252 | }
|
| 253 |
|
| 254 | if (hdr->code == s_wefota_msg_table[s_status].resp_code
|
| 255 | && hdr->count == s_wefota_msg_table[s_status].resp_count) { // code and count right
|
| 256 | if (hdr->count == 0) {
|
| 257 | return 0;
|
| 258 | } else {
|
| 259 | received = receive_message(sock, response + WEFOTA_HEADER_SIZE, hdr->count, server_addr);
|
| 260 | printf("recv data len=%d\n", received);
|
| 261 | return received;
|
| 262 | }
|
| 263 | } else { // code or count wrong
|
| 264 | if (hdr->count == 0) {
|
| 265 | return -1;
|
| 266 | } else {
|
| 267 | received = receive_message(sock, response + WEFOTA_HEADER_SIZE, hdr->count, server_addr);
|
| 268 | printf("recv data len=%d, drop\n", received);
|
| 269 | return -1;
|
| 270 | }
|
| 271 | }
|
| 272 | }while(0);
|
| 273 |
|
| 274 | printf("retry (%d/%d)\n", retry_count + 1, max_retries);
|
| 275 | retry_count++;
|
| 276 | sleep(WEFOTA_TIMEOUT_SEC);
|
| 277 | }
|
| 278 |
|
| 279 | return -1;
|
| 280 | }
|
| 281 | #endif
|
| 282 |
|
| 283 | int proc_get_ip(int sock) {
|
| 284 | int ret = -1;
|
| 285 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 286 | int count = wefota_header_init(hdr);
|
| 287 | printf("proc_get_ip send code=%d, count=%d)\n", hdr->code, count);
|
| 288 |
|
| 289 | if (0 > send_with_retry(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server1_addr, s_resp, sizeof(s_resp), WEFOTA_MAX_RETRIES)) {
|
| 290 | printf("comm fail\n");
|
| 291 | return ret;
|
| 292 | }
|
| 293 |
|
| 294 | ServerAddr * data = (ServerAddr *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 295 | ret = init_wefota_server2_addr(data->ip, (int)data->port);
|
| 296 | return ret;
|
| 297 | }
|
| 298 |
|
| 299 | int proc_get_ip_1(int sock) {
|
| 300 | set_fota_status(FOTA_STATUS_GET_IP_1);
|
| 301 | return proc_get_ip(sock);
|
| 302 | }
|
| 303 |
|
| 304 | int proc_check_task(int sock) {
|
| 305 | set_fota_status(FOTA_STATUS_CHECK_TASK);
|
| 306 |
|
| 307 | int ret = -1;
|
| 308 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 309 | printf("proc_check_task header\n");
|
| 310 | int count = wefota_header_init(hdr);
|
| 311 | printf("proc_check_task send code=%d, count=%d)\n", hdr->code, count);
|
| 312 |
|
| 313 | char originVersion [32] = {0};
|
| 314 | get_version(originVersion);
|
| 315 | memcpy(s_req + WEFOTA_HEADER_SIZE, originVersion, sizeof(originVersion));
|
| 316 | printf("proc_check_task originVersion:[%s]\n", originVersion);
|
| 317 |
|
| 318 | if (0 > send_with_retry(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr, s_resp, sizeof(s_resp), WEFOTA_MAX_RETRIES)) {
|
| 319 | printf("comm fail\n");
|
| 320 | return ret;
|
| 321 | }
|
| 322 |
|
| 323 | FotaTask * data = (FotaTask *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 324 | printf("proc_check_task recv:[flag:%d][interval:%u][delay:%u][destVersion:%s]\n", data->flag, data->interval, data->delay, data->destVersion);
|
| 325 | if (data->flag == 1) {
|
| 326 | strncpy(s_destVersion, data->destVersion, sizeof(s_destVersion));
|
| 327 | printf("exist fota task\n");
|
| 328 | return 0;
|
| 329 | } else {
|
| 330 | printf("no fota task\n");
|
| 331 | return ret;
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | int proc_get_ip_2(int sock) {
|
| 336 | set_fota_status(FOTA_STATUS_GET_IP_2);
|
| 337 | return proc_get_ip(sock);
|
| 338 | }
|
| 339 |
|
| 340 | int proc_get_ver_id(int sock) {
|
| 341 | set_fota_status(FOTA_STATUS_GET_VER_ID);
|
| 342 |
|
| 343 | char product_id[32] = {0};
|
| 344 | int ret = -1;
|
| 345 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 346 | int count = wefota_header_init(hdr);
|
| 347 | printf("proc_get_ver_id send code=%d, count=%d)\n", hdr->code, count);
|
| 348 |
|
| 349 | Version version = {0};
|
| 350 | get_version(version.orig_version);
|
| 351 | strncpy(version.dest_version, s_destVersion, sizeof(version.dest_version));
|
| 352 | cfg_get_item("wefota_product_id", product_id, sizeof(product_id));
|
| 353 | strcpy(version.product, product_id);
|
| 354 | memcpy(s_req + WEFOTA_HEADER_SIZE, &version, sizeof(version));
|
| 355 |
|
| 356 | if (0 > send_with_retry(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr, s_resp, sizeof(s_resp), WEFOTA_MAX_RETRIES)) {
|
| 357 | printf("comm fail\n");
|
| 358 | return ret;
|
| 359 | }
|
| 360 |
|
| 361 | VerID * data = (VerID *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 362 | s_verID = *data;
|
| 363 | return 0;
|
| 364 | }
|
| 365 |
|
| 366 | int proc_get_diff_pack_id(int sock) {
|
| 367 | set_fota_status(FOTA_STATUS_GET_DIFF_PACK_ID);
|
| 368 |
|
| 369 | int ret = -1;
|
| 370 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 371 | int count = wefota_header_init(hdr);
|
| 372 | printf("proc_get_diff_pack_id send code=%d, count=%d)\n", hdr->code, count);
|
| 373 |
|
| 374 | memcpy(s_req + WEFOTA_HEADER_SIZE, &s_verID, sizeof(s_verID));
|
| 375 |
|
| 376 | if (0 > send_with_retry(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr, s_resp, sizeof(s_resp), WEFOTA_MAX_RETRIES)) {
|
| 377 | printf("comm fail\n");
|
| 378 | return ret;
|
| 379 | }
|
| 380 |
|
| 381 | DiffPackInfo * data = (DiffPackInfo *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 382 | s_diffpack_info = *data;
|
| 383 | printf("proc_get_diff_pack_id [diffPackID:%s][MD5:%s][size:%u]\n", s_diffpack_info.diffPackID, s_diffpack_info.MD5, s_diffpack_info.size);
|
| 384 | if (s_diffpack_info.size == 0) {
|
| 385 | printf("diff pack size==0\n");
|
| 386 | return ret;
|
| 387 | }
|
| 388 | return 0;
|
| 389 | }
|
| 390 |
|
| 391 | #if 0
|
| 392 | int proc_get_diff_pack_data(int sock) {
|
| 393 | set_fota_status(FOTA_STATUS_GET_DIFF_PACK_DATA);
|
| 394 |
|
| 395 | unsigned int startTime = time(NULL);
|
| 396 | int ret = -1;
|
| 397 | int recv_packet_retry = 0;
|
| 398 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 399 | int count = wefota_header_init(hdr);
|
| 400 | printf("proc_get_diff_pack_data send code=%d, count=%d)\n", hdr->code, count);
|
| 401 | system("rm -rf /cache/zte_fota");
|
| 402 | system("mkdir /cache/zte_fota");
|
| 403 | FILE *fp = fopen(FOTA_DOWNLOAD_FILEPATH, "wb");
|
| 404 | if (NULL == fp) {
|
| 405 | printf("open file failed\n");
|
| 406 | return ret;
|
| 407 | }
|
| 408 |
|
| 409 | unsigned int size = s_diffpack_info.size;
|
| 410 | while (size > 0) {
|
| 411 | unsigned int len = size > WEFOTA_DIFF_PACK_DATA_MAX_LEN ? WEFOTA_DIFF_PACK_DATA_MAX_LEN : size;
|
| 412 | s_pack_date_count = len + 39;
|
| 413 | unsigned int offset = s_diffpack_info.size - size;
|
| 414 | DiffPackDataReq req = {0};
|
| 415 | memcpy(req.diffPackID, s_diffpack_info.diffPackID, sizeof(req.diffPackID));
|
| 416 | req.length = len;
|
| 417 | req.offset = offset;
|
| 418 | printf("proc_get_diff_pack_data request [offset %d][req.diffPackID %s][len %d][s_pack_date_count:%d][offset %d]\n",
|
| 419 | offset, req.diffPackID, len, s_pack_date_count, offset);
|
| 420 | memcpy(s_req + WEFOTA_HEADER_SIZE, &req, sizeof(req));
|
| 421 | s_diffpack_data_req = req;
|
| 422 | if (0 > send_with_retry(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr, s_resp, sizeof(s_resp), WEFOTA_MAX_RETRIES)) {
|
| 423 | printf("comm fail\n");
|
| 424 | fclose(fp);
|
| 425 | return ret;
|
| 426 | }
|
| 427 |
|
| 428 | DiffPackDataResp * data = (DiffPackDataResp *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 429 | printf("proc_get_diff_pack_data recv [data->offset %d offset:%d][data->length %d en:%d]\n", data->offset, offset, data->length, len);
|
| 430 | if (data->offset != offset || data->length != len) {
|
| 431 | printf("offset or len error\n");
|
| 432 | if (recv_packet_retry < 3)
|
| 433 | {
|
| 434 | printf("offset error, retry\n");
|
| 435 | recv_packet_retry++;
|
| 436 | continue;
|
| 437 | }
|
| 438 | fclose(fp);
|
| 439 | return ret;
|
| 440 | }
|
| 441 | fwrite(data->data, 1, len, fp);
|
| 442 | size -= len;
|
| 443 | recv_packet_retry = 0;
|
| 444 | }
|
| 445 |
|
| 446 | fclose(fp);
|
| 447 | s_costTime = time(NULL) - startTime;
|
| 448 | return 0;
|
| 449 | }
|
| 450 | #endif
|
| 451 |
|
| 452 | static void send_request(int sockfd, struct sockaddr_in *server_addr, int offset)
|
| 453 | {
|
| 454 | char request[16];
|
| 455 | snprintf(request, sizeof(request), "%d", offset);
|
| 456 | sendto(sockfd, request, strlen(request), 0, (struct sockaddr *)server_addr, sizeof(*server_addr));
|
| 457 | }
|
| 458 |
|
| 459 | static int receive_data(int sockfd, int filefd, struct sockaddr_in *server_addr, socklen_t addr_len, int *received)
|
| 460 | {
|
| 461 | ssize_t bytes_received;
|
| 462 | struct sockaddr_in from_addr;
|
| 463 | socklen_t from_len = sizeof(from_addr);
|
| 464 |
|
| 465 | bytes_received = recvfrom(sockfd, s_resp, WEFOTA_RESP_MAX_LEN, 0, (struct sockaddr *)&from_addr, &from_len);
|
| 466 | if (bytes_received < 0)
|
| 467 | {
|
| 468 | printf("receive_data received < 0\n");
|
| 469 | perror("recvfrom failed");
|
| 470 | return -1;
|
| 471 | }
|
| 472 |
|
| 473 | DiffPackDataResp * data = (DiffPackDataResp *)(s_resp + WEFOTA_HEADER_SIZE);
|
| 474 | int offset = data->offset / 1024;
|
| 475 |
|
| 476 | printf("receive_data received offset:%u\n", data->offset);
|
| 477 |
|
| 478 | if (received[offset])
|
| 479 | {
|
| 480 | return 0;
|
| 481 | }
|
| 482 |
|
| 483 | usleep(10000);
|
| 484 | if (lseek(filefd, offset * 1024, SEEK_SET) < 0)
|
| 485 | {
|
| 486 | printf("receive_data lseek error\n");
|
| 487 | perror("lseek failed");
|
| 488 | return -1;
|
| 489 | }
|
| 490 | if (write(filefd, data->data, bytes_received - WEFOTA_HEADER_SIZE - 39) < 0)
|
| 491 | {
|
| 492 | printf("receive_data write error\n");
|
| 493 | perror("write failed");
|
| 494 | return -1;
|
| 495 | }
|
| 496 |
|
| 497 | received[offset] = 1;
|
| 498 | printf("write packet %d,%u\n", offset, data->offset);
|
| 499 |
|
| 500 | return 0;
|
| 501 | }
|
| 502 |
|
| 503 | int proc_get_diff_pack_data(int sock)
|
| 504 | {
|
| 505 | set_fota_status(FOTA_STATUS_GET_DIFF_PACK_DATA);
|
| 506 |
|
| 507 | unsigned int startTime = time(NULL);
|
| 508 | int ret = -1;
|
| 509 | int i = 0;
|
| 510 | int recv_packet_retry = 0;
|
| 511 | int upgradeStatus = -1;
|
| 512 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 513 | int count = wefota_header_init(hdr);
|
| 514 | printf("proc_get_diff_pack_data send code=%d, count=%d)\n", hdr->code, count);
|
| 515 |
|
| 516 | fota_get_update_status(&upgradeStatus);
|
| 517 | printf("proc_get_diff_pack_data fota_get_update_status upgradeStatus=%d\n", upgradeStatus);
|
| 518 | if (upgradeStatus == 0)
|
| 519 | {
|
| 520 | if(fota_is_file_exist(FOTA_DOWNLOAD_FILEPATH))
|
| 521 | {
|
| 522 | printf("proc_get_diff_pack_data upgradeStatus=0, Continue upgrading!!\n");
|
| 523 | return FOTA_NEED_CONTINUE_PREVIOUS_UPGRADE;
|
| 524 | }
|
| 525 | }
|
| 526 |
|
| 527 | system("rm -rf /cache/zte_fota");
|
| 528 | system("mkdir /cache/zte_fota");
|
| 529 |
|
| 530 | int filefd = open(FOTA_DOWNLOAD_FILEPATH, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
| 531 | if (filefd < 0)
|
| 532 | {
|
| 533 | printf("proc_get_diff_pack_data open failed\n");
|
| 534 | perror("file open failed");
|
| 535 | return ret;
|
| 536 | }
|
| 537 |
|
| 538 | unsigned int size = s_diffpack_info.size;
|
| 539 | int total_packets = size / 1024;
|
| 540 | if (size % 1024 != 0)
|
| 541 | {
|
| 542 | total_packets += 1;
|
| 543 | }
|
| 544 | int received[total_packets];
|
| 545 | fd_set read_fds;
|
| 546 | int max_fd;
|
| 547 | struct timeval timeout;
|
| 548 | int select_result = 0;
|
| 549 | int max_retry_count = 0;
|
| 550 |
|
| 551 | memset(received, 0, sizeof(received));
|
| 552 | while (size > 0)
|
| 553 | {
|
| 554 | unsigned int len = size > WEFOTA_DIFF_PACK_DATA_MAX_LEN ? WEFOTA_DIFF_PACK_DATA_MAX_LEN : size;
|
| 555 | s_pack_date_count = len + 39;
|
| 556 | unsigned int offset = s_diffpack_info.size - size;
|
| 557 | DiffPackDataReq req = {0};
|
| 558 | memcpy(req.diffPackID, s_diffpack_info.diffPackID, sizeof(req.diffPackID));
|
| 559 | req.length = len;
|
| 560 | req.offset = offset;
|
| 561 | printf("proc_get_diff_pack_data send request [offset %u][req.diffPackID %s][len %u][s_pack_date_count:%u][offset %u]\n",
|
| 562 | offset, req.diffPackID, len, s_pack_date_count, offset);
|
| 563 | memcpy(s_req + WEFOTA_HEADER_SIZE, &req, sizeof(req));
|
| 564 | s_diffpack_data_req = req;
|
| 565 | send_message(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr);
|
| 566 | size -= len;
|
| 567 | }
|
| 568 |
|
| 569 | while (1)
|
| 570 | {
|
| 571 | FD_ZERO(&read_fds);
|
| 572 | FD_SET(sock, &read_fds);
|
| 573 | max_fd = sock;
|
| 574 | while (1)
|
| 575 | {
|
| 576 | timeout.tv_sec = 3;
|
| 577 | timeout.tv_usec = 0;
|
| 578 | select_result = select(max_fd + 1, &read_fds, NULL, NULL, &timeout);
|
| 579 | if (select_result < 0)
|
| 580 | {
|
| 581 | printf("proc_get_diff_pack_data select failed\n");
|
| 582 | perror("select failed");
|
| 583 | break;
|
| 584 | }
|
| 585 | else if (select_result == 0)
|
| 586 | {
|
| 587 | printf("proc_get_diff_pack_data No data to read, or timeout\n");
|
| 588 | break;
|
| 589 | }
|
| 590 | if (FD_ISSET(sock, &read_fds))
|
| 591 | {
|
| 592 | if (receive_data(sock, filefd, &s_server2_addr, sizeof(s_server2_addr), received) < 0)
|
| 593 | {
|
| 594 | break;
|
| 595 | }
|
| 596 | }
|
| 597 | }
|
| 598 |
|
| 599 | if (max_retry_count++ > WEFOTA_MAX_RETRY_COUNT)
|
| 600 | {
|
| 601 | printf("proc_get_diff_pack_data reach the maximum retry count, break!!!\n");
|
| 602 | break;
|
| 603 | }
|
| 604 |
|
| 605 | int all_received = 1;
|
| 606 | for (i = 0; i < total_packets; i++)
|
| 607 | {
|
| 608 | if (!received[i])
|
| 609 | {
|
| 610 | all_received = 0;
|
| 611 | unsigned int len = s_diffpack_info.size - i * 1024 > WEFOTA_DIFF_PACK_DATA_MAX_LEN ? WEFOTA_DIFF_PACK_DATA_MAX_LEN : s_diffpack_info.size - i * 1024;
|
| 612 | s_pack_date_count = len + 39;
|
| 613 | unsigned int offset = i * 1024;
|
| 614 | DiffPackDataReq req = {0};
|
| 615 | memcpy(req.diffPackID, s_diffpack_info.diffPackID, sizeof(req.diffPackID));
|
| 616 | req.length = len;
|
| 617 | req.offset = offset;
|
| 618 | printf("Re request, send request [offset %u]\n", offset);
|
| 619 | memcpy(s_req + WEFOTA_HEADER_SIZE, &req, sizeof(req));
|
| 620 | s_diffpack_data_req = req;
|
| 621 | send_message(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr);
|
| 622 | }
|
| 623 | }
|
| 624 |
|
| 625 | if (all_received)
|
| 626 | {
|
| 627 | printf("All packets received successfully.\n");
|
| 628 | break;
|
| 629 | }
|
| 630 | }
|
| 631 |
|
| 632 | close(filefd);
|
| 633 | s_costTime = time(NULL) - startTime;
|
| 634 | return 0;
|
| 635 | }
|
| 636 |
|
| 637 |
|
| 638 | int proc_get_diff_pack_end(int sock) {
|
| 639 | set_fota_status(FOTA_STATUS_GET_DIFF_PACK_END);
|
| 640 |
|
| 641 | int ret = -1;
|
| 642 | WeFOTAHeader * hdr = (WeFOTAHeader *)s_req;
|
| 643 | int count = wefota_header_init(hdr);
|
| 644 | printf("proc_get_diff_pack_end send code=%d, count=%d)\n", hdr->code, count);
|
| 645 |
|
| 646 | DiffPackEnd end = {0};
|
| 647 | memcpy(end.diffPackID, s_diffpack_info.diffPackID, sizeof(end.diffPackID));
|
| 648 | end.costTime = s_costTime;
|
| 649 | memcpy(s_req + WEFOTA_HEADER_SIZE, &end, sizeof(end));
|
| 650 |
|
| 651 | if (0 > send_with_retry(sock, hdr, WEFOTA_HEADER_SIZE + count, &s_server2_addr, s_resp, sizeof(s_resp), WEFOTA_MAX_RETRIES)) {
|
| 652 | printf("comm fail\n");
|
| 653 | return ret;
|
| 654 | }
|
| 655 | return 0;
|
| 656 | }
|
| 657 |
|
| 658 | static int verify_md5(const unsigned char *expected_md5, const char *file_path)
|
| 659 | {
|
| 660 | char command[256];
|
| 661 | char expected_md5_str[33];
|
| 662 | char md5sum_output[33];
|
| 663 | int i = 0;
|
| 664 |
|
| 665 | snprintf(command, sizeof(command), "md5sum \"%s\"", file_path);
|
| 666 | FILE *pipe = popen(command, "r");
|
| 667 | if (!pipe)
|
| 668 | {
|
| 669 | printf("verify_md5 pipe error\n");
|
| 670 | return -1;
|
| 671 | }
|
| 672 |
|
| 673 | if (fgets(md5sum_output, sizeof(md5sum_output), pipe) == NULL)
|
| 674 | {
|
| 675 | printf("verify_md5 fgets error\n");
|
| 676 | pclose(pipe);
|
| 677 | return -1;
|
| 678 | }
|
| 679 |
|
| 680 | pclose(pipe);
|
| 681 |
|
| 682 | for (i = 0; i < 16; ++i)
|
| 683 | {
|
| 684 | sprintf(expected_md5_str + i * 2, "%02x", expected_md5[i]);
|
| 685 | }
|
| 686 |
|
| 687 | if (strncmp(expected_md5_str, md5sum_output, 32) == 0)
|
| 688 | {
|
| 689 | printf("md5 check OK\n");
|
| 690 | return 1;
|
| 691 | }
|
| 692 | else
|
| 693 | {
|
| 694 | printf("md5 check error\n");
|
| 695 | return 0;
|
| 696 | }
|
| 697 | }
|
| 698 |
|
| 699 | int proc_install_diff_pack(void)
|
| 700 | {
|
| 701 | const char *file_path = "/cache/zte_fota/delta.package";
|
| 702 | int result = 0;
|
| 703 |
|
| 704 | printf("**********proc_install_diff_pack begin \n");
|
| 705 | set_fota_status(FOTA_STATUS_INSTALL_DIFF_PACK);
|
| 706 |
|
| 707 | result = verify_md5(s_diffpack_info.MD5, file_path);
|
| 708 | if (result == 1)
|
| 709 | {
|
| 710 | return start_wefota_install();
|
| 711 | }
|
| 712 | else
|
| 713 | {
|
| 714 | return -1;
|
| 715 | }
|
| 716 |
|
| 717 | }
|
| 718 |
|
| 719 | int wefota_proc(void) {
|
| 720 | printf("**********wefota_proc\n");
|
| 721 | int ret = -1;
|
| 722 | int sock = -1;
|
| 723 | set_wefota_upgrade_flag_cfg("1");
|
| 724 | do {
|
| 725 | sock = create_udp_socket();
|
| 726 | if (sock < 0) {
|
| 727 | printf("**********create_udp_socket socket < 0 \n");
|
| 728 | break;
|
| 729 | }
|
| 730 |
|
| 731 | ret = init_wefota_server1_addr();
|
| 732 | if (ret < 0) {
|
| 733 | printf("**********init_wefota_server1_addr error \n");
|
| 734 | break;
|
| 735 | }
|
| 736 |
|
| 737 | ret = proc_get_ip_1(sock);
|
| 738 | if (ret < 0) {
|
| 739 | printf("**********proc_get_ip_1 error \n");
|
| 740 | break;
|
| 741 | }
|
| 742 |
|
| 743 | ret = proc_check_task(sock);
|
| 744 | if (ret < 0) {
|
| 745 | printf("**********proc_check_task error \n");
|
| 746 | break;
|
| 747 | }
|
| 748 |
|
| 749 | ret = proc_get_ip_2(sock);
|
| 750 | if (ret < 0) {
|
| 751 | printf("**********proc_get_ip_2 error \n");
|
| 752 | break;
|
| 753 | }
|
| 754 |
|
| 755 | ret = proc_get_ver_id(sock);
|
| 756 | if (ret < 0) {
|
| 757 | printf("**********proc_get_ver_id error \n");
|
| 758 | break;
|
| 759 | }
|
| 760 |
|
| 761 | ret = proc_get_diff_pack_id(sock);
|
| 762 | if (ret < 0) {
|
| 763 | printf("**********proc_get_diff_pack_id error \n");
|
| 764 | break;
|
| 765 | }
|
| 766 |
|
| 767 | ret = proc_get_diff_pack_data(sock);
|
| 768 | if (ret < 0) {
|
| 769 | printf("**********proc_get_diff_pack_data error \n");
|
| 770 | break;
|
| 771 | }
|
| 772 | else if (ret == FOTA_NEED_CONTINUE_PREVIOUS_UPGRADE)
|
| 773 | {
|
| 774 | printf("**********wefota_proc,The last upgrade was not completed, continue upgrading \n");
|
| 775 | close_udp_socket(sock);
|
| 776 | sock = -1;
|
| 777 | wefota_upgrade_without_verify();
|
| 778 | break;
|
| 779 | }
|
| 780 |
|
| 781 | ret = proc_get_diff_pack_end(sock);
|
| 782 |
|
| 783 | close_udp_socket(sock);
|
| 784 | sock = -1;
|
| 785 |
|
| 786 | ret = proc_install_diff_pack();
|
| 787 | }while(0);
|
| 788 |
|
| 789 | set_wefota_upgrade_flag_cfg("0");
|
| 790 | if (sock >= 0) {
|
| 791 | close_udp_socket(sock);
|
| 792 | }
|
| 793 | return ret;
|
| 794 | }
|
| 795 |
|
| 796 | int main(void) {
|
| 797 | printf("**********main\n");
|
| 798 | while (1) {
|
| 799 | wait_fota_conditions();
|
| 800 | wefota_proc();
|
| 801 | }
|
| 802 | return 0;
|
| 803 | }
|