c_common.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /*
  2. * Copyright (c) 2022-2024 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 "c_common.h"
  22. #include "luat_mem.h"
  23. #include "luat_base.h"
  24. #include "luat_log.h"
  25. #define COMMON_LOG_BUF_SIZE 255
  26. static char common_log_buf[COMMON_LOG_BUF_SIZE];
  27. const uint8_t ByteToAsciiTable[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  28. LUAT_WEAK void DBG_Printf(const char* format, ...){
  29. va_list args;
  30. va_start(args, format);
  31. int len = vsnprintf(common_log_buf, sizeof(common_log_buf), format, args);
  32. luat_log_write(common_log_buf, len);
  33. va_end(args);
  34. }
  35. LUAT_WEAK void DBG_HexPrintf(void *Data, unsigned int len){
  36. uint8_t *data = (uint8_t *)Data;
  37. uint8_t *uart_buf;
  38. uint32_t i,j;
  39. j = 0;
  40. if (!len) return;
  41. uart_buf = luat_heap_zalloc(len * 3 + 2);
  42. if (!uart_buf) return;
  43. for (i = 0; i < len; i++){
  44. uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
  45. uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
  46. uart_buf[j++] = ' ';
  47. }
  48. uart_buf[j++] = '\r';
  49. uart_buf[j++] = '\n';
  50. luat_log_write((char*)uart_buf, len * 3 + 2);
  51. luat_heap_free(uart_buf);
  52. }
  53. void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize)
  54. {
  55. uint8_t *Data = (uint8_t *)Src;
  56. Buf->Data = Data;
  57. Buf->Len = 0;
  58. Buf->MaxLength = MaxLen;
  59. Buf->Offset = 0;
  60. Buf->DataSize = DataSize;
  61. }
  62. uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len)
  63. {
  64. uint32_t i, p;
  65. uint8_t *Data = (uint8_t *)Src;
  66. if (Buf->Len < Len)
  67. {
  68. Len = Buf->Len;
  69. }
  70. if (Buf->DataSize > 1)
  71. {
  72. for (i = 0, p = Buf->Offset; i < Len; i++, p++)
  73. {
  74. if (p >= Buf->MaxLength)
  75. {
  76. p -= Buf->MaxLength;
  77. }
  78. memcpy(Data + (i * Buf->DataSize), Buf->Data + (p * Buf->DataSize), Buf->DataSize);
  79. }
  80. }
  81. else
  82. {
  83. for (i = 0, p = Buf->Offset; i < Len; i++, p++)
  84. {
  85. if (p >= Buf->MaxLength)
  86. {
  87. p -= Buf->MaxLength;
  88. }
  89. Data[i] = Buf->Data[p];
  90. }
  91. }
  92. return Len;
  93. }
  94. uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len)
  95. {
  96. uint32_t l;
  97. uint8_t *Data = (uint8_t *)Src;
  98. l = LoopBuffer_Query(Buf, Data, Len);
  99. Buf->Len -= l;
  100. Buf->Offset += l;
  101. if (Buf->Offset >= Buf->MaxLength)
  102. {
  103. Buf->Offset -= Buf->MaxLength;
  104. }
  105. if (!Buf->Len) {
  106. Buf->Offset = 0;
  107. }
  108. return l;
  109. }
  110. void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len)
  111. {
  112. if (Buf->Len < Len)
  113. {
  114. Len = Buf->Len;
  115. }
  116. Buf->Len -= Len;
  117. Buf->Offset += Len;
  118. if (Buf->Offset >= Buf->MaxLength)
  119. {
  120. Buf->Offset -= Buf->MaxLength;
  121. }
  122. if (!Buf->Len) {
  123. Buf->Offset = 0;
  124. }
  125. }
  126. uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len)
  127. {
  128. uint32_t i, p, cut_off = 0;
  129. uint8_t *Data = (uint8_t *)Src;
  130. if (!Buf->Len && !Buf->Offset && (Len <= Buf->Len))
  131. {
  132. memcpy(Buf->Data, Data, Len);
  133. Buf->Len = Len;
  134. return Len;
  135. }
  136. cut_off = Buf->MaxLength - Buf->Len;
  137. if (cut_off >= Len)
  138. {
  139. cut_off = 0;
  140. }
  141. else
  142. {
  143. cut_off = Len - cut_off;
  144. }
  145. if (Buf->DataSize > 1)
  146. {
  147. for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
  148. {
  149. if (p >= Buf->MaxLength)
  150. {
  151. p -= Buf->MaxLength;
  152. }
  153. memcpy(Buf->Data + (p * Buf->DataSize), Data + (i * Buf->DataSize), Buf->DataSize);
  154. }
  155. }
  156. else
  157. {
  158. for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
  159. {
  160. if (p >= Buf->MaxLength)
  161. {
  162. p -= Buf->MaxLength;
  163. }
  164. Buf->Data[p] = Data[i];
  165. }
  166. }
  167. Buf->Offset += cut_off;
  168. if (Buf->Offset >= Buf->MaxLength)
  169. Buf->Offset -= Buf->MaxLength;
  170. Buf->Len += Len;
  171. if (Buf->Len > Buf->MaxLength)
  172. Buf->Len = Buf->MaxLength;
  173. return Len;
  174. }
  175. void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen)
  176. {
  177. Buf->Data = Src;
  178. Buf->Pos = 0;
  179. Buf->MaxLen = MaxLen;
  180. }
  181. int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
  182. {
  183. if (!Len)
  184. {
  185. return -1;
  186. }
  187. if (!Buf)
  188. {
  189. return -1;
  190. }
  191. if ((Buf->Pos + Len) > Buf->MaxLen)
  192. {
  193. Len = Buf->MaxLen - Buf->Pos;
  194. }
  195. if (Len)
  196. {
  197. memcpy(&Buf->Data[Buf->Pos], Data, Len);
  198. }
  199. Buf->Pos += Len;
  200. return Len;
  201. }
  202. void DBuffer_Init(DBuffer_Struct *DBuf, uint32_t Size)
  203. {
  204. memset(DBuf, 0, sizeof(DBuffer_Struct));
  205. DBuf->pCache[0] = malloc(Size);
  206. DBuf->pCache[1] = malloc(Size);
  207. DBuf->MaxLen = Size;
  208. }
  209. void DBuffer_ReInit(DBuffer_Struct *DBuf, uint32_t Size)
  210. {
  211. if (DBuf->pCache[0]) free(DBuf->pCache[0]);
  212. if (DBuf->pCache[1]) free(DBuf->pCache[1]);
  213. DBuffer_Init(DBuf, Size);
  214. }
  215. void DBuffer_DeInit(DBuffer_Struct *DBuf)
  216. {
  217. free(DBuf->pCache[0]);
  218. free(DBuf->pCache[1]);
  219. DBuf->pCache[0] = NULL;
  220. DBuf->pCache[1] = NULL;
  221. DBuf->MaxLen = 0;
  222. }
  223. void *DBuffer_GetCache(DBuffer_Struct *DBuf, uint8_t IsCurrent)
  224. {
  225. return DBuf->pCache[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn];
  226. }
  227. void DBuffer_SwapCache(DBuffer_Struct *DBuf)
  228. {
  229. DBuf->CurCacheSn = !DBuf->CurCacheSn;
  230. }
  231. void DBuffer_SetDataLen(DBuffer_Struct *DBuf, uint32_t Len, uint8_t IsCurrent)
  232. {
  233. DBuf->pCacheLen[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn] = Len;
  234. }
  235. uint32_t DBuffer_GetDataLen(DBuffer_Struct *DBuf, uint8_t IsCurrent)
  236. {
  237. return DBuf->pCacheLen[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn];
  238. }
  239. //void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len)
  240. //{
  241. // uint32_t RestLen;
  242. // uint32_t i;
  243. // if (!Buf)
  244. // return ;
  245. // if (!Buf->Data)
  246. // return ;
  247. // if (Len >= Buf->Pos)
  248. // {
  249. // Buf->Pos = 0;
  250. // return ;
  251. // }
  252. // RestLen = Buf->Pos - Len;
  253. // memmove(Buf->Data, Buf->Data + Len, RestLen);
  254. // Buf->Pos = RestLen;
  255. //}
  256. /*****************************************************************************
  257. * FUNCTION
  258. * command_parse_param()
  259. * DESCRIPTION
  260. * Parse AT command string to parameters
  261. * PARAMETERS
  262. * char* pStr
  263. * RETURNS
  264. * pCmdParam
  265. *****************************************************************************/
  266. uint32_t CmdParseParam(int8_t* pStr, CmdParam *CP, int8_t Cut)
  267. {
  268. uint32_t paramStrLen = strlen((char *)pStr);
  269. uint32_t paramIndex = 0;
  270. uint32_t paramCharIndex = 0;
  271. uint32_t index = 0;
  272. while ((pStr[index] != '\r')
  273. && (index < paramStrLen)
  274. && (paramIndex < CP->param_max_num)) {
  275. if (pStr[index] == Cut) {
  276. /* Next param string */
  277. paramCharIndex = 0;
  278. paramIndex++;
  279. }
  280. else {
  281. if (pStr[index] != '"')
  282. {
  283. if (paramCharIndex >= CP->param_max_len)
  284. return (0);
  285. /*Get each of command param char, the param char except char ' " '*/
  286. CP->param_str[paramIndex * CP->param_max_len + paramCharIndex] = pStr[index];
  287. paramCharIndex++;
  288. }
  289. }
  290. index++;
  291. }
  292. CP->param_num = paramIndex + 1;
  293. return (1);
  294. }
  295. LUAT_WEAK int32_t OS_InitBuffer(Buffer_Struct *Buf, uint32_t Size)
  296. {
  297. if (!Buf)
  298. return 0;
  299. Buf->Data = luat_heap_malloc(Size);
  300. memset(Buf->Data, 0, Size);
  301. if (!Buf->Data)
  302. {
  303. Buf->MaxLen = 0;
  304. Buf->Pos = 0;
  305. return 0;
  306. }
  307. Buf->MaxLen = Size;
  308. Buf->Pos = 0;
  309. return Size;
  310. }
  311. LUAT_WEAK void OS_DeInitBuffer(Buffer_Struct *Buf)
  312. {
  313. if (Buf->Data)
  314. {
  315. luat_heap_free(Buf->Data);
  316. }
  317. Buf->Data = NULL;
  318. Buf->MaxLen = 0;
  319. Buf->Pos = 0;
  320. }
  321. LUAT_WEAK int32_t OS_ReInitBuffer(Buffer_Struct *Buf, uint32_t Size)
  322. {
  323. if (!Buf)
  324. return 0;
  325. if (Buf->Data)
  326. {
  327. luat_heap_free(Buf->Data);
  328. }
  329. Buf->Data = luat_heap_malloc(Size);
  330. memset(Buf->Data, 0, Size);
  331. if (!Buf->Data)
  332. {
  333. Buf->MaxLen = 0;
  334. Buf->Pos = 0;
  335. return 0;
  336. }
  337. Buf->MaxLen = Size;
  338. Buf->Pos = 0;
  339. return Size;
  340. }
  341. LUAT_WEAK int32_t OS_ReSizeBuffer(Buffer_Struct *Buf, uint32_t Size)
  342. {
  343. // uint8_t *Old;
  344. uint8_t *New;
  345. if (!Buf)
  346. return 0;
  347. // Old = Buf->Data;
  348. // if (Size < Buf->Pos)
  349. // {
  350. // Size = Buf->Pos;
  351. // }
  352. // New = OS_Zalloc(Size);
  353. // if (!New)
  354. // {
  355. // return 0;
  356. // }
  357. // if (Old)
  358. // {
  359. // memcpy(New, Old, Buf->Pos);
  360. // free(Old);
  361. // }
  362. New = luat_heap_realloc(Buf->Data, Size);
  363. if (New)
  364. {
  365. Buf->Data = New;
  366. Buf->MaxLen = Size;
  367. }
  368. return Size;
  369. }
  370. LUAT_WEAK int32_t OS_BufferWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
  371. {
  372. uint32_t WriteLen;
  373. if (!Len)
  374. {
  375. return ERROR_NONE;
  376. }
  377. if (!Buf)
  378. {
  379. return -ERROR_PARAM_INVALID;
  380. }
  381. if (!Buf->Data)
  382. {
  383. Buf->Data = luat_heap_malloc(Len);
  384. memset(Buf->Data, 0, Len);
  385. if (!Buf->Data)
  386. {
  387. return -ERROR_NO_MEMORY;
  388. }
  389. Buf->Pos = 0;
  390. Buf->MaxLen = Len;
  391. }
  392. WriteLen = Buf->Pos + Len;
  393. if (WriteLen > Buf->MaxLen)
  394. {
  395. if (!OS_ReSizeBuffer(Buf, WriteLen))
  396. {
  397. return -ERROR_NO_MEMORY;
  398. }
  399. }
  400. memcpy(&Buf->Data[Buf->Pos], Data, Len);
  401. Buf->Pos += Len;
  402. return ERROR_NONE;
  403. }
  404. LUAT_WEAK int32_t OS_BufferWriteLimit(Buffer_Struct *Buf, void *Data, uint32_t Len)
  405. {
  406. uint32_t WriteLen;
  407. if (!Len)
  408. {
  409. return ERROR_NONE;
  410. }
  411. if (!Buf)
  412. {
  413. return -ERROR_PARAM_INVALID;
  414. }
  415. if (!Buf->Data)
  416. {
  417. Buf->Data = luat_heap_malloc(Len);
  418. memset(Buf->Data, 0, Len);
  419. if (!Buf->Data)
  420. {
  421. return -ERROR_NO_MEMORY;
  422. }
  423. Buf->Pos = 0;
  424. Buf->MaxLen = Len;
  425. }
  426. WriteLen = Buf->Pos + Len;
  427. if (WriteLen > Buf->MaxLen)
  428. {
  429. return -ERROR_NO_MEMORY;
  430. }
  431. memcpy(&Buf->Data[Buf->Pos], Data, Len);
  432. Buf->Pos += Len;
  433. return ERROR_NONE;
  434. }
  435. LUAT_WEAK void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len)
  436. {
  437. uint32_t RestLen;
  438. // uint32_t i;
  439. if (!Buf)
  440. return ;
  441. if (!Buf->Data)
  442. return ;
  443. if (Len >= Buf->Pos)
  444. {
  445. Buf->Pos = 0;
  446. return ;
  447. }
  448. RestLen = Buf->Pos - Len;
  449. memmove(Buf->Data, Buf->Data + Len, RestLen);
  450. Buf->Pos = RestLen;
  451. }
  452. int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value)
  453. {
  454. uint32_t Mask,Pos1,Pos2;
  455. Pos1 = Sn/8;
  456. Pos2 = Sn%8;
  457. Mask = ~(1 << Pos2);
  458. if (Value)
  459. {
  460. Value = (1 << Pos2);
  461. }
  462. Data[Pos1] = (Data[Pos1] & Mask) | Value;
  463. //DBG("%d %d %d %d", Sn, Pos1, Pos2, Value);
  464. return 0;
  465. }
  466. int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value)
  467. {
  468. uint32_t Mask,Pos1,Pos2;
  469. Pos1 = Sn/8;
  470. Pos2 = Sn%8;
  471. Mask = (1 << Pos2);
  472. if (Data[Pos1] & Mask)
  473. {
  474. *Value = 1;
  475. }
  476. else
  477. {
  478. *Value = 0;
  479. }
  480. return -1;
  481. }
  482. uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn)
  483. {
  484. uint32_t Mask,Pos1,Pos2;
  485. Pos1 = Sn/8;
  486. Pos2 = Sn%8;
  487. Mask = (1 << Pos2);
  488. if (Data[Pos1] & Mask)
  489. {
  490. return 1;
  491. }
  492. return 0;
  493. }
  494. uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart)
  495. {
  496. uint8_t Check = CheckStart;
  497. uint8_t *Data = (uint8_t *)Src;
  498. uint32_t i;
  499. for (i = 0; i < Len; i++)
  500. {
  501. Check ^= Data[i];
  502. }
  503. return Check;
  504. }
  505. uint8_t SumCheck(uint8_t *Data, uint32_t Len)
  506. {
  507. uint8_t Check = 0;
  508. uint32_t i;
  509. for (i = 0; i < Len; i++)
  510. {
  511. Check += Data[i];
  512. }
  513. return Check;
  514. }
  515. uint8_t CRC8Cal(void *Data, uint16_t Len, uint8_t CRC8Last, uint8_t CRCRoot, uint8_t IsReverse)
  516. {
  517. uint16_t i;
  518. uint8_t CRC8 = CRC8Last;
  519. uint8_t wTemp = CRCRoot;
  520. uint8_t *Src = (uint8_t *)Data;
  521. if (IsReverse)
  522. {
  523. CRCRoot = 0;
  524. for (i = 0; i < 8; i++)
  525. {
  526. if (wTemp & (1 << (7 - i)))
  527. {
  528. CRCRoot |= 1 << i;
  529. }
  530. }
  531. while (Len--)
  532. {
  533. CRC8 ^= *Src++;
  534. for (i = 0; i < 8; i++)
  535. {
  536. if ((CRC8 & 0x01))
  537. {
  538. CRC8 >>= 1;
  539. CRC8 ^= CRCRoot;
  540. }
  541. else
  542. {
  543. CRC8 >>= 1;
  544. }
  545. }
  546. }
  547. }
  548. else
  549. {
  550. while (Len--)
  551. {
  552. CRC8 ^= *Src++;
  553. for (i = 8; i > 0; --i)
  554. {
  555. if ((CRC8 & 0x80))
  556. {
  557. CRC8 <<= 1;
  558. CRC8 ^= CRCRoot;
  559. }
  560. else
  561. {
  562. CRC8 <<= 1;
  563. }
  564. }
  565. }
  566. }
  567. return CRC8;
  568. }
  569. /************************************************************************/
  570. /* CRC16 */
  571. /************************************************************************/
  572. uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse)
  573. {
  574. uint16_t i;
  575. uint16_t CRC16 = CRC16Last;
  576. uint16_t wTemp = CRCRoot;
  577. uint8_t *Src = (uint8_t *)Data;
  578. if (IsReverse)
  579. {
  580. CRCRoot = 0;
  581. for (i = 0; i < 16; i++)
  582. {
  583. if (wTemp & (1 << (15 - i)))
  584. {
  585. CRCRoot |= 1 << i;
  586. }
  587. }
  588. while (Len--)
  589. {
  590. for (i = 0; i < 8; i++)
  591. {
  592. if ((CRC16 & 0x0001) != 0)
  593. {
  594. CRC16 >>= 1;
  595. CRC16 ^= CRCRoot;
  596. }
  597. else
  598. {
  599. CRC16 >>= 1;
  600. }
  601. if ((*Src&(1 << i)) != 0)
  602. {
  603. CRC16 ^= CRCRoot;
  604. }
  605. }
  606. Src++;
  607. }
  608. }
  609. else
  610. {
  611. while (Len--)
  612. {
  613. for (i = 8; i > 0; i--)
  614. {
  615. if ((CRC16 & 0x8000) != 0)
  616. {
  617. CRC16 <<= 1;
  618. CRC16 ^= CRCRoot;
  619. }
  620. else
  621. {
  622. CRC16 <<= 1;
  623. }
  624. if ((*Src&(1 << (i - 1))) != 0)
  625. {
  626. CRC16 ^= CRCRoot;
  627. }
  628. }
  629. Src++;
  630. }
  631. }
  632. return CRC16;
  633. }
  634. uint32_t AsciiToU32(uint8_t *Src, uint32_t Len)
  635. {
  636. uint32_t i = 0;
  637. uint32_t Temp = 0;
  638. for (i = 0; i < Len; i++)
  639. {
  640. if (Src[i])
  641. {
  642. Temp *= 10;
  643. Temp += Src[i] - '0';
  644. }
  645. else
  646. {
  647. break;
  648. }
  649. }
  650. return Temp;
  651. }
  652. /**
  653. * @brief 反转数据
  654. * @param ref 需要反转的变量
  655. * @param ch 反转长度,多少位
  656. * @retval N反转后的数据
  657. */
  658. static LongInt Reflect(LongInt ref, uint8_t ch)
  659. {
  660. LongInt value = 0;
  661. LongInt i;
  662. for (i = 1; i < (LongInt)(ch + 1); i++)
  663. {
  664. if (ref & 1)
  665. value |= (LongInt)1 << (ch - i);
  666. ref >>= 1;
  667. }
  668. return value;
  669. }
  670. /**
  671. * @brief 建立CRC32的查询表
  672. * @param Tab 表缓冲
  673. * @param Gen CRC32根
  674. * @retval None
  675. */
  676. void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen)
  677. {
  678. uint32_t crc;
  679. uint32_t i, j, temp, t1, t2, flag;
  680. if (Tab[1] != 0)
  681. return;
  682. for (i = 0; i < 256; i++)
  683. {
  684. temp = Reflect(i, 8);
  685. Tab[i] = temp << 24;
  686. for (j = 0; j < 8; j++)
  687. {
  688. flag = Tab[i] & 0x80000000;
  689. t1 = Tab[i] << 1;
  690. if (0 == flag)
  691. {
  692. t2 = 0;
  693. }
  694. else
  695. {
  696. t2 = Gen;
  697. }
  698. Tab[i] = t1 ^ t2;
  699. }
  700. crc = Tab[i];
  701. Tab[i] = Reflect(crc, 32);
  702. }
  703. }
  704. /**
  705. * @brief 计算buffer的crc校验码
  706. * @param CRC32_Table CRC32表
  707. * @param Buf 缓冲
  708. * @param Size 缓冲区长度
  709. * @param CRC32 初始CRC32值
  710. * @retval 计算后的CRC32
  711. */
  712. uint32_t CRC32_Cal(uint32_t *CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last)
  713. {
  714. uint32_t i;
  715. for (i = 0; i < Size; i++)
  716. {
  717. CRC32Last = CRC32_Table[(CRC32Last ^ Buf[i]) & 0xff] ^ (CRC32Last >> 8);
  718. }
  719. return CRC32Last;
  720. }
  721. /************************************************************************/
  722. /*时间与时间戳转换,C语言实现 */
  723. /************************************************************************/
  724. /************************************************************************/
  725. uint8_t IsLeapYear(uint32_t Year)
  726. {
  727. if ((Year % 400) == 0)
  728. return 1;
  729. if ((((Year % 4) == 0) && (Year % 100) != 0))
  730. return 1;
  731. else
  732. return 0;
  733. }
  734. 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 } };
  735. //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 } };
  736. LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time)
  737. {
  738. LongInt DYear, DDay, DSec;
  739. uint32_t Year100;
  740. DYear = Date->Year - 1970;
  741. if (DYear) //1970年以后,1972是第一个闰年,1973年是第一年需要增加一天,2100年是非闰年
  742. {
  743. DDay = DYear * 365 + ((DYear + 1) / 4) + DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
  744. // if (IsLeapYear(Date->Year))
  745. // {
  746. // DDay--;
  747. // }
  748. if (Date->Year >= 2100)
  749. {
  750. Year100 = Date->Year - 2100;
  751. DDay -= (1 + Year100 / 100);
  752. if (Date->Year >= 2400)
  753. {
  754. Year100 = Date->Year - 2400;
  755. DDay += 1 + Year100 / 400;
  756. }
  757. }
  758. }
  759. else
  760. {
  761. DDay = DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
  762. }
  763. DSec = DDay * 86400 + Time->Hour * 3600 + Time->Min * 60 + Time->Sec;
  764. return DSec;
  765. }
  766. #define YEAR_1_DAY_BEFORE2000 365
  767. #define YEAR_2_DAY_BEFORE2000 730
  768. #define YEAR_3_DAY_BEFORE2000 1096
  769. #define YEAR_1_DAY_AFTER2000 365
  770. #define YEAR_2_DAY_AFTER2000 730
  771. #define YEAR_3_DAY_AFTER2000 1095
  772. #define YEAR_4_DAY 1461
  773. #define YEAR_31_DAY 11323
  774. #define YEAR_100_DAY 36524
  775. #define YEAR_400_DAY 146097
  776. uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay)
  777. {
  778. uint32_t DYear,i, LeapFlag, Temp;
  779. uint32_t DDay;
  780. DDay = Sec / 86400;
  781. if (DDay != LastDDay)
  782. {
  783. DYear = 0;
  784. Time->Week = (4 + DDay) % 7;
  785. if (DDay >= YEAR_31_DAY)
  786. {
  787. DDay -= YEAR_31_DAY;
  788. DYear = 31;
  789. if (DDay >= YEAR_400_DAY)
  790. {
  791. Temp = DDay / YEAR_400_DAY;
  792. DYear += Temp * 400;
  793. DDay -= Temp * YEAR_400_DAY;
  794. }
  795. if (DDay >= YEAR_100_DAY)
  796. {
  797. Temp = DDay / YEAR_100_DAY;
  798. DYear += Temp * 100;
  799. DDay -= Temp * YEAR_100_DAY;
  800. }
  801. if (DDay >= YEAR_4_DAY)
  802. {
  803. Temp = DDay / YEAR_4_DAY;
  804. DYear += Temp * 4;
  805. DDay -= Temp * YEAR_4_DAY;
  806. }
  807. if (DDay >= YEAR_3_DAY_AFTER2000)
  808. {
  809. DYear += 3;
  810. DDay -= YEAR_3_DAY_AFTER2000;
  811. }
  812. else if (DDay >= YEAR_2_DAY_AFTER2000)
  813. {
  814. DYear += 2;
  815. DDay -= YEAR_2_DAY_AFTER2000;
  816. }
  817. else if (DDay >= YEAR_1_DAY_AFTER2000)
  818. {
  819. DYear += 1;
  820. DDay -= YEAR_1_DAY_AFTER2000;
  821. }
  822. }
  823. else
  824. {
  825. if (DDay >= YEAR_4_DAY)
  826. {
  827. Temp = DDay / YEAR_4_DAY;
  828. DYear += Temp * 4;
  829. DDay -= Temp * YEAR_4_DAY;
  830. }
  831. if (DDay >= YEAR_3_DAY_BEFORE2000)
  832. {
  833. DYear += 3;
  834. DDay -= YEAR_3_DAY_BEFORE2000;
  835. }
  836. else if (DDay >= YEAR_2_DAY_BEFORE2000)
  837. {
  838. DYear += 2;
  839. DDay -= YEAR_2_DAY_BEFORE2000;
  840. }
  841. else if (DDay >= YEAR_1_DAY_BEFORE2000)
  842. {
  843. DYear += 1;
  844. DDay -= YEAR_1_DAY_BEFORE2000;
  845. }
  846. }
  847. Date->Year = DYear + 1970;
  848. LeapFlag = IsLeapYear(Date->Year);
  849. Date->Mon = 12;
  850. for (i = 1; i < 12; i++)
  851. {
  852. if (DDay < DayTable[LeapFlag][i])
  853. {
  854. Date->Mon = i;
  855. break;
  856. }
  857. }
  858. Date->Day = DDay - DayTable[LeapFlag][Date->Mon - 1] + 1;
  859. }
  860. Sec = Sec % 86400;
  861. Time->Hour = Sec / 3600;
  862. Sec = Sec % 3600;
  863. Time->Min = Sec / 60;
  864. Time->Sec = Sec % 60;
  865. return DDay;
  866. }
  867. /**
  868. * \brief get a byte (8bits) from a pointer
  869. *
  870. * Caller should ensure parameters are valid.
  871. *
  872. * \param ptr the pointer
  873. * \return the byte value
  874. */
  875. uint8_t BytesGet8(const void *ptr)
  876. {
  877. const uint8_t *p = (const uint8_t *)ptr;
  878. return p[0];
  879. }
  880. /**
  881. * \brief put a byte (8bits) to a pointer
  882. *
  883. * Caller should ensure parameters are valid.
  884. *
  885. * \param ptr the pointer
  886. * \param v the byte value
  887. */
  888. void BytesPut8(void *ptr, uint8_t v)
  889. {
  890. uint8_t *p = (uint8_t *)ptr;
  891. p[0] = v;
  892. }
  893. /**
  894. * \brief get a big endian short (16bits) from a pointer
  895. *
  896. * Caller should ensure parameters are valid.
  897. *
  898. * \param ptr the pointer, may be unaligned
  899. * \return the short value
  900. */
  901. uint16_t BytesGetBe16(const void *ptr)
  902. {
  903. const uint8_t *p = (const uint8_t *)ptr;
  904. return (p[0] << 8) | p[1];
  905. }
  906. /**
  907. * \brief put a big endian short (16bits) to a pointer
  908. *
  909. * Caller should ensure parameters are valid.
  910. *
  911. * \param ptr the pointer, may be unaligned
  912. * \param v the short value
  913. */
  914. void BytesPutBe16(void *ptr, uint16_t v)
  915. {
  916. uint8_t *p = (uint8_t *)ptr;
  917. p[0] = (v >> 8) & 0xff;
  918. p[1] = v & 0xff;
  919. }
  920. /**
  921. * \brief get a big endian word (32bits) from a pointer
  922. *
  923. * Caller should ensure parameters are valid.
  924. *
  925. * \param ptr the pointer, may be unaligned
  926. * \return the word value
  927. */
  928. uint32_t BytesGetBe32(const void *ptr)
  929. {
  930. const uint8_t *p = (const uint8_t *)ptr;
  931. return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  932. }
  933. /**
  934. * \brief put a big endian word (32bits) to a pointer
  935. *
  936. * Caller should ensure parameters are valid.
  937. *
  938. * \param ptr the pointer, may be unaligned
  939. * \param v the word value
  940. */
  941. void BytesPutBe32(void *ptr, uint32_t v)
  942. {
  943. uint8_t *p = (uint8_t *)ptr;
  944. p[0] = (v >> 24) & 0xff;
  945. p[1] = (v >> 16) & 0xff;
  946. p[2] = (v >> 8) & 0xff;
  947. p[3] = v & 0xff;
  948. }
  949. /**
  950. * \brief get a little endian short (16bits) from a pointer
  951. *
  952. * Caller should ensure parameters are valid.
  953. *
  954. * \param ptr the pointer, may be unaligned
  955. * \return the short value
  956. */
  957. uint16_t BytesGetLe16(const void *ptr)
  958. {
  959. const uint8_t *p = (const uint8_t *)ptr;
  960. return p[0] | (p[1] << 8);
  961. }
  962. /**
  963. * \brief put a little endian short (16bits) to a pointer
  964. *
  965. * Caller should ensure parameters are valid.
  966. *
  967. * \param ptr the pointer, may be unaligned
  968. * \param v the short value
  969. */
  970. void BytesPutLe16(void *ptr, uint16_t v)
  971. {
  972. uint8_t *p = (uint8_t *)ptr;
  973. p[0] = v & 0xff;
  974. p[1] = (v >> 8) & 0xff;
  975. }
  976. /**
  977. * \brief get a little endian word (32bits) from a pointer
  978. *
  979. * Caller should ensure parameters are valid.
  980. *
  981. * \param ptr the pointer, may be unaligned
  982. * \return the word value
  983. */
  984. uint32_t BytesGetLe32(const void *ptr)
  985. {
  986. const uint8_t *p = (const uint8_t *)ptr;
  987. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  988. }
  989. /**
  990. * \brief put a little endian word (32bits) to a pointer
  991. *
  992. * Caller should ensure parameters are valid.
  993. *
  994. * \param ptr the pointer, may be unaligned
  995. * \param v the word value
  996. */
  997. void BytesPutLe32(void *ptr, uint32_t v)
  998. {
  999. uint8_t *p = (uint8_t *)ptr;
  1000. p[0] = v & 0xff;
  1001. p[1] = (v >> 8) & 0xff;
  1002. p[2] = (v >> 16) & 0xff;
  1003. p[3] = (v >> 24) & 0xff;
  1004. }
  1005. /**
  1006. * \brief get a little endian long long (64bits) from a pointer
  1007. *
  1008. * Caller should ensure parameters are valid.
  1009. *
  1010. * \param ptr the pointer, may be unaligned
  1011. * \return the long long value
  1012. */
  1013. uint64_t BytesGetLe64(const void *ptr)
  1014. {
  1015. const uint8_t *p = (const uint8_t *)ptr;
  1016. return BytesGetLe32(p) | ((uint64_t)BytesGetLe32(p + 4) << 32);
  1017. }
  1018. /**
  1019. * \brief put a little endian long long (64bits) to a pointer
  1020. *
  1021. * Caller should ensure parameters are valid.
  1022. *
  1023. * \param ptr the pointer, may be unaligned
  1024. * \param v the long long value
  1025. */
  1026. void BytesPutLe64(void *ptr, uint64_t v)
  1027. {
  1028. uint8_t *p = (uint8_t *)ptr;
  1029. BytesPutLe32(p, v & 0xffffffff);
  1030. BytesPutLe32(p + 4, (v >> 32) & 0xffffffff);
  1031. }
  1032. uint8_t BytesGet8FromBuf(Buffer_Struct *Buf)
  1033. {
  1034. Buf->Pos++;
  1035. return Buf->Data[Buf->Pos - 1];
  1036. }
  1037. void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v)
  1038. {
  1039. Buf->Data[Buf->Pos] = v;
  1040. Buf->Pos++;
  1041. }
  1042. uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf)
  1043. {
  1044. Buf->Pos += 2;
  1045. return (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
  1046. }
  1047. void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v)
  1048. {
  1049. Buf->Data[Buf->Pos] = (v >> 8) & 0xff;
  1050. Buf->Data[Buf->Pos + 1] = v & 0xff;
  1051. Buf->Pos += 2;
  1052. }
  1053. uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf)
  1054. {
  1055. Buf->Pos += 4;
  1056. return (Buf->Data[Buf->Pos - 4] << 24) | (Buf->Data[Buf->Pos - 3] << 16) | (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
  1057. }
  1058. void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v)
  1059. {
  1060. Buf->Data[Buf->Pos] = (v >> 24) & 0xff;
  1061. Buf->Data[Buf->Pos + 1] = (v >> 16) & 0xff;
  1062. Buf->Data[Buf->Pos + 2] = (v >> 8) & 0xff;
  1063. Buf->Data[Buf->Pos + 3] = v & 0xff;
  1064. Buf->Pos += 4;
  1065. }
  1066. uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf)
  1067. {
  1068. Buf->Pos += 2;
  1069. return Buf->Data[Buf->Pos - 2] | (Buf->Data[Buf->Pos - 1] << 8);
  1070. }
  1071. void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v)
  1072. {
  1073. Buf->Data[Buf->Pos] = v & 0xff;
  1074. Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
  1075. Buf->Pos+= 2;
  1076. }
  1077. uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf)
  1078. {
  1079. Buf->Pos += 4;
  1080. return Buf->Data[Buf->Pos - 4] | (Buf->Data[Buf->Pos - 3] << 8) | (Buf->Data[Buf->Pos - 2] << 16) | (Buf->Data[Buf->Pos - 1] << 24);
  1081. }
  1082. void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v)
  1083. {
  1084. Buf->Data[Buf->Pos] = v & 0xff;
  1085. Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
  1086. Buf->Data[Buf->Pos + 2] = (v >> 16) & 0xff;
  1087. Buf->Data[Buf->Pos + 3] = (v >> 24) & 0xff;
  1088. Buf->Pos += 4;
  1089. }
  1090. uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf)
  1091. {
  1092. uint64_t Temp = BytesGetLe32FromBuf(Buf);
  1093. return Temp | ((uint64_t)BytesGetLe32FromBuf(Buf) << 32);
  1094. }
  1095. void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v)
  1096. {
  1097. BytesPutLe32ToBuf(Buf, v & 0xffffffff);
  1098. BytesPutLe32ToBuf(Buf, (v >> 32) & 0xffffffff);
  1099. }
  1100. float BytesGetFloatFromBuf(Buffer_Struct *Buf)
  1101. {
  1102. float Temp;
  1103. Buf->Pos += 4;
  1104. memcpy(&Temp, &Buf->Data[Buf->Pos - 4], 4);
  1105. return Temp;
  1106. }
  1107. void BytesPutFloatToBuf(Buffer_Struct *Buf, float v)
  1108. {
  1109. memcpy(&Buf->Data[Buf->Pos], &v, 4);
  1110. Buf->Pos += 4;
  1111. }
  1112. double BytesGetDoubleFromBuf(Buffer_Struct *Buf)
  1113. {
  1114. double Temp;
  1115. Buf->Pos += 8;
  1116. memcpy(&Temp, &Buf->Data[Buf->Pos - 8], 8);
  1117. return Temp;
  1118. }
  1119. void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v)
  1120. {
  1121. memcpy(&Buf->Data[Buf->Pos], &v, 8);
  1122. Buf->Pos += 8;
  1123. }
  1124. void BytesGetMemoryFromBuf(Buffer_Struct *Buf, uint8_t *Data, uint32_t Len)
  1125. {
  1126. memcpy(Data, &Buf->Data[Buf->Pos], Len);
  1127. Buf->Pos += Len;
  1128. }
  1129. /*
  1130. * 转义打包
  1131. * 标识Flag,即包头包尾加入Flag
  1132. * 数据中遇到Flag -> Code F1
  1133. * 数据中遇到Code -> Code F2
  1134. */
  1135. uint32_t TransferPack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
  1136. {
  1137. uint32_t TxLen = 0;
  1138. uint32_t i;
  1139. OutBuf[0] = Flag;
  1140. TxLen = 1;
  1141. for (i = 0; i < Len; i++)
  1142. {
  1143. if (InBuf[i] == Flag)
  1144. {
  1145. OutBuf[TxLen++] = Code;
  1146. OutBuf[TxLen++] = F1;
  1147. }
  1148. else if (InBuf[i] == Code)
  1149. {
  1150. OutBuf[TxLen++] = Code;
  1151. OutBuf[TxLen++] = F2;
  1152. }
  1153. else
  1154. {
  1155. OutBuf[TxLen++] = InBuf[i];
  1156. }
  1157. }
  1158. OutBuf[TxLen++] = Flag;
  1159. return TxLen;
  1160. }
  1161. /*
  1162. * 转义解包
  1163. * 标识Flag,即包头包尾加入Flag
  1164. * 数据中遇到Code F1 -> Flag
  1165. * 数据中遇到Code F2 -> Code
  1166. * 数据中遇到Flag 出错返回0
  1167. */
  1168. uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
  1169. {
  1170. uint32_t RxLen = 0;
  1171. uint32_t i = 0;
  1172. while (i < Len)
  1173. {
  1174. if (InBuf[i] == Code)
  1175. {
  1176. if (InBuf[i+1] == F1)
  1177. {
  1178. OutBuf[RxLen++] = Flag;
  1179. }
  1180. else if (InBuf[i+1] == F2)
  1181. {
  1182. OutBuf[RxLen++] = Code;
  1183. }
  1184. else
  1185. {
  1186. return 0;
  1187. }
  1188. i += 2;
  1189. }
  1190. else if (InBuf[i] == Flag)
  1191. {
  1192. return 0;
  1193. }
  1194. else
  1195. {
  1196. OutBuf[RxLen++] = InBuf[i++];
  1197. }
  1198. }
  1199. return RxLen;
  1200. }
  1201. /*
  1202. * Insert a new entry between two known consecutive entries.
  1203. *
  1204. * This is only for internal llist manipulation where we know
  1205. * the prev/next entries already!
  1206. */
  1207. void __llist_add(llist_head *p,
  1208. llist_head *prev,
  1209. llist_head *next)
  1210. {
  1211. next->prev = p;
  1212. p->next = next;
  1213. p->prev = prev;
  1214. prev->next = p;
  1215. }
  1216. /**
  1217. * llist_add - add a new entry
  1218. * @new: new entry to be added
  1219. * @head: llist head to add it after
  1220. *
  1221. * Insert a new entry after the specified head.
  1222. * This is good for implementing stacks.
  1223. */
  1224. void llist_add(llist_head *p, llist_head *head)
  1225. {
  1226. __llist_add(p, head, head->next);
  1227. }
  1228. /**
  1229. * llist_add_tail - add a new entry
  1230. * @new: new entry to be added
  1231. * @head: llist head to add it before
  1232. *
  1233. * Insert a new entry before the specified head.
  1234. * This is useful for implementing queues.
  1235. */
  1236. void llist_add_tail(llist_head *p, llist_head *head)
  1237. {
  1238. __llist_add(p, head->prev, head);
  1239. }
  1240. /*
  1241. * Delete a llist entry by making the prev/next entries
  1242. * point to each other.
  1243. *
  1244. * This is only for internal llist manipulation where we know
  1245. * the prev/next entries already!
  1246. */
  1247. void __llist_del(llist_head * prev, llist_head * next)
  1248. {
  1249. next->prev = prev;
  1250. prev->next = next;
  1251. }
  1252. /**
  1253. * llist_del - deletes entry from llist.
  1254. * @entry: the element to delete from the llist.
  1255. * Note: llist_empty on entry does not return true after this, the entry is
  1256. * in an undefined state.
  1257. */
  1258. void llist_del(llist_head *entry)
  1259. {
  1260. if (entry->prev && entry->next)
  1261. {
  1262. __llist_del(entry->prev, entry->next);
  1263. }
  1264. entry->next = LLIST_POISON1;
  1265. entry->prev = LLIST_POISON2;
  1266. }
  1267. /**
  1268. * llist_del_init - deletes entry from llist and reinitialize it.
  1269. * @entry: the element to delete from the llist.
  1270. */
  1271. void llist_del_init(llist_head *entry)
  1272. {
  1273. __llist_del(entry->prev, entry->next);
  1274. INIT_LLIST_HEAD(entry);
  1275. }
  1276. /**
  1277. * llist_move - delete from one llist and add as another's head
  1278. * @llist: the entry to move
  1279. * @head: the head that will precede our entry
  1280. */
  1281. void llist_move(llist_head *llist, llist_head *head)
  1282. {
  1283. __llist_del(llist->prev, llist->next);
  1284. llist_add(llist, head);
  1285. }
  1286. /**
  1287. * llist_move_tail - delete from one llist and add as another's tail
  1288. * @llist: the entry to move
  1289. * @head: the head that will follow our entry
  1290. */
  1291. void llist_move_tail(llist_head *llist,
  1292. llist_head *head)
  1293. {
  1294. __llist_del(llist->prev, llist->next);
  1295. llist_add_tail(llist, head);
  1296. }
  1297. void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData)
  1298. {
  1299. llist_head *node = head->next;
  1300. llist_head *del;
  1301. int32_t result;
  1302. while (!llist_empty(head) && (node != head))
  1303. {
  1304. result = cb((void *)node, pData);
  1305. if (result > 0)
  1306. {
  1307. return node;
  1308. }
  1309. else
  1310. {
  1311. del = node;
  1312. node = node->next;
  1313. if (result < 0)
  1314. {
  1315. llist_del(del);
  1316. free(del);
  1317. }
  1318. }
  1319. }
  1320. return NULL;
  1321. }
  1322. /**
  1323. * llist_empty - tests whether a llist is empty
  1324. * @head: the llist to test.
  1325. */
  1326. int llist_empty(const llist_head *head)
  1327. {
  1328. return head->next == head;
  1329. }
  1330. uint32_t llist_num(const llist_head *head)
  1331. {
  1332. llist_head *node = head->next;
  1333. uint32_t num = 0;
  1334. if (!node)
  1335. return num;
  1336. while(node != head)
  1337. {
  1338. num++;
  1339. node = node->next;
  1340. }
  1341. return num;
  1342. }
  1343. #define PP_HTONS(x) ((uint16_t)((((x) & (uint16_t)0x00ffU) << 8) | (((x) & (uint16_t)0xff00U) >> 8)))
  1344. #define PP_NTOHS(x) PP_HTONS(x)
  1345. #define PP_HTONL(x) ((((x) & (uint32_t)0x000000ffUL) << 24) | \
  1346. (((x) & (uint32_t)0x0000ff00UL) << 8) | \
  1347. (((x) & (uint32_t)0x00ff0000UL) >> 8) | \
  1348. (((x) & (uint32_t)0xff000000UL) >> 24))
  1349. #define PP_NTOHL(x) PP_HTONL(x)
  1350. uint16_t BSP_Swap16(uint16_t n)
  1351. {
  1352. return (uint16_t)PP_HTONS(n);
  1353. }
  1354. uint32_t BSP_Swap32(uint32_t n)
  1355. {
  1356. return (uint32_t)PP_HTONL(n);
  1357. }
  1358. uint32_t utf8_to_unicode(uint8_t *string, uint32_t len, void *out, uint8_t is_only_16)
  1359. {
  1360. uint32_t i = 0;
  1361. uint32_t result = 0;
  1362. uint8_t bit, n;
  1363. if (is_only_16)
  1364. {
  1365. uint16_t *buf = (uint16_t *)out;
  1366. while (i < len)
  1367. {
  1368. if (string[i] & 0x80)
  1369. {
  1370. if (!(string[i] & (1 << 5)))
  1371. {
  1372. n = 2;
  1373. buf[result] = string[i] & ((1 << 5) - 1);
  1374. }
  1375. else
  1376. {
  1377. buf[result] = string[i] & ((1 << 4) - 1);
  1378. n = 3;
  1379. }
  1380. for (bit = 1; bit < n; bit++)
  1381. {
  1382. buf[result] = (buf[result] << 6) | (string[i + bit] & 0x3f);
  1383. }
  1384. i += n;
  1385. }
  1386. else
  1387. {
  1388. buf[result] = string[i];
  1389. i++;
  1390. }
  1391. result++;
  1392. }
  1393. }
  1394. else
  1395. {
  1396. uint8_t table[7] = {0, 0, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  1397. uint32_t *buf = (uint32_t *)out;
  1398. while (i < len)
  1399. {
  1400. if (string[i] & 0x80)
  1401. {
  1402. n = 7;
  1403. for (bit = 5; bit >= 1; bit--)
  1404. {
  1405. if (!(string[i] & (1 << bit)))
  1406. {
  1407. n -= bit;
  1408. break;
  1409. }
  1410. }
  1411. if (n >= 7)
  1412. {
  1413. return result;
  1414. }
  1415. buf[result] = string[i] & table[n];
  1416. for (bit = 1; bit < n; bit++)
  1417. {
  1418. buf[result] = (buf[result] << 6) | (string[i + bit] & 0x3f);
  1419. }
  1420. i += n;
  1421. }
  1422. else
  1423. {
  1424. buf[result] = string[i];
  1425. i++;
  1426. }
  1427. result++;
  1428. }
  1429. }
  1430. return result;
  1431. }
  1432. uint32_t unicode_to_utf8(void *in, uint32_t unicodelen, uint8_t *out, uint8_t is_only_16)
  1433. {
  1434. uint32_t i = 0;
  1435. uint32_t result = 0;
  1436. uint8_t bit, n;
  1437. if (is_only_16)
  1438. {
  1439. uint16_t *buf = (uint16_t *)in;
  1440. while (i < unicodelen)
  1441. {
  1442. if (buf[i] <= 0x007f)
  1443. {
  1444. out[result] = buf[i];
  1445. result++;
  1446. }
  1447. else
  1448. {
  1449. if (buf[i] >> 12)
  1450. {
  1451. out[result + 2] = (buf[i] & 0x3f) | 0x80;
  1452. out[result + 1] = ((buf[i] >> 6) & 0x3f) | 0x80;
  1453. out[result] = 0xe0 | (buf[i] >> 12);
  1454. result += 3;
  1455. }
  1456. else
  1457. {
  1458. out[result + 1] = (buf[i] & 0x3f) | 0x80;
  1459. out[result] = 0xc0 | (buf[i] >> 6);
  1460. result += 2;
  1461. }
  1462. }
  1463. i++;
  1464. }
  1465. }
  1466. else
  1467. {
  1468. uint8_t table[7] = {0,0,0xc0,0xe0, 0xf0, 0xf8, 0xfc};
  1469. uint8_t pos[7] = {0,0,6,12,18,24,30};
  1470. uint32_t *buf = (uint32_t *)in;
  1471. while (i < unicodelen)
  1472. {
  1473. if (buf[i] <= 0x007f)
  1474. {
  1475. out[result] = buf[i];
  1476. result++;
  1477. }
  1478. else
  1479. {
  1480. n = 6;
  1481. for (bit = 1; bit < 6; bit++)
  1482. {
  1483. if (!(buf[i] >> ((bit + 1) * 6)))
  1484. {
  1485. n = bit + 1;
  1486. break;
  1487. }
  1488. }
  1489. out[result] = table[n] | (buf[i] >> pos[n]);
  1490. for (bit = 1; bit < n; bit++)
  1491. {
  1492. out[result + bit] = ( (buf[i] >> ((n - bit - 1) * 6)) & 0x3f) | 0x80;
  1493. }
  1494. result += n;
  1495. }
  1496. i++;
  1497. }
  1498. }
  1499. return result;
  1500. }