b.liu | d440f9f | 2025-04-18 10:44:31 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <stdint.h> |
| 5 | #include <unistd.h> |
| 6 | #include <time.h> |
| 7 | #include <errno.h> |
| 8 | #include <pthread.h> |
| 9 | #include "ql_fota_api.h" |
| 10 | #include "fota_info.h" |
| 11 | #include "ql_fota.h" |
| 12 | //#include "ql_fota_log.h" |
| 13 | #include "ql_absys_api.h" |
| 14 | //#include "test_utils.h" |
| 15 | |
| 16 | |
| 17 | typedef void (*item_handler_f)(void); |
| 18 | typedef int (*init_handler_f)(void); |
| 19 | typedef int (*deinit_handler_f)(void); |
| 20 | |
| 21 | |
| 22 | #define T_ARRAY_SIZE(items) (sizeof(items)/sizeof(items[0])) |
| 23 | |
| 24 | typedef struct |
| 25 | { |
| 26 | const char *name; |
| 27 | item_handler_f handle; |
| 28 | } t_item_t; |
| 29 | |
| 30 | typedef struct |
| 31 | { |
| 32 | const char *name; |
| 33 | int item_len; |
| 34 | t_item_t *item_list; |
| 35 | } t_module_t; |
| 36 | |
| 37 | typedef struct |
| 38 | { |
| 39 | const char *name; |
| 40 | init_handler_f init_handle; |
| 41 | deinit_handler_f deinit_handle; |
| 42 | } t_init_t; |
| 43 | |
| 44 | int t_get_int(int *val); |
| 45 | int t_get_hex(uint32_t *val); |
| 46 | int t_get_char(int *val); |
| 47 | int t_get_string(char *str_buf, int str_len); |
| 48 | int t_get_int_list(int *dat_buf, int *dat_len); |
| 49 | int t_get_float_list(float *dat_buf, int *dat_len); |
| 50 | /*-----------------------------------------------------------------------------------------------*/ |
| 51 | /** |
| 52 | @brief Read a int value from stdin |
| 53 | @param[out] val, Return read data |
| 54 | @return |
| 55 | 0 - successful |
| 56 | 1 - read an enter |
| 57 | -1 - invalid input |
| 58 | */ |
| 59 | /*-----------------------------------------------------------------------------------------------*/ |
| 60 | int t_get_int(int *val) |
| 61 | { |
| 62 | int dat; |
| 63 | char *ptr_end = NULL; |
| 64 | char buf[256] = {0}; |
| 65 | |
| 66 | if(NULL == fgets(buf, sizeof(buf)-1, stdin)) |
| 67 | { |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if(0 == buf[0]) |
| 72 | { |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | if(buf[0] == '\n') |
| 77 | { |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | dat = strtol(buf, &ptr_end, 10); |
| 82 | if(ptr_end!=NULL && ptr_end[0]!='\n') |
| 83 | { |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | if(val) |
| 88 | { |
| 89 | val[0] = dat; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /*-----------------------------------------------------------------------------------------------*/ |
| 96 | /** |
| 97 | @brief Read a uint32 value from stdin |
| 98 | @param[out] val, Return read data |
| 99 | @return |
| 100 | 0 - successful |
| 101 | 1 - read an enter |
| 102 | -1 - invalid input |
| 103 | */ |
| 104 | /*-----------------------------------------------------------------------------------------------*/ |
| 105 | int t_get_hex(uint32_t *val) |
| 106 | { |
| 107 | int dat; |
| 108 | char *ptr_end = NULL; |
| 109 | char buf[256] = {0}; |
| 110 | |
| 111 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 112 | { |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if(0 == buf[0]) |
| 117 | { |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | if(buf[0] == '\n') |
| 122 | { |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | dat = strtol(buf, &ptr_end, 16); |
| 127 | if(ptr_end!=NULL && ptr_end[0]!='\n') |
| 128 | { |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | if(val) |
| 133 | { |
| 134 | val[0] = dat; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /*-----------------------------------------------------------------------------------------------*/ |
| 141 | /** |
| 142 | @brief Read a char value from stdin |
| 143 | @param[out] val, Return read data |
| 144 | @return |
| 145 | 0 - successful |
| 146 | 1 - read an enter |
| 147 | -1 - invalid input |
| 148 | */ |
| 149 | /*-----------------------------------------------------------------------------------------------*/ |
| 150 | int t_get_char(int *val) |
| 151 | { |
| 152 | char buf[256] = {0}; |
| 153 | |
| 154 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 155 | { |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | if(0 == buf[0]) |
| 160 | { |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | if(buf[0] == '\n') |
| 165 | { |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | if(buf[1]!='\n') |
| 170 | { |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | if(val) |
| 175 | { |
| 176 | val[0] = buf[0]; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /*-----------------------------------------------------------------------------------------------*/ |
| 183 | /** |
| 184 | @brief Read a string value from stdin |
| 185 | @param[out] val, Return read data |
| 186 | @return |
| 187 | 0 - successful |
| 188 | 1 - read an enter |
| 189 | -1 - invalid input |
| 190 | */ |
| 191 | /*-----------------------------------------------------------------------------------------------*/ |
| 192 | int t_get_string(char *str_buf, int str_len) |
| 193 | { |
| 194 | char *ptr; |
| 195 | char buf[256] = {0}; |
| 196 | |
| 197 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 198 | { |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | if(0 == buf[0]) |
| 203 | { |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | if(buf[0] == '\n') |
| 208 | { |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | ptr = strchr(buf, '\n'); |
| 213 | if(ptr) |
| 214 | { |
| 215 | ptr[0] = 0; |
| 216 | } |
| 217 | |
| 218 | strncpy(str_buf, buf, str_len-1); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /*-----------------------------------------------------------------------------------------------*/ |
| 224 | /** |
| 225 | @brief Read a list of int values from stdin |
| 226 | @param[out] val, Return read datas |
| 227 | @param[out&in] val, Input buffer length, output the number of read |
| 228 | @return |
| 229 | 0 - successful |
| 230 | 1 - read an enter |
| 231 | -1 - invalid input |
| 232 | */ |
| 233 | /*-----------------------------------------------------------------------------------------------*/ |
| 234 | int t_get_int_list(int *dat_buf, int *dat_len) |
| 235 | { |
| 236 | int idx = 0; |
| 237 | int len; |
| 238 | int dat; |
| 239 | char *ptr, *ptr_save; |
| 240 | char *ptr_end; |
| 241 | char buf[256] = {0}; |
| 242 | |
| 243 | if(!dat_buf || !dat_len) |
| 244 | { |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | len = dat_len[0]; |
| 249 | |
| 250 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 251 | { |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | if(0 == buf[0]) |
| 256 | { |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | if(buf[0] == '\n') |
| 261 | { |
| 262 | return 1; |
| 263 | } |
| 264 | |
| 265 | for(ptr=strtok_r(buf, ",.: \t\r\n", &ptr_save); |
| 266 | ptr!=NULL; |
| 267 | ptr=strtok_r(NULL, ",.: \t\r\n", &ptr_save)) |
| 268 | { |
| 269 | dat = strtol(ptr, &ptr_end, 10); |
| 270 | if(ptr_end!=NULL && ptr_end[0]!=0) |
| 271 | { |
| 272 | return -1; |
| 273 | } |
| 274 | if(idx >= len) |
| 275 | { |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | dat_buf[idx] = dat; |
| 280 | idx++; |
| 281 | } |
| 282 | |
| 283 | dat_len[0] = idx; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /*-----------------------------------------------------------------------------------------------*/ |
| 288 | /** |
| 289 | @brief Read a list of float values from stdin |
| 290 | @param[out] val, Return read datas |
| 291 | @param[out&in] val, Input buffer length, output the number of read |
| 292 | @return |
| 293 | 0 - successful |
| 294 | 1 - read an enter |
| 295 | -1 - invalid input |
| 296 | */ |
| 297 | /*-----------------------------------------------------------------------------------------------*/ |
| 298 | int t_get_float_list(float *dat_buf, int *dat_len) |
| 299 | { |
| 300 | int idx = 0; |
| 301 | int len; |
| 302 | float dat; |
| 303 | char *ptr, *ptr_save; |
| 304 | char *ptr_end; |
| 305 | char buf[256] = {0}; |
| 306 | |
| 307 | if(!dat_buf || !dat_len) |
| 308 | { |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | len = dat_len[0]; |
| 313 | |
| 314 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 315 | { |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | if(0 == buf[0]) |
| 320 | { |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | if(buf[0] == '\n') |
| 325 | { |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | for(ptr=strtok_r(buf, ",: \t\r\n", &ptr_save); |
| 330 | ptr!=NULL; |
| 331 | ptr=strtok_r(NULL, ",: \t\r\n", &ptr_save)) |
| 332 | { |
| 333 | dat = strtof(ptr, &ptr_end); |
| 334 | if(ptr_end!=NULL && ptr_end[0]!=0) |
| 335 | { |
| 336 | return -1; |
| 337 | } |
| 338 | if(idx >= len) |
| 339 | { |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | dat_buf[idx] = dat; |
| 344 | idx++; |
| 345 | } |
| 346 | |
| 347 | dat_len[0] = idx; |
| 348 | return 0; |
| 349 | } |
| 350 | #if 0 |
| 351 | #define DEBUG_INFO |
| 352 | #ifdef DEBUG_INFO |
| 353 | #define LOG_DBG(fmt, ...) printf("[DBG][%s_%d][%ld] "fmt"\n", __FUNCTION__, __LINE__, time(NULL), ##__VA_ARGS__) |
| 354 | #else |
| 355 | #define LOG_DBG(fmt, ...) |
| 356 | #endif |
| 357 | #define LOG_ERR(fmt, ...) printf("[DBG][%s_%d][%ld] "fmt"\n", __FUNCTION__, __LINE__, time(NULL), ##__VA_ARGS__) |
| 358 | #endif |
| 359 | |
| 360 | #define ITEM_NUM (sizeof(g_items)/sizeof(g_items[0])) |
| 361 | |
| 362 | void test_ota_api_start(void); |
| 363 | void test_get_fota_upgrade_info(void); |
| 364 | void test_ql_absys_switch(void); |
| 365 | void test_ql_absys_get_cur_active_part(void); |
| 366 | void test_ql_absys_sync(void); |
| 367 | void test_ql_absys_getstatus(void); |
| 368 | void test_ql_fota_fw_write_by_url(void); |
| 369 | |
| 370 | t_item_t g_items[] = { |
| 371 | {"API : ql_abfota_start_update", test_ota_api_start}, |
| 372 | {"API : ql_abfota_get_update_status", test_get_fota_upgrade_info}, |
| 373 | {"API : ql_absys_switch", test_ql_absys_switch}, |
| 374 | {"API : ql_absys_sync", test_ql_absys_sync}, |
| 375 | {"API : ql_absys_get_cur_active_part", test_ql_absys_get_cur_active_part}, |
| 376 | {"API : ql_absys_getstatus", test_ql_absys_getstatus} |
| 377 | }; |
| 378 | |
| 379 | void dump_items(void) |
| 380 | { |
| 381 | int i; |
| 382 | |
| 383 | printf("\n"); |
| 384 | |
| 385 | for(i=0; i<ITEM_NUM; i++) |
| 386 | { |
| 387 | printf("%d\t%s\n", i, g_items[i].name); |
| 388 | } |
| 389 | printf("-1\texit\n"); |
| 390 | } |
| 391 | |
| 392 | int main(int argc, const char **argv) |
| 393 | { |
| 394 | int ret = -1; |
| 395 | int idx = 0; |
| 396 | |
| 397 | printf("Quectel OTA API test sample, version : v0.0.1\n"); |
| 398 | |
| 399 | dump_items(); |
| 400 | |
| 401 | while(1) { |
| 402 | printf("Please enter your choice: "); |
| 403 | ret = t_get_int(&idx); |
| 404 | printf("\n"); |
| 405 | if(ret < 0) { |
| 406 | printf("Invalid input\n"); |
| 407 | continue; |
| 408 | } else if(ret == 1) { |
| 409 | dump_items(); |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | if(idx == -1) { |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | if(idx<0 || idx>=ITEM_NUM) { |
| 418 | printf("Not support idx: %d\n", idx); |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | g_items[idx].handle(); |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | void test_ota_api_start(void) |
| 428 | { |
| 429 | char package_file[128] = {0}; |
| 430 | int ret = -1; |
| 431 | |
| 432 | printf("please input the fota fbf package file dir: \n"); |
| 433 | printf("eg: /user_data/\n"); |
| 434 | //fota包同时已经放入该路径下 |
| 435 | memset(package_file, 0x0, sizeof(package_file)); |
| 436 | scanf("%s", package_file); |
| 437 | fflush(stdin); |
| 438 | ret = t_get_string((char*)package_file, sizeof(package_file)); |
| 439 | if (ret < 0 || package_file[0] == 0) { |
| 440 | printf("Invalid package file\n"); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | ret = ql_abfota_start_update((char*)package_file); |
| 445 | |
| 446 | if (ret != 0) { |
| 447 | printf("run ql_abfota_start_update failed, api return: %d\n", ret); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | printf("Update in-active partition SUCCEED\n"); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | void test_ql_absys_getstatus(void) |
| 456 | { |
| 457 | int status = 0; |
| 458 | // char stat_buf[16] = {0}; |
| 459 | sysstatus_t sys_state; |
| 460 | status = ql_absys_getstatus(&sys_state); |
| 461 | if (status < 0) { |
| 462 | printf("failed to get absys status!!!\n"); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | if (sys_state.is_damaged == 0) |
| 467 | { |
| 468 | printf("absys partition status : succeed\n"); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | printf("absys partition status : damaged\n"); |
| 473 | printf("absys partition damaged position : %s\n", sys_state.damaged_partname); |
| 474 | printf("absys needsync!!!\n"); |
| 475 | } |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | void test_get_fota_upgrade_info(void) |
| 481 | { |
| 482 | int ret = -1; |
| 483 | char stat_buf[16] = {0}; |
| 484 | update_info_t update_info; |
| 485 | ret = ql_abfota_get_update_status(&update_info); |
| 486 | if ( ret != 0) { |
| 487 | printf("run ql_abfota_start_update failed, api return: %d\n", ret); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | memset(stat_buf, 0, sizeof(stat_buf)); |
| 492 | |
| 493 | switch (update_info.ota_state) { |
| 494 | case SUCCEED: |
| 495 | strncpy(stat_buf, "SUCCEED", strlen("SUCCEED")+1); |
| 496 | break; |
| 497 | case UPDATE: |
| 498 | strncpy(stat_buf, "UPDATE", strlen("UPDATE")+1); |
| 499 | break; |
| 500 | case BACKUP: |
| 501 | strncpy(stat_buf, "BACKUP", strlen("BACKUP")+1); |
| 502 | break; |
| 503 | case FAILED: |
| 504 | strncpy(stat_buf, "FAILED", strlen("FAILED")+1); |
| 505 | break; |
| 506 | case WRITEDONE: |
| 507 | strncpy(stat_buf, "WRITEDONE", strlen("WRITEDONE")+1); |
| 508 | break; |
| 509 | case NEEDSYNC: |
| 510 | strncpy(stat_buf, "NEEDSYNC", strlen("NEEDSYNC")+1); |
| 511 | break; |
| 512 | case UNKNOWN_STATUS: |
| 513 | default: |
| 514 | strncpy(stat_buf, "UNKNOWN_STATUS", strlen("UNKNOWN_STATUS")+1); |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | printf("Current fota progress: %d\n", update_info.percentage); |
| 519 | printf("Current fota state: %s\n", stat_buf); |
| 520 | printf("Current fota exit code: %d\n", update_info.exit_code); |
| 521 | |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | void test_ql_absys_switch(void) |
| 526 | { |
| 527 | int ret = -1; |
| 528 | ret = ql_absys_switch(); |
| 529 | if (ret != 0) { |
| 530 | printf("run ql_absys_switch failed, api return: %d\n", ret); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | printf("It is okay to swith AB part to run\n"); |
| 535 | sleep(1); |
| 536 | |
| 537 | system("reboot"); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | void test_ql_absys_sync(void) |
| 543 | { |
| 544 | int ret = -1; |
| 545 | |
| 546 | ret = ql_absys_sync(); |
| 547 | if (ret != 0) { |
| 548 | printf("run ql_absys_sync failed, api return: %d\n", ret); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | printf("do AB sync succeed\n"); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | void test_ql_absys_get_cur_active_part(void) |
| 558 | { |
| 559 | int ret = -1; |
| 560 | absystem_t cur_system; |
| 561 | |
| 562 | ret = ql_absys_get_cur_active_part(&cur_system); |
| 563 | if (ret != 0) { |
| 564 | printf("run ql_absys_get_cur_active_part failed, api return: %d\n", ret); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | printf("Current active part is %c\n", (cur_system ? 'B': 'A')); |
| 569 | return; |
| 570 | } |