hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <iostream>
#include "data.h"
#include "partition.h"
#include "log.h"
#include <string.h>
#include <unistd.h>
#include "tomcat.h"
//#include "bootcontrol.h"
#include "rkboot_control.h"
 
size_t writeToString(void *ptr, size_t size, size_t count, void *stream)
{
    //((std::string*)stream)->append((char*)ptr, 0, size* count);
    if(size * count <= 512)
        std::cout<<"append size * count "<<size * count<<std::endl;
    struct ImageData data;
    struct ImageData *pData = &data;
    static unsigned int currentOffset;
    pData->data = (char*)malloc(size * count);
    pData->offset = currentOffset;
    pData->size = size * count;
    //LOGI("pData->offset = %u\n", currentOffset);
    //LOGI("pData->size = %u\n", pData->size);
    memcpy(pData->data, ptr, count * size);
    int res = writeDataToPartition(pData);
    currentOffset += size * count;
    free(pData->data);
    //pthread_testcancel();
    if(res != 0){
        return -1;
    }
    return size * count;
}
 
RK_showprogress_callback cb = NULL;
 
double progressValue = 0;
 
int showProgressValue(){
    return progressValue;
}
 
int getProgressValue(void* clientp, double dltotal, double dlnow, double ulttotal, double ulnow){
    progressValue = dlnow / dltotal * 100;
    return 0;
}
 
int getDataFromUrl(char *url)
{
    CURL *curl;
    CURLcode res;
 
    curl = curl_easy_init();
    if(curl) {
        //std::string data;
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToString);
        //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &return_val);
 
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
        //curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, getProgressValue);
        curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, getProgressValue);
        //curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, "flag");
 
 
        //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
        // data now holds response
 
        // write curl response to string variable..
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
            LOGE("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        //std::cout << data << std::endl;
        //std::cout << data.length() << std::endl;
        /*
        res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLength);
        if(!res){
            LOGE("downloadFileLength = %0.f\n", downloadFileLength);
        }else{
            LOGE("downloadFileLength error \n");
        }*/
 
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    if(res != CURLE_OK){
        LOGE("update Error.\n");
        return -1;
    }else{
        LOGE("update ok.\n");
        setSlotActivity();
    }
    return 0;
}