b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <stdint.h> |
| 5 | #include <signal.h> |
| 6 | #include <pthread.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <dlfcn.h> |
| 9 | |
| 10 | typedef struct |
| 11 | { |
| 12 | int cmdIdx; |
| 13 | char *funcName; |
| 14 | }st_api_test_case; |
| 15 | |
| 16 | |
| 17 | |
| 18 | typedef enum |
| 19 | { |
| 20 | GSW_OTA_SUCCESS, |
| 21 | GSW_OTA_FAILURE, |
| 22 | GSW_OTA_TIMEOUT, |
| 23 | GSW_OTA_INPROCESS, |
| 24 | GSW_OTA_NO_TASK |
| 25 | }E_GSW_OTA_RET; |
| 26 | |
| 27 | typedef enum _gsw_ota_ret |
| 28 | { |
| 29 | GSW_OTA_SYSTEM_A, |
| 30 | GSW_OTA_SYSTEM_B, |
| 31 | }E_GSW_OTA_SYSTEM; |
| 32 | |
| 33 | |
| 34 | typedef enum |
| 35 | { |
| 36 | GSW_UPDATE_SUCCEED = 0, //update succeed |
| 37 | GSW_UPDATE_INPROGRESS, //update in progress |
| 38 | GSW_UPDATE_BACKUP, //A/B partition sync in progress |
| 39 | GSW_UPDATE_FAILED, //update failed |
| 40 | GSW_UPDATE_WAITEDONE, //update in-active part finished,not switch update part. |
| 41 | GSW_UPDATE_NEEDSYNC, //switch update part, need sync A/B part |
| 42 | GSW_UPDATE_CANCEL //updata cancel success |
| 43 | }gsw_update_state_t; |
| 44 | |
| 45 | typedef enum |
| 46 | { |
| 47 | GSW_UPDATE_NOERROR=0, //升级成功 |
| 48 | GSW_UPDATE_ARGUMENTERROR, //升级程序启动参数错误 |
| 49 | GSW_UPDATE_SDCARDNOEXIST, //未挂载外置FLASH等存储设备 |
| 50 | GSW_UPDATE_PACKAGENOEXIST, //升级包不存在 |
| 51 | GSW_UPDATE_UNZIPFAILED, //解压升级包出错 |
| 52 | GSW_UPDATE_PARTITIONFLUSHERROR,//写入分区出错 |
| 53 | GSW_UPDATE_XMLPARSEERROR, //解析 fotaconfig.xml 文件出错 |
| 54 | GSW_UPDATE_DIFFUBIUNATTACH, //差分升级 UBI 分区挂载异常 |
| 55 | GSW_UPDATE_NOSPACELEFT, //空间不足 |
| 56 | GSW_UPDATE_FILECHECKFAILED, //MD5 值校验失败 |
| 57 | GSW_UPDATE_BSPATCHFAILED, //bspatch 合成新文件夹失败 |
| 58 | GSW_UPDATE_NOFINDPARTITION, //待升级分区不存在 |
| 59 | GSW_UPDATE_UBIVOLUMEERROR, //差分升级,待升级 UBI 卷不存在 |
| 60 | GSW_UPDATE_NOFOTACONFIGFILE, //升级包中无 fotaconfig.xml 文件 |
| 61 | GSW_UPDATE_GETOLDSOFTWAREFAILED,//读取原始版本固件失败 |
| 62 | GSW_UPDATE_FILENOTEXIST, //文件不存在 |
| 63 | GSW_UPDATE_UPGRADECANCELED, //升级或分区同步被取消 |
| 64 | GSW_UPDATE_NONEEDCANCEL, //取消升级失败 |
| 65 | GSW_UPDATE_NOGOING //升级或分区同步正在进行,不可重复操作 |
| 66 | }gsw_update_exit_code_t; |
| 67 | |
| 68 | |
| 69 | typedef struct |
| 70 | { |
| 71 | unsigned int percentage; //update progress0-100 |
| 72 | gsw_update_state_t update_state; |
| 73 | gsw_update_exit_code_t exit_code; |
| 74 | }gsw_update_info_s; |
| 75 | |
| 76 | typedef struct |
| 77 | { |
| 78 | gsw_update_state_t update_state; |
| 79 | uint8_t is_damaged; //TURE: damaged FALSE:no damaged |
| 80 | uint8_t damaged_partname[16]; |
| 81 | }gsw_system_status_s; |
| 82 | |
| 83 | typedef int32_t (*gsw_update_modem_start_autobackup)(char* file_path); |
| 84 | typedef bool (*gsw_update_modem_check_condition)(void); |
l.yang | 7b93b55 | 2025-05-28 05:00:14 -0700 | [diff] [blame] | 85 | typedef int32_t (*gsw_update_modem_result_query)(void); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 86 | typedef int32_t (*gsw_update_modem_start_nobackup)(char* file_path); |
l.yang | 7b93b55 | 2025-05-28 05:00:14 -0700 | [diff] [blame] | 87 | typedef int32_t (*gsw_update_modem_get_system)(void); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 88 | typedef int32_t (*gsw_update_modem_cancel)(void); |
| 89 | typedef int32_t (*gsw_update_modem_get_info)(gsw_update_info_s *update_info); |
| 90 | typedef int32_t (*gsw_update_modem_get_status)(gsw_system_status_s *system_status); |
| 91 | typedef int32_t (*gsw_update_modem_sync)(void); |
| 92 | typedef int32_t (*gsw_update_modem_switch)(void); |
| 93 | |
| 94 | gsw_update_modem_start_autobackup gsw_update_modem_start_autobackup_ptr = NULL; |
| 95 | |
| 96 | static void *ota_handle = NULL; |
| 97 | |
| 98 | st_api_test_case api_testcases[] = |
| 99 | { |
| 100 | {0, "print_help"}, |
| 101 | {1, "gsw_update_modem_start_autobackup"}, |
| 102 | {2, "gsw_update_modem_check_condition"}, |
| 103 | {3, "gsw_update_modem_result_query"}, |
| 104 | {4, "gsw_update_modem_get_system"}, |
| 105 | {5, "gsw_update_modem_get_info"}, |
| 106 | {6, "gsw_update_modem_get_status"}, |
| 107 | {7, "gsw_update_modem_sync"}, |
| 108 | {8, "gsw_update_modem_switch"}, |
| 109 | {9, "gsw_update_modem_start_nobackup"}, |
| 110 | {10, "gsw_update_modem_cancel"}, |
| 111 | {-1, NULL} |
| 112 | |
| 113 | }; |
| 114 | |
| 115 | void print_help(void) |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | printf("Supported test cases:\n"); |
| 120 | for(i = 0; ; i++) |
| 121 | { |
| 122 | if(api_testcases[i].cmdIdx == -1) |
| 123 | { |
| 124 | break; |
| 125 | } |
| 126 | printf("%d:\t%s\n", api_testcases[i].cmdIdx, api_testcases[i].funcName); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void *fota_thread(void *arg) |
| 131 | { |
| 132 | char *file_path = (char *)arg; |
| 133 | int ret = -1; |
| 134 | ret = gsw_update_modem_start_autobackup_ptr(file_path); |
| 135 | printf("gsw_update_modem_start_autobackup ret = %d\n", ret); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | void sigint_handler(int sig) |
| 140 | { |
| 141 | printf("sigint_handler\n"); |
| 142 | dlclose(ota_handle); |
| 143 | ota_handle = NULL; |
| 144 | gsw_update_modem_start_autobackup_ptr = NULL; |
| 145 | exit(0); |
| 146 | } |
| 147 | void clear_input_buffer() |
| 148 | { |
| 149 | int c; |
| 150 | |
| 151 | while ((c = getchar()) != '\n' && c != EOF); |
| 152 | } |
| 153 | |
| 154 | int main(int argc,char *argv[]) |
| 155 | { |
| 156 | gsw_update_modem_check_condition gsw_update_modem_check_condition_ptr = NULL; |
| 157 | gsw_update_modem_result_query gsw_update_modem_result_query_ptr = NULL; |
| 158 | gsw_update_modem_get_system gsw_update_modem_get_system_ptr = NULL; |
| 159 | gsw_update_modem_get_info gsw_update_modem_get_info_ptr = NULL; |
| 160 | gsw_update_modem_get_status gsw_update_modem_get_status_ptr = NULL; |
| 161 | gsw_update_modem_sync gsw_update_modem_sync_ptr = NULL; |
| 162 | gsw_update_modem_switch gsw_update_modem_switch_ptr = NULL; |
| 163 | gsw_update_modem_start_nobackup gsw_update_modem_start_nobackup_ptr = NULL; |
| 164 | gsw_update_modem_cancel gsw_update_modem_cancel_ptr = NULL; |
| 165 | |
| 166 | signal(SIGINT, sigint_handler); |
| 167 | if (argc < 2) |
| 168 | { |
| 169 | printf("Usage: %s <file_path>\n", argv[0]); |
| 170 | return -1; // 确保提供了文件路径 |
| 171 | } |
| 172 | char *file_path = argv[1]; |
| 173 | ota_handle = dlopen("/lib/libgsw_lib.so", RTLD_NOW ); |
| 174 | if(ota_handle == NULL) |
| 175 | { |
| 176 | printf("open lib failed\n"); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | gsw_update_modem_start_autobackup_ptr = (gsw_update_modem_start_autobackup)dlsym(ota_handle, "gsw_update_modem_start_autobackup"); |
| 181 | if(gsw_update_modem_start_autobackup_ptr == NULL) |
| 182 | { |
| 183 | printf("dlsym gsw_update_modem_start_autobackup failed\n"); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | gsw_update_modem_check_condition_ptr = (gsw_update_modem_check_condition)dlsym(ota_handle, "gsw_update_modem_check_condition"); |
| 188 | if(gsw_update_modem_check_condition_ptr == NULL) |
| 189 | { |
| 190 | printf("dlsym gsw_update_modem_check_condition failed\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | gsw_update_modem_result_query_ptr = (gsw_update_modem_result_query)dlsym(ota_handle, "gsw_update_modem_result_query"); |
| 195 | if(gsw_update_modem_result_query_ptr == NULL) |
| 196 | { |
| 197 | printf("dlsym gsw_update_modem_result_query failed\n"); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | gsw_update_modem_get_system_ptr = (gsw_update_modem_get_system)dlsym(ota_handle, "gsw_update_modem_get_system"); |
| 202 | if(gsw_update_modem_get_system_ptr == NULL) |
| 203 | { |
| 204 | printf("dlsym gsw_update_modem_get_system failed\n"); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | gsw_update_modem_get_info_ptr = (gsw_update_modem_get_info)dlsym(ota_handle, "gsw_update_modem_get_info"); |
| 209 | if(gsw_update_modem_get_info_ptr == NULL) |
| 210 | { |
| 211 | printf("dlsym gsw_update_modem_get_info failed\n"); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | gsw_update_modem_get_status_ptr = (gsw_update_modem_get_status)dlsym(ota_handle, "gsw_update_modem_get_status"); |
| 216 | if(gsw_update_modem_get_status_ptr == NULL) |
| 217 | { |
| 218 | printf("dlsym gsw_update_modem_get_status failed\n"); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | gsw_update_modem_sync_ptr = (gsw_update_modem_sync)dlsym(ota_handle, "gsw_update_modem_sync"); |
| 223 | if(gsw_update_modem_sync_ptr == NULL) |
| 224 | { |
| 225 | printf("dlsym gsw_update_modem_sync failed\n"); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | gsw_update_modem_switch_ptr = (gsw_update_modem_switch)dlsym(ota_handle, "gsw_update_modem_switch"); |
| 230 | if(gsw_update_modem_switch_ptr == NULL) |
| 231 | { |
| 232 | printf("dlsym gsw_update_modem_switch failed\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | gsw_update_modem_start_nobackup_ptr = (gsw_update_modem_start_nobackup)dlsym(ota_handle, "gsw_update_modem_start_nobackup"); |
| 237 | if(gsw_update_modem_start_nobackup_ptr == NULL) |
| 238 | { |
| 239 | printf("dlsym gsw_update_modem_start_nobackup failed\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | gsw_update_modem_cancel_ptr = (gsw_update_modem_cancel)dlsym(ota_handle, "gsw_update_modem_cancel"); |
| 244 | if(gsw_update_modem_cancel_ptr == NULL) |
| 245 | { |
| 246 | printf("dlsym gsw_update_modem_cancel failed\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | printf("Enter ota api test \n"); |
| 253 | int opt = -1; |
| 254 | print_help(); |
| 255 | while (1) |
| 256 | { |
| 257 | char buffer[100]; // 用于存储输入的缓冲区 |
| 258 | |
| 259 | printf("请输入选项: \n"); |
| 260 | if (fgets(buffer, sizeof(buffer), stdin) != NULL) |
| 261 | { |
| 262 | |
| 263 | if (sscanf(buffer, "%d", &opt) != 1) |
| 264 | { |
| 265 | printf("无效输入,请输入一个整数。\n"); |
| 266 | clear_input_buffer(); |
| 267 | continue; |
| 268 | } |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | |
| 273 | clear_input_buffer(); |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | printf("输入的整数是:%d\n", opt); |
| 278 | |
| 279 | switch(opt) |
| 280 | { |
| 281 | case 0 : |
| 282 | { |
| 283 | print_help(); |
| 284 | break; |
| 285 | } |
| 286 | case 1 : |
| 287 | { |
| 288 | int32_t ret = -1; |
| 289 | pthread_t thread_id_reboot; |
| 290 | ret = pthread_create(&thread_id_reboot, NULL, fota_thread, (void*)file_path); |
| 291 | if (ret != 0) |
| 292 | { |
| 293 | printf("pthread_create failed \n"); |
| 294 | return -1; |
| 295 | } |
| 296 | break; |
| 297 | |
| 298 | } |
| 299 | case 2 : |
| 300 | { |
| 301 | bool ret = false; |
| 302 | ret = gsw_update_modem_check_condition_ptr(); |
| 303 | printf("gsw_update_modem_check_condition ret = %d\n", ret); |
| 304 | break; |
| 305 | } |
| 306 | case 3 : |
| 307 | { |
| 308 | E_GSW_OTA_RET ret = -1; |
| 309 | ret = gsw_update_modem_result_query_ptr(); |
| 310 | printf("gsw_update_modem_result_query ret = %d\n", ret); |
| 311 | break; |
| 312 | } |
| 313 | case 4 : |
| 314 | { |
| 315 | E_GSW_OTA_SYSTEM ret = -1; |
| 316 | ret = gsw_update_modem_get_system_ptr(); |
| 317 | printf("gsw_update_modem_get_system ret = %d\n", ret); |
| 318 | printf("system %c\n", ret == 0? 'a' : 'b'); |
| 319 | |
| 320 | break; |
| 321 | } |
| 322 | case 5 : |
| 323 | { |
| 324 | int32_t ret = -1; |
| 325 | gsw_update_info_s update_info = {0}; |
| 326 | ret = gsw_update_modem_get_info_ptr(&update_info); |
| 327 | printf("gsw_update_modem_get_info ret = %d\n", ret); |
| 328 | |
| 329 | printf("update_info. = %u\n",update_info.percentage); |
| 330 | printf("update_state. = %d\n",update_info.update_state); |
| 331 | printf("exit_code. = %d\n",update_info.exit_code); |
| 332 | |
| 333 | break; |
| 334 | } |
| 335 | case 6: |
| 336 | { |
| 337 | int32_t ret = -1; |
| 338 | gsw_system_status_s system_status = {0}; |
| 339 | ret = gsw_update_modem_get_status_ptr(&system_status); |
| 340 | printf("gsw_update_modem_get_status ret is %d\n",ret); |
| 341 | break; |
| 342 | |
| 343 | } |
| 344 | case 7: |
| 345 | { |
| 346 | int32_t ret = -1; |
| 347 | ret = gsw_update_modem_sync_ptr(); |
| 348 | printf("gsw_update_modem_sync ret is %d\n",ret ); |
| 349 | break; |
| 350 | } |
| 351 | case 8 : |
| 352 | { |
| 353 | int32_t ret = -1; |
| 354 | ret = gsw_update_modem_switch_ptr(); |
| 355 | printf("gsw_update_modem_switch ret is %d \n",ret); |
| 356 | break; |
| 357 | |
| 358 | } |
| 359 | case 9: |
| 360 | { |
| 361 | int32_t ret = -1; |
| 362 | ret = gsw_update_modem_start_nobackup_ptr(file_path); |
| 363 | printf("gsw_update_modem_start_nobackup ret is %d\n",ret); |
| 364 | break; |
| 365 | } |
| 366 | case 10: |
| 367 | { |
| 368 | int32_t ret = -1; |
| 369 | ret = gsw_update_modem_cancel_ptr(); |
| 370 | printf("gsw_update_modem_cancel ret is %d\n",ret); |
| 371 | break; |
| 372 | } |
| 373 | default: |
| 374 | printf("invalid opt\n"); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | } |
| 379 | |
| 380 | |
| 381 | return 0; |
| 382 | |
| 383 | |
| 384 | } |
| 385 | |