diff options
Diffstat (limited to 'src/ua_http.cpp')
-rw-r--r-- | src/ua_http.cpp | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/src/ua_http.cpp b/src/ua_http.cpp new file mode 100644 index 0000000..ee67f15 --- /dev/null +++ b/src/ua_http.cpp @@ -0,0 +1,214 @@ +#include <glib.h> +#include <curl/curl.h> + +#include <ua_client.h> + +#define UA_FIRMWARE_DOWNLOAD_PATH "/opt/usr/data/fota/" +#define UA_FIRMWARE_DOWNLOAD_FILE UA_FIRMWARE_DOWNLOAD_PATH"delta.tar" + + +static size_t _gather_data(void *downloaded_data, + size_t size, + size_t nmemb, + void *user_data) +{ + size_t total_size = size * nmemb; + g_byte_array_append((GByteArray *)user_data, (const unsigned char *) downloaded_data, total_size); + return total_size; +} + + +void _curl_set_response(CURL *curl, + GByteArray *response_header, + GByteArray *response_body, + char **res_header, + char **res_body, + void *user_data) +{ + CURLcode curl_ret_code; + + long response = 0; + curl_ret_code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); + if (CURLE_OK != curl_ret_code) + return; + + char *tmp_header = g_strndup((const gchar *)response_header->data, response_header->len); + char *tmp_body = g_strndup((const gchar *)response_body->data, response_body->len); + + *res_header = tmp_header; + *res_body = tmp_body; +} + + +void _curl_set_common_option(CURL *curl, + const char *url, + GByteArray **response_header_ptr, + GByteArray **response_body_ptr) +{ + UA_LOG("_curl_set_common_option()"); + CURLcode curl_ret_code; + + UA_LOG("set URL = [%s]", url); + curl_ret_code = curl_easy_setopt(curl, CURLOPT_URL, url); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_URL failed!! curl_ret_code[%d]", curl_ret_code); + } + + GByteArray *response_header_data = g_byte_array_new(); + curl_ret_code = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _gather_data); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_HEADERFUNCTION failed!! curl_ret_code[%d]", curl_ret_code); + } + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_HEADERDATA, response_header_data); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_HEADERDATA failed!! curl_ret_code[%d]", curl_ret_code); + } + + GByteArray *response_body_data = g_byte_array_new(); + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _gather_data); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_WRITEFUNCTION failed!! curl_ret_code[%d]", curl_ret_code); + } + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_body_data); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_WRITEDATA failed!! curl_ret_code[%d]", curl_ret_code); + } + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_NOPROGRESS failed!! curl_ret_code[%d]", curl_ret_code); + } + + *response_header_ptr = response_header_data; + *response_body_ptr = response_body_data; + +#if 0 + curl_ret_code = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_VERBOSE failed!! curl_ret_code[%d]", curl_ret_code); + } + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_SSL_VERIFYPEER failed!! curl_ret_code[%d]", curl_ret_code); + } + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_SSL_VERIFYHOST failed!! curl_ret_code[%d]", curl_ret_code); + } + + curl_ret_code = curl_easy_setopt(curl, CURLOPT_CERTINFO, 0L); + if (CURLE_OK != curl_ret_code) { + UA_LOG("curl_easy_setopt: CURLOPT_CERTINFO failed!! curl_ret_code[%d]", curl_ret_code); + } +#endif +} + + +static void _curl_set_request_headers(CURL *curl) +{ + struct curl_slist *header = NULL; + + char *tmp_header = NULL; + + tmp_header = g_strconcat("Content-Type: ", "application/json", NULL); + UA_LOG("header=[%s]", tmp_header); + header = curl_slist_append(header, tmp_header); + g_free(tmp_header); + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); +} + + +int ua_http_send_request(rest_req_type type, char *req_url, char **res_header, char **res_body) +{ + UA_LOG("Enter http_send_request()"); + + CURL *curl; + GByteArray *response_header = NULL; + GByteArray *response_body= NULL; + CURLcode error_code; + int ret = 0; + + // Start a libcurl easy session + curl = curl_easy_init(); + + UA_LOG("curl_easy_init()"); + + _curl_set_request_headers(curl); + + if (type == UA_HTTP_GET) { + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1); + } else if (type == UA_HTTP_POST) { + curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1); + } else { + return -1; + } + + _curl_set_common_option(curl, (const char *)req_url, &response_header, &response_body); + + UA_LOG("Start curl_easy_perform......"); + error_code = curl_easy_perform(curl); + UA_LOG("curl_easy_perform(curl): %s (%d)", curl_easy_strerror(error_code), error_code); + + if (error_code == CURLE_ABORTED_BY_CALLBACK) { + ret = -1; + goto _END_OF_FUNC_; + } else if (error_code != CURLE_OK) { + ret = -1; + goto _END_OF_FUNC_; + } + + _curl_set_response(curl, response_header, response_body, res_header, res_body, NULL); + +_END_OF_FUNC_: + if (response_header) { + g_byte_array_free(response_header, TRUE); + } + if (response_body) { + g_byte_array_free(response_body, TRUE); + } + + curl_easy_cleanup(curl); + return ret; +} + + +int ua_http_download_file(const char *download_url) +{ + UA_LOG("http_download_file() enter"); + + if (!download_url) + return -1; + + int ret = 0; + CURL *curl; + FILE *fp; + CURLcode error_code; + + curl = curl_easy_init(); + if (curl) + { + fp = fopen(UA_FIRMWARE_DOWNLOAD_FILE, "wb"); + curl_easy_setopt(curl, CURLOPT_URL, download_url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + error_code = curl_easy_perform(curl); + UA_LOG("curl_easy_perform() [%d]", error_code); + curl_easy_cleanup(curl); + fclose(fp); + + if (error_code != CURLE_OK) { + remove(UA_FIRMWARE_DOWNLOAD_FILE); + ret = -1; + } + } else { + ret = -1; + } + + return ret; +} |