123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- #include "otaclient.h"
- #include <iostream>
- #include <QFile>
- #include <QDir>
- #include <thread>
- #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"<<std::endl;
- #ifndef UBUNTU1604
- givlog->warn("xml not found");
- #endif
- return;
- }
- mstrVIN = strvin;
- mstrVehicleType = strvehicletype;
- std::cout<<"vin:"<<mstrVIN<<std::endl;
- std::cout<<"VehicleType:"<<mstrVehicleType<<std::endl;
- QFile xFile;
- QByteArray ba;
- xFile.setFileName("./version");
- if(xFile.open(QIODevice::ReadOnly))
- {
- ba = xFile.readAll();
- mstrVersion = ba.toStdString();
- while(mstrVersion.size()>0)
- {
- if(mstrVersion.at(mstrVersion.size() -1) == '\n')
- {
- mstrVersion.erase(mstrVersion.size()-1,1);
- }
- else
- {
- break;
- }
- }
- std::cout<<"version:"<<mstrVersion<<std::endl;
- }
- else
- {
- std::cout<<"version file is not exist."<<std::endl;
- // givlog->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."<<std::endl;
- #ifndef UBUNTU1604
- givlog->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> channel = grpc::CreateCustomChannel(
- target_str, grpc::InsecureChannelCredentials(),cargs);
- std::unique_ptr<iv::OTA::Stub> 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"<<std::endl;
- if(xReply.bupdate() == 1)
- {
- bNeedUpdate = true;
- }
- } else {
- std::cout << status.error_code() << ": " << status.error_message()
- << std::endl;
- std::cout<<"RPC failed"<<std::endl;
- #ifndef UBUNTU1604
- givlog->warn("ota RPC Failed");
- #endif
- std::this_thread::sleep_for(std::chrono::milliseconds(900));
- return;
- }
- if(bNeedUpdate == false)
- {
- std::cout<<"Not Need Update."<<std::endl;
- return;
- }
- QString strignorefilepath = "./otaignore";
- QString strupdatesigpath = "./otaupdate.sig";
- QString strupdateconfirm = "./otaconfirm";
- if(bNeedUpdate)
- {
- QFile xFileignore;
- xFileignore.setFileName(strignorefilepath);
- if(xFileignore.open(QIODevice::ReadOnly))
- {
- char strline[256];
- int nread = xFileignore.readLine(strline,256);
- xFileignore.close();
- if(nread >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: "<<strignoreversion<<" is ignore. not need update";
- return;
- }
- }
- }
- }
- }
- if(bNeedUpdate)
- {
- QFile xFileconfirm;
- xFileconfirm.setFileName(strupdateconfirm);
- if(xFileconfirm.exists())
- {
- bNeedUpdate = true;
- }
- else
- {
- bNeedUpdate = false;
- QFile xFilesig;
- xFilesig.setFileName(strupdatesigpath);
- bool bNeedWriteSig = true;
- if(xFilesig.open(QIODevice::ReadOnly))
- {
- char strline[256];
- int nread = xFilesig.readLine(strline,256);
- xFilesig.close();
- if(nread >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."<<std::endl;
- return;
- }
- std::cout<<"version:"<<xReply.strversion()<<std::endl;
- std::cout<<" file size : "<<xReply.nfilesize()<<std::endl;
- std::cout<<" md5: "<<xReply.strmd5()<<std::endl;
- QByteArray baFile;
- iv::ota::Filereq xFilereq;
- iv::ota::FileReply xFileReply;
- xFilereq.set_strversion(mstrVersion.data(),mstrVersion.size());
- xFilereq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
- xFilereq.set_strvin(mstrVIN.data(),mstrVIN.size());
- const qint64 nFilePacMaxSize = 10000000;//10M per packet.
- bool bComplete = false;
- qint64 nPos = 0;
- while(bComplete == false)
- {
- ClientContext context2;
- xFilereq.set_nsize(nFilePacMaxSize);
- xFilereq.set_npos(nPos);
- Status status2 = stub_->downfile(&context2, xFilereq, &xFileReply);
- if (status2.ok()) {
- // std::cout<<" down successfully"<<std::endl;
- if(xFileReply.bupdate() == 1)
- {
- std::cout<<"file ok. size: "<<xFileReply.xdata().size()<<" pos:"<<nPos<<std::endl;
- #ifndef UBUNTU1604
- givlog->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"<<std::endl;
- #ifndef UBUNTU1604
- givlog->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 "<<strfilemd5<<std::endl;
- if(strfilemd5 == xFileReply.strmd5())
- {
- qDebug("file md5 ok");
- #ifndef UBUNTU1604
- givlog->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<char> pstr_ptr = std::shared_ptr<char>(new char[ndatasize]);
- if(xvehinfo.SerializeToArray(pstr_ptr.get(),ndatasize))
- {
- iv::modulecomm::ModuleSendMsg(pa,pstr_ptr.get(),ndatasize);
- std::cout<<"share veh info."<<std::endl;
- }
- else
- {
- std::cout<<"warning: "<<"serialize vehinfo fail."<<std::endl;
- }
- ncount = 0;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- ncount++;
- }
- iv::modulecomm::Unregister(pa);
- }
|