2 * \file trc_pkt_proc_base.h
3 * \brief OpenCSD : Trace packet processor base class.
5 * \copyright Copyright (c) 2015, ARM Limited. All Rights Reserved.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
20 * 3. Neither the name of the copyright holder nor the names of its contributors
21 * may be used to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef ARM_TRC_PKT_PROC_BASE_H_INCLUDED
37 #define ARM_TRC_PKT_PROC_BASE_H_INCLUDED
39 #include "interfaces/trc_data_raw_in_i.h"
40 #include "interfaces/trc_pkt_in_i.h"
41 #include "interfaces/trc_pkt_raw_in_i.h"
42 #include "interfaces/trc_indexer_pkt_i.h"
44 #include "trc_component.h"
45 #include "comp_attach_pt_t.h"
47 /** @defgroup ocsd_pkt_proc OpenCSD Library : Packet Processors.
48 @brief Classes providing Protocol Packet Processing capability.
50 Packet processors take an incoming byte stream and convert into discrete packets for the
51 required trace protocol.
58 * @brief Base Packet processing interface
60 * Defines the packet processing methods that protocol specific processors must
64 class TrcPktProcI : public TraceComponent, public ITrcDataIn
67 TrcPktProcI(const char *component_name);
68 TrcPktProcI(const char *component_name, int instIDNum);
69 virtual ~TrcPktProcI() {};
71 /** Trace byte data input interface - from ITrcDataIn.
73 virtual ocsd_datapath_resp_t TraceDataIn( const ocsd_datapath_op_t op,
74 const ocsd_trc_index_t index,
75 const uint32_t dataBlockSize,
76 const uint8_t *pDataBlock,
77 uint32_t *numBytesProcessed) = 0;
81 /* implementation packet processing interface */
83 /*! @brief Implementation function for the OCSD_OP_DATA operation */
84 virtual ocsd_datapath_resp_t processData( const ocsd_trc_index_t index,
85 const uint32_t dataBlockSize,
86 const uint8_t *pDataBlock,
87 uint32_t *numBytesProcessed) = 0;
89 virtual ocsd_datapath_resp_t onEOT() = 0; //!< Implementation function for the OCSD_OP_EOT operation
90 virtual ocsd_datapath_resp_t onReset() = 0; //!< Implementation function for the OCSD_OP_RESET operation
91 virtual ocsd_datapath_resp_t onFlush() = 0; //!< Implementation function for the OCSD_OP_FLUSH operation
92 virtual ocsd_err_t onProtocolConfig() = 0; //!< Called when the configuration object is passed to the decoder.
93 virtual const bool isBadPacket() const = 0; //!< check if the current packet is an error / bad packet
96 inline TrcPktProcI::TrcPktProcI(const char *component_name) :
97 TraceComponent(component_name)
101 inline TrcPktProcI::TrcPktProcI(const char *component_name, int instIDNum) :
102 TraceComponent(component_name,instIDNum)
107 * @class TrcPktProcBase
108 * @brief Packet Processor base class. Provides common infrastructure and interconnections for packet processors.
110 * The class is a templated base class.
111 * - P - this is the packet object class.
112 * - Pt - this is the packet type class.
113 * - Pc - this is the packet configuration class.
115 * implementations will provide concrete classes for each of these to operate under the common infrastructures.
116 * The base provides the trace data in (ITrcDataIn) interface and operates on the incoming operation type.
118 * Implementions override the 'onFn()' and data process functions defined in TrcPktProcI,
119 * with the base class ensuring consistent ordering of operations.
122 template <class P, class Pt, class Pc>
123 class TrcPktProcBase : public TrcPktProcI
126 TrcPktProcBase(const char *component_name);
127 TrcPktProcBase(const char *component_name, int instIDNum);
128 virtual ~TrcPktProcBase();
130 /** Byte trace data input interface defined in ITrcDataIn
132 The base class implementation processes the operation to call the
133 interface functions on TrcPktProcI.
135 virtual ocsd_datapath_resp_t TraceDataIn( const ocsd_datapath_op_t op,
136 const ocsd_trc_index_t index,
137 const uint32_t dataBlockSize,
138 const uint8_t *pDataBlock,
139 uint32_t *numBytesProcessed);
142 /* component attachment points */
144 //! Attachement point for the protocol packet output
145 componentAttachPt<IPktDataIn<P>> *getPacketOutAttachPt() { return &m_pkt_out_i; };
146 //! Attachment point for the protocol packet monitor
147 componentAttachPt<IPktRawDataMon<P>> *getRawPacketMonAttachPt() { return &m_pkt_raw_mon_i; };
149 //! Attachment point for a packet indexer
150 componentAttachPt<ITrcPktIndexer<Pt>> *getTraceIDIndexerAttachPt() { return &m_pkt_indexer_i; };
152 /* protocol configuration */
153 //!< Set the protocol specific configuration for the decoder.
154 virtual ocsd_err_t setProtocolConfig(const Pc *config);
155 //!< Get the configuration for the decoder.
156 virtual const Pc *getProtocolConfig() const { return m_config; };
160 /* data output functions */
161 ocsd_datapath_resp_t outputDecodedPacket(const ocsd_trc_index_t index_sop, const P *pkt);
163 void outputRawPacketToMonitor( const ocsd_trc_index_t index_sop,
166 const uint8_t *p_data);
168 void indexPacket(const ocsd_trc_index_t index_sop, const Pt *packet_type);
170 ocsd_datapath_resp_t outputOnAllInterfaces(const ocsd_trc_index_t index_sop, const P *pkt, const Pt *pkt_type, std::vector<uint8_t> &pktdata);
172 ocsd_datapath_resp_t outputOnAllInterfaces(const ocsd_trc_index_t index_sop, const P *pkt, const Pt *pkt_type, const uint8_t *pktdata, uint32_t pktlen);
174 /*! Let the derived class figure out if it needs to collate and send raw data.
175 can improve wait for sync performance if we do not need to save and send unsynced data.
177 const bool hasRawMon() const;
179 /* the protocol configuration */
182 void ClearConfigObj(); // remove our copy of the config
184 const bool checkInit(); // return true if init (configured and at least one output sink attached), false otherwise.
188 ocsd_datapath_resp_t Reset(const ocsd_trc_index_t index);
189 ocsd_datapath_resp_t Flush();
190 ocsd_datapath_resp_t EOT();
192 componentAttachPt<IPktDataIn<P>> m_pkt_out_i;
193 componentAttachPt<IPktRawDataMon<P>> m_pkt_raw_mon_i;
195 componentAttachPt<ITrcPktIndexer<Pt>> m_pkt_indexer_i;
200 template<class P,class Pt, class Pc> TrcPktProcBase<P, Pt, Pc>::TrcPktProcBase(const char *component_name) :
201 TrcPktProcI(component_name),
207 template<class P,class Pt, class Pc> TrcPktProcBase<P, Pt, Pc>::TrcPktProcBase(const char *component_name, int instIDNum) :
208 TrcPktProcI(component_name, instIDNum),
214 template<class P,class Pt, class Pc> TrcPktProcBase<P, Pt, Pc>::~TrcPktProcBase()
219 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::TraceDataIn( const ocsd_datapath_op_t op,
220 const ocsd_trc_index_t index,
221 const uint32_t dataBlockSize,
222 const uint8_t *pDataBlock,
223 uint32_t *numBytesProcessed)
225 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
230 if((dataBlockSize == 0) || (pDataBlock == 0) || (numBytesProcessed == 0))
232 if(numBytesProcessed)
233 *numBytesProcessed = 0; // ensure processed bytes value set to 0.
234 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_INVALID_PARAM_VAL,"Packet Processor: Zero length data block or NULL pointer error\n"));
235 resp = OCSD_RESP_FATAL_INVALID_PARAM;
238 resp = processData(index,dataBlockSize,pDataBlock,numBytesProcessed);
254 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_INVALID_PARAM_VAL,"Packet Processor : Unknown Datapath operation\n"));
255 resp = OCSD_RESP_FATAL_INVALID_OP;
262 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::Reset(const ocsd_trc_index_t index)
264 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
266 // reset the trace decoder attachment on main data path.
267 if(m_pkt_out_i.hasAttachedAndEnabled())
268 resp = m_pkt_out_i.first()->PacketDataIn(OCSD_OP_RESET,index,0);
270 // reset the packet processor implmentation
271 if(!OCSD_DATA_RESP_IS_FATAL(resp))
275 if(m_pkt_raw_mon_i.hasAttachedAndEnabled())
276 m_pkt_raw_mon_i.first()->RawPacketDataMon(OCSD_OP_RESET,index,0,0,0);
281 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::Flush()
283 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
284 ocsd_datapath_resp_t resplocal = OCSD_RESP_CONT;
286 // the trace decoder attachment on main data path.
287 if(m_pkt_out_i.hasAttachedAndEnabled())
288 resp = m_pkt_out_i.first()->PacketDataIn(OCSD_OP_FLUSH,0,0); // flush up the data path first.
290 // if the connected components are flushed, not flush this one.
291 if(OCSD_DATA_RESP_IS_CONT(resp))
292 resplocal = onFlush(); // local flush
294 return (resplocal > resp) ? resplocal : resp;
297 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::EOT()
299 ocsd_datapath_resp_t resp = onEOT(); // local EOT - mark any part packet as incomplete type and prepare to send
301 // the trace decoder attachment on main data path.
302 if(m_pkt_out_i.hasAttachedAndEnabled() && !OCSD_DATA_RESP_IS_FATAL(resp))
303 resp = m_pkt_out_i.first()->PacketDataIn(OCSD_OP_EOT,0,0);
306 if(m_pkt_raw_mon_i.hasAttachedAndEnabled())
307 m_pkt_raw_mon_i.first()->RawPacketDataMon(OCSD_OP_EOT,0,0,0,0);
312 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::outputDecodedPacket(const ocsd_trc_index_t index, const P *pkt)
314 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
316 // bad packet filter.
317 if((getComponentOpMode() & OCSD_OPFLG_PKTPROC_NOFWD_BAD_PKTS) && isBadPacket())
320 // send a complete packet over the primary data path
321 if(m_pkt_out_i.hasAttachedAndEnabled())
322 resp = m_pkt_out_i.first()->PacketDataIn(OCSD_OP_DATA,index,pkt);
326 template<class P,class Pt, class Pc> void TrcPktProcBase<P, Pt, Pc>::outputRawPacketToMonitor(
327 const ocsd_trc_index_t index_sop,
330 const uint8_t *p_data)
332 // never output 0 sized packets.
336 // bad packet filter.
337 if((getComponentOpMode() & OCSD_OPFLG_PKTPROC_NOMON_BAD_PKTS) && isBadPacket())
340 // packet monitor - this cannot return CONT / WAIT, but does get the raw packet data.
341 if(m_pkt_raw_mon_i.hasAttachedAndEnabled())
342 m_pkt_raw_mon_i.first()->RawPacketDataMon(OCSD_OP_DATA,index_sop,pkt,size,p_data);
345 template<class P,class Pt, class Pc> const bool TrcPktProcBase<P, Pt, Pc>::hasRawMon() const
347 return m_pkt_raw_mon_i.hasAttachedAndEnabled();
350 template<class P,class Pt, class Pc> void TrcPktProcBase<P, Pt, Pc>::indexPacket(const ocsd_trc_index_t index_sop, const Pt *packet_type)
352 // packet indexer - cannot return CONT / WAIT, just gets the current index and type.
353 if(m_pkt_indexer_i.hasAttachedAndEnabled())
354 m_pkt_indexer_i.first()->TracePktIndex(index_sop,packet_type);
357 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::outputOnAllInterfaces(const ocsd_trc_index_t index_sop, const P *pkt, const Pt *pkt_type, std::vector<uint8_t> &pktdata)
359 indexPacket(index_sop,pkt_type);
360 if(pktdata.size() > 0) // prevent out of range errors for 0 length vector.
361 outputRawPacketToMonitor(index_sop,pkt,(uint32_t)pktdata.size(),&pktdata[0]);
362 return outputDecodedPacket(index_sop,pkt);
365 template<class P,class Pt, class Pc> ocsd_datapath_resp_t TrcPktProcBase<P, Pt, Pc>::outputOnAllInterfaces(const ocsd_trc_index_t index_sop, const P *pkt, const Pt *pkt_type, const uint8_t *pktdata, uint32_t pktlen)
367 indexPacket(index_sop,pkt_type);
368 outputRawPacketToMonitor(index_sop,pkt,pktlen,pktdata);
369 return outputDecodedPacket(index_sop,pkt);
372 template<class P,class Pt, class Pc> ocsd_err_t TrcPktProcBase<P, Pt, Pc>::setProtocolConfig(const Pc *config)
374 ocsd_err_t err = OCSD_ERR_INVALID_PARAM_VAL;
378 m_config = new (std::nothrow) Pc(*config);
380 err = onProtocolConfig();
387 template<class P,class Pt, class Pc> void TrcPktProcBase<P, Pt, Pc>::ClearConfigObj()
396 template<class P,class Pt, class Pc> const bool TrcPktProcBase<P, Pt, Pc>::checkInit()
400 if( (m_config != 0) &&
401 (m_pkt_out_i.hasAttached() || m_pkt_raw_mon_i.hasAttached())
410 #endif // ARM_TRC_PKT_PROC_BASE_H_INCLUDED
412 /* End of File trc_pkt_proc_base.h */