Răsfoiți Sursa

add: 添加xmake分层构建,当前air101编译ok,下一步air105!!

Wendal Chen 3 ani în urmă
părinte
comite
dbabef7b21
68 a modificat fișierele cu 2823 adăugiri și 2228 ștergeri
  1. 1651 1651
      components/c_common/c_common.c
  2. 566 566
      components/c_common/c_common.h
  3. 8 0
      components/c_common/xmake.lua
  4. 9 0
      components/crypto/xmake.lua
  5. 8 0
      components/fs/fal/xmake.lua
  6. 0 0
      components/fs/fatfs/luat_fs_fatfs.c
  7. 8 0
      components/fs/fatfs/xmake.lua
  8. 9 0
      components/fs/flashdb/xmake.lua
  9. 0 0
      components/fs/lfs/luat_fs_lfs2.c
  10. 8 0
      components/fs/lfs/xmake.lua
  11. 0 0
      components/fs/luadb/luat_fs_luadb.c
  12. 8 0
      components/fs/luadb/xmake.lua
  13. 8 3
      components/fs/vfs/xmake.lua
  14. 17 0
      components/fs/xmake.lua
  15. 7 0
      components/mbedtls/xmake.lua
  16. 8 0
      components/mempool/bget/xmake.lua
  17. 8 0
      components/mempool/tlsf3/xmake.lua
  18. 8 0
      components/mempool/xmake.lua
  19. 7 0
      components/network/ota/xmake.lua
  20. 12 0
      components/network/xmake.lua
  21. 10 0
      components/peripheral/camera/xmake.lua
  22. 1 1
      components/peripheral/io_queue/luat_lib_io_queue.c
  23. 8 0
      components/peripheral/io_queue/xmake.lua
  24. 2 1
      components/peripheral/lora/xmake.lua
  25. 10 0
      components/peripheral/mlx90640/xmake.lua
  26. 8 0
      components/peripheral/onchip/xmake.lua
  27. 8 0
      components/peripheral/sfd/xmake.lua
  28. 9 0
      components/peripheral/sfud/xmake.lua
  29. 8 0
      components/peripheral/soft_keyboard/xmake.lua
  30. 8 0
      components/peripheral/statem/xmake.lua
  31. 1 0
      components/peripheral/usbapp/luat_lib_usbapp.c
  32. 8 0
      components/peripheral/usbapp/xmake.lua
  33. 25 0
      components/peripheral/xmake.lua
  34. 8 0
      components/serialization/cjson/xmake.lua
  35. 9 0
      components/serialization/cmux/xmake.lua
  36. 8 0
      components/serialization/lua-cjson/xmake.lua
  37. 8 0
      components/serialization/miniz/xmake.lua
  38. 8 0
      components/serialization/protobuf/xmake.lua
  39. 18 0
      components/serialization/xmake.lua
  40. 8 0
      components/serialization/zlib/xmake.lua
  41. 10 0
      components/shell/xmake.lua
  42. 8 0
      components/tools/coremark/xmake.lua
  43. 10 0
      components/tools/dbg/xmake.lua
  44. 8 0
      components/tools/i2c-tools/xmake.lua
  45. 8 0
      components/tools/iconv/xmake.lua
  46. 8 0
      components/tools/iotauth/xmake.lua
  47. 8 0
      components/tools/libcoap/xmake.lua
  48. 8 0
      components/tools/luf/xmake.lua
  49. 8 0
      components/tools/minmea/xmake.lua
  50. 8 0
      components/tools/mqttcore/xmake.lua
  51. 8 0
      components/tools/pack/xmake.lua
  52. 8 0
      components/tools/vmx/xmake.lua
  53. 30 0
      components/tools/xmake.lua
  54. 8 0
      components/tools/ymodem/xmake.lua
  55. 9 0
      components/tools/zbuff/xmake.lua
  56. 13 0
      components/ui/eink/xmake.lua
  57. 12 0
      components/ui/gtfont/xmake.lua
  58. 11 0
      components/ui/lcd/xmake.lua
  59. 11 0
      components/ui/luatfonts/xmake.lua
  60. 14 0
      components/ui/lvgl/xmake.lua
  61. 7 0
      components/ui/qrcode/xmake.lua
  62. 10 0
      components/ui/tjpgd/xmake.lua
  63. 10 0
      components/ui/u8g2/xmake.lua
  64. 9 0
      components/ui/ufont/xmake.lua
  65. 24 0
      components/ui/xmake.lua
  66. 28 4
      components/xmake.lua
  67. 2 1
      lua/xmake.lua
  68. 2 1
      luat/xmake.lua

+ 1651 - 1651
components/common/c_common.c → components/c_common/c_common.c

@@ -1,1651 +1,1651 @@
-/*
- * Copyright (c) 2022 OpenLuat & AirM2M
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#include "c_common.h"
-#include "luat_malloc.h"
-#include "luat_base.h"
-#include "luat_log.h"
-
-#define COMMON_LOG_BUF_SIZE 255
-static char common_log_buf[COMMON_LOG_BUF_SIZE];
-const uint8_t ByteToAsciiTable[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
-
-
-LUAT_WEAK void DBG_Printf(const char* format, ...){
-    va_list args;
-    va_start(args, format);
-    int len = vsnprintf(common_log_buf, sizeof(common_log_buf), format, args);
-	luat_log_write(common_log_buf, len);
-    va_end(args);
-}
-
-LUAT_WEAK void DBG_HexPrintf(void *Data, unsigned int len){
-	uint8_t *data = (uint8_t *)Data;
-	uint8_t *uart_buf;
-    uint32_t i,j;
-    j = 0;
-    if (!len) return;
-    uart_buf = luat_heap_zalloc(len * 3 + 2);
-    if (!uart_buf) return;
-    for (i = 0; i < len; i++){
-		uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
-		uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
-		uart_buf[j++] = ' ';
-    }
-    uart_buf[j++] = '\r';
-    uart_buf[j++] = '\n';
-	luat_log_write((char*)uart_buf, len * 3 + 2);
-	luat_heap_free(uart_buf);
-}
-
-void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize)
-{
-	uint8_t *Data = (uint8_t *)Src;
-	Buf->Data = Data;
-	Buf->Len = 0;
-	Buf->MaxLength = MaxLen;
-	Buf->Offset = 0;
-	Buf->DataSize = DataSize;
-}
-
-uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len)
-{
-	uint32_t i, p;
-	uint8_t *Data = (uint8_t *)Src;
-	if (Buf->Len < Len)
-	{
-		Len = Buf->Len;
-	}
-	if (Buf->DataSize > 1)
-	{
-		for (i = 0, p = Buf->Offset; i < Len; i++, p++)
-		{
-			if (p >= Buf->MaxLength)
-			{
-				p -= Buf->MaxLength;
-			}
-			memcpy(Data + (i * Buf->DataSize), Buf->Data + (p * Buf->DataSize), Buf->DataSize);
-		}
-	}
-	else
-	{
-		for (i = 0, p = Buf->Offset; i < Len; i++, p++)
-		{
-			if (p >= Buf->MaxLength)
-			{
-				p -= Buf->MaxLength;
-			}
-			Data[i] = Buf->Data[p];
-		}
-	}
-
-	return Len;
-}
-
-uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len)
-{
-	uint32_t l;
-	uint8_t *Data = (uint8_t *)Src;
-	l = LoopBuffer_Query(Buf, Data, Len);
-	Buf->Len -= l;
-	Buf->Offset += l;
-	if (Buf->Offset >= Buf->MaxLength)
-	{
-		Buf->Offset -= Buf->MaxLength;
-
-	}
-	if (!Buf->Len) {
-		Buf->Offset = 0;
-	}
-	return l;
-}
-
-void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len)
-{
-	if (Buf->Len < Len)
-	{
-		Len = Buf->Len;
-	}
-
-	Buf->Len -= Len;
-	Buf->Offset += Len;
-	if (Buf->Offset >= Buf->MaxLength)
-	{
-		Buf->Offset -= Buf->MaxLength;
-	}
-
-	if (!Buf->Len) {
-		Buf->Offset = 0;
-	}
-}
-
-uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len)
-{
-	uint32_t i, p, cut_off = 0;
-	uint8_t *Data = (uint8_t *)Src;
-	if (!Buf->Len && !Buf->Offset && (Len <= Buf->Len))
-	{
-		memcpy(Buf->Data, Data, Len);
-		Buf->Len = Len;
-		return Len;
-	}
-	cut_off = Buf->MaxLength - Buf->Len;
-	if (cut_off >= Len)
-	{
-		cut_off = 0;
-	}
-	else
-	{
-		cut_off = Len - cut_off;
-	}
-
-	if (Buf->DataSize > 1)
-	{
-		for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
-		{
-			if (p >= Buf->MaxLength)
-			{
-				p -= Buf->MaxLength;
-			}
-			memcpy(Buf->Data + (p * Buf->DataSize), Data + (i * Buf->DataSize), Buf->DataSize);
-		}
-	}
-	else
-	{
-		for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
-		{
-			if (p >= Buf->MaxLength)
-			{
-				p -= Buf->MaxLength;
-			}
-
-			Buf->Data[p] = Data[i];
-		}
-	}
-
-
-	Buf->Offset += cut_off;
-	if (Buf->Offset >= Buf->MaxLength)
-		Buf->Offset -= Buf->MaxLength;
-
-	Buf->Len += Len;
-	if (Buf->Len > Buf->MaxLength)
-		Buf->Len = Buf->MaxLength;
-
-	return Len;
-
-}
-
-
-void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen)
-{
-	Buf->Data = Src;
-	Buf->Pos = 0;
-	Buf->MaxLen = MaxLen;
-}
-
-int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
-{
-	if (!Len)
-	{
-		return -1;
-	}
-	if (!Buf)
-	{
-		return -1;
-	}
-	if ((Buf->Pos + Len) > Buf->MaxLen)
-	{
-		Len = Buf->MaxLen - Buf->Pos;
-	}
-	if (Len)
-	{
-		memcpy(&Buf->Data[Buf->Pos], Data, Len);
-	}
-	Buf->Pos += Len;
-	return Len;
-}
-
-void DBuffer_Init(DBuffer_Struct *DBuf, uint32_t Size)
-{
-	memset(DBuf, 0, sizeof(DBuffer_Struct));
-	DBuf->pCache[0] = malloc(Size);
-	DBuf->pCache[1] = malloc(Size);
-	DBuf->MaxLen = Size;
-}
-
-void DBuffer_ReInit(DBuffer_Struct *DBuf, uint32_t Size)
-{
-	if (DBuf->pCache[0]) free(DBuf->pCache[0]);
-	if (DBuf->pCache[1]) free(DBuf->pCache[1]);
-	DBuffer_Init(DBuf, Size);
-}
-
-void DBuffer_DeInit(DBuffer_Struct *DBuf)
-{
-	free(DBuf->pCache[0]);
-	free(DBuf->pCache[1]);
-	DBuf->pCache[0] = NULL;
-	DBuf->pCache[1] = NULL;
-	DBuf->MaxLen = 0;
-}
-
-void *DBuffer_GetCache(DBuffer_Struct *DBuf, uint8_t IsCurrent)
-{
-	return DBuf->pCache[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn];
-}
-
-void DBuffer_SwapCache(DBuffer_Struct *DBuf)
-{
-	DBuf->CurCacheSn = !DBuf->CurCacheSn;
-}
-
-void DBuffer_SetDataLen(DBuffer_Struct *DBuf, uint32_t Len, uint8_t IsCurrent)
-{
-	DBuf->pCacheLen[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn] = Len;
-}
-
-uint32_t DBuffer_GetDataLen(DBuffer_Struct *DBuf, uint8_t IsCurrent)
-{
-	return DBuf->pCacheLen[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn];
-}
-
-//void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len)
-//{
-//	uint32_t RestLen;
-//	uint32_t i;
-//	if (!Buf)
-//		return ;
-//	if (!Buf->Data)
-//		return ;
-//	if (Len >= Buf->Pos)
-//	{
-//		Buf->Pos = 0;
-//		return ;
-//	}
-//	RestLen = Buf->Pos - Len;
-//	memmove(Buf->Data, Buf->Data + Len, RestLen);
-//	Buf->Pos = RestLen;
-//}
-
-
-/*****************************************************************************
-* FUNCTION
-*   command_parse_param()
-* DESCRIPTION
-*    Parse AT command string to parameters
-* PARAMETERS
-*   char* pStr
-* RETURNS
-*  pCmdParam
-*****************************************************************************/
-uint32_t CmdParseParam(int8_t* pStr, CmdParam *CP, int8_t Cut)
-{
-	uint32_t paramStrLen = strlen((char *)pStr);
-	uint32_t paramIndex = 0;
-	uint32_t paramCharIndex = 0;
-	uint32_t index = 0;
-
-	while ((pStr[index] != '\r')
-		&& (index < paramStrLen)
-		&& (paramIndex < CP->param_max_num)) {
-		if (pStr[index] == Cut) {
-			/* Next param string */
-			paramCharIndex = 0;
-			paramIndex++;
-		}
-		else {
-			if (pStr[index] != '"')
-			{
-				if (paramCharIndex >= CP->param_max_len)
-					return (0);
-
-				/*Get each of command param char, the param char except char ' " '*/
-				CP->param_str[paramIndex * CP->param_max_len + paramCharIndex] = pStr[index];
-				paramCharIndex++;
-			}
-		}
-		index++;
-	}
-
-	CP->param_num = paramIndex + 1;
-
-	return (1);
-}
-
-
-LUAT_WEAK int32_t OS_InitBuffer(Buffer_Struct *Buf, uint32_t Size)
-{
-	if (!Buf)
-		return 0;
-	Buf->Data = luat_heap_malloc(Size);
-	memset(Buf->Data, 0, Size);
-	if (!Buf->Data)
-	{
-		Buf->MaxLen = 0;
-		Buf->Pos = 0;
-		return 0;
-	}
-	Buf->MaxLen = Size;
-	Buf->Pos = 0;
-	return Size;
-}
-
-LUAT_WEAK void OS_DeInitBuffer(Buffer_Struct *Buf)
-{
-	if (Buf->Data)
-	{
-		luat_heap_free(Buf->Data);
-	}
-	Buf->Data = NULL;
-	Buf->MaxLen = 0;
-	Buf->Pos = 0;
-}
-
-LUAT_WEAK int32_t OS_ReInitBuffer(Buffer_Struct *Buf, uint32_t Size)
-{
-	if (!Buf)
-		return 0;
-
-	if (Buf->Data)
-	{
-		luat_heap_free(Buf->Data);
-	}
-	Buf->Data = luat_heap_malloc(Size);
-	memset(Buf->Data, 0, Size);
-	if (!Buf->Data)
-	{
-		Buf->MaxLen = 0;
-		Buf->Pos = 0;
-		return 0;
-	}
-	Buf->MaxLen = Size;
-	Buf->Pos = 0;
-	return Size;
-}
-
-LUAT_WEAK int32_t OS_ReSizeBuffer(Buffer_Struct *Buf, uint32_t Size)
-{
-//	uint8_t *Old;
-	uint8_t *New;
-
-	if (!Buf)
-		return 0;
-
-//	Old = Buf->Data;
-//	if (Size < Buf->Pos)
-//	{
-//		Size = Buf->Pos;
-//	}
-//	New = OS_Zalloc(Size);
-//	if (!New)
-//	{
-//		return 0;
-//	}
-//	if (Old)
-//	{
-//		memcpy(New, Old, Buf->Pos);
-//		OS_Free(Old);
-//	}
-	New = luat_heap_realloc(Buf->Data, Size);
-	if (New)
-	{
-		Buf->Data = New;
-		Buf->MaxLen = Size;
-	}
-	return Size;
-}
-
-LUAT_WEAK int32_t OS_BufferWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
-{
-	uint32_t WriteLen;
-	if (!Len)
-	{
-		return ERROR_NONE;
-	}
-	if (!Buf)
-	{
-		return -ERROR_PARAM_INVALID;
-	}
-	if (!Buf->Data)
-	{
-		Buf->Data = luat_heap_malloc(Len);
-		memset(Buf->Data, 0, Len);
-		if (!Buf->Data)
-		{
-			return -ERROR_NO_MEMORY;
-		}
-		Buf->Pos = 0;
-		Buf->MaxLen = Len;
-	}
-	WriteLen = Buf->Pos + Len;
-	if (WriteLen > Buf->MaxLen)
-	{
-		if (!OS_ReSizeBuffer(Buf, WriteLen))
-		{
-			return -ERROR_NO_MEMORY;
-		}
-	}
-	memcpy(&Buf->Data[Buf->Pos], Data, Len);
-	Buf->Pos += Len;
-	return ERROR_NONE;
-}
-
-LUAT_WEAK int32_t OS_BufferWriteLimit(Buffer_Struct *Buf, void *Data, uint32_t Len)
-{
-	uint32_t WriteLen;
-	if (!Len)
-	{
-		return ERROR_NONE;
-	}
-	if (!Buf)
-	{
-		return -ERROR_PARAM_INVALID;
-	}
-	if (!Buf->Data)
-	{
-		Buf->Data = luat_heap_malloc(Len);
-		memset(Buf->Data, 0, Len);
-		if (!Buf->Data)
-		{
-			return -ERROR_NO_MEMORY;
-		}
-		Buf->Pos = 0;
-		Buf->MaxLen = Len;
-	}
-	WriteLen = Buf->Pos + Len;
-	if (WriteLen > Buf->MaxLen)
-	{
-		return -ERROR_NO_MEMORY;
-	}
-	memcpy(&Buf->Data[Buf->Pos], Data, Len);
-	Buf->Pos += Len;
-	return ERROR_NONE;
-}
-
-LUAT_WEAK void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len)
-{
-	uint32_t RestLen;
-	uint32_t i;
-	if (!Buf)
-		return ;
-	if (!Buf->Data)
-		return ;
-	if (Len >= Buf->Pos)
-	{
-		Buf->Pos = 0;
-		return ;
-	}
-	RestLen = Buf->Pos - Len;
-	memmove(Buf->Data, Buf->Data + Len, RestLen);
-	Buf->Pos = RestLen;
-}
-
-
-
-int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value)
-{
-	uint32_t Mask,Pos1,Pos2;
-
-	Pos1 = Sn/8;
-	Pos2 = Sn%8;
-
-	Mask = ~(1 << Pos2);
-	if (Value)
-	{
-		Value = (1 << Pos2);
-	}
-	Data[Pos1] = (Data[Pos1] & Mask) | Value;
-	//DBG("%d %d %d %d", Sn, Pos1, Pos2, Value);
-	return 0;
-}
-
-int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value)
-{
-	uint32_t Mask,Pos1,Pos2;
-
-	Pos1 = Sn/8;
-	Pos2 = Sn%8;
-	Mask = (1 << Pos2);
-	if (Data[Pos1] & Mask)
-	{
-		*Value = 1;
-	}
-	else
-	{
-		*Value = 0;
-	}
-	return -1;
-}
-
-uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn)
-{
-	uint32_t Mask,Pos1,Pos2;
-
-	Pos1 = Sn/8;
-	Pos2 = Sn%8;
-	Mask = (1 << Pos2);
-	if (Data[Pos1] & Mask)
-	{
-		return 1;
-	}
-	return 0;
-}
-
-uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart)
-{
-	uint8_t Check = CheckStart;
-	uint8_t *Data = (uint8_t *)Src;
-	uint32_t i;
-	for (i = 0; i < Len; i++)
-	{
-		Check ^= Data[i];
-	}
-	return Check;
-}
-
-uint8_t SumCheck(uint8_t *Data, uint32_t Len)
-{
-	uint8_t Check = 0;
-	uint32_t i;
-	for (i = 0; i < Len; i++)
-	{
-		Check += Data[i];
-	}
-	return Check;
-}
-
-
-uint8_t CRC8Cal(void *Data, uint16_t Len, uint8_t CRC8Last, uint8_t CRCRoot, uint8_t IsReverse)
-{
-	uint16_t i;
-	uint8_t CRC8 = CRC8Last;
-	uint8_t wTemp = CRCRoot;
-	uint8_t *Src = (uint8_t *)Data;
-	if (IsReverse)
-	{
-		CRCRoot = 0;
-		for (i = 0; i < 8; i++)
-		{
-			if (wTemp & (1 << (7 - i)))
-			{
-				CRCRoot |= 1 << i;
-			}
-		}
-		while (Len--)
-		{
-
-			CRC8 ^= *Src++;
-			for (i = 0; i < 8; i++)
-			{
-				if ((CRC8 & 0x01))
-				{
-					CRC8 >>= 1;
-					CRC8 ^= CRCRoot;
-				}
-				else
-				{
-					CRC8 >>= 1;
-				}
-			}
-		}
-	}
-	else
-	{
-		while (Len--)
-		{
-
-			CRC8 ^= *Src++;
-			for (i = 8; i > 0; --i)
-			{
-				if ((CRC8 & 0x80))
-				{
-					CRC8 <<= 1;
-					CRC8 ^= CRCRoot;
-				}
-				else
-				{
-					CRC8 <<= 1;
-				}
-			}
-		}
-	}
-	return CRC8;
-}
-
-/************************************************************************/
-/*  CRC16                                                                */
-/************************************************************************/
-uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse)
-{
-	uint16_t i;
-	uint16_t CRC16 = CRC16Last;
-	uint16_t wTemp = CRCRoot;
-	uint8_t *Src = (uint8_t *)Data;
-	if (IsReverse)
-	{
-		CRCRoot = 0;
-		for (i = 0; i < 16; i++)
-		{
-			if (wTemp & (1 << (15 - i)))
-			{
-				CRCRoot |= 1 << i;
-			}
-		}
-		while (Len--)
-		{
-			for (i = 0; i < 8; i++)
-			{
-				if ((CRC16 & 0x0001) != 0)
-				{
-					CRC16 >>= 1;
-					CRC16 ^= CRCRoot;
-				}
-				else
-				{
-					CRC16 >>= 1;
-				}
-				if ((*Src&(1 << i)) != 0)
-				{
-					CRC16 ^= CRCRoot;
-				}
-			}
-			Src++;
-		}
-	}
-	else
-	{
-		while (Len--)
-		{
-			for (i = 8; i > 0; i--)
-			{
-				if ((CRC16 & 0x8000) != 0)
-				{
-					CRC16 <<= 1;
-					CRC16 ^= CRCRoot;
-				}
-				else
-				{
-					CRC16 <<= 1;
-				}
-				if ((*Src&(1 << (i - 1))) != 0)
-				{
-					CRC16 ^= CRCRoot;
-				}
-			}
-			Src++;
-		}
-	}
-
-	return CRC16;
-}
-
-uint32_t AsciiToU32(uint8_t *Src, uint32_t Len)
-{
-	uint32_t i = 0;
-	uint32_t Temp = 0;
-	for (i = 0; i < Len; i++)
-	{
-
-		if (Src[i])
-		{
-			Temp *= 10;
-			Temp += Src[i] - '0';
-		}
-		else
-		{
-			break;
-		}
-	}
-	return Temp;
-}
-
-
-/**
-* @brief  反转数据
-* @param  ref 需要反转的变量
-* @param	ch 反转长度,多少位
-* @retval N反转后的数据
-*/
-static LongInt Reflect(LongInt ref, uint8_t ch)
-{
-	LongInt value = 0;
-	LongInt i;
-	for (i = 1; i < (LongInt)(ch + 1); i++)
-	{
-		if (ref & 1)
-			value |= (LongInt)1 << (ch - i);
-		ref >>= 1;
-	}
-	return value;
-}
-
-/**
-* @brief  建立CRC32的查询表
-* @param  Tab 表缓冲
-* @param	Gen CRC32根
-* @retval None
-*/
-void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen)
-{
-	uint32_t crc;
-	uint32_t i, j, temp, t1, t2, flag;
-	if (Tab[1] != 0)
-		return;
-	for (i = 0; i < 256; i++)
-	{
-		temp = Reflect(i, 8);
-		Tab[i] = temp << 24;
-		for (j = 0; j < 8; j++)
-		{
-			flag = Tab[i] & 0x80000000;
-			t1 = Tab[i] << 1;
-			if (0 == flag)
-			{
-				t2 = 0;
-			}
-			else
-			{
-				t2 = Gen;
-			}
-			Tab[i] = t1 ^ t2;
-		}
-		crc = Tab[i];
-		Tab[i] = Reflect(crc, 32);
-	}
-}
-
-
-/**
-* @brief  计算buffer的crc校验码
-* @param  CRC32_Table CRC32表
-* @param  Buf 缓冲
-* @param	Size 缓冲区长度
-* @param	CRC32 初始CRC32值
-* @retval 计算后的CRC32
-*/
-uint32_t CRC32_Cal(uint32_t *CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last)
-{
-	uint32_t i;
-	for (i = 0; i < Size; i++)
-	{
-		CRC32Last = CRC32_Table[(CRC32Last ^ Buf[i]) & 0xff] ^ (CRC32Last >> 8);
-	}
-	return CRC32Last;
-}
-
-
-/************************************************************************/
-/*时间与时间戳转换,C语言实现                                                                    */
-/************************************************************************/
-/************************************************************************/
-uint8_t IsLeapYear(uint32_t Year)
-{
-	if ((Year % 400) == 0)
-		return 1;
-	if ((((Year % 4) == 0) && (Year % 100) != 0))
-		return 1;
-	else
-		return 0;
-}
-
-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 } };
-//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 } };
-LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time)
-{
-
-	LongInt DYear, DDay, DSec;
-	uint32_t Year100;
-	DYear = Date->Year - 1970;
-	if (DYear)	//1970年以后,1972是第一个闰年,1973年是第一年需要增加一天,2100年是非闰年
-	{
-		DDay = DYear * 365 + ((DYear + 1) / 4) + DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
-//		if (IsLeapYear(Date->Year))
-//		{
-//			DDay--;
-//		}
-		if (Date->Year >= 2100)
-		{
-			Year100 = Date->Year - 2100;
-			DDay -= (1 + Year100 / 100);
-			if (Date->Year >= 2400)
-			{
-				Year100 = Date->Year - 2400;
-				DDay += 1 + Year100 / 400;
-			}
-
-		}
-
-	}
-	else
-	{
-		DDay = DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
-	}
-	DSec = DDay * 86400 + Time->Hour * 3600 + Time->Min * 60 + Time->Sec;
-	return DSec;
-}
-#define YEAR_1_DAY_BEFORE2000		365
-#define YEAR_2_DAY_BEFORE2000		730
-#define YEAR_3_DAY_BEFORE2000		1096
-
-
-#define YEAR_1_DAY_AFTER2000		365
-#define YEAR_2_DAY_AFTER2000		730
-#define YEAR_3_DAY_AFTER2000		1095
-
-#define YEAR_4_DAY		1461
-#define YEAR_31_DAY		11323
-
-#define YEAR_100_DAY	36524
-#define YEAR_400_DAY	146097
-
-uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay)
-{
-
-	uint32_t DYear,i, LeapFlag, Temp;
-	uint32_t DDay;
-	DDay = Sec / 86400;
-	if (DDay != LastDDay)
-	{
-		DYear = 0;
-		Time->Week = (4 + DDay) % 7;
-		if (DDay >= YEAR_31_DAY)
-		{
-			DDay -= YEAR_31_DAY;
-			DYear = 31;
-
-			if (DDay >= YEAR_400_DAY)
-			{
-				Temp = DDay / YEAR_400_DAY;
-				DYear += Temp * 400;
-				DDay -= Temp * YEAR_400_DAY;
-			}
-
-			if (DDay >= YEAR_100_DAY)
-			{
-				Temp = DDay / YEAR_100_DAY;
-				DYear += Temp * 100;
-				DDay -= Temp * YEAR_100_DAY;
-			}
-
-			if (DDay >= YEAR_4_DAY)
-			{
-				Temp = DDay / YEAR_4_DAY;
-				DYear += Temp * 4;
-				DDay -= Temp * YEAR_4_DAY;
-			}
-
-			if (DDay >= YEAR_3_DAY_AFTER2000)
-			{
-				DYear += 3;
-				DDay -= YEAR_3_DAY_AFTER2000;
-			}
-			else if (DDay >= YEAR_2_DAY_AFTER2000)
-			{
-				DYear += 2;
-				DDay -= YEAR_2_DAY_AFTER2000;
-			}
-			else if (DDay >= YEAR_1_DAY_AFTER2000)
-			{
-				DYear += 1;
-				DDay -= YEAR_1_DAY_AFTER2000;
-			}
-
-		}
-		else
-		{
-			if (DDay >= YEAR_4_DAY)
-			{
-				Temp = DDay / YEAR_4_DAY;
-				DYear += Temp * 4;
-				DDay -= Temp * YEAR_4_DAY;
-			}
-
-			if (DDay >= YEAR_3_DAY_BEFORE2000)
-			{
-				DYear += 3;
-				DDay -= YEAR_3_DAY_BEFORE2000;
-			}
-			else if (DDay >= YEAR_2_DAY_BEFORE2000)
-			{
-				DYear += 2;
-				DDay -= YEAR_2_DAY_BEFORE2000;
-			}
-			else if (DDay >= YEAR_1_DAY_BEFORE2000)
-			{
-				DYear += 1;
-				DDay -= YEAR_1_DAY_BEFORE2000;
-			}
-		}
-
-		Date->Year = DYear + 1970;
-		LeapFlag = IsLeapYear(Date->Year);
-		Date->Mon = 12;
-		for (i = 1; i < 12; i++)
-		{
-			if (DDay < DayTable[LeapFlag][i])
-			{
-				Date->Mon = i;
-				break;
-			}
-		}
-		Date->Day = DDay - DayTable[LeapFlag][Date->Mon - 1] + 1;
-	}
-
-	Sec = Sec % 86400;
-	Time->Hour = Sec / 3600;
-	Sec = Sec % 3600;
-	Time->Min = Sec / 60;
-	Time->Sec = Sec % 60;
-	return DDay;
-}
-
-
-/**
- * \brief get a byte (8bits) from a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer
- * \return              the byte value
- */
-uint8_t BytesGet8(const void *ptr)
-{
-    const uint8_t *p = (const uint8_t *)ptr;
-    return p[0];
-}
-
-/**
- * \brief put a byte (8bits) to a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer
- * \param v             the byte value
- */
-void BytesPut8(void *ptr, uint8_t v)
-{
-    uint8_t *p = (uint8_t *)ptr;
-    p[0] = v;
-}
-
-/**
- * \brief get a big endian short (16bits) from a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \return              the short value
- */
-uint16_t BytesGetBe16(const void *ptr)
-{
-    const uint8_t *p = (const uint8_t *)ptr;
-    return (p[0] << 8) | p[1];
-}
-
-/**
- * \brief put a big endian short (16bits) to a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \param v             the short value
- */
-void BytesPutBe16(void *ptr, uint16_t v)
-{
-    uint8_t *p = (uint8_t *)ptr;
-    p[0] = (v >> 8) & 0xff;
-    p[1] = v & 0xff;
-}
-
-/**
- * \brief get a big endian word (32bits) from a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \return              the word value
- */
-uint32_t BytesGetBe32(const void *ptr)
-{
-    const uint8_t *p = (const uint8_t *)ptr;
-    return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
-}
-
-/**
- * \brief put a big endian word (32bits) to a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \param v             the word value
- */
-void BytesPutBe32(void *ptr, uint32_t v)
-{
-    uint8_t *p = (uint8_t *)ptr;
-    p[0] = (v >> 24) & 0xff;
-    p[1] = (v >> 16) & 0xff;
-    p[2] = (v >> 8) & 0xff;
-    p[3] = v & 0xff;
-}
-
-/**
- * \brief get a little endian short (16bits) from a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \return              the short value
- */
-uint16_t BytesGetLe16(const void *ptr)
-{
-    const uint8_t *p = (const uint8_t *)ptr;
-    return p[0] | (p[1] << 8);
-}
-
-/**
- * \brief put a little endian short (16bits) to a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \param v             the short value
- */
-void BytesPutLe16(void *ptr, uint16_t v)
-{
-    uint8_t *p = (uint8_t *)ptr;
-    p[0] = v & 0xff;
-    p[1] = (v >> 8) & 0xff;
-}
-
-/**
- * \brief get a little endian word (32bits) from a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \return              the word value
- */
-uint32_t BytesGetLe32(const void *ptr)
-{
-    const uint8_t *p = (const uint8_t *)ptr;
-    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
-}
-
-/**
- * \brief put a little endian word (32bits) to a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \param v             the word value
- */
-void BytesPutLe32(void *ptr, uint32_t v)
-{
-    uint8_t *p = (uint8_t *)ptr;
-    p[0] = v & 0xff;
-    p[1] = (v >> 8) & 0xff;
-    p[2] = (v >> 16) & 0xff;
-    p[3] = (v >> 24) & 0xff;
-}
-
-/**
- * \brief get a little endian long long (64bits) from a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \return              the long long value
- */
-uint64_t BytesGetLe64(const void *ptr)
-{
-    const uint8_t *p = (const uint8_t *)ptr;
-    return BytesGetLe32(p) | ((uint64_t)BytesGetLe32(p + 4) << 32);
-}
-
-/**
- * \brief put a little endian long long (64bits) to a pointer
- *
- * Caller should ensure parameters are valid.
- *
- * \param ptr           the pointer, may be unaligned
- * \param v             the long long value
- */
-void BytesPutLe64(void *ptr, uint64_t v)
-{
-    uint8_t *p = (uint8_t *)ptr;
-    BytesPutLe32(p, v & 0xffffffff);
-    BytesPutLe32(p + 4, (v >> 32) & 0xffffffff);
-}
-
-uint8_t BytesGet8FromBuf(Buffer_Struct *Buf)
-{
-	Buf->Pos++;
-    return Buf->Data[Buf->Pos - 1];
-}
-
-
-void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v)
-{
-	Buf->Data[Buf->Pos] = v;
-	Buf->Pos++;
-}
-
-uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf)
-{
-	Buf->Pos += 2;
-    return (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
-}
-
-void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v)
-{
-	Buf->Data[Buf->Pos] = (v >> 8) & 0xff;
-	Buf->Data[Buf->Pos + 1] = v & 0xff;
-	Buf->Pos += 2;
-}
-
-uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf)
-{
-	Buf->Pos += 4;
-    return (Buf->Data[Buf->Pos - 4] << 24) | (Buf->Data[Buf->Pos - 3] << 16) | (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
-}
-
-void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v)
-{
-	Buf->Data[Buf->Pos] = (v >> 24) & 0xff;
-	Buf->Data[Buf->Pos + 1] = (v >> 16) & 0xff;
-	Buf->Data[Buf->Pos + 2] = (v >> 8) & 0xff;
-	Buf->Data[Buf->Pos + 3] = v & 0xff;
-	Buf->Pos += 4;
-}
-
-
-uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf)
-{
-	Buf->Pos += 2;
-    return Buf->Data[Buf->Pos - 2] | (Buf->Data[Buf->Pos - 1] << 8);
-}
-
-void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v)
-{
-	Buf->Data[Buf->Pos] = v & 0xff;
-    Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
-    Buf->Pos+= 2;
-}
-
-uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf)
-{
-	Buf->Pos += 4;
-    return Buf->Data[Buf->Pos - 4] | (Buf->Data[Buf->Pos - 3] << 8) | (Buf->Data[Buf->Pos - 2] << 16) | (Buf->Data[Buf->Pos - 1] << 24);
-}
-
-void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v)
-{
-	Buf->Data[Buf->Pos] = v & 0xff;
-	Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
-	Buf->Data[Buf->Pos + 2] = (v >> 16) & 0xff;
-	Buf->Data[Buf->Pos + 3] = (v >> 24) & 0xff;
-	Buf->Pos += 4;
-}
-
-uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf)
-{
-	uint64_t Temp = BytesGetLe32FromBuf(Buf);
-    return Temp | ((uint64_t)BytesGetLe32FromBuf(Buf) << 32);
-}
-
-void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v)
-{
-
-	BytesPutLe32ToBuf(Buf, v & 0xffffffff);
-	BytesPutLe32ToBuf(Buf, (v >> 32) & 0xffffffff);
-}
-
-float BytesGetFloatFromBuf(Buffer_Struct *Buf)
-{
-	float Temp;
-	Buf->Pos += 4;
-	memcpy(&Temp, &Buf->Data[Buf->Pos - 4], 4);
-    return Temp;
-}
-
-void BytesPutFloatToBuf(Buffer_Struct *Buf, float v)
-{
-	memcpy(&Buf->Data[Buf->Pos], &v, 4);
-	Buf->Pos += 4;
-}
-
-double BytesGetDoubleFromBuf(Buffer_Struct *Buf)
-{
-	double Temp;
-	Buf->Pos += 8;
-	memcpy(&Temp, &Buf->Data[Buf->Pos - 8], 8);
-    return Temp;
-}
-
-void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v)
-{
-	memcpy(&Buf->Data[Buf->Pos], &v, 8);
-	Buf->Pos += 8;
-}
-
-void BytesGetMemoryFromBuf(Buffer_Struct *Buf, uint8_t *Data, uint32_t Len)
-{
-	memcpy(Data, &Buf->Data[Buf->Pos], Len);
-	Buf->Pos += Len;
-}
-
-/*
- * 转义打包
- * 标识Flag,即包头包尾加入Flag
- * 数据中遇到Flag -> Code F1
- * 数据中遇到Code -> Code F2
- */
-
-uint32_t TransferPack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
-{
-	uint32_t TxLen = 0;
-	uint32_t i;
-	OutBuf[0] = Flag;
-	TxLen = 1;
-	for (i = 0; i < Len; i++)
-	{
-		if (InBuf[i] == Flag)
-		{
-			OutBuf[TxLen++] = Code;
-			OutBuf[TxLen++] = F1;
-		}
-		else if (InBuf[i] == Code)
-		{
-			OutBuf[TxLen++] = Code;
-			OutBuf[TxLen++] = F2;
-		}
-		else
-		{
-			OutBuf[TxLen++] = InBuf[i];
-		}
-	}
-	OutBuf[TxLen++] = Flag;
-	return TxLen;
-}
-
-
-/*
- * 转义解包
- * 标识Flag,即包头包尾加入Flag
- * 数据中遇到Code F1 -> Flag
- * 数据中遇到Code F2 -> Code
- * 数据中遇到Flag 出错返回0
- */
-uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
-{
-	uint32_t RxLen = 0;
-	uint32_t i = 0;
-	while (i < Len)
-	{
-		if (InBuf[i] == Code)
-		{
-			if (InBuf[i+1] == F1)
-			{
-				OutBuf[RxLen++] = Flag;
-			}
-			else if (InBuf[i+1] == F2)
-			{
-				OutBuf[RxLen++] = Code;
-			}
-			else
-			{
-				return 0;
-			}
-			i += 2;
-		}
-		else if (InBuf[i] == Flag)
-		{
-			return 0;
-		}
-		else
-		{
-			OutBuf[RxLen++] = InBuf[i++];
-		}
-	}
-	return RxLen;
-}
-
-/*
- * Insert a new entry between two known consecutive entries.
- *
- * This is only for internal llist manipulation where we know
- * the prev/next entries already!
- */
-void __llist_add(llist_head *p,
-                         llist_head *prev,
-                         llist_head *next)
-{
-	next->prev = p;
-	p->next = next;
-	p->prev = prev;
-	prev->next = p;
-}
-
-/**
- * llist_add - add a new entry
- * @new: new entry to be added
- * @head: llist head to add it after
- *
- * Insert a new entry after the specified head.
- * This is good for implementing stacks.
- */
-void llist_add(llist_head *p, llist_head *head)
-{
-	__llist_add(p, head, head->next);
-}
-
-/**
- * llist_add_tail - add a new entry
- * @new: new entry to be added
- * @head: llist head to add it before
- *
- * Insert a new entry before the specified head.
- * This is useful for implementing queues.
- */
-void llist_add_tail(llist_head *p, llist_head *head)
-{
-	__llist_add(p, head->prev, head);
-}
-
-/*
- * Delete a llist entry by making the prev/next entries
- * point to each other.
- *
- * This is only for internal llist manipulation where we know
- * the prev/next entries already!
- */
-void __llist_del(llist_head * prev, llist_head * next)
-{
-	next->prev = prev;
-	prev->next = next;
-}
-
-/**
- * llist_del - deletes entry from llist.
- * @entry: the element to delete from the llist.
- * Note: llist_empty on entry does not return true after this, the entry is
- * in an undefined state.
- */
-void llist_del(llist_head *entry)
-{
-	if (entry->prev && entry->next)
-	{
-		__llist_del(entry->prev, entry->next);
-	}
-	entry->next = LLIST_POISON1;
-	entry->prev = LLIST_POISON2;
-}
-
-/**
- * llist_del_init - deletes entry from llist and reinitialize it.
- * @entry: the element to delete from the llist.
- */
-void llist_del_init(llist_head *entry)
-{
-	__llist_del(entry->prev, entry->next);
-	INIT_LLIST_HEAD(entry);
-}
-
-/**
- * llist_move - delete from one llist and add as another's head
- * @llist: the entry to move
- * @head: the head that will precede our entry
- */
-void llist_move(llist_head *llist, llist_head *head)
-{
-        __llist_del(llist->prev, llist->next);
-        llist_add(llist, head);
-}
-
-/**
- * llist_move_tail - delete from one llist and add as another's tail
- * @llist: the entry to move
- * @head: the head that will follow our entry
- */
-void llist_move_tail(llist_head *llist,
-				  llist_head *head)
-{
-        __llist_del(llist->prev, llist->next);
-        llist_add_tail(llist, head);
-}
-
-void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData)
-{
-	llist_head *node = head->next;
-	llist_head *del;
-	int32_t result;
-	while (!llist_empty(head) && (node != head))
-	{
-		result = cb((void *)node, pData);
-		if (result > 0)
-		{
-			return node;
-		}
-		else
-		{
-			del = node;
-			node = node->next;
-			if (result < 0)
-			{
-				llist_del(del);
-				free(del);
-			}
-		}
-	}
-	return NULL;
-}
-
-/**
- * llist_empty - tests whether a llist is empty
- * @head: the llist to test.
- */
-int llist_empty(const llist_head *head)
-{
-	return head->next == head;
-}
-
-uint32_t llist_num(const llist_head *head)
-{
-	llist_head *node = head->next;
-	uint32_t num = 0;
-	if (!node)
-		return num;
-	while(node != head)
-	{
-		num++;
-		node = node->next;
-	}
-	return num;
-}
-
-#define PP_HTONS(x) ((uint16_t)((((x) & (uint16_t)0x00ffU) << 8) | (((x) & (uint16_t)0xff00U) >> 8)))
-#define PP_NTOHS(x) PP_HTONS(x)
-#define PP_HTONL(x) ((((x) & (uint32_t)0x000000ffUL) << 24) | \
-                     (((x) & (uint32_t)0x0000ff00UL) <<  8) | \
-                     (((x) & (uint32_t)0x00ff0000UL) >>  8) | \
-                     (((x) & (uint32_t)0xff000000UL) >> 24))
-#define PP_NTOHL(x) PP_HTONL(x)
-
-uint16_t BSP_Swap16(uint16_t n)
-{
-  return (uint16_t)PP_HTONS(n);
-}
-
-uint32_t BSP_Swap32(uint32_t n)
-{
-  return (uint32_t)PP_HTONL(n);
-}
-
-uint32_t utf8_to_unicode(uint8_t *string, uint32_t len, void *out, uint8_t is_only_16)
-{
-	uint32_t i = 0;
-	uint32_t result = 0;
-	uint8_t bit, n;
-	if (is_only_16)
-	{
-		uint16_t *buf = (uint16_t *)out;
-		while (i < len)
-		{
-			if (string[i] & 0x80)
-			{
-				if (!(string[i] & (1 << 5)))
-				{
-					n = 2;
-					buf[result] = string[i] & ((1 << 5) - 1);
-				}
-				else
-				{
-					buf[result] = string[i] & ((1 << 4) - 1);
-					n = 3;
-				}
-
-				for (bit = 1; bit < n; bit++)
-				{
-					buf[result] = (buf[result] << 6) | (string[i + bit] & 0x3f);
-				}
-				i += n;
-			}
-			else
-			{
-				buf[result] = string[i];
-				i++;
-			}
-			result++;
-		}
-	}
-	else
-	{
-		uint8_t table[7] = {0, 0, 0x1f, 0x0f, 0x07, 0x03, 0x01};
-		uint32_t *buf = (uint32_t *)out;
-		while (i < len)
-		{
-			if (string[i] & 0x80)
-			{
-				n = 7;
-				for (bit = 5; bit >= 1; bit--)
-				{
-					if (!(string[i] & (1 << bit)))
-					{
-						n -= bit;
-						break;
-					}
-				}
-				if (n >= 7)
-				{
-					return result;
-				}
-				buf[result] = string[i] & table[n];
-
-				for (bit = 1; bit < n; bit++)
-				{
-					buf[result] = (buf[result] << 6) | (string[i + bit] & 0x3f);
-				}
-				i += n;
-			}
-			else
-			{
-				buf[result] = string[i];
-				i++;
-			}
-			result++;
-		}
-	}
-	return result;
-}
-
-uint32_t unicode_to_utf8(void *in, uint32_t unicodelen, uint8_t *out, uint8_t is_only_16)
-{
-	uint32_t i = 0;
-	uint32_t result = 0;
-	uint8_t bit, n;
-	if (is_only_16)
-	{
-		uint16_t *buf = (uint16_t *)in;
-		while (i < unicodelen)
-		{
-			if (buf[i] <= 0x007f)
-			{
-				out[result] = buf[i];
-
-				result++;
-			}
-			else
-			{
-				if (buf[i] >> 12)
-				{
-					out[result + 2] = (buf[i] & 0x3f) | 0x80;
-					out[result + 1] = ((buf[i] >> 6) & 0x3f) | 0x80;
-					out[result] = 0xe0 | (buf[i] >> 12);
-					result += 3;
-				}
-				else
-				{
-					out[result + 1] = (buf[i] & 0x3f) | 0x80;
-					out[result] = 0xc0 | (buf[i] >> 6);
-					result += 2;
-				}
-			}
-			i++;
-		}
-
-	}
-	else
-	{
-		uint8_t table[7] = {0,0,0xc0,0xe0, 0xf0, 0xf8, 0xfc};
-		uint8_t pos[7] = {0,0,6,12,18,24,30};
-		uint32_t *buf = (uint32_t *)in;
-		while (i < unicodelen)
-		{
-			if (buf[i] <= 0x007f)
-			{
-				out[result] = buf[i];
-
-				result++;
-			}
-			else
-			{
-				n = 6;
-				for (bit = 1; bit < 6; bit++)
-				{
-					if (!(buf[i] >> ((bit + 1) * 6)))
-					{
-						n = bit + 1;
-						break;
-					}
-				}
-				out[result] = table[n] | (buf[i] >> pos[n]);
-				for (bit = 1; bit < n; bit++)
-				{
-					out[result + bit] = ( (buf[i] >> ((n - bit - 1) * 6)) & 0x3f) | 0x80;
-				}
-				result += n;
-			}
-			i++;
-		}
-	}
-	return result;
-}
+/*
+ * Copyright (c) 2022 OpenLuat & AirM2M
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "c_common.h"
+#include "luat_malloc.h"
+#include "luat_base.h"
+#include "luat_log.h"
+
+#define COMMON_LOG_BUF_SIZE 255
+static char common_log_buf[COMMON_LOG_BUF_SIZE];
+const uint8_t ByteToAsciiTable[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+
+
+LUAT_WEAK void DBG_Printf(const char* format, ...){
+    va_list args;
+    va_start(args, format);
+    int len = vsnprintf(common_log_buf, sizeof(common_log_buf), format, args);
+	luat_log_write(common_log_buf, len);
+    va_end(args);
+}
+
+LUAT_WEAK void DBG_HexPrintf(void *Data, unsigned int len){
+	uint8_t *data = (uint8_t *)Data;
+	uint8_t *uart_buf;
+    uint32_t i,j;
+    j = 0;
+    if (!len) return;
+    uart_buf = luat_heap_zalloc(len * 3 + 2);
+    if (!uart_buf) return;
+    for (i = 0; i < len; i++){
+		uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
+		uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
+		uart_buf[j++] = ' ';
+    }
+    uart_buf[j++] = '\r';
+    uart_buf[j++] = '\n';
+	luat_log_write((char*)uart_buf, len * 3 + 2);
+	luat_heap_free(uart_buf);
+}
+
+void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize)
+{
+	uint8_t *Data = (uint8_t *)Src;
+	Buf->Data = Data;
+	Buf->Len = 0;
+	Buf->MaxLength = MaxLen;
+	Buf->Offset = 0;
+	Buf->DataSize = DataSize;
+}
+
+uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len)
+{
+	uint32_t i, p;
+	uint8_t *Data = (uint8_t *)Src;
+	if (Buf->Len < Len)
+	{
+		Len = Buf->Len;
+	}
+	if (Buf->DataSize > 1)
+	{
+		for (i = 0, p = Buf->Offset; i < Len; i++, p++)
+		{
+			if (p >= Buf->MaxLength)
+			{
+				p -= Buf->MaxLength;
+			}
+			memcpy(Data + (i * Buf->DataSize), Buf->Data + (p * Buf->DataSize), Buf->DataSize);
+		}
+	}
+	else
+	{
+		for (i = 0, p = Buf->Offset; i < Len; i++, p++)
+		{
+			if (p >= Buf->MaxLength)
+			{
+				p -= Buf->MaxLength;
+			}
+			Data[i] = Buf->Data[p];
+		}
+	}
+
+	return Len;
+}
+
+uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len)
+{
+	uint32_t l;
+	uint8_t *Data = (uint8_t *)Src;
+	l = LoopBuffer_Query(Buf, Data, Len);
+	Buf->Len -= l;
+	Buf->Offset += l;
+	if (Buf->Offset >= Buf->MaxLength)
+	{
+		Buf->Offset -= Buf->MaxLength;
+
+	}
+	if (!Buf->Len) {
+		Buf->Offset = 0;
+	}
+	return l;
+}
+
+void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len)
+{
+	if (Buf->Len < Len)
+	{
+		Len = Buf->Len;
+	}
+
+	Buf->Len -= Len;
+	Buf->Offset += Len;
+	if (Buf->Offset >= Buf->MaxLength)
+	{
+		Buf->Offset -= Buf->MaxLength;
+	}
+
+	if (!Buf->Len) {
+		Buf->Offset = 0;
+	}
+}
+
+uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len)
+{
+	uint32_t i, p, cut_off = 0;
+	uint8_t *Data = (uint8_t *)Src;
+	if (!Buf->Len && !Buf->Offset && (Len <= Buf->Len))
+	{
+		memcpy(Buf->Data, Data, Len);
+		Buf->Len = Len;
+		return Len;
+	}
+	cut_off = Buf->MaxLength - Buf->Len;
+	if (cut_off >= Len)
+	{
+		cut_off = 0;
+	}
+	else
+	{
+		cut_off = Len - cut_off;
+	}
+
+	if (Buf->DataSize > 1)
+	{
+		for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
+		{
+			if (p >= Buf->MaxLength)
+			{
+				p -= Buf->MaxLength;
+			}
+			memcpy(Buf->Data + (p * Buf->DataSize), Data + (i * Buf->DataSize), Buf->DataSize);
+		}
+	}
+	else
+	{
+		for (i = 0, p = Buf->Offset + Buf->Len; i < Len; i++, p++)
+		{
+			if (p >= Buf->MaxLength)
+			{
+				p -= Buf->MaxLength;
+			}
+
+			Buf->Data[p] = Data[i];
+		}
+	}
+
+
+	Buf->Offset += cut_off;
+	if (Buf->Offset >= Buf->MaxLength)
+		Buf->Offset -= Buf->MaxLength;
+
+	Buf->Len += Len;
+	if (Buf->Len > Buf->MaxLength)
+		Buf->Len = Buf->MaxLength;
+
+	return Len;
+
+}
+
+
+void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen)
+{
+	Buf->Data = Src;
+	Buf->Pos = 0;
+	Buf->MaxLen = MaxLen;
+}
+
+int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
+{
+	if (!Len)
+	{
+		return -1;
+	}
+	if (!Buf)
+	{
+		return -1;
+	}
+	if ((Buf->Pos + Len) > Buf->MaxLen)
+	{
+		Len = Buf->MaxLen - Buf->Pos;
+	}
+	if (Len)
+	{
+		memcpy(&Buf->Data[Buf->Pos], Data, Len);
+	}
+	Buf->Pos += Len;
+	return Len;
+}
+
+void DBuffer_Init(DBuffer_Struct *DBuf, uint32_t Size)
+{
+	memset(DBuf, 0, sizeof(DBuffer_Struct));
+	DBuf->pCache[0] = malloc(Size);
+	DBuf->pCache[1] = malloc(Size);
+	DBuf->MaxLen = Size;
+}
+
+void DBuffer_ReInit(DBuffer_Struct *DBuf, uint32_t Size)
+{
+	if (DBuf->pCache[0]) free(DBuf->pCache[0]);
+	if (DBuf->pCache[1]) free(DBuf->pCache[1]);
+	DBuffer_Init(DBuf, Size);
+}
+
+void DBuffer_DeInit(DBuffer_Struct *DBuf)
+{
+	free(DBuf->pCache[0]);
+	free(DBuf->pCache[1]);
+	DBuf->pCache[0] = NULL;
+	DBuf->pCache[1] = NULL;
+	DBuf->MaxLen = 0;
+}
+
+void *DBuffer_GetCache(DBuffer_Struct *DBuf, uint8_t IsCurrent)
+{
+	return DBuf->pCache[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn];
+}
+
+void DBuffer_SwapCache(DBuffer_Struct *DBuf)
+{
+	DBuf->CurCacheSn = !DBuf->CurCacheSn;
+}
+
+void DBuffer_SetDataLen(DBuffer_Struct *DBuf, uint32_t Len, uint8_t IsCurrent)
+{
+	DBuf->pCacheLen[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn] = Len;
+}
+
+uint32_t DBuffer_GetDataLen(DBuffer_Struct *DBuf, uint8_t IsCurrent)
+{
+	return DBuf->pCacheLen[IsCurrent?DBuf->CurCacheSn:!DBuf->CurCacheSn];
+}
+
+//void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len)
+//{
+//	uint32_t RestLen;
+//	uint32_t i;
+//	if (!Buf)
+//		return ;
+//	if (!Buf->Data)
+//		return ;
+//	if (Len >= Buf->Pos)
+//	{
+//		Buf->Pos = 0;
+//		return ;
+//	}
+//	RestLen = Buf->Pos - Len;
+//	memmove(Buf->Data, Buf->Data + Len, RestLen);
+//	Buf->Pos = RestLen;
+//}
+
+
+/*****************************************************************************
+* FUNCTION
+*   command_parse_param()
+* DESCRIPTION
+*    Parse AT command string to parameters
+* PARAMETERS
+*   char* pStr
+* RETURNS
+*  pCmdParam
+*****************************************************************************/
+uint32_t CmdParseParam(int8_t* pStr, CmdParam *CP, int8_t Cut)
+{
+	uint32_t paramStrLen = strlen((char *)pStr);
+	uint32_t paramIndex = 0;
+	uint32_t paramCharIndex = 0;
+	uint32_t index = 0;
+
+	while ((pStr[index] != '\r')
+		&& (index < paramStrLen)
+		&& (paramIndex < CP->param_max_num)) {
+		if (pStr[index] == Cut) {
+			/* Next param string */
+			paramCharIndex = 0;
+			paramIndex++;
+		}
+		else {
+			if (pStr[index] != '"')
+			{
+				if (paramCharIndex >= CP->param_max_len)
+					return (0);
+
+				/*Get each of command param char, the param char except char ' " '*/
+				CP->param_str[paramIndex * CP->param_max_len + paramCharIndex] = pStr[index];
+				paramCharIndex++;
+			}
+		}
+		index++;
+	}
+
+	CP->param_num = paramIndex + 1;
+
+	return (1);
+}
+
+
+LUAT_WEAK int32_t OS_InitBuffer(Buffer_Struct *Buf, uint32_t Size)
+{
+	if (!Buf)
+		return 0;
+	Buf->Data = luat_heap_malloc(Size);
+	memset(Buf->Data, 0, Size);
+	if (!Buf->Data)
+	{
+		Buf->MaxLen = 0;
+		Buf->Pos = 0;
+		return 0;
+	}
+	Buf->MaxLen = Size;
+	Buf->Pos = 0;
+	return Size;
+}
+
+LUAT_WEAK void OS_DeInitBuffer(Buffer_Struct *Buf)
+{
+	if (Buf->Data)
+	{
+		luat_heap_free(Buf->Data);
+	}
+	Buf->Data = NULL;
+	Buf->MaxLen = 0;
+	Buf->Pos = 0;
+}
+
+LUAT_WEAK int32_t OS_ReInitBuffer(Buffer_Struct *Buf, uint32_t Size)
+{
+	if (!Buf)
+		return 0;
+
+	if (Buf->Data)
+	{
+		luat_heap_free(Buf->Data);
+	}
+	Buf->Data = luat_heap_malloc(Size);
+	memset(Buf->Data, 0, Size);
+	if (!Buf->Data)
+	{
+		Buf->MaxLen = 0;
+		Buf->Pos = 0;
+		return 0;
+	}
+	Buf->MaxLen = Size;
+	Buf->Pos = 0;
+	return Size;
+}
+
+LUAT_WEAK int32_t OS_ReSizeBuffer(Buffer_Struct *Buf, uint32_t Size)
+{
+//	uint8_t *Old;
+	uint8_t *New;
+
+	if (!Buf)
+		return 0;
+
+//	Old = Buf->Data;
+//	if (Size < Buf->Pos)
+//	{
+//		Size = Buf->Pos;
+//	}
+//	New = OS_Zalloc(Size);
+//	if (!New)
+//	{
+//		return 0;
+//	}
+//	if (Old)
+//	{
+//		memcpy(New, Old, Buf->Pos);
+//		OS_Free(Old);
+//	}
+	New = luat_heap_realloc(Buf->Data, Size);
+	if (New)
+	{
+		Buf->Data = New;
+		Buf->MaxLen = Size;
+	}
+	return Size;
+}
+
+LUAT_WEAK int32_t OS_BufferWrite(Buffer_Struct *Buf, void *Data, uint32_t Len)
+{
+	uint32_t WriteLen;
+	if (!Len)
+	{
+		return ERROR_NONE;
+	}
+	if (!Buf)
+	{
+		return -ERROR_PARAM_INVALID;
+	}
+	if (!Buf->Data)
+	{
+		Buf->Data = luat_heap_malloc(Len);
+		memset(Buf->Data, 0, Len);
+		if (!Buf->Data)
+		{
+			return -ERROR_NO_MEMORY;
+		}
+		Buf->Pos = 0;
+		Buf->MaxLen = Len;
+	}
+	WriteLen = Buf->Pos + Len;
+	if (WriteLen > Buf->MaxLen)
+	{
+		if (!OS_ReSizeBuffer(Buf, WriteLen))
+		{
+			return -ERROR_NO_MEMORY;
+		}
+	}
+	memcpy(&Buf->Data[Buf->Pos], Data, Len);
+	Buf->Pos += Len;
+	return ERROR_NONE;
+}
+
+LUAT_WEAK int32_t OS_BufferWriteLimit(Buffer_Struct *Buf, void *Data, uint32_t Len)
+{
+	uint32_t WriteLen;
+	if (!Len)
+	{
+		return ERROR_NONE;
+	}
+	if (!Buf)
+	{
+		return -ERROR_PARAM_INVALID;
+	}
+	if (!Buf->Data)
+	{
+		Buf->Data = luat_heap_malloc(Len);
+		memset(Buf->Data, 0, Len);
+		if (!Buf->Data)
+		{
+			return -ERROR_NO_MEMORY;
+		}
+		Buf->Pos = 0;
+		Buf->MaxLen = Len;
+	}
+	WriteLen = Buf->Pos + Len;
+	if (WriteLen > Buf->MaxLen)
+	{
+		return -ERROR_NO_MEMORY;
+	}
+	memcpy(&Buf->Data[Buf->Pos], Data, Len);
+	Buf->Pos += Len;
+	return ERROR_NONE;
+}
+
+LUAT_WEAK void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len)
+{
+	uint32_t RestLen;
+	uint32_t i;
+	if (!Buf)
+		return ;
+	if (!Buf->Data)
+		return ;
+	if (Len >= Buf->Pos)
+	{
+		Buf->Pos = 0;
+		return ;
+	}
+	RestLen = Buf->Pos - Len;
+	memmove(Buf->Data, Buf->Data + Len, RestLen);
+	Buf->Pos = RestLen;
+}
+
+
+
+int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value)
+{
+	uint32_t Mask,Pos1,Pos2;
+
+	Pos1 = Sn/8;
+	Pos2 = Sn%8;
+
+	Mask = ~(1 << Pos2);
+	if (Value)
+	{
+		Value = (1 << Pos2);
+	}
+	Data[Pos1] = (Data[Pos1] & Mask) | Value;
+	//DBG("%d %d %d %d", Sn, Pos1, Pos2, Value);
+	return 0;
+}
+
+int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value)
+{
+	uint32_t Mask,Pos1,Pos2;
+
+	Pos1 = Sn/8;
+	Pos2 = Sn%8;
+	Mask = (1 << Pos2);
+	if (Data[Pos1] & Mask)
+	{
+		*Value = 1;
+	}
+	else
+	{
+		*Value = 0;
+	}
+	return -1;
+}
+
+uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn)
+{
+	uint32_t Mask,Pos1,Pos2;
+
+	Pos1 = Sn/8;
+	Pos2 = Sn%8;
+	Mask = (1 << Pos2);
+	if (Data[Pos1] & Mask)
+	{
+		return 1;
+	}
+	return 0;
+}
+
+uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart)
+{
+	uint8_t Check = CheckStart;
+	uint8_t *Data = (uint8_t *)Src;
+	uint32_t i;
+	for (i = 0; i < Len; i++)
+	{
+		Check ^= Data[i];
+	}
+	return Check;
+}
+
+uint8_t SumCheck(uint8_t *Data, uint32_t Len)
+{
+	uint8_t Check = 0;
+	uint32_t i;
+	for (i = 0; i < Len; i++)
+	{
+		Check += Data[i];
+	}
+	return Check;
+}
+
+
+uint8_t CRC8Cal(void *Data, uint16_t Len, uint8_t CRC8Last, uint8_t CRCRoot, uint8_t IsReverse)
+{
+	uint16_t i;
+	uint8_t CRC8 = CRC8Last;
+	uint8_t wTemp = CRCRoot;
+	uint8_t *Src = (uint8_t *)Data;
+	if (IsReverse)
+	{
+		CRCRoot = 0;
+		for (i = 0; i < 8; i++)
+		{
+			if (wTemp & (1 << (7 - i)))
+			{
+				CRCRoot |= 1 << i;
+			}
+		}
+		while (Len--)
+		{
+
+			CRC8 ^= *Src++;
+			for (i = 0; i < 8; i++)
+			{
+				if ((CRC8 & 0x01))
+				{
+					CRC8 >>= 1;
+					CRC8 ^= CRCRoot;
+				}
+				else
+				{
+					CRC8 >>= 1;
+				}
+			}
+		}
+	}
+	else
+	{
+		while (Len--)
+		{
+
+			CRC8 ^= *Src++;
+			for (i = 8; i > 0; --i)
+			{
+				if ((CRC8 & 0x80))
+				{
+					CRC8 <<= 1;
+					CRC8 ^= CRCRoot;
+				}
+				else
+				{
+					CRC8 <<= 1;
+				}
+			}
+		}
+	}
+	return CRC8;
+}
+
+/************************************************************************/
+/*  CRC16                                                                */
+/************************************************************************/
+uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse)
+{
+	uint16_t i;
+	uint16_t CRC16 = CRC16Last;
+	uint16_t wTemp = CRCRoot;
+	uint8_t *Src = (uint8_t *)Data;
+	if (IsReverse)
+	{
+		CRCRoot = 0;
+		for (i = 0; i < 16; i++)
+		{
+			if (wTemp & (1 << (15 - i)))
+			{
+				CRCRoot |= 1 << i;
+			}
+		}
+		while (Len--)
+		{
+			for (i = 0; i < 8; i++)
+			{
+				if ((CRC16 & 0x0001) != 0)
+				{
+					CRC16 >>= 1;
+					CRC16 ^= CRCRoot;
+				}
+				else
+				{
+					CRC16 >>= 1;
+				}
+				if ((*Src&(1 << i)) != 0)
+				{
+					CRC16 ^= CRCRoot;
+				}
+			}
+			Src++;
+		}
+	}
+	else
+	{
+		while (Len--)
+		{
+			for (i = 8; i > 0; i--)
+			{
+				if ((CRC16 & 0x8000) != 0)
+				{
+					CRC16 <<= 1;
+					CRC16 ^= CRCRoot;
+				}
+				else
+				{
+					CRC16 <<= 1;
+				}
+				if ((*Src&(1 << (i - 1))) != 0)
+				{
+					CRC16 ^= CRCRoot;
+				}
+			}
+			Src++;
+		}
+	}
+
+	return CRC16;
+}
+
+uint32_t AsciiToU32(uint8_t *Src, uint32_t Len)
+{
+	uint32_t i = 0;
+	uint32_t Temp = 0;
+	for (i = 0; i < Len; i++)
+	{
+
+		if (Src[i])
+		{
+			Temp *= 10;
+			Temp += Src[i] - '0';
+		}
+		else
+		{
+			break;
+		}
+	}
+	return Temp;
+}
+
+
+/**
+* @brief  反转数据
+* @param  ref 需要反转的变量
+* @param	ch 反转长度,多少位
+* @retval N反转后的数据
+*/
+static LongInt Reflect(LongInt ref, uint8_t ch)
+{
+	LongInt value = 0;
+	LongInt i;
+	for (i = 1; i < (LongInt)(ch + 1); i++)
+	{
+		if (ref & 1)
+			value |= (LongInt)1 << (ch - i);
+		ref >>= 1;
+	}
+	return value;
+}
+
+/**
+* @brief  建立CRC32的查询表
+* @param  Tab 表缓冲
+* @param	Gen CRC32根
+* @retval None
+*/
+void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen)
+{
+	uint32_t crc;
+	uint32_t i, j, temp, t1, t2, flag;
+	if (Tab[1] != 0)
+		return;
+	for (i = 0; i < 256; i++)
+	{
+		temp = Reflect(i, 8);
+		Tab[i] = temp << 24;
+		for (j = 0; j < 8; j++)
+		{
+			flag = Tab[i] & 0x80000000;
+			t1 = Tab[i] << 1;
+			if (0 == flag)
+			{
+				t2 = 0;
+			}
+			else
+			{
+				t2 = Gen;
+			}
+			Tab[i] = t1 ^ t2;
+		}
+		crc = Tab[i];
+		Tab[i] = Reflect(crc, 32);
+	}
+}
+
+
+/**
+* @brief  计算buffer的crc校验码
+* @param  CRC32_Table CRC32表
+* @param  Buf 缓冲
+* @param	Size 缓冲区长度
+* @param	CRC32 初始CRC32值
+* @retval 计算后的CRC32
+*/
+uint32_t CRC32_Cal(uint32_t *CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last)
+{
+	uint32_t i;
+	for (i = 0; i < Size; i++)
+	{
+		CRC32Last = CRC32_Table[(CRC32Last ^ Buf[i]) & 0xff] ^ (CRC32Last >> 8);
+	}
+	return CRC32Last;
+}
+
+
+/************************************************************************/
+/*时间与时间戳转换,C语言实现                                                                    */
+/************************************************************************/
+/************************************************************************/
+uint8_t IsLeapYear(uint32_t Year)
+{
+	if ((Year % 400) == 0)
+		return 1;
+	if ((((Year % 4) == 0) && (Year % 100) != 0))
+		return 1;
+	else
+		return 0;
+}
+
+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 } };
+//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 } };
+LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time)
+{
+
+	LongInt DYear, DDay, DSec;
+	uint32_t Year100;
+	DYear = Date->Year - 1970;
+	if (DYear)	//1970年以后,1972是第一个闰年,1973年是第一年需要增加一天,2100年是非闰年
+	{
+		DDay = DYear * 365 + ((DYear + 1) / 4) + DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
+//		if (IsLeapYear(Date->Year))
+//		{
+//			DDay--;
+//		}
+		if (Date->Year >= 2100)
+		{
+			Year100 = Date->Year - 2100;
+			DDay -= (1 + Year100 / 100);
+			if (Date->Year >= 2400)
+			{
+				Year100 = Date->Year - 2400;
+				DDay += 1 + Year100 / 400;
+			}
+
+		}
+
+	}
+	else
+	{
+		DDay = DayTable[IsLeapYear(Date->Year)][Date->Mon - 1] + (Date->Day - 1);
+	}
+	DSec = DDay * 86400 + Time->Hour * 3600 + Time->Min * 60 + Time->Sec;
+	return DSec;
+}
+#define YEAR_1_DAY_BEFORE2000		365
+#define YEAR_2_DAY_BEFORE2000		730
+#define YEAR_3_DAY_BEFORE2000		1096
+
+
+#define YEAR_1_DAY_AFTER2000		365
+#define YEAR_2_DAY_AFTER2000		730
+#define YEAR_3_DAY_AFTER2000		1095
+
+#define YEAR_4_DAY		1461
+#define YEAR_31_DAY		11323
+
+#define YEAR_100_DAY	36524
+#define YEAR_400_DAY	146097
+
+uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay)
+{
+
+	uint32_t DYear,i, LeapFlag, Temp;
+	uint32_t DDay;
+	DDay = Sec / 86400;
+	if (DDay != LastDDay)
+	{
+		DYear = 0;
+		Time->Week = (4 + DDay) % 7;
+		if (DDay >= YEAR_31_DAY)
+		{
+			DDay -= YEAR_31_DAY;
+			DYear = 31;
+
+			if (DDay >= YEAR_400_DAY)
+			{
+				Temp = DDay / YEAR_400_DAY;
+				DYear += Temp * 400;
+				DDay -= Temp * YEAR_400_DAY;
+			}
+
+			if (DDay >= YEAR_100_DAY)
+			{
+				Temp = DDay / YEAR_100_DAY;
+				DYear += Temp * 100;
+				DDay -= Temp * YEAR_100_DAY;
+			}
+
+			if (DDay >= YEAR_4_DAY)
+			{
+				Temp = DDay / YEAR_4_DAY;
+				DYear += Temp * 4;
+				DDay -= Temp * YEAR_4_DAY;
+			}
+
+			if (DDay >= YEAR_3_DAY_AFTER2000)
+			{
+				DYear += 3;
+				DDay -= YEAR_3_DAY_AFTER2000;
+			}
+			else if (DDay >= YEAR_2_DAY_AFTER2000)
+			{
+				DYear += 2;
+				DDay -= YEAR_2_DAY_AFTER2000;
+			}
+			else if (DDay >= YEAR_1_DAY_AFTER2000)
+			{
+				DYear += 1;
+				DDay -= YEAR_1_DAY_AFTER2000;
+			}
+
+		}
+		else
+		{
+			if (DDay >= YEAR_4_DAY)
+			{
+				Temp = DDay / YEAR_4_DAY;
+				DYear += Temp * 4;
+				DDay -= Temp * YEAR_4_DAY;
+			}
+
+			if (DDay >= YEAR_3_DAY_BEFORE2000)
+			{
+				DYear += 3;
+				DDay -= YEAR_3_DAY_BEFORE2000;
+			}
+			else if (DDay >= YEAR_2_DAY_BEFORE2000)
+			{
+				DYear += 2;
+				DDay -= YEAR_2_DAY_BEFORE2000;
+			}
+			else if (DDay >= YEAR_1_DAY_BEFORE2000)
+			{
+				DYear += 1;
+				DDay -= YEAR_1_DAY_BEFORE2000;
+			}
+		}
+
+		Date->Year = DYear + 1970;
+		LeapFlag = IsLeapYear(Date->Year);
+		Date->Mon = 12;
+		for (i = 1; i < 12; i++)
+		{
+			if (DDay < DayTable[LeapFlag][i])
+			{
+				Date->Mon = i;
+				break;
+			}
+		}
+		Date->Day = DDay - DayTable[LeapFlag][Date->Mon - 1] + 1;
+	}
+
+	Sec = Sec % 86400;
+	Time->Hour = Sec / 3600;
+	Sec = Sec % 3600;
+	Time->Min = Sec / 60;
+	Time->Sec = Sec % 60;
+	return DDay;
+}
+
+
+/**
+ * \brief get a byte (8bits) from a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer
+ * \return              the byte value
+ */
+uint8_t BytesGet8(const void *ptr)
+{
+    const uint8_t *p = (const uint8_t *)ptr;
+    return p[0];
+}
+
+/**
+ * \brief put a byte (8bits) to a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer
+ * \param v             the byte value
+ */
+void BytesPut8(void *ptr, uint8_t v)
+{
+    uint8_t *p = (uint8_t *)ptr;
+    p[0] = v;
+}
+
+/**
+ * \brief get a big endian short (16bits) from a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \return              the short value
+ */
+uint16_t BytesGetBe16(const void *ptr)
+{
+    const uint8_t *p = (const uint8_t *)ptr;
+    return (p[0] << 8) | p[1];
+}
+
+/**
+ * \brief put a big endian short (16bits) to a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \param v             the short value
+ */
+void BytesPutBe16(void *ptr, uint16_t v)
+{
+    uint8_t *p = (uint8_t *)ptr;
+    p[0] = (v >> 8) & 0xff;
+    p[1] = v & 0xff;
+}
+
+/**
+ * \brief get a big endian word (32bits) from a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \return              the word value
+ */
+uint32_t BytesGetBe32(const void *ptr)
+{
+    const uint8_t *p = (const uint8_t *)ptr;
+    return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
+}
+
+/**
+ * \brief put a big endian word (32bits) to a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \param v             the word value
+ */
+void BytesPutBe32(void *ptr, uint32_t v)
+{
+    uint8_t *p = (uint8_t *)ptr;
+    p[0] = (v >> 24) & 0xff;
+    p[1] = (v >> 16) & 0xff;
+    p[2] = (v >> 8) & 0xff;
+    p[3] = v & 0xff;
+}
+
+/**
+ * \brief get a little endian short (16bits) from a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \return              the short value
+ */
+uint16_t BytesGetLe16(const void *ptr)
+{
+    const uint8_t *p = (const uint8_t *)ptr;
+    return p[0] | (p[1] << 8);
+}
+
+/**
+ * \brief put a little endian short (16bits) to a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \param v             the short value
+ */
+void BytesPutLe16(void *ptr, uint16_t v)
+{
+    uint8_t *p = (uint8_t *)ptr;
+    p[0] = v & 0xff;
+    p[1] = (v >> 8) & 0xff;
+}
+
+/**
+ * \brief get a little endian word (32bits) from a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \return              the word value
+ */
+uint32_t BytesGetLe32(const void *ptr)
+{
+    const uint8_t *p = (const uint8_t *)ptr;
+    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
+}
+
+/**
+ * \brief put a little endian word (32bits) to a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \param v             the word value
+ */
+void BytesPutLe32(void *ptr, uint32_t v)
+{
+    uint8_t *p = (uint8_t *)ptr;
+    p[0] = v & 0xff;
+    p[1] = (v >> 8) & 0xff;
+    p[2] = (v >> 16) & 0xff;
+    p[3] = (v >> 24) & 0xff;
+}
+
+/**
+ * \brief get a little endian long long (64bits) from a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \return              the long long value
+ */
+uint64_t BytesGetLe64(const void *ptr)
+{
+    const uint8_t *p = (const uint8_t *)ptr;
+    return BytesGetLe32(p) | ((uint64_t)BytesGetLe32(p + 4) << 32);
+}
+
+/**
+ * \brief put a little endian long long (64bits) to a pointer
+ *
+ * Caller should ensure parameters are valid.
+ *
+ * \param ptr           the pointer, may be unaligned
+ * \param v             the long long value
+ */
+void BytesPutLe64(void *ptr, uint64_t v)
+{
+    uint8_t *p = (uint8_t *)ptr;
+    BytesPutLe32(p, v & 0xffffffff);
+    BytesPutLe32(p + 4, (v >> 32) & 0xffffffff);
+}
+
+uint8_t BytesGet8FromBuf(Buffer_Struct *Buf)
+{
+	Buf->Pos++;
+    return Buf->Data[Buf->Pos - 1];
+}
+
+
+void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v)
+{
+	Buf->Data[Buf->Pos] = v;
+	Buf->Pos++;
+}
+
+uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf)
+{
+	Buf->Pos += 2;
+    return (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
+}
+
+void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v)
+{
+	Buf->Data[Buf->Pos] = (v >> 8) & 0xff;
+	Buf->Data[Buf->Pos + 1] = v & 0xff;
+	Buf->Pos += 2;
+}
+
+uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf)
+{
+	Buf->Pos += 4;
+    return (Buf->Data[Buf->Pos - 4] << 24) | (Buf->Data[Buf->Pos - 3] << 16) | (Buf->Data[Buf->Pos - 2] << 8) | Buf->Data[Buf->Pos - 1];
+}
+
+void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v)
+{
+	Buf->Data[Buf->Pos] = (v >> 24) & 0xff;
+	Buf->Data[Buf->Pos + 1] = (v >> 16) & 0xff;
+	Buf->Data[Buf->Pos + 2] = (v >> 8) & 0xff;
+	Buf->Data[Buf->Pos + 3] = v & 0xff;
+	Buf->Pos += 4;
+}
+
+
+uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf)
+{
+	Buf->Pos += 2;
+    return Buf->Data[Buf->Pos - 2] | (Buf->Data[Buf->Pos - 1] << 8);
+}
+
+void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v)
+{
+	Buf->Data[Buf->Pos] = v & 0xff;
+    Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
+    Buf->Pos+= 2;
+}
+
+uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf)
+{
+	Buf->Pos += 4;
+    return Buf->Data[Buf->Pos - 4] | (Buf->Data[Buf->Pos - 3] << 8) | (Buf->Data[Buf->Pos - 2] << 16) | (Buf->Data[Buf->Pos - 1] << 24);
+}
+
+void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v)
+{
+	Buf->Data[Buf->Pos] = v & 0xff;
+	Buf->Data[Buf->Pos + 1] = (v >> 8) & 0xff;
+	Buf->Data[Buf->Pos + 2] = (v >> 16) & 0xff;
+	Buf->Data[Buf->Pos + 3] = (v >> 24) & 0xff;
+	Buf->Pos += 4;
+}
+
+uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf)
+{
+	uint64_t Temp = BytesGetLe32FromBuf(Buf);
+    return Temp | ((uint64_t)BytesGetLe32FromBuf(Buf) << 32);
+}
+
+void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v)
+{
+
+	BytesPutLe32ToBuf(Buf, v & 0xffffffff);
+	BytesPutLe32ToBuf(Buf, (v >> 32) & 0xffffffff);
+}
+
+float BytesGetFloatFromBuf(Buffer_Struct *Buf)
+{
+	float Temp;
+	Buf->Pos += 4;
+	memcpy(&Temp, &Buf->Data[Buf->Pos - 4], 4);
+    return Temp;
+}
+
+void BytesPutFloatToBuf(Buffer_Struct *Buf, float v)
+{
+	memcpy(&Buf->Data[Buf->Pos], &v, 4);
+	Buf->Pos += 4;
+}
+
+double BytesGetDoubleFromBuf(Buffer_Struct *Buf)
+{
+	double Temp;
+	Buf->Pos += 8;
+	memcpy(&Temp, &Buf->Data[Buf->Pos - 8], 8);
+    return Temp;
+}
+
+void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v)
+{
+	memcpy(&Buf->Data[Buf->Pos], &v, 8);
+	Buf->Pos += 8;
+}
+
+void BytesGetMemoryFromBuf(Buffer_Struct *Buf, uint8_t *Data, uint32_t Len)
+{
+	memcpy(Data, &Buf->Data[Buf->Pos], Len);
+	Buf->Pos += Len;
+}
+
+/*
+ * 转义打包
+ * 标识Flag,即包头包尾加入Flag
+ * 数据中遇到Flag -> Code F1
+ * 数据中遇到Code -> Code F2
+ */
+
+uint32_t TransferPack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
+{
+	uint32_t TxLen = 0;
+	uint32_t i;
+	OutBuf[0] = Flag;
+	TxLen = 1;
+	for (i = 0; i < Len; i++)
+	{
+		if (InBuf[i] == Flag)
+		{
+			OutBuf[TxLen++] = Code;
+			OutBuf[TxLen++] = F1;
+		}
+		else if (InBuf[i] == Code)
+		{
+			OutBuf[TxLen++] = Code;
+			OutBuf[TxLen++] = F2;
+		}
+		else
+		{
+			OutBuf[TxLen++] = InBuf[i];
+		}
+	}
+	OutBuf[TxLen++] = Flag;
+	return TxLen;
+}
+
+
+/*
+ * 转义解包
+ * 标识Flag,即包头包尾加入Flag
+ * 数据中遇到Code F1 -> Flag
+ * 数据中遇到Code F2 -> Code
+ * 数据中遇到Flag 出错返回0
+ */
+uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf)
+{
+	uint32_t RxLen = 0;
+	uint32_t i = 0;
+	while (i < Len)
+	{
+		if (InBuf[i] == Code)
+		{
+			if (InBuf[i+1] == F1)
+			{
+				OutBuf[RxLen++] = Flag;
+			}
+			else if (InBuf[i+1] == F2)
+			{
+				OutBuf[RxLen++] = Code;
+			}
+			else
+			{
+				return 0;
+			}
+			i += 2;
+		}
+		else if (InBuf[i] == Flag)
+		{
+			return 0;
+		}
+		else
+		{
+			OutBuf[RxLen++] = InBuf[i++];
+		}
+	}
+	return RxLen;
+}
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal llist manipulation where we know
+ * the prev/next entries already!
+ */
+void __llist_add(llist_head *p,
+                         llist_head *prev,
+                         llist_head *next)
+{
+	next->prev = p;
+	p->next = next;
+	p->prev = prev;
+	prev->next = p;
+}
+
+/**
+ * llist_add - add a new entry
+ * @new: new entry to be added
+ * @head: llist head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+void llist_add(llist_head *p, llist_head *head)
+{
+	__llist_add(p, head, head->next);
+}
+
+/**
+ * llist_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: llist head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+void llist_add_tail(llist_head *p, llist_head *head)
+{
+	__llist_add(p, head->prev, head);
+}
+
+/*
+ * Delete a llist entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal llist manipulation where we know
+ * the prev/next entries already!
+ */
+void __llist_del(llist_head * prev, llist_head * next)
+{
+	next->prev = prev;
+	prev->next = next;
+}
+
+/**
+ * llist_del - deletes entry from llist.
+ * @entry: the element to delete from the llist.
+ * Note: llist_empty on entry does not return true after this, the entry is
+ * in an undefined state.
+ */
+void llist_del(llist_head *entry)
+{
+	if (entry->prev && entry->next)
+	{
+		__llist_del(entry->prev, entry->next);
+	}
+	entry->next = LLIST_POISON1;
+	entry->prev = LLIST_POISON2;
+}
+
+/**
+ * llist_del_init - deletes entry from llist and reinitialize it.
+ * @entry: the element to delete from the llist.
+ */
+void llist_del_init(llist_head *entry)
+{
+	__llist_del(entry->prev, entry->next);
+	INIT_LLIST_HEAD(entry);
+}
+
+/**
+ * llist_move - delete from one llist and add as another's head
+ * @llist: the entry to move
+ * @head: the head that will precede our entry
+ */
+void llist_move(llist_head *llist, llist_head *head)
+{
+        __llist_del(llist->prev, llist->next);
+        llist_add(llist, head);
+}
+
+/**
+ * llist_move_tail - delete from one llist and add as another's tail
+ * @llist: the entry to move
+ * @head: the head that will follow our entry
+ */
+void llist_move_tail(llist_head *llist,
+				  llist_head *head)
+{
+        __llist_del(llist->prev, llist->next);
+        llist_add_tail(llist, head);
+}
+
+void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData)
+{
+	llist_head *node = head->next;
+	llist_head *del;
+	int32_t result;
+	while (!llist_empty(head) && (node != head))
+	{
+		result = cb((void *)node, pData);
+		if (result > 0)
+		{
+			return node;
+		}
+		else
+		{
+			del = node;
+			node = node->next;
+			if (result < 0)
+			{
+				llist_del(del);
+				free(del);
+			}
+		}
+	}
+	return NULL;
+}
+
+/**
+ * llist_empty - tests whether a llist is empty
+ * @head: the llist to test.
+ */
+int llist_empty(const llist_head *head)
+{
+	return head->next == head;
+}
+
+uint32_t llist_num(const llist_head *head)
+{
+	llist_head *node = head->next;
+	uint32_t num = 0;
+	if (!node)
+		return num;
+	while(node != head)
+	{
+		num++;
+		node = node->next;
+	}
+	return num;
+}
+
+#define PP_HTONS(x) ((uint16_t)((((x) & (uint16_t)0x00ffU) << 8) | (((x) & (uint16_t)0xff00U) >> 8)))
+#define PP_NTOHS(x) PP_HTONS(x)
+#define PP_HTONL(x) ((((x) & (uint32_t)0x000000ffUL) << 24) | \
+                     (((x) & (uint32_t)0x0000ff00UL) <<  8) | \
+                     (((x) & (uint32_t)0x00ff0000UL) >>  8) | \
+                     (((x) & (uint32_t)0xff000000UL) >> 24))
+#define PP_NTOHL(x) PP_HTONL(x)
+
+uint16_t BSP_Swap16(uint16_t n)
+{
+  return (uint16_t)PP_HTONS(n);
+}
+
+uint32_t BSP_Swap32(uint32_t n)
+{
+  return (uint32_t)PP_HTONL(n);
+}
+
+uint32_t utf8_to_unicode(uint8_t *string, uint32_t len, void *out, uint8_t is_only_16)
+{
+	uint32_t i = 0;
+	uint32_t result = 0;
+	uint8_t bit, n;
+	if (is_only_16)
+	{
+		uint16_t *buf = (uint16_t *)out;
+		while (i < len)
+		{
+			if (string[i] & 0x80)
+			{
+				if (!(string[i] & (1 << 5)))
+				{
+					n = 2;
+					buf[result] = string[i] & ((1 << 5) - 1);
+				}
+				else
+				{
+					buf[result] = string[i] & ((1 << 4) - 1);
+					n = 3;
+				}
+
+				for (bit = 1; bit < n; bit++)
+				{
+					buf[result] = (buf[result] << 6) | (string[i + bit] & 0x3f);
+				}
+				i += n;
+			}
+			else
+			{
+				buf[result] = string[i];
+				i++;
+			}
+			result++;
+		}
+	}
+	else
+	{
+		uint8_t table[7] = {0, 0, 0x1f, 0x0f, 0x07, 0x03, 0x01};
+		uint32_t *buf = (uint32_t *)out;
+		while (i < len)
+		{
+			if (string[i] & 0x80)
+			{
+				n = 7;
+				for (bit = 5; bit >= 1; bit--)
+				{
+					if (!(string[i] & (1 << bit)))
+					{
+						n -= bit;
+						break;
+					}
+				}
+				if (n >= 7)
+				{
+					return result;
+				}
+				buf[result] = string[i] & table[n];
+
+				for (bit = 1; bit < n; bit++)
+				{
+					buf[result] = (buf[result] << 6) | (string[i + bit] & 0x3f);
+				}
+				i += n;
+			}
+			else
+			{
+				buf[result] = string[i];
+				i++;
+			}
+			result++;
+		}
+	}
+	return result;
+}
+
+uint32_t unicode_to_utf8(void *in, uint32_t unicodelen, uint8_t *out, uint8_t is_only_16)
+{
+	uint32_t i = 0;
+	uint32_t result = 0;
+	uint8_t bit, n;
+	if (is_only_16)
+	{
+		uint16_t *buf = (uint16_t *)in;
+		while (i < unicodelen)
+		{
+			if (buf[i] <= 0x007f)
+			{
+				out[result] = buf[i];
+
+				result++;
+			}
+			else
+			{
+				if (buf[i] >> 12)
+				{
+					out[result + 2] = (buf[i] & 0x3f) | 0x80;
+					out[result + 1] = ((buf[i] >> 6) & 0x3f) | 0x80;
+					out[result] = 0xe0 | (buf[i] >> 12);
+					result += 3;
+				}
+				else
+				{
+					out[result + 1] = (buf[i] & 0x3f) | 0x80;
+					out[result] = 0xc0 | (buf[i] >> 6);
+					result += 2;
+				}
+			}
+			i++;
+		}
+
+	}
+	else
+	{
+		uint8_t table[7] = {0,0,0xc0,0xe0, 0xf0, 0xf8, 0xfc};
+		uint8_t pos[7] = {0,0,6,12,18,24,30};
+		uint32_t *buf = (uint32_t *)in;
+		while (i < unicodelen)
+		{
+			if (buf[i] <= 0x007f)
+			{
+				out[result] = buf[i];
+
+				result++;
+			}
+			else
+			{
+				n = 6;
+				for (bit = 1; bit < 6; bit++)
+				{
+					if (!(buf[i] >> ((bit + 1) * 6)))
+					{
+						n = bit + 1;
+						break;
+					}
+				}
+				out[result] = table[n] | (buf[i] >> pos[n]);
+				for (bit = 1; bit < n; bit++)
+				{
+					out[result + bit] = ( (buf[i] >> ((n - bit - 1) * 6)) & 0x3f) | 0x80;
+				}
+				result += n;
+			}
+			i++;
+		}
+	}
+	return result;
+}

+ 566 - 566
components/common/c_common.h → components/c_common/c_common.h

@@ -1,566 +1,566 @@
-/*
- * Copyright (c) 2022 OpenLuat & AirM2M
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifndef __C_COMMON_H__
-#define __C_COMMON_H__
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <stdarg.h>
-#include "luat_conf_bsp.h"
-typedef struct
-{
-	uint32_t MaigcNum; //升级包标识,标识不对直接抛弃
-	uint32_t CRC32;		//后续字节的CRC32校验,所有CRC32规则与ZIP压缩一致
-	uint32_t Param1;	//升级参数,其中byte0升级类型,byte1升级包存放位置,byte2外设总线序号,和platform_define里的外设序号一致
-	uint32_t Param2;	//额外的参数,需要和外置存储总线配合使用,一般是外置存储总线的PIN
-	uint32_t DataStartAddress;//升级包在flash中的起始地址,外部和内部都可以用
-	uint32_t DataLen;//升级包大小
-	uint32_t DataCRC32;//升级包整包数据的CRC32
-	char FilePath[100];//升级包在文件系统中的绝对路径,如果在flash中则随意填写
-}CoreUpgrade_HeadStruct;
-
-typedef struct
-{
-	uint32_t MaigcNum; //升级包标识,标识不对直接抛弃
-	uint32_t CRC32;		//后续字节的CRC32校验
-	uint32_t MainVersion;//目标的底层版本,升级成功的话就是这个版本号了
-	uint32_t AppVersion;//整包的版本号
-	uint32_t STDVersion;//允许升级的底层版本号
-	uint32_t DataLen;//升级包大小
-	uint32_t DataCRC32;//升级包整包数据的CRC32,这里验证传输的准确性
-}CoreUpgrade_FileHeadStruct;
-
-typedef struct
-{
-	uint32_t MaigcNum; //升级包标识,标识不对直接抛弃
-	uint32_t TotalLen;	//解压前占据的空间
-	uint32_t DataLen;	//解压后占据的空间,如果和TotalLen一样,则表示未启用压缩,不需要解压,也没有压缩参数
-						//如果是0,则表示是差分升级
-						//其他表示是整包升级,数据包经过了lzma压缩
-	uint32_t DataCRC32;	//解压后的数据的CRC32,这里验证烧录的正确性
-	uint32_t StartAddress;	//烧写的起始地址
-}CoreUpgrade_SectorStruct;
-
-typedef struct
-{
-	uint32_t param_max_num;
-	uint32_t param_max_len;
-	uint32_t param_num;
-	int8_t *param_str;
-}CmdParam;
-
-typedef struct
-{
-	uint8_t Sec;
-	uint8_t Min;
-	uint8_t Hour;
-	uint8_t Week;//表示日期0~6,sun~sat,表示预约时,bit0~bit6,sun~sat
-}Time_UserDataStruct;
-
-typedef struct
-{
-	uint16_t Year;
-	uint8_t Mon;
-	uint8_t Day;
-}Date_UserDataStruct;
-
-typedef union
-{
-	uint32_t dwTime;
-	Time_UserDataStruct Time;
-}Time_Union;
-
-typedef union
-{
-	uint32_t dwDate;
-	Date_UserDataStruct Date;
-}Date_Union;
-
-typedef struct
-{
-	uint8_t *Data;
-	uint32_t Len;
-	uint32_t Offset;
-	uint32_t MaxLength;
-	uint32_t DataSize;
-}Loop_Buffer;
-
-typedef struct
-{
-	uint8_t *Data;
-	uint32_t Pos;
-	uint32_t MaxLen;
-}Buffer_Struct;
-
-
-typedef struct
-{
-	uint8_t *pCache[2];
-	uint32_t pCacheLen[2];
-	uint32_t MaxLen;
-	uint8_t CurCacheSn;
-}DBuffer_Struct;
-
-typedef union
-{
-	void *p;
-	char *pc8;
-	uint8_t *pu8;
-	uint16_t *pu16;
-	uint32_t *pu32;
-	uint32_t u32;
-	uint8_t u8[4];
-	uint16_t u16[2];
-}PV_Union;
-
-enum
-{
-	ERROR_NONE,
-	ERROR_NO_SUCH_ID,
-	ERROR_PERMISSION_DENIED,
-	ERROR_PARAM_INVALID,
-	ERROR_PARAM_OVERFLOW,
-	ERROR_DEVICE_BUSY,
-	ERROR_OPERATION_FAILED,
-	ERROR_BUFFER_FULL,
-	ERROR_NO_MEMORY,
-	ERROR_CMD_NOT_SUPPORT,
-	ERROR_NO_DATA,
-	ERROR_NO_FLASH,
-	ERROR_NO_TIMER,
-	ERROR_TIMEOUT,
-	ERROR_SSL_HANDSHAKE,
-	ERROR_PROTOCL,
-	ERROR_ID_INVALID,
-	ERROR_MID_INVALID,
-	ERROR_RETRY_TOO_MUCH,
-	ERROR_CMD_BLOCK,
-	LIST_FIND = 1,
-	LIST_PASS = 0,
-	LIST_DEL = -1,
-
-	DMA_CB_DONE = 0,
-	UART_CB_TX_BUFFER_DONE,
-	UART_CB_TX_ALL_DONE,
-	UART_CB_RX_NEW,
-	UART_CB_RX_TIMEOUT,
-	UART_CB_RX_BUFFER_FULL,
-	UART_CB_ERROR,
-	UART_CB_CONNECTED,	//串口工具对方已经打开
-	DMA_CB_ERROR = 0xffffffff,
-
-
-	CORE_EVENT_ID_ANY = 0,
-	CORE_EVENT_ID_START = 0xf0000000,
-	CORE_EVENT_TIMEOUT,
-	CORE_TIMER_TIMEOUT = 0xf0010000,
-	SERVICE_EVENT_ID_START = 0xf0100000,
-	DEV_EVENT_ID_START = 0xf0200000,
-	DEV_SPIFLASH_SPI_DONE,
-	DEV_SDHC_SPI_DONE,
-	USB_APP_EVENT_ID_START = 0xf0300000,
-	USER_EVENT_ID_START = 0xf1000000,
-	INVALID_EVENT_ID = 0xffffffff,
-
-	NW_EVENT_SENT = 0,
-	NW_EVENT_RECV,
-	NW_EVENT_ERR,
-	NW_EVENT_CONNECTED,
-	NW_EVENT_REMOTE_CLOSE,
-	NW_EVENT_ACCEPT,
-	NW_EVENT_CLOSE_OK,
-
-
-
-};
-
-#define INVALID_HANDLE_VALUE  ((void *)0xffffffff)
-#define INVALID_PARAM  (0xffffffff)
-#define CRC32_GEN		(0x04C11DB7)
-#define CRC32_START		(0xffffffff)
-#define CRC16_CCITT_GEN		(0x1021)
-#define CRC16_MODBUS_GEN		(0x8005)
-#define CRC16_START		(0xffff)
-#define CRC16_IBM_SEED	(0xffff)
-#define CRC16_CCITT_SEED		(0x1D0F)
-#define HANDLE			void *
-#ifndef BIT
-#define BIT(n)      (1UL << (n))
-#endif
-#ifndef MIN
-#define MIN(X,Y)	(((X) < (Y))?(X):(Y))
-#endif
-typedef void (* TaskFun_t)( void * );
-typedef void (* CommonFun_t)(void);
-typedef void(* CBDataFun_t)(uint8_t *Data, uint32_t Len);
-typedef int32_t(*CBFuncEx_t)(void *pData, void *pParam);
-typedef uint64_t LongInt;
-
-
-
-#define INIT_FUN_EXPORT(fn, location, level) const CommonFun_t __ex_init_##fn __attribute__((section(location level))) = fn
-#define INIT_HW_EXPORT(fn, level) INIT_FUN_EXPORT(fn, ".preinit_fun_array.", level)
-#define INIT_DRV_EXPORT(fn, level) INIT_FUN_EXPORT(fn, ".init_fun_array.", level)
-#define INIT_TASK_EXPORT(fn, level) INIT_FUN_EXPORT(fn, ".task_fun_array.", level)
-
-/* board init routines will be called in board_init() function */
-#define INIT_BOARD_EXPORT(fn)           INIT_EXPORT(fn, "1")
-
-/* pre/device/component/env/app init routines will be called in init_thread */
-/* components pre-initialization (pure software initilization) */
-#define INIT_PREV_EXPORT(fn)            INIT_EXPORT(fn, "2")
-/* device initialization */
-#define INIT_DEVICE_EXPORT(fn)          INIT_EXPORT(fn, "3")
-/* components initialization (dfs, lwip, ...) */
-#define INIT_COMPONENT_EXPORT(fn)       INIT_EXPORT(fn, "4")
-/* environment initialization (mount disk, ...) */
-#define INIT_ENV_EXPORT(fn)             INIT_EXPORT(fn, "5")
-/* appliation initialization (rtgui application etc ...) */
-#define INIT_APP_EXPORT(fn)             INIT_EXPORT(fn, "6")
-
-typedef struct
-{
-	uint32_t ID;
-	uint32_t Param1;
-	uint32_t Param2;
-	uint32_t Param3;
-}OS_EVENT;
-
-typedef struct
-{
-	CBFuncEx_t CB;
-	union {
-		void *pParam;	//用户回调模式
-		uint32_t MaxCnt;	//设置捕获模式时的最大tick,捕获时的tick
-	}uParam;
-
-    union {
-    	struct {
-    	    uint8_t Level;	//IO输入输出电平,捕获模式下中断时IO电平
-    	    uint8_t PullMode; //IO上下拉控制
-    	} IOArg;
-    	struct {
-    	    uint8_t ExtiMode;	//中断模式
-    	    uint8_t PullMode; //IO上下拉控制
-    	} ExitArg;
-    	uint16_t Time;	//delay时间,us
-    } uArg;
-    uint8_t Operation;	//操作类型
-    uint8_t PinOrDelay;		//IO操作时为IOpin,delay操作时则为微调值,0~47,48为1us
-}OPQueue_CmdStruct;
-
-enum
-{
-    UDATA_TYPE_UNDEFINED = 0,
-	UDATA_MULTIPLE_RESOURCE,
-	UDATA_TYPE_STRING,
-	UDATA_TYPE_OPAQUE,
-	UDATA_TYPE_INTEGER,
-	UDATA_TYPE_DWORD,
-	UDATA_TYPE_WORD,
-	UDATA_TYPE_BYTE,
-	UDATA_TYPE_FLOAT,
-	UDATA_TYPE_BOOLEAN,
-	UDATA_TYPE_UNSIGNED,
-	UDATA_TYPE_UNUSD
-};
-
-typedef struct _u_data_t uData_t;
-
-struct _u_data_t
-{
-    union
-    {
-        uint8_t     asBoolean;
-        uint64_t	asUnsigned;
-        int64_t     asInteger;
-        PV_Union	asDword;
-        double      asFloat;
-        struct
-        {
-            size_t   length;
-            uint8_t *buffer;
-        } asBuffer;
-        struct
-        {
-            size_t       count;
-            uData_t 	*array;
-        } asChildren;
-    } value;
-	uint32_t ID;
-	uint8_t Type;
-};
-
-__attribute__((weak)) uint8_t OS_CheckInIrq(void);
-#ifdef __BUILD_OS__
-HANDLE OS_MutexCreate(void);
-HANDLE OS_MutexCreateUnlock(void);
-void OS_MutexLock(HANDLE Sem);
-int32_t OS_MutexLockWtihTime(HANDLE Sem, uint32_t TimeoutMs);
-void OS_MutexRelease(HANDLE Sem);
-void OS_MutexDelete(HANDLE Sem);
-void OS_SuspendTask(HANDLE taskHandle);
-void OS_ResumeTask(HANDLE taskHandle);
-uint8_t OS_IsSchedulerRun(void);
-#endif
-uint32_t OS_EnterCritical(void);
-void OS_ExitCritical(uint32_t Critical);
-void *OS_Malloc(uint32_t Size);
-void *OS_Zalloc(uint32_t Size);
-void OS_Free(void *p);
-void *OS_Realloc(void *buf, uint32_t size);
-void OS_MemInfo(uint32_t *curalloc, uint32_t *totfree, uint32_t *maxfree);
-int32_t OS_InitBuffer(Buffer_Struct *Buf, uint32_t Size);
-void OS_DeInitBuffer(Buffer_Struct *Buf);
-int32_t OS_ReInitBuffer(Buffer_Struct *Buf, uint32_t Size);
-int32_t OS_ReSizeBuffer(Buffer_Struct *Buf, uint32_t Size);
-int32_t OS_BufferWrite(Buffer_Struct *Buf, void *Data, uint32_t Len);
-int32_t OS_BufferWriteLimit(Buffer_Struct *Buf, void *Data, uint32_t Len);
-void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len);
-void DBuffer_Init(DBuffer_Struct *DBuf, uint32_t Size);
-void DBuffer_ReInit(DBuffer_Struct *DBuf, uint32_t Size);
-void DBuffer_DeInit(DBuffer_Struct *DBuf);
-void *DBuffer_GetCache(DBuffer_Struct *DBuf, uint8_t IsCurrent);
-void DBuffer_SwapCache(DBuffer_Struct *DBuf);
-void DBuffer_SetDataLen(DBuffer_Struct *DBuf, uint32_t Len, uint8_t IsCurrent);
-uint32_t DBuffer_GetDataLen(DBuffer_Struct *DBuf, uint8_t IsCurrent);
-
-
-void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen);
-int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len);
-void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len);
-void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize);
-uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len);
-uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len);
-void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len);
-uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len);
-int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value);
-int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value);
-uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn);
-uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart);
-uint8_t SumCheck(uint8_t *Data, uint32_t Len);
-uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse);
-uint32_t AsciiToU32(uint8_t *Src, uint32_t Len);
-void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen);
-uint32_t CRC32_Cal(uint32_t * CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last);
-uint32_t CmdParseParam(int8_t* pStr, CmdParam *CmdParam, int8_t Cut);
-uint8_t IsLeapYear(uint32_t Year);
-LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time);
-uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay);
-/*
- * 转义解包
- * 标识Flag,即包头包尾加入Flag
- * 数据中遇到Code F1 -> Flag
- * 数据中遇到Code F2 -> Code
- * 数据中遇到Flag 出错返回0
- */
-
-uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf);
-/*
- * llist相关代码,大部分来自linux内核
- */
-/**
- * container_of - cast a member of a structure out to the containing structure
- *
- * @ptr:	the pointer to the member.
- * @type:	the type of the container struct this is embedded in.
- * @member:	the name of the member within the struct.
- *
- */
-#define container_of(ptr, type, member) ({			\
-        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
-        (type *)( (char *)__mptr - offsetof(type,member) );})
-
-
-/*
- * These are non-NULL pointers that will result in page faults
- * under normal circumstances, used to verify that nobody uses
- * non-initialized llist entries.
- */
-#define LLIST_POISON1  (0)
-#define LLIST_POISON2  (0)
-
-/*
- * Simple doubly linked llist implementation.
- *
- * Some of the internal functions ("__xxx") are useful when
- * manipulating whole llists rather than single entries, as
- * sometimes we already know the next/prev entries and we can
- * generate better code by using them directly rather than
- * using the generic single-entry routines.
- */
-
-typedef struct llist_head_t{
-    struct llist_head_t *next, *prev;
-}llist_head;
-
-#define LLIST_HEAD_INIT(name) { &(name), &(name) }
-
-#define LLIST_HEAD(name) \
-	llist_head name = LLIST_HEAD_INIT(name)
-
-#define INIT_LLIST_HEAD(ptr) do { \
-	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
-} while (0)
-
-/*
- * Insert a new entry between two known consecutive entries.
- *
- * This is only for internal llist manipulation where we know
- * the prev/next entries already!
- */
-void __llist_add(llist_head *p,
-                         llist_head *prev,
-                         llist_head *next);
-
-/**
- * llist_add - add a new entry
- * @new: new entry to be added
- * @head: llist head to add it after
- *
- * Insert a new entry after the specified head.
- * This is good for implementing stacks.
- */
-void llist_add(llist_head *p, llist_head *head);
-
-/**
- * llist_add_tail - add a new entry
- * @new: new entry to be added
- * @head: llist head to add it before
- *
- * Insert a new entry before the specified head.
- * This is useful for implementing queues.
- */
-void llist_add_tail(llist_head *p, llist_head *head);
-
-
-/*
- * Delete a llist entry by making the prev/next entries
- * point to each other.
- *
- * This is only for internal llist manipulation where we know
- * the prev/next entries already!
- */
-void __llist_del(llist_head * prev, llist_head * next);
-
-/**
- * llist_del - deletes entry from llist.
- * @entry: the element to delete from the llist.
- * Note: llist_empty on entry does not return true after this, the entry is
- * in an undefined state.
- */
-void llist_del(llist_head *entry);
-
-/**
- * llist_del_init - deletes entry from llist and reinitialize it.
- * @entry: the element to delete from the llist.
- */
-void llist_del_init(llist_head *entry);
-
-/**
- * llist_move - delete from one llist and add as another's head
- * @llist: the entry to move
- * @head: the head that will precede our entry
- */
-void llist_move(llist_head *llist, llist_head *head);
-
-/**
- * llist_move_tail - delete from one llist and add as another's tail
- * @llist: the entry to move
- * @head: the head that will follow our entry
- */
-void llist_move_tail(llist_head *llist,
-				  llist_head *head);
-
-/**
- * llist_empty - tests whether a llist is empty
- * @head: the llist to test.
- */
-int llist_empty(const llist_head *head);
-
-uint32_t llist_num(const llist_head *head);
-
-void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData);
-/**
- * llist_entry - get the struct for this entry
- * @ptr:	the &llist_head pointer.
- * @type:	the type of the struct this is embedded in.
- * @member:	the name of the llist_struct within the struct.
- */
-#define llist_entry(ptr, type, member) \
-	container_of(ptr, type, member)
-
-
-uint16_t BSP_Swap16(uint16_t n);
-uint32_t BSP_Swap32(uint32_t n);
-
-uint8_t BytesGet8(const void *ptr);
-void BytesPut8(void *ptr, uint8_t v);
-uint16_t BytesGetBe16(const void *ptr);
-void BytesPutBe16(void *ptr, uint16_t v);
-uint32_t BytesGetBe32(const void *ptr);
-void BytesPutBe32(void *ptr, uint32_t v);
-uint16_t BytesGetLe16(const void *ptr);
-void BytesPutLe16(void *ptr, uint16_t v);
-uint32_t BytesGetLe32(const void *ptr);
-void BytesPutLe32(void *ptr, uint32_t v);
-uint64_t BytesGetLe64(const void *ptr);
-void BytesPutLe64(void *ptr, uint64_t v);
-uint8_t BytesGet8FromBuf(Buffer_Struct *Buf);
-void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v);
-uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf);
-void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v);
-uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf);
-void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v);
-uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf);
-void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v);
-uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf);
-void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v);
-uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf);
-void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v);
-float BytesGetFloatFromBuf(Buffer_Struct *Buf);
-void BytesPutFloatToBuf(Buffer_Struct *Buf, float v);
-double BytesGetDoubleFromBuf(Buffer_Struct *Buf);
-void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v);
-/*************************************************************************/
-
-#define malloc luat_heap_malloc
-#define free luat_heap_free
-#define realloc luat_heap_realloc
-#define zalloc luat_heap_zalloc
-#define calloc luat_heap_calloc
-#ifdef __LUATOS_TICK_64BIT__
-extern uint64_t GetSysTickMS(void);
-#else
-#define GetSysTickMS luat_mcu_ticks
-#endif
-
-
-#ifndef ASSERT
-#if defined(__DEBUG__)
-#define ASSERT( x ) if( ( x ) == 0 ) { __disable_irq(); DBG_Trace("\r\nassert %s,%d", __FUNCTION__, __LINE__); for( ;; ); }
-#else
-#define ASSERT( x )
-#endif
-#endif
-#endif
+/*
+ * Copyright (c) 2022 OpenLuat & AirM2M
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __C_COMMON_H__
+#define __C_COMMON_H__
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include "luat_conf_bsp.h"
+typedef struct
+{
+	uint32_t MaigcNum; //升级包标识,标识不对直接抛弃
+	uint32_t CRC32;		//后续字节的CRC32校验,所有CRC32规则与ZIP压缩一致
+	uint32_t Param1;	//升级参数,其中byte0升级类型,byte1升级包存放位置,byte2外设总线序号,和platform_define里的外设序号一致
+	uint32_t Param2;	//额外的参数,需要和外置存储总线配合使用,一般是外置存储总线的PIN
+	uint32_t DataStartAddress;//升级包在flash中的起始地址,外部和内部都可以用
+	uint32_t DataLen;//升级包大小
+	uint32_t DataCRC32;//升级包整包数据的CRC32
+	char FilePath[100];//升级包在文件系统中的绝对路径,如果在flash中则随意填写
+}CoreUpgrade_HeadStruct;
+
+typedef struct
+{
+	uint32_t MaigcNum; //升级包标识,标识不对直接抛弃
+	uint32_t CRC32;		//后续字节的CRC32校验
+	uint32_t MainVersion;//目标的底层版本,升级成功的话就是这个版本号了
+	uint32_t AppVersion;//整包的版本号
+	uint32_t STDVersion;//允许升级的底层版本号
+	uint32_t DataLen;//升级包大小
+	uint32_t DataCRC32;//升级包整包数据的CRC32,这里验证传输的准确性
+}CoreUpgrade_FileHeadStruct;
+
+typedef struct
+{
+	uint32_t MaigcNum; //升级包标识,标识不对直接抛弃
+	uint32_t TotalLen;	//解压前占据的空间
+	uint32_t DataLen;	//解压后占据的空间,如果和TotalLen一样,则表示未启用压缩,不需要解压,也没有压缩参数
+						//如果是0,则表示是差分升级
+						//其他表示是整包升级,数据包经过了lzma压缩
+	uint32_t DataCRC32;	//解压后的数据的CRC32,这里验证烧录的正确性
+	uint32_t StartAddress;	//烧写的起始地址
+}CoreUpgrade_SectorStruct;
+
+typedef struct
+{
+	uint32_t param_max_num;
+	uint32_t param_max_len;
+	uint32_t param_num;
+	int8_t *param_str;
+}CmdParam;
+
+typedef struct
+{
+	uint8_t Sec;
+	uint8_t Min;
+	uint8_t Hour;
+	uint8_t Week;//表示日期0~6,sun~sat,表示预约时,bit0~bit6,sun~sat
+}Time_UserDataStruct;
+
+typedef struct
+{
+	uint16_t Year;
+	uint8_t Mon;
+	uint8_t Day;
+}Date_UserDataStruct;
+
+typedef union
+{
+	uint32_t dwTime;
+	Time_UserDataStruct Time;
+}Time_Union;
+
+typedef union
+{
+	uint32_t dwDate;
+	Date_UserDataStruct Date;
+}Date_Union;
+
+typedef struct
+{
+	uint8_t *Data;
+	uint32_t Len;
+	uint32_t Offset;
+	uint32_t MaxLength;
+	uint32_t DataSize;
+}Loop_Buffer;
+
+typedef struct
+{
+	uint8_t *Data;
+	uint32_t Pos;
+	uint32_t MaxLen;
+}Buffer_Struct;
+
+
+typedef struct
+{
+	uint8_t *pCache[2];
+	uint32_t pCacheLen[2];
+	uint32_t MaxLen;
+	uint8_t CurCacheSn;
+}DBuffer_Struct;
+
+typedef union
+{
+	void *p;
+	char *pc8;
+	uint8_t *pu8;
+	uint16_t *pu16;
+	uint32_t *pu32;
+	uint32_t u32;
+	uint8_t u8[4];
+	uint16_t u16[2];
+}PV_Union;
+
+enum
+{
+	ERROR_NONE,
+	ERROR_NO_SUCH_ID,
+	ERROR_PERMISSION_DENIED,
+	ERROR_PARAM_INVALID,
+	ERROR_PARAM_OVERFLOW,
+	ERROR_DEVICE_BUSY,
+	ERROR_OPERATION_FAILED,
+	ERROR_BUFFER_FULL,
+	ERROR_NO_MEMORY,
+	ERROR_CMD_NOT_SUPPORT,
+	ERROR_NO_DATA,
+	ERROR_NO_FLASH,
+	ERROR_NO_TIMER,
+	ERROR_TIMEOUT,
+	ERROR_SSL_HANDSHAKE,
+	ERROR_PROTOCL,
+	ERROR_ID_INVALID,
+	ERROR_MID_INVALID,
+	ERROR_RETRY_TOO_MUCH,
+	ERROR_CMD_BLOCK,
+	LIST_FIND = 1,
+	LIST_PASS = 0,
+	LIST_DEL = -1,
+
+	DMA_CB_DONE = 0,
+	UART_CB_TX_BUFFER_DONE,
+	UART_CB_TX_ALL_DONE,
+	UART_CB_RX_NEW,
+	UART_CB_RX_TIMEOUT,
+	UART_CB_RX_BUFFER_FULL,
+	UART_CB_ERROR,
+	UART_CB_CONNECTED,	//串口工具对方已经打开
+	DMA_CB_ERROR = 0xffffffff,
+
+
+	CORE_EVENT_ID_ANY = 0,
+	CORE_EVENT_ID_START = 0xf0000000,
+	CORE_EVENT_TIMEOUT,
+	CORE_TIMER_TIMEOUT = 0xf0010000,
+	SERVICE_EVENT_ID_START = 0xf0100000,
+	DEV_EVENT_ID_START = 0xf0200000,
+	DEV_SPIFLASH_SPI_DONE,
+	DEV_SDHC_SPI_DONE,
+	USB_APP_EVENT_ID_START = 0xf0300000,
+	USER_EVENT_ID_START = 0xf1000000,
+	INVALID_EVENT_ID = 0xffffffff,
+
+	NW_EVENT_SENT = 0,
+	NW_EVENT_RECV,
+	NW_EVENT_ERR,
+	NW_EVENT_CONNECTED,
+	NW_EVENT_REMOTE_CLOSE,
+	NW_EVENT_ACCEPT,
+	NW_EVENT_CLOSE_OK,
+
+
+
+};
+
+#define INVALID_HANDLE_VALUE  ((void *)0xffffffff)
+#define INVALID_PARAM  (0xffffffff)
+#define CRC32_GEN		(0x04C11DB7)
+#define CRC32_START		(0xffffffff)
+#define CRC16_CCITT_GEN		(0x1021)
+#define CRC16_MODBUS_GEN		(0x8005)
+#define CRC16_START		(0xffff)
+#define CRC16_IBM_SEED	(0xffff)
+#define CRC16_CCITT_SEED		(0x1D0F)
+#define HANDLE			void *
+#ifndef BIT
+#define BIT(n)      (1UL << (n))
+#endif
+#ifndef MIN
+#define MIN(X,Y)	(((X) < (Y))?(X):(Y))
+#endif
+typedef void (* TaskFun_t)( void * );
+typedef void (* CommonFun_t)(void);
+typedef void(* CBDataFun_t)(uint8_t *Data, uint32_t Len);
+typedef int32_t(*CBFuncEx_t)(void *pData, void *pParam);
+typedef uint64_t LongInt;
+
+
+
+#define INIT_FUN_EXPORT(fn, location, level) const CommonFun_t __ex_init_##fn __attribute__((section(location level))) = fn
+#define INIT_HW_EXPORT(fn, level) INIT_FUN_EXPORT(fn, ".preinit_fun_array.", level)
+#define INIT_DRV_EXPORT(fn, level) INIT_FUN_EXPORT(fn, ".init_fun_array.", level)
+#define INIT_TASK_EXPORT(fn, level) INIT_FUN_EXPORT(fn, ".task_fun_array.", level)
+
+/* board init routines will be called in board_init() function */
+#define INIT_BOARD_EXPORT(fn)           INIT_EXPORT(fn, "1")
+
+/* pre/device/component/env/app init routines will be called in init_thread */
+/* components pre-initialization (pure software initilization) */
+#define INIT_PREV_EXPORT(fn)            INIT_EXPORT(fn, "2")
+/* device initialization */
+#define INIT_DEVICE_EXPORT(fn)          INIT_EXPORT(fn, "3")
+/* components initialization (dfs, lwip, ...) */
+#define INIT_COMPONENT_EXPORT(fn)       INIT_EXPORT(fn, "4")
+/* environment initialization (mount disk, ...) */
+#define INIT_ENV_EXPORT(fn)             INIT_EXPORT(fn, "5")
+/* appliation initialization (rtgui application etc ...) */
+#define INIT_APP_EXPORT(fn)             INIT_EXPORT(fn, "6")
+
+typedef struct
+{
+	uint32_t ID;
+	uint32_t Param1;
+	uint32_t Param2;
+	uint32_t Param3;
+}OS_EVENT;
+
+typedef struct
+{
+	CBFuncEx_t CB;
+	union {
+		void *pParam;	//用户回调模式
+		uint32_t MaxCnt;	//设置捕获模式时的最大tick,捕获时的tick
+	}uParam;
+
+    union {
+    	struct {
+    	    uint8_t Level;	//IO输入输出电平,捕获模式下中断时IO电平
+    	    uint8_t PullMode; //IO上下拉控制
+    	} IOArg;
+    	struct {
+    	    uint8_t ExtiMode;	//中断模式
+    	    uint8_t PullMode; //IO上下拉控制
+    	} ExitArg;
+    	uint16_t Time;	//delay时间,us
+    } uArg;
+    uint8_t Operation;	//操作类型
+    uint8_t PinOrDelay;		//IO操作时为IOpin,delay操作时则为微调值,0~47,48为1us
+}OPQueue_CmdStruct;
+
+enum
+{
+    UDATA_TYPE_UNDEFINED = 0,
+	UDATA_MULTIPLE_RESOURCE,
+	UDATA_TYPE_STRING,
+	UDATA_TYPE_OPAQUE,
+	UDATA_TYPE_INTEGER,
+	UDATA_TYPE_DWORD,
+	UDATA_TYPE_WORD,
+	UDATA_TYPE_BYTE,
+	UDATA_TYPE_FLOAT,
+	UDATA_TYPE_BOOLEAN,
+	UDATA_TYPE_UNSIGNED,
+	UDATA_TYPE_UNUSD
+};
+
+typedef struct _u_data_t uData_t;
+
+struct _u_data_t
+{
+    union
+    {
+        uint8_t     asBoolean;
+        uint64_t	asUnsigned;
+        int64_t     asInteger;
+        PV_Union	asDword;
+        double      asFloat;
+        struct
+        {
+            size_t   length;
+            uint8_t *buffer;
+        } asBuffer;
+        struct
+        {
+            size_t       count;
+            uData_t 	*array;
+        } asChildren;
+    } value;
+	uint32_t ID;
+	uint8_t Type;
+};
+
+__attribute__((weak)) uint8_t OS_CheckInIrq(void);
+#ifdef __BUILD_OS__
+HANDLE OS_MutexCreate(void);
+HANDLE OS_MutexCreateUnlock(void);
+void OS_MutexLock(HANDLE Sem);
+int32_t OS_MutexLockWtihTime(HANDLE Sem, uint32_t TimeoutMs);
+void OS_MutexRelease(HANDLE Sem);
+void OS_MutexDelete(HANDLE Sem);
+void OS_SuspendTask(HANDLE taskHandle);
+void OS_ResumeTask(HANDLE taskHandle);
+uint8_t OS_IsSchedulerRun(void);
+#endif
+uint32_t OS_EnterCritical(void);
+void OS_ExitCritical(uint32_t Critical);
+void *OS_Malloc(uint32_t Size);
+void *OS_Zalloc(uint32_t Size);
+void OS_Free(void *p);
+void *OS_Realloc(void *buf, uint32_t size);
+void OS_MemInfo(uint32_t *curalloc, uint32_t *totfree, uint32_t *maxfree);
+int32_t OS_InitBuffer(Buffer_Struct *Buf, uint32_t Size);
+void OS_DeInitBuffer(Buffer_Struct *Buf);
+int32_t OS_ReInitBuffer(Buffer_Struct *Buf, uint32_t Size);
+int32_t OS_ReSizeBuffer(Buffer_Struct *Buf, uint32_t Size);
+int32_t OS_BufferWrite(Buffer_Struct *Buf, void *Data, uint32_t Len);
+int32_t OS_BufferWriteLimit(Buffer_Struct *Buf, void *Data, uint32_t Len);
+void OS_BufferRemove(Buffer_Struct *Buf, uint32_t Len);
+void DBuffer_Init(DBuffer_Struct *DBuf, uint32_t Size);
+void DBuffer_ReInit(DBuffer_Struct *DBuf, uint32_t Size);
+void DBuffer_DeInit(DBuffer_Struct *DBuf);
+void *DBuffer_GetCache(DBuffer_Struct *DBuf, uint8_t IsCurrent);
+void DBuffer_SwapCache(DBuffer_Struct *DBuf);
+void DBuffer_SetDataLen(DBuffer_Struct *DBuf, uint32_t Len, uint8_t IsCurrent);
+uint32_t DBuffer_GetDataLen(DBuffer_Struct *DBuf, uint8_t IsCurrent);
+
+
+void Buffer_StaticInit(Buffer_Struct *Buf, void *Src, uint32_t MaxLen);
+int32_t Buffer_StaticWrite(Buffer_Struct *Buf, void *Data, uint32_t Len);
+void Buffer_Remove(Buffer_Struct *Buf, uint32_t Len);
+void LoopBuffer_Init(Loop_Buffer *Buf, void *Src, uint32_t MaxLen, uint32_t DataSize);
+uint32_t LoopBuffer_Query(Loop_Buffer *Buf, void *Src, uint32_t Len);
+uint32_t LoopBuffer_Read(Loop_Buffer *Buf, void *Src, uint32_t Len);
+void LoopBuffer_Del(Loop_Buffer *Buf, uint32_t Len);
+uint32_t LoopBuffer_Write(Loop_Buffer *Buf, void *Src, uint32_t Len);
+int32_t BSP_SetBit(uint8_t *Data, uint32_t Sn, uint8_t Value);
+int32_t BSP_GetBit(uint8_t *Data, uint32_t Sn, uint8_t *Value);
+uint8_t BSP_TestBit(uint8_t *Data, uint32_t Sn);
+uint8_t XorCheck(void *Src, uint32_t Len, uint8_t CheckStart);
+uint8_t SumCheck(uint8_t *Data, uint32_t Len);
+uint16_t CRC16Cal(void *Data, uint16_t Len, uint16_t CRC16Last, uint16_t CRCRoot, uint8_t IsReverse);
+uint32_t AsciiToU32(uint8_t *Src, uint32_t Len);
+void CRC32_CreateTable(uint32_t *Tab, uint32_t Gen);
+uint32_t CRC32_Cal(uint32_t * CRC32_Table, uint8_t *Buf, uint32_t Size, uint32_t CRC32Last);
+uint32_t CmdParseParam(int8_t* pStr, CmdParam *CmdParam, int8_t Cut);
+uint8_t IsLeapYear(uint32_t Year);
+LongInt UTC2Tamp(Date_UserDataStruct *Date, Time_UserDataStruct *Time);
+uint32_t Tamp2UTC(LongInt Sec, Date_UserDataStruct *Date, Time_UserDataStruct *Time, uint32_t LastDDay);
+/*
+ * 转义解包
+ * 标识Flag,即包头包尾加入Flag
+ * 数据中遇到Code F1 -> Flag
+ * 数据中遇到Code F2 -> Code
+ * 数据中遇到Flag 出错返回0
+ */
+
+uint32_t TransferUnpack(uint8_t Flag, uint8_t Code, uint8_t F1, uint8_t F2, uint8_t *InBuf, uint32_t Len, uint8_t *OutBuf);
+/*
+ * llist相关代码,大部分来自linux内核
+ */
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ *
+ * @ptr:	the pointer to the member.
+ * @type:	the type of the container struct this is embedded in.
+ * @member:	the name of the member within the struct.
+ *
+ */
+#define container_of(ptr, type, member) ({			\
+        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        (type *)( (char *)__mptr - offsetof(type,member) );})
+
+
+/*
+ * These are non-NULL pointers that will result in page faults
+ * under normal circumstances, used to verify that nobody uses
+ * non-initialized llist entries.
+ */
+#define LLIST_POISON1  (0)
+#define LLIST_POISON2  (0)
+
+/*
+ * Simple doubly linked llist implementation.
+ *
+ * Some of the internal functions ("__xxx") are useful when
+ * manipulating whole llists rather than single entries, as
+ * sometimes we already know the next/prev entries and we can
+ * generate better code by using them directly rather than
+ * using the generic single-entry routines.
+ */
+
+typedef struct llist_head_t{
+    struct llist_head_t *next, *prev;
+}llist_head;
+
+#define LLIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LLIST_HEAD(name) \
+	llist_head name = LLIST_HEAD_INIT(name)
+
+#define INIT_LLIST_HEAD(ptr) do { \
+	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
+} while (0)
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal llist manipulation where we know
+ * the prev/next entries already!
+ */
+void __llist_add(llist_head *p,
+                         llist_head *prev,
+                         llist_head *next);
+
+/**
+ * llist_add - add a new entry
+ * @new: new entry to be added
+ * @head: llist head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+void llist_add(llist_head *p, llist_head *head);
+
+/**
+ * llist_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: llist head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+void llist_add_tail(llist_head *p, llist_head *head);
+
+
+/*
+ * Delete a llist entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal llist manipulation where we know
+ * the prev/next entries already!
+ */
+void __llist_del(llist_head * prev, llist_head * next);
+
+/**
+ * llist_del - deletes entry from llist.
+ * @entry: the element to delete from the llist.
+ * Note: llist_empty on entry does not return true after this, the entry is
+ * in an undefined state.
+ */
+void llist_del(llist_head *entry);
+
+/**
+ * llist_del_init - deletes entry from llist and reinitialize it.
+ * @entry: the element to delete from the llist.
+ */
+void llist_del_init(llist_head *entry);
+
+/**
+ * llist_move - delete from one llist and add as another's head
+ * @llist: the entry to move
+ * @head: the head that will precede our entry
+ */
+void llist_move(llist_head *llist, llist_head *head);
+
+/**
+ * llist_move_tail - delete from one llist and add as another's tail
+ * @llist: the entry to move
+ * @head: the head that will follow our entry
+ */
+void llist_move_tail(llist_head *llist,
+				  llist_head *head);
+
+/**
+ * llist_empty - tests whether a llist is empty
+ * @head: the llist to test.
+ */
+int llist_empty(const llist_head *head);
+
+uint32_t llist_num(const llist_head *head);
+
+void *llist_traversal(llist_head *head, CBFuncEx_t cb, void *pData);
+/**
+ * llist_entry - get the struct for this entry
+ * @ptr:	the &llist_head pointer.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the llist_struct within the struct.
+ */
+#define llist_entry(ptr, type, member) \
+	container_of(ptr, type, member)
+
+
+uint16_t BSP_Swap16(uint16_t n);
+uint32_t BSP_Swap32(uint32_t n);
+
+uint8_t BytesGet8(const void *ptr);
+void BytesPut8(void *ptr, uint8_t v);
+uint16_t BytesGetBe16(const void *ptr);
+void BytesPutBe16(void *ptr, uint16_t v);
+uint32_t BytesGetBe32(const void *ptr);
+void BytesPutBe32(void *ptr, uint32_t v);
+uint16_t BytesGetLe16(const void *ptr);
+void BytesPutLe16(void *ptr, uint16_t v);
+uint32_t BytesGetLe32(const void *ptr);
+void BytesPutLe32(void *ptr, uint32_t v);
+uint64_t BytesGetLe64(const void *ptr);
+void BytesPutLe64(void *ptr, uint64_t v);
+uint8_t BytesGet8FromBuf(Buffer_Struct *Buf);
+void BytesPut8ToBuf(Buffer_Struct *Buf, uint8_t v);
+uint16_t BytesGetBe16FromBuf(Buffer_Struct *Buf);
+void BytesPutBe16ToBuf(Buffer_Struct *Buf, uint16_t v);
+uint32_t BytesGetBe32FromBuf(Buffer_Struct *Buf);
+void BytesPutBe32ToBuf(Buffer_Struct *Buf, uint32_t v);
+uint16_t BytesGetLe16FromBuf(Buffer_Struct *Buf);
+void BytesPutLe16ToBuf(Buffer_Struct *Buf, uint16_t v);
+uint32_t BytesGetLe32FromBuf(Buffer_Struct *Buf);
+void BytesPutLe32ToBuf(Buffer_Struct *Buf, uint32_t v);
+uint64_t BytesGetLe64FromBuf(Buffer_Struct *Buf);
+void BytesPutLe64ToBuf(Buffer_Struct *Buf, uint64_t v);
+float BytesGetFloatFromBuf(Buffer_Struct *Buf);
+void BytesPutFloatToBuf(Buffer_Struct *Buf, float v);
+double BytesGetDoubleFromBuf(Buffer_Struct *Buf);
+void BytesPutDoubleToBuf(Buffer_Struct *Buf, double v);
+/*************************************************************************/
+
+#define malloc luat_heap_malloc
+#define free luat_heap_free
+#define realloc luat_heap_realloc
+#define zalloc luat_heap_zalloc
+#define calloc luat_heap_calloc
+#ifdef __LUATOS_TICK_64BIT__
+extern uint64_t GetSysTickMS(void);
+#else
+#define GetSysTickMS luat_mcu_ticks
+#endif
+
+
+#ifndef ASSERT
+#if defined(__DEBUG__)
+#define ASSERT( x ) if( ( x ) == 0 ) { __disable_irq(); DBG_Trace("\r\nassert %s,%d", __FUNCTION__, __LINE__); for( ;; ); }
+#else
+#define ASSERT( x )
+#endif
+#endif
+#endif

+ 8 - 0
components/c_common/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("c_common")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 9 - 0
components/crypto/xmake.lua

@@ -0,0 +1,9 @@
+
+
+
+target("crypto")
+    set_kind("static")
+    add_files("src/*.c")
+    add_includedirs("inc",{public = true})
+    add_includedirs("../mbedtls/include",{public = true})
+target_end()

+ 8 - 0
components/fs/fal/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("fal")
+    set_kind("static")
+    add_files("src/*.c")
+    add_includedirs("inc",{public = true})
+target_end()

+ 0 - 0
components/fs/vfs/luat_fs_fatfs.c → components/fs/fatfs/luat_fs_fatfs.c


+ 8 - 0
components/fs/fatfs/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("fatfs")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 9 - 0
components/fs/flashdb/xmake.lua

@@ -0,0 +1,9 @@
+
+
+
+target("flashdb")
+    set_kind("static")
+    add_files("src/*.c")
+    add_includedirs("inc",{public = true})
+    add_includedirs("../fal/inc",{public = true})
+target_end()

+ 0 - 0
components/fs/vfs/luat_fs_lfs2.c → components/fs/lfs/luat_fs_lfs2.c


+ 8 - 0
components/fs/lfs/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("lfs")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 0 - 0
components/fs/vfs/luat_fs_luadb.c → components/fs/luadb/luat_fs_luadb.c


+ 8 - 0
components/fs/luadb/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("luadb")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 3
components/fs/vfs/xmake.lua

@@ -1,3 +1,8 @@
-target("fs_vfs")
-    set_kind("binary")
-    add_files("*.c")
+
+
+
+target("vfs")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 17 - 0
components/fs/xmake.lua

@@ -0,0 +1,17 @@
+
+includes("fal")
+includes("fatfs")
+includes("flashdb")
+includes("lfs")
+includes("luadb")
+includes("vfs")
+
+target("fs")
+    set_kind("static")
+    add_deps("fal")
+    add_deps("fatfs")
+    add_deps("flashdb")
+    add_deps("lfs")
+    add_deps("luadb")
+    add_deps("vfs")
+target_end()

+ 7 - 0
components/mbedtls/xmake.lua

@@ -0,0 +1,7 @@
+
+target("mbedtls")
+    set_kind("static")
+    
+    add_includedirs("include",{public = true})
+    add_files("library/*.c")
+target_end()

+ 8 - 0
components/mempool/bget/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("bget")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/mempool/tlsf3/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("tlsf3")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/mempool/xmake.lua

@@ -0,0 +1,8 @@
+
+includes("bget")
+includes("tlsf3")
+
+target("mempool")
+    set_kind("static")
+    add_deps("bget", "tlsf3")
+target_end()

+ 7 - 0
components/network/ota/xmake.lua

@@ -0,0 +1,7 @@
+
+
+target("ota")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 12 - 0
components/network/xmake.lua

@@ -0,0 +1,12 @@
+
+
+-- includes("adapter")
+-- includes("fota")
+-- includes("libemqtt")
+-- includes("libhttp")
+includes("ota")
+
+target("network")
+    set_kind("static")
+    add_deps("ota")
+target_end()

+ 10 - 0
components/peripheral/camera/xmake.lua

@@ -0,0 +1,10 @@
+
+
+
+target("camera")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../../ui/lcd",{public = true})
+    add_includedirs("../../ui/u8g2",{public = true})
+target_end()

+ 1 - 1
components/peripheral/io_queue/luat_lib_io_queue.c

@@ -7,7 +7,7 @@
 @demo io_queue
 */
 #include "luat_base.h"
-#include "luat_multimedia.h"
+// #include "luat_multimedia.h"
 #include "luat_msgbus.h"
 #include "luat_zbuff.h"
 #define LUAT_LOG_TAG "io_queue"

+ 8 - 0
components/peripheral/io_queue/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("io_queue")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 2 - 1
components/peripheral/lora/xmake.lua

@@ -2,4 +2,5 @@ target("lora")
     set_kind("static")
     add_includedirs(".")
     add_files("*.c")
-    add_files("sx126x/*.c")
+    add_files("sx126x/*.c")
+target_end()

+ 10 - 0
components/peripheral/mlx90640/xmake.lua

@@ -0,0 +1,10 @@
+
+
+
+target("mlx90640")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../../ui/lcd",{public = true})
+    add_includedirs("../../ui/u8g2",{public = true})
+target_end()

+ 8 - 0
components/peripheral/onchip/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("onchip")
+    set_kind("static")
+    add_files("src/*.c")
+    add_includedirs("inc",{public = true})
+target_end()

+ 8 - 0
components/peripheral/sfd/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("sfd")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs("../../fs/lfs",{public = true})
+target_end()

+ 9 - 0
components/peripheral/sfud/xmake.lua

@@ -0,0 +1,9 @@
+
+
+
+target("sfud")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../../fs/lfs",{public = true})
+target_end()

+ 8 - 0
components/peripheral/soft_keyboard/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("soft_keyboard")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/peripheral/statem/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("statem")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 1 - 0
components/peripheral/usbapp/luat_lib_usbapp.c

@@ -8,6 +8,7 @@
 #include "luat_base.h"
 #include "luat_msgbus.h"
 #include "luat_zbuff.h"
+#include "luat_usbapp.h"
 
 #define USB_ID0 0
 

+ 8 - 0
components/peripheral/usbapp/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("usbapp")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 25 - 0
components/peripheral/xmake.lua

@@ -0,0 +1,25 @@
+
+
+includes("camera")
+includes("io_queue")
+includes("lora")
+includes("mlx90640")
+includes("onchip")
+includes("sfd")
+includes("sfud")
+includes("soft_keyboard")
+includes("statem")
+includes("usbapp")
+
+target("peripheral")
+    set_kind("camera")
+    add_deps("io_queue")
+    add_deps("lora")
+    add_deps("mlx90640")
+    add_deps("onchip")
+    add_deps("sfd")
+    add_deps("sfud")
+    add_deps("soft_keyboard")
+    add_deps("statem")
+    add_deps("usbapp")
+target_end()

+ 8 - 0
components/serialization/cjson/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("cjson")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 9 - 0
components/serialization/cmux/xmake.lua

@@ -0,0 +1,9 @@
+
+
+
+target("cmux")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../../shell",{public = true})
+target_end()

+ 8 - 0
components/serialization/lua-cjson/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("lua-cjson")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/serialization/miniz/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("miniz")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/serialization/protobuf/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("protobuf")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 18 - 0
components/serialization/xmake.lua

@@ -0,0 +1,18 @@
+
+
+includes("cjson")
+includes("cmux")
+includes("lua-cjson")
+includes("miniz")
+includes("protobuf")
+includes("zlib")
+
+target("serialization")
+    set_kind("static")
+    add_deps("cjson")
+    add_deps("cmux")
+    add_deps("lua-cjson")
+    add_deps("miniz")
+    add_deps("protobuf")
+    add_deps("zlib")
+target_end()

+ 8 - 0
components/serialization/zlib/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("zlib")
+    set_kind("static")
+    add_files("*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 10 - 0
components/shell/xmake.lua

@@ -0,0 +1,10 @@
+
+
+
+target("shell")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../serialization/cmux",{public = true})
+    add_includedirs("../tools/i2c-tools",{public = true})
+target_end()

+ 8 - 0
components/tools/coremark/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("coremark")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 10 - 0
components/tools/dbg/xmake.lua

@@ -0,0 +1,10 @@
+
+
+
+target("dbg")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../../serialization/cmux",{public = true})
+    add_includedirs("../../serialization/cjson",{public = true})
+target_end()

+ 8 - 0
components/tools/i2c-tools/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("i2c-tools")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/iconv/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("iconv")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/iotauth/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("iotauth")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/libcoap/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("libcoap")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/luf/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("luf")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/minmea/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("minmea")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/mqttcore/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("mqttcore")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/pack/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("pack")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 8 - 0
components/tools/vmx/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("vmx")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 30 - 0
components/tools/xmake.lua

@@ -0,0 +1,30 @@
+
+
+includes("coremark")
+includes("dbg")
+includes("i2c-tools")
+includes("iconv")
+includes("iotauth")
+includes("libcoap")
+includes("minmea")
+includes("mqttcore")
+includes("pack")
+includes("vmx")
+includes("ymodem")
+includes("zbuff")
+
+target("tools")
+    set_kind("static")
+    add_deps("coremark")
+    add_deps("dbg")
+    add_deps("i2c-tools")
+    add_deps("iconv")
+    add_deps("iotauth")
+    add_deps("libcoap")
+    add_deps("minmea")
+    add_deps("mqttcore")
+    add_deps("pack")
+    add_deps("vmx")
+    add_deps("ymodem")
+    add_deps("zbuff")
+target_end()

+ 8 - 0
components/tools/ymodem/xmake.lua

@@ -0,0 +1,8 @@
+
+
+
+target("ymodem")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+target_end()

+ 9 - 0
components/tools/zbuff/xmake.lua

@@ -0,0 +1,9 @@
+
+
+
+target("zbuff")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../pack",{public = true})
+target_end()

+ 13 - 0
components/ui/eink/xmake.lua

@@ -0,0 +1,13 @@
+
+
+
+target("eink")
+    set_kind("static")
+    add_files("./*.c")
+    add_files("../epaper/*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../epaper",{public = true})
+    add_includedirs("../u8g2",{public = true})
+    add_includedirs("../qrcode",{public = true})
+    add_includedirs("../gtfont/",{public = true})
+target_end()

+ 12 - 0
components/ui/gtfont/xmake.lua

@@ -0,0 +1,12 @@
+
+
+
+target("gtfont")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../lcd",{public = true})
+    add_includedirs("../u8g2/",{public = true})
+    add_includedirs("../eink/",{public = true})
+    add_includedirs("../epaper/",{public = true})
+target_end()

+ 11 - 0
components/ui/lcd/xmake.lua

@@ -0,0 +1,11 @@
+
+
+target("lcd")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../u8g2/",{public = true})
+    add_includedirs("../qrcode/",{public = true})
+    add_includedirs("../tjpgd/",{public = true})
+    add_includedirs("../gtfont/",{public = true})
+target_end()

+ 11 - 0
components/ui/luatfonts/xmake.lua

@@ -0,0 +1,11 @@
+
+
+
+target("luatfonts")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../u8g2",{public = true})
+    add_includedirs("../lvgl",{public = true})
+    add_includedirs("../lvgl/src",{public = true})
+target_end()

+ 14 - 0
components/ui/lvgl/xmake.lua

@@ -0,0 +1,14 @@
+target("lvgl")
+    set_kind("static")
+    --target:set("default", true)
+    add_files("**.c")
+
+    add_includedirs(".",{public = true})
+    add_includedirs("./binding",{public = true})
+    add_includedirs("./gen",{public = true})
+    add_includedirs("./src",{public = true})
+    add_includedirs("./font",{public = true})
+    add_includedirs("../lcd",{public = true})
+    add_includedirs("../u8g2",{public = true})
+    add_includedirs("../qrcode",{public = true})
+target_end()

+ 7 - 0
components/ui/qrcode/xmake.lua

@@ -0,0 +1,7 @@
+
+
+add_includedirs(".",{public = true})
+target("qrcode")
+    set_kind("static")
+    add_files("./*.c")
+target_end()

+ 10 - 0
components/ui/tjpgd/xmake.lua

@@ -0,0 +1,10 @@
+
+
+
+target("tjpgd")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../lcd",{public = true})
+    add_includedirs("../u8g2/",{public = true})
+target_end()

+ 10 - 0
components/ui/u8g2/xmake.lua

@@ -0,0 +1,10 @@
+
+
+target("u8g2")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../qrcode/",{public = true})
+    add_includedirs("../gtfont/",{public = true})
+target_end()
+

+ 9 - 0
components/ui/ufont/xmake.lua

@@ -0,0 +1,9 @@
+
+
+target("ufont")
+    set_kind("static")
+    add_files("./*.c")
+    add_includedirs(".",{public = true})
+    add_includedirs("../lvgl/",{public = true})
+    add_includedirs("../lvgl/src/lv_font",{public = true})
+target_end()

+ 24 - 0
components/ui/xmake.lua

@@ -0,0 +1,24 @@
+includes("lcd")
+includes("u8g2")
+includes("eink")
+includes("lvgl")
+includes("ufont")
+includes("gtfont")
+includes("luatfonts")
+includes("qrcode")
+includes("tjpgd")
+
+--add_deps("u8g2")
+target("ui")
+    set_kind("static")
+    add_deps("lcd")
+    add_deps("u8g2")
+    add_deps("eink")
+    add_deps("lvgl")
+    add_deps("ufont")
+    add_deps("gtfont")
+    add_deps("luatfonts")
+    add_deps("qrcode")
+    add_deps("tjpgd")
+
+target_end()

+ 28 - 4
components/xmake.lua

@@ -1,4 +1,28 @@
-include("fs/")
-include("crypto/")
-include("mempool/")
-include("peripheral")
+
+
+includes("c_common")
+includes("crypto")
+includes("fs")
+includes("mempool")
+includes("serialization")
+includes("tools")
+includes("ui")
+includes("mbedtls")
+includes("peripheral")
+includes("shell")
+includes("network")
+
+target("luatos_components")
+    set_kind("static")
+    add_deps("c_common")
+    add_deps("crypto")
+    add_deps("fs")
+    add_deps("mempool")
+    add_deps("serialization")
+    add_deps("tools")
+    add_deps("ui")
+    add_deps("mbedtls")
+    add_deps("peripheral")
+    add_deps("shell")
+    add_deps("network")
+target_end()

+ 2 - 1
lua/xmake.lua

@@ -1,4 +1,5 @@
-target("lua")
+target("luatos_lua")
     set_kind("static")
     add_includedirs("include")
     add_files("src/*.c")
+target_end()

+ 2 - 1
luat/xmake.lua

@@ -1,4 +1,5 @@
-target("luat")
+target("luatos_core")
     set_kind("static")
     add_includedirs("include")
     add_files("modules/*.c")
+target_end()