bsp_common.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "bsp_common.h"
  22. const uint8_t ByteToAsciiTable[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  23. void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize)
  24. {
  25. uint8_t *Data = (uint8_t *)Src;
  26. Buf->Data = Data;
  27. Buf->Len = 0;
  28. Buf->MaxLength = MaxLen;
  29. Buf->Offset = 0;
  30. Buf->DataSize = DataSize;
  31. }
  32. uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len)
  33. {
  34. uint32_t i, p;
  35. uint8_t *Data = (uint8_t *)Src;
  36. if (Buf->Len < Len)
  37. {
  38. Len = Buf->Len;
  39. }
  40. if (Buf->DataSize > 1)
  41. {
  42. for (i = 0, p = Buf->Offset; i < Len; i++, p++)
  43. {
  44. if (p >= Buf->MaxLength)
  45. {
  46. p -= Buf->MaxLength;
  47. }
  48. memcpy(Data + (i * Buf->DataSize), Buf->Data + (p * Buf->DataSize), Buf->DataSize);
  49. }
  50. }
  51. else
  52. {
  53. for (i = 0, p = Buf->Offset; i < Len; i++, p++)
  54. {
  55. if (p >= Buf->MaxLength)
  56. {
  57. p -= Buf->MaxLength;
  58. }
  59. Data[i] = Buf->Data[p];
  60. }
  61. }
  62. return Len;
  63. }
  64. uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len)
  65. {
  66. uint32_t l;
  67. uint8_t *Data = (uint8_t *)Src;
  68. l = LoopBuffer_Query(Buf, Data, Len);
  69. Buf->Len -= l;
  70. Buf->Offset += l;
  71. if (Buf->Offset >= Buf->MaxLength)
  72. {
  73. Buf->Offset -= Buf->MaxLength;
  74. }
  75. if (!Buf->Len) {
  76. Buf->Offset = 0;
  77. }
  78. return l;
  79. }
  80. void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len)
  81. {
  82. if (Buf->Len < Len)
  83. {
  84. Len = Buf->Len;
  85. }
  86. Buf->Len -= Len;
  87. Buf->Offset += Len;
  88. if (Buf->Offset >= Buf->MaxLength)
  89. {
  90. Buf->Offset -= Buf->MaxLength;
  91. }
  92. if (!Buf->Len) {
  93. Buf->Offset = 0;
  94. }
  95. }
  96. uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len)
  97. {
  98. uint32_t i, p, cut_off = 0;
  99. uint8_t *Data = (uint8_t *)Src;
  100. if (!Buf->Len && !Buf->Offset && (Len <= Buf->Len))
  101. {
  102. memcpy(Buf->Data, Data, Len);
  103. Buf->Len = Len;
  104. return Len;
  105. }
  106. cut_off = Buf->MaxLength - Buf->Len;
  107. if (cut_off >= Len)
  108. {
  109. cut_off = 0;
  110. }
  111. else
  112. {
  113. cut_off = Len - cut_off;
  114. }
  115. if (Buf->DataSize > 1)
  116. {
  117. for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
  118. {
  119. if (p >= Buf->MaxLength)
  120. {
  121. p -= Buf->MaxLength;
  122. }
  123. memcpy(Buf->Data + (p * Buf->DataSize), Data + (i * Buf->DataSize), Buf->DataSize);
  124. }
  125. }
  126. else
  127. {
  128. for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
  129. {
  130. if (p >= Buf->MaxLength)
  131. {
  132. p -= Buf->MaxLength;
  133. }
  134. Buf->Data[p] = Data[i];
  135. }
  136. }
  137. Buf->Offset += cut_off;
  138. if (Buf->Offset >= Buf->MaxLength)
  139. Buf->Offset -= Buf->MaxLength;
  140. Buf->Len += Len;
  141. if (Buf->Len > Buf->MaxLength)
  142. Buf->Len = Buf->MaxLength;
  143. return Len;
  144. }
  145. void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen)
  146. {
  147. Buf->Data = Src;
  148. Buf->Pos = 0;
  149. Buf->MaxLen = MaxLen;
  150. }
  151. int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
  152. {
  153. if (!Len)
  154. {
  155. return -1;
  156. }
  157. if (!Buf)
  158. {
  159. return -1;
  160. }
  161. if ((Buf->Pos + Len) > Buf->MaxLen)
  162. {
  163. Len = Buf->MaxLen - Buf->Pos;
  164. }
  165. if (Len)
  166. {
  167. memcpy(&Buf->Data[Buf->Pos], Data, Len);
  168. }
  169. Buf->Pos += Len;
  170. return Len;
  171. }
  172. //void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len)
  173. //{
  174. // uint32_t RestLen;
  175. // uint32_t i;
  176. // if (!Buf)
  177. // return ;
  178. // if (!Buf->Data)
  179. // return ;
  180. // if (Len >= Buf->Pos)
  181. // {
  182. // Buf->Pos = 0;
  183. // return ;
  184. // }
  185. // RestLen = Buf->Pos - Len;
  186. // memmove(Buf->Data, Buf->Data + Len, RestLen);
  187. // Buf->Pos = RestLen;
  188. //}
  189. /*****************************************************************************
  190. * FUNCTION
  191. * command_parse_param()
  192. * DESCRIPTION
  193. * Parse AT command string to parameters
  194. * PARAMETERS
  195. * char* pStr
  196. * RETURNS
  197. * pCmdParam
  198. *****************************************************************************/
  199. uint32_t CmdParseParam(int8_t* pStr, CmdParam *CP, int8_t Cut)
  200. {
  201. uint32_t paramStrLen = strlen((char *)pStr);
  202. uint32_t paramIndex = 0;
  203. uint32_t paramCharIndex = 0;
  204. uint32_t index = 0;
  205. while ((pStr[index] != '\r')
  206. && (index < paramStrLen)
  207. && (paramIndex < CP->param_max_num)) {
  208. if (pStr[index] == Cut) {
  209. /* Next param string */
  210. paramCharIndex = 0;
  211. paramIndex++;
  212. }
  213. else {
  214. if (pStr[index] != '"')
  215. {
  216. if (paramCharIndex >= CP->param_max_len)
  217. return (0);
  218. /*Get each of command param char, the param char except char ' " '*/
  219. CP->param_str[paramIndex * CP->param_max_len + paramCharIndex] = pStr[index];
  220. paramCharIndex++;
  221. }
  222. }
  223. index++;
  224. }
  225. CP->param_num = paramIndex + 1;
  226. return (1);
  227. }
  228. __attribute__((weak)) uint8_t OS_CheckInIrq(void)
  229. {
  230. return __get_IPSR();
  231. }
  232. __attribute__((weak)) void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len)
  233. {
  234. uint32_t RestLen;
  235. uint32_t i;
  236. if (!Buf)
  237. return ;
  238. if (!Buf->Data)
  239. return ;
  240. if (Len >= Buf->Pos)
  241. {
  242. Buf->Pos = 0;
  243. return ;
  244. }
  245. RestLen = Buf->Pos - Len;
  246. memmove(Buf->Data, Buf->Data + Len, RestLen);
  247. Buf->Pos = RestLen;
  248. }
  249. int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value)
  250. {
  251. uint32_t Mask,Pos1,Pos2;
  252. Pos1 = Sn/8;
  253. Pos2 = Sn%8;
  254. Mask = ~(1 << Pos2);
  255. if (Value)
  256. {
  257. Value = (1 << Pos2);
  258. }
  259. Data[Pos1] = (Data[Pos1] & Mask) | Value;
  260. //DBG("%d %d %d %d", Sn, Pos1, Pos2, Value);
  261. return 0;
  262. }
  263. int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value)
  264. {
  265. uint32_t Mask,Pos1,Pos2;
  266. Pos1 = Sn/8;
  267. Pos2 = Sn%8;
  268. Mask = (1 << Pos2);
  269. if (Data[Pos1] & Mask)
  270. {
  271. *Value = 1;
  272. }
  273. else
  274. {
  275. *Value = 0;
  276. }
  277. return -1;
  278. }
  279. uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn)
  280. {
  281. uint32_t Mask,Pos1,Pos2;
  282. Pos1 = Sn/8;
  283. Pos2 = Sn%8;
  284. Mask = (1 << Pos2);
  285. if (Data[Pos1] & Mask)
  286. {
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart)
  292. {
  293. uint8_t Check = CheckStart;
  294. uint8_t *Data = (uint8_t *)Src;
  295. uint32_t i;
  296. for (i = 0; i < Len; i++)
  297. {
  298. Check ^= Data[i];
  299. }
  300. return Check;
  301. }
  302. uint8_t SumCheck(uint8_t *Data, uint32_t Len)
  303. {
  304. uint8_t Check = 0;
  305. uint32_t i;
  306. for (i = 0; i < Len; i++)
  307. {
  308. Check += Data[i];
  309. }
  310. return Check;
  311. }
  312. uint8_t CRC8Cal(void *Data, uint16_t Len, uint8_t CRC8Last, uint8_t CRCRoot, uint8_t IsReverse)
  313. {
  314. uint16_t i;
  315. uint8_t CRC8 = CRC8Last;
  316. uint8_t wTemp = CRCRoot;
  317. uint8_t *Src = (uint8_t *)Data;
  318. if (IsReverse)
  319. {
  320. CRCRoot = 0;
  321. for (i = 0; i < 8; i++)
  322. {
  323. if (wTemp & (1 << (7 - i)))
  324. {
  325. CRCRoot |= 1 << i;
  326. }
  327. }
  328. while (Len--)
  329. {
  330. CRC8 ^= *Src++;
  331. for (i = 0; i < 8; i++)
  332. {
  333. if ((CRC8 & 0x01))
  334. {
  335. CRC8 >>= 1;
  336. CRC8 ^= CRCRoot;
  337. }
  338. else
  339. {
  340. CRC8 >>= 1;
  341. }
  342. }
  343. }
  344. }
  345. else
  346. {
  347. while (Len--)
  348. {
  349. CRC8 ^= *Src++;
  350. for (i = 8; i > 0; --i)
  351. {
  352. if ((CRC8 & 0x80))
  353. {
  354. CRC8 <<= 1;
  355. CRC8 ^= CRCRoot;
  356. }
  357. else
  358. {
  359. CRC8 <<= 1;
  360. }
  361. }
  362. }
  363. }
  364. return CRC8;
  365. }
  366. /************************************************************************/
  367. /* CRC16 */
  368. /************************************************************************/
  369. uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse)
  370. {
  371. uint16_t i;
  372. uint16_t CRC16 = CRC16Last;
  373. uint16_t wTemp = CRCRoot;
  374. uint8_t *Src = (uint8_t *)Data;
  375. if (IsReverse)
  376. {
  377. CRCRoot = 0;
  378. for (i = 0; i < 16; i++)
  379. {
  380. if (wTemp & (1 << (15 - i)))
  381. {
  382. CRCRoot |= 1 << i;
  383. }
  384. }
  385. while (Len--)
  386. {
  387. for (i = 0; i < 8; i++)
  388. {
  389. if ((CRC16 & 0x0001) != 0)
  390. {
  391. CRC16 >>= 1;
  392. CRC16 ^= CRCRoot;
  393. }
  394. else
  395. {
  396. CRC16 >>= 1;
  397. }
  398. if ((*Src&(1 << i)) != 0)
  399. {
  400. CRC16 ^= CRCRoot;
  401. }
  402. }
  403. Src++;
  404. }
  405. }
  406. else
  407. {
  408. while (Len--)
  409. {
  410. for (i = 8; i > 0; i--)
  411. {
  412. if ((CRC16 & 0x8000) != 0)
  413. {
  414. CRC16 <<= 1;
  415. CRC16 ^= CRCRoot;
  416. }
  417. else
  418. {
  419. CRC16 <<= 1;
  420. }
  421. if ((*Src&(1 << (i - 1))) != 0)
  422. {
  423. CRC16 ^= CRCRoot;
  424. }
  425. }
  426. Src++;
  427. }
  428. }
  429. return CRC16;
  430. }
  431. uint32_t AsciiToU32(uint8_t *Src, uint32_t Len)
  432. {
  433. uint32_t i = 0;
  434. uint32_t Temp = 0;
  435. for (i = 0; i < Len; i++)
  436. {
  437. if (Src[i])
  438. {
  439. Temp *= 10;
  440. Temp += Src[i] - '0';
  441. }
  442. else
  443. {
  444. break;
  445. }
  446. }
  447. return Temp;
  448. }
  449. /**
  450. * @brief 反转数据
  451. * @param ref 需要反转的变量
  452. * @param ch 反转长度,多少位
  453. * @retval N反转后的数据
  454. */
  455. static LongInt Reflect(LongInt ref, uint8_t ch)
  456. {
  457. LongInt value = 0;
  458. LongInt i;
  459. for (i = 1; i < (LongInt)(ch + 1); i++)
  460. {
  461. if (ref & 1)
  462. value |= (LongInt)1 << (ch - i);
  463. ref >>= 1;
  464. }
  465. return value;
  466. }
  467. /**
  468. * @brief 建立CRC32的查询表
  469. * @param Tab 表缓冲
  470. * @param Gen CRC32根
  471. * @retval None
  472. */
  473. void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen)
  474. {
  475. uint32_t crc;
  476. uint32_t i, j, temp, t1, t2, flag;
  477. if (Tab[1] != 0)
  478. return;
  479. for (i = 0; i < 256; i++)
  480. {
  481. temp = Reflect(i, 8);
  482. Tab[i] = temp << 24;
  483. for (j = 0; j < 8; j++)
  484. {
  485. flag = Tab[i] & 0x80000000;
  486. t1 = Tab[i] << 1;
  487. if (0 == flag)
  488. {
  489. t2 = 0;
  490. }
  491. else
  492. {
  493. t2 = Gen;
  494. }
  495. Tab[i] = t1 ^ t2;
  496. }
  497. crc = Tab[i];
  498. Tab[i] = Reflect(crc, 32);
  499. }
  500. }
  501. /**
  502. * @brief 计算buffer的crc校验码
  503. * @param CRC32_Table CRC32表
  504. * @param Buf 缓冲
  505. * @param Size 缓冲区长度
  506. * @param CRC32 初始CRC32值
  507. * @retval 计算后的CRC32
  508. */
  509. uint32_t CRC32_Cal(uint32_t *CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last)
  510. {
  511. uint32_t i;
  512. for (i = 0; i < Size; i++)
  513. {
  514. CRC32Last = CRC32_Table[(CRC32Last ^ Buf[i]) & 0xff] ^ (CRC32Last >> 8);
  515. }
  516. return CRC32Last;
  517. }
  518. /************************************************************************/
  519. /*时间与时间戳转换,C语言实现 */
  520. /************************************************************************/
  521. /************************************************************************/
  522. uint8_t IsLeapYear(uint32_t Year)
  523. {
  524. if ((Year % 400) == 0)
  525. return 1;
  526. if ((((Year % 4) == 0) && (Year % 100) != 0))
  527. return 1;
  528. else
  529. return 0;
  530. }
  531. const uint32_t DayTable[2][12] = { { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } };
  532. //const uint32_t DayTable[2][12] = { { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } };
  533. LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time)
  534. {
  535. LongInt DYear, DDay, DSec;
  536. uint32_t Year100;
  537. DYear = Date->Year - 1970;
  538. if (DYear) //1970年以后,1972是第一个闰年,1973年是第一年需要增加一天,2100年是非闰年
  539. {
  540. DDay = DYear * 365 + ((DYear + 1) / 4) + DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
  541. // if (IsLeapYear(Date->Year))
  542. // {
  543. // DDay--;
  544. // }
  545. if (Date->Year >= 2100)
  546. {
  547. Year100 = Date->Year - 2100;
  548. DDay -= (1 + Year100 / 100);
  549. if (Date->Year >= 2400)
  550. {
  551. Year100 = Date->Year - 2400;
  552. DDay += 1 + Year100 / 400;
  553. }
  554. }
  555. }
  556. else
  557. {
  558. DDay = DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
  559. }
  560. DSec = DDay * 86400 + Time->Hour * 3600 + Time->Min * 60 + Time->Sec;
  561. return DSec;
  562. }
  563. #define YEAR_1_DAY_BEFORE2000 365
  564. #define YEAR_2_DAY_BEFORE2000 730
  565. #define YEAR_3_DAY_BEFORE2000 1096
  566. #define YEAR_1_DAY_AFTER2000 365
  567. #define YEAR_2_DAY_AFTER2000 730
  568. #define YEAR_3_DAY_AFTER2000 1095
  569. #define YEAR_4_DAY 1461
  570. #define YEAR_31_DAY 11323
  571. #define YEAR_100_DAY 36524
  572. #define YEAR_400_DAY 146097
  573. uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay)
  574. {
  575. uint32_t DYear,i, LeapFlag, Temp;
  576. uint32_t DDay;
  577. DDay = Sec / 86400;
  578. if (DDay != LastDDay)
  579. {
  580. DYear = 0;
  581. Time->Week = (4 + DDay) % 7;
  582. if (DDay >= YEAR_31_DAY)
  583. {
  584. DDay -= YEAR_31_DAY;
  585. DYear = 31;
  586. if (DDay >= YEAR_400_DAY)
  587. {
  588. Temp = DDay / YEAR_400_DAY;
  589. DYear += Temp * 400;
  590. DDay -= Temp * YEAR_400_DAY;
  591. }
  592. if (DDay >= YEAR_100_DAY)
  593. {
  594. Temp = DDay / YEAR_100_DAY;
  595. DYear += Temp * 100;
  596. DDay -= Temp * YEAR_100_DAY;
  597. }
  598. if (DDay >= YEAR_4_DAY)
  599. {
  600. Temp = DDay / YEAR_4_DAY;
  601. DYear += Temp * 4;
  602. DDay -= Temp * YEAR_4_DAY;
  603. }
  604. if (DDay >= YEAR_3_DAY_AFTER2000)
  605. {
  606. DYear += 3;
  607. DDay -= YEAR_3_DAY_AFTER2000;
  608. }
  609. else if (DDay >= YEAR_2_DAY_AFTER2000)
  610. {
  611. DYear += 2;
  612. DDay -= YEAR_2_DAY_AFTER2000;
  613. }
  614. else if (DDay >= YEAR_1_DAY_AFTER2000)
  615. {
  616. DYear += 1;
  617. DDay -= YEAR_1_DAY_AFTER2000;
  618. }
  619. }
  620. else
  621. {
  622. if (DDay >= YEAR_4_DAY)
  623. {
  624. Temp = DDay / YEAR_4_DAY;
  625. DYear += Temp * 4;
  626. DDay -= Temp * YEAR_4_DAY;
  627. }
  628. if (DDay >= YEAR_3_DAY_BEFORE2000)
  629. {
  630. DYear += 3;
  631. DDay -= YEAR_3_DAY_BEFORE2000;
  632. }
  633. else if (DDay >= YEAR_2_DAY_BEFORE2000)
  634. {
  635. DYear += 2;
  636. DDay -= YEAR_2_DAY_BEFORE2000;
  637. }
  638. else if (DDay >= YEAR_1_DAY_BEFORE2000)
  639. {
  640. DYear += 1;
  641. DDay -= YEAR_1_DAY_BEFORE2000;
  642. }
  643. }
  644. Date->Year = DYear + 1970;
  645. LeapFlag = IsLeapYear(Date->Year);
  646. Date->Mon = 12;
  647. for (i = 1; i < 12; i++)
  648. {
  649. if (DDay < DayTable[LeapFlag][i])
  650. {
  651. Date->Mon = i;
  652. break;
  653. }
  654. }
  655. Date->Day = DDay - DayTable[LeapFlag][Date->Mon - 1] + 1;
  656. }
  657. Sec = Sec % 86400;
  658. Time->Hour = Sec / 3600;
  659. Sec = Sec % 3600;
  660. Time->Min = Sec / 60;
  661. Time->Sec = Sec % 60;
  662. return DDay;
  663. }
  664. /**
  665. * \brief get a byte (8bits) from a pointer
  666. *
  667. * Caller should ensure parameters are valid.
  668. *
  669. * \param ptr the pointer
  670. * \return the byte value
  671. */
  672. uint8_t BytesGet8(const void *ptr)
  673. {
  674. const uint8_t *p = (const uint8_t *)ptr;
  675. return p[0];
  676. }
  677. /**
  678. * \brief put a byte (8bits) to a pointer
  679. *
  680. * Caller should ensure parameters are valid.
  681. *
  682. * \param ptr the pointer
  683. * \param v the byte value
  684. */
  685. void BytesPut8(void *ptr, uint8_t v)
  686. {
  687. uint8_t *p = (uint8_t *)ptr;
  688. p[0] = v;
  689. }
  690. /**
  691. * \brief get a big endian short (16bits) from a pointer
  692. *
  693. * Caller should ensure parameters are valid.
  694. *
  695. * \param ptr the pointer, may be unaligned
  696. * \return the short value
  697. */
  698. uint16_t BytesGetBe16(const void *ptr)
  699. {
  700. const uint8_t *p = (const uint8_t *)ptr;
  701. return (p[0] << 8) | p[1];
  702. }
  703. /**
  704. * \brief put a big endian short (16bits) to a pointer
  705. *
  706. * Caller should ensure parameters are valid.
  707. *
  708. * \param ptr the pointer, may be unaligned
  709. * \param v the short value
  710. */
  711. void BytesPutBe16(void *ptr, uint16_t v)
  712. {
  713. uint8_t *p = (uint8_t *)ptr;
  714. p[0] = (v >> 8) & 0xff;
  715. p[1] = v & 0xff;
  716. }
  717. /**
  718. * \brief get a big endian word (32bits) from a pointer
  719. *
  720. * Caller should ensure parameters are valid.
  721. *
  722. * \param ptr the pointer, may be unaligned
  723. * \return the word value
  724. */
  725. uint32_t BytesGetBe32(const void *ptr)
  726. {
  727. const uint8_t *p = (const uint8_t *)ptr;
  728. return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  729. }
  730. /**
  731. * \brief put a big endian word (32bits) to a pointer
  732. *
  733. * Caller should ensure parameters are valid.
  734. *
  735. * \param ptr the pointer, may be unaligned
  736. * \param v the word value
  737. */
  738. void BytesPutBe32(void *ptr, uint32_t v)
  739. {
  740. uint8_t *p = (uint8_t *)ptr;
  741. p[0] = (v >> 24) & 0xff;
  742. p[1] = (v >> 16) & 0xff;
  743. p[2] = (v >> 8) & 0xff;
  744. p[3] = v & 0xff;
  745. }
  746. /**
  747. * \brief get a little endian short (16bits) from a pointer
  748. *
  749. * Caller should ensure parameters are valid.
  750. *
  751. * \param ptr the pointer, may be unaligned
  752. * \return the short value
  753. */
  754. uint16_t BytesGetLe16(const void *ptr)
  755. {
  756. const uint8_t *p = (const uint8_t *)ptr;
  757. return p[0] | (p[1] << 8);
  758. }
  759. /**
  760. * \brief put a little endian short (16bits) to a pointer
  761. *
  762. * Caller should ensure parameters are valid.
  763. *
  764. * \param ptr the pointer, may be unaligned
  765. * \param v the short value
  766. */
  767. void BytesPutLe16(void *ptr, uint16_t v)
  768. {
  769. uint8_t *p = (uint8_t *)ptr;
  770. p[0] = v & 0xff;
  771. p[1] = (v >> 8) & 0xff;
  772. }
  773. /**
  774. * \brief get a little endian word (32bits) from a pointer
  775. *
  776. * Caller should ensure parameters are valid.
  777. *
  778. * \param ptr the pointer, may be unaligned
  779. * \return the word value
  780. */
  781. uint32_t BytesGetLe32(const void *ptr)
  782. {
  783. const uint8_t *p = (const uint8_t *)ptr;
  784. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  785. }
  786. /**
  787. * \brief put a little endian word (32bits) to a pointer
  788. *
  789. * Caller should ensure parameters are valid.
  790. *
  791. * \param ptr the pointer, may be unaligned
  792. * \param v the word value
  793. */
  794. void BytesPutLe32(void *ptr, uint32_t v)
  795. {
  796. uint8_t *p = (uint8_t *)ptr;
  797. p[0] = v & 0xff;
  798. p[1] = (v >> 8) & 0xff;
  799. p[2] = (v >> 16) & 0xff;
  800. p[3] = (v >> 24) & 0xff;
  801. }
  802. /**
  803. * \brief get a little endian long long (64bits) from a pointer
  804. *
  805. * Caller should ensure parameters are valid.
  806. *
  807. * \param ptr the pointer, may be unaligned
  808. * \return the long long value
  809. */
  810. uint64_t BytesGetLe64(const void *ptr)
  811. {
  812. const uint8_t *p = (const uint8_t *)ptr;
  813. return BytesGetLe32(p) | ((uint64_t)BytesGetLe32(p + 4) << 32);
  814. }
  815. /**
  816. * \brief put a little endian long long (64bits) to a pointer
  817. *
  818. * Caller should ensure parameters are valid.
  819. *
  820. * \param ptr the pointer, may be unaligned
  821. * \param v the long long value
  822. */
  823. void BytesPutLe64(void *ptr, uint64_t v)
  824. {
  825. uint8_t *p = (uint8_t *)ptr;
  826. BytesPutLe32(p, v & 0xffffffff);
  827. BytesPutLe32(p + 4, (v >> 32) & 0xffffffff);
  828. }
  829. uint8_t BytesGet8FromBuf(Buffer_Struct *Buf)
  830. {
  831. Buf->Pos++;
  832. return Buf->Data[Buf->Pos - 1];
  833. }
  834. void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v)
  835. {
  836. Buf->Data[Buf->Pos] = v;
  837. Buf->Pos++;
  838. }
  839. uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf)
  840. {
  841. Buf->Pos += 2;
  842. return (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
  843. }
  844. void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v)
  845. {
  846. Buf->Data[Buf->Pos] = (v >> 8) & 0xff;
  847. Buf->Data[Buf->Pos + 1] = v & 0xff;
  848. Buf->Pos += 2;
  849. }
  850. uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf)
  851. {
  852. Buf->Pos += 4;
  853. return (Buf->Data[Buf->Pos - 4] << 24) | (Buf->Data[Buf->Pos - 3] << 16) | (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
  854. }
  855. void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v)
  856. {
  857. Buf->Data[Buf->Pos] = (v >> 24) & 0xff;
  858. Buf->Data[Buf->Pos + 1] = (v >> 16) & 0xff;
  859. Buf->Data[Buf->Pos + 2] = (v >> 8) & 0xff;
  860. Buf->Data[Buf->Pos + 3] = v & 0xff;
  861. Buf->Pos += 4;
  862. }
  863. uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf)
  864. {
  865. Buf->Pos += 2;
  866. return Buf->Data[Buf->Pos - 2] | (Buf->Data[Buf->Pos - 1] << 8);
  867. }
  868. void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v)
  869. {
  870. Buf->Data[Buf->Pos] = v & 0xff;
  871. Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
  872. Buf->Pos+= 2;
  873. }
  874. uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf)
  875. {
  876. Buf->Pos += 4;
  877. return Buf->Data[Buf->Pos - 4] | (Buf->Data[Buf->Pos - 3] << 8) | (Buf->Data[Buf->Pos - 2] << 16) | (Buf->Data[Buf->Pos - 1] << 24);
  878. }
  879. void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v)
  880. {
  881. Buf->Data[Buf->Pos] = v & 0xff;
  882. Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
  883. Buf->Data[Buf->Pos + 2] = (v >> 16) & 0xff;
  884. Buf->Data[Buf->Pos + 3] = (v >> 24) & 0xff;
  885. Buf->Pos += 4;
  886. }
  887. uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf)
  888. {
  889. uint64_t Temp = BytesGetLe32FromBuf(Buf);
  890. return Temp | ((uint64_t)BytesGetLe32FromBuf(Buf) << 32);
  891. }
  892. void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v)
  893. {
  894. BytesPutLe32ToBuf(Buf, v & 0xffffffff);
  895. BytesPutLe32ToBuf(Buf, (v >> 32) & 0xffffffff);
  896. }
  897. float BytesGetFloatFromBuf(Buffer_Struct *Buf)
  898. {
  899. float Temp;
  900. Buf->Pos += 4;
  901. memcpy(&Temp, &Buf->Data[Buf->Pos - 4], 4);
  902. return Temp;
  903. }
  904. void BytesPutFloatToBuf(Buffer_Struct *Buf, float v)
  905. {
  906. memcpy(&Buf->Data[Buf->Pos], &v, 4);
  907. Buf->Pos += 4;
  908. }
  909. double BytesGetDoubleFromBuf(Buffer_Struct *Buf)
  910. {
  911. double Temp;
  912. Buf->Pos += 8;
  913. memcpy(&Temp, &Buf->Data[Buf->Pos - 8], 8);
  914. return Temp;
  915. }
  916. void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v)
  917. {
  918. memcpy(&Buf->Data[Buf->Pos], &v, 8);
  919. Buf->Pos += 8;
  920. }
  921. void BytesGetMemoryFromBuf(Buffer_Struct *Buf, uint8_t *Data, uint32_t Len)
  922. {
  923. memcpy(Data, &Buf->Data[Buf->Pos], Len);
  924. Buf->Pos += Len;
  925. }
  926. /*
  927. * 转义打包
  928. * 标识Flag,即包头包尾加入Flag
  929. * 数据中遇到Flag -> Code F1
  930. * 数据中遇到Code -> Code F2
  931. */
  932. uint32_t TransferPack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
  933. {
  934. uint32_t TxLen = 0;
  935. uint32_t i;
  936. OutBuf[0] = Flag;
  937. TxLen = 1;
  938. for (i = 0; i < Len; i++)
  939. {
  940. if (InBuf[i] == Flag)
  941. {
  942. OutBuf[TxLen++] = Code;
  943. OutBuf[TxLen++] = F1;
  944. }
  945. else if (InBuf[i] == Code)
  946. {
  947. OutBuf[TxLen++] = Code;
  948. OutBuf[TxLen++] = F2;
  949. }
  950. else
  951. {
  952. OutBuf[TxLen++] = InBuf[i];
  953. }
  954. }
  955. OutBuf[TxLen++] = Flag;
  956. return TxLen;
  957. }
  958. /*
  959. * 转义解包
  960. * 标识Flag,即包头包尾加入Flag
  961. * 数据中遇到Code F1 -> Flag
  962. * 数据中遇到Code F2 -> Code
  963. * 数据中遇到Flag 出错返回0
  964. */
  965. uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
  966. {
  967. uint32_t RxLen = 0;
  968. uint32_t i = 0;
  969. while (i < Len)
  970. {
  971. if (InBuf[i] == Code)
  972. {
  973. if (InBuf[i+1] == F1)
  974. {
  975. OutBuf[RxLen++] = Flag;
  976. }
  977. else if (InBuf[i+1] == F2)
  978. {
  979. OutBuf[RxLen++] = Code;
  980. }
  981. else
  982. {
  983. return 0;
  984. }
  985. i += 2;
  986. }
  987. else if (InBuf[i] == Flag)
  988. {
  989. return 0;
  990. }
  991. else
  992. {
  993. OutBuf[RxLen++] = InBuf[i++];
  994. }
  995. }
  996. return RxLen;
  997. }
  998. /*
  999. * Insert a new entry between two known consecutive entries.
  1000. *
  1001. * This is only for internal llist manipulation where we know
  1002. * the prev/next entries already!
  1003. */
  1004. void __llist_add(llist_head *p,
  1005. llist_head *prev,
  1006. llist_head *next)
  1007. {
  1008. next->prev = p;
  1009. p->next = next;
  1010. p->prev = prev;
  1011. prev->next = p;
  1012. }
  1013. /**
  1014. * llist_add - add a new entry
  1015. * @new: new entry to be added
  1016. * @head: llist head to add it after
  1017. *
  1018. * Insert a new entry after the specified head.
  1019. * This is good for implementing stacks.
  1020. */
  1021. void llist_add(llist_head *p, llist_head *head)
  1022. {
  1023. __llist_add(p, head, head->next);
  1024. }
  1025. /**
  1026. * llist_add_tail - add a new entry
  1027. * @new: new entry to be added
  1028. * @head: llist head to add it before
  1029. *
  1030. * Insert a new entry before the specified head.
  1031. * This is useful for implementing queues.
  1032. */
  1033. void llist_add_tail(llist_head *p, llist_head *head)
  1034. {
  1035. __llist_add(p, head->prev, head);
  1036. }
  1037. /*
  1038. * Delete a llist entry by making the prev/next entries
  1039. * point to each other.
  1040. *
  1041. * This is only for internal llist manipulation where we know
  1042. * the prev/next entries already!
  1043. */
  1044. void __llist_del(llist_head * prev, llist_head * next)
  1045. {
  1046. next->prev = prev;
  1047. prev->next = next;
  1048. }
  1049. /**
  1050. * llist_del - deletes entry from llist.
  1051. * @entry: the element to delete from the llist.
  1052. * Note: llist_empty on entry does not return true after this, the entry is
  1053. * in an undefined state.
  1054. */
  1055. void llist_del(llist_head *entry)
  1056. {
  1057. if (entry->prev && entry->next)
  1058. {
  1059. __llist_del(entry->prev, entry->next);
  1060. }
  1061. entry->next = LLIST_POISON1;
  1062. entry->prev = LLIST_POISON2;
  1063. }
  1064. /**
  1065. * llist_del_init - deletes entry from llist and reinitialize it.
  1066. * @entry: the element to delete from the llist.
  1067. */
  1068. void llist_del_init(llist_head *entry)
  1069. {
  1070. __llist_del(entry->prev, entry->next);
  1071. INIT_LLIST_HEAD(entry);
  1072. }
  1073. /**
  1074. * llist_move - delete from one llist and add as another's head
  1075. * @llist: the entry to move
  1076. * @head: the head that will precede our entry
  1077. */
  1078. void llist_move(llist_head *llist, llist_head *head)
  1079. {
  1080. __llist_del(llist->prev, llist->next);
  1081. llist_add(llist, head);
  1082. }
  1083. /**
  1084. * llist_move_tail - delete from one llist and add as another's tail
  1085. * @llist: the entry to move
  1086. * @head: the head that will follow our entry
  1087. */
  1088. void llist_move_tail(llist_head *llist,
  1089. llist_head *head)
  1090. {
  1091. __llist_del(llist->prev, llist->next);
  1092. llist_add_tail(llist, head);
  1093. }
  1094. void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData)
  1095. {
  1096. llist_head *node = head->next;
  1097. llist_head *del;
  1098. int32_t result;
  1099. while (!llist_empty(head) && (node != head))
  1100. {
  1101. result = cb((void *)node, pData);
  1102. if (result > 0)
  1103. {
  1104. return node;
  1105. }
  1106. else
  1107. {
  1108. del = node;
  1109. node = node->next;
  1110. if (result < 0)
  1111. {
  1112. llist_del(del);
  1113. cb((void *)del, NULL);
  1114. }
  1115. }
  1116. }
  1117. return NULL;
  1118. }
  1119. /**
  1120. * llist_empty - tests whether a llist is empty
  1121. * @head: the llist to test.
  1122. */
  1123. int llist_empty(const llist_head *head)
  1124. {
  1125. return head->next == head;
  1126. }
  1127. uint32_t llist_num(const llist_head *head)
  1128. {
  1129. llist_head *node = head->next;
  1130. uint32_t num = 0;
  1131. if (!node)
  1132. return num;
  1133. while(node != head)
  1134. {
  1135. num++;
  1136. node = node->next;
  1137. }
  1138. return num;
  1139. }