bsp_common.c 31 KB

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