| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <stdint.h> |
| #include <signal.h> |
| #include <pthread.h> |
| #include <stdbool.h> |
| #include <dlfcn.h> |
| |
| typedef struct |
| { |
| int cmdIdx; |
| char *funcName; |
| }st_api_test_case; |
| |
| |
| |
| typedef enum |
| { |
| GSW_OTA_SUCCESS, |
| GSW_OTA_FAILURE, |
| GSW_OTA_TIMEOUT, |
| GSW_OTA_INPROCESS, |
| GSW_OTA_NO_TASK |
| }E_GSW_OTA_RET; |
| |
| typedef enum _gsw_ota_ret |
| { |
| GSW_OTA_SYSTEM_A, |
| GSW_OTA_SYSTEM_B, |
| }E_GSW_OTA_SYSTEM; |
| |
| |
| typedef enum |
| { |
| GSW_UPDATE_SUCCEED = 0, //update succeed |
| GSW_UPDATE_INPROGRESS, //update in progress |
| GSW_UPDATE_BACKUP, //A/B partition sync in progress |
| GSW_UPDATE_FAILED, //update failed |
| GSW_UPDATE_WAITEDONE, //update in-active part finished,not switch update part. |
| GSW_UPDATE_NEEDSYNC, //switch update part, need sync A/B part |
| GSW_UPDATE_CANCEL //updata cancel success |
| }gsw_update_state_t; |
| |
| typedef enum |
| { |
| GSW_UPDATE_NOERROR=0, //升级成功 |
| GSW_UPDATE_ARGUMENTERROR, //升级程序启动参数错误 |
| GSW_UPDATE_SDCARDNOEXIST, //未挂载外置FLASH等存储设备 |
| GSW_UPDATE_PACKAGENOEXIST, //升级包不存在 |
| GSW_UPDATE_UNZIPFAILED, //解压升级包出错 |
| GSW_UPDATE_PARTITIONFLUSHERROR,//写入分区出错 |
| GSW_UPDATE_XMLPARSEERROR, //解析 fotaconfig.xml 文件出错 |
| GSW_UPDATE_DIFFUBIUNATTACH, //差分升级 UBI 分区挂载异常 |
| GSW_UPDATE_NOSPACELEFT, //空间不足 |
| GSW_UPDATE_FILECHECKFAILED, //MD5 值校验失败 |
| GSW_UPDATE_BSPATCHFAILED, //bspatch 合成新文件夹失败 |
| GSW_UPDATE_NOFINDPARTITION, //待升级分区不存在 |
| GSW_UPDATE_UBIVOLUMEERROR, //差分升级,待升级 UBI 卷不存在 |
| GSW_UPDATE_NOFOTACONFIGFILE, //升级包中无 fotaconfig.xml 文件 |
| GSW_UPDATE_GETOLDSOFTWAREFAILED,//读取原始版本固件失败 |
| GSW_UPDATE_FILENOTEXIST, //文件不存在 |
| GSW_UPDATE_UPGRADECANCELED, //升级或分区同步被取消 |
| GSW_UPDATE_NONEEDCANCEL, //取消升级失败 |
| GSW_UPDATE_NOGOING //升级或分区同步正在进行,不可重复操作 |
| }gsw_update_exit_code_t; |
| |
| |
| typedef struct |
| { |
| unsigned int percentage; //update progress0-100 |
| gsw_update_state_t update_state; |
| gsw_update_exit_code_t exit_code; |
| }gsw_update_info_s; |
| |
| typedef struct |
| { |
| gsw_update_state_t update_state; |
| uint8_t is_damaged; //TURE: damaged FALSE:no damaged |
| uint8_t damaged_partname[16]; |
| }gsw_system_status_s; |
| |
| typedef int32_t (*gsw_update_modem_start_autobackup)(char* file_path); |
| typedef bool (*gsw_update_modem_check_condition)(void); |
| typedef int32_t (*gsw_update_modem_result_query)(void); |
| typedef int32_t (*gsw_update_modem_start_nobackup)(char* file_path); |
| typedef int32_t (*gsw_update_modem_get_system)(void); |
| typedef int32_t (*gsw_update_modem_cancel)(void); |
| typedef int32_t (*gsw_update_modem_get_info)(gsw_update_info_s *update_info); |
| typedef int32_t (*gsw_update_modem_get_status)(gsw_system_status_s *system_status); |
| typedef int32_t (*gsw_update_modem_sync)(void); |
| typedef int32_t (*gsw_update_modem_switch)(void); |
| |
| gsw_update_modem_start_autobackup gsw_update_modem_start_autobackup_ptr = NULL; |
| |
| static void *ota_handle = NULL; |
| |
| st_api_test_case api_testcases[] = |
| { |
| {0, "print_help"}, |
| {1, "gsw_update_modem_start_autobackup"}, |
| {2, "gsw_update_modem_check_condition"}, |
| {3, "gsw_update_modem_result_query"}, |
| {4, "gsw_update_modem_get_system"}, |
| {5, "gsw_update_modem_get_info"}, |
| {6, "gsw_update_modem_get_status"}, |
| {7, "gsw_update_modem_sync"}, |
| {8, "gsw_update_modem_switch"}, |
| {9, "gsw_update_modem_start_nobackup"}, |
| {10, "gsw_update_modem_cancel"}, |
| {-1, NULL} |
| |
| }; |
| |
| void print_help(void) |
| { |
| int i; |
| |
| printf("Supported test cases:\n"); |
| for(i = 0; ; i++) |
| { |
| if(api_testcases[i].cmdIdx == -1) |
| { |
| break; |
| } |
| printf("%d:\t%s\n", api_testcases[i].cmdIdx, api_testcases[i].funcName); |
| } |
| } |
| |
| void *fota_thread(void *arg) |
| { |
| char *file_path = (char *)arg; |
| int ret = -1; |
| ret = gsw_update_modem_start_autobackup_ptr(file_path); |
| printf("gsw_update_modem_start_autobackup ret = %d\n", ret); |
| return NULL; |
| } |
| |
| void sigint_handler(int sig) |
| { |
| printf("sigint_handler\n"); |
| dlclose(ota_handle); |
| ota_handle = NULL; |
| gsw_update_modem_start_autobackup_ptr = NULL; |
| exit(0); |
| } |
| void clear_input_buffer() |
| { |
| int c; |
| |
| while ((c = getchar()) != '\n' && c != EOF); |
| } |
| |
| int main(int argc,char *argv[]) |
| { |
| gsw_update_modem_check_condition gsw_update_modem_check_condition_ptr = NULL; |
| gsw_update_modem_result_query gsw_update_modem_result_query_ptr = NULL; |
| gsw_update_modem_get_system gsw_update_modem_get_system_ptr = NULL; |
| gsw_update_modem_get_info gsw_update_modem_get_info_ptr = NULL; |
| gsw_update_modem_get_status gsw_update_modem_get_status_ptr = NULL; |
| gsw_update_modem_sync gsw_update_modem_sync_ptr = NULL; |
| gsw_update_modem_switch gsw_update_modem_switch_ptr = NULL; |
| gsw_update_modem_start_nobackup gsw_update_modem_start_nobackup_ptr = NULL; |
| gsw_update_modem_cancel gsw_update_modem_cancel_ptr = NULL; |
| |
| signal(SIGINT, sigint_handler); |
| if (argc < 2) |
| { |
| printf("Usage: %s <file_path>\n", argv[0]); |
| return -1; // 确保提供了文件路径 |
| } |
| char *file_path = argv[1]; |
| ota_handle = dlopen("/lib/libgsw_lib.so", RTLD_NOW ); |
| if(ota_handle == NULL) |
| { |
| printf("open lib failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_start_autobackup_ptr = (gsw_update_modem_start_autobackup)dlsym(ota_handle, "gsw_update_modem_start_autobackup"); |
| if(gsw_update_modem_start_autobackup_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_start_autobackup failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_check_condition_ptr = (gsw_update_modem_check_condition)dlsym(ota_handle, "gsw_update_modem_check_condition"); |
| if(gsw_update_modem_check_condition_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_check_condition failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_result_query_ptr = (gsw_update_modem_result_query)dlsym(ota_handle, "gsw_update_modem_result_query"); |
| if(gsw_update_modem_result_query_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_result_query failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_get_system_ptr = (gsw_update_modem_get_system)dlsym(ota_handle, "gsw_update_modem_get_system"); |
| if(gsw_update_modem_get_system_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_get_system failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_get_info_ptr = (gsw_update_modem_get_info)dlsym(ota_handle, "gsw_update_modem_get_info"); |
| if(gsw_update_modem_get_info_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_get_info failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_get_status_ptr = (gsw_update_modem_get_status)dlsym(ota_handle, "gsw_update_modem_get_status"); |
| if(gsw_update_modem_get_status_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_get_status failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_sync_ptr = (gsw_update_modem_sync)dlsym(ota_handle, "gsw_update_modem_sync"); |
| if(gsw_update_modem_sync_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_sync failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_switch_ptr = (gsw_update_modem_switch)dlsym(ota_handle, "gsw_update_modem_switch"); |
| if(gsw_update_modem_switch_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_switch failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_start_nobackup_ptr = (gsw_update_modem_start_nobackup)dlsym(ota_handle, "gsw_update_modem_start_nobackup"); |
| if(gsw_update_modem_start_nobackup_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_start_nobackup failed\n"); |
| return -1; |
| } |
| |
| gsw_update_modem_cancel_ptr = (gsw_update_modem_cancel)dlsym(ota_handle, "gsw_update_modem_cancel"); |
| if(gsw_update_modem_cancel_ptr == NULL) |
| { |
| printf("dlsym gsw_update_modem_cancel failed\n"); |
| return -1; |
| } |
| |
| |
| |
| printf("Enter ota api test \n"); |
| int opt = -1; |
| print_help(); |
| while (1) |
| { |
| char buffer[100]; // 用于存储输入的缓冲区 |
| |
| printf("请输入选项: \n"); |
| if (fgets(buffer, sizeof(buffer), stdin) != NULL) |
| { |
| |
| if (sscanf(buffer, "%d", &opt) != 1) |
| { |
| printf("无效输入,请输入一个整数。\n"); |
| clear_input_buffer(); |
| continue; |
| } |
| } |
| else |
| { |
| |
| clear_input_buffer(); |
| continue; |
| } |
| |
| printf("输入的整数是:%d\n", opt); |
| |
| switch(opt) |
| { |
| case 0 : |
| { |
| print_help(); |
| break; |
| } |
| case 1 : |
| { |
| int32_t ret = -1; |
| pthread_t thread_id_reboot; |
| ret = pthread_create(&thread_id_reboot, NULL, fota_thread, (void*)file_path); |
| if (ret != 0) |
| { |
| printf("pthread_create failed \n"); |
| return -1; |
| } |
| break; |
| |
| } |
| case 2 : |
| { |
| bool ret = false; |
| ret = gsw_update_modem_check_condition_ptr(); |
| printf("gsw_update_modem_check_condition ret = %d\n", ret); |
| break; |
| } |
| case 3 : |
| { |
| E_GSW_OTA_RET ret = -1; |
| ret = gsw_update_modem_result_query_ptr(); |
| printf("gsw_update_modem_result_query ret = %d\n", ret); |
| break; |
| } |
| case 4 : |
| { |
| E_GSW_OTA_SYSTEM ret = -1; |
| ret = gsw_update_modem_get_system_ptr(); |
| printf("gsw_update_modem_get_system ret = %d\n", ret); |
| printf("system %c\n", ret == 0? 'a' : 'b'); |
| |
| break; |
| } |
| case 5 : |
| { |
| int32_t ret = -1; |
| gsw_update_info_s update_info = {0}; |
| ret = gsw_update_modem_get_info_ptr(&update_info); |
| printf("gsw_update_modem_get_info ret = %d\n", ret); |
| |
| printf("update_info. = %u\n",update_info.percentage); |
| printf("update_state. = %d\n",update_info.update_state); |
| printf("exit_code. = %d\n",update_info.exit_code); |
| |
| break; |
| } |
| case 6: |
| { |
| int32_t ret = -1; |
| gsw_system_status_s system_status = {0}; |
| ret = gsw_update_modem_get_status_ptr(&system_status); |
| printf("gsw_update_modem_get_status ret is %d\n",ret); |
| break; |
| |
| } |
| case 7: |
| { |
| int32_t ret = -1; |
| ret = gsw_update_modem_sync_ptr(); |
| printf("gsw_update_modem_sync ret is %d\n",ret ); |
| break; |
| } |
| case 8 : |
| { |
| int32_t ret = -1; |
| ret = gsw_update_modem_switch_ptr(); |
| printf("gsw_update_modem_switch ret is %d \n",ret); |
| break; |
| |
| } |
| case 9: |
| { |
| int32_t ret = -1; |
| ret = gsw_update_modem_start_nobackup_ptr(file_path); |
| printf("gsw_update_modem_start_nobackup ret is %d\n",ret); |
| break; |
| } |
| case 10: |
| { |
| int32_t ret = -1; |
| ret = gsw_update_modem_cancel_ptr(); |
| printf("gsw_update_modem_cancel ret is %d\n",ret); |
| break; |
| } |
| default: |
| printf("invalid opt\n"); |
| break; |
| } |
| |
| } |
| |
| |
| return 0; |
| |
| |
| } |
| |