connect_utils.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright 2024 NXP
  3. * NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
  4. * accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
  5. * activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
  6. * comply with and are bound by, such license terms. If you do not agree to be bound by the applicable license
  7. * terms, then you may not retain, install, activate or otherwise use the software.
  8. */
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <pthread.h>
  12. #include "connect_utils.h"
  13. #include "freemaster_client.h"
  14. #define BUFFER_SIZE 128*1024
  15. #define RECV_RETRY_COUNT 40
  16. #define RECV_RETRY_INTERVAL 25 /*ns*/
  17. #if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
  18. extern pthread_mutex_t jsonrpc_mutex;
  19. /*define the result struct.*/
  20. struct write_result {
  21. char *data;
  22. int pos;
  23. };
  24. /* show websocket error message*/
  25. void websocket_error(CURLcode res, char *message)
  26. {
  27. size_t len = 0;
  28. if (message != NULL) len = strlen(message);
  29. fprintf(stderr, "FreeMASTER: (%d) ", res);
  30. if(len) {
  31. fprintf(stderr, "%s%s", message,
  32. ((message[len - 1] != '\n') ? "\n" : ""));
  33. } else {
  34. fprintf(stderr, "%s\n", curl_easy_strerror(res));
  35. }
  36. }
  37. /* init and connect the websocket instance*/
  38. CURL * websocket_connect(const char *ws_url)
  39. {
  40. CURL *ws_curl;
  41. CURLcode res;
  42. #ifdef DEBUG
  43. fprintf(stdout, "\nInit curl connection to %s\n", ws_url);
  44. #endif
  45. ws_curl = curl_easy_init();
  46. if (ws_curl == NULL)
  47. {
  48. fprintf(stderr, "ERROR: Failed to start a curl session.\n");
  49. return NULL;
  50. }
  51. /*set the ws link address`*/
  52. curl_easy_setopt(ws_curl, CURLOPT_URL, ws_url);
  53. /*set the websocket option: stop when connected to target server*/
  54. curl_easy_setopt(ws_curl, CURLOPT_CONNECT_ONLY, 2L);
  55. /* Perform the request, res will get the return code */
  56. res = curl_easy_perform(ws_curl);
  57. if (res != CURLE_OK)
  58. {
  59. websocket_error(res, NULL);
  60. curl_easy_cleanup(ws_curl);
  61. return NULL;
  62. }
  63. return ws_curl;
  64. }
  65. /* close the connection */
  66. void websocket_close(CURL *ws_curl)
  67. {
  68. size_t sent;
  69. (void)curl_ws_send(ws_curl, "", 0, &sent, 0, CURLWS_CLOSE);
  70. /* always cleanup */
  71. curl_easy_cleanup(ws_curl);
  72. return;
  73. }
  74. /*send and got the receive*/
  75. char *websocket_request(CURL *ws_curl, char *params)
  76. {
  77. const struct curl_ws_frame *meta;
  78. size_t send, rlen;
  79. char recv_buffer[BUFFER_SIZE];
  80. CURLcode res;
  81. if (ws_curl == NULL)
  82. {
  83. websocket_error(CURLE_COULDNT_CONNECT, NULL);
  84. connect_init();
  85. return NULL;
  86. }
  87. #ifdef DEBUG
  88. fprintf(stdout, "Sending request: %s\n", params);
  89. #endif
  90. // fprintf(stdout, "Sending request: %s\n", params);
  91. res = curl_ws_send(ws_curl, params, strlen(params), &send, 0, CURLWS_TEXT);
  92. if (res != CURLE_OK)
  93. {
  94. websocket_error(res, NULL);
  95. curl_easy_cleanup(ws_curl);
  96. connect_init();
  97. return NULL;
  98. }
  99. /* retry until ws server is ready*/
  100. bool timeout = false;
  101. int count = 1;
  102. while (count < RECV_RETRY_COUNT) {
  103. #ifdef DEBUG
  104. fprintf(stdout, "Receiving response, try %d times.\n", count);
  105. #endif
  106. res = curl_ws_recv(ws_curl, recv_buffer, sizeof(recv_buffer), &rlen, &meta);
  107. if (res == CURLE_OK) {
  108. /*receive successfully*/
  109. if (strstr(recv_buffer, "jsonrpc") != NULL) {
  110. /*jsonrpc response has correct head*/
  111. break;
  112. }
  113. } else if (res != CURLE_AGAIN) {
  114. /*an error occurred, but no signal to retry again*/
  115. break;
  116. }
  117. gg_nanosleep(RECV_RETRY_INTERVAL);
  118. count++;
  119. if (count == RECV_RETRY_COUNT) timeout = true;
  120. };
  121. /*an error occurred, no data*/
  122. if (res != CURLE_OK)
  123. {
  124. websocket_error(res, NULL);
  125. if (res != CURLE_AGAIN)
  126. {
  127. curl_easy_cleanup(ws_curl);
  128. connect_init();
  129. }
  130. return NULL;
  131. }
  132. /*timeout, no data*/
  133. if (timeout) return NULL;
  134. char *data = malloc(BUFFER_SIZE);
  135. if (data == NULL)
  136. {
  137. fprintf(stderr, "\nERROR: Failed to alloc memory.\n");
  138. return NULL;
  139. }
  140. //memset(data, 0, BUFFER_SIZE);
  141. strncpy(data, recv_buffer, rlen);
  142. data[rlen] = '\0';
  143. return data;
  144. }
  145. /* Define the function how to write http response.*/
  146. static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) {
  147. struct write_result *result = (struct write_result *)stream;
  148. if (result->pos + size * nmemb >= BUFFER_SIZE - 1) {
  149. fprintf(stderr, "error: too small buffer\n");
  150. return 0;
  151. }
  152. memcpy(result->data + result->pos, ptr, size * nmemb);
  153. result->pos += size * nmemb;
  154. return size * nmemb;
  155. }
  156. char *http_request(const char *url, const char *params)
  157. {
  158. long code;
  159. char *data = NULL;
  160. struct curl_slist *headers = NULL;
  161. CURL *http_curl = NULL;
  162. CURLcode res_code;
  163. /*init the curl request instance.*/
  164. curl_global_init(CURL_GLOBAL_ALL);
  165. http_curl = curl_easy_init();
  166. if(http_curl) {
  167. data = malloc(BUFFER_SIZE);
  168. struct write_result write_result = {.data = data, .pos = 0};
  169. /* define the http request headers */
  170. headers = curl_slist_append(headers, "content-type: application/json;");
  171. /*define the type of the http request.*/
  172. curl_easy_setopt(http_curl, CURLOPT_POST, 1);
  173. /*define the URL to visit.*/
  174. curl_easy_setopt(http_curl, CURLOPT_URL, url);
  175. /*define the request header.*/
  176. curl_easy_setopt(http_curl, CURLOPT_HTTPHEADER, headers);
  177. /*set the http post data.*/
  178. curl_easy_setopt(http_curl, CURLOPT_POSTFIELDS, params);
  179. curl_easy_setopt(http_curl, CURLOPT_POSTFIELDSIZE, (long)strlen(params));
  180. /*use the defined function to save the return data.*/
  181. curl_easy_setopt(http_curl, CURLOPT_WRITEFUNCTION, write_response);
  182. /*define the write result data.*/
  183. curl_easy_setopt(http_curl, CURLOPT_WRITEDATA, &write_result);
  184. /* Perform the request, res will get the return code */
  185. res_code = curl_easy_perform(http_curl);
  186. if(res_code) {
  187. fprintf(stderr, "error: unable to request data from %s:\n", url);
  188. }
  189. /* check the http response status code. */
  190. curl_easy_getinfo(http_curl, CURLINFO_RESPONSE_CODE, &code);
  191. if(res_code != 200) {
  192. fprintf(stderr, "error: server responded with code %ld\n", code);
  193. }
  194. /* close the http request and free the http_curl.*/
  195. curl_easy_cleanup(http_curl);
  196. curl_slist_free_all(headers);
  197. curl_global_cleanup();
  198. /* zero-terminate the result */
  199. data[write_result.pos] = '\0';
  200. return data;
  201. }
  202. return 0;
  203. }
  204. #endif