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
CalcCrc16(const UINT8 *buf, UINT32 len)39 STATIC 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
OsWriteTlv(UINT8 *tlvBuf, UINT8 type, UINT8 len, UINT8 *value)57 STATIC 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
OsTlvEncode(const TlvTable *table, UINT8 *srcBuf, UINT8 *tlvBuf, INT32 tlvBufLen)72 STATIC UINT32 OsTlvEncode(const TlvTable *table, UINT8 *srcBuf, UINT8 *tlvBuf, INT32 tlvBufLen)
73 {
74 UINT32 len = 0;
75 const TlvTable *tlvTableItem = table;
76
77 while (tlvTableItem->tag != TRACE_TLV_TYPE_NULL) {
78 if ((len + tlvTableItem->elemSize + sizeof(UINT8) + sizeof(UINT8)) > tlvBufLen) {
79 break;
80 }
81 len += OsWriteTlv(tlvBuf + len, tlvTableItem->tag, tlvTableItem->elemSize, srcBuf + tlvTableItem->elemOffset);
82 tlvTableItem++;
83 }
84 return len;
85 }
86
OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen)87 UINT32 OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen)
88 {
89 UINT16 crc;
90 INT32 len;
91 INT32 tlvBufLen;
92 UINT8 *tlvBuf = NULL;
93
94 TraceMsgTlvHead *head = (TraceMsgTlvHead *)dest;
95 tlvBufLen = destLen - sizeof(TraceMsgTlvHead);
96
97 if ((tlvBufLen <= 0) || (table == NULL)) {
98 return 0;
99 }
100
101 tlvBuf = dest + sizeof(TraceMsgTlvHead);
102 len = OsTlvEncode(table, src, tlvBuf, tlvBufLen);
103 crc = CalcCrc16(tlvBuf, len);
104
105 head->magicNum = TRACE_TLV_MSG_HEAD;
106 head->msgType = type;
107 head->len = len;
108 head->crc = crc;
109 return len + sizeof(TraceMsgTlvHead);
110 }
111