AppFuncLib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2022-01-21 09:28:20
  4. * @Version : V3.0
  5. * @LastEditors : ChenJie
  6. * @LastEditTime : 2022-02-14 15:23:30
  7. * @Description : file content
  8. * @FilePath : \S32K146_4G\src\AppFuncLib.c
  9. */
  10. /*
  11. * AppFuncLib.c
  12. *应用层函数库
  13. * Created on: 2022年1月21日
  14. * Author: QiXiang_CHENJIE
  15. */
  16. #include "AppFuncLib.h"
  17. #include "AppGlobalVar.h"
  18. #include "Hal_Fls.h"
  19. #include "AppTaskUart1.h"
  20. /**
  21. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  22. * @param {UINT16} *ErrorArray
  23. * @param {UINT8} Errorlen
  24. * @return {*}
  25. */
  26. static uint8 bcc_chk_fota(uint8 *data, uint8 length);
  27. static uint8 Fota_crc_chk(uint8 *data, uint8 length);
  28. uint16 GetErrorNum(uint16 *ErrorArray, uint8 Errorlen)
  29. {
  30. uint16 OutNum;
  31. OutNum = *(ErrorArray);
  32. for (uint8 i = 0; i < Errorlen - 1; i++)
  33. {
  34. *(ErrorArray + i) = *(ErrorArray + i + 1);
  35. if (*(ErrorArray + i + 1) == 0)
  36. break;
  37. }
  38. return OutNum;
  39. }
  40. uint8 PutErrorNum(uint16 *ErrorArray, uint8 Errorlen, uint16 ErrorNum)
  41. {
  42. for (uint8 i = 0; i < Errorlen; i++)
  43. {
  44. if (*(ErrorArray + i) == 0)
  45. {
  46. *(ErrorArray + i) = ErrorNum;
  47. return 0;
  48. }
  49. else
  50. {
  51. if (*(ErrorArray + i) == ErrorNum)
  52. {
  53. return 1;
  54. }
  55. else
  56. {
  57. continue;
  58. }
  59. }
  60. }
  61. return 2;
  62. }
  63. uint16 ATstrdel(char *str)
  64. {
  65. char *p = str;
  66. bool flag = false;
  67. while (*str)
  68. {
  69. if (*str > 0x20)
  70. {
  71. *(p) = *str;
  72. p = p + 1;
  73. flag = false;
  74. }
  75. else
  76. {
  77. if (!flag)
  78. {
  79. *(p) = ',';
  80. p = p + 1;
  81. flag = true;
  82. }
  83. }
  84. str++;
  85. }
  86. *p = '\0';
  87. return 0;
  88. }
  89. uint16 mstrlen(const char *s)
  90. {
  91. uint16 out = 0;
  92. const char *ss = s;
  93. while (*ss)
  94. ss++;
  95. out = (ss - s);
  96. return out;
  97. }
  98. int mstrncmp(const char *s1, const char *s2, int n)
  99. {
  100. const unsigned char *c1 = (const unsigned char *)s1;
  101. const unsigned char *c2 = (const unsigned char *)s2;
  102. unsigned char ch;
  103. int d = 0;
  104. while (n--)
  105. {
  106. d = (int)(ch = *c1++) - (int)*c2++;
  107. if (d || !ch)
  108. break;
  109. }
  110. return d;
  111. }
  112. unsigned char HexToChar(unsigned char bHex)
  113. {
  114. if ((bHex >= 0) && (bHex <= 9))
  115. bHex += 0x30;
  116. else if ((bHex >= 10) && (bHex <= 15)) //大写字母
  117. bHex += 0x37;
  118. else
  119. bHex = 0xff;
  120. return bHex;
  121. }
  122. unsigned char CharToHex(unsigned char bChar)
  123. {
  124. if ((bChar >= 0x30) && (bChar <= 0x39))
  125. bChar -= 0x30;
  126. else if ((bChar >= 0x41) && (bChar <= 0x46)) //大写字母
  127. bChar -= 0x37;
  128. else if ((bChar >= 0x61) && (bChar <= 0x66)) //小写字母
  129. bChar -= 0x57;
  130. else
  131. bChar = 0xff;
  132. return bChar;
  133. }
  134. uint8 AtStrCompare(const char *a, const char *b)
  135. {
  136. uint8 out = 1;
  137. while (1)
  138. {
  139. if (*a == '\0' || *b == '\0') //判断其中是否有字符串结束
  140. {
  141. if (strlen(a) == strlen(b))
  142. {
  143. out = 1;
  144. break;
  145. }
  146. else
  147. {
  148. out = 0;
  149. break;
  150. }
  151. }
  152. else
  153. {
  154. if (*a != *b)
  155. {
  156. out = 0;
  157. break;
  158. }
  159. else if (*a == '=' && *b == '=')
  160. {
  161. out = 1;
  162. break;
  163. }
  164. }
  165. a++;
  166. b++;
  167. }
  168. return out;
  169. }
  170. unsigned short CRC16_Modbus(unsigned char *pdata, int len)
  171. {
  172. unsigned short crc = 0xFFFF;
  173. int i, j;
  174. for (j = 0; j < len; j++)
  175. {
  176. crc = crc ^ pdata[j];
  177. for (i = 0; i < 8; i++)
  178. {
  179. if ((crc & 0x0001) > 0)
  180. {
  181. crc = crc >> 1;
  182. crc = crc ^ 0xa001;
  183. }
  184. else
  185. crc = crc >> 1;
  186. }
  187. }
  188. return crc;
  189. }
  190. char *Myitoa(int value, char *result, int base)
  191. {
  192. // check that the base if valid
  193. if (base < 2 || base > 36)
  194. {
  195. *result = '\0';
  196. return result;
  197. }
  198. char *ptr = result, *ptr1 = result, tmp_char;
  199. int tmp_value;
  200. do
  201. {
  202. tmp_value = value;
  203. value /= base;
  204. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  205. } while (value);
  206. // Apply negative sign
  207. if (tmp_value < 0)
  208. *ptr++ = '-';
  209. *ptr-- = '\0';
  210. while (ptr1 < ptr)
  211. {
  212. tmp_char = *ptr;
  213. *ptr-- = *ptr1;
  214. *ptr1++ = tmp_char;
  215. }
  216. return result;
  217. }
  218. /************************************************************************
  219. * @brief 整数转字符串
  220. * @param[in] num 整数
  221. * @param[out] buf 字符串
  222. * @return 返回字符串长度
  223. ************************************************************************/
  224. inline int _itoa(int num, char buf[32])
  225. {
  226. return _i2a(num, buf, 10);
  227. }
  228. /************************************************************************
  229. * @brief 整数转字符串
  230. * @param[in] num 整数
  231. * @param[out] buf 字符串
  232. * @param[in] radix 进位制整数
  233. * @return 返回字符串长度
  234. ************************************************************************/
  235. int _i2a(int num, char buf[32], int radix)
  236. {
  237. static const char s[] = "0123456789abcdef";
  238. int n = num, R = radix;
  239. char *dst = buf;
  240. if (n < 0)
  241. {
  242. *dst++ = '-';
  243. n = -n;
  244. }
  245. if (n < 10)
  246. {
  247. *dst++ = s[n];
  248. *dst = 0;
  249. }
  250. else
  251. {
  252. char tmp[32], *p = tmp;
  253. while (n)
  254. {
  255. *p++ = s[n % R];
  256. n /= R;
  257. }
  258. while (--p != tmp)
  259. *dst++ = *p;
  260. *dst++ = *tmp;
  261. *dst = 0;
  262. }
  263. return dst - buf;
  264. }
  265. /************************************************************************
  266. * @brief 浮点数转字符串
  267. * @param[in] val 浮点数
  268. * @param[out] buf 字符串
  269. * @param[in] eps 精度(小数位)
  270. * @return 返回字符串长度
  271. ************************************************************************/
  272. int _ftoa(double val, char buf[32], int eps)
  273. {
  274. double f = val;
  275. char *p = buf;
  276. if (val < 0)
  277. {
  278. *p++ = '-';
  279. f = -f;
  280. }
  281. int n = f;
  282. int len = _itoa(n, p);
  283. return len + __ftoa(f - n, p + len, eps);
  284. }
  285. /************************************************************************
  286. * @brief 浮点数转字符串:范围(-1, 1)
  287. * @param[in] val 浮点数
  288. * @param[out] buf 字符串
  289. * @param[in] eps 精度(小数位)
  290. * @return 返回字符串长度
  291. ************************************************************************/
  292. int __ftoa(double val, char buf[32], int eps)
  293. {
  294. double f = val;
  295. char *p = buf;
  296. static const char s[] = "0123456789";
  297. if (f < 0)
  298. {
  299. *p++ = '-';
  300. f = -f;
  301. }
  302. *p++ = '.';
  303. for (int i = eps + 1, n; --i; ++p, f -= n)
  304. *p = s[n = f *= 10.0];
  305. *p = 0;
  306. return p - buf;
  307. }
  308. /************************************************************************
  309. * @brief 替换sprintf
  310. * @ref 可变长参数列表误区与陷阱——va_arg不可接受的类型
  311. * http://www.cppblog.com/ownwaterloo/archive/2009/04/21/80655.aspx
  312. ************************************************************************/
  313. int _sprintf(char *dst, const char *format, ...)
  314. {
  315. char *s = dst;
  316. const char *f = format;
  317. va_list ap, another;
  318. va_start(ap, format);
  319. va_copy(another, ap);
  320. while (*f)
  321. {
  322. int n = 1;
  323. if ('%' != *f)
  324. {
  325. *s = *f;
  326. }
  327. else
  328. {
  329. ++f;
  330. switch (*f)
  331. {
  332. case 's': // 字符串
  333. {
  334. const char *p = va_arg(ap, char *);
  335. n = strlen(p);
  336. memcpy(s, p, n);
  337. }
  338. break;
  339. case 'd':
  340. case 'u': // 整数
  341. {
  342. char buf[32];
  343. n = _itoa(va_arg(ap, int), buf);
  344. memcpy(s, buf, n);
  345. }
  346. break;
  347. case 'f': // 浮点数
  348. {
  349. char buf[32];
  350. n = _ftoa(va_arg(ap, double), buf, 6);
  351. memcpy(s, buf, n);
  352. }
  353. break;
  354. case 'x': // 16进制数
  355. {
  356. char buf[32];
  357. n = _i2a(va_arg(ap, int), buf, 16);
  358. memcpy(s, buf, n);
  359. }
  360. break;
  361. case 'c': // 字符
  362. {
  363. *s = va_arg(ap, int);
  364. }
  365. break;
  366. case '%': // 百分号
  367. {
  368. *s = '%';
  369. }
  370. break;
  371. default:
  372. {
  373. va_end(ap);
  374. int x = vsprintf(dst, format, another);
  375. va_end(another);
  376. return x;
  377. }
  378. break;
  379. }
  380. }
  381. ++f;
  382. s += n;
  383. }
  384. *s = 0;
  385. va_end(ap);
  386. return s - dst;
  387. }
  388. uint8 bcc_chk(uint8 *data, uint16 length)
  389. {
  390. uint8 bcc_chk_return = 0x00;
  391. uint16 count = 0;
  392. while (count < length)
  393. {
  394. bcc_chk_return ^= data[count];
  395. count++;
  396. }
  397. return bcc_chk_return;
  398. }
  399. uint16 crc_chk(uint8 *data, uint8 length)
  400. {
  401. uint8 j;
  402. uint16 reg_crc = 0xFFFF;
  403. while (length--)
  404. {
  405. reg_crc ^= *data++;
  406. for (j = 0; j < 8; j++)
  407. {
  408. if (reg_crc & 0x01)
  409. {
  410. reg_crc = (reg_crc >> 1) ^ 0xA001;
  411. }
  412. else
  413. {
  414. reg_crc = reg_crc >> 1;
  415. }
  416. }
  417. }
  418. return reg_crc;
  419. }
  420. /**
  421. * @brief : Fota升级处理函数,将接收的数据进行校验,搬运至升级区域,并进行应答
  422. * @param {UINT8} *DataPtr
  423. * @param {INT32} connectId
  424. * @return {*}
  425. */
  426. void Fota_Func(uint8 *DataPtr, sint32 connectId)
  427. {
  428. Fota_Type Fota_S;
  429. uint8 Fota_Answer[43];
  430. uint8 Fota_Cmd;
  431. sint8 ret;
  432. uint8 *Data_Read_Buffer = NULL;
  433. uint8 Data_Read_Crc;
  434. if (*(DataPtr + 30) == 0x01)
  435. {
  436. uint32 FlashAddStart = 0;
  437. uint32 appReceviedCRC;
  438. Hal_FlsGetAppVectorTableStartAddr(&FlashAddStart);
  439. Fota_S.Fota_Flash_Addres = FlashAddStart;
  440. Fota_Cmd = *(DataPtr + 31);
  441. Fota_Answer[0] = TCP_START_SYM1;
  442. Fota_Answer[1] = TCP_START_SYM2;
  443. Fota_Answer[2] = TCP_CONCMD_SYM;
  444. switch (Fota_Cmd)
  445. {
  446. case 0x01:
  447. {
  448. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  449. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  450. if (Fota_S.Fota_All_Data_Len >= 0x7FE00)
  451. {
  452. Fota_Answer[3] = 0x02;
  453. }
  454. else
  455. {
  456. Fota_Answer[3] = 0x01;
  457. appReceviedCRC = (uint32)(*(DataPtr + 42)<<8 | *(DataPtr + 43));
  458. Hal_FlsErase(Fota_S.Fota_Flash_Addres,Fota_S.Fota_All_Data_Len,5);
  459. Hal_SetAppInfo(Fota_S.Fota_All_Data_Len,appReceviedCRC,CONTROLLER_SELF);
  460. }
  461. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  462. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  463. Fota_Answer[22] = 0x00;
  464. Fota_Answer[23] = 0x12;
  465. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  466. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  467. tcpipConnectionSend(connectId, Fota_Answer, 43);
  468. break;
  469. }
  470. case 0x02:
  471. {
  472. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  473. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  474. Fota_S.Fota_Recv_Data_Len = *(DataPtr + 41);
  475. memset(Fota_S.Fota_Recv_Data, 0x00, 100);
  476. memcpy(Fota_S.Fota_Recv_Data, (DataPtr + 42), *(DataPtr + 41));
  477. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data, Fota_S.Fota_Recv_Data_Len);
  478. if (Fota_S.Fota_CRC == *(DataPtr + Fota_S.Fota_Recv_Data_Len + 42))
  479. {
  480. uint8 Fota_Recv_Data_Len_4 = 0;
  481. if (Fota_S.Fota_Recv_Data_Len % 4 != 0)
  482. {
  483. Fota_Recv_Data_Len_4 = Fota_S.Fota_Recv_Data_Len + 4 - (Fota_S.Fota_Recv_Data_Len % 4);
  484. }
  485. else
  486. {
  487. Fota_Recv_Data_Len_4 = Fota_S.Fota_Recv_Data_Len;
  488. }
  489. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
  490. Hal_FlsWrite(Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data,Fota_Recv_Data_Len_4, 10);
  491. memset(Data_Read_Buffer, 0x00, Fota_S.Fota_Recv_Data_Len);
  492. Hal_FlsRead(Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres, Data_Read_Buffer, Fota_S.Fota_Recv_Data_Len, 10);
  493. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer, Fota_S.Fota_Recv_Data_Len);
  494. if (Data_Read_Crc == Fota_S.Fota_CRC)
  495. {
  496. Fota_Answer[3] = 0x01;
  497. }
  498. else
  499. {
  500. Fota_Answer[3] = 0x02;
  501. Hal_FlsErase(Fota_S.Fota_Flash_Addres + Fota_S.Fota_Current_Addres,Fota_Recv_Data_Len_4,5);
  502. }
  503. }
  504. else //数据校验失败
  505. {
  506. Fota_Answer[3] = 0x02;
  507. }
  508. if (Data_Read_Buffer != NULL)
  509. free(Data_Read_Buffer);
  510. Data_Read_Buffer = NULL;
  511. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  512. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  513. Fota_Answer[22] = 0x00;
  514. Fota_Answer[23] = 0x12;
  515. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  516. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  517. tcpipConnectionSend(connectId, Fota_Answer, 43);
  518. break;
  519. }
  520. case 0x03:
  521. {
  522. Fota_S.Fota_All_Data_Len = *(DataPtr + 33) << 24 | *(DataPtr + 34) << 16 | *(DataPtr + 35) << 8 | *(DataPtr + 36);
  523. Fota_S.Fota_Current_Addres = *(DataPtr + 37) << 24 | *(DataPtr + 38) << 16 | *(DataPtr + 39) << 8 | *(DataPtr + 40);
  524. Fota_Answer[3] = 0x01;
  525. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  526. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  527. Fota_Answer[22] = 0x00;
  528. Fota_Answer[23] = 0x12;
  529. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  530. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  531. tcpipConnectionSend(connectId, Fota_Answer, 43);
  532. if (Fota_S.Fota_All_Data_Len == Fota_S.Fota_Current_Addres)
  533. {
  534. Hal_FlsCheckIsTransferSucceed();
  535. Fota_update_flag = TRUE;
  536. }
  537. else
  538. {
  539. Fota_update_flag = FALSE;
  540. }
  541. break;
  542. }
  543. default:
  544. {
  545. Fota_Answer[3] = 0x02;
  546. memcpy(&Fota_Answer[4], (DataPtr + 4), BATT_SN_LEN);
  547. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  548. Fota_Answer[22] = 0x00;
  549. Fota_Answer[23] = 0x12;
  550. memcpy(&Fota_Answer[24], (DataPtr + 24), 18);
  551. Fota_Answer[42] = bcc_chk_fota(Fota_Answer, 42);
  552. tcpipConnectionSend(connectId, Fota_Answer, 43);
  553. break;
  554. }
  555. }
  556. }
  557. }
  558. /**
  559. * @brief : fota网络校验函数
  560. * @param {UINT8} *data
  561. * @param {UINT8} length
  562. * @return {*}
  563. */
  564. static uint8 bcc_chk_fota(uint8 *data, uint8 length)
  565. {
  566. uint8 bcc_chk_return = 0x00;
  567. uint8 count = 0;
  568. while (count < length)
  569. {
  570. bcc_chk_return ^= data[count];
  571. count++;
  572. }
  573. return bcc_chk_return;
  574. }
  575. /**
  576. * @brief : Fota校验函数
  577. * @param {UINT8} *data
  578. * @param {UINT8} length
  579. * @return {*}
  580. */
  581. static uint8 Fota_crc_chk(uint8 *data, uint8 length)
  582. {
  583. uint8 reg_crc = 0x00;
  584. while (length--)
  585. {
  586. reg_crc ^= *data++;
  587. }
  588. return reg_crc;
  589. }