http_client.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "utils_httpc.h"
  19. #include "lite-utils.h"
  20. #include "ca.h"
  21. #include "utils_sha2.h"
  22. int IOT_HTTP_Get_Token(const char *product_sn, const char *device_sn, const char *device_sercret, char *token)
  23. {
  24. int ret = SUCCESS_RET;
  25. http_client_t *http_client_post = (http_client_t *)HAL_Malloc(sizeof(http_client_t));
  26. if(NULL == http_client_post)
  27. {
  28. LOG_ERROR("http_client_post malloc fail\n");
  29. return FAILURE_RET;
  30. }
  31. http_client_data_t *http_data_post = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t));
  32. if(NULL == http_data_post)
  33. {
  34. HAL_Free(http_client_post);
  35. LOG_ERROR("http_data_post malloc fail\n");
  36. return FAILURE_RET;
  37. }
  38. memset(http_client_post, 0, sizeof(http_client_t));
  39. memset(http_data_post, 0, sizeof(http_client_data_t));
  40. http_data_post->response_buf = (char *)HAL_Malloc(1024);
  41. if(NULL == http_data_post->response_buf)
  42. {
  43. HAL_Free(http_client_post);
  44. HAL_Free(http_data_post);
  45. LOG_ERROR("http_data_post->response_buf malloc fail\n");
  46. return FAILURE_RET;
  47. }
  48. memset(http_data_post->response_buf, 0, 1024);
  49. http_data_post->response_buf_len = 1024;
  50. http_data_post->post_content_type = (char *)"application/json";
  51. http_data_post->post_buf = (unsigned char *)HAL_Malloc(1024);
  52. if(NULL == http_data_post->post_buf)
  53. {
  54. HAL_Free(http_client_post);
  55. HAL_Free(http_data_post->response_buf);
  56. HAL_Free(http_data_post);
  57. LOG_ERROR("http_data_post->post_buf malloc fail\n");
  58. return FAILURE_RET;
  59. }
  60. memset(http_data_post->post_buf, 0, 1024);
  61. HAL_Snprintf((char *)http_data_post->post_buf, 1024, "{\"ProductSN\":\"%s\",\"DeviceSN\":\"%s\"}",product_sn, device_sn);
  62. http_data_post->post_buf_len = strlen((char *)http_data_post->post_buf);
  63. uint8_t mac_output_hex[32] = {0};
  64. char mac_output_char[65] = {0};
  65. utils_hmac_sha256((const uint8_t *)http_data_post->post_buf, http_data_post->post_buf_len, (const uint8_t *)device_sercret, strlen(device_sercret), mac_output_hex);
  66. LITE_hexbuf_convert((unsigned char *)mac_output_hex, mac_output_char, 32, 0);
  67. LOG_DEBUG("hmac:%s\r\n",mac_output_char);
  68. http_client_post->header = (char *)HAL_Malloc(1024);
  69. if(NULL == http_client_post->header)
  70. {
  71. HAL_Free(http_client_post);
  72. HAL_Free(http_data_post->response_buf);
  73. HAL_Free(http_data_post->post_buf);
  74. HAL_Free(http_data_post);
  75. LOG_ERROR("http_client_post->header malloc fail\n");
  76. return FAILURE_RET;
  77. }
  78. memset(http_client_post->header, 0, 1024);
  79. HAL_Snprintf(http_client_post->header, 1024, "Content-Type: application/json\r\nAuthorization: %s\r\nbody: %s\r\n",mac_output_char,http_data_post->post_buf);
  80. const char *ca_crt = iot_https_ca_get();
  81. char *url = (char *)"https://http-cn-sh2.iot.ucloud.cn/auth";
  82. ret = http_client_common(http_client_post, url, 443, ca_crt, HTTP_POST, http_data_post,5000);
  83. if(SUCCESS_RET != ret)
  84. {
  85. LOG_ERROR("HTTP_POST error\n");
  86. goto end;
  87. }
  88. ret = http_client_recv_data(http_client_post, 5000, http_data_post);
  89. if(SUCCESS_RET != ret)
  90. {
  91. LOG_ERROR("http_client_recv_data error\n");
  92. goto end;
  93. }
  94. LOG_DEBUG("response_buf:%s\n",http_data_post->response_buf);
  95. char *temp = LITE_json_value_of((char *)"Token", http_data_post->response_buf);
  96. if(NULL == temp)
  97. {
  98. LOG_ERROR("parse Token error\n");
  99. char *temp_message = LITE_json_value_of((char *)"Message", http_data_post->response_buf);
  100. LOG_ERROR("%s\n", temp_message);
  101. HAL_Free(temp_message);
  102. goto end;
  103. }
  104. strncpy(token,temp,strlen(temp));
  105. token[strlen(temp)+1] = '\0';
  106. LOG_DEBUG("token:%s\n",token);
  107. HAL_Free(temp);
  108. end:
  109. http_client_close(http_client_post);
  110. HAL_Free(http_client_post->header);
  111. HAL_Free(http_client_post);
  112. HAL_Free(http_data_post->response_buf);
  113. HAL_Free(http_data_post->post_buf);
  114. HAL_Free(http_data_post);
  115. return ret;
  116. }
  117. int IOT_HTTP_Publish(char *token, char *topic, char *data, uint32_t timeout_ms)
  118. {
  119. int ret = SUCCESS_RET;
  120. http_client_t *http_client_post = (http_client_t *)HAL_Malloc(sizeof(http_client_t));
  121. if(NULL == http_client_post)
  122. {
  123. LOG_ERROR("http_client_post malloc fail\n");
  124. return FAILURE_RET;
  125. }
  126. http_client_data_t *http_data_post = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t));
  127. if(NULL == http_data_post)
  128. {
  129. HAL_Free(http_client_post);
  130. LOG_ERROR("http_data_post malloc fail\n");
  131. return FAILURE_RET;
  132. }
  133. memset(http_client_post, 0, sizeof(http_client_t));
  134. memset(http_data_post, 0, sizeof(http_client_data_t));
  135. http_data_post->response_buf = (char *)HAL_Malloc(1024);
  136. if(NULL == http_data_post->response_buf)
  137. {
  138. HAL_Free(http_client_post);
  139. HAL_Free(http_data_post);
  140. LOG_ERROR("http_data_post->response_buf malloc fail\n");
  141. return FAILURE_RET;
  142. }
  143. memset(http_data_post->response_buf, 0, 1024);
  144. http_data_post->response_buf_len = 1024;
  145. http_data_post->post_content_type = (char *)"application/octet-stream";
  146. http_data_post->post_buf = (unsigned char *)HAL_Malloc(1024);
  147. if(NULL == http_data_post->post_buf)
  148. {
  149. HAL_Free(http_client_post);
  150. HAL_Free(http_data_post->response_buf);
  151. HAL_Free(http_data_post);
  152. LOG_ERROR("http_data_post->post_buf malloc fail\n");
  153. return FAILURE_RET;
  154. }
  155. memset(http_data_post->post_buf, 0, 1024);
  156. HAL_Snprintf((char *)http_data_post->post_buf, 1024, "%s",data);
  157. http_data_post->post_buf_len = strlen((char *)http_data_post->post_buf);
  158. http_client_post->header = (char *)HAL_Malloc(1024);
  159. if(NULL == http_client_post->header)
  160. {
  161. HAL_Free(http_client_post);
  162. HAL_Free(http_data_post->response_buf);
  163. HAL_Free(http_data_post->post_buf);
  164. HAL_Free(http_data_post);
  165. LOG_ERROR("http_client_post->header malloc fail\n");
  166. return FAILURE_RET;
  167. }
  168. memset(http_client_post->header, 0, 1024);
  169. HAL_Snprintf(http_client_post->header, 1024, "Password: %s\r\nContent-Type: application/octet-stream\r\nbody: %s\r\n", token, http_data_post->post_buf);
  170. const char *ca_crt = iot_https_ca_get();
  171. char *url = (char *)HAL_Malloc(256);
  172. if(NULL == url)
  173. {
  174. goto end;
  175. LOG_ERROR("http_client_post->header malloc fail\n");
  176. return FAILURE_RET;
  177. }
  178. memset(url, 0, 256);
  179. HAL_Snprintf(url, 256, "https://http-cn-sh2.iot.ucloud.cn/topic/%s",topic);
  180. ret = http_client_common(http_client_post, url, 443, ca_crt, HTTP_POST, http_data_post,5000);
  181. if(SUCCESS_RET != ret)
  182. {
  183. LOG_ERROR("HTTP_POST error\n");
  184. goto end;
  185. }
  186. HAL_Free(url);
  187. ret = http_client_recv_data(http_client_post, 5000, http_data_post);
  188. if(SUCCESS_RET != ret)
  189. {
  190. LOG_ERROR("http_client_recv_data error\n");
  191. goto end;
  192. }
  193. LOG_DEBUG("response_buf:%s\n",http_data_post->response_buf);
  194. char *temp_message = LITE_json_value_of((char *)"Message", http_data_post->response_buf);
  195. if(NULL == temp_message)
  196. {
  197. LOG_ERROR("parse Message error\n");
  198. goto end;
  199. }
  200. LOG_DEBUG("%s\n", temp_message);
  201. HAL_Free(temp_message);
  202. end:
  203. http_client_close(http_client_post);
  204. HAL_Free(http_client_post->header);
  205. HAL_Free(http_client_post);
  206. HAL_Free(http_data_post->response_buf);
  207. HAL_Free(http_data_post->post_buf);
  208. HAL_Free(http_data_post);
  209. return ret;
  210. }