6 #define UA_FIRMWARE_DOWNLOAD_PATH "/opt/usr/data/fota/"
7 #define UA_FIRMWARE_DOWNLOAD_FILE UA_FIRMWARE_DOWNLOAD_PATH"delta.tar"
10 static size_t _gather_data(void *downloaded_data,
15 size_t total_size = size * nmemb;
16 g_byte_array_append((GByteArray *)user_data, (const unsigned char *) downloaded_data, total_size);
21 void _curl_set_response(CURL *curl,
22 GByteArray *response_header,
23 GByteArray *response_body,
28 CURLcode curl_ret_code;
31 curl_ret_code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
32 if (CURLE_OK != curl_ret_code)
35 char *tmp_header = g_strndup((const gchar *)response_header->data, response_header->len);
36 char *tmp_body = g_strndup((const gchar *)response_body->data, response_body->len);
38 *res_header = tmp_header;
43 void _curl_set_common_option(CURL *curl,
45 GByteArray **response_header_ptr,
46 GByteArray **response_body_ptr)
48 UA_LOG("_curl_set_common_option()");
49 CURLcode curl_ret_code;
51 UA_LOG("set URL = [%s]", url);
52 curl_ret_code = curl_easy_setopt(curl, CURLOPT_URL, url);
53 if (CURLE_OK != curl_ret_code) {
54 UA_LOG("curl_easy_setopt: CURLOPT_URL failed!! curl_ret_code[%d]", curl_ret_code);
57 GByteArray *response_header_data = g_byte_array_new();
58 curl_ret_code = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _gather_data);
59 if (CURLE_OK != curl_ret_code) {
60 UA_LOG("curl_easy_setopt: CURLOPT_HEADERFUNCTION failed!! curl_ret_code[%d]", curl_ret_code);
63 curl_ret_code = curl_easy_setopt(curl, CURLOPT_HEADERDATA, response_header_data);
64 if (CURLE_OK != curl_ret_code) {
65 UA_LOG("curl_easy_setopt: CURLOPT_HEADERDATA failed!! curl_ret_code[%d]", curl_ret_code);
68 GByteArray *response_body_data = g_byte_array_new();
70 curl_ret_code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _gather_data);
71 if (CURLE_OK != curl_ret_code) {
72 UA_LOG("curl_easy_setopt: CURLOPT_WRITEFUNCTION failed!! curl_ret_code[%d]", curl_ret_code);
75 curl_ret_code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_body_data);
76 if (CURLE_OK != curl_ret_code) {
77 UA_LOG("curl_easy_setopt: CURLOPT_WRITEDATA failed!! curl_ret_code[%d]", curl_ret_code);
80 curl_ret_code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
81 if (CURLE_OK != curl_ret_code) {
82 UA_LOG("curl_easy_setopt: CURLOPT_NOPROGRESS failed!! curl_ret_code[%d]", curl_ret_code);
85 *response_header_ptr = response_header_data;
86 *response_body_ptr = response_body_data;
89 curl_ret_code = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
90 if (CURLE_OK != curl_ret_code) {
91 UA_LOG("curl_easy_setopt: CURLOPT_VERBOSE failed!! curl_ret_code[%d]", curl_ret_code);
94 curl_ret_code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
95 if (CURLE_OK != curl_ret_code) {
96 UA_LOG("curl_easy_setopt: CURLOPT_SSL_VERIFYPEER failed!! curl_ret_code[%d]", curl_ret_code);
99 curl_ret_code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
100 if (CURLE_OK != curl_ret_code) {
101 UA_LOG("curl_easy_setopt: CURLOPT_SSL_VERIFYHOST failed!! curl_ret_code[%d]", curl_ret_code);
104 curl_ret_code = curl_easy_setopt(curl, CURLOPT_CERTINFO, 0L);
105 if (CURLE_OK != curl_ret_code) {
106 UA_LOG("curl_easy_setopt: CURLOPT_CERTINFO failed!! curl_ret_code[%d]", curl_ret_code);
112 static void _curl_set_request_headers(CURL *curl)
114 struct curl_slist *header = NULL;
116 char *tmp_header = NULL;
118 tmp_header = g_strconcat("Content-Type: ", "application/json", NULL);
119 UA_LOG("header=[%s]", tmp_header);
120 header = curl_slist_append(header, tmp_header);
123 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
127 int ua_http_send_request(rest_req_type type, char *req_url, char **res_header, char **res_body)
129 UA_LOG("Enter http_send_request()");
132 GByteArray *response_header = NULL;
133 GByteArray *response_body= NULL;
137 // Start a libcurl easy session
138 curl = curl_easy_init();
140 UA_LOG("curl_easy_init()");
142 _curl_set_request_headers(curl);
144 if (type == UA_HTTP_GET) {
145 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
146 } else if (type == UA_HTTP_POST) {
147 curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
152 _curl_set_common_option(curl, (const char *)req_url, &response_header, &response_body);
154 UA_LOG("Start curl_easy_perform......");
155 error_code = curl_easy_perform(curl);
156 UA_LOG("curl_easy_perform(curl): %s (%d)", curl_easy_strerror(error_code), error_code);
158 if (error_code == CURLE_ABORTED_BY_CALLBACK) {
161 } else if (error_code != CURLE_OK) {
166 _curl_set_response(curl, response_header, response_body, res_header, res_body, NULL);
169 if (response_header) {
170 g_byte_array_free(response_header, TRUE);
173 g_byte_array_free(response_body, TRUE);
176 curl_easy_cleanup(curl);
181 int ua_http_download_file(const char *download_url)
183 UA_LOG("http_download_file() enter");
193 curl = curl_easy_init();
196 fp = fopen(UA_FIRMWARE_DOWNLOAD_FILE, "wb");
197 curl_easy_setopt(curl, CURLOPT_URL, download_url);
198 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
199 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
200 error_code = curl_easy_perform(curl);
201 UA_LOG("curl_easy_perform() [%d]", error_code);
202 curl_easy_cleanup(curl);
205 if (error_code != CURLE_OK) {
206 remove(UA_FIRMWARE_DOWNLOAD_FILE);