hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include "log.h"
#include "deviceInfo.h"
#include <curl/curl.h>
#include <fstream>
#include <string>
#include <vector>
 
 
void DeviceInfo::setHost(std::string _host){
    host = _host;
}
 
void DeviceInfo::setCurrentConfig(){
    std::ifstream f(VersionPath);
    std::string strLine, strItemName, strItemValue;
 
    while(getline(f, strLine)){
        std::string::size_type line_size, pos;
        line_size = strLine.size();
        if(line_size == 0 || strLine[0] == '#')
            continue;
        pos = strLine.find("=");
 
        if(pos == std::string::npos)
            continue;
        strItemName = strLine.substr(0, pos);
        strItemValue = strLine.substr(pos+1);
        if(strItemName.compare("RK_MODEL") == 0){
            currentModle = strItemValue;
        }else if(strItemName.compare("RK_VERSION") == 0){
            currentVersion = strItemValue;
        }else if(strItemName.compare("RK_OTA_HOST") == 0){
            //setHost(strItemValue);
        }
    }
    f.close();
}
 
void DeviceInfo::setTargetConfigData(std::string data){
    std::string::size_type pos1, pos2;
    std::vector<std::string> v;
    pos2 = data.find("\n");
    pos1 = 0;
    while(std::string::npos != pos2){
        v.push_back(data.substr(pos1, pos2 - pos1));
        data.substr(pos1, pos2 - pos1);
        pos1 = pos2 + 1;
        pos2 = data.find("\n", pos1);
    }
 
    for(std::vector<std::string>::size_type i = 0; i != v.size(); i++){
        std::string strItemName, strItemValue;
        if(v[i].size() == 0 || v[i][0] == '#'){
            continue;
        }
        pos1 = v[i].find("=");
        if(pos1 == std::string::npos){
            continue;
        }
        strItemName = v[i].substr(0, pos1);
        strItemValue = v[i].substr(pos1+1);
        if(strItemName.compare("RK_MODEL") == 0){
            targetModel = strItemValue;
        }else if(strItemName.compare("RK_VERSION") == 0){
            targetVersion = strItemValue;
        }
    }
}
bool DeviceInfo::compareVersion(){
    std::cout << targetVersion << std::endl;
    std::cout << currentVersion << std::endl;
    int retval =  currentVersion.compare(targetVersion);
    if(retval < 0)
        return true;
    else
        return false;
}
 
std::string DeviceInfo::getCurrentVersion(){
    return currentVersion;
}
 
std::string DeviceInfo::getCurrentModel(){
    return currentModle;
}
 
std::string DeviceInfo::getTargetVersion(){
    return targetVersion;
}
 
std::string DeviceInfo::getTargetModel(){
    return targetModel;
}
 
std::string DeviceInfo::getHost(){
    return host;
}
 
static size_t writeConfigToString(void *ptr, size_t size, size_t count, void *stream){
    ((std::string*)stream)->append((char*)ptr, 0, size* count);
    return size* count;
}
 
void DeviceInfoInternel::setTargetConfig(){
    CURL *curl;
    CURLcode res;
    std::string data;
 
    curl = curl_easy_init();
    if(curl) {
        std::string url = host;
        std::cout << "url is " << url << std::endl;
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeConfigToString);
        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)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
 
        /* always cleanup */
        curl_easy_cleanup(curl);
        setTargetConfigData(data);
    }
    std::cout << targetVersion << std::endl;
    std::cout << targetModel << std::endl;
}