1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2023 Rockchip Electronics Co., Ltd.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License");
5*4882a593Smuzhiyun * you may not use this file except in compliance with the License.
6*4882a593Smuzhiyun * You may obtain a copy of the License at
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software
11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS,
12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4882a593Smuzhiyun * See the License for the specific language governing permissions and
14*4882a593Smuzhiyun * limitations under the License.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <stdio.h>
18*4882a593Smuzhiyun #include <curl/curl.h>
19*4882a593Smuzhiyun #include <string.h>
20*4882a593Smuzhiyun #include <stdbool.h>
21*4882a593Smuzhiyun #include "log.h"
22*4882a593Smuzhiyun
my_write_func(void * ptr,size_t size,size_t nmemb,FILE * stream)23*4882a593Smuzhiyun size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun return fwrite(ptr, size, nmemb, stream);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun extern double processvalue;
29*4882a593Smuzhiyun static int down_processvalue;
30*4882a593Smuzhiyun
my_progress_func(char * progress_data,double t,double d,double ultotal,double ulnow)31*4882a593Smuzhiyun int my_progress_func(char *progress_data,
32*4882a593Smuzhiyun double t, /* dltotal */
33*4882a593Smuzhiyun double d, /* dlnow */
34*4882a593Smuzhiyun double ultotal,
35*4882a593Smuzhiyun double ulnow)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun processvalue = ulnow / ultotal * 100 / 110;
38*4882a593Smuzhiyun //LOGI("ultotal is %f, ulnow is %f, t is %f, d is %f\n", ultotal, ulnow, t, d);
39*4882a593Smuzhiyun if (down_processvalue != (int)(d / t * 100)) {
40*4882a593Smuzhiyun down_processvalue = (int)(d / t * 100);
41*4882a593Smuzhiyun LOGI("down_processvalue is %d%\n", down_processvalue);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun return 0;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
download_file(char * url,char const * output_filename)47*4882a593Smuzhiyun int download_file(char *url, char const *output_filename)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun if (strncmp(url, "http", 4) != 0) {
50*4882a593Smuzhiyun LOGI("where the file is local.\n");
51*4882a593Smuzhiyun return -2;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun CURL *curl;
54*4882a593Smuzhiyun CURLcode res;
55*4882a593Smuzhiyun FILE *outfile;
56*4882a593Smuzhiyun char const *progress_data = "* ";
57*4882a593Smuzhiyun down_processvalue = -1;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun curl = curl_easy_init();
60*4882a593Smuzhiyun if (curl) {
61*4882a593Smuzhiyun outfile = fopen(output_filename, "wb");
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun curl_easy_setopt(curl, CURLOPT_URL, url);
64*4882a593Smuzhiyun curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
65*4882a593Smuzhiyun curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
66*4882a593Smuzhiyun curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
67*4882a593Smuzhiyun curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
68*4882a593Smuzhiyun curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun res = curl_easy_perform(curl);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (res != CURLE_OK)
73*4882a593Smuzhiyun LOGE("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
74*4882a593Smuzhiyun fclose(outfile);
75*4882a593Smuzhiyun /* always cleanup */
76*4882a593Smuzhiyun curl_easy_cleanup(curl);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun if (res != CURLE_OK) {
79*4882a593Smuzhiyun LOGE("download Error.\n");
80*4882a593Smuzhiyun return -1;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85