http_post_demo.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "ql_api_osi.h"
  16. #include "ql_fs.h"
  17. #include "ql_log.h"
  18. #include "ql_api_datacall.h"
  19. #include "ql_http_client.h"
  20. #define QL_HTTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  21. #define QL_HTTP_LOG(msg, ...) QL_LOG(QL_HTTP_LOG_LEVEL, "ql_HTTP", msg, ##__VA_ARGS__)
  22. #define QL_HTTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_HTTP", msg, ##__VA_ARGS__)
  23. ql_task_t http_post_task = NULL;
  24. static http_client_t http_cli = 0;
  25. static ql_sem_t http_semp;
  26. char upload_file[] = "UFS:form-data.txt";
  27. int ql_create_upload_file(void)
  28. {
  29. int err = QL_OSI_SUCCESS;
  30. int fd = 0;
  31. char test_str[] = {"POST /Jocelyn/processorder.php HTTP/1.1\r\n\
  32. Host: 220.180.239.212:8300\r\n\
  33. Content-type: application/x-www-form-urlencoded\r\n\
  34. User-Agent: Quectel-Module\r\n\
  35. Accept: */*\r\n\
  36. Content-length: 48\r\n\
  37. Connection: Keep-Alive\r\n\
  38. \r\n\
  39. Message=1111&Appleqty=2222&Orangeqty=3333&find=1"};
  40. /* write http file */
  41. fd = ql_fopen(upload_file, "wb+");
  42. if(fd < 0)
  43. {
  44. QL_HTTP_LOG("file failed");
  45. err = fd;
  46. goto exit;
  47. }
  48. err = ql_fwrite(test_str, strlen(test_str), 1, fd);
  49. if(err < 0)
  50. {
  51. QL_HTTP_LOG("file failed");
  52. ql_fclose(fd);
  53. goto exit;
  54. }else{
  55. err = QL_OSI_SUCCESS;
  56. }
  57. ql_fclose(fd);
  58. exit:
  59. if(err < 0)
  60. {
  61. QL_HTTP_LOG("errcode is %x", err);
  62. }
  63. return err;
  64. }
  65. static void http_event_cb(http_client_t *client, int evt, int evt_code, void *arg)
  66. {
  67. QL_HTTP_LOG("*client:%d, http_cli:%d", *client, http_cli);
  68. if(*client != http_cli)
  69. return;
  70. QL_HTTP_LOG("evt:%d, evt_code:%d", evt, evt_code);
  71. switch(evt){
  72. case HTTP_EVENT_SESSION_ESTABLISH:{
  73. if(evt_code != QL_HTTP_OK){
  74. QL_HTTP_LOG("HTTP session create failed!!!!!");
  75. ql_rtos_semaphore_release(http_semp);
  76. }
  77. }
  78. break;
  79. case HTTP_EVENT_RESPONE_STATE_LINE:{
  80. if(evt_code == QL_HTTP_OK){
  81. int resp_code = 0;
  82. int content_length = 0;
  83. int chunk_encode = 0;
  84. char *location = NULL;
  85. ql_httpc_getinfo(client, HTTP_INFO_RESPONSE_CODE, &resp_code);
  86. QL_HTTP_LOG("response code:%d", resp_code);
  87. ql_httpc_getinfo(client, HTTP_INFO_CHUNK_ENCODE, &chunk_encode);
  88. if(chunk_encode == 0){
  89. ql_httpc_getinfo(client, HTTP_INFO_CONTENT_LEN, &content_length);
  90. QL_HTTP_LOG("content_length:%d",content_length);
  91. }else{
  92. QL_HTTP_LOG("http chunk encode!!!");
  93. }
  94. if(resp_code >= 300 && resp_code < 400){
  95. ql_httpc_getinfo(client, HTTP_INFO_LOCATION, &location);
  96. QL_HTTP_LOG("redirect location:%s", location);
  97. free(location);
  98. }
  99. }
  100. }
  101. break;
  102. case HTTP_EVENT_SESSION_DISCONNECT:{
  103. if(evt_code == QL_HTTP_OK){
  104. QL_HTTP_LOG("===http transfer end!!!!");
  105. }else{
  106. QL_HTTP_LOG("===http transfer occur exception!!!!!");
  107. }
  108. ql_rtos_semaphore_release(http_semp);
  109. }
  110. break;
  111. }
  112. }
  113. static int http_write_response_data(http_client_t *client, void *arg, char *data, int size, unsigned char end)
  114. {
  115. unsigned char *recbuff=NULL;
  116. QL_HTTP_LOG("recv size:%d", size);
  117. recbuff=malloc(size+1);
  118. if(NULL!=recbuff)
  119. {
  120. memset(recbuff, 0, size+1);
  121. memcpy(recbuff, data, size);
  122. QL_HTTP_LOG("recv data: %s", recbuff);
  123. free(recbuff);
  124. recbuff=NULL;
  125. }
  126. return size;
  127. }
  128. static void http_post_app_thread(void * arg)
  129. {
  130. int ret = 0,i = 0;
  131. int profile_idx = 1;
  132. int run_num = 0;
  133. uint8_t nSim = 0;
  134. char ip4_addr_str[16] = {0};
  135. char url[] = "http://220.180.239.212:8300";
  136. char *username = "admin";
  137. char *password = "admin";
  138. char author_info[64] = {0};
  139. int raw_request = 1;
  140. ql_data_call_info_s info;
  141. snprintf(author_info, 64, "%s:%s", username, password);
  142. ql_rtos_task_sleep_s(5);
  143. QL_HTTP_LOG("========== http new demo start ==========");
  144. QL_HTTP_LOG("wait for network register done");
  145. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  146. i++;
  147. ql_rtos_task_sleep_ms(1000);
  148. }
  149. if(ret == 0){
  150. i = 0;
  151. QL_HTTP_LOG("====network registered!!!!====");
  152. }else{
  153. QL_HTTP_LOG("====network register failure!!!!!====");
  154. goto exit;
  155. }
  156. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  157. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  158. if(ret != 0){
  159. QL_HTTP_LOG("====data call failure!!!!=====");
  160. }
  161. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  162. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  163. if(ret != 0){
  164. QL_HTTP_LOG("ql_get_data_call_info ret: %d", ret);
  165. ql_stop_data_call(nSim, profile_idx);
  166. goto exit;
  167. }
  168. QL_HTTP_LOG("info->profile_idx: %d", info.profile_idx);
  169. QL_HTTP_LOG("info->ip_version: %d", info.ip_version);
  170. QL_HTTP_LOG("info->v4.state: %d", info.v4.state);
  171. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  172. QL_HTTP_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  173. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  174. QL_HTTP_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  175. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  176. QL_HTTP_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  177. ql_rtos_semaphore_create(&http_semp, 0);
  178. if(ql_create_upload_file() != 0){
  179. QL_HTTP_LOG("create_upload_file failed.");
  180. goto exit;
  181. }
  182. do {
  183. QL_HTTP_LOG("==============http_client_post_test[%d]================\n",run_num+1);
  184. if(ql_httpc_new(&http_cli, http_event_cb, NULL) != QL_HTTP_OK){
  185. QL_HTTP_LOG("http client create failed!!!!");
  186. break;
  187. }
  188. //自定义请求头和body以文件的方式upload
  189. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_SIM_ID, nSim);
  190. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_PDPCID, profile_idx);
  191. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_URL, (char *)url);
  192. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_WRITE_FUNC, http_write_response_data);
  193. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_METHOD, HTTP_METHOD_POST);
  194. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_RAW_REQUEST, &raw_request);
  195. ql_httpc_setopt(&http_cli, HTTP_CLIENT_OPT_RAW_FILE, upload_file);
  196. if(ql_httpc_perform(&http_cli) == QL_HTTP_OK)
  197. {
  198. QL_HTTP_LOG("wait http perform end!!!!!!");
  199. ql_rtos_semaphore_wait(http_semp, QL_WAIT_FOREVER);
  200. }
  201. else
  202. {
  203. QL_HTTP_LOG("http perform failed!!!!!!!!!!");
  204. }
  205. ql_httpc_release(&http_cli);
  206. http_cli = 0;
  207. QL_HTTP_LOG("==============http_client_test_end[%d]================\n",run_num+1);
  208. run_num++;
  209. ql_rtos_task_sleep_s(20);
  210. }while(run_num < 10);
  211. exit:
  212. ql_rtos_semaphore_delete(http_semp);
  213. ql_rtos_task_delete(http_post_task);
  214. return;
  215. }
  216. void ql_http_post_app_init(void)
  217. {
  218. QlOSStatus err = QL_OSI_SUCCESS;
  219. err = ql_rtos_task_create(&http_post_task, 4096, APP_PRIORITY_REALTIME, "QhttpPost", http_post_app_thread, NULL, 5);
  220. if(err != QL_OSI_SUCCESS)
  221. {
  222. QL_HTTP_LOG("created task failed");
  223. }
  224. }