otaclient.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "otaclient.h"
  2. #include <iostream>
  3. #include <QFile>
  4. #include <QDir>
  5. #include <thread>
  6. #include "md5file.h"
  7. #include "xmlparam.h"
  8. #ifndef UBUNTU1604
  9. #include "ivlog.h"
  10. extern iv::Ivlog * givlog;
  11. #endif
  12. extern std::string gstrserverip;//"123.57.212.138";
  13. extern std::string gstrserverport;//"9000";
  14. extern std::string gstrwaitnettime;
  15. otaclient::otaclient()
  16. {
  17. mpthreadinfo = new std::thread(&otaclient::threadbroadvehinfo,this);
  18. mstrserverip = gstrserverip;
  19. mstrserverport = gstrserverport;
  20. iv::xmlparam::Xmlparam xp("./vin.xml");
  21. std::string strvin = xp.GetParam("VIN","AAAAAAAAAAAAAAAAA");
  22. std::string strvehicletype = xp.GetParam("VehicleType","");
  23. if(strvehicletype == "")
  24. {
  25. mbInitOK = false;
  26. std::cout<<"xml not found"<<std::endl;
  27. #ifndef UBUNTU1604
  28. givlog->warn("xml not found");
  29. #endif
  30. return;
  31. }
  32. mstrVIN = strvin;
  33. mstrVehicleType = strvehicletype;
  34. std::cout<<"vin:"<<mstrVIN<<std::endl;
  35. std::cout<<"VehicleType:"<<mstrVehicleType<<std::endl;
  36. QFile xFile;
  37. QByteArray ba;
  38. xFile.setFileName("./version");
  39. if(xFile.open(QIODevice::ReadOnly))
  40. {
  41. ba = xFile.readAll();
  42. mstrVersion = ba.toStdString();
  43. while(mstrVersion.size()>0)
  44. {
  45. if(mstrVersion.at(mstrVersion.size() -1) == '\n')
  46. {
  47. mstrVersion.erase(mstrVersion.size()-1,1);
  48. }
  49. else
  50. {
  51. break;
  52. }
  53. }
  54. std::cout<<"version:"<<mstrVersion<<std::endl;
  55. }
  56. else
  57. {
  58. std::cout<<"version file is not exist."<<std::endl;
  59. // givlog->warn("version file not found");
  60. mbInitOK = false;
  61. }
  62. xFile.close();
  63. }
  64. otaclient::~otaclient()
  65. {
  66. mbRun = false;
  67. requestInterruption();
  68. mpthreadinfo->join();
  69. }
  70. void otaclient::run()
  71. {
  72. if(mbInitOK == false)
  73. {
  74. std::cout<<"Init Fail. so not connect server."<<std::endl;
  75. #ifndef UBUNTU1604
  76. givlog->warn("Init Fail. so not connect server.");
  77. #endif
  78. return;
  79. }
  80. int mswait = atoi(gstrwaitnettime.data());
  81. msleep(mswait);
  82. std::string target_str =mstrserverip +":";
  83. target_str = target_str + mstrserverport ;//std::to_string()
  84. auto cargs = grpc::ChannelArguments();
  85. cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
  86. cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
  87. std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
  88. target_str, grpc::InsecureChannelCredentials(),cargs);
  89. std::unique_ptr<iv::OTA::Stub> stub_ = iv::OTA::NewStub(channel);
  90. iv::ota::queryReply xReply;
  91. iv::ota::queryreq xReq;
  92. xReq.set_strversion(mstrVersion.data(),mstrVersion.size());
  93. xReq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
  94. xReq.set_strvin(mstrVIN.data(),mstrVIN.size());
  95. ClientContext context ;
  96. bool bNeedUpdate = false;
  97. Status status = stub_->query(&context, xReq, &xReply);
  98. if (status.ok()) {
  99. std::cout<<" query successfully"<<std::endl;
  100. if(xReply.bupdate() == 1)
  101. {
  102. bNeedUpdate = true;
  103. }
  104. } else {
  105. std::cout << status.error_code() << ": " << status.error_message()
  106. << std::endl;
  107. std::cout<<"RPC failed"<<std::endl;
  108. #ifndef UBUNTU1604
  109. givlog->warn("ota RPC Failed");
  110. #endif
  111. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  112. return;
  113. }
  114. if(bNeedUpdate == false)
  115. {
  116. std::cout<<"Not Need Update."<<std::endl;
  117. return;
  118. }
  119. QString strignorefilepath = "./otaignore";
  120. QString strupdatesigpath = "./otaupdate.sig";
  121. QString strupdateconfirm = "./otaconfirm";
  122. if(bNeedUpdate)
  123. {
  124. QFile xFileignore;
  125. xFileignore.setFileName(strignorefilepath);
  126. if(xFileignore.open(QIODevice::ReadOnly))
  127. {
  128. char strline[256];
  129. int nread = xFileignore.readLine(strline,256);
  130. xFileignore.close();
  131. if(nread >0)
  132. {
  133. if(strline[nread-1] == '\n')
  134. {
  135. nread = nread -1;
  136. }
  137. if(nread >0)
  138. {
  139. strline[nread] = 0;
  140. std::string strignoreversion = strline;
  141. if(strignoreversion == xReply.strversion())
  142. {
  143. std::cout<<" version: "<<strignoreversion<<" is ignore. not need update";
  144. return;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. if(bNeedUpdate)
  151. {
  152. QFile xFileconfirm;
  153. xFileconfirm.setFileName(strupdateconfirm);
  154. if(xFileconfirm.exists())
  155. {
  156. bNeedUpdate = true;
  157. }
  158. else
  159. {
  160. bNeedUpdate = false;
  161. QFile xFilesig;
  162. xFilesig.setFileName(strupdatesigpath);
  163. bool bNeedWriteSig = true;
  164. if(xFilesig.open(QIODevice::ReadOnly))
  165. {
  166. char strline[256];
  167. int nread = xFilesig.readLine(strline,256);
  168. xFilesig.close();
  169. if(nread >0)
  170. {
  171. if(strline[nread-1] == '\n')
  172. {
  173. nread = nread -1;
  174. }
  175. if(nread >0)
  176. {
  177. strline[nread] = 0;
  178. std::string strsigversion = strline;
  179. if(strsigversion == xReply.strversion())
  180. {
  181. bNeedWriteSig = false;
  182. }
  183. }
  184. }
  185. }
  186. if(bNeedWriteSig)
  187. {
  188. if(xFilesig.open(QIODevice::ReadWrite))
  189. {
  190. xFilesig.write(xReply.strversion().data());
  191. xFilesig.close();
  192. }
  193. }
  194. }
  195. }
  196. if(bNeedUpdate == false)
  197. {
  198. std::cout<<"no confirm file. so not update."<<std::endl;
  199. return;
  200. }
  201. std::cout<<"version:"<<xReply.strversion()<<std::endl;
  202. std::cout<<" file size : "<<xReply.nfilesize()<<std::endl;
  203. std::cout<<" md5: "<<xReply.strmd5()<<std::endl;
  204. QByteArray baFile;
  205. iv::ota::Filereq xFilereq;
  206. iv::ota::FileReply xFileReply;
  207. xFilereq.set_strversion(mstrVersion.data(),mstrVersion.size());
  208. xFilereq.set_strvehicletype(mstrVehicleType.data(),mstrVehicleType.size());
  209. xFilereq.set_strvin(mstrVIN.data(),mstrVIN.size());
  210. const qint64 nFilePacMaxSize = 10000000;//10M per packet.
  211. bool bComplete = false;
  212. qint64 nPos = 0;
  213. while(bComplete == false)
  214. {
  215. ClientContext context2;
  216. xFilereq.set_nsize(nFilePacMaxSize);
  217. xFilereq.set_npos(nPos);
  218. Status status2 = stub_->downfile(&context2, xFilereq, &xFileReply);
  219. if (status2.ok()) {
  220. // std::cout<<" down successfully"<<std::endl;
  221. if(xFileReply.bupdate() == 1)
  222. {
  223. std::cout<<"file ok. size: "<<xFileReply.xdata().size()<<" pos:"<<nPos<<std::endl;
  224. #ifndef UBUNTU1604
  225. givlog->verbose("file ok.size: %ld pos: %ld",xFileReply.xdata().size(),nPos);
  226. #endif
  227. nPos = nPos + xFileReply.nsize();
  228. baFile.append(xFileReply.xdata().data(),xFileReply.xdata().size());
  229. if(xFileReply.blastpac())
  230. {
  231. bComplete = true;
  232. }
  233. }
  234. else
  235. {
  236. break;
  237. }
  238. } else {
  239. std::cout << status2.error_code() << ": " << status2.error_message()
  240. << std::endl;
  241. std::cout<<"RPC failed"<<std::endl;
  242. #ifndef UBUNTU1604
  243. givlog->warn("ota download file RPC Failed");
  244. #endif
  245. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  246. break;
  247. }
  248. }
  249. QDir xDir;
  250. if(bComplete)
  251. {
  252. if((xFileReply.nfilesize() == nPos)&&(baFile.size() == nPos))
  253. {
  254. qDebug("down file succesfully.");
  255. QFile xFile;
  256. std::string strfilepath = "./../temp.zip";
  257. std::string strfileupdate = "./../update.zip";
  258. xFile.setFileName(strfilepath.data());
  259. if(xFile.open(QIODevice::ReadWrite))
  260. {
  261. xFile.write(baFile);
  262. xFile.close();
  263. std::string strfilemd5 = getFileMD5(strfilepath);
  264. std::cout<<" calc md5 is "<<strfilemd5<<std::endl;
  265. if(strfilemd5 == xFileReply.strmd5())
  266. {
  267. qDebug("file md5 ok");
  268. #ifndef UBUNTU1604
  269. givlog->verbose("file md5 ok");
  270. #endif
  271. QString oldname = strfilepath.data();
  272. QString newname = strfileupdate.data();
  273. QFile xFileold;
  274. xFileold.setFileName(newname);
  275. bool bCanRename = true;
  276. if(xFileold.exists())
  277. {
  278. if(xDir.remove(newname))
  279. {
  280. }
  281. else
  282. {
  283. bCanRename = false;
  284. qDebug("can't remove old file. FAIL.");
  285. #ifndef UBUNTU1604
  286. givlog->warn("can't remove old file. FAIL.");
  287. #endif
  288. }
  289. }
  290. if(bCanRename)
  291. {
  292. bool bRename = xDir.rename(oldname,newname);
  293. if(bRename)
  294. {
  295. qDebug("Successfully rename to update.zip.");
  296. xDir.remove(strignorefilepath);
  297. xDir.remove(strupdateconfirm);
  298. xDir.remove(strupdatesigpath);
  299. #ifndef UBUNTU1604
  300. givlog->verbose("Successfully rename to update.zip.");
  301. #endif
  302. }
  303. else
  304. {
  305. qDebug("Fail rename to update.zip.");
  306. #ifndef UBUNTU1604
  307. givlog->verbose("Fail rename to update.zip.");
  308. #endif
  309. }
  310. }
  311. }
  312. else
  313. {
  314. qDebug("file md5 fail. calc md5 is %s server md5 is %s ",
  315. strfilemd5.data(),xFileReply.strmd5().data());
  316. #ifndef UBUNTU1604
  317. givlog->warn("file md5 fail. calc md5 is %s server md5 is %s ",
  318. strfilemd5.data(),xFileReply.strmd5().data());
  319. #endif
  320. }
  321. }
  322. else
  323. {
  324. xFile.close();
  325. qDebug("save down file error.");
  326. #ifndef UBUNTU1604
  327. givlog->warn("save down file error.");
  328. #endif
  329. }
  330. }
  331. else
  332. {
  333. qDebug("nFileSize is %ld pos is %lld baFile size is %d",xFileReply.nfilesize(),nPos,
  334. baFile.size());
  335. #ifndef UBUNTU1604
  336. givlog->verbose("nFileSize is %ld pos is %ld baFile size is %ld",xFileReply.nfilesize(),nPos,
  337. baFile.size());
  338. #endif
  339. }
  340. }
  341. else
  342. {
  343. qDebug("DownLoad File Error.");
  344. #ifndef UBUNTU1604
  345. givlog->warn("DownLoad File Error.");
  346. #endif
  347. }
  348. }
  349. void otaclient::threadbroadvehinfo()
  350. {
  351. void * pa = iv::modulecomm::RegisterSend("otavehinfo",1000,1);
  352. int ncount = 100;
  353. while(mbRun)
  354. {
  355. if(ncount>=100)
  356. {
  357. iv::veh::vehinfo xvehinfo;
  358. xvehinfo.set_vin(mstrVIN);
  359. xvehinfo.set_type(mstrVehicleType);
  360. xvehinfo.set_version(mstrVersion);
  361. int ndatasize = xvehinfo.ByteSize();
  362. std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[ndatasize]);
  363. if(xvehinfo.SerializeToArray(pstr_ptr.get(),ndatasize))
  364. {
  365. iv::modulecomm::ModuleSendMsg(pa,pstr_ptr.get(),ndatasize);
  366. std::cout<<"share veh info."<<std::endl;
  367. }
  368. else
  369. {
  370. std::cout<<"warning: "<<"serialize vehinfo fail."<<std::endl;
  371. }
  372. ncount = 0;
  373. }
  374. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  375. ncount++;
  376. }
  377. iv::modulecomm::Unregister(pa);
  378. }