dogthread.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #include "dogthread.h"
  2. #include "../ytCore/yt_unit_shm.h"
  3. #include <QCryptographicHash>
  4. extern void MD5Init (MD5_CTX *context);
  5. extern void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen);
  6. extern void MD5Final (unsigned char digest[16], MD5_CTX *context);
  7. extern YT_UNIT_SHM *ytShm;
  8. #define S11 7
  9. #define S12 12
  10. #define S13 17
  11. #define S14 22
  12. #define S21 5
  13. #define S22 9
  14. #define S23 14
  15. #define S24 20
  16. #define S31 4
  17. #define S32 11
  18. #define S33 16
  19. #define S34 23
  20. #define S41 6
  21. #define S42 10
  22. #define S43 15
  23. #define S44 21
  24. static void MD5_memcpy (POINTER output, POINTER input, unsigned int len);
  25. static void MD5Transform (UINT4 state[4], unsigned char block[64]);
  26. static void Encode (unsigned char *output, UINT4 *input, unsigned int len);
  27. static void MD5_memset (POINTER output, int value, unsigned int len);
  28. static void Decode (UINT4 *output, unsigned char *input, unsigned int len);
  29. static unsigned char PADDING[64] = {
  30. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  33. };
  34. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  35. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  36. #define H(x, y, z) ((x) ^ (y) ^ (z))
  37. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  38. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  39. #define FF(a, b, c, d, x, s, ac) { (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  40. #define GG(a, b, c, d, x, s, ac) { (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  41. #define HH(a, b, c, d, x, s, ac) { (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  42. #define II(a, b, c, d, x, s, ac) { (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  43. static void MD5Transform (UINT4 state[4], unsigned char block[64])
  44. {
  45. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  46. Decode (x, block, 64);
  47. /* Round 1 */
  48. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  49. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  50. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  51. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  52. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  53. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  54. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  55. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  56. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  57. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  58. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  59. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  60. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  61. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  62. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  63. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  64. /* Round 2 */
  65. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  66. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  67. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  68. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  69. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  70. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  71. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  72. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  73. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  74. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  75. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  76. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  77. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  78. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  79. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  80. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  81. /* Round 3 */
  82. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  83. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  84. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  85. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  86. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  87. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  88. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  89. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  90. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  91. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  92. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  93. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  94. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  95. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  96. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  97. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  98. /* Round 4 */
  99. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  100. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  101. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  102. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  103. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  104. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  105. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  106. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  107. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  108. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  109. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  110. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  111. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  112. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  113. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  114. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  115. state[0] += a;
  116. state[1] += b;
  117. state[2] += c;
  118. state[3] += d;
  119. MD5_memset ((POINTER)x, 0, sizeof (x));
  120. }
  121. static void Decode (UINT4 *output, unsigned char *input, unsigned int len)
  122. {
  123. unsigned int i, j;
  124. for (i = 0, j = 0; j < len; i++, j += 4)
  125. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  126. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  127. }
  128. static void MD5_memcpy (POINTER output, POINTER input, unsigned int len)
  129. {
  130. unsigned int i;
  131. for (i = 0; i < len; i++)
  132. output[i] = input[i];
  133. }
  134. static void MD5_memset (POINTER output, int value, unsigned int len)
  135. {
  136. unsigned int i;
  137. for (i = 0; i < len; i++)
  138. ((char *)output)[i] = (char)value;
  139. }
  140. static void Encode (unsigned char *output, UINT4 *input, unsigned int len)
  141. {
  142. unsigned int i, j;
  143. for (i = 0, j = 0; j < len; i++, j += 4) {
  144. output[j] = (unsigned char)(input[i] & 0xff);
  145. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  146. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  147. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  148. }
  149. }
  150. void MD5Init (MD5_CTX *context) /* context */
  151. {
  152. context->count[0] = context->count[1] = 0;
  153. /* Load magic initialization constants.
  154. */
  155. context->state[0] = 0x67452301;
  156. context->state[1] = 0xefcdab89;
  157. context->state[2] = 0x98badcfe;
  158. context->state[3] = 0x10325476;
  159. }
  160. void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen)
  161. {
  162. unsigned int i, index, partLen;
  163. /* Compute number of bytes mod 64 */
  164. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  165. /* Update number of bits */
  166. if ((context->count[0] += ((UINT4)inputLen << 3))
  167. < ((UINT4)inputLen << 3))
  168. context->count[1]++;
  169. context->count[1] += ((UINT4)inputLen >> 29);
  170. partLen = 64 - index;
  171. /* Transform as many times as possible.
  172. */
  173. if (inputLen >= partLen) {
  174. MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
  175. MD5Transform (context->state, context->buffer);
  176. for (i = partLen; i + 63 < inputLen; i += 64)
  177. MD5Transform (context->state, &input[i]);
  178. index = 0;
  179. }
  180. else
  181. i = 0;
  182. /* Buffer remaining input */
  183. MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i);
  184. }
  185. void MD5Final (unsigned char digest[16], MD5_CTX *context)
  186. {
  187. unsigned char bits[8];
  188. unsigned int index, padLen;
  189. /* Save number of bits */
  190. Encode (bits, context->count, 8);
  191. /* Pad out to 56 mod 64.
  192. */
  193. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  194. padLen = (index < 56) ? (56 - index) : (120 - index);
  195. MD5Update (context, PADDING, padLen);
  196. /* Append length (before padding) */
  197. MD5Update (context, bits, 8);
  198. /* Store state in digest */
  199. Encode (digest, context->state, 16);
  200. /* Zeroize sensitive information.
  201. */
  202. MD5_memset ((POINTER)context, 0, sizeof (*context));
  203. }
  204. dogThread::dogThread(QObject *parent, QStringList params) :
  205. QThread(parent)
  206. {
  207. runFlag = false;
  208. QFile db_dir(QString(DB_PATH));
  209. QDateTime t = QDateTime::currentDateTime();
  210. start_t = t.toTime_t();
  211. start_h = t.time().hour();
  212. if(!db_dir.exists())
  213. system(QString("mkdir -p %1").arg(DB_PATH).toUtf8().data());
  214. Params.clear();
  215. if(params.length()>0)
  216. Params.append(params);
  217. }
  218. void dogThread::start_app()
  219. {
  220. QString cmd = QString("./%1 ").arg(APP_NAME);
  221. if(Params.length()>0)
  222. {
  223. for(int i=0;i<Params.length();i++)
  224. {
  225. if(Params.at(i).compare("&")!=0)
  226. cmd.append(QString("%1 ").arg(Params.at(i)));
  227. }
  228. }
  229. cmd.append(" &");
  230. system(cmd.toUtf8().data());
  231. }
  232. int dogThread::mk_verqry(unsigned char *cmd)
  233. {
  234. int i,row=0;
  235. size_t idLen = strlen(APP_ID);
  236. unsigned char crc=0x00;
  237. unsigned short dLen = idLen+4;
  238. unsigned short Len = dLen+14;
  239. cmd[row++] = 0x7e;
  240. cmd[row++] = 0x01;
  241. cmd[row++] = (dLen>>8)&0xff;
  242. cmd[row++] = dLen&0xff;
  243. cmd[row++] = 0x05;
  244. cmd[row++] = 0x85;
  245. cmd[row++] = 0x83;
  246. cmd[row++] = 0x75;
  247. cmd[row++] = 0x89;
  248. cmd[row++] = 0x73;
  249. cmd[row++] = 0x79;
  250. cmd[row++] = 0x84;
  251. cmd[row++] = 0x01;
  252. cmd[row++] = (idLen>>8)&0xff;
  253. cmd[row++] = idLen&0xff;
  254. for(i=0;i<idLen;i++)
  255. cmd[row++] = APP_ID[i]&0xff;
  256. cmd[row++] = 0x00;
  257. cmd[row] = 0x00;
  258. cmd[row+1] = 0xe7;
  259. for(i=1;i<row;i++)
  260. crc += cmd[i];
  261. cmd[row] = crc&0xff;
  262. if(Len!=(row+2))
  263. return 0;
  264. return Len;
  265. }
  266. int dogThread::mk_proqry(unsigned char *cmd, int idx)
  267. {
  268. int i, row=0;
  269. size_t idLen = strlen(APP_ID);
  270. unsigned char crc=0x00;
  271. unsigned short dLen = idLen+8;
  272. unsigned short Len = dLen+14;
  273. cmd[row++] = 0x7e;
  274. cmd[row++] = 0x01;
  275. cmd[row++] = (dLen>>8)&0xff;
  276. cmd[row++] = dLen&0xff;
  277. cmd[row++] = 0x05;
  278. cmd[row++] = 0x85;
  279. cmd[row++] = 0x83;
  280. cmd[row++] = 0x75;
  281. cmd[row++] = 0x89;
  282. cmd[row++] = 0x73;
  283. cmd[row++] = 0x79;
  284. cmd[row++] = 0x84;
  285. cmd[row++] = 0x02;
  286. cmd[row++] = (idLen>>8)&0xff;
  287. cmd[row++] = idLen&0xff;
  288. for(i=0;i<idLen;i++)
  289. cmd[row++] = APP_ID[i]&0xff;
  290. cmd[row++] = 0x00;
  291. cmd[row++] = t_Major;
  292. cmd[row++] = t_Minor;
  293. cmd[row++] = (idx>>8)&0xff;
  294. cmd[row++] = idx&0xff;
  295. cmd[row] = 0x00;
  296. cmd[row+1] = 0xe7;
  297. for(i=1;i<row;i++)
  298. crc += cmd[i];
  299. cmd[row] = crc&0xff;
  300. if(Len!=(row+2))
  301. return 0;
  302. return Len;
  303. }
  304. bool dogThread::check_frame(QByteArray rtn, int idx, FILE *fd)
  305. {
  306. int i,bLen,t_len,crc_len,pro_len;
  307. unsigned char crc=0x00;
  308. unsigned char data[1200];
  309. unsigned short t_datachk,datachk=0x0000;
  310. if(rtn.length()<=14)
  311. return false;
  312. bLen = (int)(((rtn.at(2)&0x00ff)<<8)|(rtn.at(3)&0x00ff))+14;
  313. if(rtn.length()!=bLen)
  314. return false;
  315. if(((rtn.at(0)&0xff)!=0x7e)||((rtn.at(rtn.length()-1)&0xff)!=0xe7))
  316. return false;
  317. for(i=1;i<(rtn.length()-2);i++)
  318. crc += (rtn.at(i)&0xff);
  319. if((rtn.at(rtn.length()-2)&0xff)!=crc)
  320. return false;
  321. switch(rtn.at(12)&0xff){
  322. case 0x01:
  323. {
  324. bzero(t_DeviceTypeId,sizeof(t_DeviceTypeId));
  325. bzero(t_Checksum,sizeof(t_Checksum));
  326. t_Major = 0x00;
  327. t_Minor = 0x00;
  328. t_len = (int)(((rtn.at(13)&0x00ff)<<8)|(rtn.at(14)&0x00ff));
  329. if(t_len>0){
  330. for(i=0;i<t_len;i++){
  331. t_DeviceTypeId[i] = rtn.at(15+i)&0xff;
  332. }
  333. t_DeviceTypeId[t_len]=0x00;
  334. }
  335. t_Major = rtn.at(16+t_len)&0xff;
  336. t_Minor = rtn.at(17+t_len)&0xff;
  337. t_Length = ((rtn.at(18+t_len)&0x000000ff)<<24)|((rtn.at(19+t_len)&0x000000ff)<<16)|((rtn.at(20+t_len)&0x000000ff)<<8)|(rtn.at(21+t_len)&0x000000ff);
  338. crc_len = (int)(((rtn.at(22+t_len)&0x00ff)<<8)|(rtn.at(23+t_len)&0x00ff));
  339. if(crc_len>0){
  340. for(i=0;i<crc_len;i++){
  341. t_Checksum[i] = rtn.at(24+t_len+i);
  342. }
  343. t_Checksum[crc_len]=0x00;
  344. }
  345. t_UpTime = ((rtn.at(25+t_len+crc_len)&0x000000ff)<<24)|((rtn.at(26+t_len+crc_len)&0x000000ff)<<16)|((rtn.at(27+t_len+crc_len)&0x000000ff)<<8)|(rtn.at(28+t_len+crc_len)&0x000000ff);
  346. printf("t_DeviceTypeId [%s], APP_ID [%s]\n",t_DeviceTypeId,APP_ID);
  347. printf("t_Major*256+t_Minor [%d], Major*256+Minor [%d]\n",t_Major*256+t_Minor,Major*256+Minor);
  348. printf("t_UpTime [%ld], UpTime [%ld]\n",t_UpTime,UpTime);
  349. printf("t_UpTime yyyymmddhh: %s, current yyyymmddhh %s\n",(QDateTime::fromTime_t(t_UpTime)).toString("yyyy-MM-dd hh").toUtf8().data(),QDateTime::currentDateTime().toString("yyyy-MM-dd hh").toUtf8().data());
  350. printf("t_Checksum [%s], Checksum [%s]\n",t_Checksum,Checksum);
  351. if(strcmp((QDateTime::fromTime_t(t_UpTime)).toString("yyyy-MM-dd hh").toUtf8().data(),QDateTime::currentDateTime().toString("yyyy-MM-dd hh").toUtf8().data())==0){
  352. printf("equal test\n");
  353. if(strcmp(t_DeviceTypeId,APP_ID)!=0)
  354. return false;
  355. if((t_Major*256+t_Minor)<(Major*256+Minor))
  356. return false;
  357. if(t_UpTime==UpTime)
  358. return false;
  359. if(strcmp(t_Checksum,Checksum)==0)
  360. return false;
  361. }else{
  362. return false;
  363. }
  364. }
  365. break;
  366. case 0x02:
  367. {
  368. bzero(t_DeviceTypeId,sizeof(t_DeviceTypeId));
  369. bzero(data,sizeof(data));
  370. t_len = (int)(((rtn.at(13)&0x00ff)<<8)|(rtn.at(14)&0x00ff));
  371. if(t_len>0){
  372. for(i=0;i<t_len;i++){
  373. t_DeviceTypeId[i] = rtn.at(15+i)&0xff;
  374. }
  375. t_DeviceTypeId[t_len]=0x00;
  376. }
  377. if((t_Major!=(rtn.at(16+t_len)&0xff))||(t_Minor!=(rtn.at(17+t_len)&0xff)))
  378. return false;
  379. if(strcmp(t_DeviceTypeId,APP_ID)!=0)
  380. return false;
  381. pro_len = ((rtn.at(20+t_len)&0x00ff)<<8)|(rtn.at(21+t_len)&0x00ff);
  382. for(i=0;i<pro_len;i++){
  383. data[i] = rtn.at(22+t_len+i)&0xff;
  384. datachk += (rtn.at(22+t_len+i)&0x00ff);
  385. //datachk = (datachk+rtn.at(22+t_len+i)&0x00ff)&0xffff;
  386. }
  387. MD5Update (&context, data, pro_len);
  388. t_datachk = ((rtn.at(23+t_len+pro_len)&0x00ff)<<8)|(rtn.at(24+t_len+pro_len)&0x00ff);
  389. if(t_datachk==datachk){
  390. fwrite(data,pro_len,1,fd);
  391. }else{
  392. return false;
  393. }
  394. }
  395. break;
  396. }
  397. return true;
  398. }
  399. void dogThread::chk_version()
  400. {
  401. unsigned char cmd[1024], md5[16];
  402. char chksum[33];
  403. int len,times;
  404. QByteArray Rtn;
  405. FILE *pfile;
  406. so->connectToHost("172.16.120.191",51212);
  407. if(so->waitForConnected(3000)){
  408. set_termtime();
  409. len = mk_verqry(cmd);
  410. if(len>0){
  411. so->write((const char *)cmd,len);
  412. if(so->waitForBytesWritten(3000)){
  413. set_termtime();
  414. if(so->waitForReadyRead(3000)){
  415. set_termtime();
  416. Rtn = so->readAll();
  417. QString dataTmp = "";
  418. for(int i=0;i<Rtn.length();i++){
  419. dataTmp.append(QString("%1").arg(Rtn.at(i)&0xff,2,16,QChar('0')));
  420. }
  421. printf("dataTmp [%s]\n",dataTmp.toUtf8().data());
  422. if(!check_frame(Rtn,0,NULL)){
  423. so->abort();
  424. return;
  425. }else{
  426. times = t_Length>>10;
  427. if(t_Length&1023)
  428. times++;
  429. pfile = fopen("/home/zyj/IOT-03/program.tmp","wb");
  430. if(pfile!=NULL){
  431. MD5Init (&context);
  432. for(int i=0;i<times;i++){
  433. len = mk_proqry(cmd,i);
  434. if(len>0){
  435. so->write((const char *)cmd,len);
  436. if(so->waitForBytesWritten(3000)){
  437. set_termtime();
  438. if(so->waitForReadyRead(3000)){
  439. set_termtime();
  440. Rtn = so->readAll();
  441. if(!check_frame(Rtn,i,pfile)){
  442. fclose(pfile);
  443. so->abort();
  444. return;
  445. }
  446. }else
  447. break;
  448. }else
  449. break;
  450. }
  451. }
  452. fclose(pfile);
  453. so->close();
  454. printf("222222\n");
  455. // MD5Final (md5, &context);
  456. // for(int i=0;i<16;i++){
  457. // if(((md5[i]>>4)&0x0f)<0x0a)
  458. // chksum[i*2+0] = (md5[i]>>4)|0x30;
  459. // else
  460. // chksum[i*2+0] = (md5[i]>>4)+0x57;
  461. // if((md5[i]&0x0f)<0x0a)
  462. // chksum[i*2+1] = (md5[i]&0x0f)|0x30;
  463. // else
  464. // chksum[i*2+1] = (md5[i]&0x0f)+0x57;
  465. // }
  466. // chksum[32]=0x00;
  467. QString md5;
  468. int len=0;
  469. FILE *file;
  470. unsigned char buffer[1024];
  471. QCryptographicHash hash(QCryptographicHash::Md5);
  472. if((file=fopen("/home/zyj/IOT-03/program.tmp","rb"))!=NULL){
  473. while ((len=fread(buffer,1,1024,file))>0) {
  474. hash.addData(QByteArray((const char*)buffer,len));
  475. }
  476. md5.append(hash.result().toHex());
  477. fclose(file);
  478. }
  479. printf("33333333 chksum [%s], t_Checksum [%s]\n",md5.toUtf8().data(),t_Checksum);
  480. if(strcmp(md5.toUtf8().data(),t_Checksum)==0){
  481. Major = t_Major;
  482. Minor = t_Minor;
  483. Length =t_Length;
  484. UpTime = t_UpTime;
  485. sprintf(Checksum,"%s",md5.toUtf8().data());
  486. save_cfg();
  487. runFlag = true;
  488. system("killall ytCore");
  489. rename("/home/zyj/IOT-03/program.tmp","/home/zyj/IOT-03/ytCore");
  490. system("chmod +x /home/zyj/IOT-03/ytCore");
  491. sleep(3);
  492. system("reboot");
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. }
  500. }
  501. void dogThread::set_termtime()
  502. {
  503. QDateTime curDatetime = QDateTime::currentDateTime();
  504. uint chkTime = curDatetime.toTime_t();
  505. ytShm->currentTime.YEAR = curDatetime.date().year()&0xffff;
  506. ytShm->currentTime.MONTH = curDatetime.date().month()&0xff;
  507. ytShm->currentTime.DAY = curDatetime.date().day()&0xff;
  508. ytShm->currentTime.HOUR = curDatetime.time().hour()&0xff;
  509. ytShm->currentTime.MINUTE = curDatetime.time().minute()&0xff;
  510. ytShm->currentTime.SECOND = curDatetime.time().second()&0xff;
  511. ytShm->currentTime.TIMESTAMP = chkTime;
  512. }
  513. void dogThread::run()
  514. {
  515. QDateTime curDatetime;
  516. so = new QTcpSocket;
  517. load_param();
  518. chk_file();
  519. if(!runFlag)
  520. chk_version();
  521. while(runFlag)
  522. {
  523. bool restart_app = false;
  524. curDatetime = QDateTime::currentDateTime();
  525. uint chkTime = curDatetime.toTime_t();
  526. set_termtime();
  527. for(quint8 i=1;i<60;i++)
  528. {
  529. if(ytShm->dogTimeList.dogTime[i].ENABLED==0x01){
  530. if((ytShm->dogTimeList.dogTime[i].LASTFEED<chkTime)
  531. &&((chkTime>(ytShm->dogTimeList.dogTime[i].LASTFEED
  532. +ytShm->dogTimeList.dogTime[i].WAITSEC))))
  533. {
  534. restart_app = true;
  535. break;
  536. }
  537. }
  538. }
  539. if(restart_app)
  540. {
  541. system(QString("killall %1").arg(APP_NAME).toUtf8().data());
  542. usleep(500000);
  543. start_app();
  544. }else if((curDatetime.time().hour()!=start_h)&&(curDatetime.time().minute()>12)){
  545. chk_version();
  546. start_h = curDatetime.time().hour();
  547. }
  548. sleep(1);
  549. }
  550. // while(runFlag)
  551. // {
  552. // bool restart_app = false;
  553. // curDatetime = QDateTime::currentDateTime();
  554. // uint chkTime = curDatetime.toTime_t();
  555. // set_termtime();
  556. // for(quint8 i=1;i<60;i++)
  557. // {
  558. // if(ytShm->dogTimeList.dogTime[i].ENABLED==0x01){
  559. // if((ytShm->dogTimeList.dogTime[i].LASTFEED<chkTime)
  560. // &&((chkTime>(ytShm->dogTimeList.dogTime[i].LASTFEED
  561. // +ytShm->dogTimeList.dogTime[i].WAITSEC))))
  562. // {
  563. // restart_app = true;
  564. // break;
  565. // }
  566. // }
  567. // }
  568. // if(restart_app)
  569. // {
  570. // system(QString("killall %1").arg(APP_NAME).toUtf8().data());
  571. // usleep(500000);
  572. // start_app();
  573. // }
  574. // sleep(1);
  575. // }
  576. }
  577. void dogThread::chk_file()
  578. {
  579. int i,len;
  580. unsigned char md5[16];
  581. QCryptographicHash hash(QCryptographicHash::Md5);
  582. unsigned char buffer[1024];
  583. // char MD5[33];
  584. QString MD5;
  585. FILE *file = fopen("/home/zyj/IOT-03/ytCore","rb");
  586. if(file!=NULL){
  587. // MD5Init (&context);
  588. // while((len=fread (buffer, 1, 1024, file))>0){
  589. // MD5Update (&context, buffer, len);
  590. // }
  591. // fclose(file);
  592. // MD5Final (md5, &context);
  593. // for(i=0;i<16;i++){
  594. // if(((md5[i]>>4)&0x0f)<0x0a)
  595. // MD5[i*2+0] = (md5[i]>>4)|0x30;
  596. // else
  597. // MD5[i*2+0] = (md5[i]>>4)+0x57;
  598. // if((md5[i]&0x0f)<0x0a)
  599. // MD5[i*2+1] = (md5[i]&0x0f)|0x30;
  600. // else
  601. // MD5[i*2+1] = (md5[i]&0x0f)+0x57;
  602. // }
  603. // MD5[32] = 0x00;
  604. while ((len=fread(buffer,1,1024,file))>0) {
  605. hash.addData(QByteArray((const char*)buffer,len));
  606. }
  607. MD5.append(hash.result().toHex());
  608. fclose(file);
  609. printf("MD5: %s\n",MD5.toUtf8().data());
  610. if(strcmp(MD5.toUtf8().data(),Checksum)==0){
  611. system("chmod +x /home/zyj/IOT-03/ytCore");
  612. runFlag = true;
  613. system("./ytCore &");
  614. }
  615. }
  616. }
  617. void dogThread::trim(char *buf, char *dat)
  618. {
  619. int i,n=0;
  620. bool start = false;
  621. for(i=0;i<256;i++)
  622. dat[i] = 0x00;
  623. for(i=0;i<strlen(buf);i++){
  624. if(!start){
  625. if(!isspace(buf[i])){
  626. start = true;
  627. if(isprint(buf[i]))
  628. dat[n++] = buf[i];
  629. }
  630. }else if(isprint(buf[i]))
  631. dat[n++] = buf[i];
  632. }
  633. for(i=(strlen(dat)-1);i>=0;i--){
  634. if((!isspace(dat[i]))&&(isprint(buf[i]))&&(buf[i]!='\r')&&(buf[i]!='\n'))
  635. break;
  636. else
  637. dat[i] = 0x00;
  638. }
  639. }
  640. void dogThread::chk_param(char *buf)
  641. {
  642. char name[256],value[256];
  643. char tmp[256];
  644. int i,n=0;
  645. bool start = false;
  646. bzero(name,sizeof(name));
  647. bzero(value,sizeof(value));
  648. trim(buf,tmp);
  649. for(i=0;i<strlen(tmp);i++){
  650. buf[i] = tmp[i];
  651. buf[i+1] = 0x00;
  652. }
  653. buf[strlen(tmp)] = 0x00;
  654. if(buf[0]=='#')
  655. return;
  656. for(i=0;i<strlen(buf);i++){
  657. if(!start){
  658. if(buf[i]=='='){
  659. start = true;
  660. n=0;
  661. }else
  662. name[n++] = buf[i];
  663. }else{
  664. value[n++] = buf[i];
  665. }
  666. }
  667. trim(name,tmp);
  668. bzero(name,sizeof(name));
  669. for(i=0;i<strlen(tmp);i++)
  670. name[i] = toupper(tmp[i]);
  671. name[strlen(tmp)]=0x00;
  672. trim(value,tmp);
  673. bzero(value,sizeof(value));
  674. for(i=0;i<strlen(tmp);i++)
  675. value[i] = tmp[i];
  676. value[strlen(tmp)]=0x00;
  677. if(strcmp(name,"MAJOR")==0)
  678. Major = atoi(value)&0xff;
  679. else if(strcmp(name,"MINOR")==0)
  680. Minor = atoi(value)&0xff;
  681. else if(strcmp(name,"LENGTH")==0)
  682. Length = atol(value)&0xffffffff;
  683. else if(strcmp(name,"UPTIME")==0)
  684. UpTime = atoll(value)&0xffffffff;
  685. else if(strcmp(name,"CHECKSUM")==0){
  686. for(i=0;i<32;i++)
  687. Checksum[i] = value[i];
  688. Checksum[32] = 0x00;
  689. printf("md5: %s\n",Checksum);
  690. }
  691. }
  692. bool dogThread::load_param()
  693. {
  694. char buf[256];
  695. FILE *file = fopen(daefile,"r");
  696. if(file!=NULL){
  697. while(!feof(file)){
  698. fgets(buf,256,file);
  699. chk_param(buf);
  700. }
  701. fclose(file);
  702. return true;
  703. }
  704. return false;
  705. }
  706. void dogThread::save_cfg()
  707. {
  708. FILE *file;
  709. char str[256];
  710. QDateTime dt = QDateTime::currentDateTime();
  711. file = fopen(daefile,"w");
  712. sprintf(str,"###################\r\n");
  713. printf("%s",str);
  714. fwrite(str,strlen(str),1,file);
  715. sprintf(str,"# Program Version File\r\n");
  716. printf("%s",str);
  717. fwrite(str,strlen(str),1,file);
  718. sprintf(str,"# Auto Create at %04d-%02d-%02d %02d:%02d:%02d\r\n"
  719. ,dt.date().year(),dt.date().month(),dt.date().day()
  720. ,dt.time().hour(),dt.time().minute(),dt.time().second());
  721. printf("%s",str);
  722. fwrite(str,strlen(str),1,file);
  723. sprintf(str,"###################\r\n\r\n");
  724. printf("%s",str);
  725. fwrite(str,strlen(str),1,file);
  726. printf("%s",str);
  727. sprintf(str,"DEVTYPE= %s\r\n",APP_ID);
  728. fwrite(str,strlen(str),1,file);
  729. printf("%s",str);
  730. sprintf(str,"MAJOR= %d\r\n",Major);
  731. fwrite(str,strlen(str),1,file);
  732. printf("%s",str);
  733. sprintf(str,"MINOR = %d\r\n",Minor);
  734. fwrite(str,strlen(str),1,file);
  735. printf("%s",str);
  736. sprintf(str,"LENGTH = %ld\r\n",Length);
  737. fwrite(str,strlen(str),1,file);
  738. printf("%s",str);
  739. sprintf(str,"UPTIME = %ld\r\n",UpTime);
  740. fwrite(str,strlen(str),1,file);
  741. printf("%s",str);
  742. sprintf(str,"CHECKSUM = %s\r\n",Checksum);
  743. fwrite(str,strlen(str),1,file);
  744. printf("%s",str);
  745. fflush(file);
  746. fclose(file);
  747. runFlag=true;
  748. }