ota_fetch.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. * Licensed under the MIT License (the "License"); you may not use this file
  6. except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://opensource.org/licenses/MIT
  9. * Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is
  11. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND,
  13. * either express or implied. See the License for the specific language
  14. governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "ota_fetch.h"
  22. #include <string.h>
  23. #include "qcloud_iot_ca.h"
  24. #include "qcloud_iot_export.h"
  25. #include "qcloud_iot_import.h"
  26. #include "utils_httpc.h"
  27. #define OTA_HTTP_HEAD_CONTENT_LEN 256
  28. /* ofc, OTA fetch channel */
  29. typedef struct {
  30. const char * url;
  31. HTTPClient http; /* http client */
  32. HTTPClientData http_data; /* http client data */
  33. } OTAHTTPStruct;
  34. #ifdef OTA_USE_HTTPS
  35. static int is_begin_with(const char *str1, char *str2)
  36. {
  37. if (str1 == NULL || str2 == NULL)
  38. return -1;
  39. int len1 = strlen(str1);
  40. int len2 = strlen(str2);
  41. if ((len1 < len2) || (len1 == 0 || len2 == 0))
  42. return -1;
  43. char *p = str2;
  44. int i = 0;
  45. while (*p != '\0') {
  46. if (*p != str1[i])
  47. return 0;
  48. p++;
  49. i++;
  50. }
  51. return 1;
  52. }
  53. #endif
  54. static char sg_head_content[OTA_HTTP_HEAD_CONTENT_LEN];
  55. void * ofc_Init(unsigned char profile_idx, const char *url, uint32_t offset, uint32_t size)
  56. {
  57. OTAHTTPStruct *h_odc;
  58. if (NULL == (h_odc = HAL_Malloc(sizeof(OTAHTTPStruct)))) {
  59. Log_e("allocate for h_odc failed");
  60. return NULL;
  61. }
  62. memset(h_odc, 0, sizeof(OTAHTTPStruct));
  63. memset(sg_head_content, 0, OTA_HTTP_HEAD_CONTENT_LEN);
  64. HAL_Snprintf(sg_head_content, OTA_HTTP_HEAD_CONTENT_LEN,
  65. "Accept: "
  66. "text/html,application/xhtml+xml,application/xml;q=0.9,*/"
  67. "*;q=0.8\r\n"
  68. "Accept-Encoding: gzip, deflate\r\n"
  69. "Range: bytes=%d-%d\r\n",
  70. offset, size);
  71. Log_d("head_content:%s", sg_head_content);
  72. /* set http request-header parameter */
  73. h_odc->http.header = sg_head_content;
  74. h_odc->http.profile_idx = profile_idx;
  75. h_odc->url = url;
  76. return h_odc;
  77. }
  78. int32_t qcloud_ofc_connect(void *handle)
  79. {
  80. IOT_FUNC_ENTRY;
  81. OTAHTTPStruct *h_odc = (OTAHTTPStruct *)handle;
  82. int port = 80;
  83. const char *ca_crt = NULL;
  84. #ifdef OTA_USE_HTTPS
  85. if (is_begin_with(h_odc->url, "https")) {
  86. port = 443;
  87. ca_crt = iot_https_ca_get();
  88. }
  89. #endif
  90. int32_t rc = qcloud_http_client_common(&h_odc->http, h_odc->url, port, ca_crt, HTTP_GET, &h_odc->http_data);
  91. IOT_FUNC_EXIT_RC(rc);
  92. }
  93. int32_t qcloud_ofc_fetch(void *handle, char *buf, uint32_t bufLen, uint32_t timeout_s)
  94. {
  95. IOT_FUNC_ENTRY;
  96. int diff;
  97. OTAHTTPStruct *h_odc = (OTAHTTPStruct *)handle;
  98. h_odc->http_data.response_buf = buf;
  99. h_odc->http_data.response_buf_len = bufLen;
  100. diff = h_odc->http_data.response_content_len - h_odc->http_data.retrieve_len;
  101. int rc = qcloud_http_recv_data(&h_odc->http, timeout_s * 1000, &h_odc->http_data);
  102. if (QCLOUD_RET_SUCCESS != rc) {
  103. if (rc == QCLOUD_ERR_HTTP_NOT_FOUND)
  104. IOT_FUNC_EXIT_RC(IOT_OTA_ERR_FETCH_NOT_EXIST);
  105. if (rc == QCLOUD_ERR_HTTP_AUTH)
  106. IOT_FUNC_EXIT_RC(IOT_OTA_ERR_FETCH_AUTH_FAIL);
  107. if (rc == QCLOUD_ERR_HTTP_TIMEOUT)
  108. IOT_FUNC_EXIT_RC(IOT_OTA_ERR_FETCH_TIMEOUT);
  109. IOT_FUNC_EXIT_RC(rc);
  110. }
  111. IOT_FUNC_EXIT_RC(h_odc->http_data.response_content_len - h_odc->http_data.retrieve_len - diff);
  112. }
  113. int qcloud_ofc_deinit(void *handle)
  114. {
  115. IOT_FUNC_ENTRY;
  116. OTAHTTPStruct *h_odc = (OTAHTTPStruct *)handle;
  117. if (NULL == handle)
  118. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  119. if (h_odc->http.network_stack.is_connected(&h_odc->http.network_stack)) {
  120. h_odc->http.network_stack.disconnect(&h_odc->http.network_stack);
  121. }
  122. HAL_Free(handle);
  123. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  124. }
  125. #ifdef __cplusplus
  126. }
  127. #endif