xref: /freebsd/contrib/opencsd/decoder/source/stm/trc_pkt_elem_stm.cpp (revision c120c5646da1a1d2c4d90fd069a7e2a8d559eb46)
1 /*
2  * \file       trc_pkt_elem_stm.cpp
3  * \brief      OpenCSD : STM decode - packet class
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 /*
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sstream>
36 #include <iomanip>
37 #include "opencsd/stm/trc_pkt_elem_stm.h"
38 
StmTrcPacket()39 StmTrcPacket::StmTrcPacket()
40 {
41     initStartState();
42 }
43 
operator =(const ocsd_stm_pkt * p_pkt)44 StmTrcPacket &StmTrcPacket::operator =(const ocsd_stm_pkt *p_pkt)
45 {
46     *dynamic_cast<ocsd_stm_pkt *>(this) = *p_pkt;
47     return *this;
48 }
49 
initStartState()50 void StmTrcPacket::initStartState()
51 {
52     master = 0;
53     channel = 0;
54     timestamp = 0;
55     ts_type = STM_TS_UNKNOWN;
56     type =  STM_PKT_NOTSYNC;
57     initNextPacket();
58 }
59 
initNextPacket()60 void StmTrcPacket::initNextPacket()
61 {
62     err_type = STM_PKT_NO_ERR_TYPE;
63     pkt_ts_bits = 0;
64     pkt_has_marker = 0;
65     pkt_has_ts = 0;
66 }
67 
setTS(const uint64_t ts_val,const uint8_t updatedBits)68 void StmTrcPacket::setTS(const uint64_t ts_val, const uint8_t updatedBits)
69 {
70     if(updatedBits == 64)
71     {
72         timestamp = ts_val;
73     }
74     else
75     {
76         uint64_t mask = (0x1ULL << updatedBits) - 1;
77         timestamp &= ~mask;
78         timestamp |= ts_val & mask;
79     }
80     pkt_ts_bits = updatedBits;  // mark number of bits
81     pkt_has_ts = 1;
82 }
83 
84 // printing
toString(std::string & str) const85 void StmTrcPacket::toString(std::string &str) const
86 {
87     std::string name, desc;
88     std::ostringstream oss;
89 
90     pktTypeName(type,name, desc);
91     str = name + ":" + desc;
92 
93     // extended information
94     switch(type)
95     {
96     case STM_PKT_INCOMPLETE_EOT:
97     case STM_PKT_BAD_SEQUENCE:
98         pktTypeName(err_type,name, desc);
99         str+= "[" + name + "]";
100         break;
101 
102     case STM_PKT_VERSION:
103         oss << "; Ver=" << (uint16_t)payload.D8;
104         str+= oss.str();
105         break;
106 
107     case STM_PKT_FREQ:
108         oss << "; Freq=" << std::dec << payload.D32 << "Hz";
109         str+= oss.str();
110         break;
111 
112     case STM_PKT_TRIG:
113         oss << "; TrigData=0x" << std::hex << std::setw(2) << std::setfill('0') << (uint16_t)payload.D8;
114         str+= oss.str();
115         break;
116 
117     case STM_PKT_M8:
118         oss << "; Master=0x" << std::hex << std::setw(2) << std::setfill('0') << (uint16_t)master;
119         str+= oss.str();
120         break;
121 
122     case STM_PKT_C8:
123     case STM_PKT_C16:
124         oss << "; Chan=0x" << std::hex << std::setw(4) << std::setfill('0') << channel;
125         str+= oss.str();
126         break;
127 
128     case STM_PKT_D4:
129         oss << "; Data=0x" << std::hex << std::setw(1) << (uint16_t)(payload.D8 & 0xF);
130         str+= oss.str();
131         break;
132 
133     case STM_PKT_D8:
134         oss << "; Data=0x" << std::hex << std::setw(2) << std::setfill('0') << (uint16_t)payload.D8;
135         str+= oss.str();
136         break;
137 
138     case STM_PKT_D16:
139         oss << "; Data=0x" << std::hex << std::setw(4) << std::setfill('0') << payload.D16;
140         str+= oss.str();
141         break;
142 
143     case STM_PKT_D32:
144         oss << "; Data=0x" << std::hex << std::setw(8) << std::setfill('0') << payload.D32;
145         str+= oss.str();
146         break;
147 
148     case STM_PKT_D64:
149         oss << "; Data=0x" << std::hex << std::setw(16) << std::setfill('0') << payload.D64;
150         str+= oss.str();
151         break;
152     }
153 
154     if(isTSPkt())
155     {
156         std::string valStr;
157         trcPrintableElem::getValStr(valStr,64,64,timestamp,true,pkt_ts_bits);
158         str += "; TS=" + valStr;
159     }
160 }
161 
toStringFmt(const uint32_t fmtFlags,std::string & str) const162 void StmTrcPacket::toStringFmt(const uint32_t fmtFlags, std::string &str) const
163 {
164     // no formatting for now.
165     toString(str);
166 }
167 
pktTypeName(const ocsd_stm_pkt_type pkt_type,std::string & name,std::string & desc) const168 void StmTrcPacket::pktTypeName(const ocsd_stm_pkt_type pkt_type, std::string &name, std::string &desc) const
169 {
170     std::ostringstream oss_name;
171     std::ostringstream oss_desc;
172     bool addMarkerTS = false;
173 
174 
175     switch(pkt_type)
176     {
177     case STM_PKT_RESERVED:
178         oss_name << "RESERVED";
179         oss_desc << "Reserved Packet Header";
180         break;
181 
182     case STM_PKT_NOTSYNC:
183         oss_name << "NOTSYNC";
184         oss_desc << "STM not synchronised";
185         break;
186 
187     case STM_PKT_INCOMPLETE_EOT:
188         oss_name << "INCOMPLETE_EOT";
189         oss_desc << "Incomplete packet flushed at end of trace";
190         break;
191 
192     case STM_PKT_NO_ERR_TYPE:
193         oss_name << "NO_ERR_TYPE";
194         oss_desc << "Error type not set";
195         break;
196 
197     case STM_PKT_BAD_SEQUENCE:
198         oss_name << "BAD_SEQUENCE";
199         oss_desc << "Invalid sequence in packet";
200         break;
201 
202     case STM_PKT_ASYNC:
203         oss_name << "ASYNC";
204         oss_desc << "Alignment synchronisation packet";
205         break;
206 
207     case STM_PKT_VERSION:
208         oss_name << "VERSION";
209         oss_desc << "Version packet";
210         break;
211 
212     case STM_PKT_FREQ:
213         oss_name << "FREQ";
214         oss_desc << "Frequency packet";
215         break;
216 
217     case STM_PKT_NULL:
218         oss_name << "NULL";
219         oss_desc << "Null packet";
220         break;
221 
222     case STM_PKT_TRIG:
223         oss_name << "TRIG";
224         oss_desc << "Trigger packet";
225         addMarkerTS = true;
226         break;
227 
228     case STM_PKT_GERR:
229         oss_name << "GERR";
230         oss_desc << "Global Error";
231         break;
232 
233     case STM_PKT_MERR:
234         oss_name << "MERR";
235         oss_desc << "Master Error";
236         break;
237 
238     case STM_PKT_M8:
239         oss_name << "M8";
240         oss_desc << "Set current master";
241         break;
242 
243     case STM_PKT_C8:
244         oss_name << "C8";
245         oss_desc << "Set current channel";
246         break;
247 
248     case STM_PKT_C16:
249         oss_name << "C16";
250         oss_desc << "Set current channel";
251         break;
252 
253     case STM_PKT_FLAG:
254         oss_name << "FLAG";
255         oss_desc << "Flag packet";
256         addMarkerTS = true;
257         break;
258 
259     case STM_PKT_D4:
260         oss_name << "D4";
261         oss_desc << "4 bit data";
262         addMarkerTS = true;
263         break;
264 
265     case STM_PKT_D8:
266         oss_name << "D8";
267         oss_desc << "8 bit data";
268         addMarkerTS = true;
269         break;
270 
271     case STM_PKT_D16:
272         oss_name << "D16";
273         oss_desc << "16 bit data";
274         addMarkerTS = true;
275         break;
276 
277     case STM_PKT_D32:
278         oss_name << "D32";
279         oss_desc << "32 bit data";
280         addMarkerTS = true;
281         break;
282 
283     case STM_PKT_D64:
284         oss_name << "D64";
285         oss_desc << "64 bit data";
286         addMarkerTS = true;
287         break;
288 
289     default:
290         oss_name << "UNKNOWN";
291         oss_desc << "ERROR: unknown packet type";
292         break;
293     }
294 
295     if(addMarkerTS)
296     {
297         if(isMarkerPkt())
298         {
299             oss_name << "M";
300             oss_desc << " + marker";
301         }
302 
303         if(isTSPkt())
304         {
305             oss_name << "TS";
306             oss_desc << " + timestamp";
307         }
308     }
309     desc = oss_desc.str();
310     name =  oss_name.str();
311 }
312 
313 
314 /* End of File trc_pkt_elem_stm.cpp */
315