#include "otaclient.h" #include #include #include #include #include "md5file.h" #include "xmlparam.h" #ifndef UBUNTU1604 #include "ivlog.h" extern iv::Ivlog * givlog; #endif extern std::string gstrserverip;//"123.57.212.138"; extern std::string gstrserverport;//"9000"; extern std::string gstrwaitnettime; otaclient::otaclient() { mpthreadinfo = new std::thread(&otaclient::threadbroadvehinfo,this); mstrserverip = gstrserverip; mstrserverport = gstrserverport; iv::xmlparam::Xmlparam xp("./vin.xml"); std::string strvin = xp.GetParam("VIN","AAAAAAAAAAAAAAAAA"); std::string strvehicletype = xp.GetParam("VehicleType",""); if(strvehicletype == "") { mbInitOK = false; std::cout<<"xml not found"<warn("xml not found"); #endif return; } mstrVIN = strvin; mstrVehicleType = strvehicletype; std::cout<<"vin:"<0) { if(mstrVersion.at(mstrVersion.size() -1) == '\n') { mstrVersion.erase(mstrVersion.size()-1,1); } else { break; } } std::cout<<"version:"<warn("version file not found"); mbInitOK = false; } xFile.close(); } otaclient::~otaclient() { mbRun = false; requestInterruption(); mpthreadinfo->join(); } void otaclient::run() { if(mbInitOK == false) { std::cout<<"Init Fail. so not connect server."<warn("Init Fail. so not connect server."); #endif return; } int mswait = atoi(gstrwaitnettime.data()); msleep(mswait); std::string target_str =mstrserverip +":"; target_str = target_str + mstrserverport ;//std::to_string() auto cargs = grpc::ChannelArguments(); cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB cargs.SetMaxSendMessageSize(1024 * 1024 * 1024); std::shared_ptr channel = grpc::CreateCustomChannel( target_str, grpc::InsecureChannelCredentials(),cargs); std::unique_ptr stub_ = iv::OTA::NewStub(channel); iv::ota::queryReply xReply; iv::ota::queryreq xReq; xReq.set_strversion(mstrVersion.data(),mstrVersion.size()); xReq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size()); xReq.set_strvin(mstrVIN.data(),mstrVIN.size()); ClientContext context ; bool bNeedUpdate = false; Status status = stub_->query(&context, xReq, &xReply); if (status.ok()) { std::cout<<" query successfully"<warn("ota RPC Failed"); #endif std::this_thread::sleep_for(std::chrono::milliseconds(900)); return; } if(bNeedUpdate == false) { std::cout<<"Not Need Update."<0) { if(strline[nread-1] == '\n') { nread = nread -1; } if(nread >0) { strline[nread] = 0; std::string strignoreversion = strline; if(strignoreversion == xReply.strversion()) { std::cout<<" version: "<0) { if(strline[nread-1] == '\n') { nread = nread -1; } if(nread >0) { strline[nread] = 0; std::string strsigversion = strline; if(strsigversion == xReply.strversion()) { bNeedWriteSig = false; } } } } if(bNeedWriteSig) { if(xFilesig.open(QIODevice::ReadWrite)) { xFilesig.write(xReply.strversion().data()); xFilesig.close(); } } } } if(bNeedUpdate == false) { std::cout<<"no confirm file. so not update."<downfile(&context2, xFilereq, &xFileReply); if (status2.ok()) { // std::cout<<" down successfully"<verbose("file ok.size: %ld pos: %ld",xFileReply.xdata().size(),nPos); #endif nPos = nPos + xFileReply.nsize(); baFile.append(xFileReply.xdata().data(),xFileReply.xdata().size()); if(xFileReply.blastpac()) { bComplete = true; } } else { break; } } else { std::cout << status2.error_code() << ": " << status2.error_message() << std::endl; std::cout<<"RPC failed"<warn("ota download file RPC Failed"); #endif std::this_thread::sleep_for(std::chrono::milliseconds(900)); break; } } QDir xDir; if(bComplete) { if((xFileReply.nfilesize() == nPos)&&(baFile.size() == nPos)) { qDebug("down file succesfully."); QFile xFile; std::string strfilepath = "./../temp.zip"; std::string strfileupdate = "./../update.zip"; xFile.setFileName(strfilepath.data()); if(xFile.open(QIODevice::ReadWrite)) { xFile.write(baFile); xFile.close(); std::string strfilemd5 = getFileMD5(strfilepath); std::cout<<" calc md5 is "<verbose("file md5 ok"); #endif QString oldname = strfilepath.data(); QString newname = strfileupdate.data(); QFile xFileold; xFileold.setFileName(newname); bool bCanRename = true; if(xFileold.exists()) { if(xDir.remove(newname)) { } else { bCanRename = false; qDebug("can't remove old file. FAIL."); #ifndef UBUNTU1604 givlog->warn("can't remove old file. FAIL."); #endif } } if(bCanRename) { bool bRename = xDir.rename(oldname,newname); if(bRename) { qDebug("Successfully rename to update.zip."); xDir.remove(strignorefilepath); xDir.remove(strupdateconfirm); xDir.remove(strupdatesigpath); #ifndef UBUNTU1604 givlog->verbose("Successfully rename to update.zip."); #endif } else { qDebug("Fail rename to update.zip."); #ifndef UBUNTU1604 givlog->verbose("Fail rename to update.zip."); #endif } } } else { qDebug("file md5 fail. calc md5 is %s server md5 is %s ", strfilemd5.data(),xFileReply.strmd5().data()); #ifndef UBUNTU1604 givlog->warn("file md5 fail. calc md5 is %s server md5 is %s ", strfilemd5.data(),xFileReply.strmd5().data()); #endif } } else { xFile.close(); qDebug("save down file error."); #ifndef UBUNTU1604 givlog->warn("save down file error."); #endif } } else { qDebug("nFileSize is %ld pos is %lld baFile size is %d",xFileReply.nfilesize(),nPos, baFile.size()); #ifndef UBUNTU1604 givlog->verbose("nFileSize is %ld pos is %ld baFile size is %ld",xFileReply.nfilesize(),nPos, baFile.size()); #endif } } else { qDebug("DownLoad File Error."); #ifndef UBUNTU1604 givlog->warn("DownLoad File Error."); #endif } } void otaclient::threadbroadvehinfo() { void * pa = iv::modulecomm::RegisterSend("otavehinfo",1000,1); int ncount = 100; while(mbRun) { if(ncount>=100) { iv::veh::vehinfo xvehinfo; xvehinfo.set_vin(mstrVIN); xvehinfo.set_type(mstrVehicleType); xvehinfo.set_version(mstrVersion); int ndatasize = xvehinfo.ByteSize(); std::shared_ptr pstr_ptr = std::shared_ptr(new char[ndatasize]); if(xvehinfo.SerializeToArray(pstr_ptr.get(),ndatasize)) { iv::modulecomm::ModuleSendMsg(pa,pstr_ptr.get(),ndatasize); std::cout<<"share veh info."<