xref: /aosp_15_r20/external/OpenCSD/decoder/include/pkt_printers/pkt_printer_t.h (revision 02ca8ccacfba7e0df68f3332a95f3180334d6649)
1 /*
2  * \file       pkt_printer_t.h
3  * \brief      OpenCSD : Test packet printer.
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 #ifndef ARM_PKT_PRINTER_T_H_INCLUDED
36 #define ARM_PKT_PRINTER_T_H_INCLUDED
37 
38 #include "opencsd.h"
39 
40 #include <string>
41 #include <sstream>
42 //#include <iostream>
43 #include <iomanip>
44 
45 template<class P>
46 class PacketPrinter : public IPktDataIn<P>, public IPktRawDataMon<P>, public ItemPrinter
47 {
48 public:
49     PacketPrinter(const uint8_t trcID);
50     PacketPrinter(const uint8_t trcID, ocsdMsgLogger *pMsgLogger);
51     virtual ~PacketPrinter();
52 
53 
54     virtual ocsd_datapath_resp_t PacketDataIn( const ocsd_datapath_op_t op,
55                                         const ocsd_trc_index_t index_sop,
56                                         const P *p_packet_in);
57 
58     virtual void RawPacketDataMon( const ocsd_datapath_op_t op,
59                                    const ocsd_trc_index_t index_sop,
60                                    const P *pkt,
61                                    const uint32_t size,
62                                    const uint8_t *p_data);
63 
64 
65 private:
66     void printIdx_ID(const ocsd_trc_index_t index_sop);
67 
68     uint8_t m_trcID;
69     bool m_bRawPrint;
70     std::ostringstream m_oss;
71     ocsd_datapath_resp_t m_last_resp;
72 
73 };
74 
PacketPrinter(uint8_t trcID)75 template<class P> PacketPrinter<P>::PacketPrinter(uint8_t trcID) :
76     m_trcID(trcID),
77     m_bRawPrint(false),
78     m_last_resp(OCSD_RESP_CONT)
79 {
80 }
81 
PacketPrinter(const uint8_t trcID,ocsdMsgLogger * pMsgLogger)82 template<class P> PacketPrinter<P>::PacketPrinter(const uint8_t trcID, ocsdMsgLogger *pMsgLogger) :
83     m_trcID(trcID),
84     m_bRawPrint(false),
85     m_last_resp(OCSD_RESP_CONT)
86 {
87     setMessageLogger(pMsgLogger);
88 }
89 
~PacketPrinter()90 template<class P> PacketPrinter<P>::~PacketPrinter()
91 {
92 }
93 
PacketDataIn(const ocsd_datapath_op_t op,const ocsd_trc_index_t index_sop,const P * p_packet_in)94 template<class P> ocsd_datapath_resp_t PacketPrinter<P>::PacketDataIn( const ocsd_datapath_op_t op,
95                                         const ocsd_trc_index_t index_sop,
96                                         const P *p_packet_in)
97 {
98     std::string pktstr;
99     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
100 
101     if (is_muted())
102         return resp;
103 
104     // wait / flush test verification
105     if(!m_bRawPrint && (m_last_resp == OCSD_RESP_WAIT))
106     {
107         // expect a flush or a complete reset after a wait.
108         if((op != OCSD_OP_FLUSH) && (op != OCSD_OP_RESET))
109         {
110             m_oss <<"ID:"<< std::hex << (uint32_t)m_trcID << "\tERROR: FLUSH operation expected after wait on trace decode path\n";
111             itemPrintLine(m_oss.str());
112             m_oss.str("");
113             return OCSD_RESP_FATAL_INVALID_OP;
114         }
115     }
116 
117     switch(op)
118     {
119     case OCSD_OP_DATA:
120         p_packet_in->toString(pktstr);
121         if(!m_bRawPrint)
122             printIdx_ID(index_sop);
123         m_oss << ";\t" << pktstr << std::endl;
124 
125         // test the wait/flush response mechnism
126         if(getTestWaits() && !m_bRawPrint)
127         {
128             decTestWaits();
129             resp = OCSD_RESP_WAIT;
130         }
131         break;
132 
133     case OCSD_OP_EOT:
134         m_oss <<"ID:"<< std::hex << (uint32_t)m_trcID << "\tEND OF TRACE DATA\n";
135         break;
136 
137     case OCSD_OP_FLUSH:
138         m_oss <<"ID:"<< std::hex << (uint32_t)m_trcID << "\tFLUSH operation on trace decode path\n";
139         break;
140 
141     case OCSD_OP_RESET:
142         m_oss <<"ID:"<< std::hex << (uint32_t)m_trcID << "\tRESET operation on trace decode path\n";
143         break;
144     }
145 
146     m_last_resp = resp;
147     itemPrintLine(m_oss.str());
148     m_oss.str("");
149     return resp;
150 }
151 
RawPacketDataMon(const ocsd_datapath_op_t op,const ocsd_trc_index_t index_sop,const P * pkt,const uint32_t size,const uint8_t * p_data)152 template<class P> void PacketPrinter<P>::RawPacketDataMon( const ocsd_datapath_op_t op,
153                                    const ocsd_trc_index_t index_sop,
154                                    const P *pkt,
155                                    const uint32_t size,
156                                    const uint8_t *p_data)
157 {
158     if (is_muted())
159         return;
160 
161     switch(op)
162     {
163     case OCSD_OP_DATA:
164         printIdx_ID(index_sop);
165         m_oss << "; [";
166         if((size > 0) && (p_data != 0))
167         {
168             uint32_t data = 0;
169             for(uint32_t i = 0; i < size; i++)
170             {
171                 data = (uint32_t)(p_data[i] & 0xFF);
172                 m_oss << "0x" << std::hex << std::setw(2) << std::setfill('0') << data << " ";
173             }
174         }
175         m_oss << "]";
176         m_bRawPrint = true;
177         PacketDataIn(op,index_sop,pkt);
178         m_bRawPrint = false;
179         break;
180 
181     default:
182         PacketDataIn(op,index_sop,pkt);
183         break;
184     }
185 
186 }
187 
printIdx_ID(const ocsd_trc_index_t index_sop)188 template<class P> void PacketPrinter<P>::printIdx_ID(const ocsd_trc_index_t index_sop)
189 {
190     m_oss << "Idx:" << std::dec << index_sop << "; ID:"<< std::hex << (uint32_t)m_trcID;
191 }
192 
193 #endif // ARM_PKT_PRINTER_T_H_INCLUDED
194 
195 /* End of File pkt_printer_t.h */
196