yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*************************************************************************************************/
/*!
 *  \file
 *
 *  \brief  Trace message implementation.
 *
 *  Copyright (c) 2009-2018 Arm Ltd. All Rights Reserved.
 *
 *  Copyright (c) 2019-2020 Packetcraft, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/*************************************************************************************************/
 
#include "wsf_types.h"
#include "wsf_trace.h"
 
#include "wsf_bufio.h"
#include "wsf_buf.h"
#include "pal_sys.h"
#include <stdarg.h>
#include "wsf_assert.h"
#include "wsf_cs.h"
 
/**************************************************************************************************
  Macros
**************************************************************************************************/
 
/*! \brief      Length of newline and carriage return suffix. */
#define WSF_TRACE_SUFFIX_LEN 2
 
#ifndef WSF_PRINTF_MAX_LEN
/*! \brief      Maximum length of a trace string. */
#define WSF_PRINTF_MAX_LEN 128
#endif
 
#ifndef WSF_TOKEN_RING_BUF_SIZE
/*! \brief      Size of token ring buffer (multiple of 2^N). */
#define WSF_TOKEN_RING_BUF_SIZE 64
#endif
 
/*! \brief      Ring buffer flow control condition detected. */
#define WSF_TOKEN_FLAG_FLOW_CTRL (1 << 28)
 
/*! UART TX buffer size. */
#ifndef WSF_TRACE_BUFIO_BUFFER_SIZE
#define WSF_TRACE_BUFIO_BUFFER_SIZE 2048U
#endif
 
/**************************************************************************************************
  Data types
**************************************************************************************************/
 
/*! \brief Trace control block. */
static struct
{
    WsfTraceHandler_t sendMsgCback; /*!< Send trace message callback. */
    uint32_t numDropMsg;            /*!< Number of dropped messages since last successful. */
    bool_t enabled;                 /*!< Tracing state. */
 
#if WSF_TOKEN_ENABLED == TRUE
    union
    {
        struct
        {
            uint32_t token; /*!< Token. */
            uint32_t param; /*!< Parameter. */
        } v;                /*!< Value accessor. */
        /* Use native packing for speed. Host will resolve endian. */
        uint8_t buf[8];                 /*!< Buffer accessor. */
    } ringBuf[WSF_TOKEN_RING_BUF_SIZE]; /*!< Token message ring buffer. */
    uint32_t prodIdx;                   /*!< Ring buffer producer index. */
    uint32_t consIdx;                   /*!< Ring buffer consumer index. */
#endif
} wsfTraceCb;
 
#if WSF_TOKEN_ENABLED == TRUE
/*************************************************************************************************/
/*!
 *  \brief  Store tokenized message.
 *
 *  \param  tok      Message token.
 *  \param  param    Message parameter.
 */
/*************************************************************************************************/
void WsfToken(uint32_t tok, uint32_t param)
{
    static uint32_t flags = 0;
 
    if (!wsfTraceCb.enabled)
    {
        return;
    }
 
    WSF_CS_INIT(cs);
    uint32_t lock = WSF_CS_ENTER();
 
    uint32_t prodIdx = (wsfTraceCb.prodIdx + 1) & (WSF_TOKEN_RING_BUF_SIZE - 1);
 
    if (prodIdx != wsfTraceCb.consIdx)
    {
        wsfTraceCb.ringBuf[wsfTraceCb.prodIdx].v.token = tok | flags;
        wsfTraceCb.ringBuf[wsfTraceCb.prodIdx].v.param = param;
        wsfTraceCb.prodIdx = prodIdx;
        flags = 0;
    }
    else
    {
        flags = WSF_TOKEN_FLAG_FLOW_CTRL;
    }
 
    WSF_CS_EXIT(lock);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Service the trace ring buffer.
 *
 *  \return TRUE if trace messages pending, FALSE otherwise.
 *
 *  This routine is called in the main loop for a "push" type trace systems.
 */
/*************************************************************************************************/
bool_t WsfTokenService(void)
{
    WSF_ASSERT(wsfTraceCb.sendMsgCback);
 
    if (wsfTraceCb.consIdx != wsfTraceCb.prodIdx)
    {
        /* Advance consumer counter only if write successful. */
        if (wsfTraceCb.sendMsgCback((uint8_t *)&wsfTraceCb.ringBuf[wsfTraceCb.consIdx], sizeof(wsfTraceCb.ringBuf[0])))
        {
            wsfTraceCb.consIdx = (wsfTraceCb.consIdx + 1) & (WSF_TOKEN_RING_BUF_SIZE - 1);
        }
        return TRUE;
    }
 
    return FALSE;
}
#endif
 
#if WSF_TRACE_ENABLED == TRUE
/*************************************************************************************************/
/*!
 *  \brief  Create overflow message.
 *
 *  \param  pBuf    Buffer to store output in.
 *  \param  pStr    Format string.
 *  Addition parameters variable arguments to the format string.
 *
 *  \return Length in bytes of overflow message.
 */
/*************************************************************************************************/
uint8_t wsfTraceOverFlowMessage(char *pBuf, const char *pStr, ...)
{
    uint8_t len;
    va_list args;
 
    va_start(args, pStr);
 
    len = PrintVsn(pBuf, WSF_PRINTF_MAX_LEN, pStr, args);
 
    va_end(args);
 
    return len;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Output trace message.
 *
 *  \param  pStr    Format string
 *  Addition parameters variable arguments to the format string.
 */
/*************************************************************************************************/
void WsfTrace(const char *pStr, ...)
{
    if (!wsfTraceCb.enabled)
    {
        /* Discard message when disabled. */
        return;
    }
 
    if (wsfTraceCb.sendMsgCback != NULL)
    {
        uint32_t len;
        va_list args;
        /* Use heap memory to ease stack utilization. */
        static char buf[WSF_PRINTF_MAX_LEN];
 
        va_start(args, pStr);
 
        /* Dropped message notification. */
        if (wsfTraceCb.numDropMsg)
        {
            static char dropMsg[] = ">>> Trace buffer overflowed; %u message(s) lost <<<\r\n";
 
            len = wsfTraceOverFlowMessage(buf, dropMsg, wsfTraceCb.numDropMsg);
 
            if (wsfTraceCb.sendMsgCback((uint8_t *)buf, len) == FALSE)
            {
                wsfTraceCb.numDropMsg++;
 
                /* Trace I/O flow control continues. */
                return;
            }
 
            wsfTraceCb.numDropMsg = 0;
        }
 
        /* Format message. */
        len = PrintVsn(buf, WSF_PRINTF_MAX_LEN - WSF_TRACE_SUFFIX_LEN, pStr, args);
        buf[len++] = '\r';
        buf[len++] = '\n';
 
        /* Deliver message. */
        if (wsfTraceCb.sendMsgCback((uint8_t *)buf, (uint8_t)len) == FALSE)
        {
            /* Trace I/O flow controlled; drop message. */
            wsfTraceCb.numDropMsg = 1;
        }
    }
}
#endif
 
/*************************************************************************************************/
/*!
 *  \brief  Enable trace messages.
 *
 *  \param  enable    TRUE to enable, FALSE to disable
 */
/*************************************************************************************************/
void WsfTraceEnable(bool_t enable)
{
    WSF_ASSERT(wsfTraceCb.sendMsgCback);
    wsfTraceCb.enabled = enable;
 
    /*
     * TOKEN | TRACE | Action
     *     0 |     0 | No tracing of any kind
     *     0 |     1 | Tracing through buffered UART
     *     1 |     0 | Tokenized tracing through CHCI
     *     1 |     1 | Tokenized tracing through buffered UART
     */
    (void)(wsfTraceCb); // fix warning
}
 
/*************************************************************************************************/
/*!
 *  \brief  Register trace handler.
 *
 *  \param  traceCback    Token event handler.
 *
 *  This routine registers trace output handler. This callback is called when the trace data is
 *  ready for writing.
 */
/*************************************************************************************************/
void WsfTraceRegisterHandler(WsfTraceHandler_t traceCback)
{
    wsfTraceCb.sendMsgCback = traceCback;
}