• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef __cplusplus
36 #if __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 #endif /* __cplusplus */
40 
41 #define CRC_WIDTH  8
42 #define CRC_POLY   0x1021
43 #define CRC_TOPBIT 0x8000
44 
CalcCrc16(const UINT8 * buf,UINT32 len)45 STATIC UINT16 CalcCrc16(const UINT8 *buf, UINT32 len)
46 {
47     UINT32 i;
48     UINT16 crc = 0;
49 
50     for (; len > 0; len--) {
51         crc = crc ^ (*buf++ << CRC_WIDTH);
52         for (i = 0; i < CRC_WIDTH; i++) {
53             if (crc & CRC_TOPBIT) {
54                 crc = (crc << 1) ^ CRC_POLY;
55             } else {
56                 crc <<= 1;
57             }
58         }
59     }
60     return crc;
61 }
62 
OsWriteTlv(UINT8 * tlvBuf,UINT8 type,UINT8 len,UINT8 * value)63 STATIC UINT32 OsWriteTlv(UINT8 *tlvBuf, UINT8 type, UINT8 len, UINT8 *value)
64 {
65     TraceMsgTlvBody *body = (TraceMsgTlvBody *)tlvBuf;
66 
67     if (len == 0) {
68         return 0;
69     }
70 
71     body->type = type;
72     body->len = len;
73     /* Do not check return value for performance, if copy failed, only this package will be discarded */
74     (VOID)memcpy_s(body->value, len, value, len);
75     return len + sizeof(body->type) + sizeof(body->len);
76 }
77 
OsTlvEncode(const TlvTable * table,UINT8 * srcBuf,UINT8 * tlvBuf,INT32 tlvBufLen)78 STATIC UINT32 OsTlvEncode(const TlvTable *table, UINT8 *srcBuf, UINT8 *tlvBuf, INT32 tlvBufLen)
79 {
80     UINT32 len = 0;
81     const TlvTable *tlvTableItem = table;
82 
83     while (tlvTableItem->tag != TRACE_TLV_TYPE_NULL) {
84         if ((len + tlvTableItem->elemSize + sizeof(UINT8) + sizeof(UINT8)) > tlvBufLen) {
85             break;
86         }
87         len += OsWriteTlv(tlvBuf + len, tlvTableItem->tag, tlvTableItem->elemSize, srcBuf + tlvTableItem->elemOffset);
88         tlvTableItem++;
89     }
90     return len;
91 }
92 
OsTraceDataEncode(UINT8 type,const TlvTable * table,UINT8 * src,UINT8 * dest,INT32 destLen)93 UINT32 OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen)
94 {
95     UINT16 crc;
96     INT32 len;
97     INT32 tlvBufLen;
98     UINT8 *tlvBuf = NULL;
99 
100     TraceMsgTlvHead *head = (TraceMsgTlvHead *)dest;
101     tlvBufLen = destLen - sizeof(TraceMsgTlvHead);
102 
103     if ((tlvBufLen <= 0) || (table == NULL)) {
104         return 0;
105     }
106 
107     tlvBuf = dest + sizeof(TraceMsgTlvHead);
108     len = OsTlvEncode(table, src, tlvBuf, tlvBufLen);
109     crc = CalcCrc16(tlvBuf, len);
110 
111     head->magicNum = TRACE_TLV_MSG_HEAD;
112     head->msgType  = type;
113     head->len      = len;
114     head->crc      = crc;
115     return len + sizeof(TraceMsgTlvHead);
116 }
117 
118 #ifdef __cplusplus
119 #if __cplusplus
120 }
121 #endif /* __cplusplus */
122 #endif /* __cplusplus */
123