cloudota_demo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <time.h>
  17. #include "ql_api_osi.h"
  18. #include "ql_api_nw.h"
  19. #include "ql_log.h"
  20. #include "ql_api_datacall.h"
  21. #include "ql_cloud_ota.h"
  22. #include "ql_power.h"
  23. #define QL_CTSREG_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define cloudota_printf(msg, ...) QL_LOG(QL_CTSREG_LOG_LEVEL, "ql_cloudota_DEMO", msg, ##__VA_ARGS__)
  25. #define cloudota_printf_psuh(msg, ...) QL_LOG_PUSH("ql_cloudota_DEMO", msg, ##__VA_ARGS__)
  26. static ql_task_t cloudota_task = NULL;
  27. static void cloudota_cb(qlHttpFotaStart start, int progress)
  28. {
  29. cloudota_printf("\r\n[cloudota_cb]start(%d) progress:%d \r\n", start, progress);
  30. switch(start)
  31. {
  32. case QL_HTTPFOTA_PUB_DOWN_FIRMWARE_SUCCESS:
  33. cloudota_printf("upgrade success\r\n");
  34. break;
  35. case QL_HTTPFOTA_PUB_DOWN_FIRMWARE_UPGRADE:
  36. cloudota_printf("reboot to upgrade\r\n");
  37. ql_power_reset(RESET_NORMAL);
  38. break;
  39. case QL_HTTPFOTA_PUB_GET_VERSION_FAIL:
  40. cloudota_printf("Get targetversion fail\r\n");
  41. break;
  42. default:
  43. cloudota_printf("start=%d",start);
  44. break;
  45. }
  46. }
  47. static void cloudota_app_thread(void * arg)
  48. {
  49. int ret = 0;
  50. int i = 0;
  51. int profile_idx = 1;
  52. ql_data_call_info_s info;
  53. char ip4_addr_str[16] = {0};
  54. uint8_t sim_id = 0;
  55. bool have_version = FALSE;
  56. char target_version[64] = {0};
  57. ql_rtos_task_sleep_s(10);
  58. cloudota_printf("==========cloudota demo start ==========");
  59. cloudota_printf("wait for network register done");
  60. while((ret = ql_network_register_wait(sim_id, 120)) != 0 && i < 10){
  61. i++;
  62. ql_rtos_task_sleep_s(1);
  63. }
  64. if(ret == 0){
  65. i = 0;
  66. cloudota_printf("====network registered!!!!====");
  67. }else{
  68. cloudota_printf("====network register failure!!!!!====");
  69. goto exit;
  70. }
  71. ql_set_data_call_asyn_mode(sim_id, profile_idx, 0);
  72. cloudota_printf("===start data call====");
  73. ret=ql_start_data_call(sim_id, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  74. cloudota_printf("===data call result:%d", ret);
  75. if(ret != 0){
  76. cloudota_printf("====data call failure!!!!=====");
  77. }
  78. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  79. ret = ql_get_data_call_info(sim_id, profile_idx, &info);
  80. if(ret != 0){
  81. cloudota_printf("ql_get_data_call_info ret: %d", ret);
  82. ql_stop_data_call(sim_id, profile_idx);
  83. goto exit;
  84. }
  85. cloudota_printf("info->profile_idx: %d", info.profile_idx);
  86. cloudota_printf("info->ip_version: %d", info.ip_version);
  87. cloudota_printf("info->v4.state: %d", info.v4.state);
  88. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  89. cloudota_printf("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  90. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  91. cloudota_printf("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  92. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  93. cloudota_printf("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  94. ql_cloudota_init();
  95. cloudota_printf("start check version\r\n");
  96. have_version = ql_check_cloudota_info(profile_idx, target_version, 64, cloudota_cb);
  97. if(have_version == FALSE)
  98. {
  99. cloudota_printf("\r\nNot have new version\r\n");
  100. goto exit;
  101. }
  102. have_version = ql_cloudota_download(profile_idx, cloudota_cb);
  103. if(have_version != 0)
  104. {
  105. cloudota_printf("\r\nql_cloudota_download error\r\n");
  106. goto exit;
  107. }
  108. exit:
  109. ql_rtos_task_delete(cloudota_task);
  110. return;
  111. }
  112. int ql_cloudota_app_init(void)
  113. {
  114. QlOSStatus err = QL_OSI_SUCCESS;
  115. err = ql_rtos_task_create(&cloudota_task, 5*1024, APP_PRIORITY_ABOVE_NORMAL, "cloudota_app", cloudota_app_thread, NULL, 5);
  116. if(err != QL_OSI_SUCCESS)
  117. {
  118. cloudota_printf("ctsreg_app init failed");
  119. }
  120. return err;
  121. }