更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
trace_tlv.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "trace_tlv.h"
33#include "securec.h"
34
35#define CRC_WIDTH 8
36#define CRC_POLY 0x1021
37#define CRC_TOPBIT 0x8000
38
39STATIC UINT16 CalcCrc16(const UINT8 *buf, UINT32 len)
40{
41 UINT32 i;
42 UINT16 crc = 0;
43
44 for (; len > 0; len--) {
45 crc = crc ^ (*buf++ << CRC_WIDTH);
46 for (i = 0; i < CRC_WIDTH; i++) {
47 if (crc & CRC_TOPBIT) {
48 crc = (crc << 1) ^ CRC_POLY;
49 } else {
50 crc <<= 1;
51 }
52 }
53 }
54 return crc;
55}
56
57STATIC UINT32 OsWriteTlv(UINT8 *tlvBuf, UINT8 type, UINT8 len, UINT8 *value)
58{
59 TraceMsgTlvBody *body = (TraceMsgTlvBody *)tlvBuf;
60
61 if (len == 0) {
62 return 0;
63 }
64
65 body->type = type;
66 body->len = len;
67 /* Do not check return value for performance, if copy failed, only this package will be discarded */
68 (VOID)memcpy_s(body->value, len, value, len);
69 return len + sizeof(body->type) + sizeof(body->len);
70}
71/*
72解码步骤
73
74读取 Tag(或Type)并使用 ntohl 将其转成主机字节序,指针偏移4;
75读取 Length ntohl** 将其转成主机字节序,指针偏移4;
76根据得到的长度读取 Value,若为 int、char、short、long 类型,将其转为主机字节序,指针偏移;若值为字符串,读取后指针偏移 Length;
77重复上述三步,继续读取后面的 TLV 单元。
78*/
79STATIC UINT32 OsTlvEncode(const TlvTable *table, UINT8 *srcBuf, UINT8 *tlvBuf, INT32 tlvBufLen)
80{
81 UINT32 len = 0;
82 const TlvTable *tlvTableItem = table;
83
84 while (tlvTableItem->tag != TRACE_TLV_TYPE_NULL) {
85 if ((len + tlvTableItem->elemSize + sizeof(UINT8) + sizeof(UINT8)) > tlvBufLen) {
86 break;
87 }
88 len += OsWriteTlv(tlvBuf + len, tlvTableItem->tag, tlvTableItem->elemSize, srcBuf + tlvTableItem->elemOffset);
89 tlvTableItem++;
90 }
91 return len;
92}
93
94/*!
95对trace数据进行 Tlv 编码
96
97TLV 是一种可变的格式,其中:
98
99T 可以理解为 Tag 或 Type ,用于标识标签或者编码格式信息;
100L 定义数值的长度;
101V 表示实际的数值。
102T 和 L 的长度固定,一般是2或4个字节,V 的长度由 Length 指定。
103
104要正确的解析对方发来的数据除了统一数据格式之外还要统一字节序。字节序是指多字节数据在计算机内存中存储
105或者网络传输时各字节的存储顺序。字节序一般分为大端和小端。
106
107大端模式(Big-Endian): 高位字节放在内存的低地址端,低位字节排放在内存的高地址端。
108小端模式(Little-Endian): 低位字节放在内存的低地址端,高位字节放在内存的高地址端。
109
110使用 htonl 将 Tag(或Type)转成网络字节序,指针偏移 4;
111使用 htonl 将 Length 转成网络字节序,指针偏移 4;
112若值 Value 为 int、char、short、long 类型,将其转为网络字节序,指针偏移;若值为字符串,写入后指针偏移 Length;
113重复上述三步,继续编码后面的 TLV 单元。
114*/
115UINT32 OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen)
116{
117 UINT16 crc;
118 INT32 len;
119 INT32 tlvBufLen;
120 UINT8 *tlvBuf = NULL;
121
122 TraceMsgTlvHead *head = (TraceMsgTlvHead *)dest;
123 tlvBufLen = destLen - sizeof(TraceMsgTlvHead);
124
125 if ((tlvBufLen <= 0) || (table == NULL)) {
126 return 0;
127 }
128
129 tlvBuf = dest + sizeof(TraceMsgTlvHead);
130 len = OsTlvEncode(table, src, tlvBuf, tlvBufLen);
131 crc = CalcCrc16(tlvBuf, len);
132
133 head->magicNum = TRACE_TLV_MSG_HEAD;
134 head->msgType = type;
135 head->len = len;
136 head->crc = crc;
137 return len + sizeof(TraceMsgTlvHead);
138}
UINT32 OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen)
Encode trace raw data.
Definition: trace_tlv.c:115
unsigned short UINT16
Definition: los_typedef.h:56
signed int INT32
Definition: los_typedef.h:60
unsigned char UINT8
Definition: los_typedef.h:55
unsigned int UINT32
Definition: los_typedef.h:57
TLV表,主要用于TLV的解码阶段
Definition: trace_tlv.h:60
UINT8 tag
Definition: trace_tlv.h:61
UINT8 elemOffset
Definition: trace_tlv.h:62
UINT8 elemSize
Definition: trace_tlv.h:63
TLV协议 消息体
Definition: trace_tlv.h:54
UINT8 len
长度
Definition: trace_tlv.h:56
UINT8 type
类型
Definition: trace_tlv.h:55
UINT8 value[]
内容
Definition: trace_tlv.h:57
TLV协议 消息头 https://blog.csdn.net/Shallwen_Deng/article/details/88930288
Definition: trace_tlv.h:47
UINT8 magicNum
魔法数字
Definition: trace_tlv.h:48
UINT16 len
消息长度
Definition: trace_tlv.h:50
UINT16 crc
CRC校验
Definition: trace_tlv.h:51
UINT8 msgType
消息类型
Definition: trace_tlv.h:49
STATIC UINT32 OsWriteTlv(UINT8 *tlvBuf, UINT8 type, UINT8 len, UINT8 *value)
Definition: trace_tlv.c:57
STATIC UINT32 OsTlvEncode(const TlvTable *table, UINT8 *srcBuf, UINT8 *tlvBuf, INT32 tlvBufLen)
Definition: trace_tlv.c:79
STATIC UINT16 CalcCrc16(const UINT8 *buf, UINT32 len)
Definition: trace_tlv.c:39