#include #include #include #include #include #include #include #include #include "checkversion.h" #include #include #include #include #include char _save_APP_path[100] = {0}; #define LOCAL_VERSION_PATH "/demo/bin/version" #define DOWNLOAD_VERSION_PATH "/tmp/version" #define DeviceID_FILE "/etc/DeviceID" std::string DeviceID ; #define URL_FILE "/demo/bin/URL/CheckAPPVersion" std::string BASE_URL_string; std::string URL_string; std::string local_app_version; // 函数用于读取文件并将其内容保存在全局字符串变量中 void readFileAndStoreInGlobal(const std::string& filename, std::string& destination) { std::ifstream file(filename); if (file.is_open()) { std::getline(file, destination); // 读取一行并存储在全局字符串中 file.close(); } else { std::cerr << "无法打开文件: " << filename << std::endl; } } //回调函数 size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) { size_t totalSize = size * nmemb; unsigned char* data = static_cast(contents); response->insert(response->end(), data, data + totalSize); return totalSize; } // 提取 "data" 字段的值 std::string extractDataValue(const std::string& jsonString) { std::size_t srcPos = jsonString.find("\"data\":\""); if (srcPos == std::string::npos){ std::cout << "未找到 data 字段 " << std::endl; return ""; // 未找到 "src" 字段 } std::size_t valueStartPos = srcPos + 8; std::size_t valueEndPos = jsonString.find("\"", valueStartPos); if (valueEndPos == std::string::npos){ std::cout << "未找到值的结束位置 " << std::endl; return ""; // 未找到值的结束位置 } return jsonString.substr(valueStartPos, valueEndPos - valueStartPos); } bool Delete_string(char *substringToDelete_download,char *substringToDelete_local,char *download_SE_APP_version,char *local_SE_APP_version){ if ((strlen(substringToDelete_download) != 0)&&(strlen(substringToDelete_local) != 0)) { char* found1 = strstr(download_SE_APP_version, substringToDelete_download); if (found1 != NULL) { memmove(found1, found1 + strlen(substringToDelete_download), strlen(found1 + strlen(substringToDelete_download)) + 1); } char* found2 = strstr(local_SE_APP_version, substringToDelete_local); if (found2 != NULL) { memmove(found2, found2 + strlen(substringToDelete_local), strlen(found2 + strlen(substringToDelete_local)) + 1); } return true; }else{ printf("substringToDelete NULL\n"); return false; } } bool getVersionFromfile(const char * filepath,char *version, int maxLength,char *parameter) { if (version == NULL || filepath == NULL) { printf("getLocalVersion is error, version == null.\n"); return false; } FILE *fp = fopen(filepath, "r"); if (fp == NULL) { printf("open %s failed, error is %s.\n", filepath, strerror(errno)); return false; } int iffind=0; char *line = NULL; size_t len = 0; size_t read; while ((read = getline(&line, &len, fp)) != -1) { if (read == 0 || line[0] == '#') { continue; } char *pline = strstr(line, parameter); if (pline != NULL && (pline = strstr(pline, "=")) != NULL) { pline++; //过滤掉等于号 //过滤掉空格 while (*pline == ' ') { pline++; } int pline_len = strlen(pline) - 1; int version_len = (pline_len > maxLength ? maxLength:pline_len); memcpy(version, pline, version_len); printf("%s: %s = %s \n",filepath,parameter, version); iffind=1; break; } } if (iffind == 0 ){ printf("Can not find %s \n",parameter); } free(line); fclose(fp); return true; } bool check_version(char *local_SE_APP_version,char *download_SE_APP_version,char *download_SE_APP_url, int sizeof_local_SE_APP_version,int sizeof_download_SE_APP_version,int sizeof_download_SE_APP_url){ //从文件中读取url readFileAndStoreInGlobal(URL_FILE,BASE_URL_string); readFileAndStoreInGlobal(DeviceID_FILE,DeviceID); curlToServer_GET(); std::cout << "local_app_version: " << local_app_version.c_str() << std::endl; strcpy(local_SE_APP_version, local_app_version.c_str()); printf("local_SE_APP_version after strcpy %s\n", local_SE_APP_version); // getVersionFromfile(LOCAL_VERSION_PATH,local_SE_APP_version,sizeof_local_SE_APP_version,"SE-APP_VERSION"); getVersionFromfile(DOWNLOAD_VERSION_PATH,download_SE_APP_version,sizeof_download_SE_APP_version,"SE-APP_VERSION"); char substringToDelete_download[10] = {0}; char substringToDelete_local[10] = {0}; if((strstr(download_SE_APP_version,"APP1") != NULL) && (strstr(local_SE_APP_version,"APP2") != NULL)){ printf("download_SE_APP_version has APP1\n"); printf("local_SE_APP_version has APP2\n"); getVersionFromfile(DOWNLOAD_VERSION_PATH,download_SE_APP_url,sizeof_download_SE_APP_url,"SE-APP1_url"); // strcpy(_save_APP_path, "/demo/bin/"); // strcat(_save_APP_path, download_SE_APP_version); strcpy(_save_APP_path, download_SE_APP_version); strcpy(substringToDelete_download, "APP1"); strcpy(substringToDelete_local, "APP2"); // printf("substringToDelete %s\n", substringToDelete); }else if((strstr(download_SE_APP_version,"APP2") != NULL) && (strstr(local_SE_APP_version,"APP1") != NULL)){ printf("download_SE_APP_version has APP2\n"); strcpy(_save_APP_path, "/demo/bin/SE-APP2.bin"); getVersionFromfile(DOWNLOAD_VERSION_PATH,download_SE_APP_url,sizeof_download_SE_APP_url,"SE-APP2_url"); // strcpy(_save_APP_path, "/demo/bin/"); // strcat(_save_APP_path, download_SE_APP_version); strcpy(_save_APP_path, download_SE_APP_version); strcpy(substringToDelete_download, "APP2"); strcpy(substringToDelete_local, "APP1"); // printf("substringToDelete %s\n", substringToDelete); }else{ printf("APP区域不匹配\n"); return false; } if(!Delete_string(substringToDelete_download,substringToDelete_local,download_SE_APP_version,local_SE_APP_version)){ return false; } printf("local_SE_APP_version after Delete_string %s\n", local_SE_APP_version); printf("check version new:%s old:%s\n", download_SE_APP_version, local_SE_APP_version); if (strcmp(download_SE_APP_version, local_SE_APP_version) != 0) { printf("check version OK!\n"); return true; }else{ printf("check version:no need to upload\n"); return false; } } void curlToServer_GET() { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); std::string checkappversionurl =BASE_URL_string + DeviceID; curl_easy_setopt(curl, CURLOPT_URL, checkappversionurl.c_str()); std::cout << "check app version url: " << checkappversionurl.c_str() << std::endl; curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Accept: */*"); headers = curl_slist_append(headers, "Host: 36.108.213.190:10004"); headers = curl_slist_append(headers, "Connection: keep-alive"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 设置响应回调函数和缓冲区 // 创建一个用于存储返回数据的 unsigned char 数组 std::string response; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); res = curl_easy_perform(curl); fprintf(stderr, "res: %s\n", curl_easy_strerror(res)); /* Check for errors */ if(res != CURLE_OK){ fprintf(stderr, "curl_easy_perform() passmessage failed: %s\n",curl_easy_strerror(res)); } else { std::cout << "Response: " << response << std::endl; local_app_version = extractDataValue(response); // std::cout << "local_app_version: " << local_app_version.c_str() << std::endl; } } curl_easy_cleanup(curl); }