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